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