]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/includes/class-wp-filesystem-ftpext.php
WordPress 4.4-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
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                 $path = dirname( $file );
331                 $filename = basename( $file );
332
333                 $file_list = @ftp_nlist( $this->link, '-a ' . $path );
334                 if ( $file_list ) {
335                         $file_list = array_map( 'basename', $file_list );
336                 }
337
338                 return $file_list && in_array( $filename, $file_list );
339         }
340
341         /**
342          * @access public
343          *
344          * @param string $file
345          * @return bool
346          */
347         public function is_file($file) {
348                 return $this->exists($file) && !$this->is_dir($file);
349         }
350
351         /**
352          * @access public
353          *
354          * @param string $path
355          * @return bool
356          */
357         public function is_dir($path) {
358                 $cwd = $this->cwd();
359                 $result = @ftp_chdir($this->link, trailingslashit($path) );
360                 if ( $result && $path == $this->cwd() || $this->cwd() != $cwd ) {
361                         @ftp_chdir($this->link, $cwd);
362                         return true;
363                 }
364                 return false;
365         }
366
367         /**
368          * @access public
369          *
370          * @param string $file
371          * @return bool
372          */
373         public function is_readable($file) {
374                 return true;
375         }
376
377         /**
378          * @access public
379          *
380          * @param string $file
381          * @return bool
382          */
383         public function is_writable($file) {
384                 return true;
385         }
386
387         /**
388          * @access public
389          *
390          * @param string $file
391          * @return bool
392          */
393         public function atime($file) {
394                 return false;
395         }
396
397         /**
398          * @access public
399          *
400          * @param string $file
401          * @return int
402          */
403         public function mtime($file) {
404                 return ftp_mdtm($this->link, $file);
405         }
406
407         /**
408          * @access public
409          *
410          * @param string $file
411          * @return int
412          */
413         public function size($file) {
414                 return ftp_size($this->link, $file);
415         }
416
417         /**
418          * @access public
419          *
420          * @param string $file
421          * @return bool
422          */
423         public function touch($file, $time = 0, $atime = 0) {
424                 return false;
425         }
426
427         /**
428          * @access public
429          *
430          * @param string $path
431          * @param mixed $chmod
432          * @param mixed $chown
433          * @param mixed $chgrp
434          * @return bool
435          */
436         public function mkdir($path, $chmod = false, $chown = false, $chgrp = false) {
437                 $path = untrailingslashit($path);
438                 if ( empty($path) )
439                         return false;
440
441                 if ( !@ftp_mkdir($this->link, $path) )
442                         return false;
443                 $this->chmod($path, $chmod);
444                 return true;
445         }
446
447         /**
448          * @access public
449          *
450          * @param string $path
451          * @param bool $recursive
452          * @return bool
453          */
454         public function rmdir($path, $recursive = false) {
455                 return $this->delete($path, $recursive);
456         }
457
458         /**
459          * @access public
460          *
461          * @staticvar bool $is_windows
462          * @param string $line
463          * @return array
464          */
465         public function parselisting($line) {
466                 static $is_windows = null;
467                 if ( is_null($is_windows) )
468                         $is_windows = stripos( ftp_systype($this->link), 'win') !== false;
469
470                 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) ) {
471                         $b = array();
472                         if ( $lucifer[3] < 70 )
473                                 $lucifer[3] +=2000;
474                         else
475                                 $lucifer[3] += 1900; // 4digit year fix
476                         $b['isdir'] = ( $lucifer[7] == '<DIR>');
477                         if ( $b['isdir'] )
478                                 $b['type'] = 'd';
479                         else
480                                 $b['type'] = 'f';
481                         $b['size'] = $lucifer[7];
482                         $b['month'] = $lucifer[1];
483                         $b['day'] = $lucifer[2];
484                         $b['year'] = $lucifer[3];
485                         $b['hour'] = $lucifer[4];
486                         $b['minute'] = $lucifer[5];
487                         $b['time'] = @mktime($lucifer[4] + (strcasecmp($lucifer[6], "PM") == 0 ? 12 : 0), $lucifer[5], 0, $lucifer[1], $lucifer[2], $lucifer[3]);
488                         $b['am/pm'] = $lucifer[6];
489                         $b['name'] = $lucifer[8];
490                 } elseif ( !$is_windows && $lucifer = preg_split('/[ ]/', $line, 9, PREG_SPLIT_NO_EMPTY)) {
491                         //echo $line."\n";
492                         $lcount = count($lucifer);
493                         if ( $lcount < 8 )
494                                 return '';
495                         $b = array();
496                         $b['isdir'] = $lucifer[0]{0} === 'd';
497                         $b['islink'] = $lucifer[0]{0} === 'l';
498                         if ( $b['isdir'] )
499                                 $b['type'] = 'd';
500                         elseif ( $b['islink'] )
501                                 $b['type'] = 'l';
502                         else
503                                 $b['type'] = 'f';
504                         $b['perms'] = $lucifer[0];
505                         $b['permsn'] = $this->getnumchmodfromh( $b['perms'] );
506                         $b['number'] = $lucifer[1];
507                         $b['owner'] = $lucifer[2];
508                         $b['group'] = $lucifer[3];
509                         $b['size'] = $lucifer[4];
510                         if ( $lcount == 8 ) {
511                                 sscanf($lucifer[5], '%d-%d-%d', $b['year'], $b['month'], $b['day']);
512                                 sscanf($lucifer[6], '%d:%d', $b['hour'], $b['minute']);
513                                 $b['time'] = @mktime($b['hour'], $b['minute'], 0, $b['month'], $b['day'], $b['year']);
514                                 $b['name'] = $lucifer[7];
515                         } else {
516                                 $b['month'] = $lucifer[5];
517                                 $b['day'] = $lucifer[6];
518                                 if ( preg_match('/([0-9]{2}):([0-9]{2})/', $lucifer[7], $l2) ) {
519                                         $b['year'] = date("Y");
520                                         $b['hour'] = $l2[1];
521                                         $b['minute'] = $l2[2];
522                                 } else {
523                                         $b['year'] = $lucifer[7];
524                                         $b['hour'] = 0;
525                                         $b['minute'] = 0;
526                                 }
527                                 $b['time'] = strtotime( sprintf('%d %s %d %02d:%02d', $b['day'], $b['month'], $b['year'], $b['hour'], $b['minute']) );
528                                 $b['name'] = $lucifer[8];
529                         }
530                 }
531
532                 // Replace symlinks formatted as "source -> target" with just the source name
533                 if ( isset( $b['islink'] ) && $b['islink'] ) {
534                         $b['name'] = preg_replace( '/(\s*->\s*.*)$/', '', $b['name'] );
535                 }
536
537                 return $b;
538         }
539
540         /**
541          * @access public
542          *
543          * @param string $path
544          * @param bool $include_hidden
545          * @param bool $recursive
546          * @return bool|array
547          */
548         public function dirlist($path = '.', $include_hidden = true, $recursive = false) {
549                 if ( $this->is_file($path) ) {
550                         $limit_file = basename($path);
551                         $path = dirname($path) . '/';
552                 } else {
553                         $limit_file = false;
554                 }
555
556                 $pwd = @ftp_pwd($this->link);
557                 if ( ! @ftp_chdir($this->link, $path) ) // Cant change to folder = folder doesn't exist
558                         return false;
559                 $list = @ftp_rawlist($this->link, '-a', false);
560                 @ftp_chdir($this->link, $pwd);
561
562                 if ( empty($list) ) // Empty array = non-existent folder (real folder will show . at least)
563                         return false;
564
565                 $dirlist = array();
566                 foreach ( $list as $k => $v ) {
567                         $entry = $this->parselisting($v);
568                         if ( empty($entry) )
569                                 continue;
570
571                         if ( '.' == $entry['name'] || '..' == $entry['name'] )
572                                 continue;
573
574                         if ( ! $include_hidden && '.' == $entry['name'][0] )
575                                 continue;
576
577                         if ( $limit_file && $entry['name'] != $limit_file)
578                                 continue;
579
580                         $dirlist[ $entry['name'] ] = $entry;
581                 }
582
583                 $ret = array();
584                 foreach ( (array)$dirlist as $struc ) {
585                         if ( 'd' == $struc['type'] ) {
586                                 if ( $recursive )
587                                         $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $include_hidden, $recursive);
588                                 else
589                                         $struc['files'] = array();
590                         }
591
592                         $ret[ $struc['name'] ] = $struc;
593                 }
594                 return $ret;
595         }
596
597         /**
598          * @access public
599          */
600         public function __destruct() {
601                 if ( $this->link )
602                         ftp_close($this->link);
603         }
604 }