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+');
124 if ( ! @ftp_fget( $this->link, $temp, $file, FTP_BINARY ) ) {
130 fseek( $temp, 0 ); // Skip back to the start of the file being written to
133 while ( ! feof($temp) )
134 $contents .= fread($temp, 8192);
144 * @param string $file
147 public function get_contents_array($file) {
148 return explode("\n", $this->get_contents($file));
154 * @param string $file
155 * @param string $contents
156 * @param bool|int $mode
159 public function put_contents($file, $contents, $mode = false ) {
160 $tempfile = wp_tempnam($file);
161 $temp = fopen( $tempfile, 'wb+' );
168 mbstring_binary_safe_encoding();
170 $data_length = strlen( $contents );
171 $bytes_written = fwrite( $temp, $contents );
173 reset_mbstring_encoding();
175 if ( $data_length !== $bytes_written ) {
181 fseek( $temp, 0 ); // Skip back to the start of the file being written to
183 $ret = @ftp_fput( $this->link, $file, $temp, FTP_BINARY );
188 $this->chmod($file, $mode);
198 public function cwd() {
199 $cwd = @ftp_pwd($this->link);
201 $cwd = trailingslashit($cwd);
211 public function chdir($dir) {
212 return @ftp_chdir($this->link, $dir);
218 * @param string $file
220 * @param bool $recursive
223 public function chmod($file, $mode = false, $recursive = false) {
225 if ( $this->is_file($file) )
226 $mode = FS_CHMOD_FILE;
227 elseif ( $this->is_dir($file) )
228 $mode = FS_CHMOD_DIR;
233 // chmod any sub-objects if recursive.
234 if ( $recursive && $this->is_dir($file) ) {
235 $filelist = $this->dirlist($file);
236 foreach ( (array)$filelist as $filename => $filemeta )
237 $this->chmod($file . '/' . $filename, $mode, $recursive);
240 // chmod the file or directory
241 if ( ! function_exists('ftp_chmod') )
242 return (bool)@ftp_site($this->link, sprintf('CHMOD %o %s', $mode, $file));
243 return (bool)@ftp_chmod($this->link, $mode, $file);
249 * @param string $file
252 public function owner($file) {
253 $dir = $this->dirlist($file);
254 return $dir[$file]['owner'];
259 * @param string $file
262 public function getchmod($file) {
263 $dir = $this->dirlist($file);
264 return $dir[$file]['permsn'];
270 * @param string $file
273 public function group($file) {
274 $dir = $this->dirlist($file);
275 return $dir[$file]['group'];
281 * @param string $source
282 * @param string $destination
283 * @param bool $overwrite
284 * @param string|bool $mode
287 public function copy($source, $destination, $overwrite = false, $mode = false) {
288 if ( ! $overwrite && $this->exists($destination) )
290 $content = $this->get_contents($source);
291 if ( false === $content )
293 return $this->put_contents($destination, $content, $mode);
299 * @param string $source
300 * @param string $destination
301 * @param bool $overwrite
304 public function move($source, $destination, $overwrite = false) {
305 return ftp_rename($this->link, $source, $destination);
311 * @param string $file
312 * @param bool $recursive
313 * @param string $type
316 public function delete($file, $recursive = false, $type = false) {
319 if ( 'f' == $type || $this->is_file($file) )
320 return @ftp_delete($this->link, $file);
322 return @ftp_rmdir($this->link, $file);
324 $filelist = $this->dirlist( trailingslashit($file) );
325 if ( !empty($filelist) )
326 foreach ( $filelist as $delete_file )
327 $this->delete( trailingslashit($file) . $delete_file['name'], $recursive, $delete_file['type'] );
328 return @ftp_rmdir($this->link, $file);
334 * @param string $file
337 public function exists($file) {
338 $list = @ftp_nlist($this->link, $file);
340 if ( empty( $list ) && $this->is_dir( $file ) ) {
341 return true; // File is an empty directory.
344 return !empty($list); //empty list = no file, so invert.
350 * @param string $file
353 public function is_file($file) {
354 return $this->exists($file) && !$this->is_dir($file);
360 * @param string $path
363 public function is_dir($path) {
365 $result = @ftp_chdir($this->link, trailingslashit($path) );
366 if ( $result && $path == $this->cwd() || $this->cwd() != $cwd ) {
367 @ftp_chdir($this->link, $cwd);
376 * @param string $file
379 public function is_readable($file) {
386 * @param string $file
389 public function is_writable($file) {
396 * @param string $file
399 public function atime($file) {
406 * @param string $file
409 public function mtime($file) {
410 return ftp_mdtm($this->link, $file);
416 * @param string $file
419 public function size($file) {
420 return ftp_size($this->link, $file);
426 * @param string $file
429 public function touch($file, $time = 0, $atime = 0) {
436 * @param string $path
437 * @param mixed $chmod
438 * @param mixed $chown
439 * @param mixed $chgrp
442 public function mkdir($path, $chmod = false, $chown = false, $chgrp = false) {
443 $path = untrailingslashit($path);
447 if ( !@ftp_mkdir($this->link, $path) )
449 $this->chmod($path, $chmod);
456 * @param string $path
457 * @param bool $recursive
460 public function rmdir($path, $recursive = false) {
461 return $this->delete($path, $recursive);
467 * @staticvar bool $is_windows
468 * @param string $line
471 public function parselisting($line) {
472 static $is_windows = null;
473 if ( is_null($is_windows) )
474 $is_windows = stripos( ftp_systype($this->link), 'win') !== false;
476 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) ) {
478 if ( $lucifer[3] < 70 )
481 $lucifer[3] += 1900; // 4digit year fix
482 $b['isdir'] = ( $lucifer[7] == '<DIR>');
487 $b['size'] = $lucifer[7];
488 $b['month'] = $lucifer[1];
489 $b['day'] = $lucifer[2];
490 $b['year'] = $lucifer[3];
491 $b['hour'] = $lucifer[4];
492 $b['minute'] = $lucifer[5];
493 $b['time'] = @mktime($lucifer[4] + (strcasecmp($lucifer[6], "PM") == 0 ? 12 : 0), $lucifer[5], 0, $lucifer[1], $lucifer[2], $lucifer[3]);
494 $b['am/pm'] = $lucifer[6];
495 $b['name'] = $lucifer[8];
496 } elseif ( !$is_windows && $lucifer = preg_split('/[ ]/', $line, 9, PREG_SPLIT_NO_EMPTY)) {
498 $lcount = count($lucifer);
502 $b['isdir'] = $lucifer[0]{0} === 'd';
503 $b['islink'] = $lucifer[0]{0} === 'l';
506 elseif ( $b['islink'] )
510 $b['perms'] = $lucifer[0];
511 $b['permsn'] = $this->getnumchmodfromh( $b['perms'] );
512 $b['number'] = $lucifer[1];
513 $b['owner'] = $lucifer[2];
514 $b['group'] = $lucifer[3];
515 $b['size'] = $lucifer[4];
516 if ( $lcount == 8 ) {
517 sscanf($lucifer[5], '%d-%d-%d', $b['year'], $b['month'], $b['day']);
518 sscanf($lucifer[6], '%d:%d', $b['hour'], $b['minute']);
519 $b['time'] = @mktime($b['hour'], $b['minute'], 0, $b['month'], $b['day'], $b['year']);
520 $b['name'] = $lucifer[7];
522 $b['month'] = $lucifer[5];
523 $b['day'] = $lucifer[6];
524 if ( preg_match('/([0-9]{2}):([0-9]{2})/', $lucifer[7], $l2) ) {
525 $b['year'] = date("Y");
527 $b['minute'] = $l2[2];
529 $b['year'] = $lucifer[7];
533 $b['time'] = strtotime( sprintf('%d %s %d %02d:%02d', $b['day'], $b['month'], $b['year'], $b['hour'], $b['minute']) );
534 $b['name'] = $lucifer[8];
538 // Replace symlinks formatted as "source -> target" with just the source name
539 if ( isset( $b['islink'] ) && $b['islink'] ) {
540 $b['name'] = preg_replace( '/(\s*->\s*.*)$/', '', $b['name'] );
549 * @param string $path
550 * @param bool $include_hidden
551 * @param bool $recursive
554 public function dirlist($path = '.', $include_hidden = true, $recursive = false) {
555 if ( $this->is_file($path) ) {
556 $limit_file = basename($path);
557 $path = dirname($path) . '/';
562 $pwd = @ftp_pwd($this->link);
563 if ( ! @ftp_chdir($this->link, $path) ) // Cant change to folder = folder doesn't exist
565 $list = @ftp_rawlist($this->link, '-a', false);
566 @ftp_chdir($this->link, $pwd);
568 if ( empty($list) ) // Empty array = non-existent folder (real folder will show . at least)
572 foreach ( $list as $k => $v ) {
573 $entry = $this->parselisting($v);
577 if ( '.' == $entry['name'] || '..' == $entry['name'] )
580 if ( ! $include_hidden && '.' == $entry['name'][0] )
583 if ( $limit_file && $entry['name'] != $limit_file)
586 $dirlist[ $entry['name'] ] = $entry;
590 foreach ( (array)$dirlist as $struc ) {
591 if ( 'd' == $struc['type'] ) {
593 $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $include_hidden, $recursive);
595 $struc['files'] = array();
598 $ret[ $struc['name'] ] = $struc;
606 public function __destruct() {
608 ftp_close($this->link);