]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/includes/class-wp-filesystem-ftpext.php
WordPress 4.1.2-scripts
[autoinstalls/wordpress.git] / wp-admin / includes / class-wp-filesystem-ftpext.php
1 <?php
2 /**
3  * WordPress FTP Filesystem.
4  *
5  * @package WordPress
6  * @subpackage Filesystem
7  */
8
9 /**
10  * WordPress Filesystem Class for implementing FTP.
11  *
12  * @since 2.5.0
13  * @package WordPress
14  * @subpackage Filesystem
15  * @uses WP_Filesystem_Base Extends class
16  */
17 class WP_Filesystem_FTPext extends WP_Filesystem_Base {
18         public $link;
19         public $errors = null;
20         public $options = array();
21
22         public function __construct($opt='') {
23                 $this->method = 'ftpext';
24                 $this->errors = new WP_Error();
25
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'));
29                         return false;
30                 }
31
32                 // This Class uses the timeout on a per-connection basis, Others use it on a per-action basis.
33
34                 if ( ! defined('FS_TIMEOUT') )
35                         define('FS_TIMEOUT', 240);
36
37                 if ( empty($opt['port']) )
38                         $this->options['port'] = 21;
39                 else
40                         $this->options['port'] = $opt['port'];
41
42                 if ( empty($opt['hostname']) )
43                         $this->errors->add('empty_hostname', __('FTP hostname is required'));
44                 else
45                         $this->options['hostname'] = $opt['hostname'];
46
47                 if ( ! empty($opt['base']) )
48                         $this->wp_base = $opt['base'];
49
50                 // Check if the options provided are OK.
51                 if ( empty($opt['username']) )
52                         $this->errors->add('empty_username', __('FTP username is required'));
53                 else
54                         $this->options['username'] = $opt['username'];
55
56                 if ( empty($opt['password']) )
57                         $this->errors->add('empty_password', __('FTP password is required'));
58                 else
59                         $this->options['password'] = $opt['password'];
60
61                 $this->options['ssl'] = false;
62                 if ( isset($opt['connection_type']) && 'ftps' == $opt['connection_type'] )
63                         $this->options['ssl'] = true;
64         }
65
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);
69                 else
70                         $this->link = @ftp_connect($this->options['hostname'], $this->options['port'], FS_CONNECT_TIMEOUT);
71
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']));
74                         return false;
75                 }
76
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']));
79                         return false;
80                 }
81
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);
86
87                 return true;
88         }
89
90         /**
91          * @param string $file
92          * @return bool|string
93          */
94         public function get_contents( $file ) {
95                 $tempfile = wp_tempnam($file);
96                 $temp = fopen($tempfile, 'w+');
97
98                 if ( ! $temp )
99                         return false;
100
101                 if ( ! @ftp_fget($this->link, $temp, $file, FTP_BINARY ) )
102                         return false;
103
104                 fseek( $temp, 0 ); // Skip back to the start of the file being written to
105                 $contents = '';
106
107                 while ( ! feof($temp) )
108                         $contents .= fread($temp, 8192);
109
110                 fclose($temp);
111                 unlink($tempfile);
112                 return $contents;
113         }
114
115         /**
116          * @param string $file
117          * @return array
118          */
119         public function get_contents_array($file) {
120                 return explode("\n", $this->get_contents($file));
121         }
122
123         /**
124          * @param string $file
125          * @param string $contents
126          * @param bool|int $mode
127          * @return bool
128          */
129         public function put_contents($file, $contents, $mode = false ) {
130                 $tempfile = wp_tempnam($file);
131                 $temp = fopen( $tempfile, 'wb+' );
132                 if ( ! $temp )
133                         return false;
134
135                 mbstring_binary_safe_encoding();
136
137                 $data_length = strlen( $contents );
138                 $bytes_written = fwrite( $temp, $contents );
139
140                 reset_mbstring_encoding();
141
142                 if ( $data_length !== $bytes_written ) {
143                         fclose( $temp );
144                         unlink( $tempfile );
145                         return false;
146                 }
147
148                 fseek( $temp, 0 ); // Skip back to the start of the file being written to
149
150                 $ret = @ftp_fput( $this->link, $file, $temp, FTP_BINARY );
151
152                 fclose($temp);
153                 unlink($tempfile);
154
155                 $this->chmod($file, $mode);
156
157                 return $ret;
158         }
159
160         /**
161          * @return string
162          */
163         public function cwd() {
164                 $cwd = @ftp_pwd($this->link);
165                 if ( $cwd )
166                         $cwd = trailingslashit($cwd);
167                 return $cwd;
168         }
169
170         /**
171          * @param string $dir
172          * @return bool
173          */
174         public function chdir($dir) {
175                 return @ftp_chdir($this->link, $dir);
176         }
177
178         /**
179          * @param string $file
180          * @param bool $group
181          * @param bool $recursive
182          */
183         public function chgrp($file, $group, $recursive = false ) {
184                 return false;
185         }
186
187         /**
188          * @param string $file
189          * @param int $mode
190          * @param bool $recursive
191          * @return bool
192          */
193         public function chmod($file, $mode = false, $recursive = false) {
194                 if ( ! $mode ) {
195                         if ( $this->is_file($file) )
196                                 $mode = FS_CHMOD_FILE;
197                         elseif ( $this->is_dir($file) )
198                                 $mode = FS_CHMOD_DIR;
199                         else
200                                 return false;
201                 }
202
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);
208                 }
209
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);
214         }
215
216         /**
217          * @param string $file
218          * @return string
219          */
220         public function owner($file) {
221                 $dir = $this->dirlist($file);
222                 return $dir[$file]['owner'];
223         }
224         /**
225          * @param string $file
226          * @return string
227          */
228         public function getchmod($file) {
229                 $dir = $this->dirlist($file);
230                 return $dir[$file]['permsn'];
231         }
232         /**
233          * @param string $file
234          * @return string
235          */
236         public function group($file) {
237                 $dir = $this->dirlist($file);
238                 return $dir[$file]['group'];
239         }
240
241         /**
242          *
243          * @param string $source
244          * @param string $destination
245          * @param bool   $overwrite
246          * @param string|bool $mode
247          * @return bool
248          */
249         public function copy($source, $destination, $overwrite = false, $mode = false) {
250                 if ( ! $overwrite && $this->exists($destination) )
251                         return false;
252                 $content = $this->get_contents($source);
253                 if ( false === $content )
254                         return false;
255                 return $this->put_contents($destination, $content, $mode);
256         }
257         /**
258          * @param string $source
259          * @param string $destination
260          * @param bool $overwrite
261          * @return bool
262          */
263         public function move($source, $destination, $overwrite = false) {
264                 return ftp_rename($this->link, $source, $destination);
265         }
266         /**
267          * @param string $file
268          * @param bool $recursive
269          * @param string $type
270          * @return bool
271          */
272         public function delete($file, $recursive = false, $type = false) {
273                 if ( empty($file) )
274                         return false;
275                 if ( 'f' == $type || $this->is_file($file) )
276                         return @ftp_delete($this->link, $file);
277                 if ( !$recursive )
278                         return @ftp_rmdir($this->link, $file);
279
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);
285         }
286         /**
287          * @param string $file
288          * @return bool
289          */
290         public function exists($file) {
291                 $list = @ftp_nlist($this->link, $file);
292                 return !empty($list); //empty list = no file, so invert.
293         }
294         /**
295          * @param string $file
296          * @return bool
297          */
298         public function is_file($file) {
299                 return $this->exists($file) && !$this->is_dir($file);
300         }
301         /**
302          * @param string $path
303          * @return bool
304          */
305         public function is_dir($path) {
306                 $cwd = $this->cwd();
307                 $result = @ftp_chdir($this->link, trailingslashit($path) );
308                 if ( $result && $path == $this->cwd() || $this->cwd() != $cwd ) {
309                         @ftp_chdir($this->link, $cwd);
310                         return true;
311                 }
312                 return false;
313         }
314
315         /**
316          * @param string $file
317          * @return bool
318          */
319         public function is_readable($file) {
320                 return true;
321         }
322         /**
323          * @param string $file
324          * @return bool
325          */
326         public function is_writable($file) {
327                 return true;
328         }
329         /**
330          * @param string $file
331          * @return bool
332          */
333         public function atime($file) {
334                 return false;
335         }
336         /**
337          * @param string $file
338          * @return int
339          */
340         public function mtime($file) {
341                 return ftp_mdtm($this->link, $file);
342         }
343         /**
344          * @param string $file
345          * @return int
346          */
347         public function size($file) {
348                 return ftp_size($this->link, $file);
349         }
350         /**
351          * @param string $file
352          * @return bool
353          */
354         public function touch($file, $time = 0, $atime = 0) {
355                 return false;
356         }
357
358         /**
359          * @param string $path
360          * @param mixed $chmod
361          * @param mixed $chown
362          * @param mixed $chgrp
363          * @return bool
364          */
365         public function mkdir($path, $chmod = false, $chown = false, $chgrp = false) {
366                 $path = untrailingslashit($path);
367                 if ( empty($path) )
368                         return false;
369
370                 if ( !@ftp_mkdir($this->link, $path) )
371                         return false;
372                 $this->chmod($path, $chmod);
373                 if ( $chown )
374                         $this->chown($path, $chown);
375                 if ( $chgrp )
376                         $this->chgrp($path, $chgrp);
377                 return true;
378         }
379
380         /**
381          * @param string $path
382          * @param bool $recursive
383          * @return bool
384          */
385         public function rmdir($path, $recursive = false) {
386                 return $this->delete($path, $recursive);
387         }
388
389         /**
390          * @staticvar bool $is_windows
391          * @param string $line
392          * @return string
393          */
394         public function parselisting($line) {
395                 static $is_windows;
396                 if ( is_null($is_windows) )
397                         $is_windows = stripos( ftp_systype($this->link), 'win') !== false;
398
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) ) {
400                         $b = array();
401                         if ( $lucifer[3] < 70 )
402                                 $lucifer[3] +=2000;
403                         else
404                                 $lucifer[3] += 1900; // 4digit year fix
405                         $b['isdir'] = ( $lucifer[7] == '<DIR>');
406                         if ( $b['isdir'] )
407                                 $b['type'] = 'd';
408                         else
409                                 $b['type'] = 'f';
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)) {
420                         //echo $line."\n";
421                         $lcount = count($lucifer);
422                         if ( $lcount < 8 )
423                                 return '';
424                         $b = array();
425                         $b['isdir'] = $lucifer[0]{0} === 'd';
426                         $b['islink'] = $lucifer[0]{0} === 'l';
427                         if ( $b['isdir'] )
428                                 $b['type'] = 'd';
429                         elseif ( $b['islink'] )
430                                 $b['type'] = 'l';
431                         else
432                                 $b['type'] = 'f';
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];
443                         } else {
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");
448                                         $b['hour'] = $l2[1];
449                                         $b['minute'] = $l2[2];
450                                 } else {
451                                         $b['year'] = $lucifer[7];
452                                         $b['hour'] = 0;
453                                         $b['minute'] = 0;
454                                 }
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];
457                         }
458                 }
459
460                 // Replace symlinks formatted as "source -> target" with just the source name
461                 if ( $b['islink'] )
462                         $b['name'] = preg_replace( '/(\s*->\s*.*)$/', '', $b['name'] );
463
464                 return $b;
465         }
466
467         /**
468          * @param string $path
469          * @param bool $include_hidden
470          * @param bool $recursive
471          * @return bool|array
472          */
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) . '/';
477                 } else {
478                         $limit_file = false;
479                 }
480
481                 $pwd = @ftp_pwd($this->link);
482                 if ( ! @ftp_chdir($this->link, $path) ) // Cant change to folder = folder doesn't exist
483                         return false;
484                 $list = @ftp_rawlist($this->link, '-a', false);
485                 @ftp_chdir($this->link, $pwd);
486
487                 if ( empty($list) ) // Empty array = non-existent folder (real folder will show . at least)
488                         return false;
489
490                 $dirlist = array();
491                 foreach ( $list as $k => $v ) {
492                         $entry = $this->parselisting($v);
493                         if ( empty($entry) )
494                                 continue;
495
496                         if ( '.' == $entry['name'] || '..' == $entry['name'] )
497                                 continue;
498
499                         if ( ! $include_hidden && '.' == $entry['name'][0] )
500                                 continue;
501
502                         if ( $limit_file && $entry['name'] != $limit_file)
503                                 continue;
504
505                         $dirlist[ $entry['name'] ] = $entry;
506                 }
507
508                 $ret = array();
509                 foreach ( (array)$dirlist as $struc ) {
510                         if ( 'd' == $struc['type'] ) {
511                                 if ( $recursive )
512                                         $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $include_hidden, $recursive);
513                                 else
514                                         $struc['files'] = array();
515                         }
516
517                         $ret[ $struc['name'] ] = $struc;
518                 }
519                 return $ret;
520         }
521
522         public function __destruct() {
523                 if ( $this->link )
524                         ftp_close($this->link);
525         }
526 }