3 * WordPress Direct Filesystem.
6 * @subpackage Filesystem
10 * WordPress Filesystem Class for direct PHP file and folder manipulation.
14 * @subpackage Filesystem
15 * @uses WP_Filesystem_Base Extends class
17 class WP_Filesystem_Direct extends WP_Filesystem_Base {
18 var $permission = null;
20 function WP_Filesystem_Direct($arg) {
21 $this->method = 'direct';
22 $this->errors = new WP_Error();
23 $this->permission = umask();
28 function setDefaultPermissions($perm) {
29 $this->permission = $perm;
31 function get_contents($file) {
32 return @file_get_contents($file);
34 function get_contents_array($file) {
37 function put_contents($file, $contents, $mode = false, $type = '') {
38 if ( ! ($fp = @fopen($file, 'w' . $type)) )
40 @fwrite($fp, $contents);
42 $this->chmod($file,$mode);
48 function chdir($dir) {
51 function chgrp($file, $group, $recursive = false) {
52 if ( ! $this->exists($file) )
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);
66 function chmod($file, $mode = false, $recursive = false) {
68 $mode = $this->permission;
69 if ( ! $this->exists($file) )
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);
83 function chown($file, $owner, $recursive = false) {
84 if ( ! $this->exists($file) )
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);
97 function owner($file) {
98 $owneruid = @fileowner($file);
101 if ( ! function_exists('posix_getpwuid') )
103 $ownerarray = posix_getpwuid($owneruid);
104 return $ownerarray['name'];
106 function getchmod($file) {
107 return substr(decoct(@fileperms($file)),3);
109 function group($file) {
110 $gid = @filegroup($file);
113 if ( ! function_exists('posix_getgrgid') )
115 $grouparray = posix_getgrgid($gid);
116 return $grouparray['name'];
119 function copy($source, $destination, $overwrite = false) {
120 if ( ! $overwrite && $this->exists($destination) )
122 return copy($source, $destination);
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);
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.
138 $file = str_replace('\\', '/', $file); //for win32, occasional problems deleteing files otherwise
140 if ( $this->is_file($file) )
141 return @unlink($file);
142 if ( ! $recursive && $this->is_dir($file) )
143 return @rmdir($file);
145 //At this point its a folder, and we're in recursive mode
146 $file = trailingslashit($file);
147 $filelist = $this->dirlist($file, 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) )
155 if ( file_exists($file) && ! @rmdir($file) )
160 function exists($file) {
161 return @file_exists($file);
164 function is_file($file) {
165 return @is_file($file);
168 function is_dir($path) {
169 return @is_dir($path);
172 function is_readable($file) {
173 return @is_readable($file);
176 function is_writable($file) {
177 return @is_writable($file);
180 function atime($file) {
181 return @fileatime($file);
184 function mtime($file) {
185 return @filemtime($file);
187 function size($file) {
188 return @filesize($file);
191 function touch($file, $time = 0, $atime = 0){
196 return @touch($file, $time, $atime);
199 function mkdir($path, $chmod = false, $chown = false, $chgrp = false){
201 $chmod = $this->permission;
203 if ( ! @mkdir($path, $chmod) )
206 $this->chown($path, $chown);
208 $this->chgrp($path, $chgrp);
212 function rmdir($path, $recursive = false) {
213 //Currently unused and untested, Use delete() instead.
215 return @rmdir($path);
217 $filelist = $this->dirlist($path);
218 foreach ($filelist as $filename => $det) {
219 if ( '/' == substr($filename, -1, 1) )
220 $this->rmdir($path . '/' . $filename, $recursive);
223 return @rmdir($path);
226 function dirlist($path, $incdot = false, $recursive = false) {
227 if ( $this->is_file($path) ) {
228 $limitFile = basename($path);
229 $path = dirname($path);
233 if ( ! $this->is_dir($path) )
240 while (false !== ($entry = $dir->read()) ) {
242 $struc['name'] = $entry;
244 if ( '.' == $struc['name'] || '..' == $struc['name'] )
245 continue; //Do not care about these folders.
246 if ( '.' == $struc['name'][0] && !$incdot)
248 if ( $limitFile && $struc['name'] != $limitFile)
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';
262 if ( 'd' == $struc['type'] ) {
264 $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $incdot, $recursive);
266 $struc['files'] = array();
269 $ret[ $struc['name'] ] = $struc;