forked from lionsoul2014/ip2region
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathip2region.c
309 lines (244 loc) · 7.25 KB
/
ip2region.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2015 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: dongyado<[email protected]> |
+----------------------------------------------------------------------+
*/
/* $Id$ */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "lib/ip2region.h"
#include "php_ip2region.h"
#include <sys/time.h>
ZEND_DECLARE_MODULE_GLOBALS(ip2region)
/* True global resources - no need for thread safety here */
static int le_ip2region;
static ip2region_t g_resource_ptr;
static ip2region_entry g_resource;
// class
static zend_class_entry *ip2region_class_entry_ptr;
//static double getTime()
//{
// struct timeval tv;
// struct timezone tz;
// gettimeofday(&tv, &tz);
//
// return (tv.tv_sec * 1000 + ((double)tv.tv_usec)/1000);
//}
static void search(
ip2region_t g_resouce_ptr,
uint_t (*func_ptr) (ip2region_t, uint_t, datablock_t),
long ip,
zval ** return_value,
datablock_t _block)
{
int res = 0;
if (g_resource_ptr != NULL){
res = (*func_ptr)(g_resouce_ptr, (uint_t) ip, _block);
}
array_init( *return_value );
if ( res == 1 )
{
add_assoc_long( *return_value, "city_id", (*_block).city_id);
add_assoc_string( *return_value, "region", (*_block).region, 1);
} else {
add_assoc_long( *return_value, "city_id", 0);
add_assoc_string( *return_value, "region", "[Error] Search Failed! Please check the path of ip2region db file.", 1);
}
}
/* {{{ php_ip2region_init_globals
*/
static void php_ip2region_init_globals(zend_ip2region_globals *ip2region_globals)
{
ip2region_globals->db_file = NULL;
}
/* }}} */
/* {{{ PHP_INI
*/
PHP_INI_BEGIN()
STD_PHP_INI_ENTRY("ip2region.db_file", NULL, PHP_INI_ALL, OnUpdateString, db_file, zend_ip2region_globals, ip2region_globals)
PHP_INI_END()
/* }}} */
/* {{{
* btree search string
* */
PHP_METHOD(ip2region_class_entry_ptr, btreeSearchString)
{
char *ip = NULL;
int arg_len;
datablock_entry _block;
uint_t (*func_ptr) (ip2region_t, uint_t, datablock_t);
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &ip, &arg_len) == FAILURE) {
return;
}
func_ptr = ip2region_btree_search;
search(g_resource_ptr, func_ptr, ip2long(ip), &return_value, &_block);
}
/* }}} */
/* {{{
* btree search
* */
PHP_METHOD(ip2region_class_entry_ptr, btreeSearch)
{
long ip;
datablock_entry _block;
uint_t (*func_ptr) (ip2region_t, uint_t, datablock_t);
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &ip) == FAILURE) {
return;
}
func_ptr = ip2region_btree_search;
search(g_resource_ptr, func_ptr, ip, &return_value, &_block);
}
/* }}} */
/* {{{
* binary search string
* */
PHP_METHOD(ip2region_class_entry_ptr, binarySearchString)
{
char *ip = NULL;
int arg_len;
datablock_entry _block;
uint_t (*func_ptr) (ip2region_t, uint_t, datablock_t);
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &ip, &arg_len) == FAILURE) {
return;
}
func_ptr = ip2region_binary_search;
search(g_resource_ptr, func_ptr, ip2long(ip), &return_value, &_block);
}
/* }}} */
/* {{{
* binary search
* */
PHP_METHOD(ip2region_class_entry_ptr, binarySearch)
{
long ip;
datablock_entry _block;
uint_t (*func_ptr) (ip2region_t, uint_t, datablock_t);
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &ip) == FAILURE) {
return;
}
func_ptr = ip2region_binary_search;
search(g_resource_ptr, func_ptr, ip, &return_value, &_block);
}
/* }}} */
/* {{{ PHP_MSHUTDOWN_FUNCTION
*/
PHP_MSHUTDOWN_FUNCTION(ip2region)
{
UNREGISTER_INI_ENTRIES();
if(g_resource_ptr != NULL) ip2region_destroy(&g_resource);
return SUCCESS;
}
/* }}} */
/* Remove if there's nothing to do at request start */
/* {{{ PHP_RINIT_FUNCTION
*/
PHP_RINIT_FUNCTION(ip2region)
{
return SUCCESS;
}
/* }}} */
/* Remove if there's nothing to do at request end */
/* {{{ PHP_RSHUTDOWN_FUNCTION
*/
PHP_RSHUTDOWN_FUNCTION(ip2region)
{
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MINFO_FUNCTION
*/
PHP_MINFO_FUNCTION(ip2region)
{
php_info_print_table_start();
php_info_print_table_header(2, "ip2region support", "enabled");
php_info_print_table_end();
DISPLAY_INI_ENTRIES();
}
/* }}} */
/* {{{ ip2region_class_functions[]
*
* Every user visible function must have an entry in ip2region_functions[].
*/
const zend_function_entry ip2region_class_functions[] = {
PHP_ME( ip2region_class_entry_ptr, btreeSearchString, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
PHP_ME( ip2region_class_entry_ptr, binarySearchString, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC )
PHP_ME( ip2region_class_entry_ptr, btreeSearch, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
PHP_ME( ip2region_class_entry_ptr, binarySearch, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC )
PHP_FE_END /* Must be the last line in ip2region_functions[] */
};
/* }}} */
/* {{{ ip2region_functions[]
*
* Every user visible function must have an entry in ip2region_functions[].
*/
const zend_function_entry ip2region_functions[] = {
PHP_FE_END /* Must be the last line in ip2region_functions[] */
};
/* }}} */
/* {{{ PHP_MINIT_FUNCTION
*/
PHP_MINIT_FUNCTION(ip2region)
{
zend_class_entry ip2region_class_entry;
// @NOTE the line should before REGISTRE_INI_ENTRIES,
// and should not comment it when you have glocbals
ZEND_INIT_MODULE_GLOBALS(ip2region, php_ip2region_init_globals, NULL);
REGISTER_INI_ENTRIES();
char* _db_file = IP2REGION_G(db_file);
if ( _db_file == NULL || ip2region_create( &g_resource, _db_file) == 0)
{
g_resource_ptr = NULL;
} else {
g_resource_ptr = &g_resource;
}
//le_ip2region = zend_register_list_destructors_ex(
// ip2region_destruction_handler, NULL, le_ip2region_name, module_number);
// init class
INIT_CLASS_ENTRY( ip2region_class_entry, "ip2region", ip2region_class_functions);
ip2region_class_entry_ptr =
zend_register_internal_class(&ip2region_class_entry TSRMLS_CC);
return SUCCESS;
}
/* }}} */
/* {{{ ip2region_module_entry
*/
zend_module_entry ip2region_module_entry = {
STANDARD_MODULE_HEADER,
"ip2region",
ip2region_functions,
PHP_MINIT(ip2region),
PHP_MSHUTDOWN(ip2region),
PHP_RINIT(ip2region),
PHP_RSHUTDOWN(ip2region),
PHP_MINFO(ip2region),
PHP_IP2REGION_VERSION,
STANDARD_MODULE_PROPERTIES
};
/* }}} */
#ifdef COMPILE_DL_IP2REGION
ZEND_GET_MODULE(ip2region)
#endif
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
*/