8 |
8 |
9 /** |
9 /** |
10 * WordPress Filesystem Class for implementing FTP Sockets. |
10 * WordPress Filesystem Class for implementing FTP Sockets. |
11 * |
11 * |
12 * @since 2.5.0 |
12 * @since 2.5.0 |
13 * @package WordPress |
13 * |
14 * @subpackage Filesystem |
14 * @see WP_Filesystem_Base |
15 * @uses WP_Filesystem_Base Extends class |
|
16 */ |
15 */ |
17 class WP_Filesystem_ftpsockets extends WP_Filesystem_Base { |
16 class WP_Filesystem_ftpsockets extends WP_Filesystem_Base { |
18 /** |
17 /** |
19 * @var ftp |
18 * @var ftp |
20 */ |
19 */ |
21 public $ftp; |
20 public $ftp; |
22 |
21 |
23 public function __construct($opt = '') { |
22 /** |
|
23 * |
|
24 * @param array $opt |
|
25 */ |
|
26 public function __construct( $opt = '' ) { |
24 $this->method = 'ftpsockets'; |
27 $this->method = 'ftpsockets'; |
25 $this->errors = new WP_Error(); |
28 $this->errors = new WP_Error(); |
26 |
29 |
27 // Check if possible to use ftp functions. |
30 // Check if possible to use ftp functions. |
28 if ( ! @include_once( ABSPATH . 'wp-admin/includes/class-ftp.php' ) ) { |
31 if ( ! @include_once( ABSPATH . 'wp-admin/includes/class-ftp.php' ) ) { |
31 $this->ftp = new ftp(); |
34 $this->ftp = new ftp(); |
32 |
35 |
33 if ( empty($opt['port']) ) |
36 if ( empty($opt['port']) ) |
34 $this->options['port'] = 21; |
37 $this->options['port'] = 21; |
35 else |
38 else |
36 $this->options['port'] = $opt['port']; |
39 $this->options['port'] = (int) $opt['port']; |
37 |
40 |
38 if ( empty($opt['hostname']) ) |
41 if ( empty($opt['hostname']) ) |
39 $this->errors->add('empty_hostname', __('FTP hostname is required')); |
42 $this->errors->add('empty_hostname', __('FTP hostname is required')); |
40 else |
43 else |
41 $this->options['hostname'] = $opt['hostname']; |
44 $this->options['hostname'] = $opt['hostname']; |
50 $this->errors->add('empty_password', __('FTP password is required')); |
53 $this->errors->add('empty_password', __('FTP password is required')); |
51 else |
54 else |
52 $this->options['password'] = $opt['password']; |
55 $this->options['password'] = $opt['password']; |
53 } |
56 } |
54 |
57 |
|
58 /** |
|
59 * |
|
60 * @return bool |
|
61 */ |
55 public function connect() { |
62 public function connect() { |
56 if ( ! $this->ftp ) |
63 if ( ! $this->ftp ) |
57 return false; |
64 return false; |
58 |
65 |
59 $this->ftp->setTimeout(FS_CONNECT_TIMEOUT); |
66 $this->ftp->setTimeout(FS_CONNECT_TIMEOUT); |
60 |
67 |
61 if ( ! $this->ftp->SetServer($this->options['hostname'], $this->options['port']) ) { |
68 if ( ! $this->ftp->SetServer( $this->options['hostname'], $this->options['port'] ) ) { |
62 $this->errors->add('connect', sprintf(__('Failed to connect to FTP Server %1$s:%2$s'), $this->options['hostname'], $this->options['port'])); |
69 $this->errors->add( 'connect', |
|
70 /* translators: %s: hostname:port */ |
|
71 sprintf( __( 'Failed to connect to FTP Server %s' ), |
|
72 $this->options['hostname'] . ':' . $this->options['port'] |
|
73 ) |
|
74 ); |
63 return false; |
75 return false; |
64 } |
76 } |
65 |
77 |
66 if ( ! $this->ftp->connect() ) { |
78 if ( ! $this->ftp->connect() ) { |
67 $this->errors->add('connect', sprintf(__('Failed to connect to FTP Server %1$s:%2$s'), $this->options['hostname'], $this->options['port'])); |
79 $this->errors->add( 'connect', |
68 return false; |
80 /* translators: %s: hostname:port */ |
69 } |
81 sprintf( __( 'Failed to connect to FTP Server %s' ), |
70 |
82 $this->options['hostname'] . ':' . $this->options['port'] |
71 if ( ! $this->ftp->login($this->options['username'], $this->options['password']) ) { |
83 ) |
72 $this->errors->add('auth', sprintf(__('Username/Password incorrect for %s'), $this->options['username'])); |
84 ); |
|
85 return false; |
|
86 } |
|
87 |
|
88 if ( ! $this->ftp->login( $this->options['username'], $this->options['password'] ) ) { |
|
89 $this->errors->add( 'auth', |
|
90 /* translators: %s: username */ |
|
91 sprintf( __( 'Username/Password incorrect for %s' ), |
|
92 $this->options['username'] |
|
93 ) |
|
94 ); |
73 return false; |
95 return false; |
74 } |
96 } |
75 |
97 |
76 $this->ftp->SetType( FTP_BINARY ); |
98 $this->ftp->SetType( FTP_BINARY ); |
77 $this->ftp->Passive( true ); |
99 $this->ftp->Passive( true ); |
78 $this->ftp->setTimeout( FS_TIMEOUT ); |
100 $this->ftp->setTimeout( FS_TIMEOUT ); |
79 return true; |
101 return true; |
80 } |
102 } |
81 |
103 |
82 /** |
104 /** |
83 * @param string $file |
105 * Retrieves the file contents. |
84 * @return false|string |
106 * |
|
107 * @since 2.5.0 |
|
108 * |
|
109 * @param string $file Filename. |
|
110 * @return string|false File contents on success, false if no temp file could be opened, |
|
111 * or if the file doesn't exist. |
85 */ |
112 */ |
86 public function get_contents( $file ) { |
113 public function get_contents( $file ) { |
87 if ( ! $this->exists($file) ) |
114 if ( ! $this->exists($file) ) |
88 return false; |
115 return false; |
89 |
116 |
90 $temp = wp_tempnam( $file ); |
117 $temp = wp_tempnam( $file ); |
91 |
118 |
92 if ( ! $temphandle = fopen($temp, 'w+') ) |
119 if ( ! $temphandle = fopen( $temp, 'w+' ) ) { |
93 return false; |
120 unlink( $temp ); |
|
121 return false; |
|
122 } |
94 |
123 |
95 mbstring_binary_safe_encoding(); |
124 mbstring_binary_safe_encoding(); |
96 |
125 |
97 if ( ! $this->ftp->fget($temphandle, $file) ) { |
126 if ( ! $this->ftp->fget($temphandle, $file) ) { |
98 fclose($temphandle); |
127 fclose($temphandle); |
161 $this->chmod($file, $mode); |
193 $this->chmod($file, $mode); |
162 |
194 |
163 return $ret; |
195 return $ret; |
164 } |
196 } |
165 |
197 |
|
198 /** |
|
199 * |
|
200 * @return string |
|
201 */ |
166 public function cwd() { |
202 public function cwd() { |
167 $cwd = $this->ftp->pwd(); |
203 $cwd = $this->ftp->pwd(); |
168 if ( $cwd ) |
204 if ( $cwd ) |
169 $cwd = trailingslashit($cwd); |
205 $cwd = trailingslashit($cwd); |
170 return $cwd; |
206 return $cwd; |
171 } |
207 } |
172 |
208 |
|
209 /** |
|
210 * |
|
211 * @param string $file |
|
212 * @return bool |
|
213 */ |
173 public function chdir($file) { |
214 public function chdir($file) { |
174 return $this->ftp->chdir($file); |
215 return $this->ftp->chdir($file); |
175 } |
216 } |
176 |
217 |
177 /** |
218 /** |
|
219 * |
178 * @param string $file |
220 * @param string $file |
179 * @param int|bool $mode |
221 * @param int|bool $mode |
180 * @param bool $recursive |
222 * @param bool $recursive |
181 * @return bool |
223 * @return bool |
182 */ |
224 */ |
200 // chmod the file or directory |
242 // chmod the file or directory |
201 return $this->ftp->chmod($file, $mode); |
243 return $this->ftp->chmod($file, $mode); |
202 } |
244 } |
203 |
245 |
204 /** |
246 /** |
|
247 * |
205 * @param string $file |
248 * @param string $file |
206 * @return string |
249 * @return string |
207 */ |
250 */ |
208 public function owner($file) { |
251 public function owner($file) { |
209 $dir = $this->dirlist($file); |
252 $dir = $this->dirlist($file); |
210 return $dir[$file]['owner']; |
253 return $dir[$file]['owner']; |
211 } |
254 } |
212 /** |
255 |
|
256 /** |
|
257 * |
213 * @param string $file |
258 * @param string $file |
214 * @return string |
259 * @return string |
215 */ |
260 */ |
216 public function getchmod($file) { |
261 public function getchmod($file) { |
217 $dir = $this->dirlist($file); |
262 $dir = $this->dirlist($file); |
218 return $dir[$file]['permsn']; |
263 return $dir[$file]['permsn']; |
219 } |
264 } |
220 /** |
265 |
|
266 /** |
|
267 * |
221 * @param string $file |
268 * @param string $file |
222 * @return string |
269 * @return string |
223 */ |
270 */ |
224 public function group($file) { |
271 public function group($file) { |
225 $dir = $this->dirlist($file); |
272 $dir = $this->dirlist($file); |
226 return $dir[$file]['group']; |
273 return $dir[$file]['group']; |
227 } |
274 } |
228 /** |
275 |
|
276 /** |
|
277 * |
|
278 * @param string $source |
|
279 * @param string $destination |
|
280 * @param bool $overwrite |
|
281 * @param int|bool $mode |
|
282 * @return bool |
|
283 */ |
|
284 public function copy($source, $destination, $overwrite = false, $mode = false) { |
|
285 if ( ! $overwrite && $this->exists($destination) ) |
|
286 return false; |
|
287 |
|
288 $content = $this->get_contents($source); |
|
289 if ( false === $content ) |
|
290 return false; |
|
291 |
|
292 return $this->put_contents($destination, $content, $mode); |
|
293 } |
|
294 |
|
295 /** |
|
296 * |
229 * @param string $source |
297 * @param string $source |
230 * @param string $destination |
298 * @param string $destination |
231 * @param bool $overwrite |
299 * @param bool $overwrite |
232 * @param int|bool $mode |
|
233 * @return bool |
|
234 */ |
|
235 public function copy($source, $destination, $overwrite = false, $mode = false) { |
|
236 if ( ! $overwrite && $this->exists($destination) ) |
|
237 return false; |
|
238 |
|
239 $content = $this->get_contents($source); |
|
240 if ( false === $content ) |
|
241 return false; |
|
242 |
|
243 return $this->put_contents($destination, $content, $mode); |
|
244 } |
|
245 /** |
|
246 * @param string $source |
|
247 * @param string $destination |
|
248 * @param bool $overwrite |
|
249 * @return bool |
300 * @return bool |
250 */ |
301 */ |
251 public function move($source, $destination, $overwrite = false ) { |
302 public function move($source, $destination, $overwrite = false ) { |
252 return $this->ftp->rename($source, $destination); |
303 return $this->ftp->rename($source, $destination); |
253 } |
304 } |
254 /** |
305 |
255 * @param string $file |
306 /** |
256 * @param bool $recursive |
307 * |
|
308 * @param string $file |
|
309 * @param bool $recursive |
257 * @param string $type |
310 * @param string $type |
258 * @return bool |
311 * @return bool |
259 */ |
312 */ |
260 public function delete($file, $recursive = false, $type = false) { |
313 public function delete($file, $recursive = false, $type = false) { |
261 if ( empty($file) ) |
314 if ( empty($file) ) |
307 } |
363 } |
308 return false; |
364 return false; |
309 } |
365 } |
310 |
366 |
311 /** |
367 /** |
|
368 * |
312 * @param string $file |
369 * @param string $file |
313 * @return bool |
370 * @return bool |
314 */ |
371 */ |
315 public function is_readable($file) { |
372 public function is_readable($file) { |
316 return true; |
373 return true; |
317 } |
374 } |
318 |
375 |
319 /** |
376 /** |
|
377 * |
320 * @param string $file |
378 * @param string $file |
321 * @return bool |
379 * @return bool |
322 */ |
380 */ |
323 public function is_writable($file) { |
381 public function is_writable($file) { |
324 return true; |
382 return true; |
325 } |
383 } |
326 |
384 |
327 /** |
385 /** |
|
386 * |
328 * @param string $file |
387 * @param string $file |
329 * @return bool |
388 * @return bool |
330 */ |
389 */ |
331 public function atime($file) { |
390 public function atime($file) { |
332 return false; |
391 return false; |
333 } |
392 } |
334 |
393 |
335 /** |
394 /** |
|
395 * |
336 * @param string $file |
396 * @param string $file |
337 * @return int |
397 * @return int |
338 */ |
398 */ |
339 public function mtime($file) { |
399 public function mtime($file) { |
340 return $this->ftp->mdtm($file); |
400 return $this->ftp->mdtm($file); |
345 * @return int |
405 * @return int |
346 */ |
406 */ |
347 public function size($file) { |
407 public function size($file) { |
348 return $this->ftp->filesize($file); |
408 return $this->ftp->filesize($file); |
349 } |
409 } |
350 /** |
410 |
|
411 /** |
|
412 * |
351 * @param string $file |
413 * @param string $file |
352 * @param int $time |
414 * @param int $time |
353 * @param int $atime |
415 * @param int $atime |
354 * @return bool |
416 * @return bool |
355 */ |
417 */ |
356 public function touch($file, $time = 0, $atime = 0 ) { |
418 public function touch($file, $time = 0, $atime = 0 ) { |
357 return false; |
419 return false; |
358 } |
420 } |
359 |
421 |
360 /** |
422 /** |
|
423 * |
361 * @param string $path |
424 * @param string $path |
362 * @param mixed $chmod |
425 * @param mixed $chmod |
363 * @param mixed $chown |
426 * @param mixed $chown |
364 * @param mixed $chgrp |
427 * @param mixed $chgrp |
365 * @return bool |
428 * @return bool |
366 */ |
429 */ |
367 public function mkdir($path, $chmod = false, $chown = false, $chgrp = false ) { |
430 public function mkdir($path, $chmod = false, $chown = false, $chgrp = false ) { |
368 $path = untrailingslashit($path); |
431 $path = untrailingslashit($path); |
369 if ( empty($path) ) |
432 if ( empty($path) ) |
376 $this->chmod($path, $chmod); |
439 $this->chmod($path, $chmod); |
377 return true; |
440 return true; |
378 } |
441 } |
379 |
442 |
380 /** |
443 /** |
381 * @param sting $path |
444 * |
|
445 * @param string $path |
382 * @param bool $recursive |
446 * @param bool $recursive |
|
447 * @return bool |
383 */ |
448 */ |
384 public function rmdir($path, $recursive = false ) { |
449 public function rmdir($path, $recursive = false ) { |
385 $this->delete($path, $recursive); |
450 return $this->delete($path, $recursive); |
386 } |
451 } |
387 |
452 |
388 /** |
453 /** |
|
454 * |
389 * @param string $path |
455 * @param string $path |
390 * @param bool $include_hidden |
456 * @param bool $include_hidden |
391 * @param bool $recursive |
457 * @param bool $recursive |
392 * @return bool|array |
458 * @return bool|array |
393 */ |
459 */ |
394 public function dirlist($path = '.', $include_hidden = true, $recursive = false ) { |
460 public function dirlist($path = '.', $include_hidden = true, $recursive = false ) { |
395 if ( $this->is_file($path) ) { |
461 if ( $this->is_file($path) ) { |
396 $limit_file = basename($path); |
462 $limit_file = basename($path); |
430 |
496 |
431 // Replace symlinks formatted as "source -> target" with just the source name |
497 // Replace symlinks formatted as "source -> target" with just the source name |
432 if ( $struc['islink'] ) |
498 if ( $struc['islink'] ) |
433 $struc['name'] = preg_replace( '/(\s*->\s*.*)$/', '', $struc['name'] ); |
499 $struc['name'] = preg_replace( '/(\s*->\s*.*)$/', '', $struc['name'] ); |
434 |
500 |
|
501 // Add the Octal representation of the file permissions |
|
502 $struc['permsn'] = $this->getnumchmodfromh( $struc['perms'] ); |
|
503 |
435 $ret[ $struc['name'] ] = $struc; |
504 $ret[ $struc['name'] ] = $struc; |
436 } |
505 } |
437 |
506 |
438 reset_mbstring_encoding(); |
507 reset_mbstring_encoding(); |
439 |
508 |
440 return $ret; |
509 return $ret; |
441 } |
510 } |
442 |
511 |
|
512 /** |
|
513 */ |
443 public function __destruct() { |
514 public function __destruct() { |
444 $this->ftp->quit(); |
515 $this->ftp->quit(); |
445 } |
516 } |
446 } |
517 } |