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