3 class WP_Filesystem_Direct{
4 var $permission = null;
6 function WP_Filesystem_Direct($arg){
7 $this->errors = new WP_Error();
8 $this->permission = umask();
13 function setDefaultPermissions($perm){
14 $this->permission = $perm;
16 function find_base_dir($base = '.', $echo = false){
17 return str_replace('\\','/',ABSPATH);
19 function get_base_dir($base = '.', $echo = false){
20 return $this->find_base_dir($base, $echo);
22 function get_contents($file){
23 return @file_get_contents($file);
25 function get_contents_array($file){
28 function put_contents($file,$contents,$mode=false,$type=''){
29 if ( ! ($fp = @fopen($file,'w'.$type)) )
31 @fwrite($fp,$contents);
33 $this->chmod($file,$mode);
42 function chgrp($file,$group,$recursive=false){
43 if( ! $this->exists($file) )
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);
57 function chmod($file,$mode=false,$recursive=false){
59 $mode = $this->permission;
60 if( ! $this->exists($file) )
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);
74 function chown($file,$owner,$recursive=false){
75 if( ! $this->exists($file) )
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);
88 function owner($file){
89 $owneruid = @fileowner($file);
92 if( !function_exists('posix_getpwuid') )
94 $ownerarray = posix_getpwuid($owneruid);
95 return $ownerarray['name'];
97 function getchmod($file){
98 return @fileperms($file);
100 function gethchmod($file){
101 //From the PHP.net page for ...?
102 $perms = $this->getchmod($file);
103 if (($perms & 0xC000) == 0xC000) {
106 } elseif (($perms & 0xA000) == 0xA000) {
109 } elseif (($perms & 0x8000) == 0x8000) {
112 } elseif (($perms & 0x6000) == 0x6000) {
115 } elseif (($perms & 0x4000) == 0x4000) {
118 } elseif (($perms & 0x2000) == 0x2000) {
121 } elseif (($perms & 0x1000) == 0x1000) {
130 $info .= (($perms & 0x0100) ? 'r' : '-');
131 $info .= (($perms & 0x0080) ? 'w' : '-');
132 $info .= (($perms & 0x0040) ?
133 (($perms & 0x0800) ? 's' : 'x' ) :
134 (($perms & 0x0800) ? 'S' : '-'));
137 $info .= (($perms & 0x0020) ? 'r' : '-');
138 $info .= (($perms & 0x0010) ? 'w' : '-');
139 $info .= (($perms & 0x0008) ?
140 (($perms & 0x0400) ? 's' : 'x' ) :
141 (($perms & 0x0400) ? 'S' : '-'));
144 $info .= (($perms & 0x0004) ? 'r' : '-');
145 $info .= (($perms & 0x0002) ? 'w' : '-');
146 $info .= (($perms & 0x0001) ?
147 (($perms & 0x0200) ? 't' : 'x' ) :
148 (($perms & 0x0200) ? 'T' : '-'));
151 function getnumchmodfromh($mode) {
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];
160 $mode = str_pad($realmode,9,'-');
161 $trans = array('-'=>'0','r'=>'4','w'=>'2','x'=>'1');
162 $mode = strtr($mode,$trans);
164 $newmode .= $mode[0]+$mode[1]+$mode[2];
165 $newmode .= $mode[3]+$mode[4]+$mode[5];
166 $newmode .= $mode[6]+$mode[7]+$mode[8];
169 function group($file){
170 $gid = @filegroup($file);
173 if( !function_exists('posix_getgrgid') )
175 $grouparray = posix_getgrgid($gid);
176 return $grouparray['name'];
179 function copy($source,$destination,$overwrite=false){
180 if( ! $overwrite && $this->exists($destination) )
182 return copy($source,$destination);
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);
195 function delete($file, $recursive=false){
196 $file = str_replace('\\','/',$file); //for win32, occasional problems deleteing files otherwise
198 if( $this->is_file($file) )
199 return @unlink($file);
200 if( !$recursive && $this->is_dir($file) )
201 return @rmdir($file);
203 //At this point its a folder, and we're in recursive mode
204 $file = trailingslashit($file);
205 $filelist = $this->dirlist($file, 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) )
213 if( ! @rmdir($file) )
218 function exists($file){
219 return @file_exists($file);
222 function is_file($file){
223 return @is_file($file);
226 function is_dir($path){
227 return @is_dir($path);
230 function is_readable($file){
231 return @is_readable($file);
234 function is_writable($file){
235 return @is_writable($file);
238 function atime($file){
239 return @fileatime($file);
242 function mtime($file){
243 return @filemtime($file);
245 function size($file){
246 return @filesize($file);
249 function touch($file, $time = 0, $atime = 0){
254 return @touch($file,$time,$atime);
257 function mkdir($path, $chmod = false, $chown = false, $chgrp = false){
259 $chmod = $this->permission;
261 if( !@mkdir($path,$chmod) )
264 $this->chown($path,$chown);
266 $this->chgrp($path,$chgrp);
270 function rmdir($path,$recursive=false){
271 //Currently unused and untested, Use delete() instead.
273 return @rmdir($path);
275 $filelist = $this->dirlist($path);
276 foreach($filelist as $filename=>$det){
277 if ( '/' == substr($filename,-1,1) )
278 $this->rmdir($path.'/'.$filename,$recursive);
281 return @rmdir($path);
284 function dirlist($path,$incdot=false,$recursive=false){
285 if( $this->is_file($path) ){
286 $limitFile = basename($path);
287 $path = dirname($path);
291 if( ! $this->is_dir($path) )
296 while (false !== ($entry = $dir->read())) {
298 $struc['name'] = $entry;
300 if( '.' == $struc['name'] || '..' == $struc['name'] )
301 continue; //Do not care about these folders.
302 if( '.' == $struc['name'][0] && !$incdot)
304 if( $limitFile && $struc['name'] != $limitFile)
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';
318 if ('d' == $struc['type'] ){
320 $struc['files'] = $this->dirlist($path.'/'.$struc['name'], $incdot, $recursive);
322 $struc['files'] = array();
325 $ret[ $struc['name'] ] = $struc;
332 function __destruct(){