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
This commit is contained in:
Peter Wilson 2021-04-21 01:33:03 +00:00
parent 0a27442af5
commit 6ce6abd8c0
2 changed files with 52 additions and 22 deletions

View File

@ -426,33 +426,63 @@ if ( ! /iPad|iPod|iPhone/.test( navigator.userAgent ) ) {
})(); })();
</script> </script>
<?php <?php
else : else :
/* /*
* If this file doesn't exist, then we are using the wp-config-sample.php * If this file doesn't exist, then we are using the wp-config-sample.php
* file one level up, which is for the develop repo. * file one level up, which is for the develop repo.
*/ */
if ( file_exists( ABSPATH . 'wp-config-sample.php' ) ) { if ( file_exists( ABSPATH . 'wp-config-sample.php' ) ) {
$path_to_wp_config = ABSPATH . 'wp-config.php'; $path_to_wp_config = ABSPATH . 'wp-config.php';
} else { } else {
$path_to_wp_config = dirname( ABSPATH ) . '/wp-config.php'; $path_to_wp_config = dirname( ABSPATH ) . '/wp-config.php';
} }
$handle = fopen( $path_to_wp_config, 'w' ); $error_message = '';
foreach ( $config_file as $line ) { $handle = fopen( $path_to_wp_config, 'w' );
fwrite( $handle, $line ); /*
} * Why check for the absence of false instead of checking for resource with is_resource()?
fclose( $handle ); * To future-proof the check for when fopen returns object instead of resource, i.e. a known
chmod( $path_to_wp_config, 0666 ); * change coming in PHP.
setup_config_display_header(); */
?> 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 <a href="%2$s">Changing File Permissions</a> for more information.' ),
'<code>wp-config.php</code>',
__( 'https://wordpress.org/support/article/changing-file-permissions/' )
);
} else {
$error_message = sprintf(
/* translators: %s: wp-config.php */
__( 'Unable to write to %s file.' ),
'<code>wp-config.php</code>'
);
}
}
chmod( $path_to_wp_config, 0666 );
setup_config_display_header();
if ( false !== $handle ) :
?>
<h1 class="screen-reader-text"><?php _e( 'Successful database connection' ); ?></h1> <h1 class="screen-reader-text"><?php _e( 'Successful database connection' ); ?></h1>
<p><?php _e( 'All right, sparky! You&#8217;ve made it through this part of the installation. WordPress can now communicate with your database. If you are ready, time now to&hellip;' ); ?></p> <p><?php _e( 'All right, sparky! You&#8217;ve made it through this part of the installation. WordPress can now communicate with your database. If you are ready, time now to&hellip;' ); ?></p>
<p class="step"><a href="<?php echo $install; ?>" class="button button-large"><?php _e( 'Run the installation' ); ?></a></p> <p class="step"><a href="<?php echo $install; ?>" class="button button-large"><?php _e( 'Run the installation' ); ?></a></p>
<?php <?php
endif; else :
printf( '<p>%s</p>', $error_message );
endif;
endif;
break; break;
} } // End of the steps switch.
?> ?>
<?php wp_print_scripts( 'language-chooser' ); ?> <?php wp_print_scripts( 'language-chooser' ); ?>
</body> </body>

View File

@ -13,7 +13,7 @@
* *
* @global string $wp_version * @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. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.