";
}
}
$print['node'] = $node;
$print['message'] = $message;
return $print;
}
/**
* 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.
*
* @param $matches
* array with the matched tag patterns, usually +text+
* @return
* tag with re-written URL and when appropriate the [n] index to the
* URL list
*/
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, 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\x0B\"'");
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 .= ' [' . _print_friendly_urls(trim($newurl)) . ']';
}
}
return filter_xss_admin($ret);
}
/**
* Auxiliary function to store the Printer-friendly URLs list as static.
*
* @param $url
* absolute URL to be inserted in the list
* @return
* list of URLs previously stored if $url is 0, or the current count
* otherwise.
*/
function _print_friendly_urls($url = 0) {
static $urls = array();
if ($url !== 0) {
$url_idx = array_search($url, $urls);
if ($url_idx !== FALSE) {
return ($url_idx + 1);
}
else {
$urls[] = $url;
return count($urls);
}
}
$ret = $urls;
$urls = array();
return $ret;
}
/**
* Check URL list settings for this node
*
* @param node
* node object
* @param $format
* format of the page being generated
* @return
* TRUE if URL list should be displayed, FALSE otherwise
*/
function _print_url_list_enabled($node, $format = PRINT_HTML_FORMAT) {
if (!isset($node->type)) {
switch ($format) {
case PRINT_HTML_FORMAT:
$node_urllist = variable_get('print_display_sys_urllist', PRINT_TYPE_SYS_URLLIST_DEFAULT);
break;
case PRINT_MAIL_FORMAT:
$node_urllist = variable_get('print_mail_display_sys_urllist', PRINT_TYPE_SYS_URLLIST_DEFAULT);
break;
case PRINT_PDF_FORMAT:
$node_urllist = variable_get('print_pdf_display_sys_urllist', PRINT_TYPE_SYS_URLLIST_DEFAULT);
break;
default:
$node_urllist = PRINT_TYPE_SYS_URLLIST_DEFAULT;
}
}
else {
switch ($format) {
case PRINT_HTML_FORMAT:
$node_urllist = isset($node->print_display_urllist) ? $node->print_display_urllist : variable_get('print_display_urllist_' . $node->type, PRINT_TYPE_URLLIST_DEFAULT);
break;
case PRINT_MAIL_FORMAT:
$node_urllist = isset($node->print_mail_display_urllist) ? $node->print_mail_display_urllist : variable_get('print_mail_display_urllist_' . $node->type, PRINT_TYPE_URLLIST_DEFAULT);
break;
case PRINT_PDF_FORMAT:
$node_urllist = isset($node->print_pdf_display_urllist) ? $node->print_pdf_display_urllist : variable_get('print_pdf_display_urllist_' . $node->type, PRINT_TYPE_URLLIST_DEFAULT);
break;
default:
$node_urllist = PRINT_TYPE_URLLIST_DEFAULT;
}
}
// Get value of Printer-friendly URLs setting
return (variable_get('print_urls', PRINT_URLS_DEFAULT) && ($node_urllist));
}
/**
* Prepare a Printer-friendly-ready node body for content nodes
*
* @param $nid
* node ID of the node to be rendered into a printer-friendly page
* @param array $query
* (optional) array of key/value pairs as used in the url() function for the
* query
* @param $cid
* comment ID of the individual comment to be rendered
* @param $format
* format of the page being generated
* @param $teaser
* if set to TRUE, outputs only the node's teaser
* @param $message
* optional sender's message (used by the send email module)
* @return
* filled array ready to be used in the template
*/
function _print_generate_node($nid, $query = NULL, $cid = NULL, $format = PRINT_HTML_FORMAT, $teaser = FALSE, $message = NULL) {
global $_print_urls;
if (!isset($langcode)) {
$langcode = $GLOBALS['language_content']->language;
}
// We can take a node id
$node = node_load($nid);
if (!$node) {
// Node not found
drupal_not_found();
return FALSE;
}
elseif (!node_access('view', $node)) {
// Access is denied
drupal_access_denied();
return FALSE;
}
drupal_set_title($node->title);
$view_mode = $teaser ? 'teaser' : 'print';
// Turn off Pagination by the Paging module
unset($node->pages);
unset($node->page_count);
// Make this page a member of the original page's organic group
if (function_exists('og_set_group_context') && isset($node->og_groups)) {
og_set_group_context($node->og_groups);
}
if ($cid === NULL) {
// Adapted (simplified) version of node_view
// Render the node content
node_build_content($node, $view_mode);
// Disable the links area
unset($node->content['links']);
// Disable fivestar widget output
unset($node->content['fivestar_widget']);
// Disable service links module output
unset($node->content['service_links']);
$build = $node->content;
unset($node->content);
}
$print_comments = variable_get('print_comments', PRINT_COMMENTS_DEFAULT);
if (function_exists('comment_node_page_additions') && (($cid != NULL) || ($print_comments))) {
// Print only the requested comment (or if $cid is NULL, all of them)
$comments = comment_node_page_additions($node);
if (!empty($comments)) {
unset($comments['comment_form']);
foreach ($comments['comments'] as $key => &$comment) {
if (is_numeric($key)) {
if (($cid != NULL) && ($key != $cid)) {
unset($comments['comments'][$key]);
}
else {
unset($comment['links']);
}
}
}
$build['comments'] = $comments;
}
}
$build += array(
'#theme' => 'node',
'#node' => $node,
'#view_mode' => $view_mode,
'#language' => $langcode,
'#print_format' => $format,
);
$type = 'node';
drupal_alter(array('node_view', 'entity_view'), $build, $type);
$content = render($build);
// Get rid of any links before the content
$parts = explode('
]*?)>(.*?)()!is';
$content = preg_replace_callback($pattern, '_print_rewrite_urls', $content);
$print = _print_var_generator($node, $query, $message, $cid);
$print['content'] = $content;
return $print;
}
/**
* Prepare a Printer-friendly-ready node body for non-content pages
*
* @param $path
* path of the node to be rendered into a printer-friendly page
* @param array $query
* (optional) array of key/value pairs as used in the url() function for the
* query
* @param $format
* format of the page being generated
* @param $teaser
* if set to TRUE, outputs only the node's teaser
* @param $message
* optional sender's message (used by the send email module)
* @return
* filled array ready to be used in the template
*/
function _print_generate_path($path, $query = NULL, $format = PRINT_HTML_FORMAT, $teaser = FALSE, $message = NULL) {
global $_print_urls;
// Handle node tabs
$parts = explode('/', $path);
if (ctype_digit($parts[0]) && (count($parts) > 1)) {
$path = 'node/' . $path;
}
$path = drupal_get_normal_path($path);
menu_set_active_item($path);
// Adapted from index.php.
$node = new stdClass();
$node->body = menu_execute_active_handler($path, FALSE);
if (is_array($node->body)) {
$node->body = drupal_render($node->body);
}
if (is_int($node->body)) {
switch ($node->body) {
case MENU_NOT_FOUND:
drupal_not_found();
return FALSE;
break;
case MENU_ACCESS_DENIED:
drupal_access_denied();
return FALSE;
break;
}
}
$node->title = drupal_get_title();
$node->path = $path;
$node->changed = 0;
// Delete any links area
$node->body = preg_replace('!\s*
.*?
!sim', '', $node->body);
// Delete the contextual links also
$node->body = preg_replace('!\s*
.*?
!sim', '', $node->body);
// Check URL list settings
$_print_urls = _print_url_list_enabled($node, $format);
// Convert the a href elements
$pattern = '!<(a\s[^>]*?)>(.*?)()!is';
$node->body = preg_replace_callback($pattern, '_print_rewrite_urls', $node->body);
$print = _print_var_generator($node, $query, $message);
$print['content'] = $node->body;
return $print;
}
/**
* Prepare a Printer-friendly-ready node body for book pages
*
* @param $nid
* node ID of the node to be rendered into a printer-friendly page
* @param array $query
* (optional) array of key/value pairs as used in the url() function for the
* query
* @param $format
* format of the page being generated
* @param $teaser
* if set to TRUE, outputs only the node's teaser
* @param $message
* optional sender's message (used by the send email module)
* @return
* filled array ready to be used in the template
*/
function _print_generate_book($nid, $query = NULL, $format = PRINT_HTML_FORMAT, $teaser = FALSE, $message = NULL) {
global $_print_urls;
$node = node_load($nid);
if (!$node) {
// Node not found
drupal_not_found();
return FALSE;
}
elseif (!node_access('view', $node) || (!user_access('access printer-friendly version'))) {
// Access is denied
drupal_access_denied();
return FALSE;
}
$tree = book_menu_subtree_data($node->book);
$node->body = book_export_traverse($tree, 'book_node_export');
// Check URL list settings
$_print_urls = _print_url_list_enabled($node, $format);
// Convert the a href elements
$pattern = '!<(a\s[^>]*?)>(.*?)()!is';
$node->body = preg_replace_callback($pattern, '_print_rewrite_urls', $node->body);
$print = _print_var_generator($node, $query, $message);
$print['content'] = $node->body;
// The title is already displayed by the book_recurse, so avoid duplication
$print['title'] = '';
return $print;
}