3 * Base WordPress Filesystem
6 * @subpackage Filesystem
10 * Base WordPress Filesystem class for which Filesystem implementations extend
14 class WP_Filesystem_Base {
16 * Whether to display debug data for the connection.
22 public $verbose = false;
25 * Cached list of local filepaths to mapped remote filepaths.
31 public $cache = array();
34 * The Access method of the current connection, Set automatically.
46 public $errors = null;
51 public $options = array();
54 * Return the path on the remote filesystem of ABSPATH.
59 * @return string The location of the remote path.
61 public function abspath() {
62 $folder = $this->find_folder(ABSPATH);
63 // Perhaps the FTP folder is rooted at the WordPress install, Check for wp-includes folder in root, Could have some false positives, but rare.
64 if ( ! $folder && $this->is_dir( '/' . WPINC ) )
70 * Return the path on the remote filesystem of WP_CONTENT_DIR.
75 * @return string The location of the remote path.
77 public function wp_content_dir() {
78 return $this->find_folder(WP_CONTENT_DIR);
82 * Return the path on the remote filesystem of WP_PLUGIN_DIR.
87 * @return string The location of the remote path.
89 public function wp_plugins_dir() {
90 return $this->find_folder(WP_PLUGIN_DIR);
94 * Return the path on the remote filesystem of the Themes Directory.
99 * @param string $theme The Theme stylesheet or template for the directory.
100 * @return string The location of the remote path.
102 public function wp_themes_dir( $theme = false ) {
103 $theme_root = get_theme_root( $theme );
105 // Account for relative theme roots
106 if ( '/themes' == $theme_root || ! is_dir( $theme_root ) )
107 $theme_root = WP_CONTENT_DIR . $theme_root;
109 return $this->find_folder( $theme_root );
113 * Return the path on the remote filesystem of WP_LANG_DIR.
118 * @return string The location of the remote path.
120 public function wp_lang_dir() {
121 return $this->find_folder(WP_LANG_DIR);
125 * Locate a folder on the remote filesystem.
129 * @deprecated 2.7.0 use WP_Filesystem::abspath() or WP_Filesystem::wp_*_dir() instead.
130 * @see WP_Filesystem::abspath()
131 * @see WP_Filesystem::wp_content_dir()
132 * @see WP_Filesystem::wp_plugins_dir()
133 * @see WP_Filesystem::wp_themes_dir()
134 * @see WP_Filesystem::wp_lang_dir()
136 * @param string $base The folder to start searching from.
137 * @param bool $echo True to display debug information.
139 * @return string The location of the remote path.
141 public function find_base_dir( $base = '.', $echo = false ) {
142 _deprecated_function(__FUNCTION__, '2.7.0', 'WP_Filesystem::abspath() or WP_Filesystem::wp_*_dir()' );
143 $this->verbose = $echo;
144 return $this->abspath();
148 * Locate a folder on the remote filesystem.
152 * @deprecated 2.7.0 use WP_Filesystem::abspath() or WP_Filesystem::wp_*_dir() methods instead.
153 * @see WP_Filesystem::abspath()
154 * @see WP_Filesystem::wp_content_dir()
155 * @see WP_Filesystem::wp_plugins_dir()
156 * @see WP_Filesystem::wp_themes_dir()
157 * @see WP_Filesystem::wp_lang_dir()
159 * @param string $base The folder to start searching from.
160 * @param bool $echo True to display debug information.
161 * @return string The location of the remote path.
163 public function get_base_dir( $base = '.', $echo = false ) {
164 _deprecated_function(__FUNCTION__, '2.7.0', 'WP_Filesystem::abspath() or WP_Filesystem::wp_*_dir()' );
165 $this->verbose = $echo;
166 return $this->abspath();
170 * Locate a folder on the remote filesystem.
172 * Assumes that on Windows systems, Stripping off the Drive
173 * letter is OK Sanitizes \\ to / in windows filepaths.
178 * @param string $folder the folder to locate.
179 * @return string|false The location of the remote path, false on failure.
181 public function find_folder( $folder ) {
182 if ( isset( $this->cache[ $folder ] ) )
183 return $this->cache[ $folder ];
185 if ( stripos($this->method, 'ftp') !== false ) {
186 $constant_overrides = array(
187 'FTP_BASE' => ABSPATH,
188 'FTP_CONTENT_DIR' => WP_CONTENT_DIR,
189 'FTP_PLUGIN_DIR' => WP_PLUGIN_DIR,
190 'FTP_LANG_DIR' => WP_LANG_DIR
193 // Direct matches ( folder = CONSTANT/ )
194 foreach ( $constant_overrides as $constant => $dir ) {
195 if ( ! defined( $constant ) )
197 if ( $folder === $dir )
198 return trailingslashit( constant( $constant ) );
201 // Prefix Matches ( folder = CONSTANT/subdir )
202 foreach ( $constant_overrides as $constant => $dir ) {
203 if ( ! defined( $constant ) )
205 if ( 0 === stripos( $folder, $dir ) ) { // $folder starts with $dir
206 $potential_folder = preg_replace( '#^' . preg_quote( $dir, '#' ) . '/#i', trailingslashit( constant( $constant ) ), $folder );
207 $potential_folder = trailingslashit( $potential_folder );
209 if ( $this->is_dir( $potential_folder ) ) {
210 $this->cache[ $folder ] = $potential_folder;
211 return $potential_folder;
215 } elseif ( 'direct' == $this->method ) {
216 $folder = str_replace('\\', '/', $folder); // Windows path sanitisation
217 return trailingslashit($folder);
220 $folder = preg_replace('|^([a-z]{1}):|i', '', $folder); // Strip out windows drive letter if it's there.
221 $folder = str_replace('\\', '/', $folder); // Windows path sanitisation
223 if ( isset($this->cache[ $folder ] ) )
224 return $this->cache[ $folder ];
226 if ( $this->exists($folder) ) { // Folder exists at that absolute path.
227 $folder = trailingslashit($folder);
228 $this->cache[ $folder ] = $folder;
231 if ( $return = $this->search_for_folder($folder) )
232 $this->cache[ $folder ] = $return;
237 * Locate a folder on the remote filesystem.
239 * Expects Windows sanitized path.
244 * @param string $folder The folder to locate.
245 * @param string $base The folder to start searching from.
246 * @param bool $loop If the function has recursed, Internal use only.
247 * @return string|false The location of the remote path, false to cease looping.
249 public function search_for_folder( $folder, $base = '.', $loop = false ) {
250 if ( empty( $base ) || '.' == $base )
251 $base = trailingslashit($this->cwd());
253 $folder = untrailingslashit($folder);
255 if ( $this->verbose ) {
256 /* translators: 1: folder to locate, 2: folder to start searching from */
257 printf( "\n" . __( 'Looking for %1$s in %2$s' ) . "<br/>\n", $folder, $base );
260 $folder_parts = explode('/', $folder);
261 $folder_part_keys = array_keys( $folder_parts );
262 $last_index = array_pop( $folder_part_keys );
263 $last_path = $folder_parts[ $last_index ];
265 $files = $this->dirlist( $base );
267 foreach ( $folder_parts as $index => $key ) {
268 if ( $index == $last_index )
269 continue; // We want this to be caught by the next code block.
272 * Working from /home/ to /user/ to /wordpress/ see if that file exists within
273 * the current folder, If it's found, change into it and follow through looking
274 * for it. If it cant find WordPress down that route, it'll continue onto the next
275 * folder level, and see if that matches, and so on. If it reaches the end, and still
276 * cant find it, it'll return false for the entire function.
278 if ( isset($files[ $key ]) ){
280 // Let's try that folder:
281 $newdir = trailingslashit(path_join($base, $key));
282 if ( $this->verbose ) {
283 /* translators: %s: directory name */
284 printf( "\n" . __( 'Changing to %s' ) . "<br/>\n", $newdir );
287 // Only search for the remaining path tokens in the directory, not the full path again.
288 $newfolder = implode( '/', array_slice( $folder_parts, $index + 1 ) );
289 if ( $ret = $this->search_for_folder( $newfolder, $newdir, $loop) )
294 // Only check this as a last resort, to prevent locating the incorrect install.
295 // All above procedures will fail quickly if this is the right branch to take.
296 if (isset( $files[ $last_path ] ) ) {
297 if ( $this->verbose ) {
298 /* translators: %s: directory name */
299 printf( "\n" . __( 'Found %s' ) . "<br/>\n", $base . $last_path );
301 return trailingslashit($base . $last_path);
304 // Prevent this function from looping again.
305 // No need to proceed if we've just searched in /
306 if ( $loop || '/' == $base )
309 // As an extra last resort, Change back to / if the folder wasn't found.
310 // This comes into effect when the CWD is /home/user/ but WP is at /var/www/....
311 return $this->search_for_folder( $folder, '/', true );
316 * Return the *nix-style file permissions for a file.
318 * From the PHP documentation page for fileperms().
320 * @link https://secure.php.net/manual/en/function.fileperms.php
325 * @param string $file String filename.
326 * @return string The *nix-style representation of permissions.
328 public function gethchmod( $file ){
329 $perms = intval( $this->getchmod( $file ), 8 );
330 if (($perms & 0xC000) == 0xC000) // Socket
332 elseif (($perms & 0xA000) == 0xA000) // Symbolic Link
334 elseif (($perms & 0x8000) == 0x8000) // Regular
336 elseif (($perms & 0x6000) == 0x6000) // Block special
338 elseif (($perms & 0x4000) == 0x4000) // Directory
340 elseif (($perms & 0x2000) == 0x2000) // Character special
342 elseif (($perms & 0x1000) == 0x1000) // FIFO pipe
348 $info .= (($perms & 0x0100) ? 'r' : '-');
349 $info .= (($perms & 0x0080) ? 'w' : '-');
350 $info .= (($perms & 0x0040) ?
351 (($perms & 0x0800) ? 's' : 'x' ) :
352 (($perms & 0x0800) ? 'S' : '-'));
355 $info .= (($perms & 0x0020) ? 'r' : '-');
356 $info .= (($perms & 0x0010) ? 'w' : '-');
357 $info .= (($perms & 0x0008) ?
358 (($perms & 0x0400) ? 's' : 'x' ) :
359 (($perms & 0x0400) ? 'S' : '-'));
362 $info .= (($perms & 0x0004) ? 'r' : '-');
363 $info .= (($perms & 0x0002) ? 'w' : '-');
364 $info .= (($perms & 0x0001) ?
365 (($perms & 0x0200) ? 't' : 'x' ) :
366 (($perms & 0x0200) ? 'T' : '-'));
371 * Gets the permissions of the specified file or filepath in their octal format
375 * @param string $file
376 * @return string the last 3 characters of the octal number
378 public function getchmod( $file ) {
383 * Convert *nix-style file permissions to a octal number.
385 * Converts '-rw-r--r--' to 0644
386 * From "info at rvgate dot nl"'s comment on the PHP documentation for chmod()
388 * @link https://secure.php.net/manual/en/function.chmod.php#49614
393 * @param string $mode string The *nix-style file permission.
394 * @return int octal representation
396 public function getnumchmodfromh( $mode ) {
398 $legal = array('', 'w', 'r', 'x', '-');
399 $attarray = preg_split('//', $mode);
401 for ( $i = 0, $c = count( $attarray ); $i < $c; $i++ ) {
402 if ($key = array_search($attarray[$i], $legal)) {
403 $realmode .= $legal[$key];
407 $mode = str_pad($realmode, 10, '-', STR_PAD_LEFT);
408 $trans = array('-'=>'0', 'r'=>'4', 'w'=>'2', 'x'=>'1');
409 $mode = strtr($mode,$trans);
412 $newmode .= $mode[1] + $mode[2] + $mode[3];
413 $newmode .= $mode[4] + $mode[5] + $mode[6];
414 $newmode .= $mode[7] + $mode[8] + $mode[9];
419 * Determine if the string provided contains binary characters.
424 * @param string $text String to test against.
425 * @return bool true if string is binary, false otherwise.
427 public function is_binary( $text ) {
428 return (bool) preg_match( '|[^\x20-\x7E]|', $text ); // chr(32)..chr(127)
432 * Change the ownership of a file / folder.
434 * Default behavior is to do nothing, override this in your subclass, if desired.
439 * @param string $file Path to the file.
440 * @param mixed $owner A user name or number.
441 * @param bool $recursive Optional. If set True changes file owner recursivly. Defaults to False.
442 * @return bool Returns true on success or false on failure.
444 public function chown( $file, $owner, $recursive = false ) {
449 * Connect filesystem.
455 * @return bool True on success or false on failure (always true for WP_Filesystem_Direct).
457 public function connect() {
462 * Read entire file into a string.
468 * @param string $file Name of the file to read.
469 * @return mixed|bool Returns the read data or false on failure.
471 public function get_contents( $file ) {
476 * Read entire file into an array.
482 * @param string $file Path to the file.
483 * @return array|bool the file contents in an array or false on failure.
485 public function get_contents_array( $file ) {
490 * Write a string to a file.
496 * @param string $file Remote path to the file where to write the data.
497 * @param string $contents The data to write.
498 * @param int $mode Optional. The file permissions as octal number, usually 0644.
499 * @return bool False on failure.
501 public function put_contents( $file, $contents, $mode = false ) {
506 * Get the current working directory.
512 * @return string|bool The current working directory on success, or false on failure.
514 public function cwd() {
519 * Change current directory.
525 * @param string $dir The new current directory.
526 * @return bool|string
528 public function chdir( $dir ) {
533 * Change the file group.
539 * @param string $file Path to the file.
540 * @param mixed $group A group name or number.
541 * @param bool $recursive Optional. If set True changes file group recursively. Defaults to False.
542 * @return bool|string
544 public function chgrp( $file, $group, $recursive = false ) {
549 * Change filesystem permissions.
555 * @param string $file Path to the file.
556 * @param int $mode Optional. The permissions as octal number, usually 0644 for files, 0755 for dirs.
557 * @param bool $recursive Optional. If set True changes file group recursively. Defaults to False.
558 * @return bool|string
560 public function chmod( $file, $mode = false, $recursive = false ) {
565 * Get the file owner.
571 * @param string $file Path to the file.
572 * @return string|bool Username of the user or false on error.
574 public function owner( $file ) {
579 * Get the file's group.
585 * @param string $file Path to the file.
586 * @return string|bool The group or false on error.
588 public function group( $file ) {
599 * @param string $source Path to the source file.
600 * @param string $destination Path to the destination file.
601 * @param bool $overwrite Optional. Whether to overwrite the destination file if it exists.
603 * @param int $mode Optional. The permissions as octal number, usually 0644 for files, 0755 for dirs.
605 * @return bool True if file copied successfully, False otherwise.
607 public function copy( $source, $destination, $overwrite = false, $mode = false ) {
618 * @param string $source Path to the source file.
619 * @param string $destination Path to the destination file.
620 * @param bool $overwrite Optional. Whether to overwrite the destination file if it exists.
622 * @return bool True if file copied successfully, False otherwise.
624 public function move( $source, $destination, $overwrite = false ) {
629 * Delete a file or directory.
635 * @param string $file Path to the file.
636 * @param bool $recursive Optional. If set True changes file group recursively. Defaults to False.
638 * @param bool $type Type of resource. 'f' for file, 'd' for directory.
640 * @return bool True if the file or directory was deleted, false on failure.
642 public function delete( $file, $recursive = false, $type = false ) {
647 * Check if a file or directory exists.
653 * @param string $file Path to file/directory.
654 * @return bool Whether $file exists or not.
656 public function exists( $file ) {
661 * Check if resource is a file.
667 * @param string $file File path.
668 * @return bool Whether $file is a file.
670 public function is_file( $file ) {
675 * Check if resource is a directory.
681 * @param string $path Directory path.
682 * @return bool Whether $path is a directory.
684 public function is_dir( $path ) {
689 * Check if a file is readable.
695 * @param string $file Path to file.
696 * @return bool Whether $file is readable.
698 public function is_readable( $file ) {
703 * Check if a file or directory is writable.
709 * @param string $file Path to file.
710 * @return bool Whether $file is writable.
712 public function is_writable( $file ) {
717 * Gets the file's last access time.
723 * @param string $file Path to file.
724 * @return int|bool Unix timestamp representing last access time.
726 public function atime( $file ) {
731 * Gets the file modification time.
737 * @param string $file Path to file.
738 * @return int|bool Unix timestamp representing modification time.
740 public function mtime( $file ) {
745 * Gets the file size (in bytes).
751 * @param string $file Path to file.
752 * @return int|bool Size of the file in bytes.
754 public function size( $file ) {
759 * Set the access and modification times of a file.
761 * Note: If $file doesn't exist, it will be created.
767 * @param string $file Path to file.
768 * @param int $time Optional. Modified time to set for file.
770 * @param int $atime Optional. Access time to set for file.
772 * @return bool Whether operation was successful or not.
774 public function touch( $file, $time = 0, $atime = 0 ) {
779 * Create a directory.
785 * @param string $path Path for new directory.
786 * @param mixed $chmod Optional. The permissions as octal number, (or False to skip chmod)
788 * @param mixed $chown Optional. A user name or number (or False to skip chown)
790 * @param mixed $chgrp Optional. A group name or number (or False to skip chgrp).
792 * @return bool False if directory cannot be created, true otherwise.
794 public function mkdir( $path, $chmod = false, $chown = false, $chgrp = false ) {
799 * Delete a directory.
805 * @param string $path Path to directory.
806 * @param bool $recursive Optional. Whether to recursively remove files/directories.
808 * @return bool Whether directory is deleted successfully or not.
810 public function rmdir( $path, $recursive = false ) {
815 * Get details for files in a directory or a specific file.
821 * @param string $path Path to directory or file.
822 * @param bool $include_hidden Optional. Whether to include details of hidden ("." prefixed) files.
824 * @param bool $recursive Optional. Whether to recursively include file details in nested directories.
826 * @return array|bool {
827 * Array of files. False if unable to list directory contents.
829 * @type string $name Name of the file/directory.
830 * @type string $perms *nix representation of permissions.
831 * @type int $permsn Octal representation of permissions.
832 * @type string $owner Owner name or ID.
833 * @type int $size Size of file in bytes.
834 * @type int $lastmodunix Last modified unix timestamp.
835 * @type mixed $lastmod Last modified month (3 letter) and day (without leading 0).
836 * @type int $time Last modified time.
837 * @type string $type Type of resource. 'f' for file, 'd' for directory.
838 * @type mixed $files If a directory and $recursive is true, contains another array of files.
841 public function dirlist( $path, $include_hidden = true, $recursive = false ) {
845 } // WP_Filesystem_Base