Skip to content

Commit 989a61e

Browse files
committed
As discussed, add --with-config-file-scan-dir compile-time switch defining
a directory which will be scanned for *.ini files after the main php.ini file has been parsed. This makes it much easier to automatically deploy a modular PHP since adding extensions which have their own ini switches can now be done by simply dropping a foo.ini file in the right directory and restarting. A list of parsed ini files is maintained and shown on the phpinfo page.
1 parent 114dff3 commit 989a61e

File tree

6 files changed

+89
-15
lines changed

6 files changed

+89
-15
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
PHP 4 NEWS
22
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
33
? ? ??? 2002, Version 4.3.0
4+
- Added --with-config-file-scan-dir compile-time switch which specifies a
5+
directory which will be scanned for *.ini files.
46
- Added wddx_serialize_type(). Allows users to cast values to WDDX types (jan)
57
- Added ob_flush_all() that flushes all buffers. (Yasuo)
68
- Added ob_get_clean() and og_get_flush(). (Yasuo)

configure.in

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,12 @@ if test "$PHP_CONFIG_FILE_PATH" = "DEFAULT"; then
570570
esac
571571
fi
572572
573+
PHP_ARG_WITH(config-file-scan-dir,[directory to be scanned for configuration files],
574+
[ --with-config-file-scan-dir=PATH ], DEFAULT, no)
575+
if test "$PHP_CONFIG_FILE_SCAN_DIR" = "DEFAULT"; then
576+
PHP_CONFIG_FILE_SCAN_DIR=""
577+
fi
578+
573579
# compatibility
574580
if test -z "$with_pear" && test "$enable_pear" = "no"; then
575581
with_pear=no
@@ -890,6 +896,7 @@ EXPANDED_LIBDIR=$libdir
890896
EXPANDED_SYSCONFDIR=`eval echo $sysconfdir`
891897
EXPANDED_DATADIR=$datadir
892898
EXPANDED_PHP_CONFIG_FILE_PATH=`eval echo "$PHP_CONFIG_FILE_PATH"`
899+
EXPANDED_PHP_CONFIG_FILE_SCAN_DIR=`eval echo "$PHP_CONFIG_FILE_SCAN_DIR"`
893900
INCLUDE_PATH=.:$EXPANDED_PEAR_INSTALLDIR
894901

895902
exec_prefix=$old_exec_prefix
@@ -905,6 +912,7 @@ AC_SUBST(EXPANDED_DATADIR)
905912
AC_SUBST(EXPANDED_SYSCONFDIR)
906913
AC_SUBST(EXPANDED_LOCALSTATEDIR)
907914
AC_SUBST(EXPANDED_PHP_CONFIG_FILE_PATH)
915+
AC_SUBST(EXPANDED_PHP_CONFIG_FILE_SCAN_DIR)
908916

909917
PHP_UTILIZE_RPATHS
910918

ext/standard/info.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ ZEND_EXTERN_MODULE_GLOBALS(iconv)
5959
} \
6060

6161
PHPAPI extern char *php_ini_opened_path;
62+
PHPAPI extern char *php_ini_scanned_files;
6263

6364
/* {{{ _display_module_info
6465
*/
@@ -378,6 +379,13 @@ PHPAPI void php_print_info(int flag TSRMLS_DC)
378379
#endif
379380

380381
php_info_print_table_row(2, "Configuration File (php.ini) Path", php_ini_opened_path?php_ini_opened_path:PHP_CONFIG_FILE_PATH);
382+
383+
if(strlen(PHP_CONFIG_FILE_SCAN_DIR)) {
384+
php_info_print_table_row(2, "Scan this dir for additional .ini files", PHP_CONFIG_FILE_SCAN_DIR);
385+
if(php_ini_scanned_files) {
386+
php_info_print_table_row(2, "additional .ini files parsed", php_ini_scanned_files);
387+
}
388+
}
381389

382390
snprintf(temp_api, sizeof(temp_api), "%d", PHP_API_VERSION);
383391
php_info_print_table_row(2, "PHP API", temp_api);

