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+' );
165 mbstring_binary_safe_encoding();
167 $data_length = strlen( $contents );
168 $bytes_written = fwrite( $temp, $contents );
170 reset_mbstring_encoding();
172 if ( $data_length !== $bytes_written ) {
178 fseek( $temp, 0 ); // Skip back to the start of the file being written to
180 $ret = @ftp_fput( $this->link, $file, $temp, FTP_BINARY );
185 $this->chmod($file, $mode);
195 public function cwd() {
196 $cwd = @ftp_pwd($this->link);
198 $cwd = trailingslashit($cwd);
208 public function chdir($dir) {
209 return @ftp_chdir($this->link, $dir);
215 * @param string $file
217 * @param bool $recursive
220 public function chmod($file, $mode = false, $recursive = false) {
222 if ( $this->is_file($file) )
223 $mode = FS_CHMOD_FILE;
224 elseif ( $this->is_dir($file) )
225 $mode = FS_CHMOD_DIR;
230 // chmod any sub-objects if recursive.
231 if ( $recursive && $this->is_dir($file) ) {
232 $filelist = $this->dirlist($file);
233 foreach ( (array)$filelist as $filename => $filemeta )
234 $this->chmod($file . '/' . $filename, $mode, $recursive);
237 // chmod the file or directory
238 if ( ! function_exists('ftp_chmod') )
239 return (bool)@ftp_site($this->link, sprintf('CHMOD %o %s', $mode, $file));
240 return (bool)@ftp_chmod($this->link, $mode, $file);
246 * @param string $file
249 public function owner($file) {
250 $dir = $this->dirlist($file);
251 return $dir[$file]['owner'];
256 * @param string $file
259 public function getchmod($file) {
260 $dir = $this->dirlist($file);
261 return $dir[$file]['permsn'];
267 * @param string $file
270 public function group($file) {
271 $dir = $this->dirlist($file);
272 return $dir[$file]['group'];
278 * @param string $source
279 * @param string $destination
280 * @param bool $overwrite
281 * @param string|bool $mode
284 public function copy($source, $destination, $overwrite = false, $mode = false) {
285 if ( ! $overwrite && $this->exists($destination) )
287 $content = $this->get_contents($source);
288 if ( false === $content )
290 return $this->put_contents($destination, $content, $mode);
296 * @param string $source
297 * @param string $destination
298 * @param bool $overwrite
301 public function move($source, $destination, $overwrite = false) {
302 return ftp_rename($this->link, $source, $destination);
308 * @param string $file
309 * @param bool $recursive
310 * @param string $type
313 public function delete($file, $recursive = false, $type = false) {
316 if ( 'f' == $type || $this->is_file($file) )
317 return @ftp_delete($this->link, $file);
319 return @ftp_rmdir($this->link, $file);
321 $filelist = $this->dirlist( trailingslashit($file) );
322 if ( !empty($filelist) )
323 foreach ( $filelist as $delete_file )
324 $this->delete( trailingslashit($file) . $delete_file['name'], $recursive, $delete_file['type'] );
325 return @ftp_rmdir($this->link, $file);
331 * @param string $file
334 public function exists($file) {
335 $list = @ftp_nlist($this->link, $file);
337 if ( empty( $list ) && $this->is_dir( $file ) ) {
338 return true; // File is an empty directory.
341 return !empty($list); //empty list = no file, so invert.
347 * @param string $file
350 public function is_file($file) {
351 return $this->exists($file) && !$this->is_dir($file);
357 * @param string $path
360 public function is_dir($path) {
362 $result = @ftp_chdir($this->link, trailingslashit($path) );
363 if ( $result && $path == $this->cwd() || $this->cwd() != $cwd ) {
364 @ftp_chdir($this->link, $cwd);
373 * @param string $file
376 public function is_readable($file) {
383 * @param string $file
386 public function is_writable($file) {
393 * @param string $file
396 public function atime($file) {
403 * @param string $file
406 public function mtime($file) {
407 return ftp_mdtm($this->link, $file);
413 * @param string $file
416 public function size($file) {
417 return ftp_size($this->link, $file);
423 * @param string $file
426 public function touch($file, $time = 0, $atime = 0) {
433 * @param string $path
434 * @param mixed $chmod
435 * @param mixed $chown
436 * @param mixed $chgrp
439 public function mkdir($path, $chmod = false, $chown = false, $chgrp = false) {
440 $path = untrailingslashit($path);
444 if ( !@ftp_mkdir($this->link, $path) )
446 $this->chmod($path, $chmod);
453 * @param string $path
454 * @param bool $recursive
457 public function rmdir($path, $recursive = false) {
458 return $this->delete($path, $recursive);
464 * @staticvar bool $is_windows
465 * @param string $line
468 public function parselisting($line) {
469 static $is_windows = null;
470 if ( is_null($is_windows) )
471 $is_windows = stripos( ftp_systype($this->link), 'win') !== false;
473 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) ) {
475 if ( $lucifer[3] < 70 )
478 $lucifer[3] += 1900; // 4digit year fix
479 $b['isdir'] = ( $lucifer[7] == '<DIR>');
484 $b['size'] = $lucifer[7];
485 $b['month'] = $lucifer[1];
486 $b['day'] = $lucifer[2];
487 $b['year'] = $lucifer[3];
488 $b['hour'] = $lucifer[4];
489 $b['minute'] = $lucifer[5];
490 $b['time'] = @mktime($lucifer[4] + (strcasecmp($lucifer[6], "PM") == 0 ? 12 : 0), $lucifer[5], 0, $lucifer[1], $lucifer[2], $lucifer[3]);
491 $b['am/pm'] = $lucifer[6];
492 $b['name'] = $lucifer[8];
493 } elseif ( !$is_windows && $lucifer = preg_split('/[ ]/', $line, 9, PREG_SPLIT_NO_EMPTY)) {
495 $lcount = count($lucifer);
499 $b['isdir'] = $lucifer[0]{0} === 'd';
500 $b['islink'] = $lucifer[0]{0} === 'l';
503 elseif ( $b['islink'] )
507 $b['perms'] = $lucifer[0];
508 $b['permsn'] = $this->getnumchmodfromh( $b['perms'] );
509 $b['number'] = $lucifer[1];
510 $b['owner'] = $lucifer[2];
511 $b['group'] = $lucifer[3];
512 $b['size'] = $lucifer[4];
513 if ( $lcount == 8 ) {
514 sscanf($lucifer[5], '%d-%d-%d', $b['year'], $b['month'], $b['day']);
515 sscanf($lucifer[6], '%d:%d', $b['hour'], $b['minute']);
516 $b['time'] = @mktime($b['hour'], $b['minute'], 0, $b['month'], $b['day'], $b['year']);
517 $b['name'] = $lucifer[7];
519 $b['month'] = $lucifer[5];
520 $b['day'] = $lucifer[6];
521 if ( preg_match('/([0-9]{2}):([0-9]{2})/', $lucifer[7], $l2) ) {
522 $b['year'] = date("Y");
524 $b['minute'] = $l2[2];
526 $b['year'] = $lucifer[7];
530 $b['time'] = strtotime( sprintf('%d %s %d %02d:%02d', $b['day'], $b['month'], $b['year'], $b['hour'], $b['minute']) );
531 $b['name'] = $lucifer[8];
535 // Replace symlinks formatted as "source -> target" with just the source name
536 if ( isset( $b['islink'] ) && $b['islink'] ) {
537 $b['name'] = preg_replace( '/(\s*->\s*.*)$/', '', $b['name'] );
546 * @param string $path
547 * @param bool $include_hidden
548 * @param bool $recursive
551 public function dirlist($path = '.', $include_hidden = true, $recursive = false) {
552 if ( $this->is_file($path) ) {
553 $limit_file = basename($path);
554 $path = dirname($path) . '/';
559 $pwd = @ftp_pwd($this->link);
560 if ( ! @ftp_chdir($this->link, $path) ) // Cant change to folder = folder doesn't exist
562 $list = @ftp_rawlist($this->link, '-a', false);
563 @ftp_chdir($this->link, $pwd);
565 if ( empty($list) ) // Empty array = non-existent folder (real folder will show . at least)
569 foreach ( $list as $k => $v ) {
570 $entry = $this->parselisting($v);
574 if ( '.' == $entry['name'] || '..' == $entry['name'] )
577 if ( ! $include_hidden && '.' == $entry['name'][0] )
580 if ( $limit_file && $entry['name'] != $limit_file)
583 $dirlist[ $entry['name'] ] = $entry;
587 foreach ( (array)$dirlist as $struc ) {
588 if ( 'd' == $struc['type'] ) {
590 $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $include_hidden, $recursive);
592 $struc['files'] = array();
595 $ret[ $struc['name'] ] = $struc;
603 public function __destruct() {
605 ftp_close($this->link);