]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/includes/class-wp-filesystem-direct.php
Wordpress 2.9.1-scripts
[autoinstalls/wordpress.git] / wp-admin / includes / class-wp-filesystem-direct.php
1 <?php
2 /**
3  * WordPress Direct Filesystem.
4  *
5  * @package WordPress
6  * @subpackage Filesystem
7  */
8
9 /**
10  * WordPress Filesystem Class for direct PHP file and folder manipulation.
11  *
12  * @since 2.5
13  * @package WordPress
14  * @subpackage Filesystem
15  * @uses WP_Filesystem_Base Extends class
16  */
17 class WP_Filesystem_Direct extends WP_Filesystem_Base {
18         var $errors = null;
19         /**
20          * constructor
21          *
22          * @param $arg mixed ingored argument
23          */
24         function WP_Filesystem_Direct($arg) {
25                 $this->method = 'direct';
26                 $this->errors = new WP_Error();
27         }
28         /**
29          * connect filesystem.
30          *
31          * @return bool Returns true on success or false on failure (always true for WP_Filesystem_Direct).
32          */
33         function connect() {
34                 return true;
35         }
36         /**
37          * Reads entire file into a string
38          *
39          * @param $file string Name of the file to read.
40          * @return string|bool The function returns the read data or false on failure.
41          */
42         function get_contents($file) {
43                 return @file_get_contents($file);
44         }
45         /**
46          * Reads entire file into an array
47          *
48          * @param $file string Path to the file.
49          * @return array|bool the file contents in an array or false on failure.
50          */
51         function get_contents_array($file) {
52                 return @file($file);
53         }
54         /**
55          * Write a string to a file
56          *
57          * @param $file string Path to the file where to write the data.
58          * @param $contents string The data to write.
59          * @param $mode int (optional) The file permissions as octal number, usually 0644.
60          * @param $type string (optional) Specifies additional type of access you require to the file.
61          * @return bool False upon failure.
62          */
63         function put_contents($file, $contents, $mode = false, $type = '') {
64                 if ( ! ($fp = @fopen($file, 'w' . $type)) )
65                         return false;
66                 @fwrite($fp, $contents);
67                 @fclose($fp);
68                 $this->chmod($file, $mode);
69                 return true;
70         }
71         /**
72          * Gets the current working directory
73          *
74          * @return string|bool the current working directory on success, or false on failure.
75          */
76         function cwd() {
77                 return @getcwd();
78         }
79         /**
80          * Change directory
81          *
82          * @param $dir string The new current directory.
83          * @return bool Returns true on success or false on failure.
84          */
85         function chdir($dir) {
86                 return @chdir($dir);
87         }
88         /**
89          * Changes file group
90          *
91          * @param $file string Path to the file.
92          * @param $group mixed A group name or number.
93          * @param $recursive bool (optional) If set True changes file group recursivly. Defaults to False.
94          * @return bool Returns true on success or false on failure.
95          */
96         function chgrp($file, $group, $recursive = false) {
97                 if ( ! $this->exists($file) )
98                         return false;
99                 if ( ! $recursive )
100                         return @chgrp($file, $group);
101                 if ( ! $this->is_dir($file) )
102                         return @chgrp($file, $group);
103                 //Is a directory, and we want recursive
104                 $file = trailingslashit($file);
105                 $filelist = $this->dirlist($file);
106                 foreach ($filelist as $filename)
107                         $this->chgrp($file . $filename, $group, $recursive);
108
109                 return true;
110         }
111         /**
112          * Changes filesystem permissions
113          *
114          * @param $file string Path to the file.
115          * @param $mode int (optional) The permissions as octal number, usually 0644 for files, 0755 for dirs.
116          * @param $recursive bool (optional) If set True changes file group recursivly. Defaults to False.
117          * @return bool Returns true on success or false on failure.
118          */
119         function chmod($file, $mode = false, $recursive = false) {
120                 if ( ! $this->exists($file) )
121                         return false;
122
123                 if ( ! $mode ) {
124                         if ( $this->is_file($file) )
125                                 $mode = FS_CHMOD_FILE;
126                         elseif ( $this->is_dir($file) )
127                                 $mode = FS_CHMOD_DIR;
128                         else
129                                 return false;
130                 }
131
132                 if ( ! $recursive )
133                         return @chmod($file, $mode);
134                 if ( ! $this->is_dir($file) )
135                         return @chmod($file, $mode);
136                 //Is a directory, and we want recursive
137                 $file = trailingslashit($file);
138                 $filelist = $this->dirlist($file);
139                 foreach ($filelist as $filename)
140                         $this->chmod($file . $filename, $mode, $recursive);
141
142                 return true;
143         }
144         /**
145          * Changes file owner
146          *
147          * @param $file string Path to the file.
148          * @param $owner mixed A user name or number.
149          * @param $recursive bool (optional) If set True changes file owner recursivly. Defaults to False.
150          * @return bool Returns true on success or false on failure.
151          */
152         function chown($file, $owner, $recursive = false) {
153                 if ( ! $this->exists($file) )
154                         return false;
155                 if ( ! $recursive )
156                         return @chown($file, $owner);
157                 if ( ! $this->is_dir($file) )
158                         return @chown($file, $owner);
159                 //Is a directory, and we want recursive
160                 $filelist = $this->dirlist($file);
161                 foreach ($filelist as $filename) {
162                         $this->chown($file . '/' . $filename, $owner, $recursive);
163                 }
164                 return true;
165         }
166         /**
167          * Gets file owner
168          *
169          * @param $file string Path to the file.
170          * @return string Username of the user.
171          */
172         function owner($file) {
173                 $owneruid = @fileowner($file);
174                 if ( ! $owneruid )
175                         return false;
176                 if ( ! function_exists('posix_getpwuid') )
177                         return $owneruid;
178                 $ownerarray = posix_getpwuid($owneruid);
179                 return $ownerarray['name'];
180         }
181         /**
182          * Gets file permissions
183          *
184          * FIXME does not handle errors in fileperms()
185          *
186          * @param $file string Path to the file.
187          * @return string Mode of the file (last 4 digits).
188          */
189         function getchmod($file) {
190                 return substr(decoct(@fileperms($file)),3);
191         }
192         function group($file) {
193                 $gid = @filegroup($file);
194                 if ( ! $gid )
195                         return false;
196                 if ( ! function_exists('posix_getgrgid') )
197                         return $gid;
198                 $grouparray = posix_getgrgid($gid);
199                 return $grouparray['name'];
200         }
201
202         function copy($source, $destination, $overwrite = false) {
203                 if ( ! $overwrite && $this->exists($destination) )
204                         return false;
205                 return copy($source, $destination);
206         }
207
208         function move($source, $destination, $overwrite = false) {
209                 //Possible to use rename()?
210                 if ( $this->copy($source, $destination, $overwrite) && $this->exists($destination) ) {
211                         $this->delete($source);
212                         return true;
213                 } else {
214                         return false;
215                 }
216         }
217
218         function delete($file, $recursive = false) {
219                 if ( empty($file) ) //Some filesystems report this as /, which can cause non-expected recursive deletion of all files in the filesystem.
220                         return false;
221                 $file = str_replace('\\', '/', $file); //for win32, occasional problems deleteing files otherwise
222
223                 if ( $this->is_file($file) )
224                         return @unlink($file);
225                 if ( ! $recursive && $this->is_dir($file) )
226                         return @rmdir($file);
227
228                 //At this point its a folder, and we're in recursive mode
229                 $file = trailingslashit($file);
230                 $filelist = $this->dirlist($file, true);
231
232                 $retval = true;
233                 if ( is_array($filelist) ) //false if no files, So check first.
234                         foreach ($filelist as $filename => $fileinfo)
235                                 if ( ! $this->delete($file . $filename, $recursive) )
236                                         $retval = false;
237
238                 if ( file_exists($file) && ! @rmdir($file) )
239                         $retval = false;
240                 return $retval;
241         }
242
243         function exists($file) {
244                 return @file_exists($file);
245         }
246
247         function is_file($file) {
248                 return @is_file($file);
249         }
250
251         function is_dir($path) {
252                 return @is_dir($path);
253         }
254
255         function is_readable($file) {
256                 return @is_readable($file);
257         }
258
259         function is_writable($file) {
260                 return @is_writable($file);
261         }
262
263         function atime($file) {
264                 return @fileatime($file);
265         }
266
267         function mtime($file) {
268                 return @filemtime($file);
269         }
270         function size($file) {
271                 return @filesize($file);
272         }
273
274         function touch($file, $time = 0, $atime = 0) {
275                 if ($time == 0)
276                         $time = time();
277                 if ($atime == 0)
278                         $atime = time();
279                 return @touch($file, $time, $atime);
280         }
281
282         function mkdir($path, $chmod = false, $chown = false, $chgrp = false) {
283                 if ( ! $chmod )
284                         $chmod = FS_CHMOD_DIR;
285
286                 if ( ! @mkdir($path) )
287                         return false;
288                 $this->chmod($path, $chmod);
289                 if ( $chown )
290                         $this->chown($path, $chown);
291                 if ( $chgrp )
292                         $this->chgrp($path, $chgrp);
293                 return true;
294         }
295
296         function rmdir($path, $recursive = false) {
297                 //Currently unused and untested, Use delete() instead.
298                 if ( ! $recursive )
299                         return @rmdir($path);
300                 //recursive:
301                 $filelist = $this->dirlist($path);
302                 foreach ($filelist as $filename => $det) {
303                         if ( '/' == substr($filename, -1, 1) )
304                                 $this->rmdir($path . '/' . $filename, $recursive);
305                         @rmdir($filename);
306                 }
307                 return @rmdir($path);
308         }
309
310         function dirlist($path, $include_hidden = true, $recursive = false) {
311                 if ( $this->is_file($path) ) {
312                         $limit_file = basename($path);
313                         $path = dirname($path);
314                 } else {
315                         $limit_file = false;
316                 }
317
318                 if ( ! $this->is_dir($path) )
319                         return false;
320
321                 $dir = @dir($path);
322                 if ( ! $dir )
323                         return false;
324
325                 $ret = array();
326
327                 while (false !== ($entry = $dir->read()) ) {
328                         $struc = array();
329                         $struc['name'] = $entry;
330
331                         if ( '.' == $struc['name'] || '..' == $struc['name'] )
332                                 continue;
333
334                         if ( ! $include_hidden && '.' == $struc['name'][0] )
335                                 continue;
336
337                         if ( $limit_file && $struc['name'] != $limit_file)
338                                 continue;
339
340                         $struc['perms']         = $this->gethchmod($path.'/'.$entry);
341                         $struc['permsn']        = $this->getnumchmodfromh($struc['perms']);
342                         $struc['number']        = false;
343                         $struc['owner']         = $this->owner($path.'/'.$entry);
344                         $struc['group']         = $this->group($path.'/'.$entry);
345                         $struc['size']          = $this->size($path.'/'.$entry);
346                         $struc['lastmodunix']= $this->mtime($path.'/'.$entry);
347                         $struc['lastmod']   = date('M j',$struc['lastmodunix']);
348                         $struc['time']          = date('h:i:s',$struc['lastmodunix']);
349                         $struc['type']          = $this->is_dir($path.'/'.$entry) ? 'd' : 'f';
350
351                         if ( 'd' == $struc['type'] ) {
352                                 if ( $recursive )
353                                         $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $include_hidden, $recursive);
354                                 else
355                                         $struc['files'] = array();
356                         }
357
358                         $ret[ $struc['name'] ] = $struc;
359                 }
360                 $dir->close();
361                 unset($dir);
362                 return $ret;
363         }
364 }
365 ?>