mp-wp_genesis 1 <?php
mp-wp_genesis 2 /**
mp-wp_genesis 3 * Base WordPress Filesystem.
mp-wp_genesis 4 *
mp-wp_genesis 5 * @package WordPress
mp-wp_genesis 6 * @subpackage Filesystem
mp-wp_genesis 7 */
mp-wp_genesis 8
mp-wp_genesis 9 /**
mp-wp_genesis 10 * Base WordPress Filesystem class for which Filesystem implementations extend
mp-wp_genesis 11 *
mp-wp_genesis 12 * @since 2.5
mp-wp_genesis 13 */
mp-wp_genesis 14 class WP_Filesystem_Base {
mp-wp_genesis 15 /**
mp-wp_genesis 16 * Whether to display debug data for the connection or not.
mp-wp_genesis 17 *
mp-wp_genesis 18 * @since 2.5
mp-wp_genesis 19 * @access public
mp-wp_genesis 20 * @var bool
mp-wp_genesis 21 */
mp-wp_genesis 22 var $verbose = false;
mp-wp_genesis 23 /**
mp-wp_genesis 24 * Cached list of local filepaths to maped remote filepaths.
mp-wp_genesis 25 *
mp-wp_genesis 26 * @since 2.7
mp-wp_genesis 27 * @access private
mp-wp_genesis 28 * @var array
mp-wp_genesis 29 */
mp-wp_genesis 30 var $cache = array();
mp-wp_genesis 31
mp-wp_genesis 32 /**
mp-wp_genesis 33 * The Access method of the current connection, Set automatically.
mp-wp_genesis 34 *
mp-wp_genesis 35 * @since 2.5
mp-wp_genesis 36 * @access public
mp-wp_genesis 37 * @var string
mp-wp_genesis 38 */
mp-wp_genesis 39 var $method = '';
mp-wp_genesis 40
mp-wp_genesis 41 /**
mp-wp_genesis 42 * Returns the path on the remote filesystem of ABSPATH
mp-wp_genesis 43 *
mp-wp_genesis 44 * @since 2.7
mp-wp_genesis 45 * @access public
mp-wp_genesis 46 * @return string The location of the remote path.
mp-wp_genesis 47 */
mp-wp_genesis 48 function abspath() {
mp-wp_genesis 49 if ( defined('FTP_BASE') && strpos($this->method, 'ftp') !== false )
mp-wp_genesis 50 return FTP_BASE;
mp-wp_genesis 51 $folder = $this->find_folder(ABSPATH);
mp-wp_genesis 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.
mp-wp_genesis 53 if ( ! $folder && $this->is_dir('/wp-includes') )
mp-wp_genesis 54 $folder = '/';
mp-wp_genesis 55 return $folder;
mp-wp_genesis 56 }
mp-wp_genesis 57 /**
mp-wp_genesis 58 * Returns the path on the remote filesystem of WP_CONTENT_DIR
mp-wp_genesis 59 *
mp-wp_genesis 60 * @since 2.7
mp-wp_genesis 61 * @access public
mp-wp_genesis 62 * @return string The location of the remote path.
mp-wp_genesis 63 */
mp-wp_genesis 64 function wp_content_dir() {
mp-wp_genesis 65 if ( defined('FTP_CONTENT_DIR') && strpos($this->method, 'ftp') !== false )
mp-wp_genesis 66 return FTP_CONTENT_DIR;
mp-wp_genesis 67 return $this->find_folder(WP_CONTENT_DIR);
mp-wp_genesis 68 }
mp-wp_genesis 69 /**
mp-wp_genesis 70 * Returns the path on the remote filesystem of WP_PLUGIN_DIR
mp-wp_genesis 71 *
mp-wp_genesis 72 * @since 2.7
mp-wp_genesis 73 * @access public
mp-wp_genesis 74 *
mp-wp_genesis 75 * @return string The location of the remote path.
mp-wp_genesis 76 */
mp-wp_genesis 77 function wp_plugins_dir() {
mp-wp_genesis 78 if ( defined('FTP_PLUGIN_DIR') && strpos($this->method, 'ftp') !== false )
mp-wp_genesis 79 return FTP_PLUGIN_DIR;
mp-wp_genesis 80 return $this->find_folder(WP_PLUGIN_DIR);
mp-wp_genesis 81 }
mp-wp_genesis 82 /**
mp-wp_genesis 83 * Returns the path on the remote filesystem of the Themes Directory
mp-wp_genesis 84 *
mp-wp_genesis 85 * @since 2.7
mp-wp_genesis 86 * @access public
mp-wp_genesis 87 *
mp-wp_genesis 88 * @return string The location of the remote path.
mp-wp_genesis 89 */
mp-wp_genesis 90 function wp_themes_dir() {
mp-wp_genesis 91 return $this->wp_content_dir() . '/themes';
mp-wp_genesis 92 }
mp-wp_genesis 93
mp-wp_genesis 94 /**
mp-wp_genesis 95 * Locates a folder on the remote filesystem.
mp-wp_genesis 96 *
mp-wp_genesis 97 * Deprecated; use WP_Filesystem::abspath() or WP_Filesystem::wp_*_dir() methods instead.
mp-wp_genesis 98 *
mp-wp_genesis 99 * @since 2.5
mp-wp_genesis 100 * @deprecated 2.7
mp-wp_genesis 101 * @access public
mp-wp_genesis 102 *
mp-wp_genesis 103 * @param string $base The folder to start searching from
mp-wp_genesis 104 * @param bool $echo True to display debug information
mp-wp_genesis 105 * @return string The location of the remote path.
mp-wp_genesis 106 */
mp-wp_genesis 107 function find_base_dir($base = '.', $echo = false) {
mp-wp_genesis 108 _deprecated_function(__FUNCTION__, '2.7', 'WP_Filesystem::abspath() or WP_Filesystem::wp_*_dir()' );
mp-wp_genesis 109 $this->verbose = $echo;
mp-wp_genesis 110 return $this->abspath();
mp-wp_genesis 111 }
mp-wp_genesis 112 /**
mp-wp_genesis 113 * Locates a folder on the remote filesystem.
mp-wp_genesis 114 *
mp-wp_genesis 115 * Deprecated; use WP_Filesystem::abspath() or WP_Filesystem::wp_*_dir() methods instead.
mp-wp_genesis 116 *
mp-wp_genesis 117 * @since 2.5
mp-wp_genesis 118 * @deprecated 2.7
mp-wp_genesis 119 * @access public
mp-wp_genesis 120 *
mp-wp_genesis 121 * @param string $base The folder to start searching from
mp-wp_genesis 122 * @param bool $echo True to display debug information
mp-wp_genesis 123 * @return string The location of the remote path.
mp-wp_genesis 124 */
mp-wp_genesis 125 function get_base_dir($base = '.', $echo = false) {
mp-wp_genesis 126 _deprecated_function(__FUNCTION__, '2.7', 'WP_Filesystem::abspath() or WP_Filesystem::wp_*_dir()' );
mp-wp_genesis 127 $this->verbose = $echo;
mp-wp_genesis 128 return $this->abspath();
mp-wp_genesis 129 }
mp-wp_genesis 130
mp-wp_genesis 131 /**
mp-wp_genesis 132 * Locates a folder on the remote filesystem.
mp-wp_genesis 133 *
mp-wp_genesis 134 * Assumes that on Windows systems, Stripping off the Drive letter is OK
mp-wp_genesis 135 * Sanitizes \\ to / in windows filepaths.
mp-wp_genesis 136 *
mp-wp_genesis 137 * @since 2.7
mp-wp_genesis 138 * @access public
mp-wp_genesis 139 *
mp-wp_genesis 140 * @param string $folder the folder to locate
mp-wp_genesis 141 * @return string The location of the remote path.
mp-wp_genesis 142 */
mp-wp_genesis 143 function find_folder($folder) {
mp-wp_genesis 144
mp-wp_genesis 145 $folder = preg_replace('|^([a-z]{1}):|i', '', $folder); //Strip out windows driveletter if its there.
mp-wp_genesis 146 $folder = str_replace('\\', '/', $folder); //Windows path sanitiation
mp-wp_genesis 147
mp-wp_genesis 148 if ( isset($this->cache[ $folder ] ) )
mp-wp_genesis 149 return $this->cache[ $folder ];
mp-wp_genesis 150
mp-wp_genesis 151 if ( $this->exists($folder) ) { //Folder exists at that absolute path.
mp-wp_genesis 152 $this->cache[ $folder ] = $folder;
mp-wp_genesis 153 return $folder;
mp-wp_genesis 154 }
mp-wp_genesis 155 if( $return = $this->search_for_folder($folder) )
mp-wp_genesis 156 $this->cache[ $folder ] = $return;
mp-wp_genesis 157 return $return;
mp-wp_genesis 158 }
mp-wp_genesis 159
mp-wp_genesis 160 /**
mp-wp_genesis 161 * Locates a folder on the remote filesystem.
mp-wp_genesis 162 *
mp-wp_genesis 163 * Expects Windows sanitized path
mp-wp_genesis 164 *
mp-wp_genesis 165 * @since 2.7
mp-wp_genesis 166 * @access private
mp-wp_genesis 167 *
mp-wp_genesis 168 * @param string $folder the folder to locate
mp-wp_genesis 169 * @param string $base the folder to start searching from
mp-wp_genesis 170 * @param bool $loop if the function has recursed, Internal use only
mp-wp_genesis 171 * @return string The location of the remote path.
mp-wp_genesis 172 */
mp-wp_genesis 173 function search_for_folder($folder, $base = '.', $loop = false ) {
mp-wp_genesis 174 if ( empty( $base ) || '.' == $base )
mp-wp_genesis 175 $base = trailingslashit($this->cwd());
mp-wp_genesis 176
mp-wp_genesis 177 $folder = untrailingslashit($folder);
mp-wp_genesis 178
mp-wp_genesis 179 $folder_parts = explode('/', $folder);
mp-wp_genesis 180 $last_path = $folder_parts[ count($folder_parts) - 1 ];
mp-wp_genesis 181
mp-wp_genesis 182 $files = $this->dirlist( $base );
mp-wp_genesis 183
mp-wp_genesis 184 foreach ( $folder_parts as $key ) {
mp-wp_genesis 185 if ( $key == $last_path )
mp-wp_genesis 186 continue; //We want this to be caught by the next code block.
mp-wp_genesis 187
mp-wp_genesis 188 //Working from /home/ to /user/ to /wordpress/ see if that file exists within the current folder,
mp-wp_genesis 189 // If its found, change into it and follow through looking for it.
mp-wp_genesis 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.
mp-wp_genesis 191 // If it reaches the end, and still cant find it, it'll return false for the entire function.
mp-wp_genesis 192 if( isset($files[ $key ]) ){
mp-wp_genesis 193 //Lets try that folder:
mp-wp_genesis 194 $newdir = trailingslashit(path_join($base, $key));
mp-wp_genesis 195 if( $this->verbose )
mp-wp_genesis 196 printf( __('Changing to %s') . '<br/>', $newdir );
mp-wp_genesis 197 if( $ret = $this->search_for_folder( $folder, $newdir, $loop) )
mp-wp_genesis 198 return $ret;
mp-wp_genesis 199 }
mp-wp_genesis 200 }
mp-wp_genesis 201
mp-wp_genesis 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.
mp-wp_genesis 203 if(isset( $files[ $last_path ] ) ) {
mp-wp_genesis 204 if( $this->verbose )
mp-wp_genesis 205 printf( __('Found %s') . '<br/>', $base . $last_path );
mp-wp_genesis 206 return $base . $last_path;
mp-wp_genesis 207 }
mp-wp_genesis 208 if( $loop )
mp-wp_genesis 209 return false;//Prevent tihs function looping again.
mp-wp_genesis 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.
mp-wp_genesis 211 return $this->search_for_folder($folder, '/', true);
mp-wp_genesis 212
mp-wp_genesis 213 }
mp-wp_genesis 214
mp-wp_genesis 215 /**
mp-wp_genesis 216 * Returns the *nix style file permissions for a file
mp-wp_genesis 217 *
mp-wp_genesis 218 * From the PHP documentation page for fileperms()
mp-wp_genesis 219 *
mp-wp_genesis 220 * @link http://docs.php.net/fileperms
mp-wp_genesis 221 * @since 2.5
mp-wp_genesis 222 * @access public
mp-wp_genesis 223 *
mp-wp_genesis 224 * @param string $file string filename
mp-wp_genesis 225 * @return int octal representation of permissions
mp-wp_genesis 226 */
mp-wp_genesis 227 function gethchmod($file){
mp-wp_genesis 228 $perms = $this->getchmod($file);
mp-wp_genesis 229 if (($perms & 0xC000) == 0xC000) // Socket
mp-wp_genesis 230 $info = 's';
mp-wp_genesis 231 elseif (($perms & 0xA000) == 0xA000) // Symbolic Link
mp-wp_genesis 232 $info = 'l';
mp-wp_genesis 233 elseif (($perms & 0x8000) == 0x8000) // Regular
mp-wp_genesis 234 $info = '-';
mp-wp_genesis 235 elseif (($perms & 0x6000) == 0x6000) // Block special
mp-wp_genesis 236 $info = 'b';
mp-wp_genesis 237 elseif (($perms & 0x4000) == 0x4000) // Directory
mp-wp_genesis 238 $info = 'd';
mp-wp_genesis 239 elseif (($perms & 0x2000) == 0x2000) // Character special
mp-wp_genesis 240 $info = 'c';
mp-wp_genesis 241 elseif (($perms & 0x1000) == 0x1000)// FIFO pipe
mp-wp_genesis 242 $info = 'p';
mp-wp_genesis 243 else // Unknown
mp-wp_genesis 244 $info = 'u';
mp-wp_genesis 245
mp-wp_genesis 246 // Owner
mp-wp_genesis 247 $info .= (($perms & 0x0100) ? 'r' : '-');
mp-wp_genesis 248 $info .= (($perms & 0x0080) ? 'w' : '-');
mp-wp_genesis 249 $info .= (($perms & 0x0040) ?
mp-wp_genesis 250 (($perms & 0x0800) ? 's' : 'x' ) :
mp-wp_genesis 251 (($perms & 0x0800) ? 'S' : '-'));
mp-wp_genesis 252
mp-wp_genesis 253 // Group
mp-wp_genesis 254 $info .= (($perms & 0x0020) ? 'r' : '-');
mp-wp_genesis 255 $info .= (($perms & 0x0010) ? 'w' : '-');
mp-wp_genesis 256 $info .= (($perms & 0x0008) ?
mp-wp_genesis 257 (($perms & 0x0400) ? 's' : 'x' ) :
mp-wp_genesis 258 (($perms & 0x0400) ? 'S' : '-'));
mp-wp_genesis 259
mp-wp_genesis 260 // World
mp-wp_genesis 261 $info .= (($perms & 0x0004) ? 'r' : '-');
mp-wp_genesis 262 $info .= (($perms & 0x0002) ? 'w' : '-');
mp-wp_genesis 263 $info .= (($perms & 0x0001) ?
mp-wp_genesis 264 (($perms & 0x0200) ? 't' : 'x' ) :
mp-wp_genesis 265 (($perms & 0x0200) ? 'T' : '-'));
mp-wp_genesis 266 return $info;
mp-wp_genesis 267 }
mp-wp_genesis 268
mp-wp_genesis 269 /**
mp-wp_genesis 270 * Converts *nix style file permissions to a octal number.
mp-wp_genesis 271 *
mp-wp_genesis 272 * Converts '-rw-r--r--' to 0644
mp-wp_genesis 273 * From "info at rvgate dot nl"'s comment on the PHP documentation for chmod()
mp-wp_genesis 274 *
mp-wp_genesis 275 * @link http://docs.php.net/manual/en/function.chmod.php#49614
mp-wp_genesis 276 * @since 2.5
mp-wp_genesis 277 * @access public
mp-wp_genesis 278 *
mp-wp_genesis 279 * @param string $mode string *nix style file permission
mp-wp_genesis 280 * @return int octal representation
mp-wp_genesis 281 */
mp-wp_genesis 282 function getnumchmodfromh($mode) {
mp-wp_genesis 283 $realmode = '';
mp-wp_genesis 284 $legal = array('', 'w', 'r', 'x', '-');
mp-wp_genesis 285 $attarray = preg_split('//', $mode);
mp-wp_genesis 286
mp-wp_genesis 287 for($i=0; $i < count($attarray); $i++)
mp-wp_genesis 288 if($key = array_search($attarray[$i], $legal))
mp-wp_genesis 289 $realmode .= $legal[$key];
mp-wp_genesis 290
mp-wp_genesis 291 $mode = str_pad($realmode, 9, '-');
mp-wp_genesis 292 $trans = array('-'=>'0', 'r'=>'4', 'w'=>'2', 'x'=>'1');
mp-wp_genesis 293 $mode = strtr($mode,$trans);
mp-wp_genesis 294
mp-wp_genesis 295 $newmode = '';
mp-wp_genesis 296 $newmode .= $mode[0] + $mode[1] + $mode[2];
mp-wp_genesis 297 $newmode .= $mode[3] + $mode[4] + $mode[5];
mp-wp_genesis 298 $newmode .= $mode[6] + $mode[7] + $mode[8];
mp-wp_genesis 299 return $newmode;
mp-wp_genesis 300 }
mp-wp_genesis 301
mp-wp_genesis 302 /**
mp-wp_genesis 303 * Determines if the string provided contains binary characters.
mp-wp_genesis 304 *
mp-wp_genesis 305 * @since 2.7
mp-wp_genesis 306 * @access private
mp-wp_genesis 307 *
mp-wp_genesis 308 * @param string $text String to test against
mp-wp_genesis 309 * @return bool true if string is binary, false otherwise
mp-wp_genesis 310 */
mp-wp_genesis 311 function is_binary( $text ) {
mp-wp_genesis 312 return (bool) preg_match('|[^\x20-\x7E]|', $text); //chr(32)..chr(127)
mp-wp_genesis 313 }
mp-wp_genesis 314 }
mp-wp_genesis 315
mp-wp_genesis 316 ?>