1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the core-library package. |
5
|
|
|
* |
6
|
|
|
* (c) 2018 WEBEWEB |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace WBW\Library\Core\Network\FTP\Client; |
13
|
|
|
|
14
|
|
|
use WBW\Library\Core\Network\FTP\Exception\FtpException; |
15
|
|
|
use WBW\Library\Core\Security\Authenticator; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* SFTP client. |
19
|
|
|
* |
20
|
|
|
* @author webeweb <https://github.com/webeweb/> |
21
|
|
|
* @package WBW\Library\Core\Network\FTP\Client |
22
|
|
|
*/ |
23
|
|
|
class SftpClient extends AbstractFtpClient { |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* SFTP resource. |
27
|
|
|
* |
28
|
|
|
* @var resource |
29
|
|
|
*/ |
30
|
|
|
private $sftp; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Constructor. |
34
|
|
|
* |
35
|
|
|
* @param Authenticator $authenticator The authenticator. |
36
|
|
|
*/ |
37
|
|
|
public function __construct(Authenticator $authenticator) { |
38
|
|
|
parent::__construct($authenticator); |
39
|
|
|
|
40
|
|
|
$this->getAuthenticator()->setScheme("sftp"); |
41
|
|
|
if (null === $this->getAuthenticator()->getPort()) { |
42
|
|
|
$this->getAuthenticator()->setPort(22); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Close. |
48
|
|
|
* |
49
|
|
|
* @return SftpClient Returns this SFTP client. |
50
|
|
|
* @throws FtpException Throws a FTP exception if an I/O error occurs. |
51
|
|
|
*/ |
52
|
|
|
public function close() { |
53
|
|
|
|
54
|
|
|
if (false === ssh2_exec($this->getConnection(), "exit")) { |
55
|
|
|
throw $this->newFtpException("ssh2_exec failed: [exit]"); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return $this; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Connect. |
63
|
|
|
* |
64
|
|
|
* @return SftpClient Returns this SFTP client. |
65
|
|
|
* @throws FtpException Throws a FTP exception if an I/O error occurs. |
66
|
|
|
*/ |
67
|
|
|
public function connect() { |
68
|
|
|
|
69
|
|
|
$host = $this->getAuthenticator()->getHostname(); |
70
|
|
|
$port = $this->getAuthenticator()->getPort(); |
71
|
|
|
|
72
|
|
|
$this->setConnection(ssh2_connect($host, $port)); |
73
|
|
|
if (false === $this->getConnection()) { |
74
|
|
|
throw $this->newFtpException("ssh2_connect failed: [${host}, ${$port}]"); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Delete. |
82
|
|
|
* |
83
|
|
|
* @param string $path The path. |
84
|
|
|
* @return SftpClient Returns this SFTP client. |
85
|
|
|
* @throws FtpException Throws a FTP exception if an I/O error occurs. |
86
|
|
|
*/ |
87
|
|
|
public function delete($path) { |
88
|
|
|
|
89
|
|
|
if (false === ssh2_sftp_unlink($this->getSftp(), $path)) { |
90
|
|
|
throw $this->newFtpException("ssh2_sftp_unlink failed: [${path}]"); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Get a file. |
98
|
|
|
* |
99
|
|
|
* @param string $localFile The local file. |
100
|
|
|
* @param string $remoteFile The remote file. |
101
|
|
|
* @return SftpClient Returns this SFTP client. |
102
|
|
|
*/ |
103
|
|
|
public function get($localFile, $remoteFile) { |
104
|
|
|
|
105
|
|
|
$input = fopen("ssh2.sftp://" . intval($this->getSftp()) . $remoteFile, "r"); |
106
|
|
|
$output = fopen($localFile, "w"); |
107
|
|
|
|
108
|
|
|
stream_copy_to_stream($input, $output); |
109
|
|
|
|
110
|
|
|
fclose($input); |
111
|
|
|
fclose($output); |
112
|
|
|
|
113
|
|
|
return $this; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Get the SFTP resource. |
118
|
|
|
* |
119
|
|
|
* @return resource Returns the SFTP resource. |
120
|
|
|
*/ |
121
|
|
|
private function getSftp() { |
122
|
|
|
|
123
|
|
|
if (null === $this->sftp) { |
124
|
|
|
$this->sftp = ssh2_sftp($this->getConnection()); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $this->sftp; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Login. |
132
|
|
|
* |
133
|
|
|
* @return SftpClient Returns this SFTP client. |
134
|
|
|
* @throws FtpException Throws a FTP exception if an I/O error occurs. |
135
|
|
|
*/ |
136
|
|
|
public function login() { |
137
|
|
|
|
138
|
|
|
$username = $this->getAuthenticator()->getPasswordAuthentication()->getUsername(); |
139
|
|
|
$password = $this->getAuthenticator()->getPasswordAuthentication()->getPassword(); |
140
|
|
|
|
141
|
|
|
if (false === ssh2_auth_password($this->getConnection(), $username, $password)) { |
142
|
|
|
throw $this->newFtpException("ssh2_auth_password failed: [${username}]"); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
return $this; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Make a directory. |
150
|
|
|
* |
151
|
|
|
* @param string $directory The directory. |
152
|
|
|
* @param int $mode The mode. |
153
|
|
|
* @param bool $recursive Recursively ?. |
154
|
|
|
* @return SftpClient Returns this SFTP client. |
155
|
|
|
* @throws FtpException Throws a FTP exception if an I/O error occurs. |
156
|
|
|
*/ |
157
|
|
|
public function mkdir($directory, $mode = 0777, $recursive = false) { |
158
|
|
|
|
159
|
|
|
if (false === ssh2_sftp_mkdir($this->getSftp(), $directory, $mode, $recursive)) { |
160
|
|
|
throw $this->newFtpException("ssh2_sftp_mkdir failed: [${directory}]"); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
return $this; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Put a file. |
168
|
|
|
* |
169
|
|
|
* @param string $localFile The local file. |
170
|
|
|
* @param string $remoteFile The remote file. |
171
|
|
|
* @return SftpClient Returns this SFTP client. |
172
|
|
|
*/ |
173
|
|
|
public function put($localFile, $remoteFile) { |
174
|
|
|
|
175
|
|
|
$input = fopen($localFile, "r"); |
176
|
|
|
$output = fopen("ssh2.sftp://" . intval($this->getSftp()) . $remoteFile, "w"); |
177
|
|
|
|
178
|
|
|
stream_copy_to_stream($input, $output); |
179
|
|
|
|
180
|
|
|
fclose($input); |
181
|
|
|
fclose($output); |
182
|
|
|
|
183
|
|
|
return $this; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Rename. |
188
|
|
|
* |
189
|
|
|
* @param string $oldName The old name. |
190
|
|
|
* @param string $newName The new name. |
191
|
|
|
* @return SftpClient Returns this SFTP client. |
192
|
|
|
* @throws FtpException Throws a FTP exception if an I/O error occurs. |
193
|
|
|
*/ |
194
|
|
|
public function rename($oldName, $newName) { |
195
|
|
|
|
196
|
|
|
if (false === ssh2_sftp_rename($this->getSftp(), $oldName, $newName)) { |
197
|
|
|
throw $this->newFtpException("ssh2_sftp_rename failed: [${oldName}, ${newName}]"); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
return $this; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Remove a directory. |
205
|
|
|
* |
206
|
|
|
* @param string $directory The directory. |
207
|
|
|
* @return SftpClient Returns this SFTP client. |
208
|
|
|
* @throws FtpException Throws a FTP exception if an I/O error occurs. |
209
|
|
|
*/ |
210
|
|
|
public function rmdir($directory) { |
211
|
|
|
|
212
|
|
|
if (false === ssh2_sftp_rmdir($this->getSftp(), $directory)) { |
213
|
|
|
throw $this->newFtpException("ssh2_sftp_rmdir failed: [${directory}]"); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
return $this; |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
|