| | 538 | |
| | 539 | static function make_absolute_url( $maybe_relative_path, $url ) { |
| | 540 | if ( empty( $url ) ) |
| | 541 | return $maybe_relative_path; |
| | 542 | |
| | 543 | // Check for a scheme |
| | 544 | if ( false !== strpos( $maybe_relative_path, '://' ) ) |
| | 545 | return $maybe_relative_path; |
| | 546 | |
| | 547 | if ( ! $url_parts = @parse_url( $url ) ) |
| | 548 | return $maybe_relative_path; |
| | 549 | |
| | 550 | if ( ! $relative_url_parts = @parse_url( $maybe_relative_path ) ) |
| | 551 | return $maybe_relative_path; |
| | 552 | |
| | 553 | $absolute_path = $url_parts['scheme'] . '://' . $url_parts['host']; |
| | 554 | if ( isset( $url_parts['port'] ) ) |
| | 555 | $absolute_path .= ':' . $url_parts['port']; |
| | 556 | |
| | 557 | // Start off with the Absolute URL path |
| | 558 | $path = ! empty( $url_parts['path'] ) ? $url_parts['path'] : '/'; |
| | 559 | |
| | 560 | // If the it's a root-relative path, then great |
| | 561 | if ( ! empty( $relative_url_parts['path'] ) && '/' == $relative_url_parts['path'][0] ) { |
| | 562 | $path = $relative_url_parts['path']; |
| | 563 | |
| | 564 | // Else it's a relative path |
| | 565 | } elseif ( ! empty( $relative_url_parts['path'] ) ) { |
| | 566 | // Strip off any file components from the absolute path |
| | 567 | $path = substr( $path, 0, strrpos( $path, '/' ) + 1 ); |
| | 568 | |
| | 569 | // Build the new path |
| | 570 | $path .= $relative_url_parts['path']; |
| | 571 | |
| | 572 | // Strip all /path/../ out of the path |
| | 573 | while ( strpos( $path, '../' ) > 1 ) { |
| | 574 | $path = preg_replace( '![^/]+/\.\./!', '', $path ); |
| | 575 | } |
| | 576 | |
| | 577 | // Strip any final leading ../ from the path |
| | 578 | $path = preg_replace( '!^/(\.\./)+!', '', $path ); |
| | 579 | } |
| | 580 | |
| | 581 | // Add the Query string |
| | 582 | if ( ! empty( $relative_url_parts['query'] ) ) |
| | 583 | $path .= '?' . $relative_url_parts['query']; |
| | 584 | |
| | 585 | return $absolute_path . '/' . ltrim( $path, '/' ); |
| | 586 | } |