CommonFunctions::_findProgram PHP Method

_findProgram() private static method

Find a system program, do also path checking when not running on WINNT on WINNT we simply return the name with the exe extension to the program name
private static _findProgram ( string $strProgram ) : string
$strProgram string name of the program
return string complete path and name of the program
    private static function _findProgram($strProgram)
    {
        $path_parts = pathinfo($strProgram);
        if (empty($path_parts['basename'])) {
            return;
        }
        $arrPath = array();
        if (empty($path_parts['dirname']) || $path_parts['dirname'] == '.') {
            if (PSI_OS == 'WINNT' && empty($path_parts['extension'])) {
                $strProgram .= '.exe';
                $path_parts = pathinfo($strProgram);
            }
            if (PSI_OS == 'WINNT') {
                $arrPath = preg_split('/;/', getenv("Path"), -1, PREG_SPLIT_NO_EMPTY);
            } else {
                $arrPath = preg_split('/:/', getenv("PATH"), -1, PREG_SPLIT_NO_EMPTY);
            }
            if (defined('PSI_ADD_PATHS') && is_string(PSI_ADD_PATHS)) {
                if (preg_match(ARRAY_EXP, PSI_ADD_PATHS)) {
                    $arrPath = array_merge(eval(PSI_ADD_PATHS), $arrPath);
                    // In this order so $addpaths is before $arrPath when looking for a program
                } else {
                    $arrPath = array_merge(array(PSI_ADD_PATHS), $arrPath);
                    // In this order so $addpaths is before $arrPath when looking for a program
                }
            }
        } else {
            //directory defined
            array_push($arrPath, $path_parts['dirname']);
            $strProgram = $path_parts['filename'];
            $strProgram .= '.' . $path_parts['extension'];
        }
        //add some default paths if we still have no paths here
        if (empty($arrPath) && PSI_OS != 'WINNT') {
            if (PSI_OS == 'Android') {
                array_push($arrPath, '/system/bin');
            } else {
                array_push($arrPath, '/bin', '/sbin', '/usr/bin', '/usr/sbin', '/usr/local/bin', '/usr/local/sbin');
            }
        }
        $exceptPath = "";
        if (PSI_OS == 'WINNT' && ($windir = getenv("WinDir")) !== false) {
            $windir = strtolower($windir);
            foreach ($arrPath as $strPath) {
                if (strtolower($strPath) == $windir . "\\system32" && is_dir($windir . "\\SysWOW64")) {
                    $exceptPath = $windir . "\\sysnative";
                    array_push($arrPath, $exceptPath);
                    break;
                }
            }
        } elseif (PSI_OS == 'Android') {
            $exceptPath = '/system/bin';
        }
        foreach ($arrPath as $strPath) {
            // Path with and without trailing slash
            if (PSI_OS == 'WINNT') {
                $strPath = rtrim($strPath, "\\");
                $strPathS = $strPath . "\\";
            } else {
                $strPath = rtrim($strPath, "/");
                $strPathS = $strPath . "/";
            }
            if ($strPath !== $exceptPath && !is_dir($strPath)) {
                continue;
            }
            if (PSI_OS == 'WINNT') {
                $strProgrammpath = $strPathS . $strProgram;
            } else {
                $strProgrammpath = $strPathS . $strProgram;
            }
            if (is_executable($strProgrammpath)) {
                return $strProgrammpath;
            }
        }
    }