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 or not.
24 * Cached list of local filepaths to maped remote filepaths.
33 * The Access method of the current connection, Set automatically.
42 * Returns the path on the remote filesystem of ABSPATH
46 * @return string The location of the remote path.
49 if ( defined('FTP_BASE') && strpos($this->method, 'ftp') !== false )
51 $folder = $this->find_folder(ABSPATH);
52 //Perhaps the FTP folder is rooted at the WordPress install, Check for wp-includes folder in root, Could have some false positives, but rare.
53 if ( ! $folder && $this->is_dir('/wp-includes') )
58 * Returns the path on the remote filesystem of WP_CONTENT_DIR
62 * @return string The location of the remote path.
64 function wp_content_dir() {
65 if ( defined('FTP_CONTENT_DIR') && strpos($this->method, 'ftp') !== false )
66 return FTP_CONTENT_DIR;
67 return $this->find_folder(WP_CONTENT_DIR);
70 * Returns the path on the remote filesystem of WP_PLUGIN_DIR
75 * @return string The location of the remote path.
77 function wp_plugins_dir() {
78 if ( defined('FTP_PLUGIN_DIR') && strpos($this->method, 'ftp') !== false )
79 return FTP_PLUGIN_DIR;
80 return $this->find_folder(WP_PLUGIN_DIR);
83 * Returns the path on the remote filesystem of the Themes Directory
88 * @return string The location of the remote path.
90 function wp_themes_dir() {
91 return $this->wp_content_dir() . '/themes';
95 * Locates a folder on the remote filesystem.
97 * Deprecated; use WP_Filesystem::abspath() or WP_Filesystem::wp_*_dir() methods instead.
103 * @param string $base The folder to start searching from
104 * @param bool $echo True to display debug information
105 * @return string The location of the remote path.
107 function find_base_dir($base = '.', $echo = false) {
108 _deprecated_function(__FUNCTION__, '2.7', 'WP_Filesystem::abspath() or WP_Filesystem::wp_*_dir()' );
109 $this->verbose = $echo;
110 return $this->abspath();
113 * Locates a folder on the remote filesystem.
115 * Deprecated; use WP_Filesystem::abspath() or WP_Filesystem::wp_*_dir() methods instead.
121 * @param string $base The folder to start searching from
122 * @param bool $echo True to display debug information
123 * @return string The location of the remote path.
125 function get_base_dir($base = '.', $echo = false) {
126 _deprecated_function(__FUNCTION__, '2.7', 'WP_Filesystem::abspath() or WP_Filesystem::wp_*_dir()' );
127 $this->verbose = $echo;
128 return $this->abspath();
132 * Locates a folder on the remote filesystem.
134 * Assumes that on Windows systems, Stripping off the Drive letter is OK
135 * Sanitizes \\ to / in windows filepaths.
140 * @param string $folder the folder to locate
141 * @return string The location of the remote path.
143 function find_folder($folder) {
145 $folder = preg_replace('|^([a-z]{1}):|i', '', $folder); //Strip out windows driveletter if its there.
146 $folder = str_replace('\\', '/', $folder); //Windows path sanitiation
148 if ( isset($this->cache[ $folder ] ) )
149 return $this->cache[ $folder ];
151 if ( $this->exists($folder) ) { //Folder exists at that absolute path.
152 $this->cache[ $folder ] = $folder;
155 if( $return = $this->search_for_folder($folder) )
156 $this->cache[ $folder ] = $return;
161 * Locates a folder on the remote filesystem.
163 * Expects Windows sanitized path
168 * @param string $folder the folder to locate
169 * @param string $base the folder to start searching from
170 * @param bool $loop if the function has recursed, Internal use only
171 * @return string The location of the remote path.
173 function search_for_folder($folder, $base = '.', $loop = false ) {
174 if ( empty( $base ) || '.' == $base )
175 $base = trailingslashit($this->cwd());
177 $folder = untrailingslashit($folder);
179 $folder_parts = explode('/', $folder);
180 $last_path = $folder_parts[ count($folder_parts) - 1 ];
182 $files = $this->dirlist( $base );
184 foreach ( $folder_parts as $key ) {
185 if ( $key == $last_path )
186 continue; //We want this to be caught by the next code block.
188 //Working from /home/ to /user/ to /wordpress/ see if that file exists within the current folder,
189 // If its found, change into it and follow through looking for it.
190 // If it cant find WordPress down that route, it'll continue onto the next folder level, and see if that matches, and so on.
191 // If it reaches the end, and still cant find it, it'll return false for the entire function.
192 if( isset($files[ $key ]) ){
193 //Lets try that folder:
194 $newdir = trailingslashit(path_join($base, $key));
196 printf( __('Changing to %s') . '<br/>', $newdir );
197 if( $ret = $this->search_for_folder( $folder, $newdir, $loop) )
202 //Only check this as a last resort, to prevent locating the incorrect install. All above proceeedures will fail quickly if this is the right branch to take.
203 if(isset( $files[ $last_path ] ) ) {
205 printf( __('Found %s') . '<br/>', $base . $last_path );
206 return $base . $last_path;
209 return false;//Prevent tihs function looping again.
210 //As an extra last resort, Change back to / if the folder wasnt found. This comes into effect when the CWD is /home/user/ but WP is at /var/www/.... mainly dedicated setups.
211 return $this->search_for_folder($folder, '/', true);
216 * Returns the *nix style file permissions for a file
218 * From the PHP documentation page for fileperms()
220 * @link http://docs.php.net/fileperms
224 * @param string $file string filename
225 * @return int octal representation of permissions
227 function gethchmod($file){
228 $perms = $this->getchmod($file);
229 if (($perms & 0xC000) == 0xC000) // Socket
231 elseif (($perms & 0xA000) == 0xA000) // Symbolic Link
233 elseif (($perms & 0x8000) == 0x8000) // Regular
235 elseif (($perms & 0x6000) == 0x6000) // Block special
237 elseif (($perms & 0x4000) == 0x4000) // Directory
239 elseif (($perms & 0x2000) == 0x2000) // Character special
241 elseif (($perms & 0x1000) == 0x1000)// FIFO pipe
247 $info .= (($perms & 0x0100) ? 'r' : '-');
248 $info .= (($perms & 0x0080) ? 'w' : '-');
249 $info .= (($perms & 0x0040) ?
250 (($perms & 0x0800) ? 's' : 'x' ) :
251 (($perms & 0x0800) ? 'S' : '-'));
254 $info .= (($perms & 0x0020) ? 'r' : '-');
255 $info .= (($perms & 0x0010) ? 'w' : '-');
256 $info .= (($perms & 0x0008) ?
257 (($perms & 0x0400) ? 's' : 'x' ) :
258 (($perms & 0x0400) ? 'S' : '-'));
261 $info .= (($perms & 0x0004) ? 'r' : '-');
262 $info .= (($perms & 0x0002) ? 'w' : '-');
263 $info .= (($perms & 0x0001) ?
264 (($perms & 0x0200) ? 't' : 'x' ) :
265 (($perms & 0x0200) ? 'T' : '-'));
270 * Converts *nix style file permissions to a octal number.
272 * Converts '-rw-r--r--' to 0644
273 * From "info at rvgate dot nl"'s comment on the PHP documentation for chmod()
275 * @link http://docs.php.net/manual/en/function.chmod.php#49614
279 * @param string $mode string *nix style file permission
280 * @return int octal representation
282 function getnumchmodfromh($mode) {
284 $legal = array('', 'w', 'r', 'x', '-');
285 $attarray = preg_split('//', $mode);
287 for($i=0; $i < count($attarray); $i++)
288 if($key = array_search($attarray[$i], $legal))
289 $realmode .= $legal[$key];
291 $mode = str_pad($realmode, 9, '-');
292 $trans = array('-'=>'0', 'r'=>'4', 'w'=>'2', 'x'=>'1');
293 $mode = strtr($mode,$trans);
296 $newmode .= $mode[0] + $mode[1] + $mode[2];
297 $newmode .= $mode[3] + $mode[4] + $mode[5];
298 $newmode .= $mode[6] + $mode[7] + $mode[8];
303 * Determines if the string provided contains binary characters.
308 * @param string $text String to test against
309 * @return bool true if string is binary, false otherwise
311 function is_binary( $text ) {
312 return (bool) preg_match('|[^\x20-\x7E]|', $text); //chr(32)..chr(127)