You are here

function print_rewrite_urls in Printer, email and PDF versions 5.2

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

We need to manipulate URLs so that they are recorded as absolute in the Printer-friendly URLs list, and to add a [n] footnote marker.

Return value

string containing the changed <a> tag

3 string references to 'print_rewrite_urls'
print_generate_book in ./print.module
Outputs a printer-friendly page. Used for book pages
print_generate_node in ./print.module
Outputs a printer-friendly page. Used for content types
print_generate_path in ./print.module
Outputs a printer-friendly page. Used for drupal core pages.

File

./print.module, line 483
Display printer-friendly versions of Drupal pages

Code

function print_rewrite_urls($matches) {
  global $base_url;

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

  //Temporarily convert spaces to %20 so that it isn't split below
  $in_string = false;
  for ($i = 0; $i < strlen($matches[1]); $i++) {
    if ($matches[1][$i] == '"') {
      $in_string = !$in_string;
    }
    if ($matches[1][$i] == ' ' && $in_string) {
      $matches[1] = substr_replace($matches[1], "%20", $i, 1);
    }
  }

  // first, split the html into the different tag attributes
  $attribs = preg_split("/\\s+/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 = urldecode($url);
      }
      else {
        if (substr($url, 0, 1) == "#") {

          // URL is an anchor tag
          if ($pfurls) {
            $path = substr($_GET['q'], strlen(PRINT_PATH) + 1);
            if (is_numeric($path)) {
              $path = "node/{$path}";
            }

            // Printer-friendly URLs is on, so we need to make it absolute
            $newurl = url($path, NULL, substr(urldecode($url), 1), TRUE);
          }

          // Because base href is the original page, change the link to
          // still be usable inside the print page
          $matches[1] = str_replace($url, $_GET['q'] . $url, $matches[1]);
        }
        else {

          // URL is relative, convert it into absolute URL
          $clean_url = (bool) variable_get('clean_url', '0');
          if (substr($url, 0, 1) == "/") {

            // If it starts with '/' just append it to the server name
            $server = substr($base_url, 0, strpos($base_url, '/', 7));
            $newurl = $server . '/' . trim(urldecode($url), "/");
          }
          elseif (!$clean_url && preg_match("/^[index.php]?\\?q=.*/i", $url)) {

            // If Clean URLs is disabled, and it starts with q=?, just prepend with the base URL
            $newurl = $base_url . '/' . trim(urldecode($url), "/");
          }
          else {
            $newurl = url(trim(urldecode($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 && $newurl) {
      $ret .= ' <span class="print-footnote">[' . print_friendly_urls(trim(stripslashes($newurl))) . ']</span>';
    }
  }
  return $ret;
}