]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/includes/class-wp-filesystem-direct.php
Wordpress 2.8
[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 $permission = null;
19         var $errors = null;
20         function WP_Filesystem_Direct($arg) {
21                 $this->method = 'direct';
22                 $this->errors = new WP_Error();
23                 $this->permission = umask();
24         }
25         function connect() {
26                 return true;
27         }
28         function setDefaultPermissions($perm) {
29                 $this->permission = $perm;
30         }
31         function get_contents($file) {
32                 return @file_get_contents($file);
33         }
34         function get_contents_array($file) {
35                 return @file($file);
36         }
37         function put_contents($file, $contents, $mode = false, $type = '') {
38                 if ( ! ($fp = @fopen($file, 'w' . $type)) )
39                         return false;
40                 @fwrite($fp, $contents);
41                 @fclose($fp);
42                 $this->chmod($file,$mode);
43                 return true;
44         }
45         function cwd() {
46                 return @getcwd();
47         }
48         function chdir($dir) {
49                 return @chdir($dir);
50         }
51         function chgrp($file, $group, $recursive = false) {
52                 if ( ! $this->exists($file) )
53                         return false;
54                 if ( ! $recursive )
55                         return @chgrp($file, $group);
56                 if ( ! $this->is_dir($file) )
57                         return @chgrp($file, $group);
58                 //Is a directory, and we want recursive
59                 $file = trailingslashit($file);
60                 $filelist = $this->dirlist($file);
61                 foreach ($filelist as $filename)
62                         $this->chgrp($file . $filename, $group, $recursive);
63
64                 return true;
65         }
66         function chmod($file, $mode = false, $recursive = false) {
67                 if ( ! $mode )
68                         $mode = $this->permission;
69                 if ( ! $this->exists($file) )
70                         return false;
71                 if ( ! $recursive )
72                         return @chmod($file,$mode);
73                 if ( ! $this->is_dir($file) )
74                         return @chmod($file, $mode);
75                 //Is a directory, and we want recursive
76                 $file = trailingslashit($file);
77                 $filelist = $this->dirlist($file);
78                 foreach ($filelist as $filename)
79                         $this->chmod($file . $filename, $mode, $recursive);
80
81                 return true;
82         }
83         function chown($file, $owner, $recursive = false) {
84                 if ( ! $this->exists($file) )
85                         return false;
86                 if ( ! $recursive )
87                         return @chown($file, $owner);
88                 if ( ! $this->is_dir($file) )
89                         return @chown($file, $owner);
90                 //Is a directory, and we want recursive
91                 $filelist = $this->dirlist($file);
92                 foreach ($filelist as $filename){
93                         $this->chown($file . '/' . $filename, $owner, $recursive);
94                 }
95                 return true;
96         }
97         function owner($file) {
98                 $owneruid = @fileowner($file);
99                 if ( ! $owneruid )
100                         return false;
101                 if ( ! function_exists('posix_getpwuid') )
102                         return $owneruid;
103                 $ownerarray = posix_getpwuid($owneruid);
104                 return $ownerarray['name'];
105         }
106         function getchmod($file) {
107                 return substr(decoct(@fileperms($file)),3);
108         }
109         function group($file) {
110                 $gid = @filegroup($file);
111                 if ( ! $gid )
112                         return false;
113                 if ( ! function_exists('posix_getgrgid') )
114                         return $gid;
115                 $grouparray = posix_getgrgid($gid);
116                 return $grouparray['name'];
117         }
118
119         function copy($source, $destination, $overwrite = false) {
120                 if ( ! $overwrite && $this->exists($destination) )
121                         return false;
122                 return copy($source, $destination);
123         }
124
125         function move($source, $destination, $overwrite = false) {
126                 //Possible to use rename()?
127                 if ( $this->copy($source, $destination, $overwrite) && $this->exists($destination) ){
128                         $this->delete($source);
129                         return true;
130                 } else {
131                         return false;
132                 }
133         }
134
135         function delete($file, $recursive = false) {
136                 if ( empty($file) ) //Some filesystems report this as /, which can cause non-expected recursive deletion of all files in the filesystem.
137                         return false;
138                 $file = str_replace('\\', '/', $file); //for win32, occasional problems deleteing files otherwise
139
140                 if ( $this->is_file($file) )
141                         return @unlink($file);
142                 if ( ! $recursive && $this->is_dir($file) )
143                         return @rmdir($file);
144
145                 //At this point its a folder, and we're in recursive mode
146                 $file = trailingslashit($file);
147                 $filelist = $this->dirlist($file, true);
148
149                 $retval = true;
150                 if ( is_array($filelist) ) //false if no files, So check first.
151                         foreach ($filelist as $filename => $fileinfo)
152                                 if ( ! $this->delete($file . $filename, $recursive) )
153                                         $retval = false;
154
155                 if ( file_exists($file) && ! @rmdir($file) )
156                         $retval = false;
157                 return $retval;
158         }
159
160         function exists($file) {
161                 return @file_exists($file);
162         }
163
164         function is_file($file) {
165                 return @is_file($file);
166         }
167
168         function is_dir($path) {
169                 return @is_dir($path);
170         }
171
172         function is_readable($file) {
173                 return @is_readable($file);
174         }
175
176         function is_writable($file) {
177                 return @is_writable($file);
178         }
179
180         function atime($file) {
181                 return @fileatime($file);
182         }
183
184         function mtime($file) {
185                 return @filemtime($file);
186         }
187         function size($file) {
188                 return @filesize($file);
189         }
190
191         function touch($file, $time = 0, $atime = 0){
192                 if ($time == 0)
193                         $time = time();
194                 if ($atime == 0)
195                         $atime = time();
196                 return @touch($file, $time, $atime);
197         }
198
199         function mkdir($path, $chmod = false, $chown = false, $chgrp = false){
200                 if ( ! $chmod)
201                         $chmod = $this->permission;
202
203                 if ( ! @mkdir($path, $chmod) )
204                         return false;
205                 if ( $chown )
206                         $this->chown($path, $chown);
207                 if ( $chgrp )
208                         $this->chgrp($path, $chgrp);
209                 return true;
210         }
211
212         function rmdir($path, $recursive = false) {
213                 //Currently unused and untested, Use delete() instead.
214                 if ( ! $recursive )
215                         return @rmdir($path);
216                 //recursive:
217                 $filelist = $this->dirlist($path);
218                 foreach ($filelist as $filename => $det) {
219                         if ( '/' == substr($filename, -1, 1) )
220                                 $this->rmdir($path . '/' . $filename, $recursive);
221                         @rmdir($filename);
222                 }
223                 return @rmdir($path);
224         }
225
226         function dirlist($path, $incdot = false, $recursive = false) {
227                 if ( $this->is_file($path) ) {
228                         $limitFile = basename($path);
229                         $path = dirname($path);
230                 } else {
231                         $limitFile = false;
232                 }
233                 if ( ! $this->is_dir($path) )
234                         return false;
235
236                 $ret = array();
237                 $dir = @dir($path);
238                 if ( ! $dir )
239                         return false;
240                 while (false !== ($entry = $dir->read()) ) {
241                         $struc = array();
242                         $struc['name'] = $entry;
243
244                         if ( '.' == $struc['name'] || '..' == $struc['name'] )
245                                 continue; //Do not care about these folders.
246                         if ( '.' == $struc['name'][0] && !$incdot)
247                                 continue;
248                         if ( $limitFile && $struc['name'] != $limitFile)
249                                 continue;
250
251                         $struc['perms']         = $this->gethchmod($path.'/'.$entry);
252                         $struc['permsn']        = $this->getnumchmodfromh($struc['perms']);
253                         $struc['number']        = false;
254                         $struc['owner']         = $this->owner($path.'/'.$entry);
255                         $struc['group']         = $this->group($path.'/'.$entry);
256                         $struc['size']          = $this->size($path.'/'.$entry);
257                         $struc['lastmodunix']= $this->mtime($path.'/'.$entry);
258                         $struc['lastmod']   = date('M j',$struc['lastmodunix']);
259                         $struc['time']          = date('h:i:s',$struc['lastmodunix']);
260                         $struc['type']          = $this->is_dir($path.'/'.$entry) ? 'd' : 'f';
261
262                         if ( 'd' == $struc['type'] ) {
263                                 if ( $recursive )
264                                         $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $incdot, $recursive);
265                                 else
266                                         $struc['files'] = array();
267                         }
268
269                         $ret[ $struc['name'] ] = $struc;
270                 }
271                 $dir->close();
272                 unset($dir);
273                 return $ret;
274         }
275 }
276 ?>