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