1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SF2Helpers\SFTPBundle\SFTP; |
4
|
|
|
|
5
|
|
|
class SFTP implements ConnectionInterface, ResourceTransferInterface |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Connection instance |
9
|
|
|
* |
10
|
|
|
* @var $connection |
11
|
|
|
*/ |
12
|
|
|
private $connection; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Established sftp resource |
16
|
|
|
* |
17
|
|
|
* @var $sftp |
18
|
|
|
*/ |
19
|
|
|
private $sftp; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @inheritdoc |
23
|
|
|
*/ |
24
|
|
|
public function connect($host, $port) |
25
|
|
|
{ |
26
|
|
|
$this->connection = ssh2_connect($host, $port); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @inheritdoc |
31
|
|
|
*/ |
32
|
|
|
public function login($username, $password = null) |
33
|
|
|
{ |
34
|
|
|
if (!$this->connection) { |
35
|
|
|
throw new \LogicException('Establish connection with server before login'); |
36
|
|
|
} |
37
|
|
|
is_null($password) ? ssh2_auth_agent($this->connection, $username) : ssh2_auth_password($this->connection, $username, $password); |
38
|
|
|
$this->sftp = ssh2_sftp($this->connection); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @inheritdoc |
43
|
|
|
*/ |
44
|
|
|
public function loginWithKey($username, $pubkeyfile, $privkeyfile, $passphrase = null) |
45
|
|
|
{ |
46
|
|
|
if (!$this->connection) { |
47
|
|
|
throw new \LogicException('Establish connection with server before login'); |
48
|
|
|
} |
49
|
|
|
ssh2_auth_pubkey_file($this->connection, $username, $pubkeyfile, $privkeyfile, $passphrase); |
50
|
|
|
$this->sftp = ssh2_sftp($this->connection); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @inheritdoc |
55
|
|
|
*/ |
56
|
|
View Code Duplication |
public function fetchFrom($remoteFile, $localFile) |
|
|
|
|
57
|
|
|
{ |
58
|
|
|
$remoteData = file_get_contents('ssh2.sftp://' . intval($this->sftp) . $remoteFile); |
59
|
|
|
if (!$remoteData) { |
60
|
|
|
throw new \Exception('File can`t be loaded from the SFTP server.'); |
61
|
|
|
} |
62
|
|
|
file_put_contents($localFile, $remoteData); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @inheritdoc |
67
|
|
|
*/ |
68
|
|
View Code Duplication |
public function sendTo($localFile, $remoteFile) |
|
|
|
|
69
|
|
|
{ |
70
|
|
|
$localData = file_get_contents($localFile); |
71
|
|
|
if (!file_put_contents("ssh2.sftp://" . intval($this->sftp) . $remoteFile, $localData)) { |
72
|
|
|
throw new \Exception('File could not be uploaded to the SFTP server.'); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @inheritdoc |
78
|
|
|
*/ |
79
|
|
|
public function getRemoteFilesList($dir) |
80
|
|
|
{ |
81
|
|
|
$handle = opendir("ssh2.sftp://" . intval($this->sftp) . $dir); |
82
|
|
|
$files = array(); |
83
|
|
|
|
84
|
|
|
if (is_bool($handle)) { |
85
|
|
|
throw new \Exception('Could not read files list from remote SFTP server. Check your connection.'); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
while (false !== ($entry = readdir($handle))) { |
89
|
|
|
$files[] = $entry; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $files; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @inheritdoc |
97
|
|
|
*/ |
98
|
|
|
public function disconnect() |
99
|
|
|
{ |
100
|
|
|
unset($this->connection); |
101
|
|
|
unset($this->sftp); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.