forked from fengve/zyconf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zyconf.c
627 lines (499 loc) · 18.9 KB
/
zyconf.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
/*
+----------------------------------------------------------------------+
| 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 |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: |
+----------------------------------------------------------------------+
*/
/* $Id$ */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "php_zyconf.h"
#include "main/php_scandir.h"
ZEND_DECLARE_MODULE_GLOBALS(zyconf)
static int le_zyconf;
static int zyconf_update_interval = 15;//15s
zend_class_entry *zyconf_ce;
HashTable *zyconf_file_name;
HashTable *parsed_ini_files;
typedef struct _zyconf_filenode {
char *filename;
time_t mtime;
} zyconf_filenode;
#define PALLOC_HASHTABLE(ht) do { \
(ht) = (HashTable*)pemalloc(sizeof(HashTable), 1); \
if ((ht) == NULL) { \
zend_error(E_ERROR, "Cannot allocate HashTable"); \
} \
\
} while(0)
//使用持久化内存
#define ZYCONF_MAKE_STD_ZVAL(zv) do{ \
(zv) = (zval *) pemalloc(sizeof(zval), 1); \
INIT_PZVAL(zv); \
} while (0)
ZEND_BEGIN_ARG_INFO_EX(php_zyconf_get_arginfo, 0, 0, 2)
ZEND_ARG_INFO(0, project)
ZEND_ARG_INFO(0, name)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(php_zyconf_has_arginfo, 0, 0, 2)
ZEND_ARG_INFO(0, project)
ZEND_ARG_INFO(0, name)
ZEND_END_ARG_INFO()
const zend_function_entry zyconf_methods[]={
PHP_ME(zyconf, get, php_zyconf_get_arginfo, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
PHP_ME(zyconf, has, php_zyconf_has_arginfo, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
PHP_FE_END
};
/* {{{ zyconf_module_entry
*/
zend_module_entry zyconf_module_entry = {
STANDARD_MODULE_HEADER,
"zyconf",
NULL,
PHP_MINIT(zyconf),
PHP_MSHUTDOWN(zyconf),
PHP_RINIT(zyconf), /* Replace with NULL if there's nothing to do at request start */
PHP_RSHUTDOWN(zyconf), /* Replace with NULL if there's nothing to do at request end */
PHP_MINFO(zyconf),
ZYCONF_VERSION,
STANDARD_MODULE_PROPERTIES
};
/* }}} */
#ifdef COMPILE_DL_ZYCONF
ZEND_GET_MODULE(zyconf)
#endif
PHP_INI_BEGIN()
STD_PHP_INI_ENTRY("zyconf.directory", "", PHP_INI_ALL, OnUpdateString, directory, zend_zyconf_globals, zyconf_globals)
PHP_INI_END()
static void php_zyconf_init_globals(zend_zyconf_globals *zyconf_globals)
{
zyconf_globals->directory = NULL;
}
PHP_MINIT_FUNCTION(zyconf)
{
zend_class_entry ce;
INIT_CLASS_ENTRY(ce, "zyconf", zyconf_methods);
zyconf_ce = zend_register_internal_class(&ce TSRMLS_CC);
zend_declare_class_constant_string(zyconf_ce, "version", strlen("version"), ZYCONF_VERSION TSRMLS_CC);
REGISTER_INI_ENTRIES();
const char *dirname;
size_t dirlen;
if ((dirname = ZYCONF_G(directory)) && (dirlen = strlen(dirname))){
zval *configs;
int ndir;
struct dirent **namelist;
char *p, ini_file[MAXPATHLEN];
if ((ndir = php_scandir(dirname, &namelist, 0, php_alphasort)) > 0) {
int i;
struct stat sb;
zend_file_handle fh = {0};
PALLOC_HASHTABLE(zyconf_file_name);
zend_hash_init(zyconf_file_name, ndir-2, NULL, NULL, 1);
PALLOC_HASHTABLE(parsed_ini_files);
zend_hash_init(parsed_ini_files, ndir-2, NULL, NULL, 1);
for(i=0; i<ndir; i++){
if (!(p = strrchr(namelist[i]->d_name, '.')) || strcmp(p, ".ini")) {
free(namelist[i]);
continue;
}
snprintf(ini_file, MAXPATHLEN, "%s%c%s", dirname, DEFAULT_SLASH, namelist[i]->d_name);
if(VCWD_STAT(ini_file, &sb) == 0){
if (S_ISREG(sb.st_mode)){
zyconf_filenode *node;
if ((fh.handle.fp = VCWD_FOPEN(ini_file, "r"))){
fh.filename = ini_file;
fh.type = ZEND_HANDLE_FP;
//MAKE_STD_ZVAL(configs);
//ZVAL_NULL(configs);
//array_init(configs);
ZYCONF_MAKE_STD_ZVAL(configs);
php_zyconf_hash_init(configs, 8);
if (zend_parse_ini_file(&fh, 0, 0, php_zyconf_ini_parser_cb, configs TSRMLS_CC) == FAILURE
|| ZYCONF_G(parse_err)) {
if (!ZYCONF_G(parse_err)) {
php_error(E_WARNING, "Parsing '%s' failed", ini_file);
}
zval_ptr_dtor(&configs);
ZYCONF_G(parse_err) = 0;
free(namelist[i]);
continue;
}
zend_symtable_update(zyconf_file_name, namelist[i]->d_name, strlen(namelist[i]->d_name)+1, (void *)&configs, sizeof(zval *), NULL);
node = pemalloc(sizeof(zyconf_filenode), 1);
node->filename = pestrndup(namelist[i]->d_name, strlen(namelist[i]->d_name), 1);
node->mtime = sb.st_mtime;
zend_symtable_update(parsed_ini_files, node->filename, strlen(namelist[i]->d_name)+1, (void *)&node, sizeof(zyconf_filenode *), NULL);
/*
zval **ppzval,**ppzval2;
zend_symtable_find(zyconf_file_name, namelist[i]->d_name, strlen(namelist[i]->d_name)+1, (void **)&ppzval);
php_printf(" %s, %d, %p, %p\n", namelist[i]->d_name, strlen(namelist[i]->d_name)+1, ppzval, Z_ARRVAL_PP(ppzval));
zend_symtable_find(Z_ARRVAL_P(configs), "cps_rest_cps", strlen("cps_rest_cps")+1, (void **)&ppzval2);
php_printf(" %d, %s, %p\n", Z_STRLEN_PP(ppzval2), Z_STRVAL_PP(ppzval2), &Z_STRVAL_PP(ppzval2));
zend_symtable_find(Z_ARRVAL_PP(ppzval), "cps_rest_cps", strlen("cps_rest_cps")+1, (void **)&ppzval2);
php_printf(" %p, %s ,%d\n", Z_STRVAL_PP(ppzval2), Z_STRVAL_PP(ppzval2), Z_STRLEN_PP(ppzval2));
*/
}else{
php_error(E_ERROR, " Cloud not open %s ", ini_file);
}
}
}else{
php_error(E_ERROR, " Cloud not stat %s ", ini_file);
}
}
ZYCONF_G(last_check) = time(NULL);
}else{
php_error(E_ERROR, " Cloud not opendir %s ", dirname);
}
}else{
php_error(E_ERROR, " In php.ini zyconf.directory cat not empty ");
}
return SUCCESS;
}
PHP_MSHUTDOWN_FUNCTION(zyconf)
{
UNREGISTER_INI_ENTRIES();
if(zyconf_file_name){
php_printf("\nPHP_MSHUTDOWN_FUNCTION\n");
//php_zyconf_hash_destroy(zyconf_file_name);
}
php_printf("\nPHP_MSHUTDOWN_FUNCTION222222\n");
return SUCCESS;
}
PHP_RINIT_FUNCTION(zyconf)
{
time_t curr_time = time(NULL);
//在 $_SERVER 中获取 REQUEST_TIME
//zval **request_time;
//zend_hash_find(HASH_OF(PG(http_globals)[TRACK_VARS_SERVER]), ZEND_STRS("REQUEST_TIME"), (void **)&request_time);
//不到更新时间
if(curr_time - ZYCONF_G(last_check) < zyconf_update_interval){
return SUCCESS;
}
ZYCONF_G(last_check) = curr_time;
const char *dirname;
size_t dirlen;
if ((dirname = ZYCONF_G(directory)) && (dirlen = strlen(dirname))){
zval *configs;
int ndir;
struct dirent **namelist;
char *p, ini_file[MAXPATHLEN];
zyconf_filenode **node, *node_new;
if ((ndir = php_scandir(dirname, &namelist, 0, php_alphasort)) > 0) {
int i;
struct stat sb;
zend_file_handle fh = {0};
for(i=0; i<ndir; i++){
if (!(p = strrchr(namelist[i]->d_name, '.')) || strcmp(p, ".ini")) {
free(namelist[i]);
continue;
}
snprintf(ini_file, MAXPATHLEN, "%s%c%s", dirname, DEFAULT_SLASH, namelist[i]->d_name);
if(VCWD_STAT(ini_file, &sb) == 0){
if (S_ISREG(sb.st_mode)){
zend_symtable_find(parsed_ini_files, namelist[i]->d_name, strlen(namelist[i]->d_name)+1, (void **)&node);
if((**node).mtime == sb.st_mtime){
free(namelist[i]);
continue;
}
if ((fh.handle.fp = VCWD_FOPEN(ini_file, "r"))){
fh.filename = ini_file;
fh.type = ZEND_HANDLE_FP;
ZYCONF_MAKE_STD_ZVAL(configs);
php_zyconf_hash_init(configs, 8);
if (zend_parse_ini_file(&fh, 0, 0, php_zyconf_ini_parser_cb, configs TSRMLS_CC) == FAILURE
|| ZYCONF_G(parse_err)) {
if (!ZYCONF_G(parse_err)) {
php_error(E_WARNING, "Parsing '%s' failed", ini_file);
}
ZYCONF_G(parse_err) = 0;
free(namelist[i]);
continue;
}
zend_symtable_update(zyconf_file_name, namelist[i]->d_name, strlen(namelist[i]->d_name)+1, (void *)&configs, sizeof(zval *), NULL);
if(node != NULL){
free(*node);
}
node_new = pemalloc(sizeof(zyconf_filenode), 1);
node_new->filename = pestrndup(namelist[i]->d_name, strlen(namelist[i]->d_name), 1);
node_new->mtime = sb.st_mtime;
zend_symtable_update(parsed_ini_files, node_new->filename, strlen(namelist[i]->d_name)+1, (void *)&node_new, sizeof(zyconf_filenode *), NULL);
}
}
}
}
}
}
return SUCCESS;
}
PHP_RSHUTDOWN_FUNCTION(zyconf)
{
return SUCCESS;
}
PHP_MINFO_FUNCTION(zyconf)
{
php_info_print_table_start();
php_info_print_table_header(2, "zyconf support", "enabled");
php_info_print_table_end();
DISPLAY_INI_ENTRIES();
}
static void php_zyconf_ini_parser_cb(zval *key, zval *value, zval *index, int callback_type, zval *arr ) {
zval *element;
switch (callback_type) {
case ZEND_INI_PARSER_ENTRY:
{
char *skey, *seg, *ptr;
zval **ppzval, *dst;
if (!value) {
break;
}
dst = arr;
skey = estrndup(Z_STRVAL_P(key), Z_STRLEN_P(key));
if ((seg = php_strtok_r(skey, ".", &ptr))) {
do {
char *real_key = seg;
seg = php_strtok_r(NULL, ".", &ptr);
if (zend_symtable_find(Z_ARRVAL_P(dst), real_key, strlen(real_key) + 1, (void **) &ppzval) == FAILURE) {
if (seg) {
zval *tmp;
ZYCONF_MAKE_STD_ZVAL(tmp);
php_zyconf_hash_init(tmp, 8);
zend_symtable_update(Z_ARRVAL_P(dst), real_key, strlen(real_key) + 1, (void **)&tmp, sizeof(zval *), (void **)&ppzval);
} else {
ZYCONF_MAKE_STD_ZVAL(element);
//ZVAL_ZVAL(element, value, 1, 0);
php_zyconf_zval_persistent(value, element);
zend_symtable_update(Z_ARRVAL_P(dst), real_key, strlen(real_key) + 1, (void **)&element, sizeof(zval *), NULL);
break;
}
} else {
SEPARATE_ZVAL(ppzval);
if (IS_ARRAY != Z_TYPE_PP(ppzval)) {
if (seg) {
zval *tmp;
ZYCONF_MAKE_STD_ZVAL(tmp);
//array_init(tmp);
php_zyconf_hash_init(tmp, 8);
zend_symtable_update(Z_ARRVAL_P(dst), real_key, strlen(real_key) + 1, (void **)&tmp, sizeof(zval *), (void **)&ppzval);
} else {
ZYCONF_MAKE_STD_ZVAL(element);
//ZVAL_ZVAL(element, value, 1, 0);
php_zyconf_zval_persistent(value, element);
zend_symtable_update(Z_ARRVAL_P(dst), real_key, strlen(real_key) + 1, (void **)&element, sizeof(zval *), NULL);
}
}
}
dst = *ppzval;
} while (seg);
}
efree(skey);
}
break;
case ZEND_INI_PARSER_POP_ENTRY:
{
zval *hash, **find_hash, *dst;
if (!value) {
break;
}
if (!(Z_STRLEN_P(key) > 1 && Z_STRVAL_P(key)[0] == '0')
&& is_numeric_string(Z_STRVAL_P(key), Z_STRLEN_P(key), NULL, NULL, 0) == IS_LONG) {
ulong skey = (ulong)zend_atol(Z_STRVAL_P(key), Z_STRLEN_P(key));
if (zend_hash_index_find(Z_ARRVAL_P(arr), skey, (void **) &find_hash) == FAILURE) {
ZYCONF_MAKE_STD_ZVAL(hash);
//array_init(hash);
php_zyconf_hash_init(hash, 8);
zend_hash_index_update(Z_ARRVAL_P(arr), skey, &hash, sizeof(zval *), NULL);
} else {
hash = *find_hash;
}
} else {
char *seg, *ptr;
char *skey = estrndup(Z_STRVAL_P(key), Z_STRLEN_P(key));
dst = arr;
if ((seg = php_strtok_r(skey, ".", &ptr))) {
while (seg) {
if (zend_symtable_find(Z_ARRVAL_P(dst), seg, strlen(seg) + 1, (void **) &find_hash) == FAILURE) {
ZYCONF_MAKE_STD_ZVAL(hash);
//array_init(hash);
php_zyconf_hash_init(hash, 8);
zend_symtable_update(Z_ARRVAL_P(dst),
seg, strlen(seg) + 1, (void **)&hash, sizeof(zval *), (void **)&find_hash);
}
dst = *find_hash;
seg = php_strtok_r(NULL, ".", &ptr);
}
hash = dst;
} else {
if (zend_symtable_find(Z_ARRVAL_P(dst), seg, strlen(seg) + 1, (void **)&find_hash) == FAILURE) {
ZYCONF_MAKE_STD_ZVAL(hash);
//array_init(hash);
php_zyconf_hash_init(hash, 8);
zend_symtable_update(Z_ARRVAL_P(dst), seg, strlen(seg) + 1, (void **)&hash, sizeof(zval *), NULL);
} else {
hash = *find_hash;
}
}
efree(skey);
}
if (Z_TYPE_P(hash) != IS_ARRAY) {
zval_dtor(hash);
INIT_PZVAL(hash);
array_init(hash);
}
ZYCONF_MAKE_STD_ZVAL(element);
//ZVAL_ZVAL(element, value, 1, 0);
php_zyconf_zval_persistent(value, element);
if (index && Z_STRLEN_P(index) > 0) {
add_assoc_zval_ex(hash, Z_STRVAL_P(index), Z_STRLEN_P(index) + 1, element);
} else {
add_next_index_zval(hash, element);
}
}
break;
case ZEND_INI_PARSER_SECTION:
break;
}
}
static void php_zyconf_zval_persistent(zval *zv, zval *rv){
switch (Z_TYPE_P(zv)) {
case IS_CONSTANT:
case IS_STRING:
{
Z_TYPE_P(rv) = IS_STRING;
Z_STRLEN_P(rv) = Z_STRLEN_P(zv);
//持久化内存
Z_STRVAL_P(rv) = pestrndup(Z_STRVAL_P(zv), Z_STRLEN_P(zv), 1);
}
break;
case IS_ARRAY:
{
php_zyconf_hash_init(rv, zend_hash_num_elements(Z_ARRVAL_P(zv)));
zend_hash_copy(Z_ARRVAL_P(rv), Z_ARRVAL_P(zv), NULL, NULL, sizeof(void *));
}
break;
case IS_RESOURCE:
case IS_OBJECT:
case IS_BOOL:
case IS_LONG:
case IS_NULL:
assert(0);
break;
}
}
static void php_zyconf_hash_destroy(HashTable *ht){
Bucket *p, *q;
zval **element;
p = ht->pListHead;
while (p != NULL) {
q = p;
p = p->pListNext;
element = (zval **) q->pData;
switch(Z_TYPE_PP(element)){
case IS_STRING:
pefree(Z_STRVAL_PP(element), 1);
break;
case IS_ARRAY:
php_zyconf_hash_destroy(Z_ARRVAL_PP(element));
break;
}
if (ht->pDestructor) {
ht->pDestructor(q->pData);
}
pefree(q, ht->persistent);
}
if (ht->nTableMask) {
pefree(ht->arBuckets, ht->persistent);
}
}
PHPAPI zval * php_zyconf_get(char *file_name, int file_name_len, char *name, int name_len) {
zval **ppconf, **ppzval;
int result;
char file_name_ini[200];
HashTable *target;
if(zyconf_file_name){
do{
//获得文件下所有的
strcpy(file_name_ini, file_name);
strcat(file_name_ini, ".ini");
result = zend_symtable_find(zyconf_file_name, file_name_ini, strlen(file_name_ini)+1, (void **)&ppconf);
if(result==FAILURE){
break;
}
target = Z_ARRVAL_PP(ppconf);
char *entry, *seg, *pptr;
entry = estrndup(name, name_len);
if((seg = php_strtok_r(entry, ".", &pptr))){
while (seg) {
if (zend_hash_find(target, seg, strlen(seg) + 1, (void **) &ppzval) == FAILURE) {
efree(entry);
return NULL;
}
target = Z_ARRVAL_PP(ppzval);
seg = php_strtok_r(NULL, ".", &pptr);
}
}else{
result = zend_symtable_find(target, seg, strlen(seg) + 1, (void **)&ppzval);
/*
php_printf("%p, %s, %d\n", target, name, name_len+1);
php_printf("%p, %s ,%d\n", Z_STRVAL_PP(ppzval), Z_STRVAL_PP(ppzval), Z_STRLEN_PP(ppzval));
*/
}
efree(entry);
if(result == SUCCESS) return *ppzval;
}while(0);
}
return NULL;
}
PHPAPI int php_zyconf_has(char *file_name, int file_name_len, char *name, int name_len) {
if (php_zyconf_get(file_name, file_name_len, name, name_len)) {
return 1;
}
return 0;
}
static inline void php_zyconf_hash_init(zval *zv, size_t size) /* {{{ */ {
HashTable *ht;
PALLOC_HASHTABLE(ht);
zend_hash_init(ht, size, NULL, NULL, 1);
Z_ARRVAL_P(zv) = ht;
Z_TYPE_P(zv) = IS_ARRAY;
INIT_PZVAL(zv);
}
PHP_METHOD(zyconf, get){
char *name;
int name_len;
char *file_name;
int file_name_len;
zval *reval;
if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &file_name, &file_name_len, &name, &name_len) == FAILURE){
return;
}
reval = php_zyconf_get(file_name, file_name_len, name, name_len);
if(reval){
RETURN_ZVAL(reval, 1, 0);
}
RETURN_NULL();
}
PHP_METHOD(zyconf, has){
char *name;
int name_len;
char *file_name;
int file_name_len;
if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &file_name, &file_name_len, &name, &name_len) == FAILURE){
return;
}
RETURN_BOOL(php_zyconf_has(file_name, file_name_len, name, name_len));
}