main/build-defs.h.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,5 @@
8787
#define PHP_SYSCONFDIR "@EXPANDED_SYSCONFDIR@"
8888
#define PHP_LOCALSTATEDIR "@EXPANDED_LOCALSTATEDIR@"
8989
#define PHP_CONFIG_FILE_PATH "@EXPANDED_PHP_CONFIG_FILE_PATH@"
90+
#define PHP_CONFIG_FILE_SCAN_DIR "@EXPANDED_PHP_CONFIG_FILE_SCAN_DIR@"
9091
#define PHP_SHLIB_SUFFIX "@SHLIB_SUFFIX_NAME@"

main/main.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1108,6 +1108,7 @@ int php_module_startup(sapi_module_struct *sf, zend_module_entry *additional_mod
11081108
REGISTER_MAIN_STRINGL_CONSTANT("PHP_SYSCONFDIR", PHP_SYSCONFDIR, sizeof(PHP_SYSCONFDIR)-1, CONST_PERSISTENT | CONST_CS);
11091109
REGISTER_MAIN_STRINGL_CONSTANT("PHP_LOCALSTATEDIR", PHP_LOCALSTATEDIR, sizeof(PHP_LOCALSTATEDIR)-1, CONST_PERSISTENT | CONST_CS);
11101110
REGISTER_MAIN_STRINGL_CONSTANT("PHP_CONFIG_FILE_PATH", PHP_CONFIG_FILE_PATH, sizeof(PHP_CONFIG_FILE_PATH)-1, CONST_PERSISTENT | CONST_CS);
1111+
REGISTER_MAIN_STRINGL_CONSTANT("PHP_CONFIG_FILE_SCAN_DIR", PHP_CONFIG_FILE_SCAN_DIR, sizeof(PHP_CONFIG_FILE_SCAN_DIR)-1, CONST_PERSISTENT | CONST_CS);
11111112
REGISTER_MAIN_STRINGL_CONSTANT("PHP_SHLIB_SUFFIX", PHP_SHLIB_SUFFIX, sizeof(PHP_SHLIB_SUFFIX)-1, CONST_PERSISTENT | CONST_CS);
11121113
php_output_register_constants(TSRMLS_C);
11131114
php_rfc1867_register_constants(TSRMLS_C);

main/php_ini.c

Lines changed: 69 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@
3434
#include "SAPI.h"
3535
#include "php_main.h"
3636

37+
#ifndef S_ISREG
38+
#define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
39+
#endif
40+
3741
typedef struct _php_extension_lists {
3842
zend_llist engine;
3943
zend_llist functions;
@@ -44,6 +48,7 @@ typedef struct _php_extension_lists {
4448
static HashTable configuration_hash;
4549
PHPAPI char *php_ini_opened_path=NULL;
4650
static php_extension_lists extension_lists;
51+
PHPAPI char *php_ini_scanned_files=NULL;
4752

4853
/* {{{ php_ini_displayer_cb
4954
*/
@@ -228,6 +233,14 @@ int php_init_config()
228233
char *open_basedir;
229234
int free_ini_search_path=0;
230235
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;
231244
TSRMLS_FETCH();
232245

233246
if (zend_hash_init(&configuration_hash, 0, NULL, (dtor_func_t) pvalue_config_destructor, 1)==FAILURE) {
@@ -236,6 +249,7 @@ int php_init_config()
236249

237250
zend_llist_init(&extension_lists.engine, sizeof(char *), (llist_dtor_func_t) free_estring, 1);
238251
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);
239253

240254
safe_mode_state = PG(safe_mode);
241255
open_basedir = PG(open_basedir);
@@ -363,25 +377,62 @@ int php_init_config()
363377
PG(safe_mode) = safe_mode_state;
364378
PG(open_basedir) = open_basedir;
365379

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;
370382

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);
372384

373-
{
374-
zval tmp;
385+
{
386+
zval tmp;
375387

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+
}
383435
}
384-
385436
return SUCCESS;
386437
}
387438
/* }}} */
@@ -394,6 +445,9 @@ int php_shutdown_config(void)
394445
if (php_ini_opened_path) {
395446
free(php_ini_opened_path);
396447
}
448+
if (php_ini_scanned_files) {
449+
free(php_ini_scanned_files);
450+
}
397451
return SUCCESS;
398452
}
399453
/* }}} */

0 commit comments

Comments
 (0)