2020-02-18 21:42:15 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Nominatim;
|
|
|
|
|
|
|
|
class Shell
|
|
|
|
{
|
|
|
|
public function __construct($sBaseCmd, ...$aParams)
|
|
|
|
{
|
|
|
|
if (!$sBaseCmd) {
|
2021-01-17 23:02:50 +03:00
|
|
|
throw new \Exception('Command missing in new() call');
|
2020-02-18 21:42:15 +03:00
|
|
|
}
|
|
|
|
$this->baseCmd = $sBaseCmd;
|
|
|
|
$this->aParams = array();
|
|
|
|
$this->aEnv = null; // null = use the same environment as the current PHP process
|
|
|
|
|
|
|
|
$this->stdoutString = null;
|
|
|
|
|
|
|
|
foreach ($aParams as $sParam) {
|
|
|
|
$this->addParams($sParam);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function addParams(...$aParams)
|
|
|
|
{
|
|
|
|
foreach ($aParams as $sParam) {
|
|
|
|
if (isset($sParam) && $sParam !== null && $sParam !== '') {
|
|
|
|
array_push($this->aParams, $sParam);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function addEnvPair($sKey, $sVal)
|
|
|
|
{
|
|
|
|
if (isset($sKey) && $sKey && isset($sVal)) {
|
|
|
|
if (!isset($this->aEnv)) $this->aEnv = $_ENV;
|
|
|
|
$this->aEnv = array_merge($this->aEnv, array($sKey => $sVal), $_ENV);
|
|
|
|
}
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function escapedCmd()
|
|
|
|
{
|
|
|
|
$aEscaped = array_map(function ($sParam) {
|
|
|
|
return $this->escapeParam($sParam);
|
|
|
|
}, array_merge(array($this->baseCmd), $this->aParams));
|
|
|
|
|
|
|
|
return join(' ', $aEscaped);
|
|
|
|
}
|
|
|
|
|
2021-02-24 19:21:45 +03:00
|
|
|
public function run($bExitOnFail = false)
|
2020-02-18 21:42:15 +03:00
|
|
|
{
|
|
|
|
$sCmd = $this->escapedCmd();
|
|
|
|
// $aEnv does not need escaping, proc_open seems to handle it fine
|
|
|
|
|
|
|
|
$aFDs = array(
|
|
|
|
0 => array('pipe', 'r'),
|
|
|
|
1 => STDOUT,
|
|
|
|
2 => STDERR
|
|
|
|
);
|
|
|
|
$aPipes = null;
|
|
|
|
$hProc = @proc_open($sCmd, $aFDs, $aPipes, null, $this->aEnv);
|
|
|
|
if (!is_resource($hProc)) {
|
|
|
|
throw new \Exception('Unable to run command: ' . $sCmd);
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose($aPipes[0]); // no stdin
|
|
|
|
|
|
|
|
$iStat = proc_close($hProc);
|
2021-02-24 00:50:23 +03:00
|
|
|
|
|
|
|
if ($iStat != 0 && $bExitOnFail) {
|
|
|
|
exit($iStat);
|
|
|
|
}
|
|
|
|
|
2020-02-18 21:42:15 +03:00
|
|
|
return $iStat;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private function escapeParam($sParam)
|
|
|
|
{
|
|
|
|
if (preg_match('/^-*\w+$/', $sParam)) return $sParam;
|
|
|
|
return escapeshellarg($sParam);
|
|
|
|
}
|
|
|
|
}
|