From 9e3131815f111058bb1e82e7244ccc04f89c2447 Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Wed, 20 Feb 2019 19:44:34 +0100 Subject: [PATCH] wip #2602 @0.5 --- composer.json | 22 +++++- src/Cubist/Net/SSH2.php | 160 ++++++++++++++++++++++++++++++++++++++++ src/net/SayHello.php | 9 --- 3 files changed, 181 insertions(+), 10 deletions(-) create mode 100644 src/Cubist/Net/SSH2.php delete mode 100644 src/net/SayHello.php diff --git a/composer.json b/composer.json index bee327a..71ec7ec 100644 --- a/composer.json +++ b/composer.json @@ -1 +1,21 @@ -{"name":"cubist\/net","description":"net cubist composer package","type":"library","license":"proprietary","minimum-stability":"dev","autoload":{"psr-0":{"net":"src\/"}},"authors":[{"name":"Vincent Vanwaelscappel","email":"vincent@cubedesigners.com"}],"require":{"php":">=5.3.0"}} \ No newline at end of file +{ + "name": "cubist\/net", + "description": "net cubist composer package", + "type": "library", + "license": "proprietary", + "minimum-stability": "dev", + "autoload": { + "psr-0": { + "Cubist\\Net": "src\/" + } + }, + "authors": [ + { + "name": "Vincent Vanwaelscappel", + "email": "vincent@cubedesigners.com" + } + ], + "require": { + "php": ">=5.4.0" + } +} diff --git a/src/Cubist/Net/SSH2.php b/src/Cubist/Net/SSH2.php new file mode 100644 index 0000000..5b5e14b --- /dev/null +++ b/src/Cubist/Net/SSH2.php @@ -0,0 +1,160 @@ +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://' . $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) + { + return $this->getSFTPWrapper() . ltrim($this->_escapeCygwin($filename), '/'); + } + + 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; + } + + 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); + + // 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)); + + // Close the streams + fclose($errorStream); + fclose($stream); + return $res; + } + + protected function _escapeCygwin($url) + { + $url = preg_replace('/\/([a-z]):\//i', '/cygdrive/\1/', '/' . $url); + return $url; + } + +} diff --git a/src/net/SayHello.php b/src/net/SayHello.php deleted file mode 100644 index 220dc8f..0000000 --- a/src/net/SayHello.php +++ /dev/null @@ -1,9 +0,0 @@ -