3 class WP_Filesystem_Direct extends WP_Filesystem_Base {
4 var $permission = null;
6 function WP_Filesystem_Direct($arg) {
7 $this->method = 'direct';
8 $this->errors = new WP_Error();
9 $this->permission = umask();
14 function setDefaultPermissions($perm) {
15 $this->permission = $perm;
17 function get_contents($file) {
18 return @file_get_contents($file);
20 function get_contents_array($file) {
23 function put_contents($file, $contents, $mode = false, $type = '') {
24 if ( ! ($fp = @fopen($file, 'w' . $type)) )
26 @fwrite($fp, $contents);
28 $this->chmod($file,$mode);
34 function chdir($dir) {
37 function chgrp($file, $group, $recursive = false) {
38 if( ! $this->exists($file) )
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);
52 function chmod($file, $mode = false, $recursive = false) {
54 $mode = $this->permission;
55 if( ! $this->exists($file) )
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);
69 function chown($file, $owner, $recursive = false) {
70 if( ! $this->exists($file) )
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);
83 function owner($file) {
84 $owneruid = @fileowner($file);
87 if( ! function_exists('posix_getpwuid') )
89 $ownerarray = posix_getpwuid($owneruid);
90 return $ownerarray['name'];
92 function getchmod($file) {
93 return @fileperms($file);
95 function group($file) {
96 $gid = @filegroup($file);
99 if( ! function_exists('posix_getgrgid') )
101 $grouparray = posix_getgrgid($gid);
102 return $grouparray['name'];
105 function copy($source, $destination, $overwrite = false) {
106 if( ! $overwrite && $this->exists($destination) )
108 return copy($source, $destination);
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);
121 function delete($file, $recursive = false) {
122 $file = str_replace('\\', '/', $file); //for win32, occasional problems deleteing files otherwise
124 if( $this->is_file($file) )
125 return @unlink($file);
126 if( ! $recursive && $this->is_dir($file) )
127 return @rmdir($file);
129 //At this point its a folder, and we're in recursive mode
130 $file = trailingslashit($file);
131 $filelist = $this->dirlist($file, 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) )
139 if( ! @rmdir($file) )
144 function exists($file) {
145 return @file_exists($file);
148 function is_file($file) {
149 return @is_file($file);
152 function is_dir($path) {
153 return @is_dir($path);
156 function is_readable($file) {
157 return @is_readable($file);
160 function is_writable($file) {
161 return @is_writable($file);
164 function atime($file) {
165 return @fileatime($file);
168 function mtime($file) {
169 return @filemtime($file);
171 function size($file) {
172 return @filesize($file);
175 function touch($file, $time = 0, $atime = 0){
180 return @touch($file, $time, $atime);
183 function mkdir($path, $chmod = false, $chown = false, $chgrp = false){
185 $chmod = $this->permission;
187 if( ! @mkdir($path, $chmod) )
190 $this->chown($path, $chown);
192 $this->chgrp($path, $chgrp);
196 function rmdir($path, $recursive = false) {
197 //Currently unused and untested, Use delete() instead.
199 return @rmdir($path);
201 $filelist = $this->dirlist($path);
202 foreach($filelist as $filename => $det) {
203 if ( '/' == substr($filename, -1, 1) )
204 $this->rmdir($path . '/' . $filename, $recursive);
207 return @rmdir($path);
210 function dirlist($path, $incdot = false, $recursive = false) {
211 if( $this->is_file($path) ) {
212 $limitFile = basename($path);
213 $path = dirname($path);
217 if( ! $this->is_dir($path) )
222 while (false !== ($entry = $dir->read()) ) {
224 $struc['name'] = $entry;
226 if( '.' == $struc['name'] || '..' == $struc['name'] )
227 continue; //Do not care about these folders.
228 if( '.' == $struc['name'][0] && !$incdot)
230 if( $limitFile && $struc['name'] != $limitFile)
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';
244 if ( 'd' == $struc['type'] ) {
246 $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $incdot, $recursive);
248 $struc['files'] = array();
251 $ret[ $struc['name'] ] = $struc;