WordPress 4.4.1
[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
20         /**
21          * @access public
22          *
23          * @param array $opt
24          */
25         public function __construct( $opt = '' ) {
26                 $this->method = 'ftpext';
27                 $this->errors = new WP_Error();
28
29                 // Check if possible to use ftp functions.
30                 if ( ! extension_loaded('ftp') ) {
31                         $this->errors->add('no_ftp_ext', __('The ftp PHP extension is not available'));
32                         return;
33                 }
34
35                 // This Class uses the timeout on a per-connection basis, Others use it on a per-action basis.
36
37                 if ( ! defined('FS_TIMEOUT') )
38                         define('FS_TIMEOUT', 240);
39
40                 if ( empty($opt['port']) )
41                         $this->options['port'] = 21;
42                 else
43                         $this->options['port'] = $opt['port'];
44
45                 if ( empty($opt['hostname']) )
46                         $this->errors->add('empty_hostname', __('FTP hostname is required'));
47                 else
48                         $this->options['hostname'] = $opt['hostname'];
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         /**
67          * @access public
68          *
69          * @return bool
70          */
71         public function connect() {
72                 if ( isset($this->options['ssl']) && $this->options['ssl'] && function_exists('ftp_ssl_connect') )
73                         $this->link = @ftp_ssl_connect($this->options['hostname'], $this->options['port'], FS_CONNECT_TIMEOUT);
74                 else
75                         $this->link = @ftp_connect($this->options['hostname'], $this->options['port'], FS_CONNECT_TIMEOUT);
76
77                 if ( ! $this->link ) {
78                         $this->errors->add( 'connect',
79                                 /* translators: %s: hostname:port */
80                                 sprintf( __( 'Failed to connect to FTP Server %s' ),
81                                         $this->options['hostname'] . ':' . $this->options['port']
82                                 )
83                         );
84                         return false;
85                 }
86
87                 if ( ! @ftp_login( $this->link,$this->options['username'], $this->options['password'] ) ) {
88                         $this->errors->add( 'auth',
89                                 /* translators: %s: username */
90                                 sprintf( __( 'Username/Password incorrect for %s' ),
91                                         $this->options['username']
92                                 )
93                         );
94                         return false;
95                 }
96
97                 // Set the Connection to use Passive FTP
98                 @ftp_pasv( $this->link, true );
99                 if ( @ftp_get_option($this->link, FTP_TIMEOUT_SEC) < FS_TIMEOUT )
100                         @ftp_set_option($this->link, FTP_TIMEOUT_SEC, FS_TIMEOUT);
101
102                 return true;
103         }
104
105         /**
106          * Retrieves the file contents.
107          *
108          * @since 2.5.0
109          * @access public
110          *
111          * @param string $file Filename.
112          * @return string|false File contents on success, false if no temp file could be opened,
113          *                      or if the file couldn't be retrieved.
114          */
115         public function get_contents( $file ) {
116                 $tempfile = wp_tempnam($file);
117                 $temp = fopen($tempfile, 'w+');
118
119                 if ( ! $temp )
120                         return false;
121
122                 if ( ! @ftp_fget($this->link, $temp, $file, FTP_BINARY ) )
123                         return false;
124
125                 fseek( $temp, 0 ); // Skip back to the start of the file being written to
126                 $contents = '';
127
128                 while ( ! feof($temp) )
129                         $contents .= fread($temp, 8192);
130
131                 fclose($temp);
132                 unlink($tempfile);
133                 return $contents;
134         }
135
136         /**
137          * @access public
138          *
139          * @param string $file
140          * @return array
141          */
142         public function get_contents_array($file) {
143                 return explode("\n", $this->get_contents($file));
144         }
145
146         /**
147          * @access public
148          *
149          * @param string $file
150          * @param string $contents
151          * @param bool|int $mode
152          * @return bool
153          */
154         public function put_contents($file, $contents, $mode = false ) {
155                 $tempfile = wp_tempnam($file);
156                 $temp = fopen( $tempfile, 'wb+' );
157                 if ( ! $temp )
158                         return false;
159
160                 mbstring_binary_safe_encoding();
161
162                 $data_length = strlen( $contents );
163                 $bytes_written = fwrite( $temp, $contents );
164
165                 reset_mbstring_encoding();
166
167                 if ( $data_length !== $bytes_written ) {
168                         fclose( $temp );
169                         unlink( $tempfile );
170                         return false;
171                 }
172
173                 fseek( $temp, 0 ); // Skip back to the start of the file being written to
174
175                 $ret = @ftp_fput( $this->link, $file, $temp, FTP_BINARY );
176
177                 fclose($temp);
178                 unlink($tempfile);
179
180                 $this->chmod($file, $mode);
181
182                 return $ret;
183         }
184
185         /**
186          * @access public
187          *
188          * @return string
189          */
190         public function cwd() {
191                 $cwd = @ftp_pwd($this->link);
192                 if ( $cwd )
193                         $cwd = trailingslashit($cwd);
194                 return $cwd;
195         }
196
197         /**
198          * @access public
199          *
200          * @param string $dir
201          * @return bool
202          */
203         public function chdir($dir) {
204                 return @ftp_chdir($this->link, $dir);
205         }
206
207         /**
208          * @access public
209          *
210          * @param string $file
211          * @param int $mode
212          * @param bool $recursive
213          * @return bool
214          */
215         public function chmod($file, $mode = false, $recursive = false) {
216                 if ( ! $mode ) {
217                         if ( $this->is_file($file) )
218                                 $mode = FS_CHMOD_FILE;
219                         elseif ( $this->is_dir($file) )
220                                 $mode = FS_CHMOD_DIR;
221                         else
222                                 return false;
223                 }
224
225                 // chmod any sub-objects if recursive.
226                 if ( $recursive && $this->is_dir($file) ) {
227                         $filelist = $this->dirlist($file);
228                         foreach ( (array)$filelist as $filename => $filemeta )
229                                 $this->chmod($file . '/' . $filename, $mode, $recursive);
230                 }
231
232                 // chmod the file or directory
233                 if ( ! function_exists('ftp_chmod') )
234                         return (bool)@ftp_site($this->link, sprintf('CHMOD %o %s', $mode, $file));
235                 return (bool)@ftp_chmod($this->link, $mode, $file);
236         }
237
238         /**
239          * @access public
240          *
241          * @param string $file
242          * @return string
243          */
244         public function owner($file) {
245                 $dir = $this->dirlist($file);
246                 return $dir[$file]['owner'];
247         }
248         /**
249          * @access public
250          *
251          * @param string $file
252          * @return string
253          */
254         public function getchmod($file) {
255                 $dir = $this->dirlist($file);
256                 return $dir[$file]['permsn'];
257         }
258
259         /**
260          * @access public
261          *
262          * @param string $file
263          * @return string
264          */
265         public function group($file) {
266                 $dir = $this->dirlist($file);
267                 return $dir[$file]['group'];
268         }
269
270         /**
271          * @access public
272          *
273          * @param string $source
274          * @param string $destination
275          * @param bool   $overwrite
276          * @param string|bool $mode
277          * @return bool
278          */
279         public function copy($source, $destination, $overwrite = false, $mode = false) {
280                 if ( ! $overwrite && $this->exists($destination) )
281                         return false;
282                 $content = $this->get_contents($source);
283                 if ( false === $content )
284                         return false;
285                 return $this->put_contents($destination, $content, $mode);
286         }
287
288         /**
289          * @access public
290          *
291          * @param string $source
292          * @param string $destination
293          * @param bool $overwrite
294          * @return bool
295          */
296         public function move($source, $destination, $overwrite = false) {
297                 return ftp_rename($this->link, $source, $destination);
298         }
299
300         /**
301          * @access public
302          *
303          * @param string $file
304          * @param bool $recursive
305          * @param string $type
306          * @return bool
307          */
308         public function delete($file, $recursive = false, $type = false) {
309                 if ( empty($file) )
310                         return false;
311                 if ( 'f' == $type || $this->is_file($file) )
312                         return @ftp_delete($this->link, $file);
313                 if ( !$recursive )
314                         return @ftp_rmdir($this->link, $file);
315
316                 $filelist = $this->dirlist( trailingslashit($file) );
317                 if ( !empty($filelist) )
318                         foreach ( $filelist as $delete_file )
319                                 $this->delete( trailingslashit($file) . $delete_file['name'], $recursive, $delete_file['type'] );
320                 return @ftp_rmdir($this->link, $file);
321         }
322
323         /**
324          * @access public
325          *
326          * @param string $file
327          * @return bool
328          */
329         public function exists($file) {
330                 $list = @ftp_nlist($this->link, $file);
331
332                 if ( empty( $list ) && $this->is_dir( $file ) ) {
333                         return true; // File is an empty directory.
334                 }
335
336                 return !empty($list); //empty list = no file, so invert.
337         }
338
339         /**
340          * @access public
341          *
342          * @param string $file
343          * @return bool
344          */
345         public function is_file($file) {
346                 return $this->exists($file) && !$this->is_dir($file);
347         }
348
349         /**
350          * @access public
351          *
352          * @param string $path
353          * @return bool
354          */
355         public function is_dir($path) {
356                 $cwd = $this->cwd();
357                 $result = @ftp_chdir($this->link, trailingslashit($path) );
358                 if ( $result && $path == $this->cwd() || $this->cwd() != $cwd ) {
359                         @ftp_chdir($this->link, $cwd);
360                         return true;
361                 }
362                 return false;
363         }
364
365         /**
366          * @access public
367          *
368          * @param string $file
369          * @return bool
370          */
371         public function is_readable($file) {
372                 return true;
373         }
374
375         /**
376          * @access public
377          *
378          * @param string $file
379          * @return bool
380          */
381         public function is_writable($file) {
382                 return true;
383         }
384
385         /**
386          * @access public
387          *
388          * @param string $file
389          * @return bool
390          */
391         public function atime($file) {
392                 return false;
393         }
394
395         /**
396          * @access public
397          *
398          * @param string $file
399          * @return int
400          */
401         public function mtime($file) {
402                 return ftp_mdtm($this->link, $file);
403         }
404
405         /**
406          * @access public
407          *
408          * @param string $file
409          * @return int
410          */
411         public function size($file) {
412                 return ftp_size($this->link, $file);
413         }
414
415         /**
416          * @access public
417          *
418          * @param string $file
419          * @return bool
420          */
421         public function touch($file, $time = 0, $atime = 0) {
422                 return false;
423         }
424
425         /**
426          * @access public
427          *
428          * @param string $path
429          * @param mixed $chmod
430          * @param mixed $chown
431          * @param mixed $chgrp
432          * @return bool
433          */
434         public function mkdir($path, $chmod = false, $chown = false, $chgrp = false) {
435                 $path = untrailingslashit($path);
436                 if ( empty($path) )
437                         return false;
438
439                 if ( !@ftp_mkdir($this->link, $path) )
440                         return false;
441                 $this->chmod($path, $chmod);
442                 return true;
443         }
444
445         /**
446          * @access public
447          *
448          * @param string $path
449          * @param bool $recursive
450          * @return bool
451          */
452         public function rmdir($path, $recursive = false) {
453                 return $this->delete($path, $recursive);
454         }
455
456         /**
457          * @access public
458          *
459          * @staticvar bool $is_windows
460          * @param string $line
461          * @return array
462          */
463         public function parselisting($line) {
464                 static $is_windows = null;
465                 if ( is_null($is_windows) )
466                         $is_windows = stripos( ftp_systype($this->link), 'win') !== false;
467
468                 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) ) {
469                         $b = array();
470                         if ( $lucifer[3] < 70 )
471                                 $lucifer[3] +=2000;
472                         else
473                                 $lucifer[3] += 1900; // 4digit year fix
474                         $b['isdir'] = ( $lucifer[7] == '<DIR>');
475                         if ( $b['isdir'] )
476                                 $b['type'] = 'd';
477                         else
478                                 $b['type'] = 'f';
479                         $b['size'] = $lucifer[7];
480                         $b['month'] = $lucifer[1];
481                         $b['day'] = $lucifer[2];
482                         $b['year'] = $lucifer[3];
483                         $b['hour'] = $lucifer[4];
484                         $b['minute'] = $lucifer[5];
485                         $b['time'] = @mktime($lucifer[4] + (strcasecmp($lucifer[6], "PM") == 0 ? 12 : 0), $lucifer[5], 0, $lucifer[1], $lucifer[2], $lucifer[3]);
486                         $b['am/pm'] = $lucifer[6];
487                         $b['name'] = $lucifer[8];
488                 } elseif ( !$is_windows && $lucifer = preg_split('/[ ]/', $line, 9, PREG_SPLIT_NO_EMPTY)) {
489                         //echo $line."\n";
490                         $lcount = count($lucifer);
491                         if ( $lcount < 8 )
492                                 return '';
493                         $b = array();
494                         $b['isdir'] = $lucifer[0]{0} === 'd';
495                         $b['islink'] = $lucifer[0]{0} === 'l';
496                         if ( $b['isdir'] )
497                                 $b['type'] = 'd';
498                         elseif ( $b['islink'] )
499                                 $b['type'] = 'l';
500                         else
501                                 $b['type'] = 'f';
502                         $b['perms'] = $lucifer[0];
503                         $b['permsn'] = $this->getnumchmodfromh( $b['perms'] );
504                         $b['number'] = $lucifer[1];
505                         $b['owner'] = $lucifer[2];
506                         $b['group'] = $lucifer[3];
507                         $b['size'] = $lucifer[4];
508                         if ( $lcount == 8 ) {
509                                 sscanf($lucifer[5], '%d-%d-%d', $b['year'], $b['month'], $b['day']);
510                                 sscanf($lucifer[6], '%d:%d', $b['hour'], $b['minute']);
511                                 $b['time'] = @mktime($b['hour'], $b['minute'], 0, $b['month'], $b['day'], $b['year']);
512                                 $b['name'] = $lucifer[7];
513                         } else {
514                                 $b['month'] = $lucifer[5];
515                                 $b['day'] = $lucifer[6];
516                                 if ( preg_match('/([0-9]{2}):([0-9]{2})/', $lucifer[7], $l2) ) {
517                                         $b['year'] = date("Y");
518                                         $b['hour'] = $l2[1];
519                                         $b['minute'] = $l2[2];
520                                 } else {
521                                         $b['year'] = $lucifer[7];
522                                         $b['hour'] = 0;
523                                         $b['minute'] = 0;
524                                 }
525                                 $b['time'] = strtotime( sprintf('%d %s %d %02d:%02d', $b['day'], $b['month'], $b['year'], $b['hour'], $b['minute']) );
526                                 $b['name'] = $lucifer[8];
527                         }
528                 }
529
530                 // Replace symlinks formatted as "source -> target" with just the source name
531                 if ( isset( $b['islink'] ) && $b['islink'] ) {
532                         $b['name'] = preg_replace( '/(\s*->\s*.*)$/', '', $b['name'] );
533                 }
534
535                 return $b;
536         }
537
538         /**
539          * @access public
540          *
541          * @param string $path
542          * @param bool $include_hidden
543          * @param bool $recursive
544          * @return bool|array
545          */
546         public function dirlist($path = '.', $include_hidden = true, $recursive = false) {
547                 if ( $this->is_file($path) ) {
548                         $limit_file = basename($path);
549                         $path = dirname($path) . '/';
550                 } else {
551                         $limit_file = false;
552                 }
553
554                 $pwd = @ftp_pwd($this->link);
555                 if ( ! @ftp_chdir($this->link, $path) ) // Cant change to folder = folder doesn't exist
556                         return false;
557                 $list = @ftp_rawlist($this->link, '-a', false);
558                 @ftp_chdir($this->link, $pwd);
559
560                 if ( empty($list) ) // Empty array = non-existent folder (real folder will show . at least)
561                         return false;
562
563                 $dirlist = array();
564                 foreach ( $list as $k => $v ) {
565                         $entry = $this->parselisting($v);
566                         if ( empty($entry) )
567                                 continue;
568
569                         if ( '.' == $entry['name'] || '..' == $entry['name'] )
570                                 continue;
571
572                         if ( ! $include_hidden && '.' == $entry['name'][0] )
573                                 continue;
574
575                         if ( $limit_file && $entry['name'] != $limit_file)
576                                 continue;
577
578                         $dirlist[ $entry['name'] ] = $entry;
579                 }
580
581                 $ret = array();
582                 foreach ( (array)$dirlist as $struc ) {
583                         if ( 'd' == $struc['type'] ) {
584                                 if ( $recursive )
585                                         $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $include_hidden, $recursive);
586                                 else
587                                         $struc['files'] = array();
588                         }
589
590                         $ret[ $struc['name'] ] = $struc;
591                 }
592                 return $ret;
593         }
594
595         /**
596          * @access public
597          */
598         public function __destruct() {
599                 if ( $this->link )
600                         ftp_close($this->link);
601         }
602 }