Completed
Push — master ( a5146e...1ea8be )
by WEBEWEB
01:22
created

FtpClient::nbPut()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 4
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() {
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($directory) {
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($mode, $filename) {
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() {
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($timeout = 90) {
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($path) {
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, $remoteFile, $mode = FTP_BINARY, $resumePos = 0) {
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($remoteFile, $localStream, $mode = FTP_BINARY, $startPos = 0) {
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($localFile, $remoteFile, $mode = FTP_BINARY, $resumePos = 0) {
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() {
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($remoteFile) {
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($directory) {
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
     * Returns a list of files in the given directory.
248
     *
249
     * @param string $directory The directory.
250
     * @return array Returns a list of files in the given directory.
251
     * @throws FtpException Throws a FTP exception if an error occurs.
252
     */
253
    public function mlsd($directory) {
254
255
        $result = @ftp_mlsd($this->getConnection(), $directory);
256
        if (false === $result) {
257
            throw $this->newFtpException("ftp_mlsd failed: [${directory}]");
258
        }
259
260
        return $result;
261
    }
262
263
    /**
264
     * Retrieves a file from the FTP server and writes it to an open file (non-blocking).
265
     *
266
     * @param resource $localStream The local stream.
267
     * @param string $remoteFile The remote file.
268
     * @param int $mode The mode.
269
     * @param int $resumePos The resume position.
270
     * @return int
271
     */
272
    public function nbFget($localStream, $remoteFile, $mode = FTP_BINARY, $resumePos = 0) {
273
        return @ftp_nb_fget($this->getConnection(), $localStream, $remoteFile, $mode, $resumePos);
274
    }
275
276
    /**
277
     * Stores a file from an open file to the FTP server (non-blocking).
278
     *
279
     * @param string $remoteFile The remote file.
280
     * @param resource $localStream The local stream.
281
     * @param int $mode The mode.
282
     * @param int $startPos The start position.
283
     * @return int
284
     */
285
    public function nbFput($remoteFile, $localStream, $mode = FTP_BINARY, $startPos = 0) {
286
        return @ftp_nb_fput($this->getConnection(), $remoteFile, $localStream, $mode, $startPos);
287
    }
288
289
    /**
290
     * Retrieves a file from the FTP server and writes it to a local file (non-blocking).
291
     *
292
     * @param string $localFile The local file.
293
     * @param string $remoteFile The remote file.
294
     * @param int $mode The mode.
295
     * @param int $resumePos The resume position.
296
     * @return int
297
     */
298
    public function nbGet($localFile, $remoteFile, $mode = FTP_BINARY, $resumePos = 0) {
299
        return @ftp_nb_get($this->getConnection(), $localFile, $remoteFile, $mode, $resumePos);
300
    }
301
302
    /**
303
     * Stores a file from an open file to the FTP server (non-blocking).
304
     *
305
     * @param string $remoteFile The remote file.
306
     * @param string $localFile The local file.
307
     * @param int $mode The mode.
308
     * @param int $startPos The start position.
309
     * @return int
310
     */
311
    public function nbPut($remoteFile, $localFile, $mode = FTP_BINARY, $startPos = 0) {
312
        return @ftp_nb_put($this->getConnection(), $remoteFile, $localFile, $mode, $startPos);
313
    }
314
315
    /**
316
     * Returns a list of files in the given directory.
317
     *
318
     * @param string $directory The directory.
319
     * @return string[] Returns a list of files in the given directory.
320
     * @throws FtpException Throws a FTP exception if an error occurs.
321
     */
322
    public function nlist($directory) {
323
324
        $result = @ftp_nlist($this->getConnection(), $directory);
325
        if (false === $result) {
326
            throw $this->newFtpException("ftp_nlist failed: [${directory}]");
327
        }
328
329
        return $result;
330
    }
331
332
    /**
333
     * Turns passive mode on or off.
334
     *
335
     * @param bool $pasv The passive mode.
336
     * @return FtpClient Returns this FTP client.
337
     * @throws FtpException Throws a FTP exception if an error occurs.
338
     */
339
    public function pasv($pasv) {
340
341
        if (false === @ftp_pasv($this->getConnection(), $pasv)) {
342
            throw $this->newFtpException("ftp_pasv failed: [${pasv}]");
343
        }
344
345
        return $this;
346
    }
347
348
    /**
349
     * Uploads a file to the FTP server.
350
     *
351
     * @param string $localFile The local file.
352
     * @param string $remoteFile The remote file.
353
     * @param int $mode The mode.
354
     * @param int $startPos The start position.
355
     * @return FtpClient Returns this FTP client.
356
     * @throws FtpException Throws a FTP exception if an error occurs.
357
     */
358
    public function put($localFile, $remoteFile, $mode = FTP_IMAGE, $startPos = 0) {
359
360
        if (false === @ftp_put($this->getConnection(), $remoteFile, $localFile, $mode, $startPos)) {
361
            throw $this->newFtpException("ftp_put failed: [${remoteFile}, ${localFile}, ${mode}, ${startPos}]");
362
        }
363
364
        return $this;
365
    }
366
367
    /**
368
     * Returns the current directory name.
369
     *
370
     * @return FtpClient Returns this FTP client.
371
     * @throws FtpException Throws a FTP exception if an error occurs.
372
     */
373
    public function pwd() {
374
375
        if (false === @ftp_pwd($this->getConnection())) {
376
            throw $this->newFtpException("ftp_pwd failed");
377
        }
378
379
        return $this;
380
    }
381
382
    /**
383
     * Returns a detailed list of files in the given directory.
384
     *
385
     * @param string $directory The directory.
386
     * @param bool $recursive Recursive ?
387
     * @return array Returns a detailed list of files in the given directory.
388
     * @throws FtpException Throws a FTP exception if an error occurs.
389
     */
390
    public function rawList($directory, $recursive = false) {
391
392
        $result = @ftp_rawList($this->getConnection(), $directory, $recursive);
393
        if (false === $result) {
394
            throw $this->newFtpException("ftp_nlist failed: [${directory}, ${recursive}]");
395
        }
396
397
        return $result;
398
    }
399
400
    /**
401
     * Renames a file or a directory on the FTP server.
402
     *
403
     * @param string $oldName The old name.
404
     * @param string $newName The new name.
405
     * @return FtpClient Returns this FTP client.
406
     * @throws FtpException Throws a FTP exception if an error occurs.
407
     */
408
    public function rename($oldName, $newName) {
409
410
        if (false === @ftp_rename($this->getConnection(), $oldName, $newName)) {
411
            throw $this->newFtpException("ftp_rename failed: [${oldName}, ${newName}]");
412
        }
413
414
        return $this;
415
    }
416
417
    /**
418
     * Removes a directory.
419
     *
420
     * @param string $directory The directory.
421
     * @return FtpClient Returns this FTP client.
422
     * @throws FtpException Throws a FTP exception if an error occurs.
423
     */
424
    public function rmdir($directory) {
425
426
        if (false === @ftp_rmdir($this->getConnection(), $directory)) {
427
            throw $this->newFtpException("ftp_rmdir failed: [${directory}]");
428
        }
429
430
        return $this;
431
    }
432
433
    /**
434
     * Returns the size of the given file.
435
     *
436
     * @param string $remoteFile The remote file.
437
     * @return int Returns the size of the given file.
438
     * @throws FtpException Throws a FTP exception if an error occurs.
439
     */
440
    public function size($remoteFile) {
441
442
        $result = @ftp_size($this->getConnection(), $remoteFile);
443
        if (-1 === $result) {
444
            throw $this->newFtpException("ftp_size failed: [${remoteFile}]");
445
        }
446
447
        return $result;
448
    }
449
450
    /**
451
     * Returns the system type identifier of the remote FTP server.
452
     *
453
     * @return string Returns the system type identifier of the remote FTP server.
454
     * @throws FtpException Throws a FTP exception if an error occurs.
455
     */
456
    public function systype() {
457
458
        $result = @ftp_systype($this->getConnection());
459
        if (false === $result) {
460
            throw $this->newFtpException("ftp_systype failed");
461
        }
462
463
        return $result;
464
    }
465
}
466