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.
45 public $errors = null;
50 public $options = array();
53 * Return the path on the remote filesystem of ABSPATH.
58 * @return string The location of the remote path.
60 public function abspath() {
61 $folder = $this->find_folder(ABSPATH);
62 // Perhaps the FTP folder is rooted at the WordPress install, Check for wp-includes folder in root, Could have some false positives, but rare.
63 if ( ! $folder && $this->is_dir( '/' . WPINC ) )
69 * Return the path on the remote filesystem of WP_CONTENT_DIR.
74 * @return string The location of the remote path.
76 public function wp_content_dir() {
77 return $this->find_folder(WP_CONTENT_DIR);
81 * Return the path on the remote filesystem of WP_PLUGIN_DIR.
86 * @return string The location of the remote path.
88 public function wp_plugins_dir() {
89 return $this->find_folder(WP_PLUGIN_DIR);
93 * Return the path on the remote filesystem of the Themes Directory.
98 * @param string $theme The Theme stylesheet or template for the directory.
99 * @return string The location of the remote path.
101 public function wp_themes_dir( $theme = false ) {
102 $theme_root = get_theme_root( $theme );
104 // Account for relative theme roots
105 if ( '/themes' == $theme_root || ! is_dir( $theme_root ) )
106 $theme_root = WP_CONTENT_DIR . $theme_root;
108 return $this->find_folder( $theme_root );
112 * Return the path on the remote filesystem of WP_LANG_DIR.
117 * @return string The location of the remote path.
119 public function wp_lang_dir() {
120 return $this->find_folder(WP_LANG_DIR);
124 * Locate a folder on the remote filesystem.
128 * @deprecated 2.7.0 use WP_Filesystem::abspath() or WP_Filesystem::wp_*_dir() instead.
129 * @see WP_Filesystem::abspath()
130 * @see WP_Filesystem::wp_content_dir()
131 * @see WP_Filesystem::wp_plugins_dir()
132 * @see WP_Filesystem::wp_themes_dir()
133 * @see WP_Filesystem::wp_lang_dir()
135 * @param string $base The folder to start searching from.
136 * @param bool $echo True to display debug information.
138 * @return string The location of the remote path.
140 public function find_base_dir( $base = '.', $echo = false ) {
141 _deprecated_function(__FUNCTION__, '2.7', 'WP_Filesystem::abspath() or WP_Filesystem::wp_*_dir()' );
142 $this->verbose = $echo;
143 return $this->abspath();
147 * Locate a folder on the remote filesystem.
151 * @deprecated 2.7.0 use WP_Filesystem::abspath() or WP_Filesystem::wp_*_dir() methods instead.
152 * @see WP_Filesystem::abspath()
153 * @see WP_Filesystem::wp_content_dir()
154 * @see WP_Filesystem::wp_plugins_dir()
155 * @see WP_Filesystem::wp_themes_dir()
156 * @see WP_Filesystem::wp_lang_dir()
158 * @param string $base The folder to start searching from.
159 * @param bool $echo True to display debug information.
160 * @return string The location of the remote path.
162 public function get_base_dir( $base = '.', $echo = false ) {
163 _deprecated_function(__FUNCTION__, '2.7', 'WP_Filesystem::abspath() or WP_Filesystem::wp_*_dir()' );
164 $this->verbose = $echo;
165 return $this->abspath();
169 * Locate a folder on the remote filesystem.
171 * Assumes that on Windows systems, Stripping off the Drive
172 * letter is OK Sanitizes \\ to / in windows filepaths.
177 * @param string $folder the folder to locate.
178 * @return string|false The location of the remote path, false on failure.
180 public function find_folder( $folder ) {
181 if ( isset( $this->cache[ $folder ] ) )
182 return $this->cache[ $folder ];
184 if ( stripos($this->method, 'ftp') !== false ) {
185 $constant_overrides = array(
186 'FTP_BASE' => ABSPATH,
187 'FTP_CONTENT_DIR' => WP_CONTENT_DIR,
188 'FTP_PLUGIN_DIR' => WP_PLUGIN_DIR,
189 'FTP_LANG_DIR' => WP_LANG_DIR
192 // Direct matches ( folder = CONSTANT/ )
193 foreach ( $constant_overrides as $constant => $dir ) {
194 if ( ! defined( $constant ) )
196 if ( $folder === $dir )
197 return trailingslashit( constant( $constant ) );
200 // Prefix Matches ( folder = CONSTANT/subdir )
201 foreach ( $constant_overrides as $constant => $dir ) {
202 if ( ! defined( $constant ) )
204 if ( 0 === stripos( $folder, $dir ) ) { // $folder starts with $dir
205 $potential_folder = preg_replace( '#^' . preg_quote( $dir, '#' ) . '/#i', trailingslashit( constant( $constant ) ), $folder );
206 $potential_folder = trailingslashit( $potential_folder );
208 if ( $this->is_dir( $potential_folder ) ) {
209 $this->cache[ $folder ] = $potential_folder;
210 return $potential_folder;
214 } elseif ( 'direct' == $this->method ) {
215 $folder = str_replace('\\', '/', $folder); // Windows path sanitisation
216 return trailingslashit($folder);
219 $folder = preg_replace('|^([a-z]{1}):|i', '', $folder); // Strip out windows drive letter if it's there.
220 $folder = str_replace('\\', '/', $folder); // Windows path sanitisation
222 if ( isset($this->cache[ $folder ] ) )
223 return $this->cache[ $folder ];
225 if ( $this->exists($folder) ) { // Folder exists at that absolute path.
226 $folder = trailingslashit($folder);
227 $this->cache[ $folder ] = $folder;
230 if ( $return = $this->search_for_folder($folder) )
231 $this->cache[ $folder ] = $return;
236 * Locate a folder on the remote filesystem.
238 * Expects Windows sanitized path.
243 * @param string $folder The folder to locate.
244 * @param string $base The folder to start searching from.
245 * @param bool $loop If the function has recursed, Internal use only.
246 * @return string|false The location of the remote path, false to cease looping.
248 public function search_for_folder( $folder, $base = '.', $loop = false ) {
249 if ( empty( $base ) || '.' == $base )
250 $base = trailingslashit($this->cwd());
252 $folder = untrailingslashit($folder);
254 if ( $this->verbose ) {
255 /* translators: 1: folder to locate, 2: folder to start searching from */
256 printf( "\n" . __( 'Looking for %1$s in %2$s' ) . "<br/>\n", $folder, $base );
259 $folder_parts = explode('/', $folder);
260 $folder_part_keys = array_keys( $folder_parts );
261 $last_index = array_pop( $folder_part_keys );
262 $last_path = $folder_parts[ $last_index ];
264 $files = $this->dirlist( $base );
266 foreach ( $folder_parts as $index => $key ) {
267 if ( $index == $last_index )
268 continue; // We want this to be caught by the next code block.
271 * Working from /home/ to /user/ to /wordpress/ see if that file exists within
272 * the current folder, If it's found, change into it and follow through looking
273 * for it. If it cant find WordPress down that route, it'll continue onto the next
274 * folder level, and see if that matches, and so on. If it reaches the end, and still
275 * cant find it, it'll return false for the entire function.
277 if ( isset($files[ $key ]) ){
279 // Let's try that folder:
280 $newdir = trailingslashit(path_join($base, $key));
281 if ( $this->verbose ) {
282 /* translators: %s: directory name */
283 printf( "\n" . __( 'Changing to %s' ) . "<br/>\n", $newdir );
286 // Only search for the remaining path tokens in the directory, not the full path again.
287 $newfolder = implode( '/', array_slice( $folder_parts, $index + 1 ) );
288 if ( $ret = $this->search_for_folder( $newfolder, $newdir, $loop) )
293 // Only check this as a last resort, to prevent locating the incorrect install.
294 // All above procedures will fail quickly if this is the right branch to take.
295 if (isset( $files[ $last_path ] ) ) {
296 if ( $this->verbose ) {
297 /* translators: %s: directory name */
298 printf( "\n" . __( 'Found %s' ) . "<br/>\n", $base . $last_path );
300 return trailingslashit($base . $last_path);
303 // Prevent this function from looping again.
304 // No need to proceed if we've just searched in /
305 if ( $loop || '/' == $base )
308 // As an extra last resort, Change back to / if the folder wasn't found.
309 // This comes into effect when the CWD is /home/user/ but WP is at /var/www/....
310 return $this->search_for_folder( $folder, '/', true );
315 * Return the *nix-style file permissions for a file.
317 * From the PHP documentation page for fileperms().
319 * @link http://docs.php.net/fileperms
324 * @param string $file String filename.
325 * @return string The *nix-style representation of permissions.
327 public function gethchmod( $file ){
328 $perms = intval( $this->getchmod( $file ), 8 );
329 if (($perms & 0xC000) == 0xC000) // Socket
331 elseif (($perms & 0xA000) == 0xA000) // Symbolic Link
333 elseif (($perms & 0x8000) == 0x8000) // Regular
335 elseif (($perms & 0x6000) == 0x6000) // Block special
337 elseif (($perms & 0x4000) == 0x4000) // Directory
339 elseif (($perms & 0x2000) == 0x2000) // Character special
341 elseif (($perms & 0x1000) == 0x1000) // FIFO pipe
347 $info .= (($perms & 0x0100) ? 'r' : '-');
348 $info .= (($perms & 0x0080) ? 'w' : '-');
349 $info .= (($perms & 0x0040) ?
350 (($perms & 0x0800) ? 's' : 'x' ) :
351 (($perms & 0x0800) ? 'S' : '-'));
354 $info .= (($perms & 0x0020) ? 'r' : '-');
355 $info .= (($perms & 0x0010) ? 'w' : '-');
356 $info .= (($perms & 0x0008) ?
357 (($perms & 0x0400) ? 's' : 'x' ) :
358 (($perms & 0x0400) ? 'S' : '-'));
361 $info .= (($perms & 0x0004) ? 'r' : '-');
362 $info .= (($perms & 0x0002) ? 'w' : '-');
363 $info .= (($perms & 0x0001) ?
364 (($perms & 0x0200) ? 't' : 'x' ) :
365 (($perms & 0x0200) ? 'T' : '-'));
370 * Gets the permissions of the specified file or filepath in their octal format
374 * @param string $file
375 * @return string the last 3 characters of the octal number
377 public function getchmod( $file ) {
382 * Convert *nix-style file permissions to a octal number.
384 * Converts '-rw-r--r--' to 0644
385 * From "info at rvgate dot nl"'s comment on the PHP documentation for chmod()
387 * @link http://docs.php.net/manual/en/function.chmod.php#49614
392 * @param string $mode string The *nix-style file permission.
393 * @return int octal representation
395 public function getnumchmodfromh( $mode ) {
397 $legal = array('', 'w', 'r', 'x', '-');
398 $attarray = preg_split('//', $mode);
400 for ( $i = 0, $c = count( $attarray ); $i < $c; $i++ ) {
401 if ($key = array_search($attarray[$i], $legal)) {
402 $realmode .= $legal[$key];
406 $mode = str_pad($realmode, 10, '-', STR_PAD_LEFT);
407 $trans = array('-'=>'0', 'r'=>'4', 'w'=>'2', 'x'=>'1');
408 $mode = strtr($mode,$trans);
411 $newmode .= $mode[1] + $mode[2] + $mode[3];
412 $newmode .= $mode[4] + $mode[5] + $mode[6];
413 $newmode .= $mode[7] + $mode[8] + $mode[9];
418 * Determine if the string provided contains binary characters.
423 * @param string $text String to test against.
424 * @return bool true if string is binary, false otherwise.
426 public function is_binary( $text ) {
427 return (bool) preg_match( '|[^\x20-\x7E]|', $text ); // chr(32)..chr(127)
431 * Change the ownership of a file / folder.
433 * Default behavior is to do nothing, override this in your subclass, if desired.
438 * @param string $file Path to the file.
439 * @param mixed $owner A user name or number.
440 * @param bool $recursive Optional. If set True changes file owner recursivly. Defaults to False.
441 * @return bool Returns true on success or false on failure.
443 public function chown( $file, $owner, $recursive = false ) {
448 * Connect filesystem.
454 * @return bool True on success or false on failure (always true for WP_Filesystem_Direct).
456 public function connect() {
461 * Read entire file into a string.
467 * @param string $file Name of the file to read.
468 * @return mixed|bool Returns the read data or false on failure.
470 public function get_contents( $file ) {
475 * Read entire file into an array.
481 * @param string $file Path to the file.
482 * @return array|bool the file contents in an array or false on failure.
484 public function get_contents_array( $file ) {
489 * Write a string to a file.
495 * @param string $file Remote path to the file where to write the data.
496 * @param string $contents The data to write.
497 * @param int $mode Optional. The file permissions as octal number, usually 0644.
498 * @return bool False on failure.
500 public function put_contents( $file, $contents, $mode = false ) {
505 * Get the current working directory.
511 * @return string|bool The current working directory on success, or false on failure.
513 public function cwd() {
518 * Change current directory.
524 * @param string $dir The new current directory.
525 * @return bool|string
527 public function chdir( $dir ) {
532 * Change the file group.
538 * @param string $file Path to the file.
539 * @param mixed $group A group name or number.
540 * @param bool $recursive Optional. If set True changes file group recursively. Defaults to False.
541 * @return bool|string
543 public function chgrp( $file, $group, $recursive = false ) {
548 * Change filesystem permissions.
554 * @param string $file Path to the file.
555 * @param int $mode Optional. The permissions as octal number, usually 0644 for files, 0755 for dirs.
556 * @param bool $recursive Optional. If set True changes file group recursively. Defaults to False.
557 * @return bool|string
559 public function chmod( $file, $mode = false, $recursive = false ) {
564 * Get the file owner.
570 * @param string $file Path to the file.
571 * @return string|bool Username of the user or false on error.
573 public function owner( $file ) {
578 * Get the file's group.
584 * @param string $file Path to the file.
585 * @return string|bool The group or false on error.
587 public function group( $file ) {
598 * @param string $source Path to the source file.
599 * @param string $destination Path to the destination file.
600 * @param bool $overwrite Optional. Whether to overwrite the destination file if it exists.
602 * @param int $mode Optional. The permissions as octal number, usually 0644 for files, 0755 for dirs.
604 * @return bool True if file copied successfully, False otherwise.
606 public function copy( $source, $destination, $overwrite = false, $mode = false ) {
617 * @param string $source Path to the source file.
618 * @param string $destination Path to the destination file.
619 * @param bool $overwrite Optional. Whether to overwrite the destination file if it exists.
621 * @return bool True if file copied successfully, False otherwise.
623 public function move( $source, $destination, $overwrite = false ) {
628 * Delete a file or directory.
634 * @param string $file Path to the file.
635 * @param bool $recursive Optional. If set True changes file group recursively. Defaults to False.
637 * @param bool $type Type of resource. 'f' for file, 'd' for directory.
639 * @return bool True if the file or directory was deleted, false on failure.
641 public function delete( $file, $recursive = false, $type = false ) {
646 * Check if a file or directory exists.
652 * @param string $file Path to file/directory.
653 * @return bool Whether $file exists or not.
655 public function exists( $file ) {
660 * Check if resource is a file.
666 * @param string $file File path.
667 * @return bool Whether $file is a file.
669 public function is_file( $file ) {
674 * Check if resource is a directory.
680 * @param string $path Directory path.
681 * @return bool Whether $path is a directory.
683 public function is_dir( $path ) {
688 * Check if a file is readable.
694 * @param string $file Path to file.
695 * @return bool Whether $file is readable.
697 public function is_readable( $file ) {
702 * Check if a file or directory is writable.
708 * @param string $file Path to file.
709 * @return bool Whether $file is writable.
711 public function is_writable( $file ) {
716 * Gets the file's last access time.
722 * @param string $file Path to file.
723 * @return int|bool Unix timestamp representing last access time.
725 public function atime( $file ) {
730 * Gets the file modification time.
736 * @param string $file Path to file.
737 * @return int|bool Unix timestamp representing modification time.
739 public function mtime( $file ) {
744 * Gets the file size (in bytes).
750 * @param string $file Path to file.
751 * @return int|bool Size of the file in bytes.
753 public function size( $file ) {
758 * Set the access and modification times of a file.
760 * Note: If $file doesn't exist, it will be created.
766 * @param string $file Path to file.
767 * @param int $time Optional. Modified time to set for file.
769 * @param int $atime Optional. Access time to set for file.
771 * @return bool Whether operation was successful or not.
773 public function touch( $file, $time = 0, $atime = 0 ) {
778 * Create a directory.
784 * @param string $path Path for new directory.
785 * @param mixed $chmod Optional. The permissions as octal number, (or False to skip chmod)
787 * @param mixed $chown Optional. A user name or number (or False to skip chown)
789 * @param mixed $chgrp Optional. A group name or number (or False to skip chgrp).
791 * @return bool False if directory cannot be created, true otherwise.
793 public function mkdir( $path, $chmod = false, $chown = false, $chgrp = false ) {
798 * Delete a directory.
804 * @param string $path Path to directory.
805 * @param bool $recursive Optional. Whether to recursively remove files/directories.
807 * @return bool Whether directory is deleted successfully or not.
809 public function rmdir( $path, $recursive = false ) {
814 * Get details for files in a directory or a specific file.
820 * @param string $path Path to directory or file.
821 * @param bool $include_hidden Optional. Whether to include details of hidden ("." prefixed) files.
823 * @param bool $recursive Optional. Whether to recursively include file details in nested directories.
825 * @return array|bool {
826 * Array of files. False if unable to list directory contents.
828 * @type string $name Name of the file/directory.
829 * @type string $perms *nix representation of permissions.
830 * @type int $permsn Octal representation of permissions.
831 * @type string $owner Owner name or ID.
832 * @type int $size Size of file in bytes.
833 * @type int $lastmodunix Last modified unix timestamp.
834 * @type mixed $lastmod Last modified month (3 letter) and day (without leading 0).
835 * @type int $time Last modified time.
836 * @type string $type Type of resource. 'f' for file, 'd' for directory.
837 * @type mixed $files If a directory and $recursive is true, contains another array of files.
840 public function dirlist( $path, $include_hidden = true, $recursive = false ) {
844 } // WP_Filesystem_Base