]> _ Git - cubist_net.git/commitdiff
wait #6634 @0.5
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 17 Jan 2024 18:40:03 +0000 (19:40 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 17 Jan 2024 18:40:03 +0000 (19:40 +0100)
src/SSH2.php

index 90136a7fee2adfec1ea735c3e233e085bd9ab979..bbec4c54336b1ff18baabadb79947f35f01e7a71 100644 (file)
 <?php
 
 namespace Cubist\Net;
-class SSH2 {
-
-       protected $_host;
-       protected $_username;
-       protected $_password;
-       protected $_port = 22;
-       protected $_key = null;
-       protected $_connection = null;
-       protected $_sftp = null;
-       public $convertToCygwinPath = false;
-
-       public function __construct($host, $username, $password, $port = 22) {
-               $this->setHost($host)
-                       ->setUsername($username)
-                       ->setPassword($password)
-                       ->setPort($port);
-       }
-
-       public function getHost() {
-               return $this->_host;
-       }
-
-       public function getUsername() {
-               return $this->_username;
-       }
-
-       public function getPassword() {
-               return $this->_password;
-       }
-
-       public function getPort() {
-               return $this->_port;
-       }
-
-       public function setPort($port) {
-               $this->_port = $port;
-               return $this;
-       }
-
-       public function setHost($host) {
-               $this->_host = $host;
-               return $this;
-       }
-
-       public function setUsername($username) {
-               $this->_username = $username;
-               return $this;
-       }
-
-       public function setPassword($password) {
-               $this->_password = $password;
-               return $this;
-       }
-
-       public function _connect() {
-               if (is_null($this->_connection)) {
-                       $this->_connection = ssh2_connect($this->getHost(), $this->getPort());
-                       ssh2_auth_password($this->_connection, $this->getUsername(), $this->getPassword());
-               }
-               return $this->_connection;
-       }
-
-       public function getSFTPConnection() {
-               if (is_null($this->_sftp)) {
-                       $this->_connect();
-                       $this->_sftp = ssh2_sftp($this->_connection);
-               }
-               return $this->_sftp;
-       }
-
-
-       public function getSFTPWrapper() {
-               return 'ssh2.sftp://' . intval($this->getSFTPConnection()) . '/';
-       }
-
-       public function filePutContents($filename, $data) {
-               $dir = dirname($filename);
-               $this->mkdir($dir);
-               file_put_contents($this->_filename($filename), $data);
-       }
-
-       public function fileGetContents($filename) {
-               return file_get_contents($this->_filename($filename));
-       }
-
-       public function fileReplaceContents($string, $replacement, $filename) {
-               $contents = $this->fileGetContents($filename);
-               file_put_contents('/tmp/get', $contents);
-               $contents = str_replace($string, $replacement, $contents);
-               file_put_contents('/tmp/replaced', $string . "\n\n" . $replacement . "\n\n" . $contents);
-               $this->filePutContents($filename, $contents);
-       }
-
-       protected function _filename($filename) {
-               if ($this->convertToCygwinPath) {
-                       $filename = $this->_escapeCygwin($filename);
-               }
-               return $this->getSFTPWrapper() . ltrim($filename, '/');
-       }
-
-       public function path($path) {
-               return $this->_filename($path);
-       }
-
-       public function filesize($filename) {
-               return filesize($this->_filename($filename));
-       }
-
-       public function file_exists($filename) {
-               return file_exists($this->_filename($filename));
-       }
-
-       public function mkdir($dirname, $mode = 0777, $recursive = true) {
-               if (!$this->file_exists($dirname)) {
-                       return ssh2_sftp_mkdir($this->getSFTPConnection(), $dirname, $mode, $recursive);
-               }
-               return true;
-       }
-
-
-       /**
-        * @param $local string
-        * @param $remote string
-        * @param $mode int
-        * @return bool
-        */
-       public function send($local, $remote, $mode = 0644) {
-               if (!file_exists($local)) {
-                       throw new \Exception('Local file ' . $local . ' not found');
-               }
-               $dir = dirname($remote);
-               $this->mkdir($dir);
-               $cmd = 'scp -P' . $this->getPort() . ' ' . $local . ' ' . $this->getUsername() . '@' . $this->getHost() . ':/' . ltrim($remote, '/');
-               return `$cmd`;
-       }
-
-
-       /**
-        * @param $remote string
-        * @param $local string
-        * @return bool
-        */
-       public function recv($remote, $local) {
-               $cmd = 'scp -P' . $this->getPort() . ' ' . $this->getUsername() . '@' . $this->getHost() . ':/' . ltrim($remote, '/') . ' ' . $local;
-               return `$cmd`;
-       }
-
-       /**
-        * @param $local string
-        * @param $remote string
-        * @param $mode int
-        * @return bool
-        */
-       public function slowsend($local, $remote, $mode = 0644) {
-               if (!file_exists($local)) {
-                       throw new \Exception('Local file ' . $local . ' not found');
-               }
-               $dir = dirname($remote);
-               $this->mkdir($dir);
-               return $this->_copy($local, $this->_filename($remote));
-       }
-
-       protected function _copy($from, $to, $send = true) {
-               if (class_exists('Cubist\Util\Files\Files')) {
-                       $res = \Cubist\Util\Files\Files::copyFile($from, $to);
-               } else {
-                       $res = copy($from, $to);
-               }
-               return $res;
-       }
-
-       /**
-        * @param $remote string
-        * @param $local string
-        * @return bool
-        */
-       public function slowrecv($remote, $local) {
-               return $this->_copy($this->_filename($remote), $local, false);
-       }
-
-       public function unlink($file) {
-               if ($this->file_exists($file)) {
-                       return ssh2_sftp_unlink($this->getSFTPConnection(), $file);
-               }
-       }
-
-
-       public function exec($commande) {
-               // http://www.php.net/manual/fr/function.ssh2-exec.php#99089
-               $stream = ssh2_exec($this->_connect(), $commande);
-               $errorStream = ssh2_fetch_stream($stream, SSH2_STREAM_STDERR);
-
-               // Enable blocking for both streams
-               stream_set_blocking($errorStream, true);
-               stream_set_blocking($stream, true);
-
-               $stream_size_limit = 100000000;
-               $res = ['output' => stream_get_contents($stream, $stream_size_limit), 'error' => stream_get_contents($errorStream, $stream_size_limit)];
-
-               // Close the streams
-               fclose($errorStream);
-               fclose($stream);
-               return $res;
-       }
-
-       protected function _escapeCygwin($url) {
-               $url = preg_replace('/\/([a-z]):\//i', '/cygdrive/\1/', '/' . $url);
-               return $url;
-       }
+class SSH2
+{
+
+    protected $_host;
+    protected $_username;
+    protected $_password;
+    protected $_port = 22;
+    protected $_key = null;
+    protected $_connection = null;
+    protected $_sftp = null;
+    public $convertToCygwinPath = false;
+
+    public function __construct($host, $username, $password, $port = 22)
+    {
+        $this->setHost($host)
+            ->setUsername($username)
+            ->setPassword($password)
+            ->setPort($port);
+    }
+
+    public function getHost()
+    {
+        return $this->_host;
+    }
+
+    public function getUsername()
+    {
+        return $this->_username;
+    }
+
+    public function getPassword()
+    {
+        return $this->_password;
+    }
+
+    public function getPort()
+    {
+        return $this->_port;
+    }
+
+    public function setPort($port)
+    {
+        $this->_port = $port;
+        return $this;
+    }
+
+    public function setHost($host)
+    {
+        $this->_host = $host;
+        return $this;
+    }
+
+    public function setUsername($username)
+    {
+        $this->_username = $username;
+        return $this;
+    }
+
+    public function setPassword($password)
+    {
+        $this->_password = $password;
+        return $this;
+    }
+
+    public function _connect()
+    {
+        if (is_null($this->_connection)) {
+            $this->_connection = ssh2_connect($this->getHost(), $this->getPort());
+            ssh2_auth_password($this->_connection, $this->getUsername(), $this->getPassword());
+        }
+        return $this->_connection;
+    }
+
+    public function getSFTPConnection()
+    {
+        if (is_null($this->_sftp)) {
+            $this->_connect();
+            $this->_sftp = ssh2_sftp($this->_connection);
+        }
+        return $this->_sftp;
+    }
+
+
+    public function getSFTPWrapper()
+    {
+        return 'ssh2.sftp://' . intval($this->getSFTPConnection()) . '/';
+    }
+
+    public function filePutContents($filename, $data)
+    {
+        $dir = dirname($filename);
+        $this->mkdir($dir);
+        file_put_contents($this->_filename($filename), $data);
+    }
+
+    public function fileGetContents($filename)
+    {
+        return file_get_contents($this->_filename($filename));
+    }
+
+    public function fileReplaceContents($string, $replacement, $filename)
+    {
+        $contents = $this->fileGetContents($filename);
+        file_put_contents('/tmp/get', $contents);
+        $contents = str_replace($string, $replacement, $contents);
+        file_put_contents('/tmp/replaced', $string . "\n\n" . $replacement . "\n\n" . $contents);
+        $this->filePutContents($filename, $contents);
+    }
+
+    protected function _filename($filename)
+    {
+        if ($this->convertToCygwinPath) {
+            $filename = $this->_escapeCygwin($filename);
+        }
+        return $this->getSFTPWrapper() . ltrim($filename, '/');
+    }
+
+    public function path($path)
+    {
+        return $this->_filename($path);
+    }
+
+    public function filesize($filename)
+    {
+        return filesize($this->_filename($filename));
+    }
+
+    public function file_exists($filename)
+    {
+        return file_exists($this->_filename($filename));
+    }
+
+    public function mkdir($dirname, $mode = 0777, $recursive = true)
+    {
+        if (!$this->file_exists($dirname)) {
+            return ssh2_sftp_mkdir($this->getSFTPConnection(), $dirname, $mode, $recursive);
+        }
+        return true;
+    }
+
+
+    /**
+     * @param $local string
+     * @param $remote string
+     * @param $mode int
+     * @return bool
+     */
+    public function send($local, $remote, $mode = 0644)
+    {
+        if (!file_exists($local)) {
+            throw new \Exception('Local file ' . $local . ' not found');
+        }
+        $dir = dirname($remote);
+        $this->mkdir($dir);
+        $cmd = 'scp -P' . $this->getPort() . ' ' . $local . ' ' . $this->getUsername() . '@' . $this->getHost() . ':/' . ltrim($remote, '/');
+        return exec($cmd);
+    }
+
+
+    /**
+     * @param $remote string
+     * @param $local string
+     * @return bool
+     */
+    public function recv($remote, $local)
+    {
+        $cmd = 'scp -P' . $this->getPort() . ' ' . $this->getUsername() . '@' . $this->getHost() . ':/' . ltrim($remote, '/') . ' ' . $local;
+        return exec($cmd);
+    }
+
+    /**
+     * @param $local string
+     * @param $remote string
+     * @param $mode int
+     * @return bool
+     */
+    public function slowsend($local, $remote, $mode = 0644)
+    {
+        if (!file_exists($local)) {
+            throw new \Exception('Local file ' . $local . ' not found');
+        }
+        $dir = dirname($remote);
+        $this->mkdir($dir);
+        return $this->_copy($local, $this->_filename($remote));
+    }
+
+    protected function _copy($from, $to, $send = true)
+    {
+        if (class_exists('Cubist\Util\Files\Files')) {
+            $res = \Cubist\Util\Files\Files::copyFile($from, $to);
+        } else {
+            $res = copy($from, $to);
+        }
+        return $res;
+    }
+
+    /**
+     * @param $remote string
+     * @param $local string
+     * @return bool
+     */
+    public function slowrecv($remote, $local)
+    {
+        return $this->_copy($this->_filename($remote), $local, false);
+    }
+
+    public function unlink($file)
+    {
+        if ($this->file_exists($file)) {
+            return ssh2_sftp_unlink($this->getSFTPConnection(), $file);
+        }
+    }
+
+
+    public function exec($commande)
+    {
+        // http://www.php.net/manual/fr/function.ssh2-exec.php#99089
+        $stream = ssh2_exec($this->_connect(), $commande);
+        $errorStream = ssh2_fetch_stream($stream, SSH2_STREAM_STDERR);
+
+        // Enable blocking for both streams
+        stream_set_blocking($errorStream, true);
+        stream_set_blocking($stream, true);
+
+        $stream_size_limit = 100000000;
+        $res = ['output' => stream_get_contents($stream, $stream_size_limit), 'error' => stream_get_contents($errorStream, $stream_size_limit)];
+
+        // Close the streams
+        fclose($errorStream);
+        fclose($stream);
+        return $res;
+    }
+
+    protected function _escapeCygwin($url)
+    {
+        $url = preg_replace('/\/([a-z]):\//i', '/cygdrive/\1/', '/' . $url);
+        return $url;
+    }
 
 }