]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/includes/class-wp-filesystem-base.php
Wordpress 3.3.1-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_path = $folder_parts[ count($folder_parts) - 1 ];
197
198                 $files = $this->dirlist( $base );
199
200                 foreach ( $folder_parts as $key ) {
201                         if ( $key == $last_path )
202                                 continue; //We want this to be caught by the next code block.
203
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) )
214                                         return $ret;
215                         }
216                 }
217
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);
223                 }
224                 if ( $loop )
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);
228
229         }
230
231         /**
232          * Returns the *nix style file permissions for a file
233          *
234          * From the PHP documentation page for fileperms()
235          *
236          * @link http://docs.php.net/fileperms
237          * @since 2.5
238          * @access public
239          *
240          * @param string $file string filename
241          * @return int octal representation of permissions
242          */
243         function gethchmod($file){
244                 $perms = $this->getchmod($file);
245                 if (($perms & 0xC000) == 0xC000) // Socket
246                         $info = 's';
247                 elseif (($perms & 0xA000) == 0xA000) // Symbolic Link
248                         $info = 'l';
249                 elseif (($perms & 0x8000) == 0x8000) // Regular
250                         $info = '-';
251                 elseif (($perms & 0x6000) == 0x6000) // Block special
252                         $info = 'b';
253                 elseif (($perms & 0x4000) == 0x4000) // Directory
254                         $info = 'd';
255                 elseif (($perms & 0x2000) == 0x2000) // Character special
256                         $info = 'c';
257                 elseif (($perms & 0x1000) == 0x1000) // FIFO pipe
258                         $info = 'p';
259                 else // Unknown
260                         $info = 'u';
261
262                 // Owner
263                 $info .= (($perms & 0x0100) ? 'r' : '-');
264                 $info .= (($perms & 0x0080) ? 'w' : '-');
265                 $info .= (($perms & 0x0040) ?
266                                         (($perms & 0x0800) ? 's' : 'x' ) :
267                                         (($perms & 0x0800) ? 'S' : '-'));
268
269                 // Group
270                 $info .= (($perms & 0x0020) ? 'r' : '-');
271                 $info .= (($perms & 0x0010) ? 'w' : '-');
272                 $info .= (($perms & 0x0008) ?
273                                         (($perms & 0x0400) ? 's' : 'x' ) :
274                                         (($perms & 0x0400) ? 'S' : '-'));
275
276                 // World
277                 $info .= (($perms & 0x0004) ? 'r' : '-');
278                 $info .= (($perms & 0x0002) ? 'w' : '-');
279                 $info .= (($perms & 0x0001) ?
280                                         (($perms & 0x0200) ? 't' : 'x' ) :
281                                         (($perms & 0x0200) ? 'T' : '-'));
282                 return $info;
283         }
284
285         /**
286          * Converts *nix style file permissions to a octal number.
287          *
288          * Converts '-rw-r--r--' to 0644
289          * From "info at rvgate dot nl"'s comment on the PHP documentation for chmod()
290          *
291          * @link http://docs.php.net/manual/en/function.chmod.php#49614
292          * @since 2.5
293          * @access public
294          *
295          * @param string $mode string *nix style file permission
296          * @return int octal representation
297          */
298         function getnumchmodfromh($mode) {
299                 $realmode = '';
300                 $legal =  array('', 'w', 'r', 'x', '-');
301                 $attarray = preg_split('//', $mode);
302
303                 for ($i=0; $i < count($attarray); $i++)
304                    if ($key = array_search($attarray[$i], $legal))
305                            $realmode .= $legal[$key];
306
307                 $mode = str_pad($realmode, 9, '-');
308                 $trans = array('-'=>'0', 'r'=>'4', 'w'=>'2', 'x'=>'1');
309                 $mode = strtr($mode,$trans);
310
311                 $newmode = '';
312                 $newmode .= $mode[0] + $mode[1] + $mode[2];
313                 $newmode .= $mode[3] + $mode[4] + $mode[5];
314                 $newmode .= $mode[6] + $mode[7] + $mode[8];
315                 return $newmode;
316         }
317
318         /**
319          * Determines if the string provided contains binary characters.
320          *
321          * @since 2.7
322          * @access private
323          *
324          * @param string $text String to test against
325          * @return bool true if string is binary, false otherwise
326          */
327         function is_binary( $text ) {
328                 return (bool) preg_match('|[^\x20-\x7E]|', $text); //chr(32)..chr(127)
329         }
330 }
331
332 ?>