]> _ Git - cubist_net.git/commitdiff
wip #5661 @1
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 19 Jan 2023 19:54:06 +0000 (20:54 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 19 Jan 2023 19:54:06 +0000 (20:54 +0100)
src/SSH2.php

index 9d4ff2f8c134e5c8141ce4a3f77dbdc0d87f32b7..75d2bd500b1e58a5ec804aac751217d13903575b 100644 (file)
@@ -1,8 +1,7 @@
 <?php
 
 namespace Cubist\Net;
-class SSH2
-{
+class SSH2 {
 
        protected $_host;
        protected $_username;
@@ -12,60 +11,50 @@ class SSH2
        protected $_connection = null;
        protected $_sftp = null;
 
-       public function __construct($host, $username, $password, $port = 22)
-       {
+       public function __construct($host, $username, $password, $port = 22) {
                $this->setHost($host)
                        ->setUsername($username)
                        ->setPassword($password)
                        ->setPort($port);
        }
 
-       public function getHost()
-       {
+       public function getHost() {
                return $this->_host;
        }
 
-       public function getUsername()
-       {
+       public function getUsername() {
                return $this->_username;
        }
 
-       public function getPassword()
-       {
+       public function getPassword() {
                return $this->_password;
        }
 
-       public function getPort()
-       {
+       public function getPort() {
                return $this->_port;
        }
 
-       public function setPort($port)
-       {
+       public function setPort($port) {
                $this->_port = $port;
                return $this;
        }
 
-       public function setHost($host)
-       {
+       public function setHost($host) {
                $this->_host = $host;
                return $this;
        }
 
-       public function setUsername($username)
-       {
+       public function setUsername($username) {
                $this->_username = $username;
                return $this;
        }
 
-       public function setPassword($password)
-       {
+       public function setPassword($password) {
                $this->_password = $password;
                return $this;
        }
 
-       public function _connect()
-       {
+       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());
@@ -73,8 +62,7 @@ class SSH2
                return $this->_connection;
        }
 
-       public function getSFTPConnection()
-       {
+       public function getSFTPConnection() {
                if (is_null($this->_sftp)) {
                        $this->_connect();
                        $this->_sftp = ssh2_sftp($this->_connection);
@@ -82,25 +70,21 @@ class SSH2
                return $this->_sftp;
        }
 
-       public function getSFTPWrapper()
-       {
+       public function getSFTPWrapper() {
                return 'ssh2.sftp://' . $this->getSFTPConnection() . '/';
        }
 
-       public function filePutContents($filename, $data)
-       {
+       public function filePutContents($filename, $data) {
                $dir = dirname($filename);
                $this->mkdir($dir);
                file_put_contents($this->_filename($filename), $data);
        }
 
-       public function fileGetContents($filename)
-       {
+       public function fileGetContents($filename) {
                return file_get_contents($this->_filename($filename));
        }
 
-       public function fileReplaceContents($string, $replacement, $filename)
-       {
+       public function fileReplaceContents($string, $replacement, $filename) {
                $contents = $this->fileGetContents($filename);
                file_put_contents('/tmp/get', $contents);
                $contents = str_replace($string, $replacement, $contents);
@@ -108,31 +92,55 @@ class SSH2
                $this->filePutContents($filename, $contents);
        }
 
-       protected function _filename($filename)
-       {
+       protected function _filename($filename) {
                return $this->getSFTPWrapper() . ltrim($this->_escapeCygwin($filename), '/');
        }
 
-       public function filesize($filename)
-       {
+       public function filesize($filename) {
                return filesize($this->_filename($filename));
        }
 
-       public function file_exists($filename)
-       {
+       public function file_exists($filename) {
                return file_exists($this->_filename($filename));
        }
 
-       public function mkdir($dirname, $mode = 0777, $recursive = true)
-       {
+       public function mkdir($dirname, $mode = 0777, $recursive = true) {
                if (!$this->file_exists($dirname)) {
                        return ssh2_sftp_mkdir($this->getSFTPConnection(), $dirname, $mode, $recursive);
                }
                return true;
        }
 
-       public function exec($commande)
-       {
+       /**
+        * @param $local string
+        * @param $distant string
+        * @param $mode int
+        * @return bool
+        */
+       public function send($local, $distant, $mode = 0755) {
+               $dir = dirname($distant);
+               $this->mkdir($dir);
+               return ssh2_scp_send($this->getSFTPConnection(), $local, $distant, $mode);
+       }
+
+       /**
+        * @param $distant string
+        * @param $local string
+        * @return bool
+        */
+       public function recv($distant, $local) {
+               return ssh2_scp_recv($this->getSFTPConnection(), $distant, $local);
+       }
+
+
+       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);
@@ -143,7 +151,7 @@ class SSH2
 
                // Whichever of the two below commands is listed first will receive its appropriate output.  The second command receives nothing
                $res = array('output' => stream_get_contents($stream),
-                       'error' => stream_get_contents($errorStream));
+                            'error' => stream_get_contents($errorStream));
 
                // Close the streams
                fclose($errorStream);
@@ -151,8 +159,7 @@ class SSH2
                return $res;
        }
 
-       protected function _escapeCygwin($url)
-       {
+       protected function _escapeCygwin($url) {
                $url = preg_replace('/\/([a-z]):\//i', '/cygdrive/\1/', '/' . $url);
                return $url;
        }