public static function executeProgram($strProgramname, $strArgs, &$strBuffer, $booErrorRep = true)
{
if (defined('PSI_LOG') && is_string(PSI_LOG) && strlen(PSI_LOG) > 0 && (substr(PSI_LOG, 0, 1) == "-" || substr(PSI_LOG, 0, 1) == "+")) {
$out = self::_parse_log_file("Executing: " . trim($strProgramname . ' ' . $strArgs));
if ($out == false) {
if (substr(PSI_LOG, 0, 1) == "-") {
$strBuffer = '';
return false;
}
} else {
$strBuffer = $out;
return true;
}
}
$strBuffer = '';
$strError = '';
$pipes = array();
$strProgram = self::_findProgram($strProgramname);
$error = PSI_Error::singleton();
if (!$strProgram) {
if ($booErrorRep) {
$error->addError('find_program("' . $strProgramname . '")', 'program not found on the machine');
}
return false;
} else {
if (preg_match('/\\s/', $strProgram)) {
$strProgram = '"' . $strProgram . '"';
}
}
if (PSI_OS !== 'WINNT' && defined('PSI_SUDO_COMMANDS') && is_string(PSI_SUDO_COMMANDS)) {
if (preg_match(ARRAY_EXP, PSI_SUDO_COMMANDS)) {
$sudocommands = eval(PSI_SUDO_COMMANDS);
} else {
$sudocommands = array(PSI_SUDO_COMMANDS);
}
if (in_array($strProgramname, $sudocommands)) {
$sudoProgram = self::_findProgram("sudo");
if (!$sudoProgram) {
if ($booErrorRep) {
$error->addError('find_program("sudo")', 'program not found on the machine');
}
return false;
} else {
if (preg_match('/\\s/', $sudoProgram)) {
$strProgram = '"' . $sudoProgram . '" ' . $strProgram;
} else {
$strProgram = $sudoProgram . ' ' . $strProgram;
}
}
}
}
// see if we've gotten a |, if we have we need to do path checking on the cmd
if ($strArgs) {
$arrArgs = preg_split('/ /', $strArgs, -1, PREG_SPLIT_NO_EMPTY);
for ($i = 0, $cnt_args = count($arrArgs); $i < $cnt_args; $i++) {
if ($arrArgs[$i] == '|') {
$strCmd = $arrArgs[$i + 1];
$strNewcmd = self::_findProgram($strCmd);
$strArgs = preg_replace("/\\| " . $strCmd . '/', '| "' . $strNewcmd . '"', $strArgs);
}
}
$strArgs = ' ' . $strArgs;
}
$descriptorspec = array(0 => array("pipe", "r"), 1 => array("pipe", "w"), 2 => array("pipe", "w"));
if (defined("PSI_MODE_POPEN") && PSI_MODE_POPEN === true) {
if (PSI_OS == 'WINNT') {
$process = $pipes[1] = popen($strProgram . $strArgs . " 2>nul", "r");
} else {
$process = $pipes[1] = popen($strProgram . $strArgs . " 2>/dev/null", "r");
}
} else {
$process = proc_open($strProgram . $strArgs, $descriptorspec, $pipes);
}
if (is_resource($process)) {
self::_timeoutfgets($pipes, $strBuffer, $strError);
if (defined("PSI_MODE_POPEN") && PSI_MODE_POPEN === true) {
$return_value = pclose($pipes[1]);
} else {
fclose($pipes[0]);
fclose($pipes[1]);
fclose($pipes[2]);
// It is important that you close any pipes before calling
// proc_close in order to avoid a deadlock
$return_value = proc_close($process);
}
} else {
if ($booErrorRep) {
$error->addError($strProgram, "\nOpen process error");
}
return false;
}
$strError = trim($strError);
$strBuffer = trim($strBuffer);
if (defined('PSI_LOG') && is_string(PSI_LOG) && strlen(PSI_LOG) > 0 && substr(PSI_LOG, 0, 1) != "-" && substr(PSI_LOG, 0, 1) != "+") {
error_log("---" . gmdate('r T') . "--- Executing: " . trim($strProgramname . $strArgs) . "\n" . $strBuffer . "\n", 3, PSI_LOG);
}
if (!empty($strError)) {
if ($booErrorRep) {
$error->addError($strProgram, $strError . "\nReturn value: " . $return_value);
}
return $return_value == 0;
}
return true;
}