]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/includes/class-wp-filesystem-base.php
WordPress 4.2.2-scripts
[autoinstalls/wordpress.git] / wp-admin / includes / class-wp-filesystem-base.php
1 <?php
2 /**
3  * Base WordPress Filesystem
4  *
5  * @package WordPress
6  * @subpackage Filesystem
7  */
8
9 /**
10  * Base WordPress Filesystem class for which Filesystem implementations extend
11  *
12  * @since 2.5.0
13  */
14 class WP_Filesystem_Base {
15         /**
16          * Whether to display debug data for the connection.
17          *
18          * @access public
19          * @since 2.5.0
20          * @var bool
21          */
22         public $verbose = false;
23
24         /**
25          * Cached list of local filepaths to mapped remote filepaths.
26          *
27          * @since 2.7.0
28          * @var array
29          */
30         public $cache = array();
31
32         /**
33          * The Access method of the current connection, Set automatically.
34          *
35          * @access public
36          * @since 2.5.0
37          * @var string
38          */
39         public $method = '';
40
41         public $errors = null;
42
43         public $options = array();
44
45         /**
46          * Return the path on the remote filesystem of ABSPATH.
47          *
48          * @access public
49          * @since 2.7.0
50          *
51          * @return string The location of the remote path.
52          */
53         public function abspath() {
54                 $folder = $this->find_folder(ABSPATH);
55                 // Perhaps the FTP folder is rooted at the WordPress install, Check for wp-includes folder in root, Could have some false positives, but rare.
56                 if ( ! $folder && $this->is_dir( '/' . WPINC ) )
57                         $folder = '/';
58                 return $folder;
59         }
60
61         /**
62          * Return the path on the remote filesystem of WP_CONTENT_DIR.
63          *
64          * @access public
65          * @since 2.7.0
66          *
67          * @return string The location of the remote path.
68          */
69         public function wp_content_dir() {
70                 return $this->find_folder(WP_CONTENT_DIR);
71         }
72
73         /**
74          * Return the path on the remote filesystem of WP_PLUGIN_DIR.
75          *
76          * @access public
77          * @since 2.7.0
78          *
79          * @return string The location of the remote path.
80          */
81         public function wp_plugins_dir() {
82                 return $this->find_folder(WP_PLUGIN_DIR);
83         }
84
85         /**
86          * Return the path on the remote filesystem of the Themes Directory.
87          *
88          * @access public
89          * @since 2.7.0
90          *
91          * @param string $theme The Theme stylesheet or template for the directory.
92          * @return string The location of the remote path.
93          */
94         public function wp_themes_dir( $theme = false ) {
95                 $theme_root = get_theme_root( $theme );
96
97                 // Account for relative theme roots
98                 if ( '/themes' == $theme_root || ! is_dir( $theme_root ) )
99                         $theme_root = WP_CONTENT_DIR . $theme_root;
100
101                 return $this->find_folder( $theme_root );
102         }
103
104         /**
105          * Return the path on the remote filesystem of WP_LANG_DIR.
106          *
107          * @access public
108          * @since 3.2.0
109          *
110          * @return string The location of the remote path.
111          */
112         public function wp_lang_dir() {
113                 return $this->find_folder(WP_LANG_DIR);
114         }
115
116         /**
117          * Locate a folder on the remote filesystem.
118          *
119          * @access public
120          * @since 2.5.0
121          * @deprecated 2.7.0 use WP_Filesystem::abspath() or WP_Filesystem::wp_*_dir() instead.
122          * @see WP_Filesystem::abspath()
123          * @see WP_Filesystem::wp_content_dir()
124          * @see WP_Filesystem::wp_plugins_dir()
125          * @see WP_Filesystem::wp_themes_dir()
126          * @see WP_Filesystem::wp_lang_dir()
127          *
128          * @param string $base The folder to start searching from.
129          * @param bool   $echo True to display debug information.
130          *                     Default false.
131          * @return string The location of the remote path.
132          */
133         public function find_base_dir( $base = '.', $echo = false ) {
134                 _deprecated_function(__FUNCTION__, '2.7', 'WP_Filesystem::abspath() or WP_Filesystem::wp_*_dir()' );
135                 $this->verbose = $echo;
136                 return $this->abspath();
137         }
138
139         /**
140          * Locate a folder on the remote filesystem.
141          *
142          * @access public
143          * @since 2.5.0
144          * @deprecated 2.7.0 use WP_Filesystem::abspath() or WP_Filesystem::wp_*_dir() methods instead.
145          * @see WP_Filesystem::abspath()
146          * @see WP_Filesystem::wp_content_dir()
147          * @see WP_Filesystem::wp_plugins_dir()
148          * @see WP_Filesystem::wp_themes_dir()
149          * @see WP_Filesystem::wp_lang_dir()
150          *
151          * @param string $base The folder to start searching from.
152          * @param bool   $echo True to display debug information.
153          * @return string The location of the remote path.
154          */
155         public function get_base_dir( $base = '.', $echo = false ) {
156                 _deprecated_function(__FUNCTION__, '2.7', 'WP_Filesystem::abspath() or WP_Filesystem::wp_*_dir()' );
157                 $this->verbose = $echo;
158                 return $this->abspath();
159         }
160
161         /**
162          * Locate a folder on the remote filesystem.
163          *
164          * Assumes that on Windows systems, Stripping off the Drive
165          * letter is OK Sanitizes \\ to / in windows filepaths.
166          *
167          * @access public
168          * @since 2.7.0
169          *
170          * @param string $folder the folder to locate.
171          * @return string|false The location of the remote path, false on failure.
172          */
173         public function find_folder( $folder ) {
174
175                 if ( isset( $this->cache[ $folder ] ) )
176                         return $this->cache[ $folder ];
177
178                 if ( stripos($this->method, 'ftp') !== false ) {
179                         $constant_overrides = array(
180                                 'FTP_BASE' => ABSPATH,
181                                 'FTP_CONTENT_DIR' => WP_CONTENT_DIR,
182                                 'FTP_PLUGIN_DIR' => WP_PLUGIN_DIR,
183                                 'FTP_LANG_DIR' => WP_LANG_DIR
184                         );
185
186                         // Direct matches ( folder = CONSTANT/ )
187                         foreach ( $constant_overrides as $constant => $dir ) {
188                                 if ( ! defined( $constant ) )
189                                         continue;
190                                 if ( $folder === $dir )
191                                         return trailingslashit( constant( $constant ) );
192                         }
193
194                         // Prefix Matches ( folder = CONSTANT/subdir )
195                         foreach ( $constant_overrides as $constant => $dir ) {
196                                 if ( ! defined( $constant ) )
197                                         continue;
198                                 if ( 0 === stripos( $folder, $dir ) ) { // $folder starts with $dir
199                                         $potential_folder = preg_replace( '#^' . preg_quote( $dir, '#' ) . '/#i', trailingslashit( constant( $constant ) ), $folder );
200                                         $potential_folder = trailingslashit( $potential_folder );
201
202                                         if ( $this->is_dir( $potential_folder ) ) {
203                                                 $this->cache[ $folder ] = $potential_folder;
204                                                 return $potential_folder;
205                                         }
206                                 }
207                         }
208                 } elseif ( 'direct' == $this->method ) {
209                         $folder = str_replace('\\', '/', $folder); // Windows path sanitisation
210                         return trailingslashit($folder);
211                 }
212
213                 $folder = preg_replace('|^([a-z]{1}):|i', '', $folder); // Strip out windows drive letter if it's there.
214                 $folder = str_replace('\\', '/', $folder); // Windows path sanitisation
215
216                 if ( isset($this->cache[ $folder ] ) )
217                         return $this->cache[ $folder ];
218
219                 if ( $this->exists($folder) ) { // Folder exists at that absolute path.
220                         $folder = trailingslashit($folder);
221                         $this->cache[ $folder ] = $folder;
222                         return $folder;
223                 }
224                 if ( $return = $this->search_for_folder($folder) )
225                         $this->cache[ $folder ] = $return;
226                 return $return;
227         }
228
229         /**
230          * Locate a folder on the remote filesystem.
231          *
232          * Expects Windows sanitized path.
233          *
234          * @since 2.7.0
235          *
236          * @param string $folder The folder to locate.
237          * @param string $base   The folder to start searching from.
238          * @param bool   $loop   If the function has recursed, Internal use only.
239          * @return string|false The location of the remote path, false to cease looping.
240          */
241         public function search_for_folder( $folder, $base = '.', $loop = false ) {
242                 if ( empty( $base ) || '.' == $base )
243                         $base = trailingslashit($this->cwd());
244
245                 $folder = untrailingslashit($folder);
246
247                 if ( $this->verbose )
248                         printf( "\n" . __('Looking for %1$s in %2$s') . "<br/>\n", $folder, $base );
249
250                 $folder_parts = explode('/', $folder);
251                 $folder_part_keys = array_keys( $folder_parts );
252                 $last_index = array_pop( $folder_part_keys );
253                 $last_path = $folder_parts[ $last_index ];
254
255                 $files = $this->dirlist( $base );
256
257                 foreach ( $folder_parts as $index => $key ) {
258                         if ( $index == $last_index )
259                                 continue; // We want this to be caught by the next code block.
260
261                         /*
262                          * Working from /home/ to /user/ to /wordpress/ see if that file exists within
263                          * the current folder, If it's found, change into it and follow through looking
264                          * for it. If it cant find WordPress down that route, it'll continue onto the next
265                          * folder level, and see if that matches, and so on. If it reaches the end, and still
266                          * cant find it, it'll return false for the entire function.
267                          */
268                         if ( isset($files[ $key ]) ){
269
270                                 // Let's try that folder:
271                                 $newdir = trailingslashit(path_join($base, $key));
272                                 if ( $this->verbose )
273                                         printf( "\n" . __('Changing to %s') . "<br/>\n", $newdir );
274
275                                 // Only search for the remaining path tokens in the directory, not the full path again.
276                                 $newfolder = implode( '/', array_slice( $folder_parts, $index + 1 ) );
277                                 if ( $ret = $this->search_for_folder( $newfolder, $newdir, $loop) )
278                                         return $ret;
279                         }
280                 }
281
282                 // Only check this as a last resort, to prevent locating the incorrect install. All above procedures will fail quickly if this is the right branch to take.
283                 if (isset( $files[ $last_path ] ) ) {
284                         if ( $this->verbose )
285                                 printf( "\n" . __('Found %s') . "<br/>\n",  $base . $last_path );
286                         return trailingslashit($base . $last_path);
287                 }
288
289                 // Prevent this function from looping again.
290                 // No need to proceed if we've just searched in /
291                 if ( $loop || '/' == $base )
292                         return false;
293
294                 // As an extra last resort, Change back to / if the folder wasn't found.
295                 // This comes into effect when the CWD is /home/user/ but WP is at /var/www/....
296                 return $this->search_for_folder( $folder, '/', true );
297
298         }
299
300         /**
301          * Return the *nix-style file permissions for a file.
302          *
303          * From the PHP documentation page for fileperms().
304          *
305          * @link http://docs.php.net/fileperms
306          *
307          * @access public
308          * @since 2.5.0
309          *
310          * @param string $file String filename.
311          * @return string The *nix-style representation of permissions.
312          */
313         public function gethchmod( $file ){
314                 $perms = $this->getchmod($file);
315                 if (($perms & 0xC000) == 0xC000) // Socket
316                         $info = 's';
317                 elseif (($perms & 0xA000) == 0xA000) // Symbolic Link
318                         $info = 'l';
319                 elseif (($perms & 0x8000) == 0x8000) // Regular
320                         $info = '-';
321                 elseif (($perms & 0x6000) == 0x6000) // Block special
322                         $info = 'b';
323                 elseif (($perms & 0x4000) == 0x4000) // Directory
324                         $info = 'd';
325                 elseif (($perms & 0x2000) == 0x2000) // Character special
326                         $info = 'c';
327                 elseif (($perms & 0x1000) == 0x1000) // FIFO pipe
328                         $info = 'p';
329                 else // Unknown
330                         $info = 'u';
331
332                 // Owner
333                 $info .= (($perms & 0x0100) ? 'r' : '-');
334                 $info .= (($perms & 0x0080) ? 'w' : '-');
335                 $info .= (($perms & 0x0040) ?
336                                         (($perms & 0x0800) ? 's' : 'x' ) :
337                                         (($perms & 0x0800) ? 'S' : '-'));
338
339                 // Group
340                 $info .= (($perms & 0x0020) ? 'r' : '-');
341                 $info .= (($perms & 0x0010) ? 'w' : '-');
342                 $info .= (($perms & 0x0008) ?
343                                         (($perms & 0x0400) ? 's' : 'x' ) :
344                                         (($perms & 0x0400) ? 'S' : '-'));
345
346                 // World
347                 $info .= (($perms & 0x0004) ? 'r' : '-');
348                 $info .= (($perms & 0x0002) ? 'w' : '-');
349                 $info .= (($perms & 0x0001) ?
350                                         (($perms & 0x0200) ? 't' : 'x' ) :
351                                         (($perms & 0x0200) ? 'T' : '-'));
352                 return $info;
353         }
354
355         /**
356          * Convert *nix-style file permissions to a octal number.
357          *
358          * Converts '-rw-r--r--' to 0644
359          * From "info at rvgate dot nl"'s comment on the PHP documentation for chmod()
360          *
361          * @link http://docs.php.net/manual/en/function.chmod.php#49614
362          *
363          * @access public
364          * @since 2.5.0
365          *
366          * @param string $mode string The *nix-style file permission.
367          * @return int octal representation
368          */
369         public function getnumchmodfromh( $mode ) {
370                 $realmode = '';
371                 $legal =  array('', 'w', 'r', 'x', '-');
372                 $attarray = preg_split('//', $mode);
373
374                 for ( $i = 0, $c = count( $attarray ); $i < $c; $i++ ) {
375                    if ($key = array_search($attarray[$i], $legal)) {
376                            $realmode .= $legal[$key];
377                    }
378                 }
379
380                 $mode = str_pad($realmode, 10, '-', STR_PAD_LEFT);
381                 $trans = array('-'=>'0', 'r'=>'4', 'w'=>'2', 'x'=>'1');
382                 $mode = strtr($mode,$trans);
383
384                 $newmode = $mode[0];
385                 $newmode .= $mode[1] + $mode[2] + $mode[3];
386                 $newmode .= $mode[4] + $mode[5] + $mode[6];
387                 $newmode .= $mode[7] + $mode[8] + $mode[9];
388                 return $newmode;
389         }
390
391         /**
392          * Determine if the string provided contains binary characters.
393          *
394          * @since 2.7.0
395          *
396          * @param string $text String to test against.
397          * @return bool true if string is binary, false otherwise.
398          */
399         public function is_binary( $text ) {
400                 return (bool) preg_match( '|[^\x20-\x7E]|', $text ); // chr(32)..chr(127)
401         }
402
403         /**
404          * Change the ownership of a file / folder.
405          *
406          * Default behavior is to do nothing, override this in your subclass, if desired.
407          *
408          * @since 2.5.0
409          *
410          * @param string $file      Path to the file.
411          * @param mixed  $owner     A user name or number.
412          * @param bool   $recursive Optional. If set True changes file owner recursivly. Defaults to False.
413          * @return bool Returns true on success or false on failure.
414          */
415         public function chown( $file, $owner, $recursive = false ) {
416                 return false;
417         }
418
419         /**
420          * Connect filesystem.
421          *
422          * @since 2.5.0
423          * @abstract
424          * @return bool True on success or false on failure (always true for WP_Filesystem_Direct).
425          */
426         public function connect() {
427                 return true;
428         }
429
430         /**
431          * Read entire file into a string.
432          *
433          * @since 2.5.0
434          * @abstract
435          * @param string $file Name of the file to read.
436          * @return mixed|bool Returns the read data or false on failure.
437          */
438         public function get_contents( $file ) {
439                 return false;
440         }
441
442         /**
443          * Read entire file into an array.
444          *
445          * @since 2.5.0
446          * @abstract
447          * @param string $file Path to the file.
448          * @return array|bool the file contents in an array or false on failure.
449          */
450         public function get_contents_array( $file ) {
451                 return false;
452         }
453
454         /**
455          * Write a string to a file.
456          *
457          * @since 2.5.0
458          * @abstract
459          * @param string $file     Remote path to the file where to write the data.
460          * @param string $contents The data to write.
461          * @param int    $mode     Optional. The file permissions as octal number, usually 0644.
462          * @return bool False on failure.
463          */
464         public function put_contents( $file, $contents, $mode = false ) {
465                 return false;
466         }
467
468         /**
469          * Get the current working directory.
470          *
471          * @since 2.5.0
472          * @abstract
473          * @return string|bool The current working directory on success, or false on failure.
474          */
475         public function cwd() {
476                 return false;
477         }
478
479         /**
480          * Change current directory.
481          *
482          * @since 2.5.0
483          * @abstract
484          * @param string $dir The new current directory.
485          * @return bool Returns true on success or false on failure.
486          */
487         public function chdir( $dir ) {
488                 return false;
489         }
490
491         /**
492          * Change the file group.
493          *
494          * @since 2.5.0
495          * @abstract
496          * @param string $file      Path to the file.
497          * @param mixed  $group     A group name or number.
498          * @param bool   $recursive Optional. If set True changes file group recursively. Defaults to False.
499          * @return bool Returns true on success or false on failure.
500          */
501         public function chgrp( $file, $group, $recursive = false ) {
502                 return false;
503         }
504
505         /**
506          * Change filesystem permissions.
507          *
508          * @since 2.5.0
509          * @abstract
510          * @param string $file      Path to the file.
511          * @param int    $mode      Optional. The permissions as octal number, usually 0644 for files, 0755 for dirs.
512          * @param bool   $recursive Optional. If set True changes file group recursively. Defaults to False.
513          * @return bool Returns true on success or false on failure.
514          */
515         public function chmod( $file, $mode = false, $recursive = false ) {
516                 return false;
517         }
518
519         /**
520          * Get the file owner.
521          *
522          * @since 2.5.0
523          * @abstract
524          * @param string $file Path to the file.
525          * @return string|bool Username of the user or false on error.
526          */
527         public function owner( $file ) {
528                 return false;
529         }
530
531         /**
532          * Get the file's group.
533          *
534          * @since 2.5.0
535          * @abstract
536          * @param string $file Path to the file.
537          * @return string|bool The group or false on error.
538          */
539         public function group( $file ) {
540                 return false;
541         }
542
543         /**
544          * Copy a file.
545          *
546          * @since 2.5.0
547          * @abstract
548          * @param string $source      Path to the source file.
549          * @param string $destination Path to the destination file.
550          * @param bool   $overwrite   Optional. Whether to overwrite the destination file if it exists.
551          *                            Default false.
552          * @param int    $mode        Optional. The permissions as octal number, usually 0644 for files, 0755 for dirs.
553          *                            Default false.
554          * @return bool True if file copied successfully, False otherwise.
555          */
556         public function copy( $source, $destination, $overwrite = false, $mode = false ) {
557                 return false;
558         }
559
560         /**
561          * Move a file.
562          *
563          * @since 2.5.0
564          * @abstract
565          * @param string $source      Path to the source file.
566          * @param string $destination Path to the destination file.
567          * @param bool   $overwrite   Optional. Whether to overwrite the destination file if it exists.
568          *                            Default false.
569          * @return bool True if file copied successfully, False otherwise.
570          */
571         public function move( $source, $destination, $overwrite = false ) {
572                 return false;
573         }
574
575         /**
576          * Delete a file or directory.
577          *
578          * @since 2.5.0
579          * @abstract
580          * @param string $file      Path to the file.
581          * @param bool   $recursive Optional. If set True changes file group recursively. Defaults to False.
582          *                          Default false.
583          * @param bool   $type      Type of resource. 'f' for file, 'd' for directory.
584          *                          Default false.
585          * @return bool True if the file or directory was deleted, false on failure.
586          */
587         public function delete( $file, $recursive = false, $type = false ) {
588                 return false;
589         }
590
591         /**
592          * Check if a file or directory exists.
593          *
594          * @since 2.5.0
595          * @abstract
596          * @param string $file Path to file/directory.
597          * @return bool Whether $file exists or not.
598          */
599         public function exists( $file ) {
600                 return false;
601         }
602
603         /**
604          * Check if resource is a file.
605          *
606          * @since 2.5.0
607          * @abstract
608          * @param string $file File path.
609          * @return bool Whether $file is a file.
610          */
611         public function is_file( $file ) {
612                 return false;
613         }
614
615         /**
616          * Check if resource is a directory.
617          *
618          * @since 2.5.0
619          * @abstract
620          * @param string $path Directory path.
621          * @return bool Whether $path is a directory.
622          */
623         public function is_dir( $path ) {
624                 return false;
625         }
626
627         /**
628          * Check if a file is readable.
629          *
630          * @since 2.5.0
631          * @abstract
632          * @param string $file Path to file.
633          * @return bool Whether $file is readable.
634          */
635         public function is_readable( $file ) {
636                 return false;
637         }
638
639         /**
640          * Check if a file or directory is writable.
641          *
642          * @since 2.5.0
643          * @abstract
644          * @return bool Whether $file is writable.
645          */
646         public function is_writable( $file ) {
647                 return false;
648         }
649
650         /**
651          * Gets the file's last access time.
652          *
653          * @since 2.5.0
654          * @abstract
655          * @param string $file Path to file.
656          * @return int|bool Unix timestamp representing last access time.
657          */
658         public function atime( $file ) {
659                 return false;
660         }
661
662         /**
663          * Gets the file modification time.
664          *
665          * @since 2.5.0
666          * @abstract
667          * @param string $file Path to file.
668          * @return int|bool Unix timestamp representing modification time.
669          */
670         public function mtime( $file ) {
671                 return false;
672         }
673
674         /**
675          * Gets the file size (in bytes).
676          *
677          * @since 2.5.0
678          * @abstract
679          * @param string $file Path to file.
680          * @return int|bool Size of the file in bytes.
681          */
682         public function size( $file ) {
683                 return false;
684         }
685
686         /**
687          * Set the access and modification times of a file.
688          *
689          * Note: If $file doesn't exist, it will be created.
690          *
691          * @since 2.5.0
692          * @abstract
693          * @param string $file  Path to file.
694          * @param int    $time  Optional. Modified time to set for file.
695          *                      Default 0.
696          * @param int    $atime Optional. Access time to set for file.
697          *                      Default 0.
698          * @return bool Whether operation was successful or not.
699          */
700         public function touch( $file, $time = 0, $atime = 0 ) {
701                 return false;
702         }
703
704         /**
705          * Create a directory.
706          *
707          * @since 2.5.0
708          * @abstract
709          * @param string $path  Path for new directory.
710          * @param mixed  $chmod Optional. The permissions as octal number, (or False to skip chmod)
711          *                      Default false.
712          * @param mixed  $chown Optional. A user name or number (or False to skip chown)
713          *                      Default false.
714          * @param mixed  $chgrp Optional. A group name or number (or False to skip chgrp).
715          *                      Default false.
716          * @return bool False if directory cannot be created, true otherwise.
717          */
718         public function mkdir( $path, $chmod = false, $chown = false, $chgrp = false ) {
719                 return false;
720         }
721
722         /**
723          * Delete a directory.
724          *
725          * @since 2.5.0
726          * @abstract
727          * @param string $path      Path to directory.
728          * @param bool   $recursive Optional. Whether to recursively remove files/directories.
729          *                          Default false.
730          * @return bool Whether directory is deleted successfully or not.
731          */
732         public function rmdir( $path, $recursive = false ) {
733                 return false;
734         }
735
736         /**
737          * Get details for files in a directory or a specific file.
738          *
739          * @since 2.5.0
740          * @abstract
741          *
742          * @param string $path           Path to directory or file.
743          * @param bool   $include_hidden Optional. Whether to include details of hidden ("." prefixed) files.
744          *                               Default true.
745          * @param bool   $recursive      Optional. Whether to recursively include file details in nested directories.
746          *                               Default false.
747          * @return array|bool {
748          *     Array of files. False if unable to list directory contents.
749          *
750          *     @type string $name        Name of the file/directory.
751          *     @type string $perms       *nix representation of permissions.
752          *     @type int    $permsn      Octal representation of permissions.
753          *     @type string $owner       Owner name or ID.
754          *     @type int    $size        Size of file in bytes.
755          *     @type int    $lastmodunix Last modified unix timestamp.
756          *     @type mixed  $lastmod     Last modified month (3 letter) and day (without leading 0).
757          *     @type int    $time        Last modified time.
758          *     @type string $type        Type of resource. 'f' for file, 'd' for directory.
759          *     @type mixed  $files       If a directory and $recursive is true, contains another array of files.
760          * }
761          */
762         public function dirlist( $path, $include_hidden = true, $recursive = false ) {
763                 return false;
764         }
765
766 } // WP_Filesystem_Base