wp/wp-admin/includes/class-wp-filesystem-base.php
changeset 21 48c4eec2b7e6
parent 19 3d72ae0968f4
equal deleted inserted replaced
20:7b1b88e27a20 21:48c4eec2b7e6
     9 /**
     9 /**
    10  * Base WordPress Filesystem class which Filesystem implementations extend.
    10  * Base WordPress Filesystem class which Filesystem implementations extend.
    11  *
    11  *
    12  * @since 2.5.0
    12  * @since 2.5.0
    13  */
    13  */
       
    14 #[AllowDynamicProperties]
    14 class WP_Filesystem_Base {
    15 class WP_Filesystem_Base {
    15 
    16 
    16 	/**
    17 	/**
    17 	 * Whether to display debug data for the connection.
    18 	 * Whether to display debug data for the connection.
    18 	 *
    19 	 *
    54 	 * @return string The location of the remote path.
    55 	 * @return string The location of the remote path.
    55 	 */
    56 	 */
    56 	public function abspath() {
    57 	public function abspath() {
    57 		$folder = $this->find_folder( ABSPATH );
    58 		$folder = $this->find_folder( ABSPATH );
    58 
    59 
    59 		// Perhaps the FTP folder is rooted at the WordPress install.
    60 		/*
    60 		// Check for wp-includes folder in root. Could have some false positives, but rare.
    61 		 * Perhaps the FTP folder is rooted at the WordPress install.
       
    62 		 * Check for wp-includes folder in root. Could have some false positives, but rare.
       
    63 		 */
    61 		if ( ! $folder && $this->is_dir( '/' . WPINC ) ) {
    64 		if ( ! $folder && $this->is_dir( '/' . WPINC ) ) {
    62 			$folder = '/';
    65 			$folder = '/';
    63 		}
    66 		}
    64 
    67 
    65 		return $folder;
    68 		return $folder;
   211 						return $potential_folder;
   214 						return $potential_folder;
   212 					}
   215 					}
   213 				}
   216 				}
   214 			}
   217 			}
   215 		} elseif ( 'direct' === $this->method ) {
   218 		} elseif ( 'direct' === $this->method ) {
   216 			$folder = str_replace( '\\', '/', $folder ); // Windows path sanitisation.
   219 			$folder = str_replace( '\\', '/', $folder ); // Windows path sanitization.
   217 
   220 
   218 			return trailingslashit( $folder );
   221 			return trailingslashit( $folder );
   219 		}
   222 		}
   220 
   223 
   221 		$folder = preg_replace( '|^([a-z]{1}):|i', '', $folder ); // Strip out Windows drive letter if it's there.
   224 		$folder = preg_replace( '|^([a-z]{1}):|i', '', $folder ); // Strip out Windows drive letter if it's there.
   222 		$folder = str_replace( '\\', '/', $folder ); // Windows path sanitisation.
   225 		$folder = str_replace( '\\', '/', $folder ); // Windows path sanitization.
   223 
   226 
   224 		if ( isset( $this->cache[ $folder ] ) ) {
   227 		if ( isset( $this->cache[ $folder ] ) ) {
   225 			return $this->cache[ $folder ];
   228 			return $this->cache[ $folder ];
   226 		}
   229 		}
   227 
   230 
   260 
   263 
   261 		$folder = untrailingslashit( $folder );
   264 		$folder = untrailingslashit( $folder );
   262 
   265 
   263 		if ( $this->verbose ) {
   266 		if ( $this->verbose ) {
   264 			/* translators: 1: Folder to locate, 2: Folder to start searching from. */
   267 			/* translators: 1: Folder to locate, 2: Folder to start searching from. */
   265 			printf( "\n" . __( 'Looking for %1$s in %2$s' ) . "<br/>\n", $folder, $base );
   268 			printf( "\n" . __( 'Looking for %1$s in %2$s' ) . "<br />\n", $folder, $base );
   266 		}
   269 		}
   267 
   270 
   268 		$folder_parts     = explode( '/', $folder );
   271 		$folder_parts     = explode( '/', $folder );
   269 		$folder_part_keys = array_keys( $folder_parts );
   272 		$folder_part_keys = array_keys( $folder_parts );
   270 		$last_index       = array_pop( $folder_part_keys );
   273 		$last_index       = array_pop( $folder_part_keys );
   289 				// Let's try that folder:
   292 				// Let's try that folder:
   290 				$newdir = trailingslashit( path_join( $base, $key ) );
   293 				$newdir = trailingslashit( path_join( $base, $key ) );
   291 
   294 
   292 				if ( $this->verbose ) {
   295 				if ( $this->verbose ) {
   293 					/* translators: %s: Directory name. */
   296 					/* translators: %s: Directory name. */
   294 					printf( "\n" . __( 'Changing to %s' ) . "<br/>\n", $newdir );
   297 					printf( "\n" . __( 'Changing to %s' ) . "<br />\n", $newdir );
   295 				}
   298 				}
   296 
   299 
   297 				// Only search for the remaining path tokens in the directory, not the full path again.
   300 				// Only search for the remaining path tokens in the directory, not the full path again.
   298 				$newfolder = implode( '/', array_slice( $folder_parts, $index + 1 ) );
   301 				$newfolder = implode( '/', array_slice( $folder_parts, $index + 1 ) );
   299 				$ret       = $this->search_for_folder( $newfolder, $newdir, $loop );
   302 				$ret       = $this->search_for_folder( $newfolder, $newdir, $loop );
   302 					return $ret;
   305 					return $ret;
   303 				}
   306 				}
   304 			}
   307 			}
   305 		}
   308 		}
   306 
   309 
   307 		// Only check this as a last resort, to prevent locating the incorrect install.
   310 		/*
   308 		// All above procedures will fail quickly if this is the right branch to take.
   311 		 * Only check this as a last resort, to prevent locating the incorrect install.
       
   312 		 * All above procedures will fail quickly if this is the right branch to take.
       
   313 		 */
   309 		if ( isset( $files[ $last_path ] ) ) {
   314 		if ( isset( $files[ $last_path ] ) ) {
   310 			if ( $this->verbose ) {
   315 			if ( $this->verbose ) {
   311 				/* translators: %s: Directory name. */
   316 				/* translators: %s: Directory name. */
   312 				printf( "\n" . __( 'Found %s' ) . "<br/>\n", $base . $last_path );
   317 				printf( "\n" . __( 'Found %s' ) . "<br />\n", $base . $last_path );
   313 			}
   318 			}
   314 
   319 
   315 			return trailingslashit( $base . $last_path );
   320 			return trailingslashit( $base . $last_path );
   316 		}
   321 		}
   317 
   322 
   318 		// Prevent this function from looping again.
   323 		/*
   319 		// No need to proceed if we've just searched in `/`.
   324 		 * Prevent this function from looping again.
       
   325 		 * No need to proceed if we've just searched in `/`.
       
   326 		 */
   320 		if ( $loop || '/' === $base ) {
   327 		if ( $loop || '/' === $base ) {
   321 			return false;
   328 			return false;
   322 		}
   329 		}
   323 
   330 
   324 		// As an extra last resort, Change back to / if the folder wasn't found.
   331 		/*
   325 		// This comes into effect when the CWD is /home/user/ but WP is at /var/www/....
   332 		 * As an extra last resort, Change back to / if the folder wasn't found.
       
   333 		 * This comes into effect when the CWD is /home/user/ but WP is at /var/www/....
       
   334 		 */
   326 		return $this->search_for_folder( $folder, '/', true );
   335 		return $this->search_for_folder( $folder, '/', true );
   327 
       
   328 	}
   336 	}
   329 
   337 
   330 	/**
   338 	/**
   331 	 * Returns the *nix-style file permissions for a file.
   339 	 * Returns the *nix-style file permissions for a file.
   332 	 *
   340 	 *
   395 	public function getchmod( $file ) {
   403 	public function getchmod( $file ) {
   396 		return '777';
   404 		return '777';
   397 	}
   405 	}
   398 
   406 
   399 	/**
   407 	/**
   400 	 * Converts *nix-style file permissions to a octal number.
   408 	 * Converts *nix-style file permissions to an octal number.
   401 	 *
   409 	 *
   402 	 * Converts '-rw-r--r--' to 0644
   410 	 * Converts '-rw-r--r--' to 0644
   403 	 * From "info at rvgate dot nl"'s comment on the PHP documentation for chmod()
   411 	 * From "info at rvgate dot nl"'s comment on the PHP documentation for chmod()
   404 	 *
   412 	 *
   405 	 * @link https://www.php.net/manual/en/function.chmod.php#49614
   413 	 * @link https://www.php.net/manual/en/function.chmod.php#49614
   661 	 * Checks if a file or directory exists.
   669 	 * Checks if a file or directory exists.
   662 	 *
   670 	 *
   663 	 * @since 2.5.0
   671 	 * @since 2.5.0
   664 	 * @abstract
   672 	 * @abstract
   665 	 *
   673 	 *
   666 	 * @param string $file Path to file or directory.
   674 	 * @param string $path Path to file or directory.
   667 	 * @return bool Whether $file exists or not.
   675 	 * @return bool Whether $path exists or not.
   668 	 */
   676 	 */
   669 	public function exists( $file ) {
   677 	public function exists( $path ) {
   670 		return false;
   678 		return false;
   671 	}
   679 	}
   672 
   680 
   673 	/**
   681 	/**
   674 	 * Checks if resource is a file.
   682 	 * Checks if resource is a file.
   713 	 * Checks if a file or directory is writable.
   721 	 * Checks if a file or directory is writable.
   714 	 *
   722 	 *
   715 	 * @since 2.5.0
   723 	 * @since 2.5.0
   716 	 * @abstract
   724 	 * @abstract
   717 	 *
   725 	 *
   718 	 * @param string $file Path to file or directory.
   726 	 * @param string $path Path to file or directory.
   719 	 * @return bool Whether $file is writable.
   727 	 * @return bool Whether $path is writable.
   720 	 */
   728 	 */
   721 	public function is_writable( $file ) {
   729 	public function is_writable( $path ) {
   722 		return false;
   730 		return false;
   723 	}
   731 	}
   724 
   732 
   725 	/**
   733 	/**
   726 	 * Gets the file's last access time.
   734 	 * Gets the file's last access time.
   824 	 * @param bool   $include_hidden Optional. Whether to include details of hidden ("." prefixed) files.
   832 	 * @param bool   $include_hidden Optional. Whether to include details of hidden ("." prefixed) files.
   825 	 *                               Default true.
   833 	 *                               Default true.
   826 	 * @param bool   $recursive      Optional. Whether to recursively include file details in nested directories.
   834 	 * @param bool   $recursive      Optional. Whether to recursively include file details in nested directories.
   827 	 *                               Default false.
   835 	 *                               Default false.
   828 	 * @return array|false {
   836 	 * @return array|false {
   829 	 *     Array of files. False if unable to list directory contents.
   837 	 *     Array of arrays containing file information. False if unable to list directory contents.
   830 	 *
   838 	 *
   831 	 *     @type string $name        Name of the file or directory.
   839 	 *     @type array ...$0 {
   832 	 *     @type string $perms       *nix representation of permissions.
   840 	 *         Array of file information. Note that some elements may not be available on all filesystems.
   833 	 *     @type string $permsn      Octal representation of permissions.
   841 	 *
   834 	 *     @type string $owner       Owner name or ID.
   842 	 *         @type string           $name        Name of the file or directory.
   835 	 *     @type int    $size        Size of file in bytes.
   843 	 *         @type string           $perms       *nix representation of permissions.
   836 	 *     @type int    $lastmodunix Last modified unix timestamp.
   844 	 *         @type string           $permsn      Octal representation of permissions.
   837 	 *     @type mixed  $lastmod     Last modified month (3 letter) and day (without leading 0).
   845 	 *         @type int|string|false $number      File number. May be a numeric string. False if not available.
   838 	 *     @type int    $time        Last modified time.
   846 	 *         @type string|false     $owner       Owner name or ID, or false if not available.
   839 	 *     @type string $type        Type of resource. 'f' for file, 'd' for directory.
   847 	 *         @type string|false     $group       File permissions group, or false if not available.
   840 	 *     @type mixed  $files       If a directory and `$recursive` is true, contains another array of files.
   848 	 *         @type int|string|false $size        Size of file in bytes. May be a numeric string.
       
   849 	 *                                             False if not available.
       
   850 	 *         @type int|string|false $lastmodunix Last modified unix timestamp. May be a numeric string.
       
   851 	 *                                             False if not available.
       
   852 	 *         @type string|false     $lastmod     Last modified month (3 letters) and day (without leading 0), or
       
   853 	 *                                             false if not available.
       
   854 	 *         @type string|false     $time        Last modified time, or false if not available.
       
   855 	 *         @type string           $type        Type of resource. 'f' for file, 'd' for directory, 'l' for link.
       
   856 	 *         @type array|false      $files       If a directory and `$recursive` is true, contains another array of
       
   857 	 *                                             files. False if unable to list directory contents.
       
   858 	 *     }
   841 	 * }
   859 	 * }
   842 	 */
   860 	 */
   843 	public function dirlist( $path, $include_hidden = true, $recursive = false ) {
   861 	public function dirlist( $path, $include_hidden = true, $recursive = false ) {
   844 		return false;
   862 		return false;
   845 	}
   863 	}
   846 
       
   847 }
   864 }