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
|
|
|
* FTP client. |
19
|
|
|
* |
20
|
|
|
* @author webeweb <https://github.com/webeweb/> |
21
|
|
|
* @package WBW\Library\Core\Network\FTP\Client |
22
|
|
|
*/ |
23
|
|
|
class FtpClient extends AbstractFtpClient { |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Constructor. |
27
|
|
|
* |
28
|
|
|
* @param Authenticator $authenticator The authenticator. |
29
|
|
|
*/ |
30
|
|
|
public function __construct(Authenticator $authenticator) { |
31
|
|
|
parent::__construct($authenticator); |
32
|
|
|
|
33
|
|
|
$this->getAuthenticator()->setScheme("ftp"); |
34
|
|
|
if (null === $this->getAuthenticator()->getPort()) { |
35
|
|
|
$this->getAuthenticator()->setPort(21); |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Changes to the parent directory. |
41
|
|
|
* |
42
|
|
|
* @return FtpClient Returns this FTP client. |
43
|
|
|
* @throws FtpException Throws a FTP exception if an error occurs. |
44
|
|
|
*/ |
45
|
|
|
public function cdup(): FtpClient { |
46
|
|
|
|
47
|
|
|
if (false === @ftp_cdup($this->getConnection())) { |
48
|
|
|
throw $this->newFtpException("ftp_cdup failed"); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Changes the current directory on a FTP server. |
56
|
|
|
* |
57
|
|
|
* @param string $directory The directory. |
58
|
|
|
* @return FtpClient Returns this FTP client. |
59
|
|
|
* @throws FtpException Throws a FTP exception if an error occurs. |
60
|
|
|
*/ |
61
|
|
|
public function chdir(string $directory): FtpClient { |
62
|
|
|
|
63
|
|
|
if (false === @ftp_chdir($this->getConnection(), $directory)) { |
64
|
|
|
throw $this->newFtpException("ftp_chdir failed: [{$directory}]"); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Set permissions on a file via FTP. |
72
|
|
|
* |
73
|
|
|
* @param int $mode The mode. |
74
|
|
|
* @param string $filename The filename. |
75
|
|
|
* @return FtpClient Returns this FTP client. |
76
|
|
|
* @throws FtpException Throws a FTP exception if an error occurs. |
77
|
|
|
*/ |
78
|
|
|
public function chmod(int $mode, string $filename): FtpClient { |
79
|
|
|
|
80
|
|
|
if (false === @ftp_chmod($this->getConnection(), $mode, $filename)) { |
81
|
|
|
throw $this->newFtpException("ftp_chmod failed: [{$mode}, {$filename}]"); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $this; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Closes an FTP connection. |
89
|
|
|
* |
90
|
|
|
* @return FtpClient Returns this FTP client. |
91
|
|
|
* @throws FtpException Throws a FTP exception if an error occurs. |
92
|
|
|
*/ |
93
|
|
|
public function close(): FtpClient { |
94
|
|
|
|
95
|
|
|
if (false === @ftp_close($this->getConnection())) { |
96
|
|
|
throw $this->newFtpException("ftp_close failed"); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Opens an FTP connection. |
104
|
|
|
* |
105
|
|
|
* @param int $timeout The timeout. |
106
|
|
|
* @return FtpClient Returns this FTP client. |
107
|
|
|
* @throws FtpException Throws a FTP exception if an error occurs. |
108
|
|
|
*/ |
109
|
|
|
public function connect(int $timeout = 90): FtpClient { |
110
|
|
|
|
111
|
|
|
$host = $this->getAuthenticator()->getHostname(); |
112
|
|
|
$port = $this->getAuthenticator()->getPort(); |
113
|
|
|
|
114
|
|
|
$this->setConnection(@ftp_connect($host, $port, $timeout)); |
115
|
|
|
if (false === $this->getConnection()) { |
116
|
|
|
throw $this->newFtpException("ftp_connect failed: [{$host}, {$port}, {$timeout}]"); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $this; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Deletes a file on the FTP server. |
124
|
|
|
* |
125
|
|
|
* @param string $path The path. |
126
|
|
|
* @return FtpClient Returns this FTP client. |
127
|
|
|
* @throws FtpException Throws a FTP exception if an error occurs. |
128
|
|
|
*/ |
129
|
|
|
public function delete(string $path): FtpClient { |
130
|
|
|
|
131
|
|
|
if (false === @ftp_delete($this->getConnection(), $path)) { |
132
|
|
|
throw $this->newFtpException("ftp_delete failed: [{$path}]"); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
return $this; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Downloads a file from the FTP server and saves to an open file. |
140
|
|
|
* |
141
|
|
|
* @param resource $localStream The local stream. |
142
|
|
|
* @param string $remoteFile The remote file. |
143
|
|
|
* @param int $mode The mode. |
144
|
|
|
* @param int $resumePos The resume position. |
145
|
|
|
* @return FtpClient Returns this FTP client. |
146
|
|
|
* @throws FtpException Throws a FTP exception if an error occurs. |
147
|
|
|
*/ |
148
|
|
|
public function fget($localStream, string $remoteFile, int $mode = FTP_BINARY, int $resumePos = 0): FtpClient { |
149
|
|
|
|
150
|
|
|
if (false === @ftp_fget($this->getConnection(), $localStream, $remoteFile, $mode, $resumePos)) { |
151
|
|
|
throw $this->newFtpException("ftp_fget failed: [{$localStream}, {$remoteFile}, {$mode}, {$resumePos}]"); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return $this; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Uploads from an open file to the FTP server. |
159
|
|
|
* |
160
|
|
|
* @param string $remoteFile The remote file. |
161
|
|
|
* @param resource $localStream The local stream. |
162
|
|
|
* @param int $mode The mode. |
163
|
|
|
* @param int $startPos The start position. |
164
|
|
|
* @return FtpClient Returns this FTP client. |
165
|
|
|
* @throws FtpException Throws a FTP exception if an error occurs. |
166
|
|
|
*/ |
167
|
|
|
public function fput(string $remoteFile, $localStream, int $mode = FTP_BINARY, int $startPos = 0): FtpClient { |
168
|
|
|
|
169
|
|
|
if (false === @ftp_fput($this->getConnection(), $remoteFile, $localStream, $mode, $startPos)) { |
170
|
|
|
throw $this->newFtpException("ftp_fput failed: [{$remoteFile}, {$localStream}, {$mode}, {$startPos}]"); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
return $this; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Downloads a file from the FTP server. |
178
|
|
|
* |
179
|
|
|
* @param string $localFile The local file. |
180
|
|
|
* @param string $remoteFile The remote file. |
181
|
|
|
* @param int $mode The mode. |
182
|
|
|
* @param int $resumePos The resume position. |
183
|
|
|
* @return FtpClient Returns this FTP client. |
184
|
|
|
* @throws FtpException Throws a FTP exception if an error occurs. |
185
|
|
|
*/ |
186
|
|
|
public function get(string $localFile, string $remoteFile, int $mode = FTP_BINARY, int $resumePos = 0): FtpClient { |
187
|
|
|
|
188
|
|
|
if (false === @ftp_get($this->getConnection(), $localFile, $remoteFile, $mode, $resumePos)) { |
189
|
|
|
throw $this->newFtpException("ftp_get failed: [{$localFile}, {$remoteFile}, {$mode}, {$remoteFile}]"); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
return $this; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Logs in to an FTP connection. |
197
|
|
|
* |
198
|
|
|
* @return FtpClient Returns this FTP client. |
199
|
|
|
* @throws FtpException Throws a FTP exception if an error occurs. |
200
|
|
|
*/ |
201
|
|
|
public function login(): FtpClient { |
202
|
|
|
|
203
|
|
|
$username = $this->getAuthenticator()->getPasswordAuthentication()->getUsername(); |
204
|
|
|
$password = $this->getAuthenticator()->getPasswordAuthentication()->getPassword(); |
205
|
|
|
|
206
|
|
|
if (false === @ftp_login($this->getConnection(), $username, $password)) { |
207
|
|
|
throw $this->newFtpException("ftp_login failed: [{$username}]"); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
return $this; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Returns the last modified time of the given file. |
215
|
|
|
* |
216
|
|
|
* @param string $remoteFile The remote file. |
217
|
|
|
* @return int Returns the last modified time of the given file. |
218
|
|
|
* @throws FtpException Throws a FTP exception if an error occurs. |
219
|
|
|
*/ |
220
|
|
|
public function mdtm(string $remoteFile): int { |
221
|
|
|
|
222
|
|
|
$result = @ftp_mdtm($this->getConnection(), $remoteFile); |
223
|
|
|
if (false === $result) { |
224
|
|
|
throw $this->newFtpException("ftp_mdtm failed: [{$remoteFile}]"); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
return $result; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Creates a directory. |
232
|
|
|
* |
233
|
|
|
* @param string $directory The directory. |
234
|
|
|
* @return FtpClient Returns this FTP client. |
235
|
|
|
* @throws FtpException Throws a FTP exception if an error occurs. |
236
|
|
|
*/ |
237
|
|
|
public function mkdir(string $directory): FtpClient { |
238
|
|
|
|
239
|
|
|
if (false === @ftp_mkdir($this->getConnection(), $directory)) { |
240
|
|
|
throw $this->newFtpException("mkdir failed: [{$directory}]"); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
return $this; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Retrieves a file from the FTP server and writes it to an open file (non-blocking). |
248
|
|
|
* |
249
|
|
|
* @param resource $localStream The local stream. |
250
|
|
|
* @param string $remoteFile The remote file. |
251
|
|
|
* @param int $mode The mode. |
252
|
|
|
* @param int $resumePos The resume position. |
253
|
|
|
* @return int |
254
|
|
|
*/ |
255
|
|
|
public function nbFget($localStream, string $remoteFile, int $mode = FTP_BINARY, int $resumePos = 0): int { |
256
|
|
|
return @ftp_nb_fget($this->getConnection(), $localStream, $remoteFile, $mode, $resumePos); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* Stores a file from an open file to the FTP server (non-blocking). |
261
|
|
|
* |
262
|
|
|
* @param string $remoteFile The remote file. |
263
|
|
|
* @param resource $localStream The local stream. |
264
|
|
|
* @param int $mode The mode. |
265
|
|
|
* @param int $startPos The start position. |
266
|
|
|
* @return int |
267
|
|
|
*/ |
268
|
|
|
public function nbFput(string $remoteFile, $localStream, int $mode = FTP_BINARY, $startPos = 0): int { |
269
|
|
|
return @ftp_nb_fput($this->getConnection(), $remoteFile, $localStream, $mode, $startPos); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* Retrieves a file from the FTP server and writes it to a local file (non-blocking). |
274
|
|
|
* |
275
|
|
|
* @param string $localFile The local file. |
276
|
|
|
* @param string $remoteFile The remote file. |
277
|
|
|
* @param int $mode The mode. |
278
|
|
|
* @param int $resumePos The resume position. |
279
|
|
|
* @return int |
280
|
|
|
*/ |
281
|
|
|
public function nbGet(string $localFile, string $remoteFile, int $mode = FTP_BINARY, int $resumePos = 0): int { |
282
|
|
|
return @ftp_nb_get($this->getConnection(), $localFile, $remoteFile, $mode, $resumePos); |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* Stores a file from an open file to the FTP server (non-blocking). |
287
|
|
|
* |
288
|
|
|
* @param string $remoteFile The remote file. |
289
|
|
|
* @param string $localFile The local file. |
290
|
|
|
* @param int $mode The mode. |
291
|
|
|
* @param int $startPos The start position. |
292
|
|
|
* @return int |
293
|
|
|
*/ |
294
|
|
|
public function nbPut(string $remoteFile, string $localFile, int $mode = FTP_BINARY, int $startPos = 0): int { |
295
|
|
|
return @ftp_nb_put($this->getConnection(), $remoteFile, $localFile, $mode, $startPos); |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* Returns a list of files in the given directory. |
300
|
|
|
* |
301
|
|
|
* @param string $directory The directory. |
302
|
|
|
* @return string[] Returns a list of files in the given directory. |
303
|
|
|
* @throws FtpException Throws a FTP exception if an error occurs. |
304
|
|
|
*/ |
305
|
|
|
public function nlist(string $directory): array { |
306
|
|
|
|
307
|
|
|
$result = @ftp_nlist($this->getConnection(), $directory); |
308
|
|
|
if (false === $result) { |
309
|
|
|
throw $this->newFtpException("ftp_nlist failed: [{$directory}]"); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
return $result; |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* Turns passive mode on or off. |
317
|
|
|
* |
318
|
|
|
* @param bool $pasv The passive mode. |
319
|
|
|
* @return FtpClient Returns this FTP client. |
320
|
|
|
* @throws FtpException Throws a FTP exception if an error occurs. |
321
|
|
|
*/ |
322
|
|
|
public function pasv(bool $pasv): FtpClient { |
323
|
|
|
|
324
|
|
|
if (false === @ftp_pasv($this->getConnection(), $pasv)) { |
325
|
|
|
throw $this->newFtpException("ftp_pasv failed: [{$pasv}]"); |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
return $this; |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
/** |
332
|
|
|
* Uploads a file to the FTP server. |
333
|
|
|
* |
334
|
|
|
* @param string $localFile The local file. |
335
|
|
|
* @param string $remoteFile The remote file. |
336
|
|
|
* @param int $mode The mode. |
337
|
|
|
* @param int $startPos The start position. |
338
|
|
|
* @return FtpClient Returns this FTP client. |
339
|
|
|
* @throws FtpException Throws a FTP exception if an error occurs. |
340
|
|
|
*/ |
341
|
|
|
public function put(string $localFile, string $remoteFile, int $mode = FTP_BINARY, int $startPos = 0): FtpClient { |
342
|
|
|
|
343
|
|
|
if (false === @ftp_put($this->getConnection(), $remoteFile, $localFile, $mode, $startPos)) { |
344
|
|
|
throw $this->newFtpException("ftp_put failed: [{$remoteFile}, {$localFile}, {$mode}, {$startPos}]"); |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
return $this; |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
/** |
351
|
|
|
* Returns the current directory name. |
352
|
|
|
* |
353
|
|
|
* @return FtpClient Returns this FTP client. |
354
|
|
|
* @throws FtpException Throws a FTP exception if an error occurs. |
355
|
|
|
*/ |
356
|
|
|
public function pwd(): FtpClient { |
357
|
|
|
|
358
|
|
|
if (false === @ftp_pwd($this->getConnection())) { |
359
|
|
|
throw $this->newFtpException("ftp_pwd failed"); |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
return $this; |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
/** |
366
|
|
|
* Returns a detailed list of files in the given directory. |
367
|
|
|
* |
368
|
|
|
* @param string $directory The directory. |
369
|
|
|
* @param bool $recursive Recursive ? |
370
|
|
|
* @return array Returns a detailed list of files in the given directory. |
371
|
|
|
* @throws FtpException Throws a FTP exception if an error occurs. |
372
|
|
|
*/ |
373
|
|
|
public function rawList(string $directory, bool $recursive = false): array { |
374
|
|
|
|
375
|
|
|
$result = @ftp_rawList($this->getConnection(), $directory, $recursive); |
376
|
|
|
if (false === $result) { |
377
|
|
|
throw $this->newFtpException("ftp_nlist failed: [{$directory}, {$recursive}]"); |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
return $result; |
381
|
|
|
} |
382
|
|
|
|
383
|
|
|
/** |
384
|
|
|
* Renames a file or a directory on the FTP server. |
385
|
|
|
* |
386
|
|
|
* @param string $oldName The old name. |
387
|
|
|
* @param string $newName The new name. |
388
|
|
|
* @return FtpClient Returns this FTP client. |
389
|
|
|
* @throws FtpException Throws a FTP exception if an error occurs. |
390
|
|
|
*/ |
391
|
|
|
public function rename(string $oldName, string $newName): FtpClient { |
392
|
|
|
|
393
|
|
|
if (false === @ftp_rename($this->getConnection(), $oldName, $newName)) { |
394
|
|
|
throw $this->newFtpException("ftp_rename failed: [{$oldName}, {$newName}]"); |
395
|
|
|
} |
396
|
|
|
|
397
|
|
|
return $this; |
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
/** |
401
|
|
|
* Removes a directory. |
402
|
|
|
* |
403
|
|
|
* @param string $directory The directory. |
404
|
|
|
* @return FtpClient Returns this FTP client. |
405
|
|
|
* @throws FtpException Throws a FTP exception if an error occurs. |
406
|
|
|
*/ |
407
|
|
|
public function rmdir(string $directory): FtpClient { |
408
|
|
|
|
409
|
|
|
if (false === @ftp_rmdir($this->getConnection(), $directory)) { |
410
|
|
|
throw $this->newFtpException("ftp_rmdir failed: [{$directory}]"); |
411
|
|
|
} |
412
|
|
|
|
413
|
|
|
return $this; |
414
|
|
|
} |
415
|
|
|
|
416
|
|
|
/** |
417
|
|
|
* Returns the size of the given file. |
418
|
|
|
* |
419
|
|
|
* @param string $remoteFile The remote file. |
420
|
|
|
* @return int Returns the size of the given file. |
421
|
|
|
* @throws FtpException Throws a FTP exception if an error occurs. |
422
|
|
|
*/ |
423
|
|
|
public function size(string $remoteFile): int { |
424
|
|
|
|
425
|
|
|
$result = @ftp_size($this->getConnection(), $remoteFile); |
426
|
|
|
if (-1 === $result) { |
427
|
|
|
throw $this->newFtpException("ftp_size failed: [{$remoteFile}]"); |
428
|
|
|
} |
429
|
|
|
|
430
|
|
|
return $result; |
431
|
|
|
} |
432
|
|
|
|
433
|
|
|
/** |
434
|
|
|
* Returns the system type identifier of the remote FTP server. |
435
|
|
|
* |
436
|
|
|
* @return string Returns the system type identifier of the remote FTP server. |
437
|
|
|
* @throws FtpException Throws a FTP exception if an error occurs. |
438
|
|
|
*/ |
439
|
|
|
public function systype(): string { |
440
|
|
|
|
441
|
|
|
$result = @ftp_systype($this->getConnection()); |
442
|
|
|
if (false === $result) { |
443
|
|
|
throw $this->newFtpException("ftp_systype failed"); |
444
|
|
|
} |
445
|
|
|
|
446
|
|
|
return $result; |
447
|
|
|
} |
448
|
|
|
} |
449
|
|
|
|