geoip redirection for superforex.com.au
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

705 lines
23KB

  1. /* MaxMind, Inc., licenses this file to you under the Apache License, Version
  2. * 2.0 (the "License"); you may not use this file except in compliance with
  3. * the License. You may obtain a copy of the License at
  4. *
  5. * http://www.apache.org/licenses/LICENSE-2.0
  6. *
  7. * Unless required by applicable law or agreed to in writing, software
  8. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  9. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  10. * License for the specific language governing permissions and limitations
  11. * under the License.
  12. */
  13. #include "php_maxminddb.h"
  14. #ifdef HAVE_CONFIG_H
  15. #include "config.h"
  16. #endif
  17. #include <php.h>
  18. #include <zend.h>
  19. #include "Zend/zend_exceptions.h"
  20. #include "ext/standard/info.h"
  21. #include <maxminddb.h>
  22. #ifdef ZTS
  23. #include <TSRM.h>
  24. #endif
  25. #define __STDC_FORMAT_MACROS
  26. #include <inttypes.h>
  27. #define PHP_MAXMINDDB_NS ZEND_NS_NAME("MaxMind", "Db")
  28. #define PHP_MAXMINDDB_READER_NS ZEND_NS_NAME(PHP_MAXMINDDB_NS, "Reader")
  29. #define PHP_MAXMINDDB_READER_EX_NS \
  30. ZEND_NS_NAME(PHP_MAXMINDDB_READER_NS, "InvalidDatabaseException")
  31. #ifdef ZEND_ENGINE_3
  32. #define Z_MAXMINDDB_P(zv) php_maxminddb_fetch_object(Z_OBJ_P(zv))
  33. #define _ZVAL_STRING ZVAL_STRING
  34. #define _ZVAL_STRINGL ZVAL_STRINGL
  35. typedef size_t strsize_t;
  36. typedef zend_object free_obj_t;
  37. #else
  38. #define Z_MAXMINDDB_P(zv) \
  39. (maxminddb_obj *)zend_object_store_get_object(zv TSRMLS_CC)
  40. #define _ZVAL_STRING(a, b) ZVAL_STRING(a, b, 1)
  41. #define _ZVAL_STRINGL(a, b, c) ZVAL_STRINGL(a, b, c, 1)
  42. typedef int strsize_t;
  43. typedef void free_obj_t;
  44. #endif
  45. /* For PHP 8 compatibility */
  46. #ifndef TSRMLS_C
  47. #define TSRMLS_C
  48. #endif
  49. #ifndef TSRMLS_CC
  50. #define TSRMLS_CC
  51. #endif
  52. #ifndef TSRMLS_DC
  53. #define TSRMLS_DC
  54. #endif
  55. #ifndef ZEND_ACC_CTOR
  56. #define ZEND_ACC_CTOR 0
  57. #endif
  58. #ifdef ZEND_ENGINE_3
  59. typedef struct _maxminddb_obj {
  60. MMDB_s *mmdb;
  61. zend_object std;
  62. } maxminddb_obj;
  63. #else
  64. typedef struct _maxminddb_obj {
  65. zend_object std;
  66. MMDB_s *mmdb;
  67. } maxminddb_obj;
  68. #endif
  69. PHP_FUNCTION(maxminddb);
  70. static int
  71. get_record(INTERNAL_FUNCTION_PARAMETERS, zval *record, int *prefix_len);
  72. static const MMDB_entry_data_list_s *
  73. handle_entry_data_list(const MMDB_entry_data_list_s *entry_data_list,
  74. zval *z_value TSRMLS_DC);
  75. static const MMDB_entry_data_list_s *
  76. handle_array(const MMDB_entry_data_list_s *entry_data_list,
  77. zval *z_value TSRMLS_DC);
  78. static const MMDB_entry_data_list_s *
  79. handle_map(const MMDB_entry_data_list_s *entry_data_list,
  80. zval *z_value TSRMLS_DC);
  81. static void handle_uint128(const MMDB_entry_data_list_s *entry_data_list,
  82. zval *z_value TSRMLS_DC);
  83. static void handle_uint64(const MMDB_entry_data_list_s *entry_data_list,
  84. zval *z_value TSRMLS_DC);
  85. static void handle_uint32(const MMDB_entry_data_list_s *entry_data_list,
  86. zval *z_value TSRMLS_DC);
  87. static zend_class_entry *lookup_class(const char *name TSRMLS_DC);
  88. #define CHECK_ALLOCATED(val) \
  89. if (!val) { \
  90. zend_error(E_ERROR, "Out of memory"); \
  91. return; \
  92. }
  93. #define THROW_EXCEPTION(name, ...) \
  94. { \
  95. zend_class_entry *exception_ce = lookup_class(name TSRMLS_CC); \
  96. zend_throw_exception_ex(exception_ce, 0 TSRMLS_CC, __VA_ARGS__); \
  97. }
  98. #if PHP_VERSION_ID < 50399
  99. #define object_properties_init(zo, class_type) \
  100. { \
  101. zval *tmp; \
  102. zend_hash_copy((*zo).properties, \
  103. &class_type->default_properties, \
  104. (copy_ctor_func_t)zval_add_ref, \
  105. (void *)&tmp, \
  106. sizeof(zval *)); \
  107. }
  108. #endif
  109. static zend_object_handlers maxminddb_obj_handlers;
  110. static zend_class_entry *maxminddb_ce;
  111. static inline maxminddb_obj *
  112. php_maxminddb_fetch_object(zend_object *obj TSRMLS_DC) {
  113. #ifdef ZEND_ENGINE_3
  114. return (maxminddb_obj *)((char *)(obj)-XtOffsetOf(maxminddb_obj, std));
  115. #else
  116. return (maxminddb_obj *)obj;
  117. #endif
  118. }
  119. ZEND_BEGIN_ARG_INFO_EX(arginfo_maxmindbreader_construct, 0, 0, 1)
  120. ZEND_ARG_INFO(0, db_file)
  121. ZEND_END_ARG_INFO()
  122. PHP_METHOD(MaxMind_Db_Reader, __construct) {
  123. char *db_file = NULL;
  124. strsize_t name_len;
  125. zval *_this_zval = NULL;
  126. if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
  127. getThis(),
  128. "Os",
  129. &_this_zval,
  130. maxminddb_ce,
  131. &db_file,
  132. &name_len) == FAILURE) {
  133. THROW_EXCEPTION("InvalidArgumentException",
  134. "The constructor takes exactly one argument.");
  135. return;
  136. }
  137. if (0 != php_check_open_basedir(db_file TSRMLS_CC) ||
  138. 0 != access(db_file, R_OK)) {
  139. THROW_EXCEPTION("InvalidArgumentException",
  140. "The file \"%s\" does not exist or is not readable.",
  141. db_file);
  142. return;
  143. }
  144. MMDB_s *mmdb = (MMDB_s *)ecalloc(1, sizeof(MMDB_s));
  145. uint16_t status = MMDB_open(db_file, MMDB_MODE_MMAP, mmdb);
  146. if (MMDB_SUCCESS != status) {
  147. THROW_EXCEPTION(PHP_MAXMINDDB_READER_EX_NS,
  148. "Error opening database file (%s). Is this a valid "
  149. "MaxMind DB file?",
  150. db_file);
  151. efree(mmdb);
  152. return;
  153. }
  154. maxminddb_obj *mmdb_obj = Z_MAXMINDDB_P(getThis());
  155. mmdb_obj->mmdb = mmdb;
  156. }
  157. ZEND_BEGIN_ARG_INFO_EX(arginfo_maxmindbreader_get, 0, 0, 1)
  158. ZEND_ARG_INFO(0, ip_address)
  159. ZEND_END_ARG_INFO()
  160. PHP_METHOD(MaxMind_Db_Reader, get) {
  161. int prefix_len = 0;
  162. get_record(INTERNAL_FUNCTION_PARAM_PASSTHRU, return_value, &prefix_len);
  163. }
  164. PHP_METHOD(MaxMind_Db_Reader, getWithPrefixLen) {
  165. zval *record, *z_prefix_len;
  166. #ifdef ZEND_ENGINE_3
  167. zval _record, _z_prefix_len;
  168. record = &_record;
  169. z_prefix_len = &_z_prefix_len;
  170. #else
  171. ALLOC_INIT_ZVAL(record);
  172. ALLOC_INIT_ZVAL(z_prefix_len);
  173. #endif
  174. int prefix_len = 0;
  175. if (get_record(INTERNAL_FUNCTION_PARAM_PASSTHRU, record, &prefix_len)) {
  176. return;
  177. }
  178. array_init(return_value);
  179. add_next_index_zval(return_value, record);
  180. ZVAL_LONG(z_prefix_len, prefix_len);
  181. add_next_index_zval(return_value, z_prefix_len);
  182. }
  183. static int
  184. get_record(INTERNAL_FUNCTION_PARAMETERS, zval *record, int *prefix_len) {
  185. char *ip_address = NULL;
  186. strsize_t name_len;
  187. zval *_this_zval = NULL;
  188. if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
  189. getThis(),
  190. "Os",
  191. &_this_zval,
  192. maxminddb_ce,
  193. &ip_address,
  194. &name_len) == FAILURE) {
  195. THROW_EXCEPTION("InvalidArgumentException",
  196. "Method takes exactly one argument.");
  197. return 1;
  198. }
  199. const maxminddb_obj *mmdb_obj = (maxminddb_obj *)Z_MAXMINDDB_P(getThis());
  200. MMDB_s *mmdb = mmdb_obj->mmdb;
  201. if (NULL == mmdb) {
  202. THROW_EXCEPTION("BadMethodCallException",
  203. "Attempt to read from a closed MaxMind DB.");
  204. return 1;
  205. }
  206. struct addrinfo hints = {
  207. .ai_family = AF_UNSPEC,
  208. .ai_flags = AI_NUMERICHOST,
  209. // We set ai_socktype so that we only get one result back
  210. .ai_socktype = SOCK_STREAM};
  211. struct addrinfo *addresses = NULL;
  212. int gai_status = getaddrinfo(ip_address, NULL, &hints, &addresses);
  213. if (gai_status) {
  214. THROW_EXCEPTION("InvalidArgumentException",
  215. "The value \"%s\" is not a valid IP address.",
  216. ip_address);
  217. return 1;
  218. }
  219. if (!addresses || !addresses->ai_addr) {
  220. THROW_EXCEPTION(
  221. "InvalidArgumentException",
  222. "getaddrinfo was successful but failed to set the addrinfo");
  223. return 1;
  224. }
  225. int sa_family = addresses->ai_addr->sa_family;
  226. int mmdb_error = MMDB_SUCCESS;
  227. MMDB_lookup_result_s result =
  228. MMDB_lookup_sockaddr(mmdb, addresses->ai_addr, &mmdb_error);
  229. freeaddrinfo(addresses);
  230. if (MMDB_SUCCESS != mmdb_error) {
  231. char *exception_name;
  232. if (MMDB_IPV6_LOOKUP_IN_IPV4_DATABASE_ERROR == mmdb_error) {
  233. exception_name = "InvalidArgumentException";
  234. } else {
  235. exception_name = PHP_MAXMINDDB_READER_EX_NS;
  236. }
  237. THROW_EXCEPTION(exception_name,
  238. "Error looking up %s. %s",
  239. ip_address,
  240. MMDB_strerror(mmdb_error));
  241. return 1;
  242. }
  243. *prefix_len = result.netmask;
  244. if (sa_family == AF_INET && mmdb->metadata.ip_version == 6) {
  245. // We return the prefix length given the IPv4 address. If there is
  246. // no IPv4 subtree, we return a prefix length of 0.
  247. *prefix_len = *prefix_len >= 96 ? *prefix_len - 96 : 0;
  248. }
  249. if (!result.found_entry) {
  250. ZVAL_NULL(record);
  251. return 0;
  252. }
  253. MMDB_entry_data_list_s *entry_data_list = NULL;
  254. int status = MMDB_get_entry_data_list(&result.entry, &entry_data_list);
  255. if (MMDB_SUCCESS != status) {
  256. THROW_EXCEPTION(PHP_MAXMINDDB_READER_EX_NS,
  257. "Error while looking up data for %s. %s",
  258. ip_address,
  259. MMDB_strerror(status));
  260. MMDB_free_entry_data_list(entry_data_list);
  261. return 1;
  262. } else if (NULL == entry_data_list) {
  263. THROW_EXCEPTION(PHP_MAXMINDDB_READER_EX_NS,
  264. "Error while looking up data for %s. Your database may "
  265. "be corrupt or you have found a bug in libmaxminddb.",
  266. ip_address);
  267. return 1;
  268. }
  269. handle_entry_data_list(entry_data_list, record TSRMLS_CC);
  270. MMDB_free_entry_data_list(entry_data_list);
  271. return 0;
  272. }
  273. ZEND_BEGIN_ARG_INFO_EX(arginfo_maxmindbreader_void, 0, 0, 0)
  274. ZEND_END_ARG_INFO()
  275. PHP_METHOD(MaxMind_Db_Reader, metadata) {
  276. if (ZEND_NUM_ARGS() != 0) {
  277. THROW_EXCEPTION("InvalidArgumentException",
  278. "Method takes no arguments.");
  279. return;
  280. }
  281. const maxminddb_obj *const mmdb_obj =
  282. (maxminddb_obj *)Z_MAXMINDDB_P(getThis());
  283. if (NULL == mmdb_obj->mmdb) {
  284. THROW_EXCEPTION("BadMethodCallException",
  285. "Attempt to read from a closed MaxMind DB.");
  286. return;
  287. }
  288. const char *const name = ZEND_NS_NAME(PHP_MAXMINDDB_READER_NS, "Metadata");
  289. zend_class_entry *metadata_ce = lookup_class(name TSRMLS_CC);
  290. object_init_ex(return_value, metadata_ce);
  291. #ifdef ZEND_ENGINE_3
  292. zval _metadata_array;
  293. zval *metadata_array = &_metadata_array;
  294. ZVAL_NULL(metadata_array);
  295. #else
  296. zval *metadata_array;
  297. ALLOC_INIT_ZVAL(metadata_array);
  298. #endif
  299. MMDB_entry_data_list_s *entry_data_list;
  300. MMDB_get_metadata_as_entry_data_list(mmdb_obj->mmdb, &entry_data_list);
  301. handle_entry_data_list(entry_data_list, metadata_array TSRMLS_CC);
  302. MMDB_free_entry_data_list(entry_data_list);
  303. #if PHP_VERSION_ID >= 80000
  304. zend_call_method_with_1_params(Z_OBJ_P(return_value),
  305. metadata_ce,
  306. &metadata_ce->constructor,
  307. ZEND_CONSTRUCTOR_FUNC_NAME,
  308. NULL,
  309. metadata_array);
  310. zval_ptr_dtor(metadata_array);
  311. #elif defined(ZEND_ENGINE_3)
  312. zend_call_method_with_1_params(return_value,
  313. metadata_ce,
  314. &metadata_ce->constructor,
  315. ZEND_CONSTRUCTOR_FUNC_NAME,
  316. NULL,
  317. metadata_array);
  318. zval_ptr_dtor(metadata_array);
  319. #else
  320. zend_call_method_with_1_params(&return_value,
  321. metadata_ce,
  322. &metadata_ce->constructor,
  323. ZEND_CONSTRUCTOR_FUNC_NAME,
  324. NULL,
  325. metadata_array);
  326. zval_ptr_dtor(&metadata_array);
  327. #endif
  328. }
  329. PHP_METHOD(MaxMind_Db_Reader, close) {
  330. if (ZEND_NUM_ARGS() != 0) {
  331. THROW_EXCEPTION("InvalidArgumentException",
  332. "Method takes no arguments.");
  333. return;
  334. }
  335. maxminddb_obj *mmdb_obj = (maxminddb_obj *)Z_MAXMINDDB_P(getThis());
  336. if (NULL == mmdb_obj->mmdb) {
  337. THROW_EXCEPTION("BadMethodCallException",
  338. "Attempt to close a closed MaxMind DB.");
  339. return;
  340. }
  341. MMDB_close(mmdb_obj->mmdb);
  342. efree(mmdb_obj->mmdb);
  343. mmdb_obj->mmdb = NULL;
  344. }
  345. static const MMDB_entry_data_list_s *
  346. handle_entry_data_list(const MMDB_entry_data_list_s *entry_data_list,
  347. zval *z_value TSRMLS_DC) {
  348. switch (entry_data_list->entry_data.type) {
  349. case MMDB_DATA_TYPE_MAP:
  350. return handle_map(entry_data_list, z_value TSRMLS_CC);
  351. case MMDB_DATA_TYPE_ARRAY:
  352. return handle_array(entry_data_list, z_value TSRMLS_CC);
  353. case MMDB_DATA_TYPE_UTF8_STRING:
  354. _ZVAL_STRINGL(z_value,
  355. (char *)entry_data_list->entry_data.utf8_string,
  356. entry_data_list->entry_data.data_size);
  357. break;
  358. case MMDB_DATA_TYPE_BYTES:
  359. _ZVAL_STRINGL(z_value,
  360. (char *)entry_data_list->entry_data.bytes,
  361. entry_data_list->entry_data.data_size);
  362. break;
  363. case MMDB_DATA_TYPE_DOUBLE:
  364. ZVAL_DOUBLE(z_value, entry_data_list->entry_data.double_value);
  365. break;
  366. case MMDB_DATA_TYPE_FLOAT:
  367. ZVAL_DOUBLE(z_value, entry_data_list->entry_data.float_value);
  368. break;
  369. case MMDB_DATA_TYPE_UINT16:
  370. ZVAL_LONG(z_value, entry_data_list->entry_data.uint16);
  371. break;
  372. case MMDB_DATA_TYPE_UINT32:
  373. handle_uint32(entry_data_list, z_value TSRMLS_CC);
  374. break;
  375. case MMDB_DATA_TYPE_BOOLEAN:
  376. ZVAL_BOOL(z_value, entry_data_list->entry_data.boolean);
  377. break;
  378. case MMDB_DATA_TYPE_UINT64:
  379. handle_uint64(entry_data_list, z_value TSRMLS_CC);
  380. break;
  381. case MMDB_DATA_TYPE_UINT128:
  382. handle_uint128(entry_data_list, z_value TSRMLS_CC);
  383. break;
  384. case MMDB_DATA_TYPE_INT32:
  385. ZVAL_LONG(z_value, entry_data_list->entry_data.int32);
  386. break;
  387. default:
  388. THROW_EXCEPTION(PHP_MAXMINDDB_READER_EX_NS,
  389. "Invalid data type arguments: %d",
  390. entry_data_list->entry_data.type);
  391. return NULL;
  392. }
  393. return entry_data_list;
  394. }
  395. static const MMDB_entry_data_list_s *
  396. handle_map(const MMDB_entry_data_list_s *entry_data_list,
  397. zval *z_value TSRMLS_DC) {
  398. array_init(z_value);
  399. const uint32_t map_size = entry_data_list->entry_data.data_size;
  400. uint i;
  401. for (i = 0; i < map_size && entry_data_list; i++) {
  402. entry_data_list = entry_data_list->next;
  403. char *key = estrndup((char *)entry_data_list->entry_data.utf8_string,
  404. entry_data_list->entry_data.data_size);
  405. if (NULL == key) {
  406. THROW_EXCEPTION(PHP_MAXMINDDB_READER_EX_NS,
  407. "Invalid data type arguments");
  408. return NULL;
  409. }
  410. entry_data_list = entry_data_list->next;
  411. #ifdef ZEND_ENGINE_3
  412. zval _new_value;
  413. zval *new_value = &_new_value;
  414. ZVAL_NULL(new_value);
  415. #else
  416. zval *new_value;
  417. ALLOC_INIT_ZVAL(new_value);
  418. #endif
  419. entry_data_list =
  420. handle_entry_data_list(entry_data_list, new_value TSRMLS_CC);
  421. add_assoc_zval(z_value, key, new_value);
  422. efree(key);
  423. }
  424. return entry_data_list;
  425. }
  426. static const MMDB_entry_data_list_s *
  427. handle_array(const MMDB_entry_data_list_s *entry_data_list,
  428. zval *z_value TSRMLS_DC) {
  429. const uint32_t size = entry_data_list->entry_data.data_size;
  430. array_init(z_value);
  431. uint i;
  432. for (i = 0; i < size && entry_data_list; i++) {
  433. entry_data_list = entry_data_list->next;
  434. #ifdef ZEND_ENGINE_3
  435. zval _new_value;
  436. zval *new_value = &_new_value;
  437. ZVAL_NULL(new_value);
  438. #else
  439. zval *new_value;
  440. ALLOC_INIT_ZVAL(new_value);
  441. #endif
  442. entry_data_list =
  443. handle_entry_data_list(entry_data_list, new_value TSRMLS_CC);
  444. add_next_index_zval(z_value, new_value);
  445. }
  446. return entry_data_list;
  447. }
  448. static void handle_uint128(const MMDB_entry_data_list_s *entry_data_list,
  449. zval *z_value TSRMLS_DC) {
  450. uint64_t high = 0;
  451. uint64_t low = 0;
  452. #if MMDB_UINT128_IS_BYTE_ARRAY
  453. int i;
  454. for (i = 0; i < 8; i++) {
  455. high = (high << 8) | entry_data_list->entry_data.uint128[i];
  456. }
  457. for (i = 8; i < 16; i++) {
  458. low = (low << 8) | entry_data_list->entry_data.uint128[i];
  459. }
  460. #else
  461. high = entry_data_list->entry_data.uint128 >> 64;
  462. low = (uint64_t)entry_data_list->entry_data.uint128;
  463. #endif
  464. char *num_str;
  465. spprintf(&num_str, 0, "0x%016" PRIX64 "%016" PRIX64, high, low);
  466. CHECK_ALLOCATED(num_str);
  467. _ZVAL_STRING(z_value, num_str);
  468. efree(num_str);
  469. }
  470. static void handle_uint32(const MMDB_entry_data_list_s *entry_data_list,
  471. zval *z_value TSRMLS_DC) {
  472. uint32_t val = entry_data_list->entry_data.uint32;
  473. #if LONG_MAX >= UINT32_MAX
  474. ZVAL_LONG(z_value, val);
  475. return;
  476. #else
  477. if (val <= LONG_MAX) {
  478. ZVAL_LONG(z_value, val);
  479. return;
  480. }
  481. char *int_str;
  482. spprintf(&int_str, 0, "%" PRIu32, val);
  483. CHECK_ALLOCATED(int_str);
  484. _ZVAL_STRING(z_value, int_str);
  485. efree(int_str);
  486. #endif
  487. }
  488. static void handle_uint64(const MMDB_entry_data_list_s *entry_data_list,
  489. zval *z_value TSRMLS_DC) {
  490. uint64_t val = entry_data_list->entry_data.uint64;
  491. #if LONG_MAX >= UINT64_MAX
  492. ZVAL_LONG(z_value, val);
  493. return;
  494. #else
  495. if (val <= LONG_MAX) {
  496. ZVAL_LONG(z_value, val);
  497. return;
  498. }
  499. char *int_str;
  500. spprintf(&int_str, 0, "%" PRIu64, val);
  501. CHECK_ALLOCATED(int_str);
  502. _ZVAL_STRING(z_value, int_str);
  503. efree(int_str);
  504. #endif
  505. }
  506. static zend_class_entry *lookup_class(const char *name TSRMLS_DC) {
  507. #ifdef ZEND_ENGINE_3
  508. zend_string *n = zend_string_init(name, strlen(name), 0);
  509. zend_class_entry *ce = zend_lookup_class(n);
  510. zend_string_release(n);
  511. if (NULL == ce) {
  512. zend_error(E_ERROR, "Class %s not found", name);
  513. }
  514. return ce;
  515. #else
  516. zend_class_entry **ce;
  517. if (FAILURE == zend_lookup_class(name, strlen(name), &ce TSRMLS_CC)) {
  518. zend_error(E_ERROR, "Class %s not found", name);
  519. }
  520. return *ce;
  521. #endif
  522. }
  523. static void maxminddb_free_storage(free_obj_t *object TSRMLS_DC) {
  524. maxminddb_obj *obj =
  525. php_maxminddb_fetch_object((zend_object *)object TSRMLS_CC);
  526. if (obj->mmdb != NULL) {
  527. MMDB_close(obj->mmdb);
  528. efree(obj->mmdb);
  529. }
  530. zend_object_std_dtor(&obj->std TSRMLS_CC);
  531. #ifndef ZEND_ENGINE_3
  532. efree(object);
  533. #endif
  534. }
  535. #ifdef ZEND_ENGINE_3
  536. static zend_object *maxminddb_create_handler(zend_class_entry *type TSRMLS_DC) {
  537. maxminddb_obj *obj = (maxminddb_obj *)ecalloc(1, sizeof(maxminddb_obj));
  538. zend_object_std_init(&obj->std, type TSRMLS_CC);
  539. object_properties_init(&(obj->std), type);
  540. obj->std.handlers = &maxminddb_obj_handlers;
  541. return &obj->std;
  542. }
  543. #else
  544. static zend_object_value
  545. maxminddb_create_handler(zend_class_entry *type TSRMLS_DC) {
  546. zend_object_value retval;
  547. maxminddb_obj *obj = (maxminddb_obj *)ecalloc(1, sizeof(maxminddb_obj));
  548. zend_object_std_init(&obj->std, type TSRMLS_CC);
  549. object_properties_init(&(obj->std), type);
  550. retval.handle = zend_objects_store_put(
  551. obj, NULL, maxminddb_free_storage, NULL TSRMLS_CC);
  552. retval.handlers = &maxminddb_obj_handlers;
  553. return retval;
  554. }
  555. #endif
  556. // clang-format off
  557. static zend_function_entry maxminddb_methods[] = {
  558. PHP_ME(MaxMind_Db_Reader, __construct, arginfo_maxmindbreader_construct,
  559. ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
  560. PHP_ME(MaxMind_Db_Reader, close, arginfo_maxmindbreader_void, ZEND_ACC_PUBLIC)
  561. PHP_ME(MaxMind_Db_Reader, get, arginfo_maxmindbreader_get, ZEND_ACC_PUBLIC)
  562. PHP_ME(MaxMind_Db_Reader, getWithPrefixLen, arginfo_maxmindbreader_get, ZEND_ACC_PUBLIC)
  563. PHP_ME(MaxMind_Db_Reader, metadata, arginfo_maxmindbreader_void, ZEND_ACC_PUBLIC)
  564. { NULL, NULL, NULL }
  565. };
  566. // clang-format on
  567. PHP_MINIT_FUNCTION(maxminddb) {
  568. zend_class_entry ce;
  569. INIT_CLASS_ENTRY(ce, PHP_MAXMINDDB_READER_NS, maxminddb_methods);
  570. maxminddb_ce = zend_register_internal_class(&ce TSRMLS_CC);
  571. maxminddb_ce->create_object = maxminddb_create_handler;
  572. memcpy(&maxminddb_obj_handlers,
  573. zend_get_std_object_handlers(),
  574. sizeof(zend_object_handlers));
  575. maxminddb_obj_handlers.clone_obj = NULL;
  576. #ifdef ZEND_ENGINE_3
  577. maxminddb_obj_handlers.offset = XtOffsetOf(maxminddb_obj, std);
  578. maxminddb_obj_handlers.free_obj = maxminddb_free_storage;
  579. #endif
  580. zend_declare_class_constant_string(maxminddb_ce,
  581. "MMDB_LIB_VERSION",
  582. sizeof("MMDB_LIB_VERSION") - 1,
  583. MMDB_lib_version() TSRMLS_CC);
  584. return SUCCESS;
  585. }
  586. static PHP_MINFO_FUNCTION(maxminddb) {
  587. php_info_print_table_start();
  588. php_info_print_table_row(2, "MaxMind DB Reader", "enabled");
  589. php_info_print_table_row(
  590. 2, "maxminddb extension version", PHP_MAXMINDDB_VERSION);
  591. php_info_print_table_row(
  592. 2, "libmaxminddb library version", MMDB_lib_version());
  593. php_info_print_table_end();
  594. }
  595. zend_module_entry maxminddb_module_entry = {STANDARD_MODULE_HEADER,
  596. PHP_MAXMINDDB_EXTNAME,
  597. NULL,
  598. PHP_MINIT(maxminddb),
  599. NULL,
  600. NULL,
  601. NULL,
  602. PHP_MINFO(maxminddb),
  603. PHP_MAXMINDDB_VERSION,
  604. STANDARD_MODULE_PROPERTIES};
  605. #ifdef COMPILE_DL_MAXMINDDB
  606. ZEND_GET_MODULE(maxminddb)
  607. #endif