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 $path = dirname( $file );
331 $filename = basename( $file );
333 $file_list = @ftp_nlist( $this->link, '-a ' . $path );
335 $file_list = array_map( 'basename', $file_list );
338 return $file_list && in_array( $filename, $file_list );
344 * @param string $file
347 public function is_file($file) {
348 return $this->exists($file) && !$this->is_dir($file);
354 * @param string $path
357 public function is_dir($path) {
359 $result = @ftp_chdir($this->link, trailingslashit($path) );
360 if ( $result && $path == $this->cwd() || $this->cwd() != $cwd ) {
361 @ftp_chdir($this->link, $cwd);
370 * @param string $file
373 public function is_readable($file) {
380 * @param string $file
383 public function is_writable($file) {
390 * @param string $file
393 public function atime($file) {
400 * @param string $file
403 public function mtime($file) {
404 return ftp_mdtm($this->link, $file);
410 * @param string $file
413 public function size($file) {
414 return ftp_size($this->link, $file);
420 * @param string $file
423 public function touch($file, $time = 0, $atime = 0) {
430 * @param string $path
431 * @param mixed $chmod
432 * @param mixed $chown
433 * @param mixed $chgrp
436 public function mkdir($path, $chmod = false, $chown = false, $chgrp = false) {
437 $path = untrailingslashit($path);
441 if ( !@ftp_mkdir($this->link, $path) )
443 $this->chmod($path, $chmod);
450 * @param string $path
451 * @param bool $recursive
454 public function rmdir($path, $recursive = false) {
455 return $this->delete($path, $recursive);
461 * @staticvar bool $is_windows
462 * @param string $line
465 public function parselisting($line) {
466 static $is_windows = null;
467 if ( is_null($is_windows) )
468 $is_windows = stripos( ftp_systype($this->link), 'win') !== false;
470 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) ) {
472 if ( $lucifer[3] < 70 )
475 $lucifer[3] += 1900; // 4digit year fix
476 $b['isdir'] = ( $lucifer[7] == '<DIR>');
481 $b['size'] = $lucifer[7];
482 $b['month'] = $lucifer[1];
483 $b['day'] = $lucifer[2];
484 $b['year'] = $lucifer[3];
485 $b['hour'] = $lucifer[4];
486 $b['minute'] = $lucifer[5];
487 $b['time'] = @mktime($lucifer[4] + (strcasecmp($lucifer[6], "PM") == 0 ? 12 : 0), $lucifer[5], 0, $lucifer[1], $lucifer[2], $lucifer[3]);
488 $b['am/pm'] = $lucifer[6];
489 $b['name'] = $lucifer[8];
490 } elseif ( !$is_windows && $lucifer = preg_split('/[ ]/', $line, 9, PREG_SPLIT_NO_EMPTY)) {
492 $lcount = count($lucifer);
496 $b['isdir'] = $lucifer[0]{0} === 'd';
497 $b['islink'] = $lucifer[0]{0} === 'l';
500 elseif ( $b['islink'] )
504 $b['perms'] = $lucifer[0];
505 $b['permsn'] = $this->getnumchmodfromh( $b['perms'] );
506 $b['number'] = $lucifer[1];
507 $b['owner'] = $lucifer[2];
508 $b['group'] = $lucifer[3];
509 $b['size'] = $lucifer[4];
510 if ( $lcount == 8 ) {
511 sscanf($lucifer[5], '%d-%d-%d', $b['year'], $b['month'], $b['day']);
512 sscanf($lucifer[6], '%d:%d', $b['hour'], $b['minute']);
513 $b['time'] = @mktime($b['hour'], $b['minute'], 0, $b['month'], $b['day'], $b['year']);
514 $b['name'] = $lucifer[7];
516 $b['month'] = $lucifer[5];
517 $b['day'] = $lucifer[6];
518 if ( preg_match('/([0-9]{2}):([0-9]{2})/', $lucifer[7], $l2) ) {
519 $b['year'] = date("Y");
521 $b['minute'] = $l2[2];
523 $b['year'] = $lucifer[7];
527 $b['time'] = strtotime( sprintf('%d %s %d %02d:%02d', $b['day'], $b['month'], $b['year'], $b['hour'], $b['minute']) );
528 $b['name'] = $lucifer[8];
532 // Replace symlinks formatted as "source -> target" with just the source name
533 if ( isset( $b['islink'] ) && $b['islink'] ) {
534 $b['name'] = preg_replace( '/(\s*->\s*.*)$/', '', $b['name'] );
543 * @param string $path
544 * @param bool $include_hidden
545 * @param bool $recursive
548 public function dirlist($path = '.', $include_hidden = true, $recursive = false) {
549 if ( $this->is_file($path) ) {
550 $limit_file = basename($path);
551 $path = dirname($path) . '/';
556 $pwd = @ftp_pwd($this->link);
557 if ( ! @ftp_chdir($this->link, $path) ) // Cant change to folder = folder doesn't exist
559 $list = @ftp_rawlist($this->link, '-a', false);
560 @ftp_chdir($this->link, $pwd);
562 if ( empty($list) ) // Empty array = non-existent folder (real folder will show . at least)
566 foreach ( $list as $k => $v ) {
567 $entry = $this->parselisting($v);
571 if ( '.' == $entry['name'] || '..' == $entry['name'] )
574 if ( ! $include_hidden && '.' == $entry['name'][0] )
577 if ( $limit_file && $entry['name'] != $limit_file)
580 $dirlist[ $entry['name'] ] = $entry;
584 foreach ( (array)$dirlist as $struc ) {
585 if ( 'd' == $struc['type'] ) {
587 $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $include_hidden, $recursive);
589 $struc['files'] = array();
592 $ret[ $struc['name'] ] = $struc;
600 public function __destruct() {
602 ftp_close($this->link);