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.
30 public $cache = array();
33 * The Access method of the current connection, Set automatically.
41 public $errors = null;
43 public $options = array();
46 * Return the path on the remote filesystem of ABSPATH.
51 * @return string The location of the remote path.
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 ) )
62 * Return the path on the remote filesystem of WP_CONTENT_DIR.
67 * @return string The location of the remote path.
69 public function wp_content_dir() {
70 return $this->find_folder(WP_CONTENT_DIR);
74 * Return the path on the remote filesystem of WP_PLUGIN_DIR.
79 * @return string The location of the remote path.
81 public function wp_plugins_dir() {
82 return $this->find_folder(WP_PLUGIN_DIR);
86 * Return the path on the remote filesystem of the Themes Directory.
91 * @param string $theme The Theme stylesheet or template for the directory.
92 * @return string The location of the remote path.
94 public function wp_themes_dir( $theme = false ) {
95 $theme_root = get_theme_root( $theme );
97 // Account for relative theme roots
98 if ( '/themes' == $theme_root || ! is_dir( $theme_root ) )
99 $theme_root = WP_CONTENT_DIR . $theme_root;
101 return $this->find_folder( $theme_root );
105 * Return the path on the remote filesystem of WP_LANG_DIR.
110 * @return string The location of the remote path.
112 public function wp_lang_dir() {
113 return $this->find_folder(WP_LANG_DIR);
117 * Locate a folder on the remote filesystem.
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()
128 * @param string $base The folder to start searching from.
129 * @param bool $echo True to display debug information.
131 * @return string The location of the remote path.
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();
140 * Locate a folder on the remote filesystem.
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()
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.
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();
162 * Locate a folder on the remote filesystem.
164 * Assumes that on Windows systems, Stripping off the Drive
165 * letter is OK Sanitizes \\ to / in windows filepaths.
170 * @param string $folder the folder to locate.
171 * @return string|false The location of the remote path, false on failure.
173 public function find_folder( $folder ) {
174 if ( isset( $this->cache[ $folder ] ) )
175 return $this->cache[ $folder ];
177 if ( stripos($this->method, 'ftp') !== false ) {
178 $constant_overrides = array(
179 'FTP_BASE' => ABSPATH,
180 'FTP_CONTENT_DIR' => WP_CONTENT_DIR,
181 'FTP_PLUGIN_DIR' => WP_PLUGIN_DIR,
182 'FTP_LANG_DIR' => WP_LANG_DIR
185 // Direct matches ( folder = CONSTANT/ )
186 foreach ( $constant_overrides as $constant => $dir ) {
187 if ( ! defined( $constant ) )
189 if ( $folder === $dir )
190 return trailingslashit( constant( $constant ) );
193 // Prefix Matches ( folder = CONSTANT/subdir )
194 foreach ( $constant_overrides as $constant => $dir ) {
195 if ( ! defined( $constant ) )
197 if ( 0 === stripos( $folder, $dir ) ) { // $folder starts with $dir
198 $potential_folder = preg_replace( '#^' . preg_quote( $dir, '#' ) . '/#i', trailingslashit( constant( $constant ) ), $folder );
199 $potential_folder = trailingslashit( $potential_folder );
201 if ( $this->is_dir( $potential_folder ) ) {
202 $this->cache[ $folder ] = $potential_folder;
203 return $potential_folder;
207 } elseif ( 'direct' == $this->method ) {
208 $folder = str_replace('\\', '/', $folder); // Windows path sanitisation
209 return trailingslashit($folder);
212 $folder = preg_replace('|^([a-z]{1}):|i', '', $folder); // Strip out windows drive letter if it's there.
213 $folder = str_replace('\\', '/', $folder); // Windows path sanitisation
215 if ( isset($this->cache[ $folder ] ) )
216 return $this->cache[ $folder ];
218 if ( $this->exists($folder) ) { // Folder exists at that absolute path.
219 $folder = trailingslashit($folder);
220 $this->cache[ $folder ] = $folder;
223 if ( $return = $this->search_for_folder($folder) )
224 $this->cache[ $folder ] = $return;
229 * Locate a folder on the remote filesystem.
231 * Expects Windows sanitized path.
235 * @param string $folder The folder to locate.
236 * @param string $base The folder to start searching from.
237 * @param bool $loop If the function has recursed, Internal use only.
238 * @return string|false The location of the remote path, false to cease looping.
240 public function search_for_folder( $folder, $base = '.', $loop = false ) {
241 if ( empty( $base ) || '.' == $base )
242 $base = trailingslashit($this->cwd());
244 $folder = untrailingslashit($folder);
246 if ( $this->verbose )
247 printf( "\n" . __('Looking for %1$s in %2$s') . "<br/>\n", $folder, $base );
249 $folder_parts = explode('/', $folder);
250 $folder_part_keys = array_keys( $folder_parts );
251 $last_index = array_pop( $folder_part_keys );
252 $last_path = $folder_parts[ $last_index ];
254 $files = $this->dirlist( $base );
256 foreach ( $folder_parts as $index => $key ) {
257 if ( $index == $last_index )
258 continue; // We want this to be caught by the next code block.
261 * Working from /home/ to /user/ to /wordpress/ see if that file exists within
262 * the current folder, If it's found, change into it and follow through looking
263 * for it. If it cant find WordPress down that route, it'll continue onto the next
264 * folder level, and see if that matches, and so on. If it reaches the end, and still
265 * cant find it, it'll return false for the entire function.
267 if ( isset($files[ $key ]) ){
269 // Let's try that folder:
270 $newdir = trailingslashit(path_join($base, $key));
271 if ( $this->verbose )
272 printf( "\n" . __('Changing to %s') . "<br/>\n", $newdir );
274 // Only search for the remaining path tokens in the directory, not the full path again.
275 $newfolder = implode( '/', array_slice( $folder_parts, $index + 1 ) );
276 if ( $ret = $this->search_for_folder( $newfolder, $newdir, $loop) )
281 // 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.
282 if (isset( $files[ $last_path ] ) ) {
283 if ( $this->verbose )
284 printf( "\n" . __('Found %s') . "<br/>\n", $base . $last_path );
285 return trailingslashit($base . $last_path);
288 // Prevent this function from looping again.
289 // No need to proceed if we've just searched in /
290 if ( $loop || '/' == $base )
293 // As an extra last resort, Change back to / if the folder wasn't found.
294 // This comes into effect when the CWD is /home/user/ but WP is at /var/www/....
295 return $this->search_for_folder( $folder, '/', true );
300 * Return the *nix-style file permissions for a file.
302 * From the PHP documentation page for fileperms().
304 * @link http://docs.php.net/fileperms
309 * @param string $file String filename.
310 * @return string The *nix-style representation of permissions.
312 public function gethchmod( $file ){
313 $perms = intval( $this->getchmod( $file ), 8 );
314 if (($perms & 0xC000) == 0xC000) // Socket
316 elseif (($perms & 0xA000) == 0xA000) // Symbolic Link
318 elseif (($perms & 0x8000) == 0x8000) // Regular
320 elseif (($perms & 0x6000) == 0x6000) // Block special
322 elseif (($perms & 0x4000) == 0x4000) // Directory
324 elseif (($perms & 0x2000) == 0x2000) // Character special
326 elseif (($perms & 0x1000) == 0x1000) // FIFO pipe
332 $info .= (($perms & 0x0100) ? 'r' : '-');
333 $info .= (($perms & 0x0080) ? 'w' : '-');
334 $info .= (($perms & 0x0040) ?
335 (($perms & 0x0800) ? 's' : 'x' ) :
336 (($perms & 0x0800) ? 'S' : '-'));
339 $info .= (($perms & 0x0020) ? 'r' : '-');
340 $info .= (($perms & 0x0010) ? 'w' : '-');
341 $info .= (($perms & 0x0008) ?
342 (($perms & 0x0400) ? 's' : 'x' ) :
343 (($perms & 0x0400) ? 'S' : '-'));
346 $info .= (($perms & 0x0004) ? 'r' : '-');
347 $info .= (($perms & 0x0002) ? 'w' : '-');
348 $info .= (($perms & 0x0001) ?
349 (($perms & 0x0200) ? 't' : 'x' ) :
350 (($perms & 0x0200) ? 'T' : '-'));
355 * Gets the permissions of the specified file or filepath in their octal format
358 * @param string $file
359 * @return string the last 3 characters of the octal number
361 public function getchmod( $file ) {
366 * Convert *nix-style file permissions to a octal number.
368 * Converts '-rw-r--r--' to 0644
369 * From "info at rvgate dot nl"'s comment on the PHP documentation for chmod()
371 * @link http://docs.php.net/manual/en/function.chmod.php#49614
376 * @param string $mode string The *nix-style file permission.
377 * @return int octal representation
379 public function getnumchmodfromh( $mode ) {
381 $legal = array('', 'w', 'r', 'x', '-');
382 $attarray = preg_split('//', $mode);
384 for ( $i = 0, $c = count( $attarray ); $i < $c; $i++ ) {
385 if ($key = array_search($attarray[$i], $legal)) {
386 $realmode .= $legal[$key];
390 $mode = str_pad($realmode, 10, '-', STR_PAD_LEFT);
391 $trans = array('-'=>'0', 'r'=>'4', 'w'=>'2', 'x'=>'1');
392 $mode = strtr($mode,$trans);
395 $newmode .= $mode[1] + $mode[2] + $mode[3];
396 $newmode .= $mode[4] + $mode[5] + $mode[6];
397 $newmode .= $mode[7] + $mode[8] + $mode[9];
402 * Determine if the string provided contains binary characters.
406 * @param string $text String to test against.
407 * @return bool true if string is binary, false otherwise.
409 public function is_binary( $text ) {
410 return (bool) preg_match( '|[^\x20-\x7E]|', $text ); // chr(32)..chr(127)
414 * Change the ownership of a file / folder.
416 * Default behavior is to do nothing, override this in your subclass, if desired.
420 * @param string $file Path to the file.
421 * @param mixed $owner A user name or number.
422 * @param bool $recursive Optional. If set True changes file owner recursivly. Defaults to False.
423 * @return bool Returns true on success or false on failure.
425 public function chown( $file, $owner, $recursive = false ) {
430 * Connect filesystem.
434 * @return bool True on success or false on failure (always true for WP_Filesystem_Direct).
436 public function connect() {
441 * Read entire file into a string.
445 * @param string $file Name of the file to read.
446 * @return mixed|bool Returns the read data or false on failure.
448 public function get_contents( $file ) {
453 * Read entire file into an array.
457 * @param string $file Path to the file.
458 * @return array|bool the file contents in an array or false on failure.
460 public function get_contents_array( $file ) {
465 * Write a string to a file.
469 * @param string $file Remote path to the file where to write the data.
470 * @param string $contents The data to write.
471 * @param int $mode Optional. The file permissions as octal number, usually 0644.
472 * @return bool False on failure.
474 public function put_contents( $file, $contents, $mode = false ) {
479 * Get the current working directory.
483 * @return string|bool The current working directory on success, or false on failure.
485 public function cwd() {
490 * Change current directory.
494 * @param string $dir The new current directory.
495 * @return bool|string
497 public function chdir( $dir ) {
502 * Change the file group.
506 * @param string $file Path to the file.
507 * @param mixed $group A group name or number.
508 * @param bool $recursive Optional. If set True changes file group recursively. Defaults to False.
509 * @return bool|string
511 public function chgrp( $file, $group, $recursive = false ) {
516 * Change filesystem permissions.
520 * @param string $file Path to the file.
521 * @param int $mode Optional. The permissions as octal number, usually 0644 for files, 0755 for dirs.
522 * @param bool $recursive Optional. If set True changes file group recursively. Defaults to False.
523 * @return bool|string
525 public function chmod( $file, $mode = false, $recursive = false ) {
530 * Get the file owner.
534 * @param string $file Path to the file.
535 * @return string|bool Username of the user or false on error.
537 public function owner( $file ) {
542 * Get the file's group.
546 * @param string $file Path to the file.
547 * @return string|bool The group or false on error.
549 public function group( $file ) {
558 * @param string $source Path to the source file.
559 * @param string $destination Path to the destination file.
560 * @param bool $overwrite Optional. Whether to overwrite the destination file if it exists.
562 * @param int $mode Optional. The permissions as octal number, usually 0644 for files, 0755 for dirs.
564 * @return bool True if file copied successfully, False otherwise.
566 public function copy( $source, $destination, $overwrite = false, $mode = false ) {
575 * @param string $source Path to the source file.
576 * @param string $destination Path to the destination file.
577 * @param bool $overwrite Optional. Whether to overwrite the destination file if it exists.
579 * @return bool True if file copied successfully, False otherwise.
581 public function move( $source, $destination, $overwrite = false ) {
586 * Delete a file or directory.
590 * @param string $file Path to the file.
591 * @param bool $recursive Optional. If set True changes file group recursively. Defaults to False.
593 * @param bool $type Type of resource. 'f' for file, 'd' for directory.
595 * @return bool True if the file or directory was deleted, false on failure.
597 public function delete( $file, $recursive = false, $type = false ) {
602 * Check if a file or directory exists.
606 * @param string $file Path to file/directory.
607 * @return bool Whether $file exists or not.
609 public function exists( $file ) {
614 * Check if resource is a file.
618 * @param string $file File path.
619 * @return bool Whether $file is a file.
621 public function is_file( $file ) {
626 * Check if resource is a directory.
630 * @param string $path Directory path.
631 * @return bool Whether $path is a directory.
633 public function is_dir( $path ) {
638 * Check if a file is readable.
642 * @param string $file Path to file.
643 * @return bool Whether $file is readable.
645 public function is_readable( $file ) {
650 * Check if a file or directory is writable.
654 * @return bool Whether $file is writable.
656 public function is_writable( $file ) {
661 * Gets the file's last access time.
665 * @param string $file Path to file.
666 * @return int|bool Unix timestamp representing last access time.
668 public function atime( $file ) {
673 * Gets the file modification time.
677 * @param string $file Path to file.
678 * @return int|bool Unix timestamp representing modification time.
680 public function mtime( $file ) {
685 * Gets the file size (in bytes).
689 * @param string $file Path to file.
690 * @return int|bool Size of the file in bytes.
692 public function size( $file ) {
697 * Set the access and modification times of a file.
699 * Note: If $file doesn't exist, it will be created.
703 * @param string $file Path to file.
704 * @param int $time Optional. Modified time to set for file.
706 * @param int $atime Optional. Access time to set for file.
708 * @return bool Whether operation was successful or not.
710 public function touch( $file, $time = 0, $atime = 0 ) {
715 * Create a directory.
719 * @param string $path Path for new directory.
720 * @param mixed $chmod Optional. The permissions as octal number, (or False to skip chmod)
722 * @param mixed $chown Optional. A user name or number (or False to skip chown)
724 * @param mixed $chgrp Optional. A group name or number (or False to skip chgrp).
726 * @return bool False if directory cannot be created, true otherwise.
728 public function mkdir( $path, $chmod = false, $chown = false, $chgrp = false ) {
733 * Delete a directory.
737 * @param string $path Path to directory.
738 * @param bool $recursive Optional. Whether to recursively remove files/directories.
740 * @return bool Whether directory is deleted successfully or not.
742 public function rmdir( $path, $recursive = false ) {
747 * Get details for files in a directory or a specific file.
752 * @param string $path Path to directory or file.
753 * @param bool $include_hidden Optional. Whether to include details of hidden ("." prefixed) files.
755 * @param bool $recursive Optional. Whether to recursively include file details in nested directories.
757 * @return array|bool {
758 * Array of files. False if unable to list directory contents.
760 * @type string $name Name of the file/directory.
761 * @type string $perms *nix representation of permissions.
762 * @type int $permsn Octal representation of permissions.
763 * @type string $owner Owner name or ID.
764 * @type int $size Size of file in bytes.
765 * @type int $lastmodunix Last modified unix timestamp.
766 * @type mixed $lastmod Last modified month (3 letter) and day (without leading 0).
767 * @type int $time Last modified time.
768 * @type string $type Type of resource. 'f' for file, 'd' for directory.
769 * @type mixed $files If a directory and $recursive is true, contains another array of files.
772 public function dirlist( $path, $include_hidden = true, $recursive = false ) {
776 } // WP_Filesystem_Base