You are here

function cf_adjust_url in Common Functionality 7.2

Fix relative url pulled from the remote server.

This url is turned into an absolute url.

@see: cf_adjust_urls()

Parameters

string $url: The URL to change.

string $server: The hostname or ip address of the server to use when generating absolute urls. This must not contain the 'http://' prefixes nor the suffixes such as '/' or ':80'.

string $relative_path: all relative paths will have this prepended to the absolute url.

string $scheme: (optional) The 'http' at the front of most urls. A common alternative is 'https'.

string $suffix: (optional) The suffix to prepend to the url. Most cases this should be '/', but if the links are being cached on a different server and a different sub-path, then this must be used.

int $port: (optional) The port number of the web-server. In almost all cases this should be 80. If $schema is set to 'https', then normally this should instead be 443.

Return value

string|bool The new string if it was successfully altered and FALSE otherwise.

Related topics

1 call to cf_adjust_url()
cf_http_adjust_urls in modules/cf_http/cf_http.module
Fix relative urls pulled from the remote server.

File

modules/cf_http/cf_http.module, line 542
Common Functionality - HTTP module.

Code

function cf_adjust_url($url, $server, $relative_path, $scheme = 'http', $suffix = '/', $port = 80) {
  if (!is_string($url)) {
    if (class_exists('cf_error')) {
      cf_error::invalid_string('url');
    }
    return FALSE;
  }
  $parsed_url = parse_url($url);
  if (!isset($parsed_url['host'])) {
    $parsed_url['scheme'] = $scheme;
    $parsed_url['host'] = $server;
    if (!($scheme == 'http' && $port == 80) && !($scheme == 'https' && $port == 443)) {
      $parsed_url['port'] = $port;
    }
    $generated_url = $parsed_url['scheme'] . '://';
    $generated_url .= $parsed_url['host'];
    if (!empty($parsed_url['port'])) {
      $generated_url .= ':' . $parsed_url['port'];
    }
    if (!empty($parsed_url['path'])) {
      if (preg_match('/^\\//i', $parsed_url['path']) == 0) {
        $generated_url .= $relative_path . '/';
      }
      $generated_url .= $parsed_url['path'];
    }
    else {
      $generated_url .= $relative_path . '/';
    }
    if (!empty($parsed_url['query'])) {
      $generated_url .= '?' . $parsed_url['query'];
    }
    if (!empty($parsed_url['fragment'])) {
      $generated_url .= '#' . $parsed_url['fragment'];
    }
    return $generated_url;
  }
  return FALSE;
}