Fix. Find php interpreter when use CGI/FastCGI mode. (#2640)

Prev version has bug when extension_dir has relative path (e.g. `ext`)
`basedir('ext')` returns not empty string by dot symbol `.` so it going to
infinity loop.
Also official distro of PHP has no executable binaries with version (phpX.exe)
only `php.exe`.
Tested on OpenServer with PHP 5.3-7.1 under Apache/Nginx.
And also on manual install of Nginx 1.11 / PHP 5.4
This commit is contained in:
Alexey Melnichuk 2017-06-05 17:47:38 +03:00 committed by FusionPBX
parent 053c419773
commit 4115ca3bd9
1 changed files with 72 additions and 17 deletions

View File

@ -1280,15 +1280,16 @@ if (!function_exists('switch_conf_xml')) {
//prepare the php variables //prepare the php variables
if (stristr(PHP_OS, 'WIN')) { if (stristr(PHP_OS, 'WIN')) {
$bindir = find_php_by_extension(); $php_bin = win_find_php('php.exe');
if(!$bindir) if(!$php_bin){ // relay on system path
$bindir = getenv(PHPRC); $php_bin = 'php.exe';
}
$secure_path = path_join($_SERVER["DOCUMENT_ROOT"], PROJECT_PATH, 'secure'); $secure_path = path_join($_SERVER["DOCUMENT_ROOT"], PROJECT_PATH, 'secure');
$v_mail_bat = path_join($secure_path, 'mailto.bat'); $v_mail_bat = path_join($secure_path, 'mailto.bat');
$v_mail_cmd = '@' . $v_mail_cmd = '@' .
'"' . str_replace('/', '\\', path_join($bindir, 'php5.exe')) . '" ' . '"' . str_replace('/', '\\', $php_bin) . '" ' .
'"' . str_replace('/', '\\', path_join($secure_path, 'v_mailto.php')) . '" '; '"' . str_replace('/', '\\', path_join($secure_path, 'v_mailto.php')) . '" ';
$fout = fopen($v_mail_bat, "w+"); $fout = fopen($v_mail_bat, "w+");
@ -1297,7 +1298,7 @@ if (!function_exists('switch_conf_xml')) {
$v_mailer_app = '"' . str_replace('/', '\\', $v_mail_bat) . '"'; $v_mailer_app = '"' . str_replace('/', '\\', $v_mail_bat) . '"';
$v_mailer_app_args = ""; $v_mailer_app_args = "";
unset($v_mail_bat, $v_mail_cmd, $secure_path, $bindir, $fout); unset($v_mail_bat, $v_mail_cmd, $secure_path, $php_bin, $fout);
} }
else { else {
if (file_exists(PHP_BINDIR.'/php')) { define("PHP_BIN", "php"); } if (file_exists(PHP_BINDIR.'/php')) { define("PHP_BIN", "php"); }
@ -1510,6 +1511,7 @@ if(!function_exists('path_join')) {
else $prefix = ''; else $prefix = '';
} }
$path = trim( $path, '/' ); $path = trim( $path, '/' );
$path = trim( $path, '\\' );
} }
if($prefix === null){ if($prefix === null){
@ -1522,22 +1524,75 @@ if(!function_exists('path_join')) {
} }
} }
if(!function_exists('find_php_by_extension')) { if(!function_exists('win_find_php')) {
// Tested on WAMP and OpenServer function win_find_php_in_root($root, $bin){
function find_php_by_extension(){ while(true) {
$bin_dir = get_cfg_var('extension_dir'); $php_bin = path_join($root, $bin);
if(file_exists($php_bin)){
while($bin_dir){ $php_bin = str_replace('/', '\\', $php_bin);
$bin_dir = dirname($bin_dir); return $php_bin;
$php_bin = path_join($bin_dir, 'php.exe'); }
if(file_exists($php_bin)) $prev_root = $root;
break; $root = dirname($root);
if((!$root)&&($prev_root == $root)){
return false;
}
} }
}
if(!$bin_dir) //Tested on WAMP and OpenServer
//Can get wrong result if `extension_dir` set as relative path.
function win_find_php_by_extension($bin_name){
$bin_dir = get_cfg_var('extension_dir');
return win_find_php_in_root($bin_dir, $bin_name);
}
// Works since PHP 5.4
function win_find_php_by_binary($bin_name){
if(!defined('PHP_BINARY')){
return false; return false;
}
$bin_dir = realpath(PHP_BINARY);
if(!$bin_dir){
$bin_dir = PHP_BINARY;
}
$bin_dir = dirname($bin_dir);
return win_find_php_in_root($bin_dir, $bin_name);
}
return $bin_dir; function win_find_php_by_phprc($bin_name){
$bin_dir = getenv(PHPRC);
if(!$bin_dir){
return false;
}
$bin_dir = realpath($bin_dir);
return win_find_php_in_root($bin_dir, $bin_name);
}
//on Windows PHP_BIN set in compile time to c:\php
//It possible redifine it in env, but not all installation do it
function win_find_php_by_bin($bin_name){
if(!defined('PHP_BIN')){
return false;
}
$bin_dir = realpath(PHP_BIN);
if(!$bin_dir){
$bin_dir = PHP_BIN;
}
$bin_dir = dirname($bin_dir);
return win_find_php_in_root($bin_dir, $bin_name);
}
function win_find_php($bin_name){
$php_bin = win_find_php_by_binary($bin_name);
if($php_bin) return $php_bin;
$php_bin = win_find_php_by_extension($bin_name);
if($php_bin) return $php_bin;
$php_bin = win_find_php_by_bin($bin_name);
if($php_bin) return $php_bin;
$php_bin = win_find_php_by_phprc($bin_name);
if($php_bin) return $php_bin;
return false;
} }
} }