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