]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/includes/class-wp-filesystem-direct.php
WordPress 4.4.1
[autoinstalls/wordpress.git] / wp-admin / includes / class-wp-filesystem-direct.php
1 <?php
2 /**
3  * WordPress Direct Filesystem.
4  *
5  * @package WordPress
6  * @subpackage Filesystem
7  */
8
9 /**
10  * WordPress Filesystem Class for direct PHP file and folder manipulation.
11  *
12  * @since 2.5.0
13  * @package WordPress
14  * @subpackage Filesystem
15  * @uses WP_Filesystem_Base Extends class
16  */
17 class WP_Filesystem_Direct extends WP_Filesystem_Base {
18
19         /**
20          * constructor
21          *
22          * @access public
23          *
24          * @param mixed $arg ignored argument
25          */
26         public function __construct($arg) {
27                 $this->method = 'direct';
28                 $this->errors = new WP_Error();
29         }
30
31         /**
32          * Reads entire file into a string
33          *
34          * @access public
35          *
36          * @param string $file Name of the file to read.
37          * @return string|bool The function returns the read data or false on failure.
38          */
39         public function get_contents($file) {
40                 return @file_get_contents($file);
41         }
42
43         /**
44          * Reads entire file into an array
45          *
46          * @access public
47          *
48          * @param string $file Path to the file.
49          * @return array|bool the file contents in an array or false on failure.
50          */
51         public function get_contents_array($file) {
52                 return @file($file);
53         }
54
55         /**
56          * Write a string to a file
57          *
58          * @access public
59          *
60          * @param string $file     Remote path to the file where to write the data.
61          * @param string $contents The data to write.
62          * @param int    $mode     Optional. The file permissions as octal number, usually 0644.
63          *                         Default false.
64          * @return bool False upon failure, true otherwise.
65          */
66         public function put_contents( $file, $contents, $mode = false ) {
67                 $fp = @fopen( $file, 'wb' );
68                 if ( ! $fp )
69                         return false;
70
71                 mbstring_binary_safe_encoding();
72
73                 $data_length = strlen( $contents );
74
75                 $bytes_written = fwrite( $fp, $contents );
76
77                 reset_mbstring_encoding();
78
79                 fclose( $fp );
80
81                 if ( $data_length !== $bytes_written )
82                         return false;
83
84                 $this->chmod( $file, $mode );
85
86                 return true;
87         }
88
89         /**
90          * Gets the current working directory
91          *
92          * @access public
93          *
94          * @return string|bool the current working directory on success, or false on failure.
95          */
96         public function cwd() {
97                 return @getcwd();
98         }
99
100         /**
101          * Change directory
102          *
103          * @access public
104          *
105          * @param string $dir The new current directory.
106          * @return bool Returns true on success or false on failure.
107          */
108         public function chdir($dir) {
109                 return @chdir($dir);
110         }
111
112         /**
113          * Changes file group
114          *
115          * @access public
116          *
117          * @param string $file      Path to the file.
118          * @param mixed  $group     A group name or number.
119          * @param bool   $recursive Optional. If set True changes file group recursively. Default false.
120          * @return bool Returns true on success or false on failure.
121          */
122         public function chgrp($file, $group, $recursive = false) {
123                 if ( ! $this->exists($file) )
124                         return false;
125                 if ( ! $recursive )
126                         return @chgrp($file, $group);
127                 if ( ! $this->is_dir($file) )
128                         return @chgrp($file, $group);
129                 // Is a directory, and we want recursive
130                 $file = trailingslashit($file);
131                 $filelist = $this->dirlist($file);
132                 foreach ($filelist as $filename)
133                         $this->chgrp($file . $filename, $group, $recursive);
134
135                 return true;
136         }
137
138         /**
139          * Changes filesystem permissions
140          *
141          * @access public
142          *
143          * @param string $file      Path to the file.
144          * @param int    $mode      Optional. The permissions as octal number, usually 0644 for files,
145          *                          0755 for dirs. Default false.
146          * @param bool   $recursive Optional. If set True changes file group recursively. Default false.
147          * @return bool Returns true on success or false on failure.
148          */
149         public function chmod($file, $mode = false, $recursive = false) {
150                 if ( ! $mode ) {
151                         if ( $this->is_file($file) )
152                                 $mode = FS_CHMOD_FILE;
153                         elseif ( $this->is_dir($file) )
154                                 $mode = FS_CHMOD_DIR;
155                         else
156                                 return false;
157                 }
158
159                 if ( ! $recursive || ! $this->is_dir($file) )
160                         return @chmod($file, $mode);
161                 // Is a directory, and we want recursive
162                 $file = trailingslashit($file);
163                 $filelist = $this->dirlist($file);
164                 foreach ( (array)$filelist as $filename => $filemeta)
165                         $this->chmod($file . $filename, $mode, $recursive);
166
167                 return true;
168         }
169
170         /**
171          * Changes file owner
172          *
173          * @access public
174          *
175          * @param string $file      Path to the file.
176          * @param mixed  $owner     A user name or number.
177          * @param bool   $recursive Optional. If set True changes file owner recursively.
178          *                          Default false.
179          * @return bool Returns true on success or false on failure.
180          */
181         public function chown($file, $owner, $recursive = false) {
182                 if ( ! $this->exists($file) )
183                         return false;
184                 if ( ! $recursive )
185                         return @chown($file, $owner);
186                 if ( ! $this->is_dir($file) )
187                         return @chown($file, $owner);
188                 // Is a directory, and we want recursive
189                 $filelist = $this->dirlist($file);
190                 foreach ($filelist as $filename) {
191                         $this->chown($file . '/' . $filename, $owner, $recursive);
192                 }
193                 return true;
194         }
195
196         /**
197          * Gets file owner
198          *
199          * @access public
200          *
201          * @param string $file Path to the file.
202          * @return string|bool Username of the user or false on error.
203          */
204         public function owner($file) {
205                 $owneruid = @fileowner($file);
206                 if ( ! $owneruid )
207                         return false;
208                 if ( ! function_exists('posix_getpwuid') )
209                         return $owneruid;
210                 $ownerarray = posix_getpwuid($owneruid);
211                 return $ownerarray['name'];
212         }
213
214         /**
215          * Gets file permissions
216          *
217          * FIXME does not handle errors in fileperms()
218          *
219          * @access public
220          *
221          * @param string $file Path to the file.
222          * @return string Mode of the file (last 3 digits).
223          */
224         public function getchmod($file) {
225                 return substr( decoct( @fileperms( $file ) ), -3 );
226         }
227
228         /**
229          * @access public
230          *
231          * @param string $file
232          * @return string|false
233          */
234         public function group($file) {
235                 $gid = @filegroup($file);
236                 if ( ! $gid )
237                         return false;
238                 if ( ! function_exists('posix_getgrgid') )
239                         return $gid;
240                 $grouparray = posix_getgrgid($gid);
241                 return $grouparray['name'];
242         }
243
244         /**
245          * @access public
246          *
247          * @param string $source
248          * @param string $destination
249          * @param bool   $overwrite
250          * @param int    $mode
251          * @return bool
252          */
253         public function copy($source, $destination, $overwrite = false, $mode = false) {
254                 if ( ! $overwrite && $this->exists($destination) )
255                         return false;
256
257                 $rtval = copy($source, $destination);
258                 if ( $mode )
259                         $this->chmod($destination, $mode);
260                 return $rtval;
261         }
262
263         /**
264          * @access public
265          *
266          * @param string $source
267          * @param string $destination
268          * @param bool $overwrite
269          * @return bool
270          */
271         public function move($source, $destination, $overwrite = false) {
272                 if ( ! $overwrite && $this->exists($destination) )
273                         return false;
274
275                 // Try using rename first. if that fails (for example, source is read only) try copy.
276                 if ( @rename($source, $destination) )
277                         return true;
278
279                 if ( $this->copy($source, $destination, $overwrite) && $this->exists($destination) ) {
280                         $this->delete($source);
281                         return true;
282                 } else {
283                         return false;
284                 }
285         }
286
287         /**
288          * @access public
289          *
290          * @param string $file
291          * @param bool $recursive
292          * @param string $type
293          * @return bool
294          */
295         public function delete($file, $recursive = false, $type = false) {
296                 if ( empty( $file ) ) // Some filesystems report this as /, which can cause non-expected recursive deletion of all files in the filesystem.
297                         return false;
298                 $file = str_replace( '\\', '/', $file ); // for win32, occasional problems deleting files otherwise
299
300                 if ( 'f' == $type || $this->is_file($file) )
301                         return @unlink($file);
302                 if ( ! $recursive && $this->is_dir($file) )
303                         return @rmdir($file);
304
305                 // At this point it's a folder, and we're in recursive mode
306                 $file = trailingslashit($file);
307                 $filelist = $this->dirlist($file, true);
308
309                 $retval = true;
310                 if ( is_array( $filelist ) ) {
311                         foreach ( $filelist as $filename => $fileinfo ) {
312                                 if ( ! $this->delete($file . $filename, $recursive, $fileinfo['type']) )
313                                         $retval = false;
314                         }
315                 }
316
317                 if ( file_exists($file) && ! @rmdir($file) )
318                         $retval = false;
319
320                 return $retval;
321         }
322         /**
323          * @access public
324          *
325          * @param string $file
326          * @return bool
327          */
328         public function exists($file) {
329                 return @file_exists($file);
330         }
331         /**
332          * @access public
333          *
334          * @param string $file
335          * @return bool
336          */
337         public function is_file($file) {
338                 return @is_file($file);
339         }
340         /**
341          * @access public
342          *
343          * @param string $path
344          * @return bool
345          */
346         public function is_dir($path) {
347                 return @is_dir($path);
348         }
349
350         /**
351          * @access public
352          *
353          * @param string $file
354          * @return bool
355          */
356         public function is_readable($file) {
357                 return @is_readable($file);
358         }
359
360         /**
361          * @access public
362          *
363          * @param string $file
364          * @return bool
365          */
366         public function is_writable($file) {
367                 return @is_writable($file);
368         }
369
370         /**
371          * @access public
372          *
373          * @param string $file
374          * @return int
375          */
376         public function atime($file) {
377                 return @fileatime($file);
378         }
379
380         /**
381          * @access public
382          *
383          * @param string $file
384          * @return int
385          */
386         public function mtime($file) {
387                 return @filemtime($file);
388         }
389
390         /**
391          * @access public
392          *
393          * @param string $file
394          * @return int
395          */
396         public function size($file) {
397                 return @filesize($file);
398         }
399
400         /**
401          * @access public
402          *
403          * @param string $file
404          * @param int $time
405          * @param int $atime
406          * @return bool
407          */
408         public function touch($file, $time = 0, $atime = 0) {
409                 if ($time == 0)
410                         $time = time();
411                 if ($atime == 0)
412                         $atime = time();
413                 return @touch($file, $time, $atime);
414         }
415
416         /**
417          * @access public
418          *
419          * @param string $path
420          * @param mixed  $chmod
421          * @param mixed  $chown
422          * @param mixed  $chgrp
423          * @return bool
424          */
425         public function mkdir($path, $chmod = false, $chown = false, $chgrp = false) {
426                 // Safe mode fails with a trailing slash under certain PHP versions.
427                 $path = untrailingslashit($path);
428                 if ( empty($path) )
429                         return false;
430
431                 if ( ! $chmod )
432                         $chmod = FS_CHMOD_DIR;
433
434                 if ( ! @mkdir($path) )
435                         return false;
436                 $this->chmod($path, $chmod);
437                 if ( $chown )
438                         $this->chown($path, $chown);
439                 if ( $chgrp )
440                         $this->chgrp($path, $chgrp);
441                 return true;
442         }
443
444         /**
445          * @access public
446          *
447          * @param string $path
448          * @param bool $recursive
449          * @return bool
450          */
451         public function rmdir($path, $recursive = false) {
452                 return $this->delete($path, $recursive);
453         }
454
455         /**
456          * @access public
457          *
458          * @param string $path
459          * @param bool $include_hidden
460          * @param bool $recursive
461          * @return bool|array
462          */
463         public function dirlist($path, $include_hidden = true, $recursive = false) {
464                 if ( $this->is_file($path) ) {
465                         $limit_file = basename($path);
466                         $path = dirname($path);
467                 } else {
468                         $limit_file = false;
469                 }
470
471                 if ( ! $this->is_dir($path) )
472                         return false;
473
474                 $dir = @dir($path);
475                 if ( ! $dir )
476                         return false;
477
478                 $ret = array();
479
480                 while (false !== ($entry = $dir->read()) ) {
481                         $struc = array();
482                         $struc['name'] = $entry;
483
484                         if ( '.' == $struc['name'] || '..' == $struc['name'] )
485                                 continue;
486
487                         if ( ! $include_hidden && '.' == $struc['name'][0] )
488                                 continue;
489
490                         if ( $limit_file && $struc['name'] != $limit_file)
491                                 continue;
492
493                         $struc['perms']         = $this->gethchmod($path.'/'.$entry);
494                         $struc['permsn']        = $this->getnumchmodfromh($struc['perms']);
495                         $struc['number']        = false;
496                         $struc['owner']         = $this->owner($path.'/'.$entry);
497                         $struc['group']         = $this->group($path.'/'.$entry);
498                         $struc['size']          = $this->size($path.'/'.$entry);
499                         $struc['lastmodunix']= $this->mtime($path.'/'.$entry);
500                         $struc['lastmod']   = date('M j',$struc['lastmodunix']);
501                         $struc['time']          = date('h:i:s',$struc['lastmodunix']);
502                         $struc['type']          = $this->is_dir($path.'/'.$entry) ? 'd' : 'f';
503
504                         if ( 'd' == $struc['type'] ) {
505                                 if ( $recursive )
506                                         $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $include_hidden, $recursive);
507                                 else
508                                         $struc['files'] = array();
509                         }
510
511                         $ret[ $struc['name'] ] = $struc;
512                 }
513                 $dir->close();
514                 unset($dir);
515                 return $ret;
516         }
517 }