From 5af649efbdbb756b4e632ff67d04af9905ad328f Mon Sep 17 00:00:00 2001 From: Zeev Suraski Date: Sat, 11 May 2002 03:41:17 +0000 Subject: [PATCH] Revert to the old php_ini.c, and reimplement the binary-path searching. Should now also work under UNIX (CLI/CGI) --- main/SAPI.h | 3 +- main/main.c | 2 +- main/php_ini.c | 180 +++++++++++++++++++++++++++----------------- main/php_ini.h | 2 +- sapi/cgi/cgi_main.c | 2 + sapi/cli/php_cli.c | 2 + 6 files changed, 118 insertions(+), 73 deletions(-) diff --git a/main/SAPI.h b/main/SAPI.h index 1b228cb5f37..8a5edfcbe41 100644 --- a/main/SAPI.h +++ b/main/SAPI.h @@ -186,6 +186,7 @@ struct _sapi_module_struct { void (*unblock_interruptions)(void); void (*default_post_reader)(TSRMLS_D); + char *executable_location; }; @@ -216,7 +217,7 @@ struct _sapi_post_entry { SAPI_API SAPI_POST_READER_FUNC(sapi_read_standard_form_data); SAPI_API SAPI_POST_READER_FUNC(php_default_post_reader); -#define STANDARD_SAPI_MODULE_PROPERTIES NULL +#define STANDARD_SAPI_MODULE_PROPERTIES NULL, NULL #endif /* SAPI_H */ diff --git a/main/main.c b/main/main.c index 72c91096ac0..982220ce64c 100644 --- a/main/main.c +++ b/main/main.c @@ -959,7 +959,7 @@ int php_module_startup(sapi_module_struct *sf) /* this will read in php.ini, set up the configuration parameters, load zend extensions and register php function extensions to be loaded later */ - if (php_init_config(sf->php_ini_path_override) == FAILURE) { + if (php_init_config() == FAILURE) { return FAILURE; } diff --git a/main/php_ini.c b/main/php_ini.c index 33b09a6d7bd..e0bb7e2013f 100644 --- a/main/php_ini.c +++ b/main/php_ini.c @@ -18,6 +18,9 @@ /* $Id$ */ +/* Check CWD for php.ini */ +#define INI_CHECK_CWD + #include "php.h" #ifndef PHP_WIN32 #include "build-defs.h" @@ -108,7 +111,7 @@ PHPAPI void display_ini_entries(zend_module_entry *module) if (module) { module_number = module->module_number; - } else { + } else { module_number = 0; } php_info_print_table_start(); @@ -122,25 +125,25 @@ PHPAPI void display_ini_entries(zend_module_entry *module) #ifdef ZTS # if (ZEND_DEBUG) -# define ZEND_EXTENSION_TOKEN "zend_extension_debug_ts" +# define ZEND_EXTENSION_TOKEN "zend_extension_debug_ts" # else -# define ZEND_EXTENSION_TOKEN "zend_extension_ts" +# define ZEND_EXTENSION_TOKEN "zend_extension_ts" # endif #else # if (ZEND_DEBUG) -# define ZEND_EXTENSION_TOKEN "zend_extension_debug" +# define ZEND_EXTENSION_TOKEN "zend_extension_debug" # else -# define ZEND_EXTENSION_TOKEN "zend_extension" +# define ZEND_EXTENSION_TOKEN "zend_extension" # endif #endif /* {{{ pvalue_config_destructor */ static void pvalue_config_destructor(zval *pvalue) -{ - if (Z_TYPE_P(pvalue) == IS_STRING && Z_STRVAL_P(pvalue) != empty_string) { - free(Z_STRVAL_P(pvalue)); - } +{ + if (Z_TYPE_P(pvalue) == IS_STRING && Z_STRVAL_P(pvalue) != empty_string) { + free(Z_STRVAL_P(pvalue)); + } } /* }}} */ @@ -157,14 +160,14 @@ static void php_config_ini_parser_cb(zval *arg1, zval *arg2, int callback_type, } if (!strcasecmp(Z_STRVAL_P(arg1), "extension")) { /* load function module */ zval copy; - + copy = *arg2; zval_copy_ctor(©); copy.refcount = 0; - zend_llist_add_element(&extension_lists.functions, ©); + zend_llist_add_element(&extension_lists.functions, ©); } else if (!strcasecmp(Z_STRVAL_P(arg1), ZEND_EXTENSION_TOKEN)) { /* load Zend extension */ char *extension_name = estrndup(Z_STRVAL_P(arg2), Z_STRLEN_P(arg2)); - + zend_llist_add_element(&extension_lists.engine, &extension_name); } else { zend_hash_update(&configuration_hash, Z_STRVAL_P(arg1), Z_STRLEN_P(arg1)+1, arg2, sizeof(zval), (void **) &entry); @@ -199,10 +202,13 @@ static void php_load_zend_extension_cb(void *arg TSRMLS_DC) /* {{{ php_init_config */ -int php_init_config(char *php_ini_path_override) +int php_init_config() { + char *env_location, *php_ini_search_path; + char *binary_location; int safe_mode_state; char *open_basedir; + int free_ini_search_path=0; zend_file_handle fh; TSRMLS_FETCH(); @@ -212,68 +218,103 @@ int php_init_config(char *php_ini_path_override) zend_llist_init(&extension_lists.engine, sizeof(char *), (llist_dtor_func_t) free_estring, 1); zend_llist_init(&extension_lists.functions, sizeof(zval), (llist_dtor_func_t) ZVAL_DESTRUCTOR, 1); - + safe_mode_state = PG(safe_mode); open_basedir = PG(open_basedir); + + env_location = getenv("PHPRC"); + if (!env_location) { + env_location=""; + } + if (sapi_module.php_ini_path_override) { + php_ini_search_path = sapi_module.php_ini_path_override; + free_ini_search_path = 0; + } else { + char *default_location; + char paths_separator[] = { ZEND_PATHS_SEPARATOR, 0 }; + + php_ini_search_path = (char *) emalloc(MAX_PATH*3+strlen(env_location)+3+1); + free_ini_search_path = 1; + php_ini_search_path[0] = 0; + + /* + * Prepare search path + */ + + /* Add cwd */ +#ifdef INI_CHECK_CWD + if (strcmp(sapi_module.name, "cli")!=0) { + strcat(php_ini_search_path, "."); + strcat(php_ini_search_path, paths_separator); + } +#endif + + /* Add binary directory */ +#ifdef PHP_WIN32 + binary_location = (char *) emalloc(MAX_PATH); + if (GetModuleFileName(0, binary_location, MAX_PATH)==0) { + efree(binary_location); + binary_location = NULL; + } +#else + if (sapi_module.executable_location) { + binary_location = estrdup(sapi_module.executable_location); + } else { + binary_location = NULL; + } +#endif + if (binary_location) { + char *separator_location = strrchr(binary_location, DEFAULT_SLASH); + + if (separator_location) { + *(separator_location+1) = 0; + } + strcat(php_ini_search_path, binary_location); + strcat(php_ini_search_path, paths_separator); + efree(binary_location); + } + + /* Add environment location */ + if (env_location[0]) { + strcat(php_ini_search_path, env_location); + strcat(php_ini_search_path, paths_separator); + } + + /* Add default location */ +#ifdef PHP_WIN32 + default_location = (char *) emalloc(MAX_PATH+1); + + if (!GetWindowsDirectory(default_location, MAX_PATH)) { + default_location[0]=0; + } + strcat(php_ini_search_path, default_location); + efree(default_location); +#else + default_location = PHP_CONFIG_FILE_PATH; + strcat(php_ini_search_path, default_location); +#endif + } + PG(safe_mode) = 0; PG(open_basedir) = NULL; fh.handle.fp = NULL; + /* Check if php_ini_path_override is a file */ + if (sapi_module.php_ini_path_override && sapi_module.php_ini_path_override[0]) { + struct stat statbuf; - /* If no override given (usually from the command line) then check the environment. */ - if (!php_ini_path_override) { - php_ini_path_override = getenv("PHPRC"); - } - if (php_ini_path_override && *php_ini_path_override) { - - /* Try to open php_ini_path_override if not a directory. */ - struct stat st; - if ((0 == VCWD_STAT(php_ini_path_override, &st)) && (S_IFDIR != (st.st_mode & S_IFMT))) { - fh.handle.fp = VCWD_FOPEN(php_ini_path_override, "r"); - if (fh.handle.fp) { - php_ini_opened_path = estrdup(php_ini_path_override); + if (!VCWD_STAT(sapi_module.php_ini_path_override, &statbuf)) { + if (!((statbuf.st_mode & S_IFMT) == S_IFDIR)) { + fh.handle.fp = VCWD_FOPEN(sapi_module.php_ini_path_override, "r"); } } - - /* If we did not manage to open php_ini_path_override then search it as a directory. */ - if (!fh.handle.fp) { - fh.handle.fp = php_fopen_with_path("php.ini", "r", php_ini_path_override, &php_ini_opened_path TSRMLS_CC); - } - } - -#define INI_CHECK_CWD -#ifdef INI_CHECK_CWD - if (!fh.handle.fp && (0 != strcmp(sapi_module.name, "cli"))) { - /* Search the current directory - possible security risk? */ - fh.handle.fp = php_fopen_with_path("php.ini", "r", ".", &php_ini_opened_path TSRMLS_CC); + /* Search php.ini file in search path */ + if (!fh.handle.fp) + fh.handle.fp = php_fopen_with_path("php.ini", "r", php_ini_search_path, &php_ini_opened_path TSRMLS_CC); + if (free_ini_search_path) { + efree(php_ini_search_path); } -#endif - -#ifdef PHP_WIN32 - if (!fh.handle.fp) { - /* Search for php.ini in the same directory as the executable. */ - char search_path[MAX_PATH]; - if (GetModuleFileName(0,search_path,sizeof(search_path))) { - char* p = strrchr(search_path,'\\'); - if (p) *++p = 0; - fh.handle.fp = php_fopen_with_path("php.ini", "r", search_path, &php_ini_opened_path TSRMLS_CC); - } - } - if (!fh.handle.fp) { - /* Search for php.ini in the Windows base directory. */ - char search_path[MAX_PATH]; - if (GetWindowsDirectory(search_path,sizeof(search_path))) { - fh.handle.fp = php_fopen_with_path("php.ini", "r", search_path, &php_ini_opened_path TSRMLS_CC); - } - } -#else - if (!fh.handle.fp) { - /* Search for php.ini in the (platform-specific) default places. */ - fh.handle.fp = php_fopen_with_path("php.ini", "r", PHP_CONFIG_FILE_PATH, &php_ini_opened_path TSRMLS_CC); - } -#endif - PG(safe_mode) = safe_mode_state; PG(open_basedir) = open_basedir; @@ -284,11 +325,10 @@ int php_init_config(char *php_ini_path_override) fh.filename = php_ini_opened_path; zend_parse_ini_file(&fh, 1, php_config_ini_parser_cb, &extension_lists); - - /* If we succeeded in opening an INI file, preserve the name of the file opened. */ + if (php_ini_opened_path) { zval tmp; - + Z_STRLEN(tmp) = strlen(php_ini_opened_path); Z_STRVAL(tmp) = zend_strndup(php_ini_opened_path, Z_STRLEN(tmp)); Z_TYPE(tmp) = IS_STRING; @@ -296,7 +336,7 @@ int php_init_config(char *php_ini_path_override) efree(php_ini_opened_path); php_ini_opened_path = zend_strndup(Z_STRVAL(tmp), Z_STRLEN(tmp)); } - + return SUCCESS; } /* }}} */ @@ -344,7 +384,7 @@ zval *cfg_get_entry(char *name, uint name_length) PHPAPI int cfg_get_long(char *varname, long *result) { zval *tmp, var; - + if (zend_hash_find(&configuration_hash, varname, strlen(varname)+1, (void **) &tmp)==FAILURE) { *result=(long)NULL; return FAILURE; @@ -362,7 +402,7 @@ PHPAPI int cfg_get_long(char *varname, long *result) PHPAPI int cfg_get_double(char *varname, double *result) { zval *tmp, var; - + if (zend_hash_find(&configuration_hash, varname, strlen(varname)+1, (void **) &tmp)==FAILURE) { *result=(double)0; return FAILURE; diff --git a/main/php_ini.h b/main/php_ini.h index de7566b6d76..a191c3b978c 100644 --- a/main/php_ini.h +++ b/main/php_ini.h @@ -21,7 +21,7 @@ #include "zend_ini.h" -int php_init_config(char *php_ini_path_override); +int php_init_config(); int php_shutdown_config(void); void php_ini_delayed_modules_startup(TSRMLS_D); zval *cfg_get_entry(char *name, uint name_length); diff --git a/sapi/cgi/cgi_main.c b/sapi/cgi/cgi_main.c index 46b967ba559..3c31a429220 100644 --- a/sapi/cgi/cgi_main.c +++ b/sapi/cgi/cgi_main.c @@ -565,6 +565,8 @@ int main(int argc, char *argv[]) tsrm_ls = ts_resource(0); #endif + cgi_sapi_module.executable_location = argv[0]; + /* startup after we get the above ini override se we get things right */ if (php_module_startup(&cgi_sapi_module)==FAILURE) { #ifdef ZTS diff --git a/sapi/cli/php_cli.c b/sapi/cli/php_cli.c index 6f496d23b2e..3db7e6dce43 100644 --- a/sapi/cli/php_cli.c +++ b/sapi/cli/php_cli.c @@ -361,6 +361,8 @@ int main(int argc, char *argv[]) ap_php_optind = orig_optind; ap_php_optarg = orig_optarg; + cli_sapi_module.executable_location = argv[0]; + /* startup after we get the above ini override se we get things right */ if (php_module_startup(&cli_sapi_module)==FAILURE) { return FAILURE;