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 ) {
175 if ( isset( $this->cache[ $folder ] ) )
176 return $this->cache[ $folder ];
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
186 // Direct matches ( folder = CONSTANT/ )
187 foreach ( $constant_overrides as $constant => $dir ) {
188 if ( ! defined( $constant ) )
190 if ( $folder === $dir )
191 return trailingslashit( constant( $constant ) );
194 // Prefix Matches ( folder = CONSTANT/subdir )
195 foreach ( $constant_overrides as $constant => $dir ) {
196 if ( ! defined( $constant ) )
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 );
202 if ( $this->is_dir( $potential_folder ) ) {
203 $this->cache[ $folder ] = $potential_folder;
204 return $potential_folder;
208 } elseif ( 'direct' == $this->method ) {
209 $folder = str_replace('\\', '/', $folder); // Windows path sanitisation
210 return trailingslashit($folder);
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
216 if ( isset($this->cache[ $folder ] ) )
217 return $this->cache[ $folder ];
219 if ( $this->exists($folder) ) { // Folder exists at that absolute path.
220 $folder = trailingslashit($folder);
221 $this->cache[ $folder ] = $folder;
224 if ( $return = $this->search_for_folder($folder) )
225 $this->cache[ $folder ] = $return;
230 * Locate a folder on the remote filesystem.
232 * Expects Windows sanitized path.
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.
241 public function search_for_folder( $folder, $base = '.', $loop = false ) {
242 if ( empty( $base ) || '.' == $base )
243 $base = trailingslashit($this->cwd());
245 $folder = untrailingslashit($folder);
247 if ( $this->verbose )
248 printf( "\n" . __('Looking for %1$s in %2$s') . "<br/>\n", $folder, $base );
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 ];
255 $files = $this->dirlist( $base );
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.
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.
268 if ( isset($files[ $key ]) ){
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 );
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) )
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);
289 // Prevent this function from looping again.
290 // No need to proceed if we've just searched in /
291 if ( $loop || '/' == $base )
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 );
301 * Return the *nix-style file permissions for a file.
303 * From the PHP documentation page for fileperms().
305 * @link http://docs.php.net/fileperms
310 * @param string $file String filename.
311 * @return string The *nix-style representation of permissions.
313 public function gethchmod( $file ){
314 $perms = $this->getchmod($file);
315 if (($perms & 0xC000) == 0xC000) // Socket
317 elseif (($perms & 0xA000) == 0xA000) // Symbolic Link
319 elseif (($perms & 0x8000) == 0x8000) // Regular
321 elseif (($perms & 0x6000) == 0x6000) // Block special
323 elseif (($perms & 0x4000) == 0x4000) // Directory
325 elseif (($perms & 0x2000) == 0x2000) // Character special
327 elseif (($perms & 0x1000) == 0x1000) // FIFO pipe
333 $info .= (($perms & 0x0100) ? 'r' : '-');
334 $info .= (($perms & 0x0080) ? 'w' : '-');
335 $info .= (($perms & 0x0040) ?
336 (($perms & 0x0800) ? 's' : 'x' ) :
337 (($perms & 0x0800) ? 'S' : '-'));
340 $info .= (($perms & 0x0020) ? 'r' : '-');
341 $info .= (($perms & 0x0010) ? 'w' : '-');
342 $info .= (($perms & 0x0008) ?
343 (($perms & 0x0400) ? 's' : 'x' ) :
344 (($perms & 0x0400) ? 'S' : '-'));
347 $info .= (($perms & 0x0004) ? 'r' : '-');
348 $info .= (($perms & 0x0002) ? 'w' : '-');
349 $info .= (($perms & 0x0001) ?
350 (($perms & 0x0200) ? 't' : 'x' ) :
351 (($perms & 0x0200) ? 'T' : '-'));
356 * Convert *nix-style file permissions to a octal number.
358 * Converts '-rw-r--r--' to 0644
359 * From "info at rvgate dot nl"'s comment on the PHP documentation for chmod()
361 * @link http://docs.php.net/manual/en/function.chmod.php#49614
366 * @param string $mode string The *nix-style file permission.
367 * @return int octal representation
369 public function getnumchmodfromh( $mode ) {
371 $legal = array('', 'w', 'r', 'x', '-');
372 $attarray = preg_split('//', $mode);
374 for ( $i = 0, $c = count( $attarray ); $i < $c; $i++ ) {
375 if ($key = array_search($attarray[$i], $legal)) {
376 $realmode .= $legal[$key];
380 $mode = str_pad($realmode, 10, '-', STR_PAD_LEFT);
381 $trans = array('-'=>'0', 'r'=>'4', 'w'=>'2', 'x'=>'1');
382 $mode = strtr($mode,$trans);
385 $newmode .= $mode[1] + $mode[2] + $mode[3];
386 $newmode .= $mode[4] + $mode[5] + $mode[6];
387 $newmode .= $mode[7] + $mode[8] + $mode[9];
392 * Determine if the string provided contains binary characters.
396 * @param string $text String to test against.
397 * @return bool true if string is binary, false otherwise.
399 public function is_binary( $text ) {
400 return (bool) preg_match( '|[^\x20-\x7E]|', $text ); // chr(32)..chr(127)
404 * Change the ownership of a file / folder.
406 * Default behavior is to do nothing, override this in your subclass, if desired.
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.
415 public function chown( $file, $owner, $recursive = false ) {
420 * Connect filesystem.
424 * @return bool True on success or false on failure (always true for WP_Filesystem_Direct).
426 public function connect() {
431 * Read entire file into a string.
435 * @param string $file Name of the file to read.
436 * @return mixed|bool Returns the read data or false on failure.
438 public function get_contents( $file ) {
443 * Read entire file into an array.
447 * @param string $file Path to the file.
448 * @return array|bool the file contents in an array or false on failure.
450 public function get_contents_array( $file ) {
455 * Write a string to a file.
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.
464 public function put_contents( $file, $contents, $mode = false ) {
469 * Get the current working directory.
473 * @return string|bool The current working directory on success, or false on failure.
475 public function cwd() {
480 * Change current directory.
484 * @param string $dir The new current directory.
485 * @return bool Returns true on success or false on failure.
487 public function chdir( $dir ) {
492 * Change the file group.
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.
501 public function chgrp( $file, $group, $recursive = false ) {
506 * Change filesystem permissions.
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.
515 public function chmod( $file, $mode = false, $recursive = false ) {
520 * Get the file owner.
524 * @param string $file Path to the file.
525 * @return string|bool Username of the user or false on error.
527 public function owner( $file ) {
532 * Get the file's group.
536 * @param string $file Path to the file.
537 * @return string|bool The group or false on error.
539 public function group( $file ) {
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.
552 * @param int $mode Optional. The permissions as octal number, usually 0644 for files, 0755 for dirs.
554 * @return bool True if file copied successfully, False otherwise.
556 public function copy( $source, $destination, $overwrite = false, $mode = false ) {
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.
569 * @return bool True if file copied successfully, False otherwise.
571 public function move( $source, $destination, $overwrite = false ) {
576 * Delete a file or directory.
580 * @param string $file Path to the file.
581 * @param bool $recursive Optional. If set True changes file group recursively. Defaults to False.
583 * @param bool $type Type of resource. 'f' for file, 'd' for directory.
585 * @return bool True if the file or directory was deleted, false on failure.
587 public function delete( $file, $recursive = false, $type = false ) {
592 * Check if a file or directory exists.
596 * @param string $file Path to file/directory.
597 * @return bool Whether $file exists or not.
599 public function exists( $file ) {
604 * Check if resource is a file.
608 * @param string $file File path.
609 * @return bool Whether $file is a file.
611 public function is_file( $file ) {
616 * Check if resource is a directory.
620 * @param string $path Directory path.
621 * @return bool Whether $path is a directory.
623 public function is_dir( $path ) {
628 * Check if a file is readable.
632 * @param string $file Path to file.
633 * @return bool Whether $file is readable.
635 public function is_readable( $file ) {
640 * Check if a file or directory is writable.
644 * @return bool Whether $file is writable.
646 public function is_writable( $file ) {
651 * Gets the file's last access time.
655 * @param string $file Path to file.
656 * @return int|bool Unix timestamp representing last access time.
658 public function atime( $file ) {
663 * Gets the file modification time.
667 * @param string $file Path to file.
668 * @return int|bool Unix timestamp representing modification time.
670 public function mtime( $file ) {
675 * Gets the file size (in bytes).
679 * @param string $file Path to file.
680 * @return int|bool Size of the file in bytes.
682 public function size( $file ) {
687 * Set the access and modification times of a file.
689 * Note: If $file doesn't exist, it will be created.
693 * @param string $file Path to file.
694 * @param int $time Optional. Modified time to set for file.
696 * @param int $atime Optional. Access time to set for file.
698 * @return bool Whether operation was successful or not.
700 public function touch( $file, $time = 0, $atime = 0 ) {
705 * Create a directory.
709 * @param string $path Path for new directory.
710 * @param mixed $chmod Optional. The permissions as octal number, (or False to skip chmod)
712 * @param mixed $chown Optional. A user name or number (or False to skip chown)
714 * @param mixed $chgrp Optional. A group name or number (or False to skip chgrp).
716 * @return bool False if directory cannot be created, true otherwise.
718 public function mkdir( $path, $chmod = false, $chown = false, $chgrp = false ) {
723 * Delete a directory.
727 * @param string $path Path to directory.
728 * @param bool $recursive Optional. Whether to recursively remove files/directories.
730 * @return bool Whether directory is deleted successfully or not.
732 public function rmdir( $path, $recursive = false ) {
737 * Get details for files in a directory or a specific file.
742 * @param string $path Path to directory or file.
743 * @param bool $include_hidden Optional. Whether to include details of hidden ("." prefixed) files.
745 * @param bool $recursive Optional. Whether to recursively include file details in nested directories.
747 * @return array|bool {
748 * Array of files. False if unable to list directory contents.
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.
762 public function dirlist( $path, $include_hidden = true, $recursive = false ) {
766 } // WP_Filesystem_Base