1
|
|
|
<?php /** MicroFTP */ |
2
|
|
|
|
3
|
|
|
namespace Micro\Web; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Simple FTP Class |
7
|
|
|
* |
8
|
|
|
* @author Shay Anderson 05.11 |
9
|
|
|
* @link shayanderson.com |
10
|
|
|
* @license http://www.gnu.org/licenses/gpl.html GPL License |
11
|
|
|
* @package Micro |
12
|
|
|
* @subpackage Web |
13
|
|
|
* @version 1.0 |
14
|
|
|
* @since 1.0 |
15
|
|
|
* @final |
16
|
|
|
*/ |
17
|
|
|
final class Ftp |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Last error |
21
|
|
|
* |
22
|
|
|
* @var string $error |
23
|
|
|
*/ |
24
|
|
|
public $error; |
25
|
|
|
/** |
26
|
|
|
* FTP passive mode flag |
27
|
|
|
* |
28
|
|
|
* @var bool $passive |
29
|
|
|
*/ |
30
|
|
|
public $passive = false; |
31
|
|
|
/** |
32
|
|
|
* SSL-FTP connection flag |
33
|
|
|
* |
34
|
|
|
* @var bool $ssl |
35
|
|
|
*/ |
36
|
|
|
public $ssl = false; |
37
|
|
|
/** |
38
|
|
|
* System type of FTP server |
39
|
|
|
* |
40
|
|
|
* @var string $system_type |
41
|
|
|
*/ |
42
|
|
|
public $system_type; |
43
|
|
|
/** |
44
|
|
|
* FTP host |
45
|
|
|
* |
46
|
|
|
* @var string $_host |
47
|
|
|
*/ |
48
|
|
|
private $_host; |
49
|
|
|
/** |
50
|
|
|
* FTP port |
51
|
|
|
* |
52
|
|
|
* @var int $_port |
53
|
|
|
*/ |
54
|
|
|
private $_port = 21; |
55
|
|
|
/** |
56
|
|
|
* FTP password |
57
|
|
|
* |
58
|
|
|
* @var string $_pwd |
59
|
|
|
*/ |
60
|
|
|
private $_pwd; |
61
|
|
|
/** |
62
|
|
|
* FTP stream |
63
|
|
|
* |
64
|
|
|
* @var resource $_id |
65
|
|
|
*/ |
66
|
|
|
private $_stream; |
67
|
|
|
/** |
68
|
|
|
* FTP timeout |
69
|
|
|
* |
70
|
|
|
* @var int $_timeout |
71
|
|
|
*/ |
72
|
|
|
private $_timeout = 90; |
73
|
|
|
/** |
74
|
|
|
* FTP user |
75
|
|
|
* |
76
|
|
|
* @var string $_user |
77
|
|
|
*/ |
78
|
|
|
private $_user; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Initialize connection params |
82
|
|
|
* |
83
|
|
|
* @access public |
84
|
|
|
* |
85
|
|
|
* @param array $params |
86
|
|
|
* |
87
|
|
|
* @result void |
88
|
|
|
*/ |
89
|
|
|
public function __construct(array $params = []) |
90
|
|
|
{ |
91
|
|
|
$this->_host = $params['host'] ?: null; |
92
|
|
|
$this->_user = $params['user'] ?: null; |
93
|
|
|
$this->_pwd = $params['password'] ?: null; |
94
|
|
|
$this->_port = (int)$params['port'] ?: 21; |
95
|
|
|
$this->_timeout = (int)$params['timeout'] ?: 90; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Auto close connection |
100
|
|
|
*/ |
101
|
|
|
public function __destruct() |
102
|
|
|
{ |
103
|
|
|
$this->close(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Close FTP connection |
108
|
|
|
*/ |
109
|
|
|
public function close() |
110
|
|
|
{ |
111
|
|
|
// check for valid FTP stream |
112
|
|
|
if ($this->_stream) { |
113
|
|
|
// close FTP connection |
114
|
|
|
ftp_close($this->_stream); |
115
|
|
|
|
116
|
|
|
// reset stream |
117
|
|
|
$this->_stream = false; |
|
|
|
|
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Change current directory on FTP server |
123
|
|
|
* |
124
|
|
|
* @param string $directory |
125
|
|
|
* |
126
|
|
|
* @return bool |
127
|
|
|
*/ |
128
|
|
|
public function cd($directory = null) |
129
|
|
|
{ |
130
|
|
|
if (ftp_chdir($this->_stream, $directory)) { |
131
|
|
|
return true; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
$this->error = "Failed to change directory to \"{$directory}\""; |
135
|
|
|
|
136
|
|
|
return false; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Set file permissions |
141
|
|
|
* |
142
|
|
|
* @param int $permissions (ex: 0644) |
143
|
|
|
* @param string $remote_file |
144
|
|
|
* |
145
|
|
|
* @return false |
146
|
|
|
*/ |
147
|
|
|
public function chmod($permissions = 0, $remote_file = null) |
148
|
|
|
{ |
149
|
|
|
if (ftp_chmod($this->_stream, $permissions, $remote_file)) { |
150
|
|
|
return true; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
$this->error = 'Failed to set file permissions for "' . $remote_file . '"'; |
154
|
|
|
|
155
|
|
|
return false; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Connect to FTP server |
160
|
|
|
* |
161
|
|
|
* @return bool |
162
|
|
|
*/ |
163
|
|
|
public function connect() |
164
|
|
|
{ |
165
|
|
|
$func = $this->ssl ? 'ftp_ssl_connect' : 'ftp_connect'; |
166
|
|
|
$this->_stream = $func($this->_host, $this->_port, $this->_timeout); |
167
|
|
|
|
168
|
|
|
if (!$this->_stream) { |
169
|
|
|
$this->error = 'Failed to connect ' . $this->_host . '.'; |
170
|
|
|
return false; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
if (ftp_login($this->_stream, $this->_user, $this->_pwd)) { |
174
|
|
|
ftp_pasv($this->_stream, (bool)$this->passive); |
175
|
|
|
|
176
|
|
|
$this->system_type = ftp_systype($this->_stream); |
177
|
|
|
|
178
|
|
|
return true; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
$this->error = 'Failed to connect to ' . $this->_host . ' (login failed)'; |
182
|
|
|
|
183
|
|
|
return false; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Delete file on FTP server |
188
|
|
|
* |
189
|
|
|
* @param string $remote_file |
190
|
|
|
* |
191
|
|
|
* @return bool |
192
|
|
|
*/ |
193
|
|
|
public function delete($remote_file = null) |
194
|
|
|
{ |
195
|
|
|
if (ftp_delete($this->_stream, $remote_file)) { |
196
|
|
|
return true; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
$this->error = 'Failed to delete file "' . $remote_file . '"'; |
200
|
|
|
|
201
|
|
|
return false; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Download file from server |
206
|
|
|
* |
207
|
|
|
* @param string $remote_file |
208
|
|
|
* @param string $local_file |
209
|
|
|
* @param int $mode |
210
|
|
|
* |
211
|
|
|
* @return bool |
212
|
|
|
*/ |
213
|
|
View Code Duplication |
public function get($remote_file = null, $local_file = null, $mode = FTP_ASCII) |
|
|
|
|
214
|
|
|
{ |
215
|
|
|
if (ftp_get($this->_stream, $local_file, $remote_file, $mode)) { |
216
|
|
|
return true; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
$this->error = 'Failed to download file "' . $remote_file . '"'; |
220
|
|
|
|
221
|
|
|
return false; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Get list of files/directories in directory |
226
|
|
|
* |
227
|
|
|
* @param string $directory |
228
|
|
|
* |
229
|
|
|
* @return array |
230
|
|
|
*/ |
231
|
|
|
public function ls($directory = null) |
232
|
|
|
{ |
233
|
|
|
if ($list = ftp_nlist($this->_stream, $directory)) { |
234
|
|
|
return $list; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
$this->error = 'Failed to get directory list'; |
238
|
|
|
|
239
|
|
|
return []; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Create directory on FTP server |
244
|
|
|
* |
245
|
|
|
* @param string $directory |
246
|
|
|
* |
247
|
|
|
* @return bool |
248
|
|
|
*/ |
249
|
|
View Code Duplication |
public function mkdir($directory = null) |
|
|
|
|
250
|
|
|
{ |
251
|
|
|
if (ftp_mkdir($this->_stream, $directory)) { |
252
|
|
|
return true; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
$this->error = 'Failed to create directory "' . $directory . '"'; |
256
|
|
|
|
257
|
|
|
return false; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Upload file to server |
262
|
|
|
* |
263
|
|
|
* @param string $local_file |
264
|
|
|
* @param string $remote_file |
265
|
|
|
* @param int $mode |
266
|
|
|
* |
267
|
|
|
* @return bool |
268
|
|
|
*/ |
269
|
|
View Code Duplication |
public function put($local_file = null, $remote_file = null, $mode = FTP_ASCII) |
|
|
|
|
270
|
|
|
{ |
271
|
|
|
if (ftp_put($this->_stream, $remote_file, $local_file, $mode)) { |
272
|
|
|
return true; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
$this->error = 'Failed to upload file "' . $local_file . '"'; |
276
|
|
|
|
277
|
|
|
return false; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* Get current directory |
282
|
|
|
* |
283
|
|
|
* @return string |
284
|
|
|
*/ |
285
|
|
|
public function pwd() |
286
|
|
|
{ |
287
|
|
|
return ftp_pwd($this->_stream); |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* Rename file on FTP server |
292
|
|
|
* |
293
|
|
|
* @param string $old_name |
294
|
|
|
* @param string $new_name |
295
|
|
|
* |
296
|
|
|
* @return bool |
297
|
|
|
*/ |
298
|
|
|
public function rename($old_name = null, $new_name = null) |
299
|
|
|
{ |
300
|
|
|
if (ftp_rename($this->_stream, $old_name, $new_name)) { |
301
|
|
|
return true; |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
$this->error = 'Failed to rename file "' . $old_name . '"'; |
305
|
|
|
|
306
|
|
|
return false; |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* Remove directory on FTP server |
311
|
|
|
* |
312
|
|
|
* @param string $directory |
313
|
|
|
* |
314
|
|
|
* @return bool |
315
|
|
|
*/ |
316
|
|
View Code Duplication |
public function rmdir($directory = null) |
|
|
|
|
317
|
|
|
{ |
318
|
|
|
if (ftp_rmdir($this->_stream, $directory)) { |
319
|
|
|
return true; |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
$this->error = 'Failed to remove directory "' . $directory . '"'; |
323
|
|
|
|
324
|
|
|
return false; |
325
|
|
|
} |
326
|
|
|
} |
327
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..