You are here

function http_request_create_absolute_url in Feeds 7

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.2 libraries/http_request.inc \http_request_create_absolute_url()

Create an absolute url.

Parameters

string $url: The href to transform.

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

Return value

string an absolute url

1 call to http_request_create_absolute_url()
http_request_get_common_syndication in libraries/http_request.inc
Discover RSS or atom feeds at the given URL. If document in given URL is an HTML document, function attempts to discover RSS or Atom feeds.

File

libraries/http_request.inc, line 313
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);
    $path = dirname($parsed_url['path']);

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

      // Backtracking from the existing path.
      $cparts = array_merge(array_filter(explode("/", $path)), array_filter(explode("/", $url)));
      foreach ($cparts as $i => $part) {
        if ($part == '.') {
          $cparts[$i] = null;
        }
        if ($part == '..') {
          $cparts[$i - 1] = null;
          $cparts[$i] = null;
        }
      }
      $cparts = array_filter($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;
}