ArrayUtil.php
7 months ago
BlocksUtil.php
5 months ago
COTMigrationUtil.php
1 year ago
DatabaseUtil.php
1 year ago
FilesystemUtil.php
3 months ago
HtmlSanitizer.php
2 years ago
LegacyRestApiStub.php
4 weeks ago
PluginInstaller.php
1 year ago
ProductUtil.php
9 months ago
Types.php
1 year ago
URL.php
1 year ago
URLException.php
4 years ago
Users.php
4 months ago
WebhookUtil.php
4 weeks ago
FilesystemUtil.php
229 lines
| 1 | <?php |
| 2 | declare( strict_types = 1 ); |
| 3 | |
| 4 | namespace Automattic\WooCommerce\Internal\Utilities; |
| 5 | |
| 6 | use Automattic\Jetpack\Constants; |
| 7 | use Automattic\WooCommerce\Proxies\LegacyProxy; |
| 8 | use Exception; |
| 9 | use WP_Filesystem_Base; |
| 10 | |
| 11 | /** |
| 12 | * FilesystemUtil class. |
| 13 | */ |
| 14 | class FilesystemUtil { |
| 15 | |
| 16 | /** |
| 17 | * Transient key for tracking FTP filesystem initialization failures. |
| 18 | */ |
| 19 | private const FTP_INIT_FAILURE_TRANSIENT = 'wc_ftp_filesystem_init_failed'; |
| 20 | |
| 21 | /** |
| 22 | * Cooldown period in minutes before retrying a failed FTP connection. |
| 23 | */ |
| 24 | private const FTP_INIT_COOLDOWN_MINUTES = 2; |
| 25 | |
| 26 | /** |
| 27 | * Wrapper to retrieve the class instance contained in the $wp_filesystem global, after initializing if necessary. |
| 28 | * |
| 29 | * @return WP_Filesystem_Base |
| 30 | * @throws Exception Thrown when the filesystem fails to initialize. |
| 31 | */ |
| 32 | public static function get_wp_filesystem(): WP_Filesystem_Base { |
| 33 | global $wp_filesystem; |
| 34 | |
| 35 | $initialized = ( $wp_filesystem instanceof WP_Filesystem_Base ) || self::initialize_wp_filesystem(); |
| 36 | |
| 37 | if ( ! $initialized || ! self::is_usable_ftp_filesystem( $wp_filesystem ) ) { |
| 38 | throw new Exception( 'The WordPress filesystem could not be initialized.' ); |
| 39 | } |
| 40 | |
| 41 | return $wp_filesystem; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Get the WP filesystem method, with a fallback to 'direct' if no FS_METHOD constant exists and there are not FTP related options/credentials set. |
| 46 | * |
| 47 | * @return string|false The name of the WP filesystem method to use. |
| 48 | */ |
| 49 | public static function get_wp_filesystem_method_or_direct() { |
| 50 | $proxy = wc_get_container()->get( LegacyProxy::class ); |
| 51 | if ( ! self::constant_exists( 'FS_METHOD' ) && false === $proxy->call_function( 'get_option', 'ftp_credentials' ) && ! self::constant_exists( 'FTP_HOST' ) ) { |
| 52 | return 'direct'; |
| 53 | } |
| 54 | |
| 55 | $method = $proxy->call_function( 'get_filesystem_method' ); |
| 56 | if ( $method ) { |
| 57 | return $method; |
| 58 | } |
| 59 | |
| 60 | return 'direct'; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Check if a constant exists and is not null. |
| 65 | * |
| 66 | * @param string $name Constant name. |
| 67 | * @return bool True if the constant exists and its value is not null. |
| 68 | */ |
| 69 | private static function constant_exists( string $name ): bool { |
| 70 | return Constants::is_defined( $name ) && ! is_null( Constants::get_constant( $name ) ); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Recursively creates a directory (if it doesn't exist) and adds an empty index.html and a .htaccess to prevent |
| 75 | * directory listing. |
| 76 | * |
| 77 | * @since 9.3.0 |
| 78 | * |
| 79 | * @param string $path Directory to create. |
| 80 | * @param bool $allow_file_access Whether to allow file access while preventing directory listing. Default false (deny all access). |
| 81 | * @throws \Exception In case of error. |
| 82 | */ |
| 83 | public static function mkdir_p_not_indexable( string $path, bool $allow_file_access = false ): void { |
| 84 | $wp_fs = self::get_wp_filesystem(); |
| 85 | |
| 86 | if ( $wp_fs->is_dir( $path ) ) { |
| 87 | return; |
| 88 | } |
| 89 | |
| 90 | if ( ! wp_mkdir_p( $path ) ) { |
| 91 | throw new \Exception( esc_html( sprintf( 'Could not create directory: %s.', wp_basename( $path ) ) ) ); |
| 92 | } |
| 93 | |
| 94 | $htaccess_content = $allow_file_access ? 'Options -Indexes' : 'deny from all'; |
| 95 | |
| 96 | $files = array( |
| 97 | '.htaccess' => $htaccess_content, |
| 98 | 'index.html' => '', |
| 99 | ); |
| 100 | |
| 101 | foreach ( $files as $name => $content ) { |
| 102 | $wp_fs->put_contents( trailingslashit( $path ) . $name, $content ); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Wrapper to initialize the WP filesystem with defined credentials if they are available. |
| 108 | * |
| 109 | * @return bool True if the $wp_filesystem global was successfully initialized. |
| 110 | */ |
| 111 | protected static function initialize_wp_filesystem(): bool { |
| 112 | global $wp_filesystem; |
| 113 | |
| 114 | if ( $wp_filesystem instanceof WP_Filesystem_Base ) { |
| 115 | return true; |
| 116 | } |
| 117 | |
| 118 | require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 119 | |
| 120 | $method = self::get_wp_filesystem_method_or_direct(); |
| 121 | $initialized = false; |
| 122 | |
| 123 | if ( 'direct' === $method ) { |
| 124 | $initialized = WP_Filesystem(); |
| 125 | } elseif ( false !== $method ) { |
| 126 | $is_ftp = in_array( $method, array( 'ftpext', 'ftpsockets' ), true ); |
| 127 | |
| 128 | if ( $is_ftp && get_transient( self::FTP_INIT_FAILURE_TRANSIENT ) ) { |
| 129 | return false; |
| 130 | } |
| 131 | |
| 132 | // See https://core.trac.wordpress.org/changeset/56341. |
| 133 | ob_start(); |
| 134 | $credentials = request_filesystem_credentials( '' ); |
| 135 | ob_end_clean(); |
| 136 | |
| 137 | $initialized = $credentials && WP_Filesystem( $credentials ); |
| 138 | |
| 139 | if ( $is_ftp ) { |
| 140 | if ( ! $initialized ) { |
| 141 | // A fixed cooldown is used instead of exponential backoff since this handles a non-critical |
| 142 | // edge case (broken FTP filesystem during logging) that most sites will never encounter. |
| 143 | set_transient( self::FTP_INIT_FAILURE_TRANSIENT, true, self::FTP_INIT_COOLDOWN_MINUTES * MINUTE_IN_SECONDS ); |
| 144 | error_log( sprintf( 'WooCommerce: FTP filesystem connection failed. Please check your FTP credentials. Retrying in %d minutes.', self::FTP_INIT_COOLDOWN_MINUTES ) ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log |
| 145 | } else { |
| 146 | delete_transient( self::FTP_INIT_FAILURE_TRANSIENT ); |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | return is_null( $initialized ) ? false : $initialized; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Check if an FTP-based filesystem instance is usable. |
| 156 | * |
| 157 | * Checks both the connection resource and the error state. The connection |
| 158 | * resource can be null if PHP's max execution time interrupted ftp_connect() |
| 159 | * before it completed, leaving the instance in a broken state without errors. |
| 160 | * |
| 161 | * @param WP_Filesystem_Base $wp_filesystem The filesystem instance to check. |
| 162 | * @return bool False if FTP-based and unusable, true otherwise. |
| 163 | */ |
| 164 | private static function is_usable_ftp_filesystem( WP_Filesystem_Base $wp_filesystem ): bool { |
| 165 | $has_broken_state = false; |
| 166 | $has_errors = false; |
| 167 | |
| 168 | if ( 'ftpext' === $wp_filesystem->method ) { |
| 169 | $has_broken_state = empty( $wp_filesystem->link ); |
| 170 | $has_errors = is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->has_errors(); |
| 171 | } |
| 172 | |
| 173 | if ( 'ftpsockets' === $wp_filesystem->method ) { |
| 174 | $has_broken_state = empty( $wp_filesystem->ftp ); |
| 175 | $has_errors = is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->has_errors(); |
| 176 | } |
| 177 | |
| 178 | return ! $has_broken_state && ! $has_errors; |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Validate that a file path is a valid upload path. |
| 183 | * |
| 184 | * @param string $path The path to validate. |
| 185 | * @throws \Exception If the file path is not a valid upload path. |
| 186 | */ |
| 187 | public static function validate_upload_file_path( string $path ): void { |
| 188 | $wp_filesystem = self::get_wp_filesystem(); |
| 189 | |
| 190 | // File must exist and be readable. |
| 191 | $is_valid_file = $wp_filesystem->is_readable( $path ); |
| 192 | |
| 193 | // Check that file is within an allowed location. |
| 194 | if ( $is_valid_file ) { |
| 195 | $is_valid_file = self::file_is_in_directory( $path, $wp_filesystem->abspath() ); |
| 196 | if ( ! $is_valid_file ) { |
| 197 | $upload_dir = wp_get_upload_dir(); |
| 198 | $is_valid_file = false === $upload_dir['error'] && self::file_is_in_directory( $path, $upload_dir['basedir'] ); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | if ( ! $is_valid_file ) { |
| 203 | throw new \Exception( esc_html__( 'File path is not a valid upload path.', 'woocommerce' ) ); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Check if a given file is inside a given directory. |
| 209 | * |
| 210 | * @param string $file_path The full path of the file to check. |
| 211 | * @param string $directory The path of the directory to check. |
| 212 | * @return bool True if the file is inside the directory. |
| 213 | */ |
| 214 | private static function file_is_in_directory( string $file_path, string $directory ): bool { |
| 215 | // Extract protocol if it exists. |
| 216 | $protocol = ''; |
| 217 | if ( preg_match( '#^([a-z0-9]+://)#i', $file_path, $matches ) ) { |
| 218 | $protocol = $matches[1]; |
| 219 | $file_path = preg_replace( '#^[a-z0-9]+://#i', '', $file_path ); |
| 220 | } |
| 221 | |
| 222 | $file_path = (string) new URL( $file_path ); // This resolves '/../' sequences. |
| 223 | $file_path = preg_replace( '/^file:\\/\\//', $protocol, $file_path ); |
| 224 | $file_path = preg_replace( '/^file:\\/\\//', '', $file_path ); |
| 225 | |
| 226 | return 0 === stripos( wp_normalize_path( $file_path ), trailingslashit( wp_normalize_path( $directory ) ) ); |
| 227 | } |
| 228 | } |
| 229 |