|
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 mixed |
|
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
|
|
|
* Closes this SFTP connection. |
|
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
|
|
|
if (false === ssh2_exec($this->getConnection(), "exit")) { |
|
54
|
|
|
throw $this->newFTPException("close failed"); |
|
55
|
|
|
} |
|
56
|
|
|
return $this; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Opens this SFTP connection. |
|
61
|
|
|
* |
|
62
|
|
|
* @return SFTPClient Returns this SFTP client. |
|
63
|
|
|
* @throws FTPException Throws a FTP exception if an I/O error occurs. |
|
64
|
|
|
*/ |
|
65
|
|
|
public function connect() { |
|
66
|
|
|
|
|
67
|
|
|
$host = $this->getAuthenticator()->getHostname(); |
|
68
|
|
|
$port = $this->getAuthenticator()->getPort(); |
|
69
|
|
|
|
|
70
|
|
|
$this->setConnection(ssh2_connect($host, $port)); |
|
71
|
|
|
if (false === $this->getConnection()) { |
|
72
|
|
|
throw $this->newFTPException("connection failed"); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return $this; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Deletes a file on the SFTP server. |
|
80
|
|
|
* |
|
81
|
|
|
* @param string $path The file to delete. |
|
82
|
|
|
* @return SFTPClient Returns this SFTP client. |
|
83
|
|
|
* @throws FTPException Throws a FTP exception if an I/O error occurs. |
|
84
|
|
|
*/ |
|
85
|
|
|
public function delete($path) { |
|
86
|
|
|
if (false === ssh2_sftp_unlink($this->getSFTP(), $path)) { |
|
87
|
|
|
throw $this->newFTPException(sprintf("delete %s failed", $path)); |
|
88
|
|
|
} |
|
89
|
|
|
return $this; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Get the SFTP resource. |
|
94
|
|
|
* |
|
95
|
|
|
* @return mixed Returns the SFTP resource. |
|
96
|
|
|
*/ |
|
97
|
|
|
private function getSFTP() { |
|
98
|
|
|
if (null === $this->sftp) { |
|
99
|
|
|
$this->sftp = ssh2_sftp($this->getConnection()); |
|
100
|
|
|
} |
|
101
|
|
|
return $this->sftp; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Logs in to this SFTP connection. |
|
106
|
|
|
* |
|
107
|
|
|
* @return SFTPClient Returns this SFTP client. |
|
108
|
|
|
* @throws FTPException Throws a FTP exception if an I/O error occurs. |
|
109
|
|
|
*/ |
|
110
|
|
|
public function login() { |
|
111
|
|
|
|
|
112
|
|
|
$username = $this->getAuthenticator()->getPasswordAuthentication()->getUsername(); |
|
113
|
|
|
$password = $this->getAuthenticator()->getPasswordAuthentication()->getPassword(); |
|
114
|
|
|
|
|
115
|
|
|
if (false === ssh2_auth_password($this->getConnection(), $username, $password)) { |
|
116
|
|
|
throw $this->newFTPException("login failed"); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
return $this; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Creates a directory. |
|
124
|
|
|
* |
|
125
|
|
|
* @param string $directory The directory. |
|
126
|
|
|
* @param int $mode The mode. |
|
127
|
|
|
* @param bool $recursive Recursively ?. |
|
128
|
|
|
* @return SFTPClient Returns this SFTP client. |
|
129
|
|
|
* @throws FTPException Throws a FTP exception if an I/O error occurs. |
|
130
|
|
|
*/ |
|
131
|
|
|
public function mkdir($directory, $mode = 0777, $recursive = false) { |
|
132
|
|
|
if (false === ssh2_sftp_mkdir($this->getSFTP(), $directory, $mode, $recursive)) { |
|
133
|
|
|
throw $this->newFTPException(sprintf("mkdir %s failed", $directory)); |
|
134
|
|
|
} |
|
135
|
|
|
return $this; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Uploads a file to the SFTP server. |
|
140
|
|
|
* |
|
141
|
|
|
* @param string $localFile The local file. |
|
142
|
|
|
* @param string $remoteFile The remote file. |
|
143
|
|
|
* @return SFTPClient Returns this SFTP client. |
|
144
|
|
|
*/ |
|
145
|
|
|
public function put($localFile, $remoteFile) { |
|
146
|
|
|
$stream = fopen("ssh2.sftp://" . intval($this->getSFTP()) . $remoteFile, "w"); |
|
147
|
|
|
fwrite($stream, file_get_contents($localFile)); |
|
148
|
|
|
fclose($stream); |
|
149
|
|
|
return $this; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Renames a file or a directory on the SFTP server. |
|
154
|
|
|
* |
|
155
|
|
|
* @param string $oldName The old file/directory name. |
|
156
|
|
|
* @param string $newName The new name. |
|
157
|
|
|
* @return SFTPClient Returns this SFTP client. |
|
158
|
|
|
* @throws FTPException Throws a FTP exception if an I/O error occurs. |
|
159
|
|
|
*/ |
|
160
|
|
|
public function rename($oldName, $newName) { |
|
161
|
|
|
if (false === ssh2_sftp_rename($this->getSFTP(), $oldName, $newName)) { |
|
162
|
|
|
throw $this->newFTPException(sprintf("rename %s into %s failed", $oldName, $newName)); |
|
163
|
|
|
} |
|
164
|
|
|
return $this; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Removes a directory. |
|
169
|
|
|
* |
|
170
|
|
|
* @param string $directory The directory. |
|
171
|
|
|
* @return SFTPClient Returns this SFTP client. |
|
172
|
|
|
* @throws FTPException Throws a FTP exception if an I/O error occurs. |
|
173
|
|
|
*/ |
|
174
|
|
|
public function rmdir($directory) { |
|
175
|
|
|
if (false === ssh2_sftp_rmdir($this->getSFTP(), $directory)) { |
|
176
|
|
|
throw $this->newFTPException(sprintf("rmdir %s failed", $directory)); |
|
177
|
|
|
} |
|
178
|
|
|
return $this; |
|
179
|
|
|
} |
|
180
|
|
|
} |
|
181
|
|
|
|