CommonFunctions::executeProgram PHP Method

executeProgram() public static method

does very crude pipe checking. you need ' | ' for it to work ie $program = CommonFunctions::executeProgram('netstat', '-anp | grep LIST'); NOT $program = CommonFunctions::executeProgram('netstat', '-anp|grep LIST');
public static executeProgram ( string $strProgramname, string $strArgs, &$strBuffer, boolean $booErrorRep = true ) : boolean
$strProgramname string name of the program
$strArgs string arguments to the program
$booErrorRep boolean en- or disables the reporting of errors which should be logged
return boolean command successfull or not
    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;
    }

Usage Example

 /**
  * fill the private content var through command or data access
  */
 public function __construct()
 {
     parent::__construct();
     switch (defined('PSI_SENSOR_LMSENSORS_ACCESS') ? strtolower(PSI_SENSOR_LMSENSORS_ACCESS) : 'command') {
         case 'command':
             if (CommonFunctions::executeProgram("sensors", "", $lines)) {
                 // Martijn Stolk: Dirty fix for misinterpreted output of sensors,
                 // where info could come on next line when the label is too long.
                 $lines = str_replace(":\n", ":", $lines);
                 $lines = str_replace("\n\n", "\n", $lines);
                 $this->_lines = preg_split("/\n/", $lines, -1, PREG_SPLIT_NO_EMPTY);
             }
             break;
         case 'data':
             if (CommonFunctions::rfts(APP_ROOT . '/data/lmsensors.txt', $lines)) {
                 $lines = str_replace(":\n", ":", $lines);
                 $lines = str_replace("\n\n", "\n", $lines);
                 $this->_lines = preg_split("/\n/", $lines, -1, PREG_SPLIT_NO_EMPTY);
             }
             break;
         default:
             $this->error->addConfigError('__construct()', 'PSI_SENSOR_LMSENSORS_ACCESS');
             break;
     }
 }
All Usage Examples Of CommonFunctions::executeProgram