You are here

function http_request_create_absolute_url in Feeds 7.2

Same name and namespace in other branches
  1. 8.2 libraries/http_request.inc \http_request_create_absolute_url()
  2. 6 libraries/http_request.inc \http_request_create_absolute_url()
  3. 7 libraries/http_request.inc \http_request_create_absolute_url()

Create an absolute url.

Parameters

string $url: The href to transform.

string $base_url: The url to be used as the base for a relative $url.

Return value

string An absolute url

2 calls to http_request_create_absolute_url()
FeedsHTTPRequestTestCase::testHTTPRequestCreateAbsoluteUrl in tests/http_request.test
Tests http_request_create_absolute_url().
http_request_get_common_syndication in libraries/http_request.inc
Discovers RSS or atom feeds at the given URL.

File

libraries/http_request.inc, line 561
Download via HTTP.

Code

function http_request_create_absolute_url($url, $base_url) {
  $url = trim($url);
  if (valid_url($url, TRUE)) {

    // Valid absolute url already.
    return $url;
  }

  // Turn relative url into absolute.
  if (valid_url($url, FALSE)) {

    // Produces variables $scheme, $host, $user, $pass, $path, $query and
    // $fragment.
    $parsed_url = parse_url($base_url);
    if ($parsed_url === FALSE) {

      // Invalid $base_url.
      return FALSE;
    }
    $path = isset($parsed_url['path']) ? $parsed_url['path'] : '';
    if (strlen($path) > 0 && substr($path, -1) != '/') {

      // Path ends not with '/', so remove all before previous '/'.
      $path = dirname($path);
    }

    // Adding to the existing path.
    $cparts = array();
    if ($url[0] == '/') {
      $cparts = array_filter(explode("/", $url));
    }
    else {

      // Backtracking from the existing path.
      $path_cparts = array_filter(explode("/", $path));
      $url_cparts = array_filter(explode("/", $url));
      $cparts = array_merge($path_cparts, $url_cparts);
    }
    $remove_parts = 0;

    // Start from behind.
    $reverse_cparts = array_reverse($cparts);
    foreach ($reverse_cparts as $i => &$part) {
      if ($part == '.') {
        $part = NULL;
      }
      elseif ($part == '..') {
        $part = NULL;
        $remove_parts++;
      }
      elseif ($remove_parts > 0) {

        // If the current part isn't "..", and we had ".." before, then delete
        // the part.
        $part = NULL;
        $remove_parts--;
      }
    }
    $cparts = array_filter(array_reverse($reverse_cparts));
    $path = implode("/", $cparts);

    // Build the prefix to the path.
    $absolute_url = '';
    if (isset($parsed_url['scheme'])) {
      $absolute_url = $parsed_url['scheme'] . '://';
    }
    if (isset($parsed_url['user'])) {
      $absolute_url .= $parsed_url['user'];
      if (isset($pass)) {
        $absolute_url .= ':' . $parsed_url['pass'];
      }
      $absolute_url .= '@';
    }
    if (isset($parsed_url['host'])) {
      $absolute_url .= $parsed_url['host'] . '/';
    }
    $absolute_url .= $path;
    if (valid_url($absolute_url, TRUE)) {
      return $absolute_url;
    }
  }
  return FALSE;
}