]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/includes/class-wp-filesystem-ftpsockets.php
WordPress 4.3.1-scripts
[autoinstalls/wordpress.git] / wp-admin / includes / class-wp-filesystem-ftpsockets.php
1 <?php
2 /**
3  * WordPress FTP Sockets Filesystem.
4  *
5  * @package WordPress
6  * @subpackage Filesystem
7  */
8
9 /**
10  * WordPress Filesystem Class for implementing FTP Sockets.
11  *
12  * @since 2.5.0
13  * @package WordPress
14  * @subpackage Filesystem
15  * @uses WP_Filesystem_Base Extends class
16  */
17 class WP_Filesystem_ftpsockets extends WP_Filesystem_Base {
18         /**
19          * @var ftp
20          */
21         public $ftp;
22
23         /**
24          *
25          * @param array $opt
26          */
27         public function __construct( $opt  = '' ) {
28                 $this->method = 'ftpsockets';
29                 $this->errors = new WP_Error();
30
31                 // Check if possible to use ftp functions.
32                 if ( ! @include_once( ABSPATH . 'wp-admin/includes/class-ftp.php' ) ) {
33                         return;
34                 }
35                 $this->ftp = new ftp();
36
37                 if ( empty($opt['port']) )
38                         $this->options['port'] = 21;
39                 else
40                         $this->options['port'] = $opt['port'];
41
42                 if ( empty($opt['hostname']) )
43                         $this->errors->add('empty_hostname', __('FTP hostname is required'));
44                 else
45                         $this->options['hostname'] = $opt['hostname'];
46
47                 // Check if the options provided are OK.
48                 if ( empty ($opt['username']) )
49                         $this->errors->add('empty_username', __('FTP username is required'));
50                 else
51                         $this->options['username'] = $opt['username'];
52
53                 if ( empty ($opt['password']) )
54                         $this->errors->add('empty_password', __('FTP password is required'));
55                 else
56                         $this->options['password'] = $opt['password'];
57         }
58
59         /**
60          *
61          * @return bool
62          */
63         public function connect() {
64                 if ( ! $this->ftp )
65                         return false;
66
67                 $this->ftp->setTimeout(FS_CONNECT_TIMEOUT);
68
69                 if ( ! $this->ftp->SetServer($this->options['hostname'], $this->options['port']) ) {
70                         $this->errors->add('connect', sprintf(__('Failed to connect to FTP Server %1$s:%2$s'), $this->options['hostname'], $this->options['port']));
71                         return false;
72                 }
73
74                 if ( ! $this->ftp->connect() ) {
75                         $this->errors->add('connect', sprintf(__('Failed to connect to FTP Server %1$s:%2$s'), $this->options['hostname'], $this->options['port']));
76                         return false;
77                 }
78
79                 if ( ! $this->ftp->login($this->options['username'], $this->options['password']) ) {
80                         $this->errors->add('auth', sprintf(__('Username/Password incorrect for %s'), $this->options['username']));
81                         return false;
82                 }
83
84                 $this->ftp->SetType( FTP_BINARY );
85                 $this->ftp->Passive( true );
86                 $this->ftp->setTimeout( FS_TIMEOUT );
87                 return true;
88         }
89
90         /**
91          * @param string $file
92          * @return false|string
93          */
94         public function get_contents( $file ) {
95                 if ( ! $this->exists($file) )
96                         return false;
97
98                 $temp = wp_tempnam( $file );
99
100                 if ( ! $temphandle = fopen($temp, 'w+') )
101                         return false;
102
103                 mbstring_binary_safe_encoding();
104
105                 if ( ! $this->ftp->fget($temphandle, $file) ) {
106                         fclose($temphandle);
107                         unlink($temp);
108
109                         reset_mbstring_encoding();
110
111                         return ''; // Blank document, File does exist, It's just blank.
112                 }
113
114                 reset_mbstring_encoding();
115
116                 fseek( $temphandle, 0 ); // Skip back to the start of the file being written to
117                 $contents = '';
118
119                 while ( ! feof($temphandle) )
120                         $contents .= fread($temphandle, 8192);
121
122                 fclose($temphandle);
123                 unlink($temp);
124                 return $contents;
125         }
126         /**
127          * @param string $file
128          * @return array
129          */
130         public function get_contents_array($file) {
131                 return explode("\n", $this->get_contents($file) );
132         }
133
134         /**
135          * @param string $file
136          * @param string $contents
137          * @param int|bool $mode
138          * @return bool
139          */
140         public function put_contents($file, $contents, $mode = false ) {
141                 $temp = wp_tempnam( $file );
142                 if ( ! $temphandle = @fopen($temp, 'w+') ) {
143                         unlink($temp);
144                         return false;
145                 }
146
147                 // The FTP class uses string functions internally during file download/upload
148                 mbstring_binary_safe_encoding();
149
150                 $bytes_written = fwrite( $temphandle, $contents );
151                 if ( false === $bytes_written || $bytes_written != strlen( $contents ) ) {
152                         fclose( $temphandle );
153                         unlink( $temp );
154
155                         reset_mbstring_encoding();
156
157                         return false;
158                 }
159
160                 fseek( $temphandle, 0 ); // Skip back to the start of the file being written to
161
162                 $ret = $this->ftp->fput($file, $temphandle);
163
164                 reset_mbstring_encoding();
165
166                 fclose($temphandle);
167                 unlink($temp);
168
169                 $this->chmod($file, $mode);
170
171                 return $ret;
172         }
173
174         /**
175          *
176          * @return string
177          */
178         public function cwd() {
179                 $cwd = $this->ftp->pwd();
180                 if ( $cwd )
181                         $cwd = trailingslashit($cwd);
182                 return $cwd;
183         }
184
185         /**
186          *
187          * @param string $file
188          * @return bool
189          */
190         public function chdir($file) {
191                 return $this->ftp->chdir($file);
192         }
193
194         /**
195          * @param string $file
196          * @param int|bool $mode
197          * @param bool $recursive
198          * @return bool
199          */
200         public function chmod($file, $mode = false, $recursive = false ) {
201                 if ( ! $mode ) {
202                         if ( $this->is_file($file) )
203                                 $mode = FS_CHMOD_FILE;
204                         elseif ( $this->is_dir($file) )
205                                 $mode = FS_CHMOD_DIR;
206                         else
207                                 return false;
208                 }
209
210                 // chmod any sub-objects if recursive.
211                 if ( $recursive && $this->is_dir($file) ) {
212                         $filelist = $this->dirlist($file);
213                         foreach ( (array)$filelist as $filename => $filemeta )
214                                 $this->chmod($file . '/' . $filename, $mode, $recursive);
215                 }
216
217                 // chmod the file or directory
218                 return $this->ftp->chmod($file, $mode);
219         }
220
221         /**
222          * @param string $file
223          * @return string
224          */
225         public function owner($file) {
226                 $dir = $this->dirlist($file);
227                 return $dir[$file]['owner'];
228         }
229         /**
230          * @param string $file
231          * @return string
232          */
233         public function getchmod($file) {
234                 $dir = $this->dirlist($file);
235                 return $dir[$file]['permsn'];
236         }
237         /**
238          * @param string $file
239          * @return string
240          */
241         public function group($file) {
242                 $dir = $this->dirlist($file);
243                 return $dir[$file]['group'];
244         }
245         /**
246          * @param string   $source
247          * @param string   $destination
248          * @param bool     $overwrite
249          * @param int|bool $mode
250          * @return bool
251          */
252         public function copy($source, $destination, $overwrite = false, $mode = false) {
253                 if ( ! $overwrite && $this->exists($destination) )
254                         return false;
255
256                 $content = $this->get_contents($source);
257                 if ( false === $content )
258                         return false;
259
260                 return $this->put_contents($destination, $content, $mode);
261         }
262         /**
263          * @param string $source
264          * @param string $destination
265          * @param bool   $overwrite
266          * @return bool
267          */
268         public function move($source, $destination, $overwrite = false ) {
269                 return $this->ftp->rename($source, $destination);
270         }
271         /**
272          * @param string $file
273          * @param bool   $recursive
274          * @param string $type
275          * @return bool
276          */
277         public function delete($file, $recursive = false, $type = false) {
278                 if ( empty($file) )
279                         return false;
280                 if ( 'f' == $type || $this->is_file($file) )
281                         return $this->ftp->delete($file);
282                 if ( !$recursive )
283                         return $this->ftp->rmdir($file);
284
285                 return $this->ftp->mdel($file);
286         }
287
288         /**
289          * @param string $file
290          * @return bool
291          */
292         public function exists( $file ) {
293                 $list = $this->ftp->nlist( $file );
294
295                 if ( empty( $list ) && $this->is_dir( $file ) ) {
296                         return true; // File is an empty directory.
297                 }
298
299                 return !empty( $list ); //empty list = no file, so invert.
300                 // Return $this->ftp->is_exists($file); has issues with ABOR+426 responses on the ncFTPd server.
301         }
302
303         /**
304          * @param string $file
305          * @return bool
306          */
307         public function is_file($file) {
308                 if ( $this->is_dir($file) )
309                         return false;
310                 if ( $this->exists($file) )
311                         return true;
312                 return false;
313         }
314
315         /**
316          * @param string $path
317          * @return bool
318          */
319         public function is_dir($path) {
320                 $cwd = $this->cwd();
321                 if ( $this->chdir($path) ) {
322                         $this->chdir($cwd);
323                         return true;
324                 }
325                 return false;
326         }
327
328         /**
329          * @param string $file
330          * @return bool
331          */
332         public function is_readable($file) {
333                 return true;
334         }
335
336         /**
337          * @param string $file
338          * @return bool
339          */
340         public function is_writable($file) {
341                 return true;
342         }
343
344         /**
345          * @param string $file
346          * @return bool
347          */
348         public function atime($file) {
349                 return false;
350         }
351
352         /**
353          * @param string $file
354          * @return int
355          */
356         public function mtime($file) {
357                 return $this->ftp->mdtm($file);
358         }
359
360         /**
361          * @param string $file
362          * @return int
363          */
364         public function size($file) {
365                 return $this->ftp->filesize($file);
366         }
367         /**
368          * @param string $file
369          * @param int $time
370          * @param int $atime
371          * @return bool
372          */
373         public function touch($file, $time = 0, $atime = 0 ) {
374                 return false;
375         }
376
377         /**
378          * @param string $path
379          * @param mixed  $chmod
380          * @param mixed  $chown
381          * @param mixed  $chgrp
382          * @return bool
383          */
384         public function mkdir($path, $chmod = false, $chown = false, $chgrp = false ) {
385                 $path = untrailingslashit($path);
386                 if ( empty($path) )
387                         return false;
388
389                 if ( ! $this->ftp->mkdir($path) )
390                         return false;
391                 if ( ! $chmod )
392                         $chmod = FS_CHMOD_DIR;
393                 $this->chmod($path, $chmod);
394                 return true;
395         }
396
397         /**
398          * @param sting $path
399          * @param bool $recursive
400          */
401         public function rmdir($path, $recursive = false ) {
402                 $this->delete($path, $recursive);
403         }
404
405         /**
406          * @param string $path
407          * @param bool   $include_hidden
408          * @param bool   $recursive
409          * @return bool|array
410          */
411         public function dirlist($path = '.', $include_hidden = true, $recursive = false ) {
412                 if ( $this->is_file($path) ) {
413                         $limit_file = basename($path);
414                         $path = dirname($path) . '/';
415                 } else {
416                         $limit_file = false;
417                 }
418
419                 mbstring_binary_safe_encoding();
420
421                 $list = $this->ftp->dirlist($path);
422                 if ( empty( $list ) && ! $this->exists( $path ) ) {
423
424                         reset_mbstring_encoding();
425
426                         return false;
427                 }
428
429                 $ret = array();
430                 foreach ( $list as $struc ) {
431
432                         if ( '.' == $struc['name'] || '..' == $struc['name'] )
433                                 continue;
434
435                         if ( ! $include_hidden && '.' == $struc['name'][0] )
436                                 continue;
437
438                         if ( $limit_file && $struc['name'] != $limit_file )
439                                 continue;
440
441                         if ( 'd' == $struc['type'] ) {
442                                 if ( $recursive )
443                                         $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $include_hidden, $recursive);
444                                 else
445                                         $struc['files'] = array();
446                         }
447
448                         // Replace symlinks formatted as "source -> target" with just the source name
449                         if ( $struc['islink'] )
450                                 $struc['name'] = preg_replace( '/(\s*->\s*.*)$/', '', $struc['name'] );
451
452                         // Add the Octal representation of the file permissions
453                         $struc['permsn'] = $this->getnumchmodfromh( $struc['perms'] );
454
455                         $ret[ $struc['name'] ] = $struc;
456                 }
457
458                 reset_mbstring_encoding();
459
460                 return $ret;
461         }
462
463         /**
464          * @access public
465          */
466         public function __destruct() {
467                 $this->ftp->quit();
468         }
469 }