From a07b9d51f48ac3029c9e7ad08dc7d1be106d4965 Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Wed, 20 Feb 2019 18:17:33 +0100 Subject: [PATCH] wip #2602 @0.25 --- src/Cubist/Util/CommandLine.php | 261 ++++++++++++++++++++++++++++++++ src/Cubist/Util/Zip.php | 4 +- 2 files changed, 263 insertions(+), 2 deletions(-) create mode 100644 src/Cubist/Util/CommandLine.php diff --git a/src/Cubist/Util/CommandLine.php b/src/Cubist/Util/CommandLine.php new file mode 100644 index 0000000..2de9c41 --- /dev/null +++ b/src/Cubist/Util/CommandLine.php @@ -0,0 +1,261 @@ +program = $program; + $this->commande = null; + if (null === $output) { + $this->output = tempnam(sys_get_temp_dir(), 'Cubeist'); + } else { + $this->temp_output = false; + $this->output = $output; + } + $this->error = $error; + } + + public function setArg($name = null, $val = null) + { + if (null === $name && null === $val) { + return; + } + if (stristr($val, ' ') && !stristr($val, '>') && !stristr($val, '<')) { + $val = '"' . $val . '"'; + } + $this->args[] = array($name, $val); + } + + public function __get($varname) + { + if ($varname == 'output') { + return file_get_contents($this->output); + } elseif ($varname == 'outputfile') { + return $this->output; + } elseif ($varname == 'commande' || $varname == 'command') { + if (null === $this->commande) { + $this->makeCommande($this->output, $this->error); + } + return $this->commande; + } elseif ($varname == 'execTime') { + return $this->execTime; + } + } + + public function cd($path) + { + $this->cd = $path; + } + + public function setLang($lang) + { + $this->setEnv('LANG', $lang); + } + + public function setEnv($name, $val) + { + if (PATH_SEPARATOR == ':') { + $this->env[$name] = $val; + } + } + + public function setPath($val) + { + if (PATH_SEPARATOR == ':') { + $this->setEnv('PATH', $val); + } else { + $this->program = $val . '' . $this->program; + } + } + + public function setNohup($val) + { + $this->nohup = $val; + } + + public function setManualArg($val) + { + $this->args[] = array(null, $val); + } + + protected function _preExecute() + { + + } + + public function execute($fonction = 'shell_exec') + { + $startTime = microtime(true); + $this->_preExecute(); + if ($fonction instanceof CubeIT_Networking_SSH2) { + $this->makeCommande(); + $o = $fonction->exec($this->commande); + file_put_contents($this->output, $o['output'] . "\n\n---\n\n" . $o['error']); + } else if (null === $this->ssh) { + $this->makeCommande($this->output, $this->error); + $fonction($this->commande); + } else { + $this->makeCommande(); + + $key = ''; + if (null !== $this->ssh['key']) { + $key = ' -i ' . $this->ssh['key']; + } + + $c = 'echo "' . $this->commande . '" | ssh ' . $key . ' -p ' . $this->ssh['port'] . ' ' . $this->ssh['username'] . '@' . $this->ssh['host'] . ' \'bash -s\''; + $c .= ' > ' . $this->output; + if ($this->error) { + $c .= ' 2>&1 '; + } + + $fonction($c); + } + $endTime = microtime(true); + $this->execTime = $endTime - $startTime; + } + + public function __set($varname, $value) + { + $this->setArg($varname, $value); + } + + public function __destruct() + { + if ($this->temp_output && file_exists($this->output)) { + //unlink($this->output); + } + } + + public function setLongArgumentSeparator($separator = "=") + { + $this->longArgSeparator = $separator; + return $this; + } + + public function makeCommande($output = false, $error = false) + { + $commande = $this->program; + $commandes = array(); + foreach ($this->args as $arg) { + if (strlen($arg[0]) == 1) { + $commande .= ' -' . $arg[0]; + if (null !== $arg[1]) { + $commande .= ' ' . $arg[1]; + } + } elseif (null === $arg[0]) { + $commande .= ' ' . $arg[1]; + } else { + if (substr($arg[0], 0, 1) == '-') { + $commande .= ' ' . $arg[0]; + if (null !== $arg[1]) { + $commande .= ' ' . $arg[1]; + } + } else { + $commande .= ' --' . $arg[0]; + if (null !== $arg[1]) { + $commande .= $this->longArgSeparator . $arg[1]; + } + } + } + } + + if (null !== $this->cd) { + $commandes[] = 'cd ' . $this->cd; + } + if (count($this->env) > 0) { + foreach ($this->env as $var => $val) { + if ($var == 'PATH') { + $commandes[] = 'export ' . $var . '=$' . $var . ':' . $val; + } else { + $commandes[] = 'export ' . $var . '=' . $val; + } + } + } + + if ($this->nohup) { + $commande = 'nohup ' . $commande; + } + + $commandes[] = $commande; + + $this->commande = implode(';', $commandes); + + if ($output) { + $this->commande .= ' > ' . $output; + } + if ($error) { + $this->commande .= ' 2>&1 '; + } + if ($this->nohup) { + $this->commande .= ' & echo $!'; + } + } + + public static function getArgs($argv) + { + $filename = array_shift($argv); + + $i = 0; + $res = array(); + $skip = false; + foreach ($argv as $j => $v) { + if ($skip) { + $skip = false; + continue; + } + if (substr($v, 0, 2) == '--') { + list($k, $value) = explode('=', $v); + $res[trim($k, '-')] = trim($value, ' "\''); + } elseif (substr($v, 0, 1) == '-') { + $res[trim($v, '-')] = trim($argv[$j + 1], ' \'"'); + $skip = true; + } else { + $res[$i] = trim($v, "'\" "); + } + $i++; + } + return $res; + } + + public function setSSH($host, $username, $password, $port = 22, $key = null) + { + $this->ssh = array('host' => $host, 'username' => $username, 'password' => $password, 'port' => $port, 'key' => $key); + } + + public function debug($debugfunc = array('ChromePhp', 'log')) + { + + $e = explode('/', $this->program); + $p = array_pop($e); + file_put_contents('/tmp/' . $p . '.' . microtime(true) . '.txt', $this->commande . "\n\n" . file_get_contents($this->output)); + // if (!is_callable($debugfunc)) { + // return; + // } + + /*if ($debugfunc == 'fb') { + call_user_func($debugfunc, file_get_contents($this->output), $this->commande); + } else { + call_user_func($debugfunc, array('commande' => $this->commande, 'output' => file_get_contents($this->output))); + }*/ + } + +} diff --git a/src/Cubist/Util/Zip.php b/src/Cubist/Util/Zip.php index 3a9e03b..498b902 100644 --- a/src/Cubist/Util/Zip.php +++ b/src/Cubist/Util/Zip.php @@ -11,7 +11,7 @@ class Zip mkdir($dir, 0777, true); } if (true) { - $cl = new CubeIT_CommandLine('unzip'); + $cl = new CommandLine('unzip'); $cl->cd($dir); $cl->setArg('o'); $cl->setArg(null, $zip); @@ -28,7 +28,7 @@ class Zip { $zipexe = 'zip'; - $cl = new CubeIT_CommandLine($zipexe); + $cl = new CommandLine($zipexe); if (is_array($source)) { $cl->setArg('j'); $cl->setArg(null, $zip); -- 2.39.5