From 6ce6abd8c07f095f5b1bc7cb569e4c4cd22d25e2 Mon Sep 17 00:00:00 2001 From: Peter Wilson Date: Wed, 21 Apr 2021 01:33:03 +0000 Subject: [PATCH] Upgrade/Install: Prevent possible type errors during installation. Prevent a `TypeError` from occurring during installation if `wp-config.php` is not writable. In PHP 8.0 this can cause a fatal error, in earlier versions of PHP a warning would be thrown. Account for a change in type returned by `fopen()` coming in a future version of PHP. Minor coding standards fixes in the `/wp-admin/setup-config.php` file. Props xknown, hellofromTonya, jrf, peterwilsoncc. See #51423. Built from https://develop.svn.wordpress.org/trunk@50775 git-svn-id: http://core.svn.wordpress.org/trunk@50384 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/setup-config.php | 72 +++++++++++++++++++++++++++------------ wp-includes/version.php | 2 +- 2 files changed, 52 insertions(+), 22 deletions(-) diff --git a/wp-admin/setup-config.php b/wp-admin/setup-config.php index f89968ca28..7e06726443 100644 --- a/wp-admin/setup-config.php +++ b/wp-admin/setup-config.php @@ -426,33 +426,63 @@ if ( ! /iPad|iPod|iPhone/.test( navigator.userAgent ) ) { })(); + $error_message = ''; + $handle = fopen( $path_to_wp_config, 'w' ); + /* + * Why check for the absence of false instead of checking for resource with is_resource()? + * To future-proof the check for when fopen returns object instead of resource, i.e. a known + * change coming in PHP. + */ + if ( false !== $handle ) { + foreach ( $config_file as $line ) { + fwrite( $handle, $line ); + } + fclose( $handle ); + } else { + $wp_config_perms = fileperms( $path_to_wp_config ); + if ( ! empty( $wp_config_perms ) && ! is_writable( $path_to_wp_config ) ) { + $error_message = sprintf( + /* translators: 1 wp-config.php, 2: Documentation URL. */ + __( 'You need to make the file %1$s writable before you can save your changes. See Changing File Permissions for more information.' ), + 'wp-config.php', + __( 'https://wordpress.org/support/article/changing-file-permissions/' ) + ); + } else { + $error_message = sprintf( + /* translators: %s: wp-config.php */ + __( 'Unable to write to %s file.' ), + 'wp-config.php' + ); + } + } + + chmod( $path_to_wp_config, 0666 ); + setup_config_display_header(); + + if ( false !== $handle ) : + ?>

- %s

', $error_message ); + endif; + endif; break; -} +} // End of the steps switch. ?> diff --git a/wp-includes/version.php b/wp-includes/version.php index 7a10370664..155b3d074b 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -13,7 +13,7 @@ * * @global string $wp_version */ -$wp_version = '5.8-alpha-50774'; +$wp_version = '5.8-alpha-50775'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.