[ Root ] [ Search ] [ Index ]

PHP Cross Reference of WordPress 3.0.1

Provided by Yoast

title

Body

[close]

/wp-admin/includes/ -> class-wp-filesystem-direct.php (source)

   1  <?php
   2  /**
   3   * WordPress Direct Filesystem.
   4   *
   5   * @package WordPress
   6   * @subpackage Filesystem
   7   */
   8  
   9  /**
  10   * WordPress Filesystem Class for direct PHP file and folder manipulation.
  11   *
  12   * @since 2.5
  13   * @package WordPress
  14   * @subpackage Filesystem
  15   * @uses WP_Filesystem_Base Extends class
  16   */
  17  class WP_Filesystem_Direct extends WP_Filesystem_Base {
  18      var $errors = null;
  19      /**
  20       * constructor
  21       *
  22       * @param $arg mixed ingored argument
  23       */
  24  	function WP_Filesystem_Direct($arg) {
  25          $this->method = 'direct';
  26          $this->errors = new WP_Error();
  27      }
  28      /**
  29       * connect filesystem.
  30       *
  31       * @return bool Returns true on success or false on failure (always true for WP_Filesystem_Direct).
  32       */
  33  	function connect() {
  34          return true;
  35      }
  36      /**
  37       * Reads entire file into a string
  38       *
  39       * @param $file string Name of the file to read.
  40       * @return string|bool The function returns the read data or false on failure.
  41       */
  42  	function get_contents($file) {
  43          return @file_get_contents($file);
  44      }
  45      /**
  46       * Reads entire file into an array
  47       *
  48       * @param $file string Path to the file.
  49       * @return array|bool the file contents in an array or false on failure.
  50       */
  51  	function get_contents_array($file) {
  52          return @file($file);
  53      }
  54      /**
  55       * Write a string to a file
  56       *
  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.
  61       */
  62  	function put_contents($file, $contents, $mode = false ) {
  63          if ( ! ($fp = @fopen($file, 'w')) )
  64              return false;
  65          @fwrite($fp, $contents);
  66          @fclose($fp);
  67          $this->chmod($file, $mode);
  68          return true;
  69      }
  70      /**
  71       * Gets the current working directory
  72       *
  73       * @return string|bool the current working directory on success, or false on failure.
  74       */
  75  	function cwd() {
  76          return @getcwd();
  77      }
  78      /**
  79       * Change directory
  80       *
  81       * @param $dir string The new current directory.
  82       * @return bool Returns true on success or false on failure.
  83       */
  84  	function chdir($dir) {
  85          return @chdir($dir);
  86      }
  87      /**
  88       * Changes file group
  89       *
  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.
  94       */
  95  	function chgrp($file, $group, $recursive = false) {
  96          if ( ! $this->exists($file) )
  97              return false;
  98          if ( ! $recursive )
  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);
 107  
 108          return true;
 109      }
 110      /**
 111       * Changes filesystem permissions
 112       *
 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.
 117       */
 118  	function chmod($file, $mode = false, $recursive = false) {
 119          if ( ! $mode ) {
 120              if ( $this->is_file($file) )
 121                  $mode = FS_CHMOD_FILE;
 122              elseif ( $this->is_dir($file) )
 123                  $mode = FS_CHMOD_DIR;
 124              else
 125                  return false;
 126          }
 127  
 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);
 135  
 136          return true;
 137      }
 138      /**
 139       * Changes file owner
 140       *
 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.
 145       */
 146  	function chown($file, $owner, $recursive = false) {
 147          if ( ! $this->exists($file) )
 148              return false;
 149          if ( ! $recursive )
 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);
 157          }
 158          return true;
 159      }
 160      /**
 161       * Gets file owner
 162       *
 163       * @param $file string Path to the file.
 164       * @return string Username of the user.
 165       */
 166  	function owner($file) {
 167          $owneruid = @fileowner($file);
 168          if ( ! $owneruid )
 169              return false;
 170          if ( ! function_exists('posix_getpwuid') )
 171              return $owneruid;
 172          $ownerarray = posix_getpwuid($owneruid);
 173          return $ownerarray['name'];
 174      }
 175      /**
 176       * Gets file permissions
 177       *
 178       * FIXME does not handle errors in fileperms()
 179       *
 180       * @param $file string Path to the file.
 181       * @return string Mode of the file (last 4 digits).
 182       */
 183  	function getchmod($file) {
 184          return substr(decoct(@fileperms($file)),3);
 185      }
 186  	function group($file) {
 187          $gid = @filegroup($file);
 188          if ( ! $gid )
 189              return false;
 190          if ( ! function_exists('posix_getgrgid') )
 191              return $gid;
 192          $grouparray = posix_getgrgid($gid);
 193          return $grouparray['name'];
 194      }
 195  
 196  	function copy($source, $destination, $overwrite = false) {
 197          if ( ! $overwrite && $this->exists($destination) )
 198              return false;
 199  
 200          return copy($source, $destination);
 201      }
 202  
 203  	function move($source, $destination, $overwrite = false) {
 204          if ( ! $overwrite && $this->exists($destination) )
 205              return false;
 206  
 207          // try using rename first.  if that fails (for example, source is read only) try copy
 208          if ( @rename($source, $destination) )
 209              return true;
 210  
 211          if ( $this->copy($source, $destination, $overwrite) && $this->exists($destination) ) {
 212              $this->delete($source);
 213              return true;
 214          } else {
 215              return false;
 216          }
 217      }
 218  
 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.
 221              return false;
 222          $file = str_replace('\\', '/', $file); //for win32, occasional problems deleteing files otherwise
 223  
 224          if ( $this->is_file($file) )
 225              return @unlink($file);
 226          if ( ! $recursive && $this->is_dir($file) )
 227              return @rmdir($file);
 228  
 229          //At this point its a folder, and we're in recursive mode
 230          $file = trailingslashit($file);
 231          $filelist = $this->dirlist($file, true);
 232  
 233          $retval = 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) )
 237                      $retval = false;
 238  
 239          if ( file_exists($file) && ! @rmdir($file) )
 240              $retval = false;
 241          return $retval;
 242      }
 243  
 244  	function exists($file) {
 245          return @file_exists($file);
 246      }
 247  
 248  	function is_file($file) {
 249          return @is_file($file);
 250      }
 251  
 252  	function is_dir($path) {
 253          return @is_dir($path);
 254      }
 255  
 256  	function is_readable($file) {
 257          return @is_readable($file);
 258      }
 259  
 260  	function is_writable($file) {
 261          return @is_writable($file);
 262      }
 263  
 264  	function atime($file) {
 265          return @fileatime($file);
 266      }
 267  
 268  	function mtime($file) {
 269          return @filemtime($file);
 270      }
 271  	function size($file) {
 272          return @filesize($file);
 273      }
 274  
 275  	function touch($file, $time = 0, $atime = 0) {
 276          if ($time == 0)
 277              $time = time();
 278          if ($atime == 0)
 279              $atime = time();
 280          return @touch($file, $time, $atime);
 281      }
 282  
 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);
 286          if ( empty($path) )
 287              $path = '/';
 288  
 289          if ( ! $chmod )
 290              $chmod = FS_CHMOD_DIR;
 291  
 292          if ( ! @mkdir($path) )
 293              return false;
 294          $this->chmod($path, $chmod);
 295          if ( $chown )
 296              $this->chown($path, $chown);
 297          if ( $chgrp )
 298              $this->chgrp($path, $chgrp);
 299          return true;
 300      }
 301  
 302  	function rmdir($path, $recursive = false) {
 303          return $this->delete($path, $recursive);
 304      }
 305  
 306  	function dirlist($path, $include_hidden = true, $recursive = false) {
 307          if ( $this->is_file($path) ) {
 308              $limit_file = basename($path);
 309              $path = dirname($path);
 310          } else {
 311              $limit_file = false;
 312          }
 313  
 314          if ( ! $this->is_dir($path) )
 315              return false;
 316  
 317          $dir = @dir($path);
 318          if ( ! $dir )
 319              return false;
 320  
 321          $ret = array();
 322  
 323          while (false !== ($entry = $dir->read()) ) {
 324              $struc = array();
 325              $struc['name'] = $entry;
 326  
 327              if ( '.' == $struc['name'] || '..' == $struc['name'] )
 328                  continue;
 329  
 330              if ( ! $include_hidden && '.' == $struc['name'][0] )
 331                  continue;
 332  
 333              if ( $limit_file && $struc['name'] != $limit_file)
 334                  continue;
 335  
 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';
 346  
 347              if ( 'd' == $struc['type'] ) {
 348                  if ( $recursive )
 349                      $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $include_hidden, $recursive);
 350                  else
 351                      $struc['files'] = array();
 352              }
 353  
 354              $ret[ $struc['name'] ] = $struc;
 355          }
 356          $dir->close();
 357          unset($dir);
 358          return $ret;
 359      }
 360  }
 361  ?>


Generated: Thu Oct 14 05:12:05 2010 Cross-referenced by PHPXref 0.7