You are here

function http_request_create_absolute_url in Feeds 6

Same name and namespace in other branches
  1. 8.2 libraries/http_request.inc \http_request_create_absolute_url()
  2. 7.2 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

1 call to 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 356
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;
}