You are here

function cf_http_adjust_urls in Common Functionality 7

Same name and namespace in other branches
  1. 7.2 modules/cf_http/cf_http.module \cf_http_adjust_urls()

Fix relative urls pulled from the remote server.

These urls are turned into absolute urls.

Why: Custom php scripts need a straight-forward and easy way to pull data from another website. This is useful as an alternative to iframe and has advantages and disadvantages to iframes. An advantage is that this allows showing remote content even if the remote url is down (via caching). A disadvantage is that remote images and links need to be processed, updated, and possibly even manually cached.

Parameters

string $text: The html document text whose urls are to be altered.

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.

array $function_history: (optional) An array of function names, ie: array('0' => 'my_function_name').

Return value

array An array containing the connection status and return http response. The array keys:

  • adjusted: A boolean with TRUE representing that the text's urls were

successfully adjuested, FALSE otherwise.

  • text: The complete html text with all links adjusted to absolute paths.

File

modules/cf_http/cf_http.module, line 424

Code

function cf_http_adjust_urls($text, $server, $relative_path, $scheme = 'http', $suffix = '/', $port = 80, array $function_history = array()) {
  cf_error_append_history($function_history, __FUNCTION__);
  $results = array(
    'adjusted' => FALSE,
    'text' => $text,
  );
  $matches = array();
  foreach (array(
    'src',
    'href',
  ) as $attr_key => $attribute) {
    $result = preg_match_all('/(<[^>]*' . $attribute . '\\s*=\\s*)(["|\'])([^>]*)>/i', $text, $matches);
    if ($result > 0) {
      foreach ($matches[0] as $key => &$value) {
        $parts = explode($matches[2][$key], $matches[3][$key], 2);
        $parsed_url = parse_url($parts[0]);
        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'];
          }
          $safe_expression = preg_replace('/\\`/i', '\\`', $matches[1][$key] . $matches[2][$key] . $parts[0] . $matches[2][$key]);
          $safe_expression = preg_replace('/\\?/i', '\\?', $safe_expression);
          $safe_expression = preg_replace('/\\./i', '\\.', $safe_expression);
          $safe_expression = preg_replace('/\\~/i', '\\~', $safe_expression);
          $safe_text = preg_replace('`' . $safe_expression . '`si', $matches[1][$key] . $matches[2][$key] . $generated_url . $matches[2][$key], $results['text']);
          if (is_string($safe_text)) {
            $results['text'] = $safe_text;
          }
        }
      }
    }
  }
  return $results;
}