diff --git a/src/wp-admin/includes/class-ftp.php b/src/wp-admin/includes/class-ftp.php index 46236c649d640..ba4245657672e 100644 --- a/src/wp-admin/includes/class-ftp.php +++ b/src/wp-admin/includes/class-ftp.php @@ -284,7 +284,7 @@ function SetServer($host, $port=21, $reconnect=true) { $this->SendMSG("Incorrect port syntax"); return FALSE; } else { - $ip=@gethostbyname($host); + $ip=@get_host_by_name($host); $dns=@gethostbyaddr($host); if(!$ip) $ip=$host; if(!$dns) $dns=$host; diff --git a/src/wp-admin/includes/class-wp-debug-data.php b/src/wp-admin/includes/class-wp-debug-data.php index cd04297684bcd..451bbaaddf54f 100644 --- a/src/wp-admin/includes/class-wp-debug-data.php +++ b/src/wp-admin/includes/class-wp-debug-data.php @@ -287,7 +287,7 @@ private static function get_wp_core(): array { 'value' => sprintf( /* translators: 1: The IP address WordPress.org resolves to. 2: The error returned by the lookup. */ __( 'Unable to reach WordPress.org at %1$s: %2$s' ), - gethostbyname( 'wordpress.org' ), + get_host_by_name( 'wordpress.org' ), $wp_dotorg->get_error_message() ), 'debug' => $wp_dotorg->get_error_message(), diff --git a/src/wp-admin/includes/class-wp-site-health.php b/src/wp-admin/includes/class-wp-site-health.php index d97d4f996a0cb..0a13e9a96e94c 100644 --- a/src/wp-admin/includes/class-wp-site-health.php +++ b/src/wp-admin/includes/class-wp-site-health.php @@ -1328,7 +1328,7 @@ public function get_test_dotorg_communication() { sprintf( /* translators: 1: The IP address WordPress.org resolves to. 2: The error returned by the lookup. */ __( 'Your site is unable to reach WordPress.org at %1$s, and returned the error: %2$s' ), - gethostbyname( 'api.wordpress.org' ), + get_host_by_name( 'api.wordpress.org' ), $wp_dotorg->get_error_message() ) ) diff --git a/src/wp-includes/http.php b/src/wp-includes/http.php index b343bb69f572b..3efc7727c94ce 100644 --- a/src/wp-includes/http.php +++ b/src/wp-includes/http.php @@ -536,6 +536,33 @@ function send_origin_headers() { return false; } +function get_host_list_by_name( $host ) { + # Get DNS_A (IPv4) and DNS_AAAA (IPv6) records + $records = dns_get_record( $host, DNS_AAAA | DNS_A ); + + $ips = array(); + foreach ( $records as $record ) { + if ( $record["type"] == "A" ) { + $ips[] = $record["ip"]; + } + if ( $record["type"] == "AAAA" ) { + $ips[] = $record["ipv6"]; + } + } + + return $ips; +} + +function get_host_by_name( $host ) { + $ips = get_host_list_by_name( $host ); + + if ( $ips == false ) { + return $host; + } + + return $ips[0]; +} + /** * Validates a URL as safe for use in the HTTP API. * @@ -592,8 +619,8 @@ function wp_http_validate_url( $url ) { if ( preg_match( '#^(([1-9]?\d|1\d\d|25[0-5]|2[0-4]\d)\.){3}([1-9]?\d|1\d\d|25[0-5]|2[0-4]\d)$#', $host ) ) { $ip = $host; } else { - $ip = gethostbyname( $host ); - if ( $ip === $host ) { // Error condition for gethostbyname(). + $ip = get_host_by_name( $host ); + if ( $ip === $host ) { // Error condition for get_host_by_name(). return false; } } diff --git a/tests/phpunit/tests/http/base.php b/tests/phpunit/tests/http/base.php index 49e4672ad2941..c8369839cf262 100644 --- a/tests/phpunit/tests/http/base.php +++ b/tests/phpunit/tests/http/base.php @@ -377,7 +377,7 @@ public function data_post_redirect_to_method_300() { * @covers ::wp_remote_retrieve_body */ public function test_ip_url_with_host_header() { - $ip = gethostbyname( 'api.wordpress.org' ); + $ip = get_host_by_name( 'api.wordpress.org' ); $url = 'http://' . $ip . '/core/tests/1.0/redirection.php?print-pass=1'; $args = array( 'headers' => array(