[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

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

   1  <?php
   2  /**
   3   * WordPress Filesystem Class for implementing SSH2
   4   *
   5   * To use this class you must follow these steps for PHP 5.2.6+
   6   *
   7   * @contrib http://kevin.vanzonneveld.net/techblog/article/make_ssh_connections_with_php/ - Installation Notes
   8   *
   9   * Complie libssh2 (Note: Only 0.14 is officaly working with PHP 5.2.6+ right now, But many users have found the latest versions work)
  10   *
  11   * cd /usr/src
  12   * wget http://surfnet.dl.sourceforge.net/sourceforge/libssh2/libssh2-0.14.tar.gz
  13   * tar -zxvf libssh2-0.14.tar.gz
  14   * cd libssh2-0.14/
  15   * ./configure
  16   * make all install
  17   *
  18   * Note: Do not leave the directory yet!
  19   *
  20   * Enter: pecl install -f ssh2
  21   *
  22   * Copy the ssh.so file it creates to your PHP Module Directory.
  23   * Open up your PHP.INI file and look for where extensions are placed.
  24   * Add in your PHP.ini file: extension=ssh2.so
  25   *
  26   * Restart Apache!
  27   * Check phpinfo() streams to confirm that: ssh2.shell, ssh2.exec, ssh2.tunnel, ssh2.scp, ssh2.sftp  exist.
  28   *
  29   * Note: as of WordPress 2.8, This utilises the PHP5+ function 'stream_get_contents'
  30   *
  31   * @since 2.7.0
  32   *
  33   * @package WordPress
  34   * @subpackage Filesystem
  35   */
  36  class WP_Filesystem_SSH2 extends WP_Filesystem_Base {
  37  
  38      /**
  39       * @since 2.7.0
  40       * @var resource
  41       */
  42      public $link = false;
  43  
  44      /**
  45       * @since 2.7.0
  46       * @var resource
  47       */
  48      public $sftp_link;
  49  
  50      /**
  51       * @since 2.7.0
  52       * @var bool
  53       */
  54      public $keys = false;
  55  
  56      /**
  57       * Constructor.
  58       *
  59       * @since 2.7.0
  60       *
  61       * @param array $opt
  62       */
  63  	public function __construct( $opt = '' ) {
  64          $this->method = 'ssh2';
  65          $this->errors = new WP_Error();
  66  
  67          //Check if possible to use ssh2 functions.
  68          if ( ! extension_loaded( 'ssh2' ) ) {
  69              $this->errors->add( 'no_ssh2_ext', __( 'The ssh2 PHP extension is not available' ) );
  70              return;
  71          }
  72          if ( ! function_exists( 'stream_get_contents' ) ) {
  73              $this->errors->add(
  74                  'ssh2_php_requirement',
  75                  sprintf(
  76                      /* translators: %s: stream_get_contents() */
  77                      __( 'The ssh2 PHP extension is available, however, we require the PHP5 function %s' ),
  78                      '<code>stream_get_contents()</code>'
  79                  )
  80              );
  81              return;
  82          }
  83  
  84          // Set defaults:
  85          if ( empty( $opt['port'] ) ) {
  86              $this->options['port'] = 22;
  87          } else {
  88              $this->options['port'] = $opt['port'];
  89          }
  90  
  91          if ( empty( $opt['hostname'] ) ) {
  92              $this->errors->add( 'empty_hostname', __( 'SSH2 hostname is required' ) );
  93          } else {
  94              $this->options['hostname'] = $opt['hostname'];
  95          }
  96  
  97          // Check if the options provided are OK.
  98          if ( ! empty( $opt['public_key'] ) && ! empty( $opt['private_key'] ) ) {
  99              $this->options['public_key']  = $opt['public_key'];
 100              $this->options['private_key'] = $opt['private_key'];
 101  
 102              $this->options['hostkey'] = array( 'hostkey' => 'ssh-rsa' );
 103  
 104              $this->keys = true;
 105          } elseif ( empty( $opt['username'] ) ) {
 106              $this->errors->add( 'empty_username', __( 'SSH2 username is required' ) );
 107          }
 108  
 109          if ( ! empty( $opt['username'] ) ) {
 110              $this->options['username'] = $opt['username'];
 111          }
 112  
 113          if ( empty( $opt['password'] ) ) {
 114              // Password can be blank if we are using keys.
 115              if ( ! $this->keys ) {
 116                  $this->errors->add( 'empty_password', __( 'SSH2 password is required' ) );
 117              }
 118          } else {
 119              $this->options['password'] = $opt['password'];
 120          }
 121      }
 122  
 123      /**
 124       * Connects filesystem.
 125       *
 126       * @since 2.7.0
 127       *
 128       * @return bool True on success, false on failure.
 129       */
 130  	public function connect() {
 131          if ( ! $this->keys ) {
 132              $this->link = @ssh2_connect( $this->options['hostname'], $this->options['port'] );
 133          } else {
 134              $this->link = @ssh2_connect( $this->options['hostname'], $this->options['port'], $this->options['hostkey'] );
 135          }
 136  
 137          if ( ! $this->link ) {
 138              $this->errors->add(
 139                  'connect',
 140                  sprintf(
 141                      /* translators: %s: hostname:port */
 142                      __( 'Failed to connect to SSH2 Server %s' ),
 143                      $this->options['hostname'] . ':' . $this->options['port']
 144                  )
 145              );
 146              return false;
 147          }
 148  
 149          if ( ! $this->keys ) {
 150              if ( ! @ssh2_auth_password( $this->link, $this->options['username'], $this->options['password'] ) ) {
 151                  $this->errors->add(
 152                      'auth',
 153                      sprintf(
 154                          /* translators: %s: Username. */
 155                          __( 'Username/Password incorrect for %s' ),
 156                          $this->options['username']
 157                      )
 158                  );
 159                  return false;
 160              }
 161          } else {
 162              if ( ! @ssh2_auth_pubkey_file( $this->link, $this->options['username'], $this->options['public_key'], $this->options['private_key'], $this->options['password'] ) ) {
 163                  $this->errors->add(
 164                      'auth',
 165                      sprintf(
 166                          /* translators: %s: Username. */
 167                          __( 'Public and Private keys incorrect for %s' ),
 168                          $this->options['username']
 169                      )
 170                  );
 171                  return false;
 172              }
 173          }
 174  
 175          $this->sftp_link = ssh2_sftp( $this->link );
 176          if ( ! $this->sftp_link ) {
 177              $this->errors->add(
 178                  'connect',
 179                  sprintf(
 180                      /* translators: %s: hostname:port */
 181                      __( 'Failed to initialize a SFTP subsystem session with the SSH2 Server %s' ),
 182                      $this->options['hostname'] . ':' . $this->options['port']
 183                  )
 184              );
 185              return false;
 186          }
 187  
 188          return true;
 189      }
 190  
 191      /**
 192       * Gets the ssh2.sftp PHP stream wrapper path to open for the given file.
 193       *
 194       * This method also works around a PHP bug where the root directory (/) cannot
 195       * be opened by PHP functions, causing a false failure. In order to work around
 196       * this, the path is converted to /./ which is semantically the same as /
 197       * See https://bugs.php.net/bug.php?id=64169 for more details.
 198       *
 199       * @since 4.4.0
 200       *
 201       * @param string $path The File/Directory path on the remote server to return
 202       * @return string The ssh2.sftp:// wrapped path to use.
 203       */
 204  	public function sftp_path( $path ) {
 205          if ( '/' === $path ) {
 206              $path = '/./';
 207          }
 208          return 'ssh2.sftp://' . $this->sftp_link . '/' . ltrim( $path, '/' );
 209      }
 210  
 211      /**
 212       * @since 2.7.0
 213       *
 214       * @param string $command
 215       * @param bool $returnbool
 216       * @return bool|string True on success, false on failure. String if the command was executed, `$returnbool`
 217       *                     is false (default), and data from the resulting stream was retrieved.
 218       */
 219  	public function run_command( $command, $returnbool = false ) {
 220          if ( ! $this->link ) {
 221              return false;
 222          }
 223  
 224          $stream = ssh2_exec( $this->link, $command );
 225          if ( ! $stream ) {
 226              $this->errors->add(
 227                  'command',
 228                  sprintf(
 229                      /* translators: %s: Command. */
 230                      __( 'Unable to perform command: %s' ),
 231                      $command
 232                  )
 233              );
 234          } else {
 235              stream_set_blocking( $stream, true );
 236              stream_set_timeout( $stream, FS_TIMEOUT );
 237              $data = stream_get_contents( $stream );
 238              fclose( $stream );
 239  
 240              if ( $returnbool ) {
 241                  return ( $data === false ) ? false : '' != trim( $data );
 242              } else {
 243                  return $data;
 244              }
 245          }
 246          return false;
 247      }
 248  
 249      /**
 250       * Reads entire file into a string.
 251       *
 252       * @since 2.7.0
 253       *
 254       * @param string $file Name of the file to read.
 255       * @return string|false Read data on success, false if no temporary file could be opened,
 256       *                      or if the file couldn't be retrieved.
 257       */
 258  	public function get_contents( $file ) {
 259          return file_get_contents( $this->sftp_path( $file ) );
 260      }
 261  
 262      /**
 263       * Reads entire file into an array.
 264       *
 265       * @since 2.7.0
 266       *
 267       * @param string $file Path to the file.
 268       * @return array|false File contents in an array on success, false on failure.
 269       */
 270  	public function get_contents_array( $file ) {
 271          return file( $this->sftp_path( $file ) );
 272      }
 273  
 274      /**
 275       * Writes a string to a file.
 276       *
 277       * @since 2.7.0
 278       *
 279       * @param string    $file     Remote path to the file where to write the data.
 280       * @param string    $contents The data to write.
 281       * @param int|false $mode     Optional. The file permissions as octal number, usually 0644.
 282       *                            Default false.
 283       * @return bool True on success, false on failure.
 284       */
 285  	public function put_contents( $file, $contents, $mode = false ) {
 286          $ret = file_put_contents( $this->sftp_path( $file ), $contents );
 287  
 288          if ( $ret !== strlen( $contents ) ) {
 289              return false;
 290          }
 291  
 292          $this->chmod( $file, $mode );
 293  
 294          return true;
 295      }
 296  
 297      /**
 298       * Gets the current working directory.
 299       *
 300       * @since 2.7.0
 301       *
 302       * @return string|false The current working directory on success, false on failure.
 303       */
 304  	public function cwd() {
 305          $cwd = ssh2_sftp_realpath( $this->sftp_link, '.' );
 306          if ( $cwd ) {
 307              $cwd = trailingslashit( trim( $cwd ) );
 308          }
 309          return $cwd;
 310      }
 311  
 312      /**
 313       * Changes current directory.
 314       *
 315       * @since 2.7.0
 316       *
 317       * @param string $dir The new current directory.
 318       * @return bool True on success, false on failure.
 319       */
 320  	public function chdir( $dir ) {
 321          return $this->run_command( 'cd ' . $dir, true );
 322      }
 323  
 324      /**
 325       * Changes the file group.
 326       *
 327       * @since 2.7.0
 328       *
 329       * @param string     $file      Path to the file.
 330       * @param string|int $group     A group name or number.
 331       * @param bool       $recursive Optional. If set to true, changes file group recursively.
 332       *                              Default false.
 333       * @return bool True on success, false on failure.
 334       */
 335  	public function chgrp( $file, $group, $recursive = false ) {
 336          if ( ! $this->exists( $file ) ) {
 337              return false;
 338          }
 339          if ( ! $recursive || ! $this->is_dir( $file ) ) {
 340              return $this->run_command( sprintf( 'chgrp %s %s', escapeshellarg( $group ), escapeshellarg( $file ) ), true );
 341          }
 342          return $this->run_command( sprintf( 'chgrp -R %s %s', escapeshellarg( $group ), escapeshellarg( $file ) ), true );
 343      }
 344  
 345      /**
 346       * Changes filesystem permissions.
 347       *
 348       * @since 2.7.0
 349       *
 350       * @param string    $file      Path to the file.
 351       * @param int|false $mode      Optional. The permissions as octal number, usually 0644 for files,
 352       *                             0755 for directories. Default false.
 353       * @param bool      $recursive Optional. If set to true, changes file group recursively.
 354       *                             Default false.
 355       * @return bool True on success, false on failure.
 356       */
 357  	public function chmod( $file, $mode = false, $recursive = false ) {
 358          if ( ! $this->exists( $file ) ) {
 359              return false;
 360          }
 361  
 362          if ( ! $mode ) {
 363              if ( $this->is_file( $file ) ) {
 364                  $mode = FS_CHMOD_FILE;
 365              } elseif ( $this->is_dir( $file ) ) {
 366                  $mode = FS_CHMOD_DIR;
 367              } else {
 368                  return false;
 369              }
 370          }
 371  
 372          if ( ! $recursive || ! $this->is_dir( $file ) ) {
 373              return $this->run_command( sprintf( 'chmod %o %s', $mode, escapeshellarg( $file ) ), true );
 374          }
 375          return $this->run_command( sprintf( 'chmod -R %o %s', $mode, escapeshellarg( $file ) ), true );
 376      }
 377  
 378      /**
 379       * Changes the owner of a file or directory.
 380       *
 381       * @since 2.7.0
 382       *
 383       * @param string     $file      Path to the file or directory.
 384       * @param string|int $owner     A user name or number.
 385       * @param bool       $recursive Optional. If set to true, changes file owner recursively.
 386       *                              Default false.
 387       * @return bool True on success, false on failure.
 388       */
 389  	public function chown( $file, $owner, $recursive = false ) {
 390          if ( ! $this->exists( $file ) ) {
 391              return false;
 392          }
 393          if ( ! $recursive || ! $this->is_dir( $file ) ) {
 394              return $this->run_command( sprintf( 'chown %s %s', escapeshellarg( $owner ), escapeshellarg( $file ) ), true );
 395          }
 396          return $this->run_command( sprintf( 'chown -R %s %s', escapeshellarg( $owner ), escapeshellarg( $file ) ), true );
 397      }
 398  
 399      /**
 400       * Gets the file owner.
 401       *
 402       * @since 2.7.0
 403       *
 404       * @param string $file Path to the file.
 405       * @return string|false Username of the owner on success, false on failure.
 406       */
 407  	public function owner( $file ) {
 408          $owneruid = @fileowner( $this->sftp_path( $file ) );
 409          if ( ! $owneruid ) {
 410              return false;
 411          }
 412          if ( ! function_exists( 'posix_getpwuid' ) ) {
 413              return $owneruid;
 414          }
 415          $ownerarray = posix_getpwuid( $owneruid );
 416          return $ownerarray['name'];
 417      }
 418  
 419      /**
 420       * Gets the permissions of the specified file or filepath in their octal format.
 421       *
 422       * @since 2.7.0
 423       *
 424       * @param string $file Path to the file.
 425       * @return string Mode of the file (the last 3 digits).
 426       */
 427  	public function getchmod( $file ) {
 428          return substr( decoct( @fileperms( $this->sftp_path( $file ) ) ), -3 );
 429      }
 430  
 431      /**
 432       * Gets the file's group.
 433       *
 434       * @since 2.7.0
 435       *
 436       * @param string $file Path to the file.
 437       * @return string|false The group on success, false on failure.
 438       */
 439  	public function group( $file ) {
 440          $gid = @filegroup( $this->sftp_path( $file ) );
 441          if ( ! $gid ) {
 442              return false;
 443          }
 444          if ( ! function_exists( 'posix_getgrgid' ) ) {
 445              return $gid;
 446          }
 447          $grouparray = posix_getgrgid( $gid );
 448          return $grouparray['name'];
 449      }
 450  
 451      /**
 452       * Copies a file.
 453       *
 454       * @since 2.7.0
 455       *
 456       * @param string    $source      Path to the source file.
 457       * @param string    $destination Path to the destination file.
 458       * @param bool      $overwrite   Optional. Whether to overwrite the destination file if it exists.
 459       *                               Default false.
 460       * @param int|false $mode        Optional. The permissions as octal number, usually 0644 for files,
 461       *                               0755 for dirs. Default false.
 462       * @return bool True on success, false on failure.
 463       */
 464  	public function copy( $source, $destination, $overwrite = false, $mode = false ) {
 465          if ( ! $overwrite && $this->exists( $destination ) ) {
 466              return false;
 467          }
 468          $content = $this->get_contents( $source );
 469          if ( false === $content ) {
 470              return false;
 471          }
 472          return $this->put_contents( $destination, $content, $mode );
 473      }
 474  
 475      /**
 476       * Moves a file.
 477       *
 478       * @since 2.7.0
 479       *
 480       * @param string $source      Path to the source file.
 481       * @param string $destination Path to the destination file.
 482       * @param bool   $overwrite   Optional. Whether to overwrite the destination file if it exists.
 483       *                            Default false.
 484       * @return bool True on success, false on failure.
 485       */
 486  	public function move( $source, $destination, $overwrite = false ) {
 487          if ( $this->exists( $destination ) ) {
 488              if ( $overwrite ) {
 489                  // We need to remove the destination file before we can rename the source.
 490                  $this->delete( $destination, false, 'f' );
 491              } else {
 492                  // If we're not overwriting, the rename will fail, so return early.
 493                  return false;
 494              }
 495          }
 496  
 497          return ssh2_sftp_rename( $this->sftp_link, $source, $destination );
 498      }
 499  
 500      /**
 501       * Deletes a file or directory.
 502       *
 503       * @since 2.7.0
 504       *
 505       * @param string       $file      Path to the file or directory.
 506       * @param bool         $recursive Optional. If set to true, changes file group recursively.
 507       *                                Default false.
 508       * @param string|false $type      Type of resource. 'f' for file, 'd' for directory.
 509       *                                Default false.
 510       * @return bool True on success, false on failure.
 511       */
 512  	public function delete( $file, $recursive = false, $type = false ) {
 513          if ( 'f' == $type || $this->is_file( $file ) ) {
 514              return ssh2_sftp_unlink( $this->sftp_link, $file );
 515          }
 516          if ( ! $recursive ) {
 517              return ssh2_sftp_rmdir( $this->sftp_link, $file );
 518          }
 519          $filelist = $this->dirlist( $file );
 520          if ( is_array( $filelist ) ) {
 521              foreach ( $filelist as $filename => $fileinfo ) {
 522                  $this->delete( $file . '/' . $filename, $recursive, $fileinfo['type'] );
 523              }
 524          }
 525          return ssh2_sftp_rmdir( $this->sftp_link, $file );
 526      }
 527  
 528      /**
 529       * Checks if a file or directory exists.
 530       *
 531       * @since 2.7.0
 532       *
 533       * @param string $file Path to file or directory.
 534       * @return bool Whether $file exists or not.
 535       */
 536  	public function exists( $file ) {
 537          return file_exists( $this->sftp_path( $file ) );
 538      }
 539  
 540      /**
 541       * Checks if resource is a file.
 542       *
 543       * @since 2.7.0
 544       *
 545       * @param string $file File path.
 546       * @return bool Whether $file is a file.
 547       */
 548  	public function is_file( $file ) {
 549          return is_file( $this->sftp_path( $file ) );
 550      }
 551  
 552      /**
 553       * Checks if resource is a directory.
 554       *
 555       * @since 2.7.0
 556       *
 557       * @param string $path Directory path.
 558       * @return bool Whether $path is a directory.
 559       */
 560  	public function is_dir( $path ) {
 561          return is_dir( $this->sftp_path( $path ) );
 562      }
 563  
 564      /**
 565       * Checks if a file is readable.
 566       *
 567       * @since 2.7.0
 568       *
 569       * @param string $file Path to file.
 570       * @return bool Whether $file is readable.
 571       */
 572  	public function is_readable( $file ) {
 573          return is_readable( $this->sftp_path( $file ) );
 574      }
 575  
 576      /**
 577       * Checks if a file or directory is writable.
 578       *
 579       * @since 2.7.0
 580       *
 581       * @param string $file Path to file or directory.
 582       * @return bool Whether $file is writable.
 583       */
 584  	public function is_writable( $file ) {
 585          // PHP will base it's writable checks on system_user === file_owner, not ssh_user === file_owner
 586          return true;
 587      }
 588  
 589      /**
 590       * Gets the file's last access time.
 591       *
 592       * @since 2.7.0
 593       *
 594       * @param string $file Path to file.
 595       * @return int|false Unix timestamp representing last access time, false on failure.
 596       */
 597  	public function atime( $file ) {
 598          return fileatime( $this->sftp_path( $file ) );
 599      }
 600  
 601      /**
 602       * Gets the file modification time.
 603       *
 604       * @since 2.7.0
 605       *
 606       * @param string $file Path to file.
 607       * @return int|false Unix timestamp representing modification time, false on failure.
 608       */
 609  	public function mtime( $file ) {
 610          return filemtime( $this->sftp_path( $file ) );
 611      }
 612  
 613      /**
 614       * Gets the file size (in bytes).
 615       *
 616       * @since 2.7.0
 617       *
 618       * @param string $file Path to file.
 619       * @return int|false Size of the file in bytes on success, false on failure.
 620       */
 621  	public function size( $file ) {
 622          return filesize( $this->sftp_path( $file ) );
 623      }
 624  
 625      /**
 626       * Sets the access and modification times of a file.
 627       *
 628       * Note: Not implemented.
 629       *
 630       * @since 2.7.0
 631       *
 632       * @param string $file  Path to file.
 633       * @param int    $time  Optional. Modified time to set for file.
 634       *                      Default 0.
 635       * @param int    $atime Optional. Access time to set for file.
 636       *                      Default 0.
 637       */
 638  	public function touch( $file, $time = 0, $atime = 0 ) {
 639          // Not implemented.
 640      }
 641  
 642      /**
 643       * Creates a directory.
 644       *
 645       * @since 2.7.0
 646       *
 647       * @param string     $path  Path for new directory.
 648       * @param int|false  $chmod Optional. The permissions as octal number (or false to skip chmod).
 649       *                          Default false.
 650       * @param string|int $chown Optional. A user name or number (or false to skip chown).
 651       *                          Default false.
 652       * @param string|int $chgrp Optional. A group name or number (or false to skip chgrp).
 653       *                          Default false.
 654       * @return bool True on success, false on failure.
 655       */
 656  	public function mkdir( $path, $chmod = false, $chown = false, $chgrp = false ) {
 657          $path = untrailingslashit( $path );
 658          if ( empty( $path ) ) {
 659              return false;
 660          }
 661  
 662          if ( ! $chmod ) {
 663              $chmod = FS_CHMOD_DIR;
 664          }
 665          if ( ! ssh2_sftp_mkdir( $this->sftp_link, $path, $chmod, true ) ) {
 666              return false;
 667          }
 668          if ( $chown ) {
 669              $this->chown( $path, $chown );
 670          }
 671          if ( $chgrp ) {
 672              $this->chgrp( $path, $chgrp );
 673          }
 674          return true;
 675      }
 676  
 677      /**
 678       * Deletes a directory.
 679       *
 680       * @since 2.7.0
 681       *
 682       * @param string $path      Path to directory.
 683       * @param bool   $recursive Optional. Whether to recursively remove files/directories.
 684       *                          Default false.
 685       * @return bool True on success, false on failure.
 686       */
 687  	public function rmdir( $path, $recursive = false ) {
 688          return $this->delete( $path, $recursive );
 689      }
 690  
 691      /**
 692       * Gets details for files in a directory or a specific file.
 693       *
 694       * @since 2.7.0
 695       *
 696       * @param string $path           Path to directory or file.
 697       * @param bool   $include_hidden Optional. Whether to include details of hidden ("." prefixed) files.
 698       *                               Default true.
 699       * @param bool   $recursive      Optional. Whether to recursively include file details in nested directories.
 700       *                               Default false.
 701       * @return array|false {
 702       *     Array of files. False if unable to list directory contents.
 703       *
 704       *     @type string $name        Name of the file or directory.
 705       *     @type string $perms       *nix representation of permissions.
 706       *     @type int    $permsn      Octal representation of permissions.
 707       *     @type string $owner       Owner name or ID.
 708       *     @type int    $size        Size of file in bytes.
 709       *     @type int    $lastmodunix Last modified unix timestamp.
 710       *     @type mixed  $lastmod     Last modified month (3 letter) and day (without leading 0).
 711       *     @type int    $time        Last modified time.
 712       *     @type string $type        Type of resource. 'f' for file, 'd' for directory.
 713       *     @type mixed  $files       If a directory and $recursive is true, contains another array of files.
 714       * }
 715       */
 716  	public function dirlist( $path, $include_hidden = true, $recursive = false ) {
 717          if ( $this->is_file( $path ) ) {
 718              $limit_file = basename( $path );
 719              $path       = dirname( $path );
 720          } else {
 721              $limit_file = false;
 722          }
 723  
 724          if ( ! $this->is_dir( $path ) || ! $this->is_readable( $path ) ) {
 725              return false;
 726          }
 727  
 728          $ret = array();
 729          $dir = dir( $this->sftp_path( $path ) );
 730  
 731          if ( ! $dir ) {
 732              return false;
 733          }
 734  
 735          while ( false !== ( $entry = $dir->read() ) ) {
 736              $struc         = array();
 737              $struc['name'] = $entry;
 738  
 739              if ( '.' == $struc['name'] || '..' == $struc['name'] ) {
 740                  continue; //Do not care about these folders.
 741              }
 742  
 743              if ( ! $include_hidden && '.' == $struc['name'][0] ) {
 744                  continue;
 745              }
 746  
 747              if ( $limit_file && $struc['name'] != $limit_file ) {
 748                  continue;
 749              }
 750  
 751              $struc['perms']       = $this->gethchmod( $path . '/' . $entry );
 752              $struc['permsn']      = $this->getnumchmodfromh( $struc['perms'] );
 753              $struc['number']      = false;
 754              $struc['owner']       = $this->owner( $path . '/' . $entry );
 755              $struc['group']       = $this->group( $path . '/' . $entry );
 756              $struc['size']        = $this->size( $path . '/' . $entry );
 757              $struc['lastmodunix'] = $this->mtime( $path . '/' . $entry );
 758              $struc['lastmod']     = gmdate( 'M j', $struc['lastmodunix'] );
 759              $struc['time']        = gmdate( 'h:i:s', $struc['lastmodunix'] );
 760              $struc['type']        = $this->is_dir( $path . '/' . $entry ) ? 'd' : 'f';
 761  
 762              if ( 'd' == $struc['type'] ) {
 763                  if ( $recursive ) {
 764                      $struc['files'] = $this->dirlist( $path . '/' . $struc['name'], $include_hidden, $recursive );
 765                  } else {
 766                      $struc['files'] = array();
 767                  }
 768              }
 769  
 770              $ret[ $struc['name'] ] = $struc;
 771          }
 772          $dir->close();
 773          unset( $dir );
 774          return $ret;
 775      }
 776  }


Generated: Tue Sep 17 01:00:03 2019 Cross-referenced by PHPXref 0.7.1