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 ); |
| 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 | /** |