Make WordPress Core

Ticket #41722: 41722.diff

File 41722.diff, 3.0 KB (added by schlessera, 6 years ago)

Fix for parsing both IPv4 & IPv6 hosts

  • src/wp-includes/wp-db.php

    diff --git src/wp-includes/wp-db.php src/wp-includes/wp-db.php
    index 57ed865a35..99668af09b 100644
    class wpdb { 
    14501450                if ( $this->use_mysqli ) {
    14511451                        $this->dbh = mysqli_init();
    14521452
    1453                         // mysqli_real_connect doesn't support the host param including a port or socket
    1454                         // like mysql_connect does. This duplicates how mysql_connect detects a port and/or socket file.
    1455                         $port = null;
    1456                         $socket = null;
    1457                         $host = $this->dbhost;
    1458                         $port_or_socket = strstr( $host, ':' );
    1459                         if ( ! empty( $port_or_socket ) ) {
    1460                                 $host = substr( $host, 0, strpos( $host, ':' ) );
    1461                                 $port_or_socket = substr( $port_or_socket, 1 );
    1462                                 if ( 0 !== strpos( $port_or_socket, '/' ) ) {
    1463                                         $port = intval( $port_or_socket );
    1464                                         $maybe_socket = strstr( $port_or_socket, ':' );
    1465                                         if ( ! empty( $maybe_socket ) ) {
    1466                                                 $socket = substr( $maybe_socket, 1 );
    1467                                         }
    1468                                 } else {
    1469                                         $socket = $port_or_socket;
    1470                                 }
    1471                         }
     1453                        list( $host, $port, $socket ) = $this->parse_db_host( $this->dbhost );
    14721454
    14731455                        if ( WP_DEBUG ) {
    14741456                                mysqli_real_connect( $this->dbh, $host, $this->dbuser, $this->dbpassword, null, $port, $socket, $client_flags );
    class wpdb { 
    15601542        }
    15611543
    15621544        /**
     1545         * Parse the DB_HOST setting to interpret it for mysqli_real_connect.
     1546         *
     1547         * mysqli_real_connect doesn't support the host param including a port or
     1548         * socket like mysql_connect does. This duplicates how mysql_connect detects
     1549         * a port and/or socket file.
     1550         *
     1551         * @since 4.9.0
     1552         *
     1553         * @param string $host The DB_HOST setting to parse.
     1554         *
     1555         * @return array Array containing the host, the port and the socket, in that
     1556         *               order.
     1557         */
     1558        public function parse_db_host( $host ) {
     1559                $port    = null;
     1560                $socket  = null;
     1561                $is_ipv6 = false;
     1562
     1563                // We need to check for an IPv6 address first.
     1564                // An IPv6 address will always contain at least two colons.
     1565                if ( substr_count( $host, ':' ) > 1 ) {
     1566                        $pattern = '/^(?:\[)?(?<host>[0-9a-fA-F:]+)(?:\]:(?<port>[\d]+))?(?:\/(?<socket>.+))?/';
     1567                        $is_ipv6 = true;
     1568                } else {
     1569                        // We seem to be dealing with an IPv4 address.
     1570                        $pattern = '/^(?:\[)?(?<host>[^:^\/]+)(?::(?<port>[\d]+))?(?:\/(?<socket>.+))?/';
     1571                }
     1572
     1573                $matches = array();
     1574                $result = preg_match( $pattern, $host, $matches );
     1575
     1576                if ( 1 !== $result ) {
     1577                        // Couldn't parse the address, just return as is to try it out.
     1578                        return array( $host, $port, $socket );
     1579                }
     1580
     1581                foreach ( array( 'host', 'port', 'socket' ) as $component ) {
     1582                        if ( array_key_exists( $component, $matches ) ) {
     1583                                $$component = $matches[$component];
     1584                        }
     1585                }
     1586
     1587                // Apparently, mysqli_real_connect() needs IPv6 hosts enclosed in
     1588                // brackets.
     1589                if ( $is_ipv6 ) {
     1590                        $host = "[$host]";
     1591                }
     1592
     1593                return array( $host, $port, $socket );
     1594        }
     1595
     1596        /**
    15631597         * Checks that the connection to the database is still up. If not, try to reconnect.
    15641598         *
    15651599         * If this function is unable to reconnect, it will forcibly die, or if after the