3 * WordPress FTP Filesystem.
6 * @subpackage Filesystem
10 * WordPress Filesystem Class for implementing FTP.
14 * @subpackage Filesystem
15 * @uses WP_Filesystem_Base Extends class
17 class WP_Filesystem_FTPext extends WP_Filesystem_Base {
25 public function __construct( $opt = '' ) {
26 $this->method = 'ftpext';
27 $this->errors = new WP_Error();
29 // Check if possible to use ftp functions.
30 if ( ! extension_loaded('ftp') ) {
31 $this->errors->add('no_ftp_ext', __('The ftp PHP extension is not available'));
35 // This Class uses the timeout on a per-connection basis, Others use it on a per-action basis.
37 if ( ! defined('FS_TIMEOUT') )
38 define('FS_TIMEOUT', 240);
40 if ( empty($opt['port']) )
41 $this->options['port'] = 21;
43 $this->options['port'] = $opt['port'];
45 if ( empty($opt['hostname']) )
46 $this->errors->add('empty_hostname', __('FTP hostname is required'));
48 $this->options['hostname'] = $opt['hostname'];
50 // Check if the options provided are OK.
51 if ( empty($opt['username']) )
52 $this->errors->add('empty_username', __('FTP username is required'));
54 $this->options['username'] = $opt['username'];
56 if ( empty($opt['password']) )
57 $this->errors->add('empty_password', __('FTP password is required'));
59 $this->options['password'] = $opt['password'];
61 $this->options['ssl'] = false;
62 if ( isset($opt['connection_type']) && 'ftps' == $opt['connection_type'] )
63 $this->options['ssl'] = true;
71 public function connect() {
72 if ( isset($this->options['ssl']) && $this->options['ssl'] && function_exists('ftp_ssl_connect') )
73 $this->link = @ftp_ssl_connect($this->options['hostname'], $this->options['port'], FS_CONNECT_TIMEOUT);
75 $this->link = @ftp_connect($this->options['hostname'], $this->options['port'], FS_CONNECT_TIMEOUT);
77 if ( ! $this->link ) {
78 $this->errors->add( 'connect',
79 /* translators: %s: hostname:port */
80 sprintf( __( 'Failed to connect to FTP Server %s' ),
81 $this->options['hostname'] . ':' . $this->options['port']
87 if ( ! @ftp_login( $this->link,$this->options['username'], $this->options['password'] ) ) {
88 $this->errors->add( 'auth',
89 /* translators: %s: username */
90 sprintf( __( 'Username/Password incorrect for %s' ),
91 $this->options['username']
97 // Set the Connection to use Passive FTP
98 @ftp_pasv( $this->link, true );
99 if ( @ftp_get_option($this->link, FTP_TIMEOUT_SEC) < FS_TIMEOUT )
100 @ftp_set_option($this->link, FTP_TIMEOUT_SEC, FS_TIMEOUT);
106 * Retrieves the file contents.
111 * @param string $file Filename.
112 * @return string|false File contents on success, false if no temp file could be opened,
113 * or if the file couldn't be retrieved.
115 public function get_contents( $file ) {
116 $tempfile = wp_tempnam($file);
117 $temp = fopen($tempfile, 'w+');
122 if ( ! @ftp_fget($this->link, $temp, $file, FTP_BINARY ) )
125 fseek( $temp, 0 ); // Skip back to the start of the file being written to
128 while ( ! feof($temp) )
129 $contents .= fread($temp, 8192);
139 * @param string $file
142 public function get_contents_array($file) {
143 return explode("\n", $this->get_contents($file));
149 * @param string $file
150 * @param string $contents
151 * @param bool|int $mode
154 public function put_contents($file, $contents, $mode = false ) {
155 $tempfile = wp_tempnam($file);
156 $temp = fopen( $tempfile, 'wb+' );
160 mbstring_binary_safe_encoding();
162 $data_length = strlen( $contents );
163 $bytes_written = fwrite( $temp, $contents );
165 reset_mbstring_encoding();
167 if ( $data_length !== $bytes_written ) {
173 fseek( $temp, 0 ); // Skip back to the start of the file being written to
175 $ret = @ftp_fput( $this->link, $file, $temp, FTP_BINARY );
180 $this->chmod($file, $mode);
190 public function cwd() {
191 $cwd = @ftp_pwd($this->link);
193 $cwd = trailingslashit($cwd);
203 public function chdir($dir) {
204 return @ftp_chdir($this->link, $dir);
210 * @param string $file
212 * @param bool $recursive
215 public function chmod($file, $mode = false, $recursive = false) {
217 if ( $this->is_file($file) )
218 $mode = FS_CHMOD_FILE;
219 elseif ( $this->is_dir($file) )
220 $mode = FS_CHMOD_DIR;
225 // chmod any sub-objects if recursive.
226 if ( $recursive && $this->is_dir($file) ) {
227 $filelist = $this->dirlist($file);
228 foreach ( (array)$filelist as $filename => $filemeta )
229 $this->chmod($file . '/' . $filename, $mode, $recursive);
232 // chmod the file or directory
233 if ( ! function_exists('ftp_chmod') )
234 return (bool)@ftp_site($this->link, sprintf('CHMOD %o %s', $mode, $file));
235 return (bool)@ftp_chmod($this->link, $mode, $file);
241 * @param string $file
244 public function owner($file) {
245 $dir = $this->dirlist($file);
246 return $dir[$file]['owner'];
251 * @param string $file
254 public function getchmod($file) {
255 $dir = $this->dirlist($file);
256 return $dir[$file]['permsn'];
262 * @param string $file
265 public function group($file) {
266 $dir = $this->dirlist($file);
267 return $dir[$file]['group'];
273 * @param string $source
274 * @param string $destination
275 * @param bool $overwrite
276 * @param string|bool $mode
279 public function copy($source, $destination, $overwrite = false, $mode = false) {
280 if ( ! $overwrite && $this->exists($destination) )
282 $content = $this->get_contents($source);
283 if ( false === $content )
285 return $this->put_contents($destination, $content, $mode);
291 * @param string $source
292 * @param string $destination
293 * @param bool $overwrite
296 public function move($source, $destination, $overwrite = false) {
297 return ftp_rename($this->link, $source, $destination);
303 * @param string $file
304 * @param bool $recursive
305 * @param string $type
308 public function delete($file, $recursive = false, $type = false) {
311 if ( 'f' == $type || $this->is_file($file) )
312 return @ftp_delete($this->link, $file);
314 return @ftp_rmdir($this->link, $file);
316 $filelist = $this->dirlist( trailingslashit($file) );
317 if ( !empty($filelist) )
318 foreach ( $filelist as $delete_file )
319 $this->delete( trailingslashit($file) . $delete_file['name'], $recursive, $delete_file['type'] );
320 return @ftp_rmdir($this->link, $file);
326 * @param string $file
329 public function exists($file) {
330 $list = @ftp_nlist($this->link, $file);
332 if ( empty( $list ) && $this->is_dir( $file ) ) {
333 return true; // File is an empty directory.
336 return !empty($list); //empty list = no file, so invert.
342 * @param string $file
345 public function is_file($file) {
346 return $this->exists($file) && !$this->is_dir($file);
352 * @param string $path
355 public function is_dir($path) {
357 $result = @ftp_chdir($this->link, trailingslashit($path) );
358 if ( $result && $path == $this->cwd() || $this->cwd() != $cwd ) {
359 @ftp_chdir($this->link, $cwd);
368 * @param string $file
371 public function is_readable($file) {
378 * @param string $file
381 public function is_writable($file) {
388 * @param string $file
391 public function atime($file) {
398 * @param string $file
401 public function mtime($file) {
402 return ftp_mdtm($this->link, $file);
408 * @param string $file
411 public function size($file) {
412 return ftp_size($this->link, $file);
418 * @param string $file
421 public function touch($file, $time = 0, $atime = 0) {
428 * @param string $path
429 * @param mixed $chmod
430 * @param mixed $chown
431 * @param mixed $chgrp
434 public function mkdir($path, $chmod = false, $chown = false, $chgrp = false) {
435 $path = untrailingslashit($path);
439 if ( !@ftp_mkdir($this->link, $path) )
441 $this->chmod($path, $chmod);
448 * @param string $path
449 * @param bool $recursive
452 public function rmdir($path, $recursive = false) {
453 return $this->delete($path, $recursive);
459 * @staticvar bool $is_windows
460 * @param string $line
463 public function parselisting($line) {
464 static $is_windows = null;
465 if ( is_null($is_windows) )
466 $is_windows = stripos( ftp_systype($this->link), 'win') !== false;
468 if ( $is_windows && preg_match('/([0-9]{2})-([0-9]{2})-([0-9]{2}) +([0-9]{2}):([0-9]{2})(AM|PM) +([0-9]+|<DIR>) +(.+)/', $line, $lucifer) ) {
470 if ( $lucifer[3] < 70 )
473 $lucifer[3] += 1900; // 4digit year fix
474 $b['isdir'] = ( $lucifer[7] == '<DIR>');
479 $b['size'] = $lucifer[7];
480 $b['month'] = $lucifer[1];
481 $b['day'] = $lucifer[2];
482 $b['year'] = $lucifer[3];
483 $b['hour'] = $lucifer[4];
484 $b['minute'] = $lucifer[5];
485 $b['time'] = @mktime($lucifer[4] + (strcasecmp($lucifer[6], "PM") == 0 ? 12 : 0), $lucifer[5], 0, $lucifer[1], $lucifer[2], $lucifer[3]);
486 $b['am/pm'] = $lucifer[6];
487 $b['name'] = $lucifer[8];
488 } elseif ( !$is_windows && $lucifer = preg_split('/[ ]/', $line, 9, PREG_SPLIT_NO_EMPTY)) {
490 $lcount = count($lucifer);
494 $b['isdir'] = $lucifer[0]{0} === 'd';
495 $b['islink'] = $lucifer[0]{0} === 'l';
498 elseif ( $b['islink'] )
502 $b['perms'] = $lucifer[0];
503 $b['permsn'] = $this->getnumchmodfromh( $b['perms'] );
504 $b['number'] = $lucifer[1];
505 $b['owner'] = $lucifer[2];
506 $b['group'] = $lucifer[3];
507 $b['size'] = $lucifer[4];
508 if ( $lcount == 8 ) {
509 sscanf($lucifer[5], '%d-%d-%d', $b['year'], $b['month'], $b['day']);
510 sscanf($lucifer[6], '%d:%d', $b['hour'], $b['minute']);
511 $b['time'] = @mktime($b['hour'], $b['minute'], 0, $b['month'], $b['day'], $b['year']);
512 $b['name'] = $lucifer[7];
514 $b['month'] = $lucifer[5];
515 $b['day'] = $lucifer[6];
516 if ( preg_match('/([0-9]{2}):([0-9]{2})/', $lucifer[7], $l2) ) {
517 $b['year'] = date("Y");
519 $b['minute'] = $l2[2];
521 $b['year'] = $lucifer[7];
525 $b['time'] = strtotime( sprintf('%d %s %d %02d:%02d', $b['day'], $b['month'], $b['year'], $b['hour'], $b['minute']) );
526 $b['name'] = $lucifer[8];
530 // Replace symlinks formatted as "source -> target" with just the source name
531 if ( isset( $b['islink'] ) && $b['islink'] ) {
532 $b['name'] = preg_replace( '/(\s*->\s*.*)$/', '', $b['name'] );
541 * @param string $path
542 * @param bool $include_hidden
543 * @param bool $recursive
546 public function dirlist($path = '.', $include_hidden = true, $recursive = false) {
547 if ( $this->is_file($path) ) {
548 $limit_file = basename($path);
549 $path = dirname($path) . '/';
554 $pwd = @ftp_pwd($this->link);
555 if ( ! @ftp_chdir($this->link, $path) ) // Cant change to folder = folder doesn't exist
557 $list = @ftp_rawlist($this->link, '-a', false);
558 @ftp_chdir($this->link, $pwd);
560 if ( empty($list) ) // Empty array = non-existent folder (real folder will show . at least)
564 foreach ( $list as $k => $v ) {
565 $entry = $this->parselisting($v);
569 if ( '.' == $entry['name'] || '..' == $entry['name'] )
572 if ( ! $include_hidden && '.' == $entry['name'][0] )
575 if ( $limit_file && $entry['name'] != $limit_file)
578 $dirlist[ $entry['name'] ] = $entry;
582 foreach ( (array)$dirlist as $struc ) {
583 if ( 'd' == $struc['type'] ) {
585 $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $include_hidden, $recursive);
587 $struc['files'] = array();
590 $ret[ $struc['name'] ] = $struc;
598 public function __destruct() {
600 ftp_close($this->link);