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.
24 * Cached list of local filepaths to mapped 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 $folder = $this->find_folder(ABSPATH);
50 //Perhaps the FTP folder is rooted at the WordPress install, Check for wp-includes folder in root, Could have some false positives, but rare.
51 if ( ! $folder && $this->is_dir('/wp-includes') )
56 * Returns the path on the remote filesystem of WP_CONTENT_DIR
60 * @return string The location of the remote path.
62 function wp_content_dir() {
63 return $this->find_folder(WP_CONTENT_DIR);
66 * Returns the path on the remote filesystem of WP_PLUGIN_DIR
71 * @return string The location of the remote path.
73 function wp_plugins_dir() {
74 return $this->find_folder(WP_PLUGIN_DIR);
77 * Returns the path on the remote filesystem of the Themes Directory
82 * @return string The location of the remote path.
84 function wp_themes_dir() {
85 return $this->wp_content_dir() . 'themes/';
88 * Returns the path on the remote filesystem of WP_LANG_DIR
93 * @return string The location of the remote path.
95 function wp_lang_dir() {
96 return $this->find_folder(WP_LANG_DIR);
100 * Locates a folder on the remote filesystem.
102 * Deprecated; use WP_Filesystem::abspath() or WP_Filesystem::wp_*_dir() methods instead.
108 * @param string $base The folder to start searching from
109 * @param bool $echo True to display debug information
110 * @return string The location of the remote path.
112 function find_base_dir($base = '.', $echo = false) {
113 _deprecated_function(__FUNCTION__, '2.7', 'WP_Filesystem::abspath() or WP_Filesystem::wp_*_dir()' );
114 $this->verbose = $echo;
115 return $this->abspath();
118 * Locates a folder on the remote filesystem.
120 * Deprecated; use WP_Filesystem::abspath() or WP_Filesystem::wp_*_dir() methods instead.
126 * @param string $base The folder to start searching from
127 * @param bool $echo True to display debug information
128 * @return string The location of the remote path.
130 function get_base_dir($base = '.', $echo = false) {
131 _deprecated_function(__FUNCTION__, '2.7', 'WP_Filesystem::abspath() or WP_Filesystem::wp_*_dir()' );
132 $this->verbose = $echo;
133 return $this->abspath();
137 * Locates a folder on the remote filesystem.
139 * Assumes that on Windows systems, Stripping off the Drive letter is OK
140 * Sanitizes \\ to / in windows filepaths.
145 * @param string $folder the folder to locate
146 * @return string The location of the remote path.
148 function find_folder($folder) {
150 if ( strpos($this->method, 'ftp') !== false ) {
151 $constant_overrides = array( 'FTP_BASE' => ABSPATH, 'FTP_CONTENT_DIR' => WP_CONTENT_DIR, 'FTP_PLUGIN_DIR' => WP_PLUGIN_DIR, 'FTP_LANG_DIR' => WP_LANG_DIR );
152 foreach ( $constant_overrides as $constant => $dir )
153 if ( defined($constant) && $folder === $dir )
154 return trailingslashit(constant($constant));
155 } elseif ( 'direct' == $this->method ) {
156 $folder = str_replace('\\', '/', $folder); //Windows path sanitisation
157 return trailingslashit($folder);
160 $folder = preg_replace('|^([a-z]{1}):|i', '', $folder); //Strip out windows drive letter if it's there.
161 $folder = str_replace('\\', '/', $folder); //Windows path sanitisation
163 if ( isset($this->cache[ $folder ] ) )
164 return $this->cache[ $folder ];
166 if ( $this->exists($folder) ) { //Folder exists at that absolute path.
167 $folder = trailingslashit($folder);
168 $this->cache[ $folder ] = $folder;
171 if ( $return = $this->search_for_folder($folder) )
172 $this->cache[ $folder ] = $return;
177 * Locates a folder on the remote filesystem.
179 * Expects Windows sanitized path
184 * @param string $folder the folder to locate
185 * @param string $base the folder to start searching from
186 * @param bool $loop if the function has recursed, Internal use only
187 * @return string The location of the remote path.
189 function search_for_folder($folder, $base = '.', $loop = false ) {
190 if ( empty( $base ) || '.' == $base )
191 $base = trailingslashit($this->cwd());
193 $folder = untrailingslashit($folder);
195 $folder_parts = explode('/', $folder);
196 $last_path = $folder_parts[ count($folder_parts) - 1 ];
198 $files = $this->dirlist( $base );
200 foreach ( $folder_parts as $key ) {
201 if ( $key == $last_path )
202 continue; //We want this to be caught by the next code block.
204 //Working from /home/ to /user/ to /wordpress/ see if that file exists within the current folder,
205 // If its found, change into it and follow through looking for it.
206 // If it cant find WordPress down that route, it'll continue onto the next folder level, and see if that matches, and so on.
207 // If it reaches the end, and still cant find it, it'll return false for the entire function.
208 if ( isset($files[ $key ]) ){
209 //Lets try that folder:
210 $newdir = trailingslashit(path_join($base, $key));
211 if ( $this->verbose )
212 printf( __('Changing to %s') . '<br/>', $newdir );
213 if ( $ret = $this->search_for_folder( $folder, $newdir, $loop) )
218 //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.
219 if (isset( $files[ $last_path ] ) ) {
220 if ( $this->verbose )
221 printf( __('Found %s') . '<br/>', $base . $last_path );
222 return trailingslashit($base . $last_path);
225 return false; //Prevent this function from looping again.
226 //As an extra last resort, Change back to / if the folder wasn't found. This comes into effect when the CWD is /home/user/ but WP is at /var/www/.... mainly dedicated setups.
227 return $this->search_for_folder($folder, '/', true);
232 * Returns the *nix style file permissions for a file
234 * From the PHP documentation page for fileperms()
236 * @link http://docs.php.net/fileperms
240 * @param string $file string filename
241 * @return int octal representation of permissions
243 function gethchmod($file){
244 $perms = $this->getchmod($file);
245 if (($perms & 0xC000) == 0xC000) // Socket
247 elseif (($perms & 0xA000) == 0xA000) // Symbolic Link
249 elseif (($perms & 0x8000) == 0x8000) // Regular
251 elseif (($perms & 0x6000) == 0x6000) // Block special
253 elseif (($perms & 0x4000) == 0x4000) // Directory
255 elseif (($perms & 0x2000) == 0x2000) // Character special
257 elseif (($perms & 0x1000) == 0x1000) // FIFO pipe
263 $info .= (($perms & 0x0100) ? 'r' : '-');
264 $info .= (($perms & 0x0080) ? 'w' : '-');
265 $info .= (($perms & 0x0040) ?
266 (($perms & 0x0800) ? 's' : 'x' ) :
267 (($perms & 0x0800) ? 'S' : '-'));
270 $info .= (($perms & 0x0020) ? 'r' : '-');
271 $info .= (($perms & 0x0010) ? 'w' : '-');
272 $info .= (($perms & 0x0008) ?
273 (($perms & 0x0400) ? 's' : 'x' ) :
274 (($perms & 0x0400) ? 'S' : '-'));
277 $info .= (($perms & 0x0004) ? 'r' : '-');
278 $info .= (($perms & 0x0002) ? 'w' : '-');
279 $info .= (($perms & 0x0001) ?
280 (($perms & 0x0200) ? 't' : 'x' ) :
281 (($perms & 0x0200) ? 'T' : '-'));
286 * Converts *nix style file permissions to a octal number.
288 * Converts '-rw-r--r--' to 0644
289 * From "info at rvgate dot nl"'s comment on the PHP documentation for chmod()
291 * @link http://docs.php.net/manual/en/function.chmod.php#49614
295 * @param string $mode string *nix style file permission
296 * @return int octal representation
298 function getnumchmodfromh($mode) {
300 $legal = array('', 'w', 'r', 'x', '-');
301 $attarray = preg_split('//', $mode);
303 for ($i=0; $i < count($attarray); $i++)
304 if ($key = array_search($attarray[$i], $legal))
305 $realmode .= $legal[$key];
307 $mode = str_pad($realmode, 9, '-');
308 $trans = array('-'=>'0', 'r'=>'4', 'w'=>'2', 'x'=>'1');
309 $mode = strtr($mode,$trans);
312 $newmode .= $mode[0] + $mode[1] + $mode[2];
313 $newmode .= $mode[3] + $mode[4] + $mode[5];
314 $newmode .= $mode[6] + $mode[7] + $mode[8];
319 * Determines if the string provided contains binary characters.
324 * @param string $text String to test against
325 * @return bool true if string is binary, false otherwise
327 function is_binary( $text ) {
328 return (bool) preg_match('|[^\x20-\x7E]|', $text); //chr(32)..chr(127)