[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

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 ) ) {
 331              // Some filesystems report this as /, which can cause non-expected recursive deletion of all files in the filesystem.
 332              return false;
 333          }
 334          $file = str_replace( '\\', '/', $file ); // For Win32, occasional problems deleting files otherwise.
 335  
 336          if ( 'f' === $type || $this->is_file( $file ) ) {
 337              return @unlink( $file );
 338          }
 339          if ( ! $recursive && $this->is_dir( $file ) ) {
 340              return @rmdir( $file );
 341          }
 342  
 343          // At this point it's a folder, and we're in recursive mode.
 344          $file     = trailingslashit( $file );
 345          $filelist = $this->dirlist( $file, true );
 346  
 347          $retval = true;
 348          if ( is_array( $filelist ) ) {
 349              foreach ( $filelist as $filename => $fileinfo ) {
 350                  if ( ! $this->delete( $file . $filename, $recursive, $fileinfo['type'] ) ) {
 351                      $retval = false;
 352                  }
 353              }
 354          }
 355  
 356          if ( file_exists( $file ) && ! @rmdir( $file ) ) {
 357              $retval = false;
 358          }
 359  
 360          return $retval;
 361      }
 362  
 363      /**
 364       * Checks if a file or directory exists.
 365       *
 366       * @since 2.5.0
 367       *
 368       * @param string $file Path to file or directory.
 369       * @return bool Whether $file exists or not.
 370       */
 371  	public function exists( $file ) {
 372          return @file_exists( $file );
 373      }
 374  
 375      /**
 376       * Checks if resource is a file.
 377       *
 378       * @since 2.5.0
 379       *
 380       * @param string $file File path.
 381       * @return bool Whether $file is a file.
 382       */
 383  	public function is_file( $file ) {
 384          return @is_file( $file );
 385      }
 386  
 387      /**
 388       * Checks if resource is a directory.
 389       *
 390       * @since 2.5.0
 391       *
 392       * @param string $path Directory path.
 393       * @return bool Whether $path is a directory.
 394       */
 395  	public function is_dir( $path ) {
 396          return @is_dir( $path );
 397      }
 398  
 399      /**
 400       * Checks if a file is readable.
 401       *
 402       * @since 2.5.0
 403       *
 404       * @param string $file Path to file.
 405       * @return bool Whether $file is readable.
 406       */
 407  	public function is_readable( $file ) {
 408          return @is_readable( $file );
 409      }
 410  
 411      /**
 412       * Checks if a file or directory is writable.
 413       *
 414       * @since 2.5.0
 415       *
 416       * @param string $file Path to file or directory.
 417       * @return bool Whether $file is writable.
 418       */
 419  	public function is_writable( $file ) {
 420          return @is_writable( $file );
 421      }
 422  
 423      /**
 424       * Gets the file's last access time.
 425       *
 426       * @since 2.5.0
 427       *
 428       * @param string $file Path to file.
 429       * @return int|false Unix timestamp representing last access time, false on failure.
 430       */
 431  	public function atime( $file ) {
 432          return @fileatime( $file );
 433      }
 434  
 435      /**
 436       * Gets the file modification time.
 437       *
 438       * @since 2.5.0
 439       *
 440       * @param string $file Path to file.
 441       * @return int|false Unix timestamp representing modification time, false on failure.
 442       */
 443  	public function mtime( $file ) {
 444          return @filemtime( $file );
 445      }
 446  
 447      /**
 448       * Gets the file size (in bytes).
 449       *
 450       * @since 2.5.0
 451       *
 452       * @param string $file Path to file.
 453       * @return int|false Size of the file in bytes on success, false on failure.
 454       */
 455  	public function size( $file ) {
 456          return @filesize( $file );
 457      }
 458  
 459      /**
 460       * Sets the access and modification times of a file.
 461       *
 462       * Note: If $file doesn't exist, it will be created.
 463       *
 464       * @since 2.5.0
 465       *
 466       * @param string $file  Path to file.
 467       * @param int    $time  Optional. Modified time to set for file.
 468       *                      Default 0.
 469       * @param int    $atime Optional. Access time to set for file.
 470       *                      Default 0.
 471       * @return bool True on success, false on failure.
 472       */
 473  	public function touch( $file, $time = 0, $atime = 0 ) {
 474          if ( 0 == $time ) {
 475              $time = time();
 476          }
 477          if ( 0 == $atime ) {
 478              $atime = time();
 479          }
 480          return touch( $file, $time, $atime );
 481      }
 482  
 483      /**
 484       * Creates a directory.
 485       *
 486       * @since 2.5.0
 487       *
 488       * @param string     $path  Path for new directory.
 489       * @param int|false  $chmod Optional. The permissions as octal number (or false to skip chmod).
 490       *                          Default false.
 491       * @param string|int $chown Optional. A user name or number (or false to skip chown).
 492       *                          Default false.
 493       * @param string|int $chgrp Optional. A group name or number (or false to skip chgrp).
 494       *                          Default false.
 495       * @return bool True on success, false on failure.
 496       */
 497  	public function mkdir( $path, $chmod = false, $chown = false, $chgrp = false ) {
 498          // Safe mode fails with a trailing slash under certain PHP versions.
 499          $path = untrailingslashit( $path );
 500          if ( empty( $path ) ) {
 501              return false;
 502          }
 503  
 504          if ( ! $chmod ) {
 505              $chmod = FS_CHMOD_DIR;
 506          }
 507  
 508          if ( ! @mkdir( $path ) ) {
 509              return false;
 510          }
 511          $this->chmod( $path, $chmod );
 512          if ( $chown ) {
 513              $this->chown( $path, $chown );
 514          }
 515          if ( $chgrp ) {
 516              $this->chgrp( $path, $chgrp );
 517          }
 518          return true;
 519      }
 520  
 521      /**
 522       * Deletes a directory.
 523       *
 524       * @since 2.5.0
 525       *
 526       * @param string $path      Path to directory.
 527       * @param bool   $recursive Optional. Whether to recursively remove files/directories.
 528       *                          Default false.
 529       * @return bool True on success, false on failure.
 530       */
 531  	public function rmdir( $path, $recursive = false ) {
 532          return $this->delete( $path, $recursive );
 533      }
 534  
 535      /**
 536       * Gets details for files in a directory or a specific file.
 537       *
 538       * @since 2.5.0
 539       *
 540       * @param string $path           Path to directory or file.
 541       * @param bool   $include_hidden Optional. Whether to include details of hidden ("." prefixed) files.
 542       *                               Default true.
 543       * @param bool   $recursive      Optional. Whether to recursively include file details in nested directories.
 544       *                               Default false.
 545       * @return array|false {
 546       *     Array of files. False if unable to list directory contents.
 547       *
 548       *     @type string $name        Name of the file or directory.
 549       *     @type string $perms       *nix representation of permissions.
 550       *     @type int    $permsn      Octal representation of permissions.
 551       *     @type string $owner       Owner name or ID.
 552       *     @type int    $size        Size of file in bytes.
 553       *     @type int    $lastmodunix Last modified unix timestamp.
 554       *     @type mixed  $lastmod     Last modified month (3 letter) and day (without leading 0).
 555       *     @type int    $time        Last modified time.
 556       *     @type string $type        Type of resource. 'f' for file, 'd' for directory.
 557       *     @type mixed  $files       If a directory and $recursive is true, contains another array of files.
 558       * }
 559       */
 560  	public function dirlist( $path, $include_hidden = true, $recursive = false ) {
 561          if ( $this->is_file( $path ) ) {
 562              $limit_file = basename( $path );
 563              $path       = dirname( $path );
 564          } else {
 565              $limit_file = false;
 566          }
 567  
 568          if ( ! $this->is_dir( $path ) || ! $this->is_readable( $path ) ) {
 569              return false;
 570          }
 571  
 572          $dir = dir( $path );
 573          if ( ! $dir ) {
 574              return false;
 575          }
 576  
 577          $ret = array();
 578  
 579          while ( false !== ( $entry = $dir->read() ) ) {
 580              $struc         = array();
 581              $struc['name'] = $entry;
 582  
 583              if ( '.' === $struc['name'] || '..' === $struc['name'] ) {
 584                  continue;
 585              }
 586  
 587              if ( ! $include_hidden && '.' === $struc['name'][0] ) {
 588                  continue;
 589              }
 590  
 591              if ( $limit_file && $struc['name'] != $limit_file ) {
 592                  continue;
 593              }
 594  
 595              $struc['perms']       = $this->gethchmod( $path . '/' . $entry );
 596              $struc['permsn']      = $this->getnumchmodfromh( $struc['perms'] );
 597              $struc['number']      = false;
 598              $struc['owner']       = $this->owner( $path . '/' . $entry );
 599              $struc['group']       = $this->group( $path . '/' . $entry );
 600              $struc['size']        = $this->size( $path . '/' . $entry );
 601              $struc['lastmodunix'] = $this->mtime( $path . '/' . $entry );
 602              $struc['lastmod']     = gmdate( 'M j', $struc['lastmodunix'] );
 603              $struc['time']        = gmdate( 'h:i:s', $struc['lastmodunix'] );
 604              $struc['type']        = $this->is_dir( $path . '/' . $entry ) ? 'd' : 'f';
 605  
 606              if ( 'd' === $struc['type'] ) {
 607                  if ( $recursive ) {
 608                      $struc['files'] = $this->dirlist( $path . '/' . $struc['name'], $include_hidden, $recursive );
 609                  } else {
 610                      $struc['files'] = array();
 611                  }
 612              }
 613  
 614              $ret[ $struc['name'] ] = $struc;
 615          }
 616          $dir->close();
 617          unset( $dir );
 618          return $ret;
 619      }
 620  }


Generated : Wed Jun 3 08:20:02 2020 Cross-referenced by PHPXref