wp/wp-admin/includes/class-wp-filesystem-ftpsockets.php
changeset 21 48c4eec2b7e6
parent 19 3d72ae0968f4
equal deleted inserted replaced
20:7b1b88e27a20 21:48c4eec2b7e6
    31 	public function __construct( $opt = '' ) {
    31 	public function __construct( $opt = '' ) {
    32 		$this->method = 'ftpsockets';
    32 		$this->method = 'ftpsockets';
    33 		$this->errors = new WP_Error();
    33 		$this->errors = new WP_Error();
    34 
    34 
    35 		// Check if possible to use ftp functions.
    35 		// Check if possible to use ftp functions.
    36 		if ( ! include_once ABSPATH . 'wp-admin/includes/class-ftp.php' ) {
    36 		if ( ! require_once ABSPATH . 'wp-admin/includes/class-ftp.php' ) {
    37 			return;
    37 			return;
    38 		}
    38 		}
    39 
    39 
    40 		$this->ftp = new ftp();
    40 		$this->ftp = new ftp();
    41 
    41 
   366 
   366 
   367 		return $this->put_contents( $destination, $content, $mode );
   367 		return $this->put_contents( $destination, $content, $mode );
   368 	}
   368 	}
   369 
   369 
   370 	/**
   370 	/**
   371 	 * Moves a file.
   371 	 * Moves a file or directory.
   372 	 *
   372 	 *
   373 	 * @since 2.5.0
   373 	 * After moving files or directories, OPcache will need to be invalidated.
   374 	 *
   374 	 *
   375 	 * @param string $source      Path to the source file.
   375 	 * If moving a directory fails, `copy_dir()` can be used for a recursive copy.
   376 	 * @param string $destination Path to the destination file.
   376 	 *
   377 	 * @param bool   $overwrite   Optional. Whether to overwrite the destination file if it exists.
   377 	 * Use `move_dir()` for moving directories with OPcache invalidation and a
       
   378 	 * fallback to `copy_dir()`.
       
   379 	 *
       
   380 	 * @since 2.5.0
       
   381 	 *
       
   382 	 * @param string $source      Path to the source file or directory.
       
   383 	 * @param string $destination Path to the destination file or directory.
       
   384 	 * @param bool   $overwrite   Optional. Whether to overwrite the destination if it exists.
   378 	 *                            Default false.
   385 	 *                            Default false.
   379 	 * @return bool True on success, false on failure.
   386 	 * @return bool True on success, false on failure.
   380 	 */
   387 	 */
   381 	public function move( $source, $destination, $overwrite = false ) {
   388 	public function move( $source, $destination, $overwrite = false ) {
   382 		return $this->ftp->rename( $source, $destination );
   389 		return $this->ftp->rename( $source, $destination );
   412 
   419 
   413 	/**
   420 	/**
   414 	 * Checks if a file or directory exists.
   421 	 * Checks if a file or directory exists.
   415 	 *
   422 	 *
   416 	 * @since 2.5.0
   423 	 * @since 2.5.0
   417 	 *
   424 	 * @since 6.3.0 Returns false for an empty path.
   418 	 * @param string $file Path to file or directory.
   425 	 *
   419 	 * @return bool Whether $file exists or not.
   426 	 * @param string $path Path to file or directory.
   420 	 */
   427 	 * @return bool Whether $path exists or not.
   421 	public function exists( $file ) {
   428 	 */
   422 		$list = $this->ftp->nlist( $file );
   429 	public function exists( $path ) {
   423 
   430 		/*
   424 		if ( empty( $list ) && $this->is_dir( $file ) ) {
   431 		 * Check for empty path. If ftp::nlist() receives an empty path,
       
   432 		 * it checks the current working directory and may return true.
       
   433 		 *
       
   434 		 * See https://core.trac.wordpress.org/ticket/33058.
       
   435 		 */
       
   436 		if ( '' === $path ) {
       
   437 			return false;
       
   438 		}
       
   439 
       
   440 		$list = $this->ftp->nlist( $path );
       
   441 
       
   442 		if ( empty( $list ) && $this->is_dir( $path ) ) {
   425 			return true; // File is an empty directory.
   443 			return true; // File is an empty directory.
   426 		}
   444 		}
   427 
   445 
   428 		return ! empty( $list ); // Empty list = no file, so invert.
   446 		return ! empty( $list ); // Empty list = no file, so invert.
   429 		// Return $this->ftp->is_exists($file); has issues with ABOR+426 responses on the ncFTPd server.
   447 		// Return $this->ftp->is_exists($file); has issues with ABOR+426 responses on the ncFTPd server.
   483 	/**
   501 	/**
   484 	 * Checks if a file or directory is writable.
   502 	 * Checks if a file or directory is writable.
   485 	 *
   503 	 *
   486 	 * @since 2.5.0
   504 	 * @since 2.5.0
   487 	 *
   505 	 *
   488 	 * @param string $file Path to file or directory.
   506 	 * @param string $path Path to file or directory.
   489 	 * @return bool Whether $file is writable.
   507 	 * @return bool Whether $path is writable.
   490 	 */
   508 	 */
   491 	public function is_writable( $file ) {
   509 	public function is_writable( $path ) {
   492 		return true;
   510 		return true;
   493 	}
   511 	}
   494 
   512 
   495 	/**
   513 	/**
   496 	 * Gets the file's last access time.
   514 	 * Gets the file's last access time.
   603 	 * @param bool   $include_hidden Optional. Whether to include details of hidden ("." prefixed) files.
   621 	 * @param bool   $include_hidden Optional. Whether to include details of hidden ("." prefixed) files.
   604 	 *                               Default true.
   622 	 *                               Default true.
   605 	 * @param bool   $recursive      Optional. Whether to recursively include file details in nested directories.
   623 	 * @param bool   $recursive      Optional. Whether to recursively include file details in nested directories.
   606 	 *                               Default false.
   624 	 *                               Default false.
   607 	 * @return array|false {
   625 	 * @return array|false {
   608 	 *     Array of files. False if unable to list directory contents.
   626 	 *     Array of arrays containing file information. False if unable to list directory contents.
   609 	 *
   627 	 *
   610 	 *     @type string $name        Name of the file or directory.
   628 	 *     @type array ...$0 {
   611 	 *     @type string $perms       *nix representation of permissions.
   629 	 *         Array of file information. Note that some elements may not be available on all filesystems.
   612 	 *     @type string $permsn      Octal representation of permissions.
   630 	 *
   613 	 *     @type string $owner       Owner name or ID.
   631 	 *         @type string           $name        Name of the file or directory.
   614 	 *     @type int    $size        Size of file in bytes.
   632 	 *         @type string           $perms       *nix representation of permissions.
   615 	 *     @type int    $lastmodunix Last modified unix timestamp.
   633 	 *         @type string           $permsn      Octal representation of permissions.
   616 	 *     @type mixed  $lastmod     Last modified month (3 letter) and day (without leading 0).
   634 	 *         @type int|string|false $number      File number. May be a numeric string. False if not available.
   617 	 *     @type int    $time        Last modified time.
   635 	 *         @type string|false     $owner       Owner name or ID, or false if not available.
   618 	 *     @type string $type        Type of resource. 'f' for file, 'd' for directory.
   636 	 *         @type string|false     $group       File permissions group, or false if not available.
   619 	 *     @type mixed  $files       If a directory and `$recursive` is true, contains another array of files.
   637 	 *         @type int|string|false $size        Size of file in bytes. May be a numeric string.
       
   638 	 *                                             False if not available.
       
   639 	 *         @type int|string|false $lastmodunix Last modified unix timestamp. May be a numeric string.
       
   640 	 *                                             False if not available.
       
   641 	 *         @type string|false     $lastmod     Last modified month (3 letters) and day (without leading 0), or
       
   642 	 *                                             false if not available.
       
   643 	 *         @type string|false     $time        Last modified time, or false if not available.
       
   644 	 *         @type string           $type        Type of resource. 'f' for file, 'd' for directory, 'l' for link.
       
   645 	 *         @type array|false      $files       If a directory and `$recursive` is true, contains another array of
       
   646 	 *                                             files. False if unable to list directory contents.
       
   647 	 *     }
   620 	 * }
   648 	 * }
   621 	 */
   649 	 */
   622 	public function dirlist( $path = '.', $include_hidden = true, $recursive = false ) {
   650 	public function dirlist( $path = '.', $include_hidden = true, $recursive = false ) {
   623 		if ( $this->is_file( $path ) ) {
   651 		if ( $this->is_file( $path ) ) {
   624 			$limit_file = basename( $path );
   652 			$limit_file = basename( $path );
   636 			reset_mbstring_encoding();
   664 			reset_mbstring_encoding();
   637 
   665 
   638 			return false;
   666 			return false;
   639 		}
   667 		}
   640 
   668 
   641 		$ret = array();
   669 		$path = trailingslashit( $path );
       
   670 		$ret  = array();
   642 
   671 
   643 		foreach ( $list as $struc ) {
   672 		foreach ( $list as $struc ) {
   644 
   673 
   645 			if ( '.' === $struc['name'] || '..' === $struc['name'] ) {
   674 			if ( '.' === $struc['name'] || '..' === $struc['name'] ) {
   646 				continue;
   675 				continue;
   654 				continue;
   683 				continue;
   655 			}
   684 			}
   656 
   685 
   657 			if ( 'd' === $struc['type'] ) {
   686 			if ( 'd' === $struc['type'] ) {
   658 				if ( $recursive ) {
   687 				if ( $recursive ) {
   659 					$struc['files'] = $this->dirlist( $path . '/' . $struc['name'], $include_hidden, $recursive );
   688 					$struc['files'] = $this->dirlist( $path . $struc['name'], $include_hidden, $recursive );
   660 				} else {
   689 				} else {
   661 					$struc['files'] = array();
   690 					$struc['files'] = array();
   662 				}
   691 				}
   663 			}
   692 			}
   664 
   693