WordPress.org

Make WordPress Core

Ticket #20434: 20434[1].diff

File 20434[1].diff, 3.0 KB (added by dd32, 6 years ago)
  • class-http.php

     
    535535                        return !in_array( $check['host'], $accessible_hosts ); //Inverse logic, If its in the array, then we can't access it.
    536536
    537537        }
     538
     539        function make_absolute_url( $relative_path, $url ) {
     540
     541                if ( empty( $url ) )
     542                        return $relative_path;
     543
     544                // Check for a scheme
     545                if ( false !== strpos( $relative_path, '://' ) )
     546                        return $relative_path;
     547
     548                $absolute_path = '';
     549                $url_parts = @parse_url( $url );
     550
     551                if ( ! $url_parts )
     552                        return $relative_path;
     553
     554                $absolute_path = $url_parts['scheme'] . '://' . $url_parts['host'];
     555                if ( isset( $url_parts['port'] ) )
     556                        $absolute_path .= ':' . $url_parts['port'];
     557
     558                if ( '/' == $relative_path[0] || ! isset( $url_parts['path'] ) ) { // Absolute path provided, easy.
     559                        $new_path = $relative_path;
     560
     561                } elseif ( '../' == substr( $relative_path, 0, 3 ) ) { // Relative path to a parent directory, this will miss cases such as '/../something' or '/something/../something-else/'
     562                        $url_parts['path'] = preg_replace( '![^/]+$!', '', $url_parts['path'] ); // strip files off
     563
     564                        while( '../' == substr( $relative_path, 0, 3 ) ) {
     565                                $relative_path = substr( $relative_path, 3 );
     566                                $url_parts['path'] = preg_replace( '![^/]+$!', '', untrailingslashit( $url_parts['path'] ) );
     567                        }
     568                        $new_path = ( '.' == $url_parts['path'] ? '' : trailingslashit( $url_parts['path'] ) ) . ltrim( $relative_path, '/' );
     569
     570                } else { // Relative path to a sibling, /here/me => /here/subling
     571                        if ( '/' != $relative_path[0] )
     572                                $url_parts['path'] = preg_replace( '![^/]+$!', '', $url_parts['path'] ); // delete the last segment of the path, dirname('/here/') will return /
     573                        $new_path = ( '.' == $url_parts['path'] ? '' : trailingslashit( $url_parts['path'] ) ) . ltrim( $relative_path, '/' );
     574
     575                }
     576
     577                $absolute_path .= '/' . ltrim( $new_path, '/' );
     578
     579                return $absolute_path;
     580               
     581        }
    538582}
    539583
    540584/**
     
    730774                // If location is found, then assume redirect and redirect to location.
    731775                if ( isset($arrHeaders['headers']['location']) && 0 !== $r['_redirection'] ) {
    732776                        if ( $r['redirection']-- > 0 ) {
    733                                 return $this->request($arrHeaders['headers']['location'], $r);
     777                                return $this->request( WP_HTTP::make_absolute_url( $arrHeaders['headers']['location'], $url ), $r);
    734778                        } else {
    735779                                return new WP_Error('http_request_failed', __('Too many redirects.'));
    736780                        }
     
    11311175                // See #11305 - When running under safe mode, redirection is disabled above. Handle it manually.
    11321176                if ( ! empty( $theHeaders['headers']['location'] ) && 0 !== $r['_redirection'] ) { // _redirection: The requested number of redirections
    11331177                        if ( $r['redirection']-- > 0 ) {
    1134                                 return $this->request( $theHeaders['headers']['location'], $r );
     1178                                return $this->request( WP_HTTP::make_absolute_url( $theHeaders['headers']['location'], $url ), $r );
    11351179                        } else {
    11361180                                return new WP_Error( 'http_request_failed', __( 'Too many redirects.' ) );
    11371181                        }