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