[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

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.0
  13   *
  14   * @see WP_Filesystem_Base
  15   */
  16  class WP_Filesystem_Direct extends WP_Filesystem_Base {
  17  
  18      /**
  19       * Constructor.
  20       *
  21       * @since 2.5.0
  22       *
  23       * @param mixed $arg Not used.
  24       */
  25  	public function __construct( $arg ) {
  26          $this->method = 'direct';
  27          $this->errors = new WP_Error();
  28      }
  29  
  30      /**
  31       * Reads entire file into a string.
  32       *
  33       * @since 2.5.0
  34       *
  35       * @param string $file Name of the file to read.
  36       * @return string|false Read data on success, false on failure.
  37       */
  38  	public function get_contents( $file ) {
  39          return @file_get_contents( $file );
  40      }
  41  
  42      /**
  43       * Reads entire file into an array.
  44       *
  45       * @since 2.5.0
  46       *
  47       * @param string $file Path to the file.
  48       * @return array|false File contents in an array on success, false on failure.
  49       */
  50  	public function get_contents_array( $file ) {
  51          return @file( $file );
  52      }
  53  
  54      /**
  55       * Writes a string to a file.
  56       *
  57       * @since 2.5.0
  58       *
  59       * @param string    $file     Remote path to the file where to write the data.
  60       * @param string    $contents The data to write.
  61       * @param int|false $mode     Optional. The file permissions as octal number, usually 0644.
  62       *                            Default false.
  63       * @return bool True on success, false on failure.
  64       */
  65  	public function put_contents( $file, $contents, $mode = false ) {
  66          $fp = @fopen( $file, 'wb' );
  67          if ( ! $fp ) {
  68              return false;
  69          }
  70  
  71          mbstring_binary_safe_encoding();
  72  
  73          $data_length = strlen( $contents );
  74  
  75          $bytes_written = fwrite( $fp, $contents );
  76  
  77          reset_mbstring_encoding();
  78  
  79          fclose( $fp );
  80  
  81          if ( $data_length !== $bytes_written ) {
  82              return false;
  83          }
  84  
  85          $this->chmod( $file, $mode );
  86  
  87          return true;
  88      }
  89  
  90      /**
  91       * Gets the current working directory.
  92       *
  93       * @since 2.5.0
  94       *
  95       * @return string|false The current working directory on success, false on failure.
  96       */
  97  	public function cwd() {
  98          return getcwd();
  99      }
 100  
 101      /**
 102       * Changes current directory.
 103       *
 104       * @since 2.5.0
 105       *
 106       * @param string $dir The new current directory.
 107       * @return bool True on success, false on failure.
 108       */
 109  	public function chdir( $dir ) {
 110          return @chdir( $dir );
 111      }
 112  
 113      /**
 114       * Changes the file group.
 115       *
 116       * @since 2.5.0
 117       *
 118       * @param string     $file      Path to the file.
 119       * @param string|int $group     A group name or number.
 120       * @param bool       $recursive Optional. If set to true, changes file group recursively.
 121       *                              Default false.
 122       * @return bool True on success, false on failure.
 123       */
 124  	public function chgrp( $file, $group, $recursive = false ) {
 125          if ( ! $this->exists( $file ) ) {
 126              return false;
 127          }
 128          if ( ! $recursive ) {
 129              return chgrp( $file, $group );
 130          }
 131          if ( ! $this->is_dir( $file ) ) {
 132              return chgrp( $file, $group );
 133          }
 134          // Is a directory, and we want recursive
 135          $file     = trailingslashit( $file );
 136          $filelist = $this->dirlist( $file );
 137          foreach ( $filelist as $filename ) {
 138              $this->chgrp( $file . $filename, $group, $recursive );
 139          }
 140  
 141          return true;
 142      }
 143  
 144      /**
 145       * Changes filesystem permissions.
 146       *
 147       * @since 2.5.0
 148       *
 149       * @param string    $file      Path to the file.
 150       * @param int|false $mode      Optional. The permissions as octal number, usually 0644 for files,
 151       *                             0755 for directories. Default false.
 152       * @param bool      $recursive Optional. If set to true, changes file permissions recursively.
 153       *                             Default false.
 154       * @return bool True on success, false on failure.
 155       */
 156  	public function chmod( $file, $mode = false, $recursive = false ) {
 157          if ( ! $mode ) {
 158              if ( $this->is_file( $file ) ) {
 159                  $mode = FS_CHMOD_FILE;
 160              } elseif ( $this->is_dir( $file ) ) {
 161                  $mode = FS_CHMOD_DIR;
 162              } else {
 163                  return false;
 164              }
 165          }
 166  
 167          if ( ! $recursive || ! $this->is_dir( $file ) ) {
 168              return chmod( $file, $mode );
 169          }
 170          // Is a directory, and we want recursive
 171          $file     = trailingslashit( $file );
 172          $filelist = $this->dirlist( $file );
 173          foreach ( (array) $filelist as $filename => $filemeta ) {
 174              $this->chmod( $file . $filename, $mode, $recursive );
 175          }
 176  
 177          return true;
 178      }
 179  
 180      /**
 181       * Changes the owner of a file or directory.
 182       *
 183       * @since 2.5.0
 184       *
 185       * @param string     $file      Path to the file or directory.
 186       * @param string|int $owner     A user name or number.
 187       * @param bool       $recursive Optional. If set to true, changes file owner recursively.
 188       *                              Default false.
 189       * @return bool True on success, false on failure.
 190       */
 191  	public function chown( $file, $owner, $recursive = false ) {
 192          if ( ! $this->exists( $file ) ) {
 193              return false;
 194          }
 195          if ( ! $recursive ) {
 196              return chown( $file, $owner );
 197          }
 198          if ( ! $this->is_dir( $file ) ) {
 199              return chown( $file, $owner );
 200          }
 201          // Is a directory, and we want recursive
 202          $filelist = $this->dirlist( $file );
 203          foreach ( $filelist as $filename ) {
 204              $this->chown( $file . '/' . $filename, $owner, $recursive );
 205          }
 206          return true;
 207      }
 208  
 209      /**
 210       * Gets the file owner.
 211       *
 212       * @since 2.5.0
 213       *
 214       * @param string $file Path to the file.
 215       * @return string|false Username of the owner on success, false on failure.
 216       */
 217  	public function owner( $file ) {
 218          $owneruid = @fileowner( $file );
 219          if ( ! $owneruid ) {
 220              return false;
 221          }
 222          if ( ! function_exists( 'posix_getpwuid' ) ) {
 223              return $owneruid;
 224          }
 225          $ownerarray = posix_getpwuid( $owneruid );
 226          return $ownerarray['name'];
 227      }
 228  
 229      /**
 230       * Gets the permissions of the specified file or filepath in their octal format.
 231       *
 232       * FIXME does not handle errors in fileperms()
 233       *
 234       * @since 2.5.0
 235       *
 236       * @param string $file Path to the file.
 237       * @return string Mode of the file (the last 3 digits).
 238       */
 239  	public function getchmod( $file ) {
 240          return substr( decoct( @fileperms( $file ) ), -3 );
 241      }
 242  
 243      /**
 244       * Gets the file's group.
 245       *
 246       * @since 2.5.0
 247       *
 248       * @param string $file Path to the file.
 249       * @return string|false The group on success, false on failure.
 250       */
 251  	public function group( $file ) {
 252          $gid = @filegroup( $file );
 253          if ( ! $gid ) {
 254              return false;
 255          }
 256          if ( ! function_exists( 'posix_getgrgid' ) ) {
 257              return $gid;
 258          }
 259          $grouparray = posix_getgrgid( $gid );
 260          return $grouparray['name'];
 261      }
 262  
 263      /**
 264       * Copies a file.
 265       *
 266       * @since 2.5.0
 267       *
 268       * @param string    $source      Path to the source file.
 269       * @param string    $destination Path to the destination file.
 270       * @param bool      $overwrite   Optional. Whether to overwrite the destination file if it exists.
 271       *                               Default false.
 272       * @param int|false $mode        Optional. The permissions as octal number, usually 0644 for files,
 273       *                               0755 for dirs. Default false.
 274       * @return bool True on success, false on failure.
 275       */
 276  	public function copy( $source, $destination, $overwrite = false, $mode = false ) {
 277          if ( ! $overwrite && $this->exists( $destination ) ) {
 278              return false;
 279          }
 280  
 281          $rtval = copy( $source, $destination );
 282          if ( $mode ) {
 283              $this->chmod( $destination, $mode );
 284          }
 285          return $rtval;
 286      }
 287  
 288      /**
 289       * Moves a file.
 290       *
 291       * @since 2.5.0
 292       *
 293       * @param string $source      Path to the source file.
 294       * @param string $destination Path to the destination file.
 295       * @param bool   $overwrite   Optional. Whether to overwrite the destination file if it exists.
 296       *                            Default false.
 297       * @return bool True on success, false on failure.
 298       */
 299  	public function move( $source, $destination, $overwrite = false ) {
 300          if ( ! $overwrite && $this->exists( $destination ) ) {
 301              return false;
 302          }
 303  
 304          // Try using rename first. if that fails (for example, source is read only) try copy.
 305          if ( @rename( $source, $destination ) ) {
 306              return true;
 307          }
 308  
 309          if ( $this->copy( $source, $destination, $overwrite ) && $this->exists( $destination ) ) {
 310              $this->delete( $source );
 311              return true;
 312          } else {
 313              return false;
 314          }
 315      }
 316  
 317      /**
 318       * Deletes a file or directory.
 319       *
 320       * @since 2.5.0
 321       *
 322       * @param string       $file      Path to the file or directory.
 323       * @param bool         $recursive Optional. If set to true, deletes files and folders recursively.
 324       *                                Default false.
 325       * @param string|false $type      Type of resource. 'f' for file, 'd' for directory.
 326       *                                Default false.
 327       * @return bool True on success, false on failure.
 328       */
 329  	public function delete( $file, $recursive = false, $type = false ) {
 330          if ( empty( $file ) ) { // Some filesystems report this as /, which can cause non-expected recursive deletion of all files in the filesystem.
 331              return false;
 332          }
 333          $file = str_replace( '\\', '/', $file ); // for win32, occasional problems deleting files otherwise
 334  
 335          if ( 'f' == $type || $this->is_file( $file ) ) {
 336              return @unlink( $file );
 337          }
 338          if ( ! $recursive && $this->is_dir( $file ) ) {
 339              return @rmdir( $file );
 340          }
 341  
 342          // At this point it's a folder, and we're in recursive mode
 343          $file     = trailingslashit( $file );
 344          $filelist = $this->dirlist( $file, true );
 345  
 346          $retval = true;
 347          if ( is_array( $filelist ) ) {
 348              foreach ( $filelist as $filename => $fileinfo ) {
 349                  if ( ! $this->delete( $file . $filename, $recursive, $fileinfo['type'] ) ) {
 350                      $retval = false;
 351                  }
 352              }
 353          }
 354  
 355          if ( file_exists( $file ) && ! @rmdir( $file ) ) {
 356              $retval = false;
 357          }
 358  
 359          return $retval;
 360      }
 361  
 362      /**
 363       * Checks if a file or directory exists.
 364       *
 365       * @since 2.5.0
 366       *
 367       * @param string $file Path to file or directory.
 368       * @return bool Whether $file exists or not.
 369       */
 370  	public function exists( $file ) {
 371          return @file_exists( $file );
 372      }
 373  
 374      /**
 375       * Checks if resource is a file.
 376       *
 377       * @since 2.5.0
 378       *
 379       * @param string $file File path.
 380       * @return bool Whether $file is a file.
 381       */
 382  	public function is_file( $file ) {
 383          return @is_file( $file );
 384      }
 385  
 386      /**
 387       * Checks if resource is a directory.
 388       *
 389       * @since 2.5.0
 390       *
 391       * @param string $path Directory path.
 392       * @return bool Whether $path is a directory.
 393       */
 394  	public function is_dir( $path ) {
 395          return @is_dir( $path );
 396      }
 397  
 398      /**
 399       * Checks if a file is readable.
 400       *
 401       * @since 2.5.0
 402       *
 403       * @param string $file Path to file.
 404       * @return bool Whether $file is readable.
 405       */
 406  	public function is_readable( $file ) {
 407          return @is_readable( $file );
 408      }
 409  
 410      /**
 411       * Checks if a file or directory is writable.
 412       *
 413       * @since 2.5.0
 414       *
 415       * @param string $file Path to file or directory.
 416       * @return bool Whether $file is writable.
 417       */
 418  	public function is_writable( $file ) {
 419          return @is_writable( $file );
 420      }
 421  
 422      /**
 423       * Gets the file's last access time.
 424       *
 425       * @since 2.5.0
 426       *
 427       * @param string $file Path to file.
 428       * @return int|false Unix timestamp representing last access time, false on failure.
 429       */
 430  	public function atime( $file ) {
 431          return @fileatime( $file );
 432      }
 433  
 434      /**
 435       * Gets the file modification time.
 436       *
 437       * @since 2.5.0
 438       *
 439       * @param string $file Path to file.
 440       * @return int|false Unix timestamp representing modification time, false on failure.
 441       */
 442  	public function mtime( $file ) {
 443          return @filemtime( $file );
 444      }
 445  
 446      /**
 447       * Gets the file size (in bytes).
 448       *
 449       * @since 2.5.0
 450       *
 451       * @param string $file Path to file.
 452       * @return int|false Size of the file in bytes on success, false on failure.
 453       */
 454  	public function size( $file ) {
 455          return @filesize( $file );
 456      }
 457  
 458      /**
 459       * Sets the access and modification times of a file.
 460       *
 461       * Note: If $file doesn't exist, it will be created.
 462       *
 463       * @since 2.5.0
 464       *
 465       * @param string $file  Path to file.
 466       * @param int    $time  Optional. Modified time to set for file.
 467       *                      Default 0.
 468       * @param int    $atime Optional. Access time to set for file.
 469       *                      Default 0.
 470       * @return bool True on success, false on failure.
 471       */
 472  	public function touch( $file, $time = 0, $atime = 0 ) {
 473          if ( $time == 0 ) {
 474              $time = time();
 475          }
 476          if ( $atime == 0 ) {
 477              $atime = time();
 478          }
 479          return touch( $file, $time, $atime );
 480      }
 481  
 482      /**
 483       * Creates a directory.
 484       *
 485       * @since 2.5.0
 486       *
 487       * @param string     $path  Path for new directory.
 488       * @param int|false  $chmod Optional. The permissions as octal number (or false to skip chmod).
 489       *                          Default false.
 490       * @param string|int $chown Optional. A user name or number (or false to skip chown).
 491       *                          Default false.
 492       * @param string|int $chgrp Optional. A group name or number (or false to skip chgrp).
 493       *                          Default false.
 494       * @return bool True on success, false on failure.
 495       */
 496  	public function mkdir( $path, $chmod = false, $chown = false, $chgrp = false ) {
 497          // Safe mode fails with a trailing slash under certain PHP versions.
 498          $path = untrailingslashit( $path );
 499          if ( empty( $path ) ) {
 500              return false;
 501          }
 502  
 503          if ( ! $chmod ) {
 504              $chmod = FS_CHMOD_DIR;
 505          }
 506  
 507          if ( ! @mkdir( $path ) ) {
 508              return false;
 509          }
 510          $this->chmod( $path, $chmod );
 511          if ( $chown ) {
 512              $this->chown( $path, $chown );
 513          }
 514          if ( $chgrp ) {
 515              $this->chgrp( $path, $chgrp );
 516          }
 517          return true;
 518      }
 519  
 520      /**
 521       * Deletes a directory.
 522       *
 523       * @since 2.5.0
 524       *
 525       * @param string $path      Path to directory.
 526       * @param bool   $recursive Optional. Whether to recursively remove files/directories.
 527       *                          Default false.
 528       * @return bool True on success, false on failure.
 529       */
 530  	public function rmdir( $path, $recursive = false ) {
 531          return $this->delete( $path, $recursive );
 532      }
 533  
 534      /**
 535       * Gets details for files in a directory or a specific file.
 536       *
 537       * @since 2.5.0
 538       *
 539       * @param string $path           Path to directory or file.
 540       * @param bool   $include_hidden Optional. Whether to include details of hidden ("." prefixed) files.
 541       *                               Default true.
 542       * @param bool   $recursive      Optional. Whether to recursively include file details in nested directories.
 543       *                               Default false.
 544       * @return array|false {
 545       *     Array of files. False if unable to list directory contents.
 546       *
 547       *     @type string $name        Name of the file or directory.
 548       *     @type string $perms       *nix representation of permissions.
 549       *     @type int    $permsn      Octal representation of permissions.
 550       *     @type string $owner       Owner name or ID.
 551       *     @type int    $size        Size of file in bytes.
 552       *     @type int    $lastmodunix Last modified unix timestamp.
 553       *     @type mixed  $lastmod     Last modified month (3 letter) and day (without leading 0).
 554       *     @type int    $time        Last modified time.
 555       *     @type string $type        Type of resource. 'f' for file, 'd' for directory.
 556       *     @type mixed  $files       If a directory and $recursive is true, contains another array of files.
 557       * }
 558       */
 559  	public function dirlist( $path, $include_hidden = true, $recursive = false ) {
 560          if ( $this->is_file( $path ) ) {
 561              $limit_file = basename( $path );
 562              $path       = dirname( $path );
 563          } else {
 564              $limit_file = false;
 565          }
 566  
 567          if ( ! $this->is_dir( $path ) || ! $this->is_readable( $path ) ) {
 568              return false;
 569          }
 570  
 571          $dir = dir( $path );
 572          if ( ! $dir ) {
 573              return false;
 574          }
 575  
 576          $ret = array();
 577  
 578          while ( false !== ( $entry = $dir->read() ) ) {
 579              $struc         = array();
 580              $struc['name'] = $entry;
 581  
 582              if ( '.' == $struc['name'] || '..' == $struc['name'] ) {
 583                  continue;
 584              }
 585  
 586              if ( ! $include_hidden && '.' == $struc['name'][0] ) {
 587                  continue;
 588              }
 589  
 590              if ( $limit_file && $struc['name'] != $limit_file ) {
 591                  continue;
 592              }
 593  
 594              $struc['perms']       = $this->gethchmod( $path . '/' . $entry );
 595              $struc['permsn']      = $this->getnumchmodfromh( $struc['perms'] );
 596              $struc['number']      = false;
 597              $struc['owner']       = $this->owner( $path . '/' . $entry );
 598              $struc['group']       = $this->group( $path . '/' . $entry );
 599              $struc['size']        = $this->size( $path . '/' . $entry );
 600              $struc['lastmodunix'] = $this->mtime( $path . '/' . $entry );
 601              $struc['lastmod']     = gmdate( 'M j', $struc['lastmodunix'] );
 602              $struc['time']        = gmdate( 'h:i:s', $struc['lastmodunix'] );
 603              $struc['type']        = $this->is_dir( $path . '/' . $entry ) ? 'd' : 'f';
 604  
 605              if ( 'd' == $struc['type'] ) {
 606                  if ( $recursive ) {
 607                      $struc['files'] = $this->dirlist( $path . '/' . $struc['name'], $include_hidden, $recursive );
 608                  } else {
 609                      $struc['files'] = array();
 610                  }
 611              }
 612  
 613              $ret[ $struc['name'] ] = $struc;
 614          }
 615          $dir->close();
 616          unset( $dir );
 617          return $ret;
 618      }
 619  }


Generated: Sat Nov 23 20:47:33 2019 Cross-referenced by PHPXref 0.7