Make WordPress Core

Changeset 29861


Ignore:
Timestamp:
10/09/2014 01:10:25 AM (12 years ago)
Author:
dd32
Message:

Handle deficiencies in PHP's parse_url in older versions of PHP (<5.4.7) in WP_HTTP::make_absolute_url().

In older versions of PHP:

  • parse_url() will fail to parse a url where the scheme break (:) is present in a relative URL's path
  • parse_url() will include the hostname of a schemeless URL in the path component

This handles those two types of URL's by correcting the response from parse_url().

Fixes #28001, #29886

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-http.php

    r29853 r29861  
    672672        }
    673673
     674        /**
     675         * A wrapper for PHP's parse_url() function that handles edgecases in < PHP 5.4.7
     676         *
     677         * PHP 5.4.7 expanded parse_url()'s ability to handle non-absolute url's, including
     678         * schemeless and relative url's with :// in the path, this works around those limitations
     679         * providing a standard output on PHP 5.2~5.4+.
     680         *
     681         * Error suppression is used as prior to PHP 5.3.3, an E_WARNING would be generated when URL parsing failed.
     682         *
     683         * @since 4.1.0
     684         *
     685         * @access private
     686         * @param  string $url The URL to parse
     687         * @return bool|array False on failure; Array of URL components on success; See parse_url()'s return values.
     688         */
     689        private static function parse_url( $url ) {
     690                $parts = @parse_url( $url );
     691                if ( ! $parts ) {
     692                        // < PHP 5.4.7 compat, trouble with relative paths including a scheme break in the path
     693                        if ( '/' == $url[0] && false !== strpos( $url, '://' ) ) {
     694                                // Since we know it's a relative path, prefix with a scheme/host placeholder and try again
     695                                if ( ! $parts = @parse_url( 'placeholder://placeholder' . $url ) ) {
     696                                        return $parts;
     697                                }
     698                                // Remove the placeholder values
     699                                unset( $parts['scheme'], $parts['host'] );
     700                        } else {
     701                                return $parts;
     702                        }
     703                }
     704
     705                // < PHP 5.4.7 compat, doesn't detect schemeless URL's host field
     706                if ( '//' == substr( $url, 0, 2 ) && ! isset( $parts['host'] ) ) {
     707                        list( $parts['host'], $slashless_path ) = explode( '/', substr( $parts['path'], 2 ), 2 );
     708                        $parts['path'] = "/{$slashless_path}";
     709                }
     710
     711                return $parts;
     712        }
     713
     714        /**
     715         * Converts a relative URL to an absolute URL relative to a given URL.
     716         *
     717         * If an Absolute URL is provided, no processing of that URL is done.
     718         *
     719         * @since 3.4.0
     720         *
     721         * @access public
     722         * @param string $maybe_relative_path The URL which might be relative
     723         * @param string $url                 The URL which $maybe_relative_path is relative to
     724         * @return string An Absolute URL, in a failure condition where the URL cannot be parsed, the relative URL will be returned.
     725         */
    674726        public static function make_absolute_url( $maybe_relative_path, $url ) {
    675727                if ( empty( $url ) )
    676728                        return $maybe_relative_path;
    677729
    678                 if ( ! $url_parts = @parse_url( $url ) )
     730                if ( ! $url_parts = WP_HTTP::parse_url( $url ) ) {
    679731                        return $maybe_relative_path;
    680 
    681                 if ( ! $relative_url_parts = @parse_url( $maybe_relative_path ) )
     732                }
     733
     734                if ( ! $relative_url_parts = WP_HTTP::parse_url( $maybe_relative_path ) ) {
    682735                        return $maybe_relative_path;
     736                }
    683737
    684738                // Check for a scheme on the 'relative' url
Note: See TracChangeset for help on using the changeset viewer.