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 {
22 * @param $arg mixed ingored argument
24 function WP_Filesystem_Direct($arg) {
25 $this->method = 'direct';
26 $this->errors = new WP_Error();
31 * @return bool Returns true on success or false on failure (always true for WP_Filesystem_Direct).
37 * Reads entire file into a string
39 * @param $file string Name of the file to read.
40 * @return string|bool The function returns the read data or false on failure.
42 function get_contents($file) {
43 return @file_get_contents($file);
46 * Reads entire file into an array
48 * @param $file string Path to the file.
49 * @return array|bool the file contents in an array or false on failure.
51 function get_contents_array($file) {
55 * Write a string to a file
57 * @param $file string Remote path to the file where to write the data.
58 * @param $contents string The data to write.
59 * @param $mode int (optional) The file permissions as octal number, usually 0644.
60 * @return bool False upon failure.
62 function put_contents($file, $contents, $mode = false ) {
63 if ( ! ($fp = @fopen($file, 'w')) )
65 @fwrite($fp, $contents);
67 $this->chmod($file, $mode);
71 * Gets the current working directory
73 * @return string|bool the current working directory on success, or false on failure.
81 * @param $dir string The new current directory.
82 * @return bool Returns true on success or false on failure.
84 function chdir($dir) {
90 * @param $file string Path to the file.
91 * @param $group mixed A group name or number.
92 * @param $recursive bool (optional) If set True changes file group recursivly. Defaults to False.
93 * @return bool Returns true on success or false on failure.
95 function chgrp($file, $group, $recursive = false) {
96 if ( ! $this->exists($file) )
99 return @chgrp($file, $group);
100 if ( ! $this->is_dir($file) )
101 return @chgrp($file, $group);
102 //Is a directory, and we want recursive
103 $file = trailingslashit($file);
104 $filelist = $this->dirlist($file);
105 foreach ($filelist as $filename)
106 $this->chgrp($file . $filename, $group, $recursive);
111 * Changes filesystem permissions
113 * @param $file string Path to the file.
114 * @param $mode int (optional) The permissions as octal number, usually 0644 for files, 0755 for dirs.
115 * @param $recursive bool (optional) If set True changes file group recursivly. Defaults to False.
116 * @return bool Returns true on success or false on failure.
118 function chmod($file, $mode = false, $recursive = false) {
120 if ( $this->is_file($file) )
121 $mode = FS_CHMOD_FILE;
122 elseif ( $this->is_dir($file) )
123 $mode = FS_CHMOD_DIR;
128 if ( ! $recursive || ! $this->is_dir($file) )
129 return @chmod($file, $mode);
130 //Is a directory, and we want recursive
131 $file = trailingslashit($file);
132 $filelist = $this->dirlist($file);
133 foreach ( (array)$filelist as $filename => $filemeta)
134 $this->chmod($file . $filename, $mode, $recursive);
141 * @param $file string Path to the file.
142 * @param $owner mixed A user name or number.
143 * @param $recursive bool (optional) If set True changes file owner recursivly. Defaults to False.
144 * @return bool Returns true on success or false on failure.
146 function chown($file, $owner, $recursive = false) {
147 if ( ! $this->exists($file) )
150 return @chown($file, $owner);
151 if ( ! $this->is_dir($file) )
152 return @chown($file, $owner);
153 //Is a directory, and we want recursive
154 $filelist = $this->dirlist($file);
155 foreach ($filelist as $filename) {
156 $this->chown($file . '/' . $filename, $owner, $recursive);
163 * @param $file string Path to the file.
164 * @return string Username of the user.
166 function owner($file) {
167 $owneruid = @fileowner($file);
170 if ( ! function_exists('posix_getpwuid') )
172 $ownerarray = posix_getpwuid($owneruid);
173 return $ownerarray['name'];
176 * Gets file permissions
178 * FIXME does not handle errors in fileperms()
180 * @param $file string Path to the file.
181 * @return string Mode of the file (last 4 digits).
183 function getchmod($file) {
184 return substr(decoct(@fileperms($file)),3);
186 function group($file) {
187 $gid = @filegroup($file);
190 if ( ! function_exists('posix_getgrgid') )
192 $grouparray = posix_getgrgid($gid);
193 return $grouparray['name'];
196 function copy($source, $destination, $overwrite = false) {
197 if ( ! $overwrite && $this->exists($destination) )
200 return copy($source, $destination);
203 function move($source, $destination, $overwrite = false) {
204 if ( ! $overwrite && $this->exists($destination) )
207 // try using rename first. if that fails (for example, source is read only) try copy
208 if ( @rename($source, $destination) )
211 if ( $this->copy($source, $destination, $overwrite) && $this->exists($destination) ) {
212 $this->delete($source);
219 function delete($file, $recursive = false) {
220 if ( empty($file) ) //Some filesystems report this as /, which can cause non-expected recursive deletion of all files in the filesystem.
222 $file = str_replace('\\', '/', $file); //for win32, occasional problems deleteing files otherwise
224 if ( $this->is_file($file) )
225 return @unlink($file);
226 if ( ! $recursive && $this->is_dir($file) )
227 return @rmdir($file);
229 //At this point its a folder, and we're in recursive mode
230 $file = trailingslashit($file);
231 $filelist = $this->dirlist($file, true);
234 if ( is_array($filelist) ) //false if no files, So check first.
235 foreach ($filelist as $filename => $fileinfo)
236 if ( ! $this->delete($file . $filename, $recursive) )
239 if ( file_exists($file) && ! @rmdir($file) )
244 function exists($file) {
245 return @file_exists($file);
248 function is_file($file) {
249 return @is_file($file);
252 function is_dir($path) {
253 return @is_dir($path);
256 function is_readable($file) {
257 return @is_readable($file);
260 function is_writable($file) {
261 return @is_writable($file);
264 function atime($file) {
265 return @fileatime($file);
268 function mtime($file) {
269 return @filemtime($file);
271 function size($file) {
272 return @filesize($file);
275 function touch($file, $time = 0, $atime = 0) {
280 return @touch($file, $time, $atime);
283 function mkdir($path, $chmod = false, $chown = false, $chgrp = false) {
284 // safe mode fails with a trailing slash under certain PHP versions.
285 $path = untrailingslashit($path);
290 $chmod = FS_CHMOD_DIR;
292 if ( ! @mkdir($path) )
294 $this->chmod($path, $chmod);
296 $this->chown($path, $chown);
298 $this->chgrp($path, $chgrp);
302 function rmdir($path, $recursive = false) {
303 return $this->delete($path, $recursive);
306 function dirlist($path, $include_hidden = true, $recursive = false) {
307 if ( $this->is_file($path) ) {
308 $limit_file = basename($path);
309 $path = dirname($path);
314 if ( ! $this->is_dir($path) )
323 while (false !== ($entry = $dir->read()) ) {
325 $struc['name'] = $entry;
327 if ( '.' == $struc['name'] || '..' == $struc['name'] )
330 if ( ! $include_hidden && '.' == $struc['name'][0] )
333 if ( $limit_file && $struc['name'] != $limit_file)
336 $struc['perms'] = $this->gethchmod($path.'/'.$entry);
337 $struc['permsn'] = $this->getnumchmodfromh($struc['perms']);
338 $struc['number'] = false;
339 $struc['owner'] = $this->owner($path.'/'.$entry);
340 $struc['group'] = $this->group($path.'/'.$entry);
341 $struc['size'] = $this->size($path.'/'.$entry);
342 $struc['lastmodunix']= $this->mtime($path.'/'.$entry);
343 $struc['lastmod'] = date('M j',$struc['lastmodunix']);
344 $struc['time'] = date('h:i:s',$struc['lastmodunix']);
345 $struc['type'] = $this->is_dir($path.'/'.$entry) ? 'd' : 'f';
347 if ( 'd' == $struc['type'] ) {
349 $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $include_hidden, $recursive);
351 $struc['files'] = array();
354 $ret[ $struc['name'] ] = $struc;