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 {
24 public function __construct( $opt = '' ) {
25 $this->method = 'ftpext';
26 $this->errors = new WP_Error();
28 // Check if possible to use ftp functions.
29 if ( ! extension_loaded('ftp') ) {
30 $this->errors->add('no_ftp_ext', __('The ftp PHP extension is not available'));
34 // This Class uses the timeout on a per-connection basis, Others use it on a per-action basis.
36 if ( ! defined('FS_TIMEOUT') )
37 define('FS_TIMEOUT', 240);
39 if ( empty($opt['port']) )
40 $this->options['port'] = 21;
42 $this->options['port'] = $opt['port'];
44 if ( empty($opt['hostname']) )
45 $this->errors->add('empty_hostname', __('FTP hostname is required'));
47 $this->options['hostname'] = $opt['hostname'];
49 // Check if the options provided are OK.
50 if ( empty($opt['username']) )
51 $this->errors->add('empty_username', __('FTP username is required'));
53 $this->options['username'] = $opt['username'];
55 if ( empty($opt['password']) )
56 $this->errors->add('empty_password', __('FTP password is required'));
58 $this->options['password'] = $opt['password'];
60 $this->options['ssl'] = false;
61 if ( isset($opt['connection_type']) && 'ftps' == $opt['connection_type'] )
62 $this->options['ssl'] = true;
69 public function connect() {
70 if ( isset($this->options['ssl']) && $this->options['ssl'] && function_exists('ftp_ssl_connect') )
71 $this->link = @ftp_ssl_connect($this->options['hostname'], $this->options['port'], FS_CONNECT_TIMEOUT);
73 $this->link = @ftp_connect($this->options['hostname'], $this->options['port'], FS_CONNECT_TIMEOUT);
75 if ( ! $this->link ) {
76 $this->errors->add('connect', sprintf(__('Failed to connect to FTP Server %1$s:%2$s'), $this->options['hostname'], $this->options['port']));
80 if ( ! @ftp_login($this->link,$this->options['username'], $this->options['password']) ) {
81 $this->errors->add('auth', sprintf(__('Username/Password incorrect for %s'), $this->options['username']));
85 // Set the Connection to use Passive FTP
86 @ftp_pasv( $this->link, true );
87 if ( @ftp_get_option($this->link, FTP_TIMEOUT_SEC) < FS_TIMEOUT )
88 @ftp_set_option($this->link, FTP_TIMEOUT_SEC, FS_TIMEOUT);
95 * @return false|string
97 public function get_contents( $file ) {
98 $tempfile = wp_tempnam($file);
99 $temp = fopen($tempfile, 'w+');
104 if ( ! @ftp_fget($this->link, $temp, $file, FTP_BINARY ) )
107 fseek( $temp, 0 ); // Skip back to the start of the file being written to
110 while ( ! feof($temp) )
111 $contents .= fread($temp, 8192);
119 * @param string $file
122 public function get_contents_array($file) {
123 return explode("\n", $this->get_contents($file));
127 * @param string $file
128 * @param string $contents
129 * @param bool|int $mode
132 public function put_contents($file, $contents, $mode = false ) {
133 $tempfile = wp_tempnam($file);
134 $temp = fopen( $tempfile, 'wb+' );
138 mbstring_binary_safe_encoding();
140 $data_length = strlen( $contents );
141 $bytes_written = fwrite( $temp, $contents );
143 reset_mbstring_encoding();
145 if ( $data_length !== $bytes_written ) {
151 fseek( $temp, 0 ); // Skip back to the start of the file being written to
153 $ret = @ftp_fput( $this->link, $file, $temp, FTP_BINARY );
158 $this->chmod($file, $mode);
166 public function cwd() {
167 $cwd = @ftp_pwd($this->link);
169 $cwd = trailingslashit($cwd);
177 public function chdir($dir) {
178 return @ftp_chdir($this->link, $dir);
182 * @param string $file
184 * @param bool $recursive
187 public function chmod($file, $mode = false, $recursive = false) {
189 if ( $this->is_file($file) )
190 $mode = FS_CHMOD_FILE;
191 elseif ( $this->is_dir($file) )
192 $mode = FS_CHMOD_DIR;
197 // chmod any sub-objects if recursive.
198 if ( $recursive && $this->is_dir($file) ) {
199 $filelist = $this->dirlist($file);
200 foreach ( (array)$filelist as $filename => $filemeta )
201 $this->chmod($file . '/' . $filename, $mode, $recursive);
204 // chmod the file or directory
205 if ( ! function_exists('ftp_chmod') )
206 return (bool)@ftp_site($this->link, sprintf('CHMOD %o %s', $mode, $file));
207 return (bool)@ftp_chmod($this->link, $mode, $file);
211 * @param string $file
214 public function owner($file) {
215 $dir = $this->dirlist($file);
216 return $dir[$file]['owner'];
219 * @param string $file
222 public function getchmod($file) {
223 $dir = $this->dirlist($file);
224 return $dir[$file]['permsn'];
227 * @param string $file
230 public function group($file) {
231 $dir = $this->dirlist($file);
232 return $dir[$file]['group'];
237 * @param string $source
238 * @param string $destination
239 * @param bool $overwrite
240 * @param string|bool $mode
243 public function copy($source, $destination, $overwrite = false, $mode = false) {
244 if ( ! $overwrite && $this->exists($destination) )
246 $content = $this->get_contents($source);
247 if ( false === $content )
249 return $this->put_contents($destination, $content, $mode);
252 * @param string $source
253 * @param string $destination
254 * @param bool $overwrite
257 public function move($source, $destination, $overwrite = false) {
258 return ftp_rename($this->link, $source, $destination);
261 * @param string $file
262 * @param bool $recursive
263 * @param string $type
266 public function delete($file, $recursive = false, $type = false) {
269 if ( 'f' == $type || $this->is_file($file) )
270 return @ftp_delete($this->link, $file);
272 return @ftp_rmdir($this->link, $file);
274 $filelist = $this->dirlist( trailingslashit($file) );
275 if ( !empty($filelist) )
276 foreach ( $filelist as $delete_file )
277 $this->delete( trailingslashit($file) . $delete_file['name'], $recursive, $delete_file['type'] );
278 return @ftp_rmdir($this->link, $file);
281 * @param string $file
284 public function exists($file) {
285 $list = @ftp_nlist($this->link, $file);
287 if ( empty( $list ) && $this->is_dir( $file ) ) {
288 return true; // File is an empty directory.
291 return !empty($list); //empty list = no file, so invert.
294 * @param string $file
297 public function is_file($file) {
298 return $this->exists($file) && !$this->is_dir($file);
301 * @param string $path
304 public function is_dir($path) {
306 $result = @ftp_chdir($this->link, trailingslashit($path) );
307 if ( $result && $path == $this->cwd() || $this->cwd() != $cwd ) {
308 @ftp_chdir($this->link, $cwd);
315 * @param string $file
318 public function is_readable($file) {
322 * @param string $file
325 public function is_writable($file) {
329 * @param string $file
332 public function atime($file) {
336 * @param string $file
339 public function mtime($file) {
340 return ftp_mdtm($this->link, $file);
343 * @param string $file
346 public function size($file) {
347 return ftp_size($this->link, $file);
350 * @param string $file
353 public function touch($file, $time = 0, $atime = 0) {
358 * @param string $path
359 * @param mixed $chmod
360 * @param mixed $chown
361 * @param mixed $chgrp
364 public function mkdir($path, $chmod = false, $chown = false, $chgrp = false) {
365 $path = untrailingslashit($path);
369 if ( !@ftp_mkdir($this->link, $path) )
371 $this->chmod($path, $chmod);
376 * @param string $path
377 * @param bool $recursive
380 public function rmdir($path, $recursive = false) {
381 return $this->delete($path, $recursive);
385 * @staticvar bool $is_windows
386 * @param string $line
389 public function parselisting($line) {
390 static $is_windows = null;
391 if ( is_null($is_windows) )
392 $is_windows = stripos( ftp_systype($this->link), 'win') !== false;
394 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) ) {
396 if ( $lucifer[3] < 70 )
399 $lucifer[3] += 1900; // 4digit year fix
400 $b['isdir'] = ( $lucifer[7] == '<DIR>');
405 $b['size'] = $lucifer[7];
406 $b['month'] = $lucifer[1];
407 $b['day'] = $lucifer[2];
408 $b['year'] = $lucifer[3];
409 $b['hour'] = $lucifer[4];
410 $b['minute'] = $lucifer[5];
411 $b['time'] = @mktime($lucifer[4] + (strcasecmp($lucifer[6], "PM") == 0 ? 12 : 0), $lucifer[5], 0, $lucifer[1], $lucifer[2], $lucifer[3]);
412 $b['am/pm'] = $lucifer[6];
413 $b['name'] = $lucifer[8];
414 } elseif ( !$is_windows && $lucifer = preg_split('/[ ]/', $line, 9, PREG_SPLIT_NO_EMPTY)) {
416 $lcount = count($lucifer);
420 $b['isdir'] = $lucifer[0]{0} === 'd';
421 $b['islink'] = $lucifer[0]{0} === 'l';
424 elseif ( $b['islink'] )
428 $b['perms'] = $lucifer[0];
429 $b['permsn'] = $this->getnumchmodfromh( $b['perms'] );
430 $b['number'] = $lucifer[1];
431 $b['owner'] = $lucifer[2];
432 $b['group'] = $lucifer[3];
433 $b['size'] = $lucifer[4];
434 if ( $lcount == 8 ) {
435 sscanf($lucifer[5], '%d-%d-%d', $b['year'], $b['month'], $b['day']);
436 sscanf($lucifer[6], '%d:%d', $b['hour'], $b['minute']);
437 $b['time'] = @mktime($b['hour'], $b['minute'], 0, $b['month'], $b['day'], $b['year']);
438 $b['name'] = $lucifer[7];
440 $b['month'] = $lucifer[5];
441 $b['day'] = $lucifer[6];
442 if ( preg_match('/([0-9]{2}):([0-9]{2})/', $lucifer[7], $l2) ) {
443 $b['year'] = date("Y");
445 $b['minute'] = $l2[2];
447 $b['year'] = $lucifer[7];
451 $b['time'] = strtotime( sprintf('%d %s %d %02d:%02d', $b['day'], $b['month'], $b['year'], $b['hour'], $b['minute']) );
452 $b['name'] = $lucifer[8];
456 // Replace symlinks formatted as "source -> target" with just the source name
458 $b['name'] = preg_replace( '/(\s*->\s*.*)$/', '', $b['name'] );
464 * @param string $path
465 * @param bool $include_hidden
466 * @param bool $recursive
469 public function dirlist($path = '.', $include_hidden = true, $recursive = false) {
470 if ( $this->is_file($path) ) {
471 $limit_file = basename($path);
472 $path = dirname($path) . '/';
477 $pwd = @ftp_pwd($this->link);
478 if ( ! @ftp_chdir($this->link, $path) ) // Cant change to folder = folder doesn't exist
480 $list = @ftp_rawlist($this->link, '-a', false);
481 @ftp_chdir($this->link, $pwd);
483 if ( empty($list) ) // Empty array = non-existent folder (real folder will show . at least)
487 foreach ( $list as $k => $v ) {
488 $entry = $this->parselisting($v);
492 if ( '.' == $entry['name'] || '..' == $entry['name'] )
495 if ( ! $include_hidden && '.' == $entry['name'][0] )
498 if ( $limit_file && $entry['name'] != $limit_file)
501 $dirlist[ $entry['name'] ] = $entry;
505 foreach ( (array)$dirlist as $struc ) {
506 if ( 'd' == $struc['type'] ) {
508 $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $include_hidden, $recursive);
510 $struc['files'] = array();
513 $ret[ $struc['name'] ] = $struc;
521 public function __destruct() {
523 ftp_close($this->link);