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