laruence Wed, 07 Dec 2011 10:33:13 +0000 Revision: http://svn.php.net/viewvc?view=revision&revision=320567
Log: Implemented FR #54514 (Get php binary path during script execution). Bug: https://bugs.php.net/54514 (Open) Get php binary path during script execution Changed paths: U php/php-src/branches/PHP_5_4/NEWS U php/php-src/branches/PHP_5_4/main/main.c U php/php-src/branches/PHP_5_4/main/php_globals.h U php/php-src/branches/PHP_5_4/main/php_ini.c A php/php-src/branches/PHP_5_4/tests/basic/bug54514.phpt U php/php-src/trunk/main/main.c U php/php-src/trunk/main/php_globals.h U php/php-src/trunk/main/php_ini.c A php/php-src/trunk/tests/basic/bug54514.phpt
Modified: php/php-src/branches/PHP_5_4/NEWS =================================================================== --- php/php-src/branches/PHP_5_4/NEWS 2011-12-07 10:28:56 UTC (rev 320566) +++ php/php-src/branches/PHP_5_4/NEWS 2011-12-07 10:33:13 UTC (rev 320567) @@ -6,6 +6,8 @@ (php at mickweiss dot com) . Fixed bug #60240 (invalid read/writes when unserializing specially crafted strings). (Mike) + . Implement FR #54514 (Get php binary path during script execution). + (Laruence) - CLI SAPI: . Implement FR #60390 (Missing $_SERVER['SERVER_PORT']). (Pierre) Modified: php/php-src/branches/PHP_5_4/main/main.c =================================================================== --- php/php-src/branches/PHP_5_4/main/main.c 2011-12-07 10:28:56 UTC (rev 320566) +++ php/php-src/branches/PHP_5_4/main/main.c 2011-12-07 10:33:13 UTC (rev 320567) @@ -255,6 +255,57 @@ } /* }}} */ +/* {{{ php_binary_init + */ +static void php_binary_init(TSRMLS_D) +{ + char *binary_location; +#ifdef PHP_WIN32 + binary_location = (char *)malloc(MAXPATHLEN); + if (GetModuleFileName(0, binary_location, MAXPATHLEN) == 0) { + free(binary_location); + PG(php_binary) = NULL; + } +#else + if (sapi_module.executable_location) { + binary_location = (char *)malloc(MAXPATHLEN); + if (!strchr(sapi_module.executable_location, '/')) { + char *envpath, *path; + int found = 0; + + if ((envpath = getenv("PATH")) != NULL) { + char *search_dir, search_path[MAXPATHLEN]; + char *last = NULL; + + path = estrdup(envpath); + search_dir = php_strtok_r(path, ":", &last); + + while (search_dir) { + snprintf(search_path, MAXPATHLEN, "%s/%s", search_dir, sapi_module.executable_location); + if (VCWD_REALPATH(search_path, binary_location) && !VCWD_ACCESS(binary_location, X_OK)) { + found = 1; + break; + } + search_dir = php_strtok_r(NULL, ":", &last); + } + efree(path); + } + if (!found) { + free(binary_location); + binary_location = NULL; + } + } else if (!VCWD_REALPATH(sapi_module.executable_location, binary_location) || VCWD_ACCESS(binary_location, X_OK)) { + free(binary_location); + binary_location = NULL; + } + } else { + binary_location = NULL; + } +#endif + PG(php_binary) = binary_location; +} +/* }}} */ + /* {{{ PHP_INI_MH */ static PHP_INI_MH(OnUpdateTimeout) @@ -1819,6 +1870,9 @@ if (core_globals->disable_classes) { free(core_globals->disable_classes); } + if (core_globals->php_binary) { + free(core_globals->php_binary); + } php_shutdown_ticks(TSRMLS_C); } @@ -2069,6 +2123,13 @@ REGISTER_MAIN_LONG_CONSTANT("PHP_WINDOWS_NT_WORKSTATION", VER_NT_WORKSTATION, CONST_PERSISTENT | CONST_CS); #endif + php_binary_init(TSRMLS_C); + if (PG(php_binary)) { + REGISTER_MAIN_STRINGL_CONSTANT("PHP_BINARY", PG(php_binary), strlen(PG(php_binary)), CONST_PERSISTENT | CONST_CS); + } else { + REGISTER_MAIN_STRINGL_CONSTANT("PHP_BINARY", "", 0, CONST_PERSISTENT | CONST_CS); + } + php_output_register_constants(TSRMLS_C); php_rfc1867_register_constants(TSRMLS_C); Modified: php/php-src/branches/PHP_5_4/main/php_globals.h =================================================================== --- php/php-src/branches/PHP_5_4/main/php_globals.h 2011-12-07 10:28:56 UTC (rev 320566) +++ php/php-src/branches/PHP_5_4/main/php_globals.h 2011-12-07 10:33:13 UTC (rev 320567) @@ -84,6 +84,7 @@ char *include_path; char *open_basedir; char *extension_dir; + char *php_binary; char *upload_tmp_dir; long upload_max_filesize; Modified: php/php-src/branches/PHP_5_4/main/php_ini.c =================================================================== --- php/php-src/branches/PHP_5_4/main/php_ini.c 2011-12-07 10:28:56 UTC (rev 320566) +++ php/php-src/branches/PHP_5_4/main/php_ini.c 2011-12-07 10:33:13 UTC (rev 320567) @@ -393,7 +393,6 @@ int search_path_size; char *default_location; char *env_location; - char *binary_location; static const char paths_separator[] = { ZEND_PATHS_SEPARATOR, 0 }; #ifdef PHP_WIN32 char *reg_location; @@ -472,52 +471,12 @@ strlcat(php_ini_search_path, ".", search_path_size); } - /* Add binary directory */ -#ifdef PHP_WIN32 - binary_location = (char *) emalloc(MAXPATHLEN); - if (GetModuleFileName(0, binary_location, MAXPATHLEN) == 0) { - efree(binary_location); - binary_location = NULL; - } -#else - if (sapi_module.executable_location) { - binary_location = (char *)emalloc(MAXPATHLEN); - if (!strchr(sapi_module.executable_location, '/')) { - char *envpath, *path; - int found = 0; + if (PG(php_binary)) { + char *separator_location, *binary_location; - if ((envpath = getenv("PATH")) != NULL) { - char *search_dir, search_path[MAXPATHLEN]; - char *last = NULL; + binary_location = estrdup(PG(php_binary)); + separator_location = strrchr(binary_location, DEFAULT_SLASH); - path = estrdup(envpath); - search_dir = php_strtok_r(path, ":", &last); - - while (search_dir) { - snprintf(search_path, MAXPATHLEN, "%s/%s", search_dir, sapi_module.executable_location); - if (VCWD_REALPATH(search_path, binary_location) && !VCWD_ACCESS(binary_location, X_OK)) { - found = 1; - break; - } - search_dir = php_strtok_r(NULL, ":", &last); - } - efree(path); - } - if (!found) { - efree(binary_location); - binary_location = NULL; - } - } else if (!VCWD_REALPATH(sapi_module.executable_location, binary_location) || VCWD_ACCESS(binary_location, X_OK)) { - efree(binary_location); - binary_location = NULL; - } - } else { - binary_location = NULL; - } -#endif - if (binary_location) { - char *separator_location = strrchr(binary_location, DEFAULT_SLASH); - if (separator_location && separator_location != binary_location) { *(separator_location) = 0; } Added: php/php-src/branches/PHP_5_4/tests/basic/bug54514.phpt =================================================================== --- php/php-src/branches/PHP_5_4/tests/basic/bug54514.phpt (rev 0) +++ php/php-src/branches/PHP_5_4/tests/basic/bug54514.phpt 2011-12-07 10:33:13 UTC (rev 320567) @@ -0,0 +1,9 @@ +--TEST-- +Req #54514 (Get php binary path during script execution) +--FILE-- +<?php +if(getenv('TEST_PHP_EXECUTABLE') === PHP_BINARY) { + echo "done"; +} +--EXPECT-- +done Modified: php/php-src/trunk/main/main.c =================================================================== --- php/php-src/trunk/main/main.c 2011-12-07 10:28:56 UTC (rev 320566) +++ php/php-src/trunk/main/main.c 2011-12-07 10:33:13 UTC (rev 320567) @@ -255,6 +255,57 @@ } /* }}} */ +/* {{{ php_binary_init + */ +static void php_binary_init(TSRMLS_D) +{ + char *binary_location; +#ifdef PHP_WIN32 + binary_location = (char *)malloc(MAXPATHLEN); + if (GetModuleFileName(0, binary_location, MAXPATHLEN) == 0) { + free(binary_location); + PG(php_binary) = NULL; + } +#else + if (sapi_module.executable_location) { + binary_location = (char *)malloc(MAXPATHLEN); + if (!strchr(sapi_module.executable_location, '/')) { + char *envpath, *path; + int found = 0; + + if ((envpath = getenv("PATH")) != NULL) { + char *search_dir, search_path[MAXPATHLEN]; + char *last = NULL; + + path = estrdup(envpath); + search_dir = php_strtok_r(path, ":", &last); + + while (search_dir) { + snprintf(search_path, MAXPATHLEN, "%s/%s", search_dir, sapi_module.executable_location); + if (VCWD_REALPATH(search_path, binary_location) && !VCWD_ACCESS(binary_location, X_OK)) { + found = 1; + break; + } + search_dir = php_strtok_r(NULL, ":", &last); + } + efree(path); + } + if (!found) { + free(binary_location); + binary_location = NULL; + } + } else if (!VCWD_REALPATH(sapi_module.executable_location, binary_location) || VCWD_ACCESS(binary_location, X_OK)) { + free(binary_location); + binary_location = NULL; + } + } else { + binary_location = NULL; + } +#endif + PG(php_binary) = binary_location; +} +/* }}} */ + /* {{{ PHP_INI_MH */ static PHP_INI_MH(OnUpdateTimeout) @@ -1819,6 +1870,9 @@ if (core_globals->disable_classes) { free(core_globals->disable_classes); } + if (core_globals->php_binary) { + free(core_globals->php_binary); + } php_shutdown_ticks(TSRMLS_C); } @@ -2069,6 +2123,13 @@ REGISTER_MAIN_LONG_CONSTANT("PHP_WINDOWS_NT_WORKSTATION", VER_NT_WORKSTATION, CONST_PERSISTENT | CONST_CS); #endif + php_binary_init(TSRMLS_C); + if (PG(php_binary)) { + REGISTER_MAIN_STRINGL_CONSTANT("PHP_BINARY", PG(php_binary), strlen(PG(php_binary)), CONST_PERSISTENT | CONST_CS); + } else { + REGISTER_MAIN_STRINGL_CONSTANT("PHP_BINARY", "", 0, CONST_PERSISTENT | CONST_CS); + } + php_output_register_constants(TSRMLS_C); php_rfc1867_register_constants(TSRMLS_C); Modified: php/php-src/trunk/main/php_globals.h =================================================================== --- php/php-src/trunk/main/php_globals.h 2011-12-07 10:28:56 UTC (rev 320566) +++ php/php-src/trunk/main/php_globals.h 2011-12-07 10:33:13 UTC (rev 320567) @@ -84,6 +84,7 @@ char *include_path; char *open_basedir; char *extension_dir; + char *php_binary; char *upload_tmp_dir; long upload_max_filesize; Modified: php/php-src/trunk/main/php_ini.c =================================================================== --- php/php-src/trunk/main/php_ini.c 2011-12-07 10:28:56 UTC (rev 320566) +++ php/php-src/trunk/main/php_ini.c 2011-12-07 10:33:13 UTC (rev 320567) @@ -393,7 +393,6 @@ int search_path_size; char *default_location; char *env_location; - char *binary_location; static const char paths_separator[] = { ZEND_PATHS_SEPARATOR, 0 }; #ifdef PHP_WIN32 char *reg_location; @@ -472,52 +471,12 @@ strlcat(php_ini_search_path, ".", search_path_size); } - /* Add binary directory */ -#ifdef PHP_WIN32 - binary_location = (char *) emalloc(MAXPATHLEN); - if (GetModuleFileName(0, binary_location, MAXPATHLEN) == 0) { - efree(binary_location); - binary_location = NULL; - } -#else - if (sapi_module.executable_location) { - binary_location = (char *)emalloc(MAXPATHLEN); - if (!strchr(sapi_module.executable_location, '/')) { - char *envpath, *path; - int found = 0; + if (PG(php_binary)) { + char *separator_location, *binary_location; - if ((envpath = getenv("PATH")) != NULL) { - char *search_dir, search_path[MAXPATHLEN]; - char *last = NULL; + binary_location = estrdup(PG(php_binary)); + separator_location = strrchr(binary_location, DEFAULT_SLASH); - path = estrdup(envpath); - search_dir = php_strtok_r(path, ":", &last); - - while (search_dir) { - snprintf(search_path, MAXPATHLEN, "%s/%s", search_dir, sapi_module.executable_location); - if (VCWD_REALPATH(search_path, binary_location) && !VCWD_ACCESS(binary_location, X_OK)) { - found = 1; - break; - } - search_dir = php_strtok_r(NULL, ":", &last); - } - efree(path); - } - if (!found) { - efree(binary_location); - binary_location = NULL; - } - } else if (!VCWD_REALPATH(sapi_module.executable_location, binary_location) || VCWD_ACCESS(binary_location, X_OK)) { - efree(binary_location); - binary_location = NULL; - } - } else { - binary_location = NULL; - } -#endif - if (binary_location) { - char *separator_location = strrchr(binary_location, DEFAULT_SLASH); - if (separator_location && separator_location != binary_location) { *(separator_location) = 0; } Added: php/php-src/trunk/tests/basic/bug54514.phpt =================================================================== --- php/php-src/trunk/tests/basic/bug54514.phpt (rev 0) +++ php/php-src/trunk/tests/basic/bug54514.phpt 2011-12-07 10:33:13 UTC (rev 320567) @@ -0,0 +1,9 @@ +--TEST-- +Req #54514 (Get php binary path during script execution) +--FILE-- +<?php +if(getenv('TEST_PHP_EXECUTABLE') === PHP_BINARY) { + echo "done"; +} +--EXPECT-- +done
-- PHP CVS Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php