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