1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of SebastianFeldmann\Ftp. |
4
|
|
|
* |
5
|
|
|
* (c) Sebastian Feldmann <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
namespace SebastianFeldmann\Ftp; |
11
|
|
|
|
12
|
|
|
use RuntimeException; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class Client |
16
|
|
|
* |
17
|
|
|
* @package SebastianFeldmann\Ftp |
18
|
|
|
* @author Sebastian Feldmann <[email protected]> |
19
|
|
|
* @link https://github.com/sebastianfeldmann/ftp |
20
|
|
|
* @since Class available since Release 0.9.0 |
21
|
|
|
* |
22
|
|
|
* @method bool cdUp() - Changes to the parent directory |
23
|
|
|
* @method string chDir(string $directory) - Changes the current directory on a FTP server |
24
|
|
|
* @method string mdtm(string $file) - Returns last modification time from given file |
25
|
|
|
* @method bool mkDir(string $path) - Create a directory on the FTP server |
26
|
|
|
* @method array mlsd(string $path) - Return list of file info arrays (>= php 7.2.0) |
27
|
|
|
* @method array nlist(string $path) - Returns list of files in given dir |
28
|
|
|
* @method bool pasv(bool $passive) - Sets the ftp passive mode on or off |
29
|
|
|
* @method bool put(string $name, string $file, int $mode) - Uploads a file to the FTP server |
30
|
|
|
* @method string pwd() - Returns current working directory |
31
|
|
|
* @method int size(string $file) - Returns given files sizes in bytes |
32
|
|
|
*/ |
33
|
|
|
class Client |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* PHP FTP connection resource. |
37
|
|
|
* |
38
|
|
|
* @var resource |
39
|
|
|
*/ |
40
|
|
|
private $connection; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Host to connect to. |
44
|
|
|
* |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
private $host; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Port to connect to. |
51
|
|
|
* |
52
|
|
|
* @var int |
53
|
|
|
*/ |
54
|
|
|
private $port; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* User to login. |
58
|
|
|
* |
59
|
|
|
* @var string |
60
|
|
|
*/ |
61
|
|
|
private $user; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Password to login. |
65
|
|
|
* |
66
|
|
|
* @var string |
67
|
|
|
*/ |
68
|
|
|
private $password; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Use passive ftp mode |
72
|
|
|
* |
73
|
|
|
* @var bool |
74
|
|
|
*/ |
75
|
|
|
private $passive; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Use ftps connection |
79
|
|
|
* |
80
|
|
|
* @var bool |
81
|
|
|
*/ |
82
|
|
|
private $isSecure; |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Client constructor. |
86
|
|
|
* |
87
|
|
|
* @param string $url |
88
|
|
|
* @param bool $passive |
89
|
|
|
* @param bool $isSecure |
90
|
|
|
*/ |
91
|
1 |
|
public function __construct(string $url, bool $passive = false, bool $isSecure = false) |
|
|
|
|
92
|
|
|
{ |
93
|
1 |
|
if (!extension_loaded('ftp')) { |
94
|
|
|
throw new RuntimeException('FTP extension is not loaded.'); |
95
|
|
|
} |
96
|
1 |
|
$this->passive = $passive; |
97
|
1 |
|
$this->setup($url); |
98
|
1 |
|
$this->login(); |
99
|
1 |
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Determine if file is a directory. |
103
|
|
|
* |
104
|
|
|
* @param string $name |
105
|
|
|
* @return bool |
106
|
|
|
*/ |
107
|
|
|
public function isDir(string $name) |
108
|
|
|
{ |
109
|
|
|
$current = $this->pwd(); |
110
|
|
|
try { |
111
|
|
|
$this->chDir($name); |
112
|
|
|
$this->chDir($current); |
113
|
|
|
return true; |
114
|
|
|
} catch (\Exception $e) { |
115
|
|
|
// do nothing |
116
|
|
|
} |
117
|
|
|
return false; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Returns to the home directory. |
122
|
|
|
* |
123
|
|
|
* @return void |
124
|
|
|
*/ |
125
|
|
|
public function chHome() |
126
|
|
|
{ |
127
|
|
|
$this->chDir(''); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Return list of all files in directory. |
132
|
|
|
* |
133
|
|
|
* @param string $path |
134
|
|
|
* @return \SebastianFeldmann\Ftp\File[] |
135
|
|
|
* @throws \Exception |
136
|
|
|
*/ |
137
|
1 |
|
public function ls(string $path = '') : array |
138
|
|
|
{ |
139
|
1 |
|
return version_compare(PHP_VERSION, '7.2.0', '>=') |
140
|
1 |
|
? $this->ls72($path) |
141
|
1 |
|
: $this->lsLegacy($path); |
142
|
|
|
|
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Return list of all files in directory for php 7.2.0 and higher. |
147
|
|
|
* |
148
|
|
|
* @param string $path |
149
|
|
|
* @return \SebastianFeldmann\Ftp\File[] |
150
|
|
|
* @throws \Exception |
151
|
|
|
*/ |
152
|
1 |
|
private function ls72(string $path) : array |
153
|
|
|
{ |
154
|
1 |
|
$list = []; |
155
|
1 |
|
foreach ($this->mlsd($path) as $fileInfo) { |
156
|
1 |
|
$list[] = new File($fileInfo); |
157
|
|
|
} |
158
|
1 |
|
return $list; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Return list of all files in directory for php versione below 7.2.0. |
163
|
|
|
* |
164
|
|
|
* @param string $path |
165
|
|
|
* @return \SebastianFeldmann\Ftp\File[] |
166
|
|
|
* @throws \Exception |
167
|
|
|
*/ |
168
|
|
|
private function lsLegacy(string $path) : array |
169
|
|
|
{ |
170
|
|
|
$list = []; |
171
|
|
|
foreach ($this->nlist($path) as $name) { |
172
|
|
|
$type = $this->isDir($name) ? 'dir' : 'file'; |
173
|
|
|
$size = $this->size($name); |
174
|
|
|
$mtime = $this->mdtm($name); |
175
|
|
|
|
176
|
|
|
if ($mtime == -1) { |
177
|
|
|
throw new RuntimeException('FTP server doesnt support \'ftp_mdtm\''); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
$list[] = new File(['name' => $name, 'modify' => $mtime, 'type' => $type, 'size' => $size]); |
181
|
|
|
} |
182
|
|
|
return $list; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Return list of directories in given path. |
187
|
|
|
* |
188
|
|
|
* @param string $path |
189
|
|
|
* @return array |
190
|
|
|
* @throws \Exception |
191
|
|
|
*/ |
192
|
|
|
public function lsDirs(string $path = '') : array |
193
|
|
|
{ |
194
|
|
|
return array_filter( |
195
|
|
|
$this->ls($path), |
196
|
|
|
function(File $file) { |
197
|
|
|
return $file->isDir(); |
198
|
|
|
} |
199
|
|
|
); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Return list of files in given path. |
204
|
|
|
* |
205
|
|
|
* @param string $path |
206
|
|
|
* @return array |
207
|
|
|
* @throws \Exception |
208
|
|
|
*/ |
209
|
|
|
public function lsFiles(string $path = '') : array |
210
|
|
|
{ |
211
|
|
|
return array_filter( |
212
|
|
|
$this->ls($path), |
213
|
|
|
function(File $file) { |
214
|
|
|
return $file->isFile(); |
215
|
|
|
} |
216
|
|
|
); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Upload local file to ftp server. |
221
|
|
|
* |
222
|
|
|
* @param string $file Path to local file that should be uploaded. |
223
|
|
|
* @param string $path Path to store the file under. |
224
|
|
|
* @param string $name Filename on the ftp server. |
225
|
|
|
* @return void |
226
|
|
|
*/ |
227
|
|
|
public function uploadFile(string $file, string $path, string $name) |
228
|
|
|
{ |
229
|
|
|
// to store the file we have to make sure the directory exists |
230
|
|
|
foreach ($this->extractDirectories($path) as $dir) { |
231
|
|
|
// if change to directory fails |
232
|
|
|
// create the dir and change into it afterwards |
233
|
|
|
try { |
234
|
|
|
$this->chDir($dir); |
235
|
|
|
} catch (\Exception $e) { |
236
|
|
|
$this->mkDir($dir); |
237
|
|
|
$this->chDir($dir); |
238
|
|
|
} |
239
|
|
|
} |
240
|
|
|
if (!$this->put($name, $file, FTP_BINARY)) { |
241
|
|
|
$error = error_get_last(); |
242
|
|
|
$message = $error['message']; |
243
|
|
|
throw new RuntimeException(sprintf('error uploading file: %s - %s', $file, $message)); |
244
|
|
|
} |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Setup local member variables by parsing the ftp url. |
249
|
|
|
* |
250
|
|
|
* @param string $url |
251
|
|
|
*/ |
252
|
1 |
|
private function setup(string $url) |
253
|
|
|
{ |
254
|
1 |
|
$parts = \parse_url($url); |
255
|
1 |
|
$this->host = $parts['host'] ?? ''; |
256
|
1 |
|
$this->port = $parts['port'] ?? 21; |
257
|
1 |
|
$this->user = $parts['user'] ?? ''; |
258
|
1 |
|
$this->password = $parts['pass'] ?? ''; |
259
|
1 |
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Setup ftp connection |
263
|
|
|
* |
264
|
|
|
* @throws \RuntimeException |
265
|
|
|
*/ |
266
|
1 |
|
private function login() |
267
|
|
|
{ |
268
|
1 |
|
if (empty($this->host)) { |
269
|
|
|
throw new RuntimeException('no host to connect to'); |
270
|
|
|
} |
271
|
|
|
|
272
|
1 |
|
$old = error_reporting(0); |
273
|
|
|
|
274
|
1 |
|
$this->connection = ($this->isSecure ? ftp_ssl_connect($this->host, $this->port) : ftp_connect($this->host, $this->port)); |
|
|
|
|
275
|
|
|
|
276
|
1 |
|
if (!$this->connection) { |
|
|
|
|
277
|
|
|
error_reporting($old); |
278
|
|
|
throw new RuntimeException(sprintf('unable to connect to ftp server %s', $this->host)); |
279
|
|
|
} |
280
|
|
|
|
281
|
1 |
|
if (!ftp_login($this->connection, $this->user, $this->password)) { |
282
|
|
|
error_reporting($old); |
283
|
|
|
throw new RuntimeException( |
284
|
|
|
sprintf('authentication failed for %s@%s', $this->user, $this->host) |
285
|
|
|
); |
286
|
|
|
} |
287
|
|
|
// set passive mode if needed |
288
|
1 |
|
$this->pasv($this->passive); |
289
|
1 |
|
error_reporting($old); |
290
|
1 |
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* Return list of remote directories to travers. |
294
|
|
|
* |
295
|
|
|
* @param string $path |
296
|
|
|
* @return array |
297
|
|
|
*/ |
298
|
|
|
private function extractDirectories(string $path) : array |
299
|
|
|
{ |
300
|
|
|
$remoteDirs = []; |
301
|
|
|
if (!empty($path)) { |
302
|
|
|
$remoteDirs = explode('/', $path); |
303
|
|
|
// fix empty first array element for absolute path |
304
|
|
|
if (substr($path, 0, 1) === '/') { |
305
|
|
|
$remoteDirs[0] = '/'; |
306
|
|
|
} |
307
|
|
|
$remoteDirs = array_filter($remoteDirs); |
308
|
|
|
} |
309
|
|
|
return $remoteDirs; |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* Handle all ftp_* functions. |
314
|
|
|
* |
315
|
|
|
* @param string $name |
316
|
|
|
* @param array $args |
317
|
|
|
* @return mixed |
318
|
|
|
*/ |
319
|
1 |
|
public function __call($name, $args) |
320
|
|
|
{ |
321
|
1 |
|
$function = 'ftp_' . strtolower($name); |
322
|
1 |
|
if (!function_exists($function)) { |
323
|
|
|
throw new RuntimeException(sprintf('invalid method call: %s', $function)); |
324
|
|
|
} |
325
|
1 |
|
$old = error_reporting(0); |
326
|
1 |
|
array_unshift($args, $this->connection); |
327
|
1 |
|
if (!$result = call_user_func_array($function, $args)) { |
328
|
|
|
$error = error_get_last(); |
329
|
|
|
error_reporting($old); |
330
|
|
|
throw new RuntimeException($error['message']); |
331
|
|
|
} |
332
|
1 |
|
error_reporting($old); |
333
|
1 |
|
return $result; |
334
|
|
|
} |
335
|
|
|
} |
336
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.