You are here

function _print_rewrite_urls in Printer, email and PDF versions 7.2

Same name and namespace in other branches
  1. 5.4 print.pages.inc \_print_rewrite_urls()
  2. 5.3 print.pages.inc \_print_rewrite_urls()
  3. 6 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 add a [n] footnote marker.

Parameters

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

Return value

string tag with re-written URL and, if applicable, the [n] index to the URL list

1 call to _print_rewrite_urls()
PrintBasicTest::testPrintRewriteUrls in tests/print_basic.test
Test rewrite of URLs.
5 string references to '_print_rewrite_urls'
print_mail_form_submit in print_mail/print_mail.inc
Form submission handler for print_mail_form().
print_pdf_generate_path in print_pdf/print_pdf.pages.inc
Gennerate a PDF for a given Drupal path.
_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 450

Code

function _print_rewrite_urls($matches) {
  global $base_url, $base_root, $_print_urls;
  $include_anchors = variable_get('print_urls_anchors', PRINT_URLS_ANCHORS_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, 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 (empty($url)) {

        // If URL is empty, use current_url.
        $path = explode('/', $_GET['q']);
        unset($path[0]);
        $path = implode('/', $path);
        if (ctype_digit($path)) {
          $path = "node/{$path}";
        }

        // Printer-friendly URLs is on, so we need to make it absolute.
        $newurl = url($path, array(
          'fragment' => drupal_substr($url, 1),
          'absolute' => TRUE,
        ));
      }
      elseif (strpos(html_entity_decode($url), '://') || preg_match('!^mailto:.*?@.*?\\..*?$!iu', html_entity_decode($url))) {

        // URL is absolute, do nothing.
        $newurl = $url;
      }
      elseif (strpos(html_entity_decode($url), '//') === 0) {

        // URL is 'almost absolute', but it does not contain protocol; replace
        // with base_path protocol.
        $newurl = (empty($_SERVER['HTTPS']) ? 'http' : 'https') . ":" . $url;
        $matches[1] = str_replace($url, $newurl, $matches[1]);
      }
      else {
        if ($url[0] == '#') {

          // URL is an anchor tag.
          if ($include_anchors && !empty($_print_urls)) {
            $path = explode('/', $_GET['q']);
            unset($path[0]);
            $path = implode('/', $path);
            if (ctype_digit($path)) {
              $path = "node/{$path}";
            }

            // Printer-friendly URLs is on, so we need to make it absolute.
            $newurl = url($path, array(
              'fragment' => drupal_substr($url, 1),
              'absolute' => TRUE,
            ));
          }

          // Because base href is the original page, change the link to
          // still be usable inside the print page.
          $matches[1] = str_replace($url, check_plain(base_path() . $_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($url, '/');
          }
          elseif (preg_match('!^(?:index.php)?\\?q=!i', $url)) {

            // If it starts with ?q=, just prepend with the base URL.
            $newurl = $base_url . '/' . trim($url, '/');
          }
          else {
            $newurl = url(trim($url, '/'), array(
              'absolute' => 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($newurl)) . ']</span>';
    }
  }
  return filter_xss_admin($ret);
}