34
34
#include "SAPI.h"
35
35
#include "php_main.h"
36
36
37
+ #ifndef S_ISREG
38
+ #define S_ISREG (mode ) (((mode) & S_IFMT) == S_IFREG)
39
+ #endif
40
+
37
41
typedef struct _php_extension_lists {
38
42
zend_llist engine ;
39
43
zend_llist functions ;
@@ -44,6 +48,7 @@ typedef struct _php_extension_lists {
44
48
static HashTable configuration_hash ;
45
49
PHPAPI char * php_ini_opened_path = NULL ;
46
50
static php_extension_lists extension_lists ;
51
+ PHPAPI char * php_ini_scanned_files = NULL ;
47
52
48
53
/* {{{ php_ini_displayer_cb
49
54
*/
@@ -228,6 +233,14 @@ int php_init_config()
228
233
char * open_basedir ;
229
234
int free_ini_search_path = 0 ;
230
235
zend_file_handle fh ;
236
+ DIR * dirp = NULL ;
237
+ struct dirent * dir_entry ;
238
+ struct stat sb ;
239
+ char ini_file [MAXPATHLEN ];
240
+ char * p ;
241
+ zend_llist scanned_ini_list ;
242
+ int l , total_l = 0 ;
243
+ zend_llist_element * element ;
231
244
TSRMLS_FETCH ();
232
245
233
246
if (zend_hash_init (& configuration_hash , 0 , NULL , (dtor_func_t ) pvalue_config_destructor , 1 )== FAILURE ) {
@@ -236,6 +249,7 @@ int php_init_config()
236
249
237
250
zend_llist_init (& extension_lists .engine , sizeof (char * ), (llist_dtor_func_t ) free_estring , 1 );
238
251
zend_llist_init (& extension_lists .functions , sizeof (zval ), (llist_dtor_func_t ) ZVAL_DESTRUCTOR , 1 );
252
+ zend_llist_init (& scanned_ini_list , sizeof (char * ), (llist_dtor_func_t ) free_estring , 1 );
239
253
240
254
safe_mode_state = PG (safe_mode );
241
255
open_basedir = PG (open_basedir );
@@ -363,25 +377,62 @@ int php_init_config()
363
377
PG (safe_mode ) = safe_mode_state ;
364
378
PG (open_basedir ) = open_basedir ;
365
379
366
- if (!fh .handle .fp ) {
367
- return SUCCESS ; /* having no configuration file is ok */
368
- }
369
- fh .type = ZEND_HANDLE_FP ;
380
+ if (fh .handle .fp ) {
381
+ fh .type = ZEND_HANDLE_FP ;
370
382
371
- zend_parse_ini_file (& fh , 1 , php_config_ini_parser_cb , & extension_lists );
383
+ zend_parse_ini_file (& fh , 1 , php_config_ini_parser_cb , & extension_lists );
372
384
373
- {
374
- zval tmp ;
385
+ {
386
+ zval tmp ;
375
387
376
- Z_STRLEN (tmp ) = strlen (fh .filename );
377
- Z_STRVAL (tmp ) = zend_strndup (fh .filename , Z_STRLEN (tmp ));
378
- Z_TYPE (tmp ) = IS_STRING ;
379
- zend_hash_update (& configuration_hash , "cfg_file_path" , sizeof ("cfg_file_path" ), (void * ) & tmp , sizeof (zval ), NULL );
380
- if (php_ini_opened_path )
381
- efree (php_ini_opened_path );
382
- php_ini_opened_path = zend_strndup (Z_STRVAL (tmp ), Z_STRLEN (tmp ));
388
+ Z_STRLEN (tmp ) = strlen (fh .filename );
389
+ Z_STRVAL (tmp ) = zend_strndup (fh .filename , Z_STRLEN (tmp ));
390
+ Z_TYPE (tmp ) = IS_STRING ;
391
+ zend_hash_update (& configuration_hash , "cfg_file_path" , sizeof ("cfg_file_path" ), (void * ) & tmp , sizeof (zval ), NULL );
392
+ if (php_ini_opened_path )
393
+ efree (php_ini_opened_path );
394
+ php_ini_opened_path = zend_strndup (Z_STRVAL (tmp ), Z_STRLEN (tmp ));
395
+ }
396
+ }
397
+
398
+ /* If the config_file_scan_dir is set at compile-time, go and scan this directory and
399
+ * parse any .ini files found in this directory. */
400
+ if (strlen (PHP_CONFIG_FILE_SCAN_DIR )) {
401
+ dirp = VCWD_OPENDIR (PHP_CONFIG_FILE_SCAN_DIR );
402
+ if (dirp ) {
403
+ fh .type = ZEND_HANDLE_FP ;
404
+ while ((dir_entry = readdir (dirp )) != NULL ) {
405
+ /* check for a .ini extension */
406
+ if ((p = strrchr (dir_entry -> d_name ,'.' )) && strcmp (p ,".ini" )) continue ;
407
+ snprintf (ini_file , MAXPATHLEN , "%s%c%s" , PHP_CONFIG_FILE_SCAN_DIR , DEFAULT_SLASH , dir_entry -> d_name );
408
+ if (VCWD_STAT (ini_file , & sb ) == 0 ) {
409
+ if (S_ISREG (sb .st_mode )) {
410
+ if ((fh .handle .fp = VCWD_FOPEN (ini_file , "r" ))) {
411
+ fh .filename = ini_file ;
412
+ zend_parse_ini_file (& fh , 1 , php_config_ini_parser_cb , & extension_lists );
413
+ /* Here, add it to the list of ini files read */
414
+ l = strlen (ini_file );
415
+ total_l += l + 2 ;
416
+ p = estrndup (ini_file ,l );
417
+ zend_llist_add_element (& scanned_ini_list , & p );
418
+ }
419
+ }
420
+ }
421
+ }
422
+ closedir (dirp );
423
+ /*
424
+ * Don't need an extra byte for the \0 in this malloc as the last
425
+ * element will not get a trailing , which gives us the byte for the \0
426
+ */
427
+ php_ini_scanned_files = (char * )malloc (total_l );
428
+ * php_ini_scanned_files = '\0' ;
429
+ for (element = scanned_ini_list .head ; element ; element = element -> next ) {
430
+ strcat (php_ini_scanned_files ,* (char * * )element -> data );
431
+ strcat (php_ini_scanned_files ,element -> next ? ",\n" :"\n" );
432
+ }
433
+ zend_llist_destroy (& scanned_ini_list );
434
+ }
383
435
}
384
-
385
436
return SUCCESS ;
386
437
}
387
438
/* }}} */
@@ -394,6 +445,9 @@ int php_shutdown_config(void)
394
445
if (php_ini_opened_path ) {
395
446
free (php_ini_opened_path );
396
447
}
448
+ if (php_ini_scanned_files ) {
449
+ free (php_ini_scanned_files );
450
+ }
397
451
return SUCCESS ;
398
452
}
399
453
/* }}} */
0 commit comments