You are here

function print_rewrite_urls in Printer, email and PDF versions 5

Same name and namespace in other branches
  1. 5.2 print.module \print_rewrite_urls()

We need to manipulate URLs in two manners, depending on the state of the Printer Friendly URLs setting: 1. When on, we need to show the original URLs, making sure that they are absolute (even anchor names). 2. When off, relative URLs must be transformed to absolute URLs, including images, but not the anchors.

1 string reference to 'print_rewrite_urls'
print_generate_node in ./print.module
Outputs a printer friendly page.

File

./print.module, line 303
Display printer friendly versions of nodes (except books)

Code

function print_rewrite_urls($matches) {

  // Get value of Printer Friendly URLs setting
  $print_settings = variable_get('print_settings', print_settings_default());
  $pfurls = !empty($print_settings['urls']);

  // first, split the html into the different tag attributes
  $attribs = preg_split("/[ \t]+/m", $matches[1]);
  for ($i = 1; $i < count($attribs); $i++) {

    // If the attribute is href or src, we may need to rewrite the URL in the value
    if (preg_match("/^href|src/i", $attribs[$i]) > 0) {

      // We may need to rewrite the URL, so let's isolate it
      preg_match("/.*?=(.*)/is", $attribs[$i], $urls);
      $url = trim($urls[1], " \t\n\r\0\v\"\\'");
      if (strpos($url, '://') || preg_match("/^mailto:.*?@.*?\\..*?\$/iu", $url)) {

        // URL is absolute, do nothing
        $newurl = $url;
      }
      else {
        if (substr($url, 0, 1) == "#") {

          // URL is an anchor tag
          if ($pfurls) {

            // Printer Friendly URLs is on, so we need to make it absolute
            $newurl = url("node/" . $node->nid, NULL, substr($url, 1), TRUE);
            $matches[1] = str_replace($url, $newurl, $matches[1]);
          }
        }
        else {

          // URL is relative, convert it into absolute URL, removing any extra beginning or end '/'
          $newurl = url(trim($url, "/"), NULL, NULL, TRUE);
          $matches[1] = str_replace($url, $newurl, $matches[1]);
        }
      }
    }
  }
  $ret = '<' . $matches[1] . '>';
  if ($attribs[0] == "a") {
    $ret .= $matches[2] . '</a>';
    if ($pfurls) {
      $ret .= ' <span class="print-footnote">[' . print_friendly_urls(trim(stripslashes($newurl))) . ']</span>';
    }
  }
  return $ret;
}