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