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 {
19 public $errors = null;
20 public $options = array();
22 public function __construct($opt='') {
23 $this->method = 'ftpext';
24 $this->errors = new WP_Error();
26 // Check if possible to use ftp functions.
27 if ( ! extension_loaded('ftp') ) {
28 $this->errors->add('no_ftp_ext', __('The ftp PHP extension is not available'));
32 // This Class uses the timeout on a per-connection basis, Others use it on a per-action basis.
34 if ( ! defined('FS_TIMEOUT') )
35 define('FS_TIMEOUT', 240);
37 if ( empty($opt['port']) )
38 $this->options['port'] = 21;
40 $this->options['port'] = $opt['port'];
42 if ( empty($opt['hostname']) )
43 $this->errors->add('empty_hostname', __('FTP hostname is required'));
45 $this->options['hostname'] = $opt['hostname'];
47 if ( ! empty($opt['base']) )
48 $this->wp_base = $opt['base'];
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;
66 public function connect() {
67 if ( isset($this->options['ssl']) && $this->options['ssl'] && function_exists('ftp_ssl_connect') )
68 $this->link = @ftp_ssl_connect($this->options['hostname'], $this->options['port'], FS_CONNECT_TIMEOUT);
70 $this->link = @ftp_connect($this->options['hostname'], $this->options['port'], FS_CONNECT_TIMEOUT);
72 if ( ! $this->link ) {
73 $this->errors->add('connect', sprintf(__('Failed to connect to FTP Server %1$s:%2$s'), $this->options['hostname'], $this->options['port']));
77 if ( ! @ftp_login($this->link,$this->options['username'], $this->options['password']) ) {
78 $this->errors->add('auth', sprintf(__('Username/Password incorrect for %s'), $this->options['username']));
82 // Set the Connection to use Passive FTP
83 @ftp_pasv( $this->link, true );
84 if ( @ftp_get_option($this->link, FTP_TIMEOUT_SEC) < FS_TIMEOUT )
85 @ftp_set_option($this->link, FTP_TIMEOUT_SEC, FS_TIMEOUT);
94 public function get_contents( $file ) {
95 $tempfile = wp_tempnam($file);
96 $temp = fopen($tempfile, 'w+');
101 if ( ! @ftp_fget($this->link, $temp, $file, FTP_BINARY ) )
104 fseek( $temp, 0 ); // Skip back to the start of the file being written to
107 while ( ! feof($temp) )
108 $contents .= fread($temp, 8192);
116 * @param string $file
119 public function get_contents_array($file) {
120 return explode("\n", $this->get_contents($file));
124 * @param string $file
125 * @param string $contents
126 * @param bool|int $mode
129 public function put_contents($file, $contents, $mode = false ) {
130 $tempfile = wp_tempnam($file);
131 $temp = fopen( $tempfile, 'wb+' );
135 mbstring_binary_safe_encoding();
137 $data_length = strlen( $contents );
138 $bytes_written = fwrite( $temp, $contents );
140 reset_mbstring_encoding();
142 if ( $data_length !== $bytes_written ) {
148 fseek( $temp, 0 ); // Skip back to the start of the file being written to
150 $ret = @ftp_fput( $this->link, $file, $temp, FTP_BINARY );
155 $this->chmod($file, $mode);
163 public function cwd() {
164 $cwd = @ftp_pwd($this->link);
166 $cwd = trailingslashit($cwd);
174 public function chdir($dir) {
175 return @ftp_chdir($this->link, $dir);
179 * @param string $file
181 * @param bool $recursive
183 public function chgrp($file, $group, $recursive = false ) {
188 * @param string $file
190 * @param bool $recursive
193 public function chmod($file, $mode = false, $recursive = false) {
195 if ( $this->is_file($file) )
196 $mode = FS_CHMOD_FILE;
197 elseif ( $this->is_dir($file) )
198 $mode = FS_CHMOD_DIR;
203 // chmod any sub-objects if recursive.
204 if ( $recursive && $this->is_dir($file) ) {
205 $filelist = $this->dirlist($file);
206 foreach ( (array)$filelist as $filename => $filemeta )
207 $this->chmod($file . '/' . $filename, $mode, $recursive);
210 // chmod the file or directory
211 if ( ! function_exists('ftp_chmod') )
212 return (bool)@ftp_site($this->link, sprintf('CHMOD %o %s', $mode, $file));
213 return (bool)@ftp_chmod($this->link, $mode, $file);
217 * @param string $file
220 public function owner($file) {
221 $dir = $this->dirlist($file);
222 return $dir[$file]['owner'];
225 * @param string $file
228 public function getchmod($file) {
229 $dir = $this->dirlist($file);
230 return $dir[$file]['permsn'];
233 * @param string $file
236 public function group($file) {
237 $dir = $this->dirlist($file);
238 return $dir[$file]['group'];
243 * @param string $source
244 * @param string $destination
245 * @param bool $overwrite
246 * @param string|bool $mode
249 public function copy($source, $destination, $overwrite = false, $mode = false) {
250 if ( ! $overwrite && $this->exists($destination) )
252 $content = $this->get_contents($source);
253 if ( false === $content )
255 return $this->put_contents($destination, $content, $mode);
258 * @param string $source
259 * @param string $destination
260 * @param bool $overwrite
263 public function move($source, $destination, $overwrite = false) {
264 return ftp_rename($this->link, $source, $destination);
267 * @param string $file
268 * @param bool $recursive
269 * @param string $type
272 public function delete($file, $recursive = false, $type = false) {
275 if ( 'f' == $type || $this->is_file($file) )
276 return @ftp_delete($this->link, $file);
278 return @ftp_rmdir($this->link, $file);
280 $filelist = $this->dirlist( trailingslashit($file) );
281 if ( !empty($filelist) )
282 foreach ( $filelist as $delete_file )
283 $this->delete( trailingslashit($file) . $delete_file['name'], $recursive, $delete_file['type'] );
284 return @ftp_rmdir($this->link, $file);
287 * @param string $file
290 public function exists($file) {
291 $list = @ftp_nlist($this->link, $file);
292 return !empty($list); //empty list = no file, so invert.
295 * @param string $file
298 public function is_file($file) {
299 return $this->exists($file) && !$this->is_dir($file);
302 * @param string $path
305 public function is_dir($path) {
307 $result = @ftp_chdir($this->link, trailingslashit($path) );
308 if ( $result && $path == $this->cwd() || $this->cwd() != $cwd ) {
309 @ftp_chdir($this->link, $cwd);
316 * @param string $file
319 public function is_readable($file) {
323 * @param string $file
326 public function is_writable($file) {
330 * @param string $file
333 public function atime($file) {
337 * @param string $file
340 public function mtime($file) {
341 return ftp_mdtm($this->link, $file);
344 * @param string $file
347 public function size($file) {
348 return ftp_size($this->link, $file);
351 * @param string $file
354 public function touch($file, $time = 0, $atime = 0) {
359 * @param string $path
360 * @param mixed $chmod
361 * @param mixed $chown
362 * @param mixed $chgrp
365 public function mkdir($path, $chmod = false, $chown = false, $chgrp = false) {
366 $path = untrailingslashit($path);
370 if ( !@ftp_mkdir($this->link, $path) )
372 $this->chmod($path, $chmod);
374 $this->chown($path, $chown);
376 $this->chgrp($path, $chgrp);
381 * @param string $path
382 * @param bool $recursive
385 public function rmdir($path, $recursive = false) {
386 return $this->delete($path, $recursive);
390 * @staticvar bool $is_windows
391 * @param string $line
394 public function parselisting($line) {
396 if ( is_null($is_windows) )
397 $is_windows = stripos( ftp_systype($this->link), 'win') !== false;
399 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) ) {
401 if ( $lucifer[3] < 70 )
404 $lucifer[3] += 1900; // 4digit year fix
405 $b['isdir'] = ( $lucifer[7] == '<DIR>');
410 $b['size'] = $lucifer[7];
411 $b['month'] = $lucifer[1];
412 $b['day'] = $lucifer[2];
413 $b['year'] = $lucifer[3];
414 $b['hour'] = $lucifer[4];
415 $b['minute'] = $lucifer[5];
416 $b['time'] = @mktime($lucifer[4] + (strcasecmp($lucifer[6], "PM") == 0 ? 12 : 0), $lucifer[5], 0, $lucifer[1], $lucifer[2], $lucifer[3]);
417 $b['am/pm'] = $lucifer[6];
418 $b['name'] = $lucifer[8];
419 } elseif ( !$is_windows && $lucifer = preg_split('/[ ]/', $line, 9, PREG_SPLIT_NO_EMPTY)) {
421 $lcount = count($lucifer);
425 $b['isdir'] = $lucifer[0]{0} === 'd';
426 $b['islink'] = $lucifer[0]{0} === 'l';
429 elseif ( $b['islink'] )
433 $b['perms'] = $lucifer[0];
434 $b['number'] = $lucifer[1];
435 $b['owner'] = $lucifer[2];
436 $b['group'] = $lucifer[3];
437 $b['size'] = $lucifer[4];
438 if ( $lcount == 8 ) {
439 sscanf($lucifer[5], '%d-%d-%d', $b['year'], $b['month'], $b['day']);
440 sscanf($lucifer[6], '%d:%d', $b['hour'], $b['minute']);
441 $b['time'] = @mktime($b['hour'], $b['minute'], 0, $b['month'], $b['day'], $b['year']);
442 $b['name'] = $lucifer[7];
444 $b['month'] = $lucifer[5];
445 $b['day'] = $lucifer[6];
446 if ( preg_match('/([0-9]{2}):([0-9]{2})/', $lucifer[7], $l2) ) {
447 $b['year'] = date("Y");
449 $b['minute'] = $l2[2];
451 $b['year'] = $lucifer[7];
455 $b['time'] = strtotime( sprintf('%d %s %d %02d:%02d', $b['day'], $b['month'], $b['year'], $b['hour'], $b['minute']) );
456 $b['name'] = $lucifer[8];
460 // Replace symlinks formatted as "source -> target" with just the source name
462 $b['name'] = preg_replace( '/(\s*->\s*.*)$/', '', $b['name'] );
468 * @param string $path
469 * @param bool $include_hidden
470 * @param bool $recursive
473 public function dirlist($path = '.', $include_hidden = true, $recursive = false) {
474 if ( $this->is_file($path) ) {
475 $limit_file = basename($path);
476 $path = dirname($path) . '/';
481 $pwd = @ftp_pwd($this->link);
482 if ( ! @ftp_chdir($this->link, $path) ) // Cant change to folder = folder doesn't exist
484 $list = @ftp_rawlist($this->link, '-a', false);
485 @ftp_chdir($this->link, $pwd);
487 if ( empty($list) ) // Empty array = non-existent folder (real folder will show . at least)
491 foreach ( $list as $k => $v ) {
492 $entry = $this->parselisting($v);
496 if ( '.' == $entry['name'] || '..' == $entry['name'] )
499 if ( ! $include_hidden && '.' == $entry['name'][0] )
502 if ( $limit_file && $entry['name'] != $limit_file)
505 $dirlist[ $entry['name'] ] = $entry;
509 foreach ( (array)$dirlist as $struc ) {
510 if ( 'd' == $struc['type'] ) {
512 $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $include_hidden, $recursive);
514 $struc['files'] = array();
517 $ret[ $struc['name'] ] = $struc;
522 public function __destruct() {
524 ftp_close($this->link);