From a687a04cb4a0387a192f9b0f95e7d89b576f84cc Mon Sep 17 00:00:00 2001 From: Flavien Audin Date: Tue, 14 Nov 2017 12:19:18 +0100 Subject: [PATCH 1/7] Update composer.json --- composer.json | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 9a65402..08ac97f 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { - "name": "herzult/php-ssh", + "name": "flavienaudin/php-ssh", "type": "library", - "description": "Provides an object-oriented wrapper for the php ssh2 extension.", + "description": "Provides an object-oriented wrapper for the php ssh2 extension. SCP included", "keywords": ["ssh", "ssh2"], "license": "MIT", "authors": [ @@ -12,6 +12,10 @@ { "name": "The contributors", "homepage": "http://github.com/Herzult/php-ssh/contributors" + }, + { + "name": "Flavien Audin", + "homepage": "https://github.com/flavienaudin/php-ssh" } ], "require": { From ac713b42595a9003e574775c5717a604e732e022 Mon Sep 17 00:00:00 2001 From: Flavien Audin Date: Tue, 14 Nov 2017 14:34:58 +0100 Subject: [PATCH 2/7] New SCP Subsystem --- .gitignore | 3 ++- src/Ssh/Scp.php | 49 +++++++++++++++++++++++++++++++++++++++++++++ src/Ssh/Session.php | 19 +++++++++++++++--- 3 files changed, 67 insertions(+), 4 deletions(-) create mode 100644 src/Ssh/Scp.php diff --git a/.gitignore b/.gitignore index f69293a..8c4a40f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ vendor *.lock -tests/bootstrap.php \ No newline at end of file +tests/bootstrap.php +.idea/ diff --git a/src/Ssh/Scp.php b/src/Ssh/Scp.php new file mode 100644 index 0000000..6a9941d --- /dev/null +++ b/src/Ssh/Scp.php @@ -0,0 +1,49 @@ + + * Request a file via SCP + * @link http://php.net/manual/en/function.ssh2-scp-recv.php + * + * @param string $remote_file

Path to the remote file.

+ * @param string $local_file

Path to the local file.

+ * @return bool true on success or false on failure. + */ + public function receiveFileScp ($remote_file, $local_file) { + return ssh2_scp_recv($this->session, $remote_file, $local_file); + } + + /** + * (PECL ssh2 >= 0.9.0)
+ * Send a file via SCP + * @link http://php.net/manual/en/function.ssh2-scp-send.php + * + * @param string $local_file

Path to the local file.

+ * @param string $remote_file

Path to the remote file.

+ * @param int $create_mode [optional]

The file will be created with the mode specified by create_mode.

+ * @return bool true on success or false on failure. + */ + public function sendFile($local_file, $remote_file, $create_mode = null) { + return ssh2_scp_send($this->session, $local_file, $remote_file, $create_mode); + } + + + public function createResource() + { + $resource = $this->getSessionResource(); + + if (!is_resource($resource)) { + throw new RuntimeException('The initialization of the SCP subsystem failed.'); + } + + $this->resource = $resource; + } + +} \ No newline at end of file diff --git a/src/Ssh/Session.php b/src/Ssh/Session.php index 4bf30a9..96f409e 100644 --- a/src/Ssh/Session.php +++ b/src/Ssh/Session.php @@ -47,17 +47,27 @@ public function setAuthentication(Authentication $authentication) /** * Returns the Sftp subsystem * - * @return Sftp + * @return Sftp|Subsystem */ public function getSftp() { return $this->getSubsystem('sftp'); } + /** + * Returns the Exec subsystem + * + * @return Scp|Subsystem + */ + public function getScp() + { + return $this->getSubsystem('scp'); + } + /** * Returns the Publickey subsystem * - * @return Publickey + * @return Publickey|Subsystem */ public function getPublickey() { @@ -67,7 +77,7 @@ public function getPublickey() /** * Returns the Exec subsystem * - * @return Exec + * @return Exec|Subsystem */ public function getExec() { @@ -106,6 +116,9 @@ protected function createSubsystem($name) case 'sftp': $subsystem = new Sftp($this); break; + case 'scp': + $subsystem = new Scp($this); + break; case 'publickey': $subsystem = new Publickey($this); break; From e62a7c8366bbe27d2987dbe55c71422868cf1bcb Mon Sep 17 00:00:00 2001 From: Flavien Audin Date: Tue, 14 Nov 2017 16:07:21 +0100 Subject: [PATCH 3/7] Fix used Resource --- src/Ssh/Scp.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Ssh/Scp.php b/src/Ssh/Scp.php index 6a9941d..f549ca3 100644 --- a/src/Ssh/Scp.php +++ b/src/Ssh/Scp.php @@ -16,8 +16,8 @@ class Scp extends Subsystem * @param string $local_file

Path to the local file.

* @return bool true on success or false on failure. */ - public function receiveFileScp ($remote_file, $local_file) { - return ssh2_scp_recv($this->session, $remote_file, $local_file); + public function receive($remote_file, $local_file) { + return ssh2_scp_recv($this->getResource(), $remote_file, $local_file); } /** @@ -30,8 +30,8 @@ public function receiveFileScp ($remote_file, $local_file) { * @param int $create_mode [optional]

The file will be created with the mode specified by create_mode.

* @return bool true on success or false on failure. */ - public function sendFile($local_file, $remote_file, $create_mode = null) { - return ssh2_scp_send($this->session, $local_file, $remote_file, $create_mode); + public function send($local_file, $remote_file, $create_mode = null) { + return ssh2_scp_send($this->getResource(), $local_file, $remote_file, $create_mode); } From 384f13ae63f1a799cc6596a073061ba90a5d31bc Mon Sep 17 00:00:00 2001 From: Flavien Audin Date: Tue, 14 Nov 2017 16:30:18 +0100 Subject: [PATCH 4/7] Fix create mode default value in send method --- src/Ssh/Scp.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Ssh/Scp.php b/src/Ssh/Scp.php index f549ca3..4f50b49 100644 --- a/src/Ssh/Scp.php +++ b/src/Ssh/Scp.php @@ -16,7 +16,8 @@ class Scp extends Subsystem * @param string $local_file

Path to the local file.

* @return bool true on success or false on failure. */ - public function receive($remote_file, $local_file) { + public function receive($remote_file, $local_file) + { return ssh2_scp_recv($this->getResource(), $remote_file, $local_file); } @@ -30,11 +31,11 @@ public function receive($remote_file, $local_file) { * @param int $create_mode [optional]

The file will be created with the mode specified by create_mode.

* @return bool true on success or false on failure. */ - public function send($local_file, $remote_file, $create_mode = null) { + public function send($local_file, $remote_file, $create_mode = 0644) + { return ssh2_scp_send($this->getResource(), $local_file, $remote_file, $create_mode); } - public function createResource() { $resource = $this->getSessionResource(); From ce4b67f47e879781ff666f4171e6a019fd7d6a53 Mon Sep 17 00:00:00 2001 From: Flavien Audin Date: Tue, 14 Nov 2017 16:38:53 +0100 Subject: [PATCH 5/7] Cleaning for PR --- .gitignore | 1 - composer.json | 8 ++------ src/Ssh/Session.php | 8 ++++---- 3 files changed, 6 insertions(+), 11 deletions(-) diff --git a/.gitignore b/.gitignore index 8c4a40f..b8592b4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,3 @@ vendor *.lock tests/bootstrap.php -.idea/ diff --git a/composer.json b/composer.json index 08ac97f..9a65402 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { - "name": "flavienaudin/php-ssh", + "name": "herzult/php-ssh", "type": "library", - "description": "Provides an object-oriented wrapper for the php ssh2 extension. SCP included", + "description": "Provides an object-oriented wrapper for the php ssh2 extension.", "keywords": ["ssh", "ssh2"], "license": "MIT", "authors": [ @@ -12,10 +12,6 @@ { "name": "The contributors", "homepage": "http://github.com/Herzult/php-ssh/contributors" - }, - { - "name": "Flavien Audin", - "homepage": "https://github.com/flavienaudin/php-ssh" } ], "require": { diff --git a/src/Ssh/Session.php b/src/Ssh/Session.php index 96f409e..ebbfcdd 100644 --- a/src/Ssh/Session.php +++ b/src/Ssh/Session.php @@ -47,7 +47,7 @@ public function setAuthentication(Authentication $authentication) /** * Returns the Sftp subsystem * - * @return Sftp|Subsystem + * @return Sftp */ public function getSftp() { @@ -57,7 +57,7 @@ public function getSftp() /** * Returns the Exec subsystem * - * @return Scp|Subsystem + * @return Scp */ public function getScp() { @@ -67,7 +67,7 @@ public function getScp() /** * Returns the Publickey subsystem * - * @return Publickey|Subsystem + * @return Publickey */ public function getPublickey() { @@ -77,7 +77,7 @@ public function getPublickey() /** * Returns the Exec subsystem * - * @return Exec|Subsystem + * @return Exec */ public function getExec() { From 122421009cb049c578a3d2664039f05d2c0c101a Mon Sep 17 00:00:00 2001 From: Flavien Audin Date: Wed, 29 Nov 2017 13:21:17 +0100 Subject: [PATCH 6/7] To publish on packagist --- README.markdown | 19 +++++++++++++++++++ composer.json | 8 ++++++-- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/README.markdown b/README.markdown index c6e0824..894471f 100644 --- a/README.markdown +++ b/README.markdown @@ -113,6 +113,25 @@ $sftp = $session->getSftp(); See the `Ssh\Sftp` class for more details on the available methods. +#### Scp + +You can easily access the SCP subsystem of a session using the `getScp()` method: + +```php +getScp(); +$scp->send('local/path/to/file','remote/path/where/send/file'); +$scp->receive('remote/path/to/file','local/path/where/save/file'); + +``` + + + +See the `Ssh\Scp` class for more details on the available methods. + #### Publickey The session also provides the `getPublickey()` method to access the publickey subsystem: diff --git a/composer.json b/composer.json index 9a65402..0d6bf67 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { - "name": "herzult/php-ssh", + "name": "faudin/php-ssh", "type": "library", - "description": "Provides an object-oriented wrapper for the php ssh2 extension.", + "description": "Provides an object-oriented wrapper for the php ssh2 extension. (based on herzult package)", "keywords": ["ssh", "ssh2"], "license": "MIT", "authors": [ @@ -9,6 +9,10 @@ "name": "Antoine Hérault", "homepage": "https://github.com/Herzult" }, + { + "name": "Flavien Audin", + "homepage": "https://github.com/flavienaudin/php-ssh" + }, { "name": "The contributors", "homepage": "http://github.com/Herzult/php-ssh/contributors" From c21e98545b2301fec9b3a33391340416cd235fd0 Mon Sep 17 00:00:00 2001 From: Flavien Audin Date: Wed, 29 Nov 2017 13:35:29 +0100 Subject: [PATCH 7/7] README update --- README.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.markdown b/README.markdown index 894471f..f2a145d 100644 --- a/README.markdown +++ b/README.markdown @@ -15,7 +15,7 @@ Installation The best way to add the library to your project is using [composer](http://getcomposer.org). - $ composer require herzult/php-ssh:~1.0 + $ composer require faudin/php-ssh:~1.0 Usage -----