@@ -54,6 +54,7 @@ static int has_per_dir_config = 0;
54
54
static int has_per_host_config = 0 ;
55
55
PHPAPI char * php_ini_opened_path = NULL ;
56
56
static php_extension_lists extension_lists ;
57
+ PHPAPI char * php_ini_scanned_path = NULL ;
57
58
PHPAPI char * php_ini_scanned_files = NULL ;
58
59
59
60
/* {{{ php_ini_displayer_cb
@@ -517,9 +518,18 @@ int php_init_config(TSRMLS_D)
517
518
518
519
PG (open_basedir ) = NULL ;
519
520
521
+ /*
522
+ * Find and open actual ini file
523
+ */
524
+
520
525
memset (& fh , 0 , sizeof (fh ));
521
- /* Check if php_ini_path_override is a file */
522
- if (!sapi_module .php_ini_ignore ) {
526
+
527
+ /* If SAPI does not want to ignore all ini files OR an overriding file/path is given.
528
+ * This allows disabling scanning for ini files in the PHP_CONFIG_FILE_SCAN_DIR but still
529
+ * load an optional ini file. */
530
+ if (!sapi_module .php_ini_ignore || sapi_module .php_ini_path_override ) {
531
+
532
+ /* Check if php_ini_file_name is a file and can be opened */
523
533
if (php_ini_file_name && php_ini_file_name [0 ]) {
524
534
struct stat statbuf ;
525
535
@@ -532,7 +542,8 @@ int php_init_config(TSRMLS_D)
532
542
}
533
543
}
534
544
}
535
- /* Search php-%sapi-module-name%.ini file in search path */
545
+
546
+ /* Otherwise search for php-%sapi-module-name%.ini file in search path */
536
547
if (!fh .handle .fp ) {
537
548
const char * fmt = "php-%s.ini" ;
538
549
char * ini_fname ;
@@ -543,7 +554,8 @@ int php_init_config(TSRMLS_D)
543
554
fh .filename = php_ini_opened_path ;
544
555
}
545
556
}
546
- /* Search php.ini file in search path */
557
+
558
+ /* If still no ini file found, search for php.ini file in search path */
547
559
if (!fh .handle .fp ) {
548
560
fh .handle .fp = php_fopen_with_path ("php.ini" , "r" , php_ini_search_path , & php_ini_opened_path TSRMLS_CC );
549
561
if (fh .handle .fp ) {
@@ -580,9 +592,16 @@ int php_init_config(TSRMLS_D)
580
592
}
581
593
}
582
594
583
- /* If the config_file_scan_dir is set at compile-time, go and scan this directory and
584
- * parse any .ini files found in this directory. */
585
- if (!sapi_module .php_ini_ignore && strlen (PHP_CONFIG_FILE_SCAN_DIR )) {
595
+ /* Check for PHP_INI_SCAN_DIR environment variable to override/set config file scan directory */
596
+ php_ini_scanned_path = getenv ("PHP_INI_SCAN_DIR" );
597
+ if (!php_ini_scanned_path ) {
598
+ /* Or fall back using possible --with-config-file-scan-dir setting (defaults to empty string!) */
599
+ php_ini_scanned_path = PHP_CONFIG_FILE_SCAN_DIR ;
600
+ }
601
+ int php_ini_scanned_path_len = strlen (php_ini_scanned_path );
602
+
603
+ /* Scan and parse any .ini files found in scan path if path not empty. */
604
+ if (!sapi_module .php_ini_ignore && php_ini_scanned_path_len ) {
586
605
struct dirent * * namelist ;
587
606
int ndir , i ;
588
607
struct stat sb ;
@@ -596,7 +615,7 @@ int php_init_config(TSRMLS_D)
596
615
/* Reset active ini section */
597
616
RESET_ACTIVE_INI_HASH ();
598
617
599
- if ((ndir = php_scandir (PHP_CONFIG_FILE_SCAN_DIR , & namelist , 0 , php_alphasort )) > 0 ) {
618
+ if ((ndir = php_scandir (php_ini_scanned_path , & namelist , 0 , php_alphasort )) > 0 ) {
600
619
zend_llist_init (& scanned_ini_list , sizeof (char * ), (llist_dtor_func_t ) free_estring , 1 );
601
620
memset (& fh , 0 , sizeof (fh ));
602
621
@@ -607,7 +626,11 @@ int php_init_config(TSRMLS_D)
607
626
free (namelist [i ]);
608
627
continue ;
609
628
}
610
- snprintf (ini_file , MAXPATHLEN , "%s%c%s" , PHP_CONFIG_FILE_SCAN_DIR , DEFAULT_SLASH , namelist [i ]-> d_name );
629
+ if (IS_SLASH (php_ini_scanned_path [php_ini_scanned_path_len - 1 ])) {
630
+ snprintf (ini_file , MAXPATHLEN , "%s%s" , php_ini_scanned_path , namelist [i ]-> d_name );
631
+ } else {
632
+ snprintf (ini_file , MAXPATHLEN , "%s%c%s" , php_ini_scanned_path , DEFAULT_SLASH , namelist [i ]-> d_name );
633
+ }
611
634
if (VCWD_STAT (ini_file , & sb ) == 0 ) {
612
635
if (S_ISREG (sb .st_mode )) {
613
636
if ((fh .handle .fp = VCWD_FOPEN (ini_file , "r" ))) {
@@ -645,6 +668,9 @@ int php_init_config(TSRMLS_D)
645
668
}
646
669
zend_llist_destroy (& scanned_ini_list );
647
670
}
671
+ } else {
672
+ /* Make sure an empty php_ini_scanned_path ends up as NULL */
673
+ php_ini_scanned_path = NULL ;
648
674
}
649
675
650
676
if (sapi_module .ini_entries ) {
0 commit comments