Make WordPress Core

Ticket #56141: manual_wp_config.diff

File manual_wp_config.diff, 4.2 KB (added by Michi91, 20 months ago)
  • src/wp-admin/setup-config.php

    From 4620757f87664807d2158601ac7c2b460b336699 Mon Sep 17 00:00:00 2001
    From: Michael <github@michaelplas.de>
    Date: Sat, 9 Sep 2023 23:47:21 +0200
    Subject: [PATCH] See https://core.trac.wordpress.org/ticket/56141
    
    ---
     src/wp-admin/setup-config.php | 26 ++++++++++++++++----
     src/wp-includes/functions.php | 46 +++++++++++++++++++++++++++++++++++
     2 files changed, 67 insertions(+), 5 deletions(-)
    
    diff --git a/src/wp-admin/setup-config.php b/src/wp-admin/setup-config.php
    index ddcb4943c7..8a8603e84b 100644
    a b switch ( $step ) { 
    410410                }
    411411                unset( $line );
    412412
    413                 if ( ! is_writable( ABSPATH ) ) :
     413                $dbhost_is_in_private_network = wp_is_dbhost_in_private_network( $dbhost );
     414
     415                if ( ! is_writable( ABSPATH ) || !$dbhost_is_in_private_network  ) :
    414416                        setup_config_display_header();
    415417                        ?>
    416418<p>
    417419                        <?php
    418                         /* translators: %s: wp-config.php */
    419                         printf( __( 'Unable to write to %s file.' ), '<code>wp-config.php</code>' );
     420                        if(!$dbhost_is_in_private_network){
     421                                //* translators: 1: Database Host, 2: wp-config.php. */
     422                                printf( __( 'Your database host %s is not part of a private network. For security reasons WordPress can not save the %s file automatically.' ), $dbhost, '<code>wp-config.php</code>' );
     423                        }
     424
     425                        if(! is_writable( ABSPATH )){
     426                                /* translators: %s: wp-config.php */
     427                                printf( __( 'Unable to write to %s file.' ), '<code>wp-config.php</code>' );
     428                        }
    420429                        ?>
    421430</p>
    422431<p id="wp-config-description">
    423432                        <?php
    424433                        /* translators: %s: wp-config.php */
    425                         printf( __( 'You can create the %s file manually and paste the following text into it.' ), '<code>wp-config.php</code>' );
    426 
     434                        printf( __( 'You can create the %s file manually and paste the following text into it. ' ), '<code>wp-config.php</code>' );
     435
     436                        printf(
     437                                /* translators: 1: Documentation URL, 2: wp-config.php */
     438                                __( 'Need more help? <a href="%1$s">Read the support article on %2$s</a>.' ),
     439                                __( 'https://wordpress.org/documentation/article/editing-wp-config-php/' ),
     440                                '<code>wp-config.php</code>'
     441                        );
     442               
    427443                        $config_text = '';
    428444
    429445                        foreach ( $config_file as $line ) {
  • src/wp-includes/functions.php

    diff --git a/src/wp-includes/functions.php b/src/wp-includes/functions.php
    index 4a2a2b1779..0986273a7e 100644
    a b function is_php_version_compatible( $required ) { 
    87548754function wp_fuzzy_number_match( $expected, $actual, $precision = 1 ) {
    87558755        return abs( (float) $expected - (float) $actual ) <= $precision;
    87568756}
     8757
     8758/**
     8759 * Checks if the given dbhost is part of Private Adress Spaces rfc1918
     8760 *
     8761 * Converts the DB Host IP into long integer and compares with the given private networks.
     8762 * Additional networks can by specifying an environment variable `WP_ALLOWED_DBNETWORK`
     8763 *
     8764 * @since 6.5.0
     8765 *
     8766 * @param string $dbhost IP or Hostname for DB Host
     8767 * @return bool Wheater is in private network or not
     8768 */
     8769function wp_is_dbhost_in_private_network( $dbhost ) {
     8770        $private_networks = array(
     8771                "127.0.0.0/8",
     8772                "10.0.0.0/8",
     8773                "172.16.0.0/12",
     8774                "192.168.0.0/16"
     8775        );
     8776
     8777        // Check if `getenv` is available on the system, if the environment variable has been set and looks like a IP with subnetmask
     8778        if ( function_exists( 'getenv' ) && getenv( 'WP_ALLOWED_DBNETWORK' ) ) {
     8779                $pattern = '/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\/(\d{1,2})$/';
     8780                $allowed_network = getenv( 'WP_ALLOWED_DBNETWORK' );
     8781
     8782                if (preg_match($pattern, $allowed_network)) {
     8783                        $private_networks[] = getenv( 'WP_ALLOWED_DBNETWORK' );
     8784                }
     8785        }
     8786
     8787        // Check if DB Host is a IP or if not get IP by hostname
     8788        $dbhostip = filter_var($dbhost, FILTER_VALIDATE_IP) ? $dbhost : gethostbyname($dbhost);
     8789
     8790        $ip_long = ip2long($dbhostip);
     8791        foreach ($private_networks as $network) {
     8792                list($network_ip, $subnet_mask) = explode('/', $network);
     8793                $network_long = ip2long($network_ip);
     8794                $subnet_mask = pow(2, (32 - $subnet_mask)) - 1;
     8795                $subnet_long = $network_long & $subnet_mask;
     8796                if (($ip_long & $subnet_mask) === $subnet_long) {
     8797            return true; // IP address is in a private network
     8798        }
     8799    }
     8800
     8801    return false; // IP address is not in any of the private networks
     8802}
     8803 No newline at end of file