Multisite: Cache absolute `dirsize` paths to avoid PHP 8 fatal.

r49212 greatly improved the performance of `get_dirsize()`, but also changed the structure of the data stored in the `dirsize_cache` transient. It stored relative paths instead of absolute ones, and also removed the unnecessary `size` array.

That difference in data structures led to a fatal error in the following environment:

* PHP 8
* Multisite
* A custom `WP_CONTENT_DIR` which is not a child of WP's `ABSPATH` folder (e.g., [https://roots.io/bedrock/ Bedrock])
* The `upload_space_check_disabled` option set to `0`

After upgrading to WP 5.6, the `dirsize_cache` transient still had data in the old format. When `wp-admin.php/index.php` was visited, `get_space_used()` received an `array` instead of an `int`, and tried to divide it by another `int`. PHP 7 would silently cast the arguments to match data types, but [https://wiki.php.net/rfc/arithmetic_operator_type_checks PHP 8 throws a fatal error]: 

`Uncaught TypeError: Unsupported operand types: array / int`

`recurse_dirsize()` was using `ABSPATH` to convert the absolute paths to relative ones, but some upload locations are not located under `ABSPATH`. In those cases, `$directory` and `$cache_path` were identical, and that triggered the early return of the old `array`, instead of the expected `int`. 

In order to avoid that, this commit restores the absolute paths, but without the `size` array. It also adds a type check when returning cached values. Using absolute paths without `size` has the result of overwriting the old data, so that it matches the new format. The type check and upgrade routine are additional safety measures.

Props peterwilsoncc, janthiel, helen, hellofromtonya, francina, pbiron.
Fixes #51913. See #19879.


Built from https://develop.svn.wordpress.org/trunk@49744


git-svn-id: http://core.svn.wordpress.org/trunk@49467 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
iandunn 2020-12-03 20:39:02 +00:00
parent 203b48f281
commit 4de6175227
3 changed files with 15 additions and 13 deletions

View File

@ -874,7 +874,7 @@ function upgrade_all() {
upgrade_550();
}
if ( $wp_current_db_version < 49632 ) {
if ( $wp_current_db_version < 49735 ) {
upgrade_560();
}
@ -2274,6 +2274,10 @@ function upgrade_560() {
*/
save_mod_rewrite_rules();
}
if ( $wp_current_db_version < 49735 ) {
delete_transient( 'dirsize_cache' );
}
}
/**

View File

@ -7626,8 +7626,6 @@ function get_dirsize( $directory, $max_execution_time = null ) {
*/
function recurse_dirsize( $directory, $exclude = null, $max_execution_time = null, &$directory_cache = null ) {
$directory = untrailingslashit( $directory );
$cache_path = untrailingslashit( str_replace( ABSPATH, '', $directory ) );
$save_cache = false;
if ( ! isset( $directory_cache ) ) {
@ -7635,8 +7633,8 @@ function recurse_dirsize( $directory, $exclude = null, $max_execution_time = nul
$save_cache = true;
}
if ( isset( $directory_cache[ $cache_path ] ) ) {
return $directory_cache[ $cache_path ];
if ( isset( $directory_cache[ $directory ] ) && is_int( $directory_cache[ $directory ] ) ) {
return $directory_cache[ $directory ];
}
if ( ! file_exists( $directory ) || ! is_dir( $directory ) || ! is_readable( $directory ) ) {
@ -7705,7 +7703,7 @@ function recurse_dirsize( $directory, $exclude = null, $max_execution_time = nul
}
}
$directory_cache[ $cache_path ] = $size;
$directory_cache[ $directory ] = $size;
// Only write the transient on the top level call and not on recursive calls.
if ( $save_cache ) {
@ -7731,12 +7729,12 @@ function clean_dirsize_cache( $path ) {
return;
}
$cache_path = untrailingslashit( str_replace( ABSPATH, '', $path ) );
unset( $directory_cache[ $cache_path ] );
$path = untrailingslashit( $path );
unset( $directory_cache[ $path ] );
while ( DIRECTORY_SEPARATOR !== $cache_path && '.' !== $cache_path && '..' !== $cache_path ) {
$cache_path = dirname( $cache_path );
unset( $directory_cache[ $cache_path ] );
while ( DIRECTORY_SEPARATOR !== $path && '.' !== $path && '..' !== $path ) {
$path = dirname( $path );
unset( $directory_cache[ $path ] );
}
set_transient( 'dirsize_cache', $directory_cache );

View File

@ -13,14 +13,14 @@
*
* @global string $wp_version
*/
$wp_version = '5.7-alpha-49741';
$wp_version = '5.7-alpha-49744';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
*
* @global int $wp_db_version
*/
$wp_db_version = 49632;
$wp_db_version = 49735;
/**
* Holds the TinyMCE version.