wp/wp-includes/http.php
changeset 19 3d72ae0968f4
parent 18 be944660c56a
child 21 48c4eec2b7e6
equal deleted inserted replaced
18:be944660c56a 19:3d72ae0968f4
   220  *
   220  *
   221  * @since 2.7.0
   221  * @since 2.7.0
   222  *
   222  *
   223  * @param array|WP_Error $response HTTP response.
   223  * @param array|WP_Error $response HTTP response.
   224  * @param string         $header   Header name to retrieve value from.
   224  * @param string         $header   Header name to retrieve value from.
   225  * @return string The header value. Empty string on if incorrect parameter given, or if the header doesn't exist.
   225  * @return array|string The header(s) value(s). Array if multiple headers with the same name are retrieved.
       
   226  *                      Empty string if incorrect parameter given, or if the header doesn't exist.
   226  */
   227  */
   227 function wp_remote_retrieve_header( $response, $header ) {
   228 function wp_remote_retrieve_header( $response, $header ) {
   228 	if ( is_wp_error( $response ) || ! isset( $response['headers'] ) ) {
   229 	if ( is_wp_error( $response ) || ! isset( $response['headers'] ) ) {
   229 		return '';
   230 		return '';
   230 	}
   231 	}
   237 }
   238 }
   238 
   239 
   239 /**
   240 /**
   240  * Retrieve only the response code from the raw response.
   241  * Retrieve only the response code from the raw response.
   241  *
   242  *
   242  * Will return an empty array if incorrect parameter value is given.
   243  * Will return an empty string if incorrect parameter value is given.
   243  *
   244  *
   244  * @since 2.7.0
   245  * @since 2.7.0
   245  *
   246  *
   246  * @param array|WP_Error $response HTTP response.
   247  * @param array|WP_Error $response HTTP response.
   247  * @return int|string The response code as an integer. Empty string on incorrect parameter given.
   248  * @return int|string The response code as an integer. Empty string on incorrect parameter given.
   255 }
   256 }
   256 
   257 
   257 /**
   258 /**
   258  * Retrieve only the response message from the raw response.
   259  * Retrieve only the response message from the raw response.
   259  *
   260  *
   260  * Will return an empty array if incorrect parameter value is given.
   261  * Will return an empty string if incorrect parameter value is given.
   261  *
   262  *
   262  * @since 2.7.0
   263  * @since 2.7.0
   263  *
   264  *
   264  * @param array|WP_Error $response HTTP response.
   265  * @param array|WP_Error $response HTTP response.
   265  * @return string The response message. Empty string on incorrect parameter given.
   266  * @return string The response message. Empty string on incorrect parameter given.
   512  *
   513  *
   513  * @param string $url Request URL.
   514  * @param string $url Request URL.
   514  * @return string|false URL or false on failure.
   515  * @return string|false URL or false on failure.
   515  */
   516  */
   516 function wp_http_validate_url( $url ) {
   517 function wp_http_validate_url( $url ) {
       
   518 	if ( ! is_string( $url ) || '' === $url || is_numeric( $url ) ) {
       
   519 		return false;
       
   520 	}
       
   521 
   517 	$original_url = $url;
   522 	$original_url = $url;
   518 	$url          = wp_kses_bad_protocol( $url, array( 'http', 'https' ) );
   523 	$url          = wp_kses_bad_protocol( $url, array( 'http', 'https' ) );
   519 	if ( ! $url || strtolower( $url ) !== strtolower( $original_url ) ) {
   524 	if ( ! $url || strtolower( $url ) !== strtolower( $original_url ) ) {
   520 		return false;
   525 		return false;
   521 	}
   526 	}
   532 	if ( false !== strpbrk( $parsed_url['host'], ':#?[]' ) ) {
   537 	if ( false !== strpbrk( $parsed_url['host'], ':#?[]' ) ) {
   533 		return false;
   538 		return false;
   534 	}
   539 	}
   535 
   540 
   536 	$parsed_home = parse_url( get_option( 'home' ) );
   541 	$parsed_home = parse_url( get_option( 'home' ) );
   537 
   542 	$same_host   = isset( $parsed_home['host'] ) && strtolower( $parsed_home['host'] ) === strtolower( $parsed_url['host'] );
   538 	if ( isset( $parsed_home['host'] ) ) {
   543 	$host        = trim( $parsed_url['host'], '.' );
   539 		$same_host = strtolower( $parsed_home['host'] ) === strtolower( $parsed_url['host'] );
       
   540 	} else {
       
   541 		$same_host = false;
       
   542 	}
       
   543 
   544 
   544 	if ( ! $same_host ) {
   545 	if ( ! $same_host ) {
   545 		$host = trim( $parsed_url['host'], '.' );
       
   546 		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 ) ) {
   546 		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 ) ) {
   547 			$ip = $host;
   547 			$ip = $host;
   548 		} else {
   548 		} else {
   549 			$ip = gethostbyname( $host );
   549 			$ip = gethostbyname( $host );
   550 			if ( $ip === $host ) { // Error condition for gethostbyname().
   550 			if ( $ip === $host ) { // Error condition for gethostbyname().
   579 	if ( empty( $parsed_url['port'] ) ) {
   579 	if ( empty( $parsed_url['port'] ) ) {
   580 		return $url;
   580 		return $url;
   581 	}
   581 	}
   582 
   582 
   583 	$port = $parsed_url['port'];
   583 	$port = $parsed_url['port'];
   584 	if ( 80 === $port || 443 === $port || 8080 === $port ) {
   584 
       
   585 	/**
       
   586 	 * Controls the list of ports considered safe in HTTP API.
       
   587 	 *
       
   588 	 * Allows to change and allow external requests for the HTTP request.
       
   589 	 *
       
   590 	 * @since 5.9.0
       
   591 	 *
       
   592 	 * @param array  $allowed_ports Array of integers for valid ports.
       
   593 	 * @param string $host          Host name of the requested URL.
       
   594 	 * @param string $url           Requested URL.
       
   595 	 */
       
   596 	$allowed_ports = apply_filters( 'http_allowed_safe_ports', array( 80, 443, 8080 ), $host, $url );
       
   597 	if ( is_array( $allowed_ports ) && in_array( $port, $allowed_ports, true ) ) {
   585 		return $url;
   598 		return $url;
   586 	}
   599 	}
   587 
   600 
   588 	if ( $parsed_home && $same_host && isset( $parsed_home['port'] ) && $parsed_home['port'] === $port ) {
   601 	if ( $parsed_home && $same_host && isset( $parsed_home['port'] ) && $parsed_home['port'] === $port ) {
   589 		return $url;
   602 		return $url;
   639 	$queried[ $host ] = (bool) $wpdb->get_var( $wpdb->prepare( "SELECT domain FROM $wpdb->blogs WHERE domain = %s LIMIT 1", $host ) );
   652 	$queried[ $host ] = (bool) $wpdb->get_var( $wpdb->prepare( "SELECT domain FROM $wpdb->blogs WHERE domain = %s LIMIT 1", $host ) );
   640 	return $queried[ $host ];
   653 	return $queried[ $host ];
   641 }
   654 }
   642 
   655 
   643 /**
   656 /**
   644  * A wrapper for PHP's parse_url() function that handles consistency in the return
   657  * A wrapper for PHP's parse_url() function that handles consistency in the return values
   645  * values across PHP versions.
   658  * across PHP versions.
   646  *
   659  *
   647  * PHP 5.4.7 expanded parse_url()'s ability to handle non-absolute url's, including
   660  * PHP 5.4.7 expanded parse_url()'s ability to handle non-absolute URLs, including
   648  * schemeless and relative url's with :// in the path. This function works around
   661  * schemeless and relative URLs with "://" in the path. This function works around
   649  * those limitations providing a standard output on PHP 5.2~5.4+.
   662  * those limitations providing a standard output on PHP 5.2~5.4+.
   650  *
   663  *
   651  * Secondly, across various PHP versions, schemeless URLs starting containing a ":"
   664  * Secondly, across various PHP versions, schemeless URLs containing a ":" in the query
   652  * in the query are being handled inconsistently. This function works around those
   665  * are being handled inconsistently. This function works around those differences as well.
   653  * differences as well.
       
   654  *
   666  *
   655  * @since 4.4.0
   667  * @since 4.4.0
   656  * @since 4.7.0 The `$component` parameter was added for parity with PHP's `parse_url()`.
   668  * @since 4.7.0 The `$component` parameter was added for parity with PHP's `parse_url()`.
   657  *
   669  *
   658  * @link https://www.php.net/manual/en/function.parse-url.php
   670  * @link https://www.php.net/manual/en/function.parse-url.php