Skip to content

Commit 6127489

Browse files
committed
initial commit
1 parent 21497f6 commit 6127489

File tree

9 files changed

+361
-0
lines changed

9 files changed

+361
-0
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2018 Jovanni Lo
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
PHP SSH2
2+
============================
3+
4+
Wrapper class for PHP's [SSH2 extension](http://php.net/manual/en/book.ssh2.php). The base class was created by [Jamie Munro](https://www.sitepoint.com/author/jmunro/) taken from [this article](https://www.sitepoint.com/using-ssh-and-sftp-with-php/).
5+
6+
## Installation
7+
```term
8+
$ composer require lodev09/php-ssh2
9+
```
10+
11+
## Usage
12+
```php
13+
14+
// connect
15+
$auth = new \SSH2\Password(SFTP_USER, SFTP_PASSWORD);
16+
$sftp = new \SSH2\SFTP(SFTP_HOST, $auth);
17+
18+
if ($sftp->is_connected() && $sftp->is_authenticated()) {
19+
// upload
20+
$sftp->put('/path/to/my/local/file', '/remote/file');
21+
22+
// download
23+
$sftp->get('/remote/file', '/local/destination/file');
24+
}
25+
```
26+
27+
### SFTP
28+
Common helper methods includes:
29+
- `SFTP::mv` - move remote file
30+
- `SFTP::rm` - delete remote file
31+
- `SFTP::list` - list remote files
32+
- `SFTP::is_dir` - check if path is a directory
33+
- `SFTP::exists` - check if path exists
34+
35+
Other native methods can be called as well for example:
36+
```php
37+
// ssh2_sftp_mkdir
38+
$sftp->mkdir(...);
39+
```
40+
41+
### SCP
42+
Just a pure wrapper of the native `ssh2_scp_xxx` functions.
43+
```php
44+
// ssh2_scp_recv
45+
$scp->recv(...);
46+
```
47+
48+
## Feedback
49+
All bugs, feature requests, pull requests, feedback, etc., are welcome. Visit my site at [www.lodev09.com](http://www.lodev09.com "www.lodev09.com") or email me at [lodev09@gmail.com](mailto:lodev09@gmail.com)
50+
51+
## Credits
52+
© 2018 - Coded by Jovanni Lo / [@lodev09](http://twitter.com/lodev09)
53+
54+
## License
55+
Released under the [MIT License](http://opensource.org/licenses/MIT).
56+
See [LICENSE](LICENSE) file.

composer.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "lodev09/php-ssh2",
3+
"description": "PHP SSH2 extension wrapper.",
4+
"homepage": "https://github.com/lodev09/php-ssh2",
5+
"license": "MIT",
6+
"keywords": [
7+
"php", "class", "ssh2", "sftp", "scp", "methods"
8+
],
9+
"authors": [
10+
{
11+
"name": "Jovanni Lo",
12+
"email": "lodev09@gmail.com"
13+
}
14+
],
15+
"require": {
16+
"php": ">=5.4"
17+
},
18+
"autoload": {
19+
"psr-4": {
20+
"SSH2\\": "src/"
21+
}
22+
}
23+
}

src/Authentication.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace SSH2;
4+
5+
class Authentication {
6+
7+
}

src/Key.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace SSH2;
4+
5+
class Key extends Authentication {
6+
protected $username;
7+
protected $publicKey;
8+
protected $privateKey;
9+
10+
public function __construct($username, $publicKey, $privateKey) {
11+
$this->username = $username;
12+
$this->password = $password;
13+
}
14+
15+
public function getUsername() {
16+
return $this->username;
17+
}
18+
19+
public function getPublicKey() {
20+
return $this->publicKey;
21+
}
22+
23+
public function getPrivateKey() {
24+
return $this->privateKey;
25+
}
26+
}

src/Password.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace SSH2;
4+
5+
class Password extends Authentication {
6+
protected $username;
7+
protected $password;
8+
9+
public function __construct($username, $password) {
10+
$this->username = $username;
11+
$this->password = $password;
12+
}
13+
14+
public function getUsername() {
15+
return $this->username;
16+
}
17+
18+
public function getPassword() {
19+
return $this->password;
20+
}
21+
}

src/SCP.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace SSH2;
4+
5+
class SCP extends SSH2 {
6+
public function __call($func, $args) {
7+
if (!$this->is_connected()) return false;
8+
$func = 'ssh2_scp_' . $func;
9+
if (function_exists($func)) {
10+
array_unshift($args, $this->conn);
11+
return call_user_func_array($func, $args);
12+
} else {
13+
trigger_error($func . ' is not a valid SCP function');
14+
}
15+
}
16+
}

src/SFTP.php

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
3+
namespace SSH2;
4+
5+
class SFTP extends SSH2 {
6+
protected $sftp;
7+
8+
public function __construct($host, Authentication $auth, $port = 22) {
9+
parent::__construct($host, $auth, $port);
10+
if ($this->is_connected()) {
11+
$this->sftp = ssh2_sftp($this->conn);
12+
} else {
13+
trigger_error('SFTP Not Connected to host');
14+
}
15+
16+
}
17+
public function __call($func, $args) {
18+
if (!$this->is_connected()) return false;
19+
$func = 'ssh2_sftp_' . $func;
20+
if (function_exists($func)) {
21+
array_unshift($args, $this->sftp);
22+
return call_user_func_array($func, $args);
23+
} else {
24+
trigger_error($func . ' is not a valid SFTP function');
25+
}
26+
}
27+
28+
public function list($addr, $limit = 0) {
29+
if (!$this->is_connected()) return false;
30+
$result = array();
31+
$files = scandir('ssh2.sftp://'.$this->sftp.'/'.$addr);
32+
if (!empty($files)) {
33+
$i = 0;
34+
foreach ($files as $file) {
35+
if ($file != '.' && $file != '..') {
36+
if ($limit && $i >= $limit) break;
37+
$result[] = $addr.'/'.$file;
38+
$i++;
39+
}
40+
}
41+
}
42+
return $result;
43+
}
44+
45+
public function mv($file, $dest_file, $rename_exists = true) {
46+
if (!$this->is_connected()) return false;
47+
48+
if ($rename_exists) {
49+
$path_info = pathinfo($dest_file);
50+
51+
$original_name = $path_info['filename'];
52+
$target_name = $original_name;
53+
$extension = $path_info['extension'];
54+
$dirname = $path_info['dirname'];
55+
56+
$i = 1;
57+
while($this->exists($dest_file)) {
58+
$target_name = $original_name.'('.$i.')';
59+
$dest_file = $dirname.'/'.$target_name.'.'.$extension;
60+
$i++;
61+
}
62+
}
63+
64+
return $this->rename($file, $dest_file);
65+
}
66+
67+
public function rm($remote_file) {
68+
return unlink('ssh2.sftp://'. $this->sftp.'/'.$remote_file);
69+
}
70+
71+
public function get($remote_file, $local_file) {
72+
if (!$this->is_connected()) return false;
73+
//ssh2_scp_recv($this->conn, $file, $dest);
74+
$data = file_get_contents('ssh2.sftp://'. $this->sftp.'/'.$remote_file);
75+
return file_put_contents($local_file, $data);
76+
}
77+
78+
public function put($local_file, $remote_file, $rename_exists = true) {
79+
if (!$this->is_connected()) return false;
80+
81+
if ($rename_exists) {
82+
$path_info = pathinfo($remote_file);
83+
84+
$original_name = $path_info['filename'];
85+
$target_name = $original_name;
86+
$extension = $path_info['extension'];
87+
$dirname = $path_info['dirname'];
88+
89+
$i = 1;
90+
while($this->exists($remote_file)) {
91+
$target_name = $original_name.'('.$i.')';
92+
$remote_file = $dirname.'/'.$target_name.'.'.$extension;
93+
$i++;
94+
}
95+
}
96+
97+
if ($stream = fopen('ssh2.sftp://'.$this->sftp.'/'.$remote_file, 'w')) {
98+
$data = file_get_contents($local_file);
99+
100+
if (fwrite($stream, $data)) {
101+
fclose($stream);
102+
return true;
103+
104+
} else return false;
105+
} return false;
106+
}
107+
108+
public function is_dir($remote_file) {
109+
return is_dir('ssh2.sftp://'.$this->sftp.'/'.$remote_file);
110+
}
111+
112+
public function exists($remote_file) {
113+
return file_exists('ssh2.sftp://'.$this->sftp.'/'.$remote_file);
114+
}
115+
}

src/SSH2.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
/**
4+
* @package SSH2 Class in PHP
5+
* @author Jovanni Lo, Jamie Munro
6+
* @link https://github.com/lodev09/php-ssh2, http://www.sitepoint.com/using-ssh-and-sftp-with-php/
7+
* @copyright 2018
8+
* @license
9+
* The MIT License (MIT)
10+
* Copyright (c) 2017 Jovanni Lo
11+
*
12+
* Permission is hereby granted, free of charge, to any person obtaining a copy
13+
* of this software and associated documentation files (the "Software"), to deal
14+
* in the Software without restriction, including without limitation the rights
15+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16+
* copies of the Software, and to permit persons to whom the Software is
17+
* furnished to do so, subject to the following conditions:
18+
*
19+
* The above copyright notice and this permission notice shall be included in all
20+
* copies or substantial portions of the Software.
21+
*
22+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28+
* SOFTWARE.
29+
*/
30+
31+
namespace SSH2;
32+
33+
class SSH2 {
34+
protected $conn;
35+
protected $authentication = false;
36+
37+
public function __construct($host, SSH2Authentication $auth, $port = 22) {
38+
$this->conn = ssh2_connect($host, $port);
39+
if ($this->is_connected()) {
40+
switch (get_class($auth)) {
41+
case 'SSH2Password':
42+
$username = $auth->getUsername();
43+
$password = $auth->getPassword();
44+
$this->authentication = ssh2_auth_password($this->conn, $username, $password);
45+
46+
if ($this->authentication === false) {
47+
trigger_error('SSH2 login is invalid');
48+
}
49+
break;
50+
51+
case 'SSH2Key':
52+
$username = $auth->getUsername();
53+
$publicKey = $auth->getPublicKey();
54+
$privateKey = $auth->getPrivateKey();
55+
56+
$this->authentication = ssh2_auth_pubkey_file($this->conn, $username, $publicKey, $privateKey);
57+
if ($this->authentication === false) {
58+
trigger_error('SSH2 login is invalid');
59+
}
60+
break;
61+
62+
default:
63+
trigger_error('Unknown SSH2 login type');
64+
}
65+
}
66+
}
67+
68+
public function is_connected() {
69+
return $this->conn ? true : false;
70+
}
71+
72+
public function is_authenticated() {
73+
return $this->authentication ? true : false;
74+
}
75+
}
76+
?>

0 commit comments

Comments
 (0)