-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmpl.c
267 lines (237 loc) · 7.07 KB
/
mpl.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
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2013 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: taogogo [email protected] |
+----------------------------------------------------------------------+
*/
/* $Id$ */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "php_mpl.h"
//hash map struct
#include "cmap.c"
struct StrHashTable tbl = {{0},NULL,NULL,simple_strhash,strcmp};
zval *cfg_value_uhm;
zval *cfg_value;
/* If you declare any globals in php_mpl.h uncomment this:
ZEND_DECLARE_MODULE_GLOBALS(mpl)
*/
/* True global resources - no need for thread safety here */
static int le_mpl;
/* {{{ mpl_methods[]
*/
const zend_function_entry mpl_methods[] = {
PHP_ME(Mpl, getLocation, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
PHP_FE_END /* Must be the last line in mpl_methods[] */
};
/* }}} */
/* {{{ mpl_module_entry
*/
zend_module_entry mpl_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
STANDARD_MODULE_HEADER,
#endif
"Mpl",
mpl_methods,
PHP_MINIT(mpl),
PHP_MSHUTDOWN(mpl),
PHP_RINIT(mpl), /* Replace with NULL if there's nothing to do at request start */
PHP_RSHUTDOWN(mpl), /* Replace with NULL if there's nothing to do at request end */
PHP_MINFO(mpl),
#if ZEND_MODULE_API_NO >= 20010901
"0.1", /* Replace with version number for your extension */
#endif
STANDARD_MODULE_PROPERTIES
};
/* }}} */
#ifdef COMPILE_DL_MPL
ZEND_GET_MODULE(mpl)
#endif
zend_class_entry *Mpl_ce;
/* {{{ PHP_MINIT_FUNCTION
*/
PHP_MINIT_FUNCTION(mpl)
{
REGISTER_STRING_CONSTANT("MPL_VERSION", "0.1", CONST_CS | CONST_PERSISTENT);
//REGISTER_INI_ENTRIES();
zend_class_entry Mpl_entry;
INIT_CLASS_ENTRY(Mpl_entry, "Mpl", mpl_methods);
Mpl_ce = zend_register_internal_class_ex(&Mpl_entry, NULL, NULL TSRMLS_CC);//注册类
//开启开关,则载入扩展时,读取数据载入内存
char *cfg_name_uhm="mpl_use_hashmap";
cfg_value_uhm = cfg_get_entry(cfg_name_uhm, strlen(cfg_name_uhm) + 1);
if(!cfg_value_uhm){
php_error_docref(NULL TSRMLS_CC, E_NOTICE, "mpl_use_hashmap should be set in php.ini");
return SUCCESS;
}
char *cfg_name="mpl_data_path";
cfg_value = cfg_get_entry(cfg_name, strlen(cfg_name) + 1);
if(!cfg_value){
php_error_docref(NULL TSRMLS_CC, E_NOTICE, "mpl_data_path should be set in php.ini");
return SUCCESS;
}
if(strcmp(Z_STRVAL_P(cfg_value_uhm), "1") == 0){
FILE *fp;
char phone_key[8];
char phone_value[100];
int pre_len = 7;
//hashmap
if((fp=fopen(Z_STRVAL_P(cfg_value),"r"))==NULL) {
php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Cannot open mbl data file");
return SUCCESS;
}else{
int line_no = 0;
while (fgets(phone_key,pre_len+1,fp)!=NULL) {
fseek(fp,-pre_len,SEEK_CUR);
fgets(phone_value,100,fp);
strncpy(phone_keys[line_no], phone_key, pre_len);
strncpy(phone_values[line_no], phone_value, 100);
cmap_insert(&tbl,phone_keys[line_no],phone_values[line_no]);
//this total cost 3+ seconds
//cmap_insert(&tbl,malloc_value(phone_key),malloc_value(phone_value));
line_no++;
}
}
}
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MSHUTDOWN_FUNCTION
*/
PHP_MSHUTDOWN_FUNCTION(mpl)
{
/* uncomment this line if you have INI entries
UNREGISTER_INI_ENTRIES();
*/
return SUCCESS;
}
/* }}} */
/* Remove if there's nothing to do at request start */
/* {{{ PHP_RINIT_FUNCTION
*/
PHP_RINIT_FUNCTION(mpl)
{
return SUCCESS;
}
/* }}} */
/* Remove if there's nothing to do at request end */
/* {{{ PHP_RSHUTDOWN_FUNCTION
*/
PHP_RSHUTDOWN_FUNCTION(mpl)
{
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MINFO_FUNCTION
*/
PHP_MINFO_FUNCTION(mpl)
{
php_info_print_table_start();
php_info_print_table_header(2, "mpl(mobile phone location extension) support", "enabled");
//读取常量
zval mpl_version;
if(zend_get_constant("MPL_VERSION", sizeof("MPL_VERSION")-1, &mpl_version TSRMLS_DC)){
php_info_print_table_row(2, "version", Z_STRVAL(mpl_version));
}
php_info_print_table_end();
}
/* }}} */
/* {{{ Mpl::getLocation
*/
PHP_METHOD(Mpl, getLocation) {
char *arg = NULL;
int arg_len;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) {
return;
}
FILE *fp;
char str[8];
char result[100];
char str_value[100];
int has_result = 0;
int pre_len = 7;
char phone_pre[8];
if(arg_len<7){
php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Argument length should be 7 or more");
return;
}
//zend_printf("debug:%s",result);
strncpy(phone_pre, arg, 7);
//开启开关,则载入扩展时,读取数据载入内存
if(strcmp(Z_STRVAL_P(cfg_value_uhm), "1") == 0){
if(cmap_get(&tbl,phone_pre)){
has_result = 1;
strncpy(result, (char*)cmap_get(&tbl,phone_pre), 100);
}
}else{
if((fp=fopen(Z_STRVAL_P(cfg_value),"r"))==NULL) {
php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Cannot open mbl data file");
return;
}else{
while (fgets(str,pre_len+1,fp)!=NULL) {
if(strcmp(str,phone_pre)==0){
fseek(fp,-pre_len,SEEK_CUR);
fgets(result,100,fp);
//zend_printf("debug:%s",result);
has_result = 1;
break;
}
}
fclose(fp);
}
}
if(has_result == 0){
//php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Cannot find this mobile phone number in data file");
return;
}else{
array_init(return_value);
char *delim = " ";
char *slice_value;
char value_keys[5][10] = {
"province",
"city",
"type",
"area_code",
"post_code",
};
//zend_printf("if %s,%s,%s",strstr(strtok(result, delim),phone_pre),strtok(result, delim),phone_pre);
if(strstr(strtok(result, delim),phone_pre)){
for(int i=0;i<5;i++){
slice_value = strtok(NULL, delim);
//zend_printf("v:%s",slice_value);
if(slice_value){
add_assoc_string(return_value,value_keys[i],slice_value,1);
}
}
}
}
}
/* }}} */
/* The previous line is meant for vim and emacs, so it can correctly fold and
unfold functions in source code. See the corresponding marks just before
function definition, where the functions purpose is also documented. Please
follow this convention for the convenience of others editing your code.
*/
/*
* 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
*/