Wordpress 2.5.1
[autoinstalls/wordpress.git] / wp-admin / includes / class-wp-filesystem-direct.php
1 <?php
2
3 class WP_Filesystem_Direct{
4         var $permission = null;
5         var $errors = array();
6         function WP_Filesystem_Direct($arg){
7                 $this->errors = new WP_Error();
8                 $this->permission = umask();
9         }
10         function connect(){
11                 return true;
12         }
13         function setDefaultPermissions($perm){
14                 $this->permission = $perm;
15         }
16         function find_base_dir($base = '.', $echo = false){
17                 return str_replace('\\','/',ABSPATH);
18         }
19         function get_base_dir($base = '.', $echo = false){
20                 return $this->find_base_dir($base, $echo);
21         }
22         function get_contents($file){
23                 return @file_get_contents($file);
24         }
25         function get_contents_array($file){
26                 return @file($file);
27         }
28         function put_contents($file,$contents,$mode=false,$type=''){
29                 if ( ! ($fp = @fopen($file,'w'.$type)) )
30                         return false;
31                 @fwrite($fp,$contents);
32                 @fclose($fp);
33                 $this->chmod($file,$mode);
34                 return true;
35         }
36         function cwd(){
37                 return @getcwd();
38         }
39         function chdir($dir){
40                 return @chdir($dir);
41         }
42         function chgrp($file,$group,$recursive=false){
43                 if( ! $this->exists($file) )
44                         return false;
45                 if( ! $recursive )
46                         return @chgrp($file,$group);
47                 if( ! $this->is_dir($file) )
48                         return @chgrp($file,$group);
49                 //Is a directory, and we want recursive
50                 $file = trailingslashit($file);
51                 $filelist = $this->dirlist($file);
52                 foreach($filelist as $filename)
53                         $this->chgrp($file . $filename, $group, $recursive);
54
55                 return true;
56         }
57         function chmod($file,$mode=false,$recursive=false){
58                 if( ! $mode )
59                         $mode = $this->permission;
60                 if( ! $this->exists($file) )
61                         return false;
62                 if( ! $recursive )
63                         return @chmod($file,$mode);
64                 if( ! $this->is_dir($file) )
65                         return @chmod($file,$mode);
66                 //Is a directory, and we want recursive
67                 $file = trailingslashit($file);
68                 $filelist = $this->dirlist($file);
69                 foreach($filelist as $filename)
70                         $this->chmod($file . $filename, $mode, $recursive);
71
72                 return true;
73         }
74         function chown($file,$owner,$recursive=false){
75                 if( ! $this->exists($file) )
76                         return false;
77                 if( ! $recursive )
78                         return @chown($file,$owner);
79                 if( ! $this->is_dir($file) )
80                         return @chown($file,$owner);
81                 //Is a directory, and we want recursive
82                 $filelist = $this->dirlist($file);
83                 foreach($filelist as $filename){
84                         $this->chown($file.'/'.$filename,$owner,$recursive);
85                 }
86                 return true;
87         }
88         function owner($file){
89                 $owneruid = @fileowner($file);
90                 if( ! $owneruid )
91                         return false;
92                 if( !function_exists('posix_getpwuid') )
93                         return $owneruid;
94                 $ownerarray = posix_getpwuid($owneruid);
95                 return $ownerarray['name'];
96         }
97         function getchmod($file){
98                 return @fileperms($file);
99         }
100         function gethchmod($file){
101                 //From the PHP.net page for ...?
102                 $perms = $this->getchmod($file);
103                 if (($perms & 0xC000) == 0xC000) {
104                         // Socket
105                         $info = 's';
106                 } elseif (($perms & 0xA000) == 0xA000) {
107                         // Symbolic Link
108                         $info = 'l';
109                 } elseif (($perms & 0x8000) == 0x8000) {
110                         // Regular
111                         $info = '-';
112                 } elseif (($perms & 0x6000) == 0x6000) {
113                         // Block special
114                         $info = 'b';
115                 } elseif (($perms & 0x4000) == 0x4000) {
116                         // Directory
117                         $info = 'd';
118                 } elseif (($perms & 0x2000) == 0x2000) {
119                         // Character special
120                         $info = 'c';
121                 } elseif (($perms & 0x1000) == 0x1000) {
122                         // FIFO pipe
123                         $info = 'p';
124                 } else {
125                         // Unknown
126                         $info = 'u';
127                 }
128
129                 // Owner
130                 $info .= (($perms & 0x0100) ? 'r' : '-');
131                 $info .= (($perms & 0x0080) ? 'w' : '-');
132                 $info .= (($perms & 0x0040) ?
133                                         (($perms & 0x0800) ? 's' : 'x' ) :
134                                         (($perms & 0x0800) ? 'S' : '-'));
135
136                 // Group
137                 $info .= (($perms & 0x0020) ? 'r' : '-');
138                 $info .= (($perms & 0x0010) ? 'w' : '-');
139                 $info .= (($perms & 0x0008) ?
140                                         (($perms & 0x0400) ? 's' : 'x' ) :
141                                         (($perms & 0x0400) ? 'S' : '-'));
142
143                 // World
144                 $info .= (($perms & 0x0004) ? 'r' : '-');
145                 $info .= (($perms & 0x0002) ? 'w' : '-');
146                 $info .= (($perms & 0x0001) ?
147                                         (($perms & 0x0200) ? 't' : 'x' ) :
148                                         (($perms & 0x0200) ? 'T' : '-'));
149                 return $info;
150         }
151         function getnumchmodfromh($mode) {
152                 $realmode = "";
153                 $legal =  array("","w","r","x","-");
154                 $attarray = preg_split("//",$mode);
155                 for($i=0;$i<count($attarray);$i++){
156                    if($key = array_search($attarray[$i],$legal)){
157                            $realmode .= $legal[$key];
158                    }
159                 }
160                 $mode = str_pad($realmode,9,'-');
161                 $trans = array('-'=>'0','r'=>'4','w'=>'2','x'=>'1');
162                 $mode = strtr($mode,$trans);
163                 $newmode = '';
164                 $newmode .= $mode[0]+$mode[1]+$mode[2];
165                 $newmode .= $mode[3]+$mode[4]+$mode[5];
166                 $newmode .= $mode[6]+$mode[7]+$mode[8];
167                 return $newmode;
168         }
169         function group($file){
170                 $gid = @filegroup($file);
171                 if( ! $gid )
172                         return false;
173                 if( !function_exists('posix_getgrgid') )
174                         return $gid;
175                 $grouparray = posix_getgrgid($gid);
176                 return $grouparray['name'];
177         }
178
179         function copy($source,$destination,$overwrite=false){
180                 if( ! $overwrite && $this->exists($destination) )
181                         return false;
182                 return copy($source,$destination);
183         }
184
185         function move($source,$destination,$overwrite=false){
186                 //Possible to use rename()?
187                 if( $this->copy($source,$destination,$overwrite) && $this->exists($destination) ){
188                         $this->delete($source);
189                         return true;
190                 } else {
191                         return false;
192                 }
193         }
194
195         function delete($file, $recursive=false){
196                 $file = str_replace('\\','/',$file); //for win32, occasional problems deleteing files otherwise
197
198                 if( $this->is_file($file) )
199                         return @unlink($file);
200                 if( !$recursive && $this->is_dir($file) )
201                         return @rmdir($file);
202
203                 //At this point its a folder, and we're in recursive mode
204                 $file = trailingslashit($file);
205                 $filelist = $this->dirlist($file, true);
206
207                 $retval = true;
208                 if( is_array($filelist) ) //false if no files, So check first.
209                         foreach($filelist as $filename=>$fileinfo)
210                                 if( ! $this->delete($file . $filename, $recursive) )
211                                         $retval = false;
212
213                 if( ! @rmdir($file) )
214                         return false;
215                 return $retval;
216         }
217
218         function exists($file){
219                 return @file_exists($file);
220         }
221
222         function is_file($file){
223                 return @is_file($file);
224         }
225
226         function is_dir($path){
227                 return @is_dir($path);
228         }
229
230         function is_readable($file){
231                 return @is_readable($file);
232         }
233
234         function is_writable($file){
235                 return @is_writable($file);
236         }
237
238         function atime($file){
239                 return @fileatime($file);
240         }
241
242         function mtime($file){
243                 return @filemtime($file);
244         }
245         function size($file){
246                 return @filesize($file);
247         }
248
249         function touch($file, $time = 0, $atime = 0){
250                 if($time == 0)
251                         $time = time();
252                 if($atime == 0)
253                         $atime = time();
254                 return @touch($file,$time,$atime);
255         }
256
257         function mkdir($path, $chmod = false, $chown = false, $chgrp = false){
258                 if( ! $chmod)
259                         $chmod = $this->permission;
260
261                 if( !@mkdir($path,$chmod) )
262                         return false;
263                 if( $chown )
264                         $this->chown($path,$chown);
265                 if( $chgrp )
266                         $this->chgrp($path,$chgrp);
267                 return true;
268         }
269
270         function rmdir($path,$recursive=false){
271                 //Currently unused and untested, Use delete() instead.
272                 if( ! $recursive )
273                         return @rmdir($path);
274                 //recursive:
275                 $filelist = $this->dirlist($path);
276                 foreach($filelist as $filename=>$det){
277                         if ( '/' == substr($filename,-1,1) )
278                                 $this->rmdir($path.'/'.$filename,$recursive);
279                         @rmdir($filename);
280                 }
281                 return @rmdir($path);
282         }
283
284         function dirlist($path,$incdot=false,$recursive=false){
285                 if( $this->is_file($path) ){
286                         $limitFile = basename($path);
287                         $path = dirname($path);
288                 } else {
289                         $limitFile = false;
290                 }
291                 if( ! $this->is_dir($path) )
292                         return false;
293
294                 $ret = array();
295                 $dir = dir($path);
296                 while (false !== ($entry = $dir->read())) {
297                         $struc = array();
298                         $struc['name']          = $entry;
299
300                         if( '.' == $struc['name'] || '..' == $struc['name'] )
301                                 continue; //Do not care about these folders.
302                         if( '.' == $struc['name'][0] && !$incdot)
303                                 continue;
304                         if( $limitFile && $struc['name'] != $limitFile)
305                                 continue;
306
307                         $struc['perms']         = $this->gethchmod($path.'/'.$entry);
308                         $struc['permsn']        = $this->getnumchmodfromh($struc['perms']);
309                         $struc['number']        = false;
310                         $struc['owner']         = $this->owner($path.'/'.$entry);
311                         $struc['group']         = $this->group($path.'/'.$entry);
312                         $struc['size']          = $this->size($path.'/'.$entry);
313                         $struc['lastmodunix']= $this->mtime($path.'/'.$entry);
314                         $struc['lastmod']   = date('M j',$struc['lastmodunix']);
315                         $struc['time']          = date('h:i:s',$struc['lastmodunix']);
316                         $struc['type']          = $this->is_dir($path.'/'.$entry) ? 'd' : 'f';
317
318                         if ('d' == $struc['type'] ){
319                                 if( $recursive )
320                                         $struc['files'] = $this->dirlist($path.'/'.$struc['name'], $incdot, $recursive);
321                                 else
322                                         $struc['files'] = array();
323                         }
324
325                         $ret[ $struc['name'] ] = $struc;
326                 }
327                 $dir->close();
328                 unset($dir);
329                 return $ret;
330         }
331
332         function __destruct(){
333                 return;
334         }
335 }
336 ?>