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