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 {
20 public function __construct($opt='') {
21 $this->method = 'ftpext';
22 $this->errors = new WP_Error();
24 // Check if possible to use ftp functions.
25 if ( ! extension_loaded('ftp') ) {
26 $this->errors->add('no_ftp_ext', __('The ftp PHP extension is not available'));
30 // This Class uses the timeout on a per-connection basis, Others use it on a per-action basis.
32 if ( ! defined('FS_TIMEOUT') )
33 define('FS_TIMEOUT', 240);
35 if ( empty($opt['port']) )
36 $this->options['port'] = 21;
38 $this->options['port'] = $opt['port'];
40 if ( empty($opt['hostname']) )
41 $this->errors->add('empty_hostname', __('FTP hostname is required'));
43 $this->options['hostname'] = $opt['hostname'];
45 // Check if the options provided are OK.
46 if ( empty($opt['username']) )
47 $this->errors->add('empty_username', __('FTP username is required'));
49 $this->options['username'] = $opt['username'];
51 if ( empty($opt['password']) )
52 $this->errors->add('empty_password', __('FTP password is required'));
54 $this->options['password'] = $opt['password'];
56 $this->options['ssl'] = false;
57 if ( isset($opt['connection_type']) && 'ftps' == $opt['connection_type'] )
58 $this->options['ssl'] = true;
61 public function connect() {
62 if ( isset($this->options['ssl']) && $this->options['ssl'] && function_exists('ftp_ssl_connect') )
63 $this->link = @ftp_ssl_connect($this->options['hostname'], $this->options['port'], FS_CONNECT_TIMEOUT);
65 $this->link = @ftp_connect($this->options['hostname'], $this->options['port'], FS_CONNECT_TIMEOUT);
67 if ( ! $this->link ) {
68 $this->errors->add('connect', sprintf(__('Failed to connect to FTP Server %1$s:%2$s'), $this->options['hostname'], $this->options['port']));
72 if ( ! @ftp_login($this->link,$this->options['username'], $this->options['password']) ) {
73 $this->errors->add('auth', sprintf(__('Username/Password incorrect for %s'), $this->options['username']));
77 // Set the Connection to use Passive FTP
78 @ftp_pasv( $this->link, true );
79 if ( @ftp_get_option($this->link, FTP_TIMEOUT_SEC) < FS_TIMEOUT )
80 @ftp_set_option($this->link, FTP_TIMEOUT_SEC, FS_TIMEOUT);
87 * @return false|string
89 public function get_contents( $file ) {
90 $tempfile = wp_tempnam($file);
91 $temp = fopen($tempfile, 'w+');
96 if ( ! @ftp_fget($this->link, $temp, $file, FTP_BINARY ) )
99 fseek( $temp, 0 ); // Skip back to the start of the file being written to
102 while ( ! feof($temp) )
103 $contents .= fread($temp, 8192);
111 * @param string $file
114 public function get_contents_array($file) {
115 return explode("\n", $this->get_contents($file));
119 * @param string $file
120 * @param string $contents
121 * @param bool|int $mode
124 public function put_contents($file, $contents, $mode = false ) {
125 $tempfile = wp_tempnam($file);
126 $temp = fopen( $tempfile, 'wb+' );
130 mbstring_binary_safe_encoding();
132 $data_length = strlen( $contents );
133 $bytes_written = fwrite( $temp, $contents );
135 reset_mbstring_encoding();
137 if ( $data_length !== $bytes_written ) {
143 fseek( $temp, 0 ); // Skip back to the start of the file being written to
145 $ret = @ftp_fput( $this->link, $file, $temp, FTP_BINARY );
150 $this->chmod($file, $mode);
158 public function cwd() {
159 $cwd = @ftp_pwd($this->link);
161 $cwd = trailingslashit($cwd);
169 public function chdir($dir) {
170 return @ftp_chdir($this->link, $dir);
174 * @param string $file
176 * @param bool $recursive
179 public function chmod($file, $mode = false, $recursive = false) {
181 if ( $this->is_file($file) )
182 $mode = FS_CHMOD_FILE;
183 elseif ( $this->is_dir($file) )
184 $mode = FS_CHMOD_DIR;
189 // chmod any sub-objects if recursive.
190 if ( $recursive && $this->is_dir($file) ) {
191 $filelist = $this->dirlist($file);
192 foreach ( (array)$filelist as $filename => $filemeta )
193 $this->chmod($file . '/' . $filename, $mode, $recursive);
196 // chmod the file or directory
197 if ( ! function_exists('ftp_chmod') )
198 return (bool)@ftp_site($this->link, sprintf('CHMOD %o %s', $mode, $file));
199 return (bool)@ftp_chmod($this->link, $mode, $file);
203 * @param string $file
206 public function owner($file) {
207 $dir = $this->dirlist($file);
208 return $dir[$file]['owner'];
211 * @param string $file
214 public function getchmod($file) {
215 $dir = $this->dirlist($file);
216 return $dir[$file]['permsn'];
219 * @param string $file
222 public function group($file) {
223 $dir = $this->dirlist($file);
224 return $dir[$file]['group'];
229 * @param string $source
230 * @param string $destination
231 * @param bool $overwrite
232 * @param string|bool $mode
235 public function copy($source, $destination, $overwrite = false, $mode = false) {
236 if ( ! $overwrite && $this->exists($destination) )
238 $content = $this->get_contents($source);
239 if ( false === $content )
241 return $this->put_contents($destination, $content, $mode);
244 * @param string $source
245 * @param string $destination
246 * @param bool $overwrite
249 public function move($source, $destination, $overwrite = false) {
250 return ftp_rename($this->link, $source, $destination);
253 * @param string $file
254 * @param bool $recursive
255 * @param string $type
258 public function delete($file, $recursive = false, $type = false) {
261 if ( 'f' == $type || $this->is_file($file) )
262 return @ftp_delete($this->link, $file);
264 return @ftp_rmdir($this->link, $file);
266 $filelist = $this->dirlist( trailingslashit($file) );
267 if ( !empty($filelist) )
268 foreach ( $filelist as $delete_file )
269 $this->delete( trailingslashit($file) . $delete_file['name'], $recursive, $delete_file['type'] );
270 return @ftp_rmdir($this->link, $file);
273 * @param string $file
276 public function exists($file) {
277 $list = @ftp_nlist($this->link, $file);
279 if ( empty( $list ) && $this->is_dir( $file ) ) {
280 return true; // File is an empty directory.
283 return !empty($list); //empty list = no file, so invert.
286 * @param string $file
289 public function is_file($file) {
290 return $this->exists($file) && !$this->is_dir($file);
293 * @param string $path
296 public function is_dir($path) {
298 $result = @ftp_chdir($this->link, trailingslashit($path) );
299 if ( $result && $path == $this->cwd() || $this->cwd() != $cwd ) {
300 @ftp_chdir($this->link, $cwd);
307 * @param string $file
310 public function is_readable($file) {
314 * @param string $file
317 public function is_writable($file) {
321 * @param string $file
324 public function atime($file) {
328 * @param string $file
331 public function mtime($file) {
332 return ftp_mdtm($this->link, $file);
335 * @param string $file
338 public function size($file) {
339 return ftp_size($this->link, $file);
342 * @param string $file
345 public function touch($file, $time = 0, $atime = 0) {
350 * @param string $path
351 * @param mixed $chmod
352 * @param mixed $chown
353 * @param mixed $chgrp
356 public function mkdir($path, $chmod = false, $chown = false, $chgrp = false) {
357 $path = untrailingslashit($path);
361 if ( !@ftp_mkdir($this->link, $path) )
363 $this->chmod($path, $chmod);
368 * @param string $path
369 * @param bool $recursive
372 public function rmdir($path, $recursive = false) {
373 return $this->delete($path, $recursive);
377 * @staticvar bool $is_windows
378 * @param string $line
381 public function parselisting($line) {
383 if ( is_null($is_windows) )
384 $is_windows = stripos( ftp_systype($this->link), 'win') !== false;
386 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) ) {
388 if ( $lucifer[3] < 70 )
391 $lucifer[3] += 1900; // 4digit year fix
392 $b['isdir'] = ( $lucifer[7] == '<DIR>');
397 $b['size'] = $lucifer[7];
398 $b['month'] = $lucifer[1];
399 $b['day'] = $lucifer[2];
400 $b['year'] = $lucifer[3];
401 $b['hour'] = $lucifer[4];
402 $b['minute'] = $lucifer[5];
403 $b['time'] = @mktime($lucifer[4] + (strcasecmp($lucifer[6], "PM") == 0 ? 12 : 0), $lucifer[5], 0, $lucifer[1], $lucifer[2], $lucifer[3]);
404 $b['am/pm'] = $lucifer[6];
405 $b['name'] = $lucifer[8];
406 } elseif ( !$is_windows && $lucifer = preg_split('/[ ]/', $line, 9, PREG_SPLIT_NO_EMPTY)) {
408 $lcount = count($lucifer);
412 $b['isdir'] = $lucifer[0]{0} === 'd';
413 $b['islink'] = $lucifer[0]{0} === 'l';
416 elseif ( $b['islink'] )
420 $b['perms'] = $lucifer[0];
421 $b['number'] = $lucifer[1];
422 $b['owner'] = $lucifer[2];
423 $b['group'] = $lucifer[3];
424 $b['size'] = $lucifer[4];
425 if ( $lcount == 8 ) {
426 sscanf($lucifer[5], '%d-%d-%d', $b['year'], $b['month'], $b['day']);
427 sscanf($lucifer[6], '%d:%d', $b['hour'], $b['minute']);
428 $b['time'] = @mktime($b['hour'], $b['minute'], 0, $b['month'], $b['day'], $b['year']);
429 $b['name'] = $lucifer[7];
431 $b['month'] = $lucifer[5];
432 $b['day'] = $lucifer[6];
433 if ( preg_match('/([0-9]{2}):([0-9]{2})/', $lucifer[7], $l2) ) {
434 $b['year'] = date("Y");
436 $b['minute'] = $l2[2];
438 $b['year'] = $lucifer[7];
442 $b['time'] = strtotime( sprintf('%d %s %d %02d:%02d', $b['day'], $b['month'], $b['year'], $b['hour'], $b['minute']) );
443 $b['name'] = $lucifer[8];
447 // Replace symlinks formatted as "source -> target" with just the source name
449 $b['name'] = preg_replace( '/(\s*->\s*.*)$/', '', $b['name'] );
455 * @param string $path
456 * @param bool $include_hidden
457 * @param bool $recursive
460 public function dirlist($path = '.', $include_hidden = true, $recursive = false) {
461 if ( $this->is_file($path) ) {
462 $limit_file = basename($path);
463 $path = dirname($path) . '/';
468 $pwd = @ftp_pwd($this->link);
469 if ( ! @ftp_chdir($this->link, $path) ) // Cant change to folder = folder doesn't exist
471 $list = @ftp_rawlist($this->link, '-a', false);
472 @ftp_chdir($this->link, $pwd);
474 if ( empty($list) ) // Empty array = non-existent folder (real folder will show . at least)
478 foreach ( $list as $k => $v ) {
479 $entry = $this->parselisting($v);
483 if ( '.' == $entry['name'] || '..' == $entry['name'] )
486 if ( ! $include_hidden && '.' == $entry['name'][0] )
489 if ( $limit_file && $entry['name'] != $limit_file)
492 $dirlist[ $entry['name'] ] = $entry;
496 foreach ( (array)$dirlist as $struc ) {
497 if ( 'd' == $struc['type'] ) {
499 $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $include_hidden, $recursive);
501 $struc['files'] = array();
504 $ret[ $struc['name'] ] = $struc;
509 public function __destruct() {
511 ftp_close($this->link);