You are here

function _print_rewrite_urls in Printer, email and PDF versions 5.3

Same name and namespace in other branches
  1. 5.4 print.pages.inc \_print_rewrite_urls()
  2. 6 print.pages.inc \_print_rewrite_urls()
  3. 7.2 print.pages.inc \_print_rewrite_urls()
  4. 7 print.pages.inc \_print_rewrite_urls()
  5. 5.x print.pages.inc \_print_rewrite_urls()

Callback function for the preg_replace_callback for URL-capable patterns

Manipulate URLs to make them absolute in the URLs list, and to add a [n] footnote marker.

Parameters

$matches: array with the matched tag patterns, usually <a...>+text+</a>

Return value

tag with re-written URL and when appropriate the [n] index to the URL list

1 call to _print_rewrite_urls()
PrintBasicTest::testPrintRewriteUrls in tests/print_basic.test
4 string references to '_print_rewrite_urls'
print_pdf_controller in print_pdf/print_pdf.pages.inc
Generate a PDF version of the printer-friendly page
_print_generate_book in ./print.pages.inc
Prepare a Printer-friendly-ready node body for book pages
_print_generate_node in ./print.pages.inc
Prepare a Printer-friendly-ready node body for content nodes
_print_generate_path in ./print.pages.inc
Prepare a Printer-friendly-ready node body for non-content pages

File

./print.pages.inc, line 330
Contains the functions to generate Printer-friendly pages.

Code

function _print_rewrite_urls($matches) {
  global $base_url, $base_root;

  // Get value of Printer-friendly URLs setting
  $print_urls = variable_get('print_urls', PRINT_URLS_DEFAULT);

  // first, split the html into the different tag attributes
  $pattern = '!\\s*(\\w+\\s*=\\s*"(?:\\\\"|[^"])*")\\s*|\\s*(\\w+\\s*=\\s*\'(?:\\\\\'|[^\'])*\')\\s*|\\s*(\\w+\\s*=\\s*\\w+)\\s*|\\s+!';
  $attribs = preg_split($pattern, $matches[1], -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
  foreach ($attribs as $key => $value) {
    $attribs[$key] = preg_replace('!(\\w)\\s*=\\s*(.*)!', '$1=$2', $value);
  }
  $size = count($attribs);
  for ($i = 1; $i < $size; $i++) {

    // If the attribute is href or src, we may need to rewrite the URL in the value
    if (preg_match('!^(?:href|src)\\s*?=(.*)!i', $attribs[$i], $urls) > 0) {
      $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 ($url[0] == '#') {

          // URL is an anchor tag
          if (!empty($print_urls)) {
            $path = explode('/', $_GET['q']);
            unset($path[0]);
            $path = implode('/', $path);
            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
          if ($url[0] == '/') {

            // If it starts with '/' just append it to the server name
            $newurl = $base_root . '/' . trim(urldecode($url), '/');
          }
          elseif (preg_match('!^(?:index.php)?\\?q=!i', $url)) {

            // If 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 (count($matches) == 4) {
    $ret .= $matches[2] . $matches[3];
    if (!empty($print_urls) && isset($newurl)) {
      $ret .= ' <span class="print-footnote">[' . _print_friendly_urls(trim(stripslashes($newurl))) . ']</span>';
    }
  }
  return $ret;
}