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);
90 public function get_contents( $file ) {
91 $tempfile = wp_tempnam($file);
92 $temp = fopen($tempfile, 'w+');
97 if ( ! @ftp_fget($this->link, $temp, $file, FTP_BINARY ) )
100 fseek( $temp, 0 ); // Skip back to the start of the file being written to
103 while ( ! feof($temp) )
104 $contents .= fread($temp, 8192);
111 public function get_contents_array($file) {
112 return explode("\n", $this->get_contents($file));
115 public function put_contents($file, $contents, $mode = false ) {
116 $tempfile = wp_tempnam($file);
117 $temp = fopen( $tempfile, 'wb+' );
121 mbstring_binary_safe_encoding();
123 $data_length = strlen( $contents );
124 $bytes_written = fwrite( $temp, $contents );
126 reset_mbstring_encoding();
128 if ( $data_length !== $bytes_written ) {
134 fseek( $temp, 0 ); // Skip back to the start of the file being written to
136 $ret = @ftp_fput( $this->link, $file, $temp, FTP_BINARY );
141 $this->chmod($file, $mode);
146 public function cwd() {
147 $cwd = @ftp_pwd($this->link);
149 $cwd = trailingslashit($cwd);
153 public function chdir($dir) {
154 return @ftp_chdir($this->link, $dir);
157 public function chgrp($file, $group, $recursive = false ) {
161 public function chmod($file, $mode = false, $recursive = false) {
163 if ( $this->is_file($file) )
164 $mode = FS_CHMOD_FILE;
165 elseif ( $this->is_dir($file) )
166 $mode = FS_CHMOD_DIR;
171 // chmod any sub-objects if recursive.
172 if ( $recursive && $this->is_dir($file) ) {
173 $filelist = $this->dirlist($file);
174 foreach ( (array)$filelist as $filename => $filemeta )
175 $this->chmod($file . '/' . $filename, $mode, $recursive);
178 // chmod the file or directory
179 if ( ! function_exists('ftp_chmod') )
180 return (bool)@ftp_site($this->link, sprintf('CHMOD %o %s', $mode, $file));
181 return (bool)@ftp_chmod($this->link, $mode, $file);
184 public function owner($file) {
185 $dir = $this->dirlist($file);
186 return $dir[$file]['owner'];
189 public function getchmod($file) {
190 $dir = $this->dirlist($file);
191 return $dir[$file]['permsn'];
194 public function group($file) {
195 $dir = $this->dirlist($file);
196 return $dir[$file]['group'];
199 public function copy($source, $destination, $overwrite = false, $mode = false) {
200 if ( ! $overwrite && $this->exists($destination) )
202 $content = $this->get_contents($source);
203 if ( false === $content )
205 return $this->put_contents($destination, $content, $mode);
208 public function move($source, $destination, $overwrite = false) {
209 return ftp_rename($this->link, $source, $destination);
212 public function delete($file, $recursive = false, $type = false) {
215 if ( 'f' == $type || $this->is_file($file) )
216 return @ftp_delete($this->link, $file);
218 return @ftp_rmdir($this->link, $file);
220 $filelist = $this->dirlist( trailingslashit($file) );
221 if ( !empty($filelist) )
222 foreach ( $filelist as $delete_file )
223 $this->delete( trailingslashit($file) . $delete_file['name'], $recursive, $delete_file['type'] );
224 return @ftp_rmdir($this->link, $file);
227 public function exists($file) {
228 $list = @ftp_nlist($this->link, $file);
229 return !empty($list); //empty list = no file, so invert.
232 public function is_file($file) {
233 return $this->exists($file) && !$this->is_dir($file);
236 public function is_dir($path) {
238 $result = @ftp_chdir($this->link, trailingslashit($path) );
239 if ( $result && $path == $this->cwd() || $this->cwd() != $cwd ) {
240 @ftp_chdir($this->link, $cwd);
246 public function is_readable($file) {
250 public function is_writable($file) {
254 public function atime($file) {
258 public function mtime($file) {
259 return ftp_mdtm($this->link, $file);
262 public function size($file) {
263 return ftp_size($this->link, $file);
266 public function touch($file, $time = 0, $atime = 0) {
270 public function mkdir($path, $chmod = false, $chown = false, $chgrp = false) {
271 $path = untrailingslashit($path);
275 if ( !@ftp_mkdir($this->link, $path) )
277 $this->chmod($path, $chmod);
279 $this->chown($path, $chown);
281 $this->chgrp($path, $chgrp);
285 public function rmdir($path, $recursive = false) {
286 return $this->delete($path, $recursive);
289 public function parselisting($line) {
291 if ( is_null($is_windows) )
292 $is_windows = stripos( ftp_systype($this->link), 'win') !== false;
294 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) ) {
296 if ( $lucifer[3] < 70 )
299 $lucifer[3] += 1900; // 4digit year fix
300 $b['isdir'] = ( $lucifer[7] == '<DIR>');
305 $b['size'] = $lucifer[7];
306 $b['month'] = $lucifer[1];
307 $b['day'] = $lucifer[2];
308 $b['year'] = $lucifer[3];
309 $b['hour'] = $lucifer[4];
310 $b['minute'] = $lucifer[5];
311 $b['time'] = @mktime($lucifer[4] + (strcasecmp($lucifer[6], "PM") == 0 ? 12 : 0), $lucifer[5], 0, $lucifer[1], $lucifer[2], $lucifer[3]);
312 $b['am/pm'] = $lucifer[6];
313 $b['name'] = $lucifer[8];
314 } elseif ( !$is_windows && $lucifer = preg_split('/[ ]/', $line, 9, PREG_SPLIT_NO_EMPTY)) {
316 $lcount = count($lucifer);
320 $b['isdir'] = $lucifer[0]{0} === 'd';
321 $b['islink'] = $lucifer[0]{0} === 'l';
324 elseif ( $b['islink'] )
328 $b['perms'] = $lucifer[0];
329 $b['number'] = $lucifer[1];
330 $b['owner'] = $lucifer[2];
331 $b['group'] = $lucifer[3];
332 $b['size'] = $lucifer[4];
333 if ( $lcount == 8 ) {
334 sscanf($lucifer[5], '%d-%d-%d', $b['year'], $b['month'], $b['day']);
335 sscanf($lucifer[6], '%d:%d', $b['hour'], $b['minute']);
336 $b['time'] = @mktime($b['hour'], $b['minute'], 0, $b['month'], $b['day'], $b['year']);
337 $b['name'] = $lucifer[7];
339 $b['month'] = $lucifer[5];
340 $b['day'] = $lucifer[6];
341 if ( preg_match('/([0-9]{2}):([0-9]{2})/', $lucifer[7], $l2) ) {
342 $b['year'] = date("Y");
344 $b['minute'] = $l2[2];
346 $b['year'] = $lucifer[7];
350 $b['time'] = strtotime( sprintf('%d %s %d %02d:%02d', $b['day'], $b['month'], $b['year'], $b['hour'], $b['minute']) );
351 $b['name'] = $lucifer[8];
355 // Replace symlinks formatted as "source -> target" with just the source name
357 $b['name'] = preg_replace( '/(\s*->\s*.*)$/', '', $b['name'] );
362 public function dirlist($path = '.', $include_hidden = true, $recursive = false) {
363 if ( $this->is_file($path) ) {
364 $limit_file = basename($path);
365 $path = dirname($path) . '/';
370 $pwd = @ftp_pwd($this->link);
371 if ( ! @ftp_chdir($this->link, $path) ) // Cant change to folder = folder doesn't exist
373 $list = @ftp_rawlist($this->link, '-a', false);
374 @ftp_chdir($this->link, $pwd);
376 if ( empty($list) ) // Empty array = non-existent folder (real folder will show . at least)
380 foreach ( $list as $k => $v ) {
381 $entry = $this->parselisting($v);
385 if ( '.' == $entry['name'] || '..' == $entry['name'] )
388 if ( ! $include_hidden && '.' == $entry['name'][0] )
391 if ( $limit_file && $entry['name'] != $limit_file)
394 $dirlist[ $entry['name'] ] = $entry;
398 foreach ( (array)$dirlist as $struc ) {
399 if ( 'd' == $struc['type'] ) {
401 $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $include_hidden, $recursive);
403 $struc['files'] = array();
406 $ret[ $struc['name'] ] = $struc;
411 public function __destruct() {
413 ftp_close($this->link);