]> _ Git - cubist_util.git/commitdiff
wip #2602 @0.25
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 20 Feb 2019 17:17:33 +0000 (18:17 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 20 Feb 2019 17:17:33 +0000 (18:17 +0100)
src/Cubist/Util/CommandLine.php [new file with mode: 0644]
src/Cubist/Util/Zip.php

diff --git a/src/Cubist/Util/CommandLine.php b/src/Cubist/Util/CommandLine.php
new file mode 100644 (file)
index 0000000..2de9c41
--- /dev/null
@@ -0,0 +1,261 @@
+<?php
+
+namespace Cubist\Util;
+class CommandLine
+{
+
+       protected $program;
+       protected $args = array();
+       protected $env = array();
+       protected $cd = null;
+       protected $commande = null;
+       protected $output;
+       protected $temp_output = true;
+       protected $error;
+       protected $nohup = false;
+       protected $execTime;
+       protected $lang;
+       protected $ssh = null;
+       protected $longArgSeparator = '=';
+
+       function __construct($program, $output = null, $error = true)
+       {
+               if (stristr($program, ' ')) {
+                       $program = '"' . $program . '"';
+               }
+               $this->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)));
+               }*/
+       }
+
+}
index 3a9e03b21e3239fd2d9196954f1cf371b1de900a..498b9026b9bca9e92ee00167a402028639274de3 100644 (file)
@@ -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);