WordPress.org

Make WordPress Core

Changeset 50775


Ignore:
Timestamp:
04/21/2021 01:31:45 AM (5 months ago)
Author:
peterwilsoncc
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/setup-config.php

    r49693 r50775  
    427427</script>
    428428            <?php
    429     else :
    430         /*
    431          * If this file doesn't exist, then we are using the wp-config-sample.php
    432          * file one level up, which is for the develop repo.
    433          */
    434         if ( file_exists( ABSPATH . 'wp-config-sample.php' ) ) {
    435             $path_to_wp_config = ABSPATH . 'wp-config.php';
    436         } else {
    437             $path_to_wp_config = dirname( ABSPATH ) . '/wp-config.php';
    438         }
    439 
    440         $handle = fopen( $path_to_wp_config, 'w' );
    441         foreach ( $config_file as $line ) {
    442             fwrite( $handle, $line );
    443         }
    444         fclose( $handle );
    445         chmod( $path_to_wp_config, 0666 );
    446         setup_config_display_header();
    447         ?>
     429        else :
     430            /*
     431             * If this file doesn't exist, then we are using the wp-config-sample.php
     432             * file one level up, which is for the develop repo.
     433             */
     434            if ( file_exists( ABSPATH . 'wp-config-sample.php' ) ) {
     435                $path_to_wp_config = ABSPATH . 'wp-config.php';
     436            } else {
     437                $path_to_wp_config = dirname( ABSPATH ) . '/wp-config.php';
     438            }
     439
     440            $error_message = '';
     441            $handle        = fopen( $path_to_wp_config, 'w' );
     442            /*
     443             * Why check for the absence of false instead of checking for resource with is_resource()?
     444             * To future-proof the check for when fopen returns object instead of resource, i.e. a known
     445             * change coming in PHP.
     446             */
     447            if ( false !== $handle ) {
     448                foreach ( $config_file as $line ) {
     449                    fwrite( $handle, $line );
     450                }
     451                fclose( $handle );
     452            } else {
     453                $wp_config_perms = fileperms( $path_to_wp_config );
     454                if ( ! empty( $wp_config_perms ) && ! is_writable( $path_to_wp_config ) ) {
     455                    $error_message   = sprintf(
     456                        /* translators: 1 wp-config.php, 2: Documentation URL. */
     457                        __( '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.' ),
     458                        '<code>wp-config.php</code>',
     459                        __( 'https://wordpress.org/support/article/changing-file-permissions/' )
     460                    );
     461                } else {
     462                    $error_message = sprintf(
     463                        /* translators: %s: wp-config.php */
     464                        __( 'Unable to write to %s file.' ),
     465                        '<code>wp-config.php</code>'
     466                    );
     467                }
     468            }
     469
     470            chmod( $path_to_wp_config, 0666 );
     471            setup_config_display_header();
     472
     473            if ( false !== $handle ) :
     474                ?>
    448475<h1 class="screen-reader-text"><?php _e( 'Successful database connection' ); ?></h1>
    449476<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>
    450477
    451478<p class="step"><a href="<?php echo $install; ?>" class="button button-large"><?php _e( 'Run the installation' ); ?></a></p>
    452         <?php
    453     endif;
     479                <?php
     480            else :
     481                printf( '<p>%s</p>', $error_message );
     482            endif;
     483        endif;
    454484        break;
    455 }
     485} // End of the steps switch.
    456486?>
    457487<?php wp_print_scripts( 'language-chooser' ); ?>
Note: See TracChangeset for help on using the changeset viewer.