@@ -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
@@ -520,9 +521,18 @@ int php_init_config(TSRMLS_D)
520
521
PG (safe_mode ) = 0 ;
521
522
PG (open_basedir ) = NULL ;
522
523
524
+ /*
525
+ * Find and open actual ini file
526
+ */
527
+
523
528
memset (& fh , 0 , sizeof (fh ));
524
- /* Check if php_ini_path_override is a file */
525
- if (!sapi_module .php_ini_ignore ) {
529
+
530
+ /* If SAPI does not want to ignore all ini files OR an overriding file/path is given.
531
+ * This allows disabling scanning for ini files in the PHP_CONFIG_FILE_SCAN_DIR but still
532
+ * load an optional ini file. */
533
+ if (!sapi_module .php_ini_ignore || sapi_module .php_ini_path_override ) {
534
+
535
+ /* Check if php_ini_file_name is a file and can be opened */
526
536
if (php_ini_file_name && php_ini_file_name [0 ]) {
527
537
struct stat statbuf ;
528
538
@@ -535,7 +545,8 @@ int php_init_config(TSRMLS_D)
535
545
}
536
546
}
537
547
}
538
- /* Search php-%sapi-module-name%.ini file in search path */
548
+
549
+ /* Otherwise search for php-%sapi-module-name%.ini file in search path */
539
550
if (!fh .handle .fp ) {
540
551
const char * fmt = "php-%s.ini" ;
541
552
char * ini_fname ;
@@ -546,7 +557,8 @@ int php_init_config(TSRMLS_D)
546
557
fh .filename = php_ini_opened_path ;
547
558
}
548
559
}
549
- /* Search php.ini file in search path */
560
+
561
+ /* If still no ini file found, search for php.ini file in search path */
550
562
if (!fh .handle .fp ) {
551
563
fh .handle .fp = php_fopen_with_path ("php.ini" , "r" , php_ini_search_path , & php_ini_opened_path TSRMLS_CC );
552
564
if (fh .handle .fp ) {
@@ -584,9 +596,16 @@ int php_init_config(TSRMLS_D)
584
596
}
585
597
}
586
598
587
- /* If the config_file_scan_dir is set at compile-time, go and scan this directory and
588
- * parse any .ini files found in this directory. */
589
- if (!sapi_module .php_ini_ignore && strlen (PHP_CONFIG_FILE_SCAN_DIR )) {
599
+ /* Check for PHP_INI_SCAN_DIR environment variable to override/set config file scan directory */
600
+ php_ini_scanned_path = getenv ("PHP_INI_SCAN_DIR" );
601
+ if (!php_ini_scanned_path ) {
602
+ /* Or fall back using possible --with-config-file-scan-dir setting (defaults to empty string!) */
603
+ php_ini_scanned_path = PHP_CONFIG_FILE_SCAN_DIR ;
604
+ }
605
+ int php_ini_scanned_path_len = strlen (php_ini_scanned_path );
606
+
607
+ /* Scan and parse any .ini files found in scan path if path not empty. */
608
+ if (!sapi_module .php_ini_ignore && php_ini_scanned_path_len ) {
590
609
struct dirent * * namelist ;
591
610
int ndir , i ;
592
611
struct stat sb ;
@@ -600,7 +619,7 @@ int php_init_config(TSRMLS_D)
600
619
/* Reset active ini section */
601
620
RESET_ACTIVE_INI_HASH ();
602
621
603
- if ((ndir = php_scandir (PHP_CONFIG_FILE_SCAN_DIR , & namelist , 0 , php_alphasort )) > 0 ) {
622
+ if ((ndir = php_scandir (php_ini_scanned_path , & namelist , 0 , php_alphasort )) > 0 ) {
604
623
zend_llist_init (& scanned_ini_list , sizeof (char * ), (llist_dtor_func_t ) free_estring , 1 );
605
624
memset (& fh , 0 , sizeof (fh ));
606
625
@@ -611,7 +630,11 @@ int php_init_config(TSRMLS_D)
611
630
free (namelist [i ]);
612
631
continue ;
613
632
}
614
- snprintf (ini_file , MAXPATHLEN , "%s%c%s" , PHP_CONFIG_FILE_SCAN_DIR , DEFAULT_SLASH , namelist [i ]-> d_name );
633
+ if (IS_SLASH (php_ini_scanned_path [php_ini_scanned_path_len - 1 ])) {
634
+ snprintf (ini_file , MAXPATHLEN , "%s%s" , php_ini_scanned_path , namelist [i ]-> d_name );
635
+ } else {
636
+ snprintf (ini_file , MAXPATHLEN , "%s%c%s" , php_ini_scanned_path , DEFAULT_SLASH , namelist [i ]-> d_name );
637
+ }
615
638
if (VCWD_STAT (ini_file , & sb ) == 0 ) {
616
639
if (S_ISREG (sb .st_mode )) {
617
640
if ((fh .handle .fp = VCWD_FOPEN (ini_file , "r" ))) {
@@ -649,6 +672,9 @@ int php_init_config(TSRMLS_D)
649
672
}
650
673
zend_llist_destroy (& scanned_ini_list );
651
674
}
675
+ } else {
676
+ /* Make sure an empty php_ini_scanned_path ends up as NULL */
677
+ php_ini_scanned_path = NULL ;
652
678
}
653
679
654
680
if (sapi_module .ini_entries ) {
0 commit comments