5 * Contains the functions to generate Printer-friendly pages.
7 * This file is included by the core PF module, and includes all the
8 * functions necessary to generate a PF version of the original page
14 $_print_urls = PRINT_URLS_DEFAULT
;
17 * Generate an HTML version of the printer-friendly page
19 * @see print_controller()
21 function print_controller_html() {
22 $args = func_get_args();
23 $path = filter_xss(implode('/', $args));
24 $cid = isset($_GET['comment']) ?
(int)$_GET['comment'] : NULL
;
30 $print = print_controller($path, $query, $cid, PRINT_HTML_FORMAT
);
31 if ($print !== FALSE
) {
32 $node = $print['node'];
33 $html = theme('print', array('print' => $print, 'type' => PRINT_HTML_FORMAT
, 'node' => $node));
34 drupal_add_http_header('Content-Type', 'text/html; charset=utf-8');
35 drupal_send_headers();
38 $nodepath = (isset($node->path
) && is_string($node->path
)) ?
drupal_get_normal_path($node->path
) : 'node/' .
$path;
39 db_merge('print_page_counter')
40 ->key(array('path' => $nodepath))
43 'timestamp' => REQUEST_TIME
,
45 ->expression('totalcount', 'totalcount + 1')
51 * Select the print generator function based on the page type
53 * Depending on the type of node, this functions chooses the appropriate
57 * path of the original page
59 * (optional) array of key/value pairs as used in the url() function for the
62 * comment ID of the individual comment to be rendered
64 * format of the page being generated
66 * if set to TRUE, outputs only the node's teaser
68 * optional sender's message (used by the send email module)
70 * array with the fields to be used in the template
71 * @see _print_generate_node()
72 * @see _print_generate_path()
73 * @see _print_generate_book()
75 function print_controller($path, $query = NULL
, $cid = NULL
, $format = PRINT_HTML_FORMAT
, $teaser = FALSE
, $message = NULL
) {
77 // If no path was provided, let's try to generate a page for the referer
80 $ref = $_SERVER['HTTP_REFERER'];
81 $path = preg_replace("!^$base_url/!", '', $ref);
82 if (($path === $ref) || empty($path)) {
83 $path = variable_get('site_frontpage', 'node');
86 if ($alias = drupal_lookup_path('source', $path)) {
87 // Indirect call with print/alias
88 // If there is a path alias with these arguments, generate a printer-friendly version for it
91 $parts = explode('/', $path);
92 if (($parts[0] == 'node') && (count($parts) > 1) && ctype_digit($parts[1])) {
94 $path = implode('/', $parts);
96 if (ctype_digit($parts[0]) && (count($parts) == 1)) {
97 $print = _print_generate_node($path, $query, $cid, $format, $teaser, $message);
100 $ret = preg_match('!^book/export/html/(.*)!i', $path, $matches);
102 // This is a book PF page link, handle trough the book handling functions
103 $print = _print_generate_book($matches[1], $query, $format, $teaser, $message);
106 // If no content node was found, handle the page printing with the 'printable' engine
107 $print = _print_generate_path($path, $query, $format, $teaser, $message);
115 * Generates a robots meta tag to tell them what they may index
118 * string with the meta robots tag
120 function _print_robots_meta_generator() {
121 $print_robots_noindex = variable_get('print_robots_noindex', PRINT_ROBOTS_NOINDEX_DEFAULT
);
122 $print_robots_nofollow = variable_get('print_robots_nofollow', PRINT_ROBOTS_NOFOLLOW_DEFAULT
);
123 $print_robots_noarchive = variable_get('print_robots_noarchive', PRINT_ROBOTS_NOARCHIVE_DEFAULT
);
124 $robots_meta = array();
126 if (!empty($print_robots_noindex)) {
127 $robots_meta[] = 'noindex';
129 if (!empty($print_robots_nofollow)) {
130 $robots_meta[] = 'nofollow';
132 if (!empty($print_robots_noarchive)) {
133 $robots_meta[] = 'noarchive';
136 if (count($robots_meta) > 0) {
137 $robots_meta = implode(', ', $robots_meta);
138 $robots_meta = "<meta name='robots' content='$robots_meta' />\n";
148 * Post-processor that fills the array for the template with common details
151 * generated node with a printer-friendly node body
152 * @param array $query
153 * (optional) array of key/value pairs as used in the url() function for the
156 * optional sender's message (used by the send email module)
158 * id of current comment being generated (NULL when not generating
159 * an individual comment)
161 * array with the fields to be used in the template
163 function _print_var_generator($node, $query = NULL
, $message = NULL
, $cid = NULL
) {
164 global $base_url, $language, $_print_urls;
166 $path = empty($node->nid
) ?
$node->path
: "node/$node->nid";
168 // print module settings
169 $print_css = variable_get('print_css', PRINT_CSS_DEFAULT
);
170 $print_keep_theme_css = variable_get('print_keep_theme_css', PRINT_KEEP_THEME_CSS_DEFAULT
);
171 $print_logo_options = variable_get('print_logo_options', PRINT_LOGO_OPTIONS_DEFAULT
);
172 $print_logo_url = variable_get('print_logo_url', PRINT_LOGO_URL_DEFAULT
);
173 $print_html_new_window = variable_get('print_html_new_window', PRINT_HTML_NEW_WINDOW_DEFAULT
);
174 $print_html_sendtoprinter = variable_get('print_html_sendtoprinter', PRINT_HTML_SENDTOPRINTER_DEFAULT
);
175 $print_html_windowclose = variable_get('print_html_windowclose', PRINT_HTML_WINDOWCLOSE_DEFAULT
);
176 $print_sourceurl_enabled = variable_get('print_sourceurl_enabled', PRINT_SOURCEURL_ENABLED_DEFAULT
);
177 $print_sourceurl_forcenode = variable_get('print_sourceurl_forcenode', PRINT_SOURCEURL_FORCENODE_DEFAULT
);
178 $print_sourceurl_date = variable_get('print_sourceurl_date', PRINT_SOURCEURL_DATE_DEFAULT
);
179 $print_footer_options = variable_get('print_footer_options', PRINT_FOOTER_OPTIONS_DEFAULT
);
180 $print_footer_user = variable_get('print_footer_user', PRINT_FOOTER_USER_DEFAULT
);
182 $print['language'] = $language->language
;
183 $print['title'] = check_plain($node->title
);
184 $print['head'] = drupal_get_html_head();
185 if ($print_html_sendtoprinter) {
186 drupal_add_js('misc/drupal.js', array('weight' => JS_LIBRARY
));
188 $print['scripts'] = drupal_get_js();
189 $print['footer_scripts'] = drupal_get_js('footer');
190 $print['robots_meta'] = _print_robots_meta_generator();
191 $print['url'] = url($path, array('absolute' => TRUE
, 'query' => $query));
192 $print['base_href'] = "<base href='" .
$print['url'] .
"' />\n";
193 $print['favicon'] = theme_get_setting('toggle_favicon') ?
"<link rel='shortcut icon' href='" .
theme_get_setting('favicon') .
"' type='image/x-icon' />\n" : '';
195 if (!empty($print_css)) {
196 drupal_add_css(strtr($print_css, array('%t' => path_to_theme())));
199 drupal_add_css(drupal_get_path('module', 'print') .
'/css/print.css');
201 $drupal_css = drupal_add_css();
202 if (!$print_keep_theme_css) {
203 foreach ($drupal_css as
$key => $css_file) {
204 if ($css_file['group'] == CSS_THEME
) {
205 // Unset the theme's CSS
206 unset($drupal_css[$key]);
211 // If we are sending a message via email, the CSS must be embedded
212 if (!empty($message)) {
214 $css_files = array_keys($drupal_css);
215 foreach ($css_files as
$filename) {
216 $res = file_exists($filename) ?
file_get_contents($filename, TRUE
) : FALSE
;
221 $print['css'] = "<style type='text/css' media='all'>$style</style>\n";
224 $print['css'] = drupal_get_css($drupal_css);
227 $window_close = ($print_html_new_window && $print_html_windowclose) ?
'window.close();' : '';
228 $print['sendtoprinter'] = $print_html_sendtoprinter ?
'<script type="text/javascript">(function ($) { Drupal.behaviors.print = {attach: function(context) {$(window).load(function() {window.print();' .
$window_close .
'})}}})(jQuery);</script>' : '';
230 switch ($print_logo_options) {
235 $logo_url = theme_get_setting('logo');
237 case
2: // user-specifed
238 $logo_url = strip_tags($print_logo_url);
241 $logo_url = preg_replace('!^' .
base_path() .
'!', '', $logo_url);
242 $site_name = variable_get('site_name', 'Drupal');
243 $print['logo'] = $logo_url ?
theme('image', array('path' => $logo_url, 'alt' => $site_name, 'attributes' => array('class' => 'print-logo', 'id' => 'logo'))) : '';
245 switch ($print_footer_options) {
250 $footer_blocks = block_get_blocks_by_region('footer');
251 $footer = variable_get('site_footer', FALSE
) .
"\n" .
drupal_render($footer_blocks);
253 case
2: // user-specifed
254 $footer = $print_footer_user;
257 $print['footer_message'] = filter_xss_admin($footer);
259 $published_site = variable_get('site_name', 0);
260 if ($published_site) {
261 $print_text_published = filter_xss(variable_get('print_text_published', t('Published on %site_name')));
262 $published = t($print_text_published, array('%site_name' => $published_site));
263 $print['site_name'] = $published .
' (' .
l($base_url, $base_url) .
')';
266 $print['site_name'] = '';
269 if ($print_sourceurl_enabled == 1) {
270 /* Grab and format the src URL */
271 if (empty($print_sourceurl_forcenode)) {
272 $url = $print['url'];
275 $url = $base_url .
'/' .
(((bool
)variable_get('clean_url', '0')) ?
'' : '?q=') .
$path;
278 $url .
= "#comment-$cid";
280 $retrieved_date = format_date(REQUEST_TIME
, 'short');
281 $print_text_retrieved = filter_xss(variable_get('print_text_retrieved', t('retrieved on %date')));
282 $retrieved = t($print_text_retrieved, array('%date' => $retrieved_date));
283 $print['printdate'] = $print_sourceurl_date ?
" ($retrieved)" : '';
285 $source_url = filter_xss(variable_get('print_text_source_url', t('Source URL')));
286 $print['source_url'] = '<strong>' .
$source_url .
$print['printdate'] .
':</strong> ' .
l($url, $url);
289 $print['source_url'] = '';
292 $print['type'] = (isset($node->type
)) ?
$node->type
: '';
294 menu_set_active_item($path);
295 $breadcrumb = drupal_get_breadcrumb();
296 if (!empty($breadcrumb)) {
297 $breadcrumb[] = menu_get_active_title();
298 $print['breadcrumb'] = filter_xss(implode(' > ', $breadcrumb));
301 $print['breadcrumb'] = '';
304 // Display the collected links at the bottom of the page. Code once taken from Kjartan Mannes' project.module
305 $print['pfp_links'] = '';
306 if (!empty($_print_urls)) {
307 $urls = _print_friendly_urls();
311 for ($i = 0; $i < $max; $i++) {
312 $pfp_links .
= '[' .
($i + 1) .
'] ' .
check_plain($urls[$i]) .
"<br />\n";
314 $links = filter_xss(variable_get('print_text_links', t('Links')));
315 $print['pfp_links'] = "<p><strong>$links:</strong><br />$pfp_links</p>";
319 $print['node'] = $node;
320 $print['message'] = $message;
326 * Callback function for the preg_replace_callback for URL-capable patterns
328 * Manipulate URLs to make them absolute in the URLs list, and to add a
329 * [n] footnote marker.
332 * array with the matched tag patterns, usually <a...>+text+</a>
334 * tag with re-written URL and when appropriate the [n] index to the
337 function _print_rewrite_urls($matches) {
338 global $base_url, $base_root, $_print_urls;
340 $include_anchors = variable_get('print_urls_anchors', PRINT_URLS_ANCHORS_DEFAULT
);
342 // first, split the html into the different tag attributes
343 $pattern = '!\s*(\w+\s*=\s*"(?:\\\"|[^"])*")\s*|\s*(\w+\s*=\s*\'(?:\\\\\'|[^\'])*\')\s*|\s*(\w+\s*=\s*\w+)\s*|\s+!';
344 $attribs = preg_split($pattern, $matches[1], -1, PREG_SPLIT_NO_EMPTY
| PREG_SPLIT_DELIM_CAPTURE
);
345 foreach ($attribs as
$key => $value) {
346 $attribs[$key] = preg_replace('!(\w)\s*=\s*(.*)!', '$1=$2', $value);
349 $size = count($attribs);
350 for ($i=1; $i < $size; $i++) {
351 // If the attribute is href or src, we may need to rewrite the URL in the value
352 if (preg_match('!^(?:href|src)\s*?=(.*)!i', $attribs[$i], $urls) > 0) {
353 $url = trim($urls[1], " \t\n\r\0\x0B\"'");
356 // If URL is empty, use current_url
357 $path = explode('/', $_GET['q']);
359 $path = implode('/', $path);
360 if (ctype_digit($path)) {
361 $path = "node/$path";
363 // Printer-friendly URLs is on, so we need to make it absolute
364 $newurl = url($path, array('fragment' => drupal_substr($url, 1), 'absolute' => TRUE
));
366 elseif (strpos(html_entity_decode($url), '://') || preg_match('!^mailto:.*?@.*?\..*?$!iu', html_entity_decode($url))) {
367 // URL is absolute, do nothing
370 elseif (strpos(html_entity_decode($url), '//') === 0) {
371 // URL is 'almost absolute', but it does not contain protocol; replace with base_path protocol
372 $newurl = (empty($_SERVER['HTTPS']) ?
'http' : 'https') .
":" .
$url;
373 $matches[1] = str_replace($url, $newurl, $matches[1]);
376 if ($url[0] == '#') {
377 // URL is an anchor tag
378 if ($include_anchors && (!empty($_print_urls))) {
379 $path = explode('/', $_GET['q']);
381 $path = implode('/', $path);
382 if (ctype_digit($path)) {
383 $path = "node/$path";
385 // Printer-friendly URLs is on, so we need to make it absolute
386 $newurl = url($path, array('fragment' => drupal_substr($url, 1), 'absolute' => TRUE
));
388 // Because base href is the original page, change the link to
389 // still be usable inside the print page
390 $matches[1] = str_replace($url, base_path() .
$_GET['q'] .
$url, $matches[1]);
393 // URL is relative, convert it into absolute URL
394 if ($url[0] == '/') {
395 // If it starts with '/' just append it to the server name
396 $newurl = $base_root .
'/' .
trim($url, '/');
398 elseif (preg_match('!^(?:index.php)?\?q=!i', $url)) {
399 // If it starts with ?q=, just prepend with the base URL
400 $newurl = $base_url .
'/' .
trim($url, '/');
403 $newurl = url(trim($url, '/'), array('absolute' => TRUE
));
405 $matches[1] = str_replace($url, $newurl, $matches[1]);
411 $ret = '<' .
$matches[1] .
'>';
412 if (count($matches) == 4) {
413 $ret .
= $matches[2] .
$matches[3];
414 if ((!empty($_print_urls)) && (isset($newurl))) {
415 $ret .
= ' <span class="print-footnote">[' .
_print_friendly_urls(trim($newurl)) .
']</span>';
423 * Auxiliary function to store the Printer-friendly URLs list as static.
426 * absolute URL to be inserted in the list
428 * list of URLs previously stored if $url is 0, or the current count
431 function _print_friendly_urls($url = 0) {
432 static
$urls = array();
434 $url_idx = array_search($url, $urls);
435 if ($url_idx !== FALSE
) {
436 return ($url_idx + 1);
449 * Check URL list settings for this node
454 * format of the page being generated
456 * TRUE if URL list should be displayed, FALSE otherwise
458 function _print_url_list_enabled($node, $format = PRINT_HTML_FORMAT
) {
459 if (!isset($node->type
)) {
461 case PRINT_HTML_FORMAT
:
462 $node_urllist = variable_get('print_display_sys_urllist', PRINT_TYPE_SYS_URLLIST_DEFAULT
);
464 case PRINT_MAIL_FORMAT
:
465 $node_urllist = variable_get('print_mail_display_sys_urllist', PRINT_TYPE_SYS_URLLIST_DEFAULT
);
467 case PRINT_PDF_FORMAT
:
468 $node_urllist = variable_get('print_pdf_display_sys_urllist', PRINT_TYPE_SYS_URLLIST_DEFAULT
);
471 $node_urllist = PRINT_TYPE_SYS_URLLIST_DEFAULT
;
476 case PRINT_HTML_FORMAT
:
477 $node_urllist = isset($node->print_display_urllist
) ?
$node->print_display_urllist
: variable_get('print_display_urllist_' .
$node->type
, PRINT_TYPE_URLLIST_DEFAULT
);
479 case PRINT_MAIL_FORMAT
:
480 $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
);
482 case PRINT_PDF_FORMAT
:
483 $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
);
486 $node_urllist = PRINT_TYPE_URLLIST_DEFAULT
;
490 // Get value of Printer-friendly URLs setting
491 return (variable_get('print_urls', PRINT_URLS_DEFAULT
) && ($node_urllist));
495 * Prepare a Printer-friendly-ready node body for content nodes
498 * node ID of the node to be rendered into a printer-friendly page
499 * @param array $query
500 * (optional) array of key/value pairs as used in the url() function for the
503 * comment ID of the individual comment to be rendered
505 * format of the page being generated
507 * if set to TRUE, outputs only the node's teaser
509 * optional sender's message (used by the send email module)
511 * filled array ready to be used in the template
513 function _print_generate_node($nid, $query = NULL
, $cid = NULL
, $format = PRINT_HTML_FORMAT
, $teaser = FALSE
, $message = NULL
) {
516 if (!isset($langcode)) {
517 $langcode = $GLOBALS['language_content']->language
;
520 // We can take a node id
521 $node = node_load($nid);
527 elseif (!node_access('view', $node)) {
529 drupal_access_denied();
532 drupal_set_title($node->title
);
534 $view_mode = $teaser ?
'teaser' : 'print';
536 // Turn off Pagination by the Paging module
538 unset($node->page_count
);
540 // Make this page a member of the original page's organic group
541 if (function_exists('og_set_group_context') && isset($node->og_groups
)) {
542 og_set_group_context($node->og_groups
);
546 // Adapted (simplified) version of node_view
547 // Render the node content
548 node_build_content($node, $view_mode);
550 // Disable the links area
551 unset($node->content
['links']);
552 // Disable fivestar widget output
553 unset($node->content
['fivestar_widget']);
554 // Disable service links module output
555 unset($node->content
['service_links']);
557 $build = $node->content
;
558 unset($node->content
);
561 $print_comments = variable_get('print_comments', PRINT_COMMENTS_DEFAULT
);
563 if (function_exists('comment_node_page_additions') && (($cid != NULL
) || ($print_comments))) {
564 // Print only the requested comment (or if $cid is NULL, all of them)
566 $comments = comment_node_page_additions($node);
567 if (!empty($comments)) {
568 unset($comments['comment_form']);
569 foreach ($comments['comments'] as
$key => &$comment) {
570 if (is_numeric($key)) {
571 if (($cid != NULL
) && ($key != $cid)) {
572 unset($comments['comments'][$key]);
575 unset($comment['links']);
580 $build['comments'] = $comments;
587 '#view_mode' => $view_mode,
588 '#language' => $langcode,
589 '#print_format' => $format,
593 drupal_alter(array('node_view', 'entity_view'), $build, $type);
595 $content = render($build);
597 // Get rid of any links before the content
598 $parts = explode('<div class="content', $content, 2);
599 if (count($parts) == 2) {
600 $pattern = '!(.*?)<a [^>]*?>(.*?)</a>(.*?)!mis';
601 $parts[0] = preg_replace($pattern, '$1$2$3', $parts[0]);
602 $content = implode('<div class="content', $parts);
605 // Check URL list settings
606 $_print_urls = _print_url_list_enabled($node, $format);
608 // Convert the a href elements
609 $pattern = '!<(a\s[^>]*?)>(.*?)(</a>)!is';
610 $content = preg_replace_callback($pattern, '_print_rewrite_urls', $content);
612 $print = _print_var_generator($node, $query, $message, $cid);
613 $print['content'] = $content;
619 * Prepare a Printer-friendly-ready node body for non-content pages
622 * path of the node to be rendered into a printer-friendly page
623 * @param array $query
624 * (optional) array of key/value pairs as used in the url() function for the
627 * format of the page being generated
629 * if set to TRUE, outputs only the node's teaser
631 * optional sender's message (used by the send email module)
633 * filled array ready to be used in the template
635 function _print_generate_path($path, $query = NULL
, $format = PRINT_HTML_FORMAT
, $teaser = FALSE
, $message = NULL
) {
639 $parts = explode('/', $path);
640 if (ctype_digit($parts[0]) && (count($parts) > 1)) {
641 $path = 'node/' .
$path;
644 $path = drupal_get_normal_path($path);
646 menu_set_active_item($path);
647 // Adapted from index.php.
648 $node = new
stdClass();
649 $node->body
= menu_execute_active_handler($path, FALSE
);
650 if (is_array($node->body
)) {
651 $node->body
= drupal_render($node->body
);
654 if (is_int($node->body
)) {
655 switch ($node->body
) {
660 case MENU_ACCESS_DENIED
:
661 drupal_access_denied();
667 $node->title
= drupal_get_title();
671 // Delete any links area
672 $node->body
= preg_replace('!\s*<div class="links">.*?</div>!sim', '', $node->body
);
674 // Check URL list settings
675 $_print_urls = _print_url_list_enabled($node, $format);
677 // Convert the a href elements
678 $pattern = '!<(a\s[^>]*?)>(.*?)(</a>)!is';
679 $node->body
= preg_replace_callback($pattern, '_print_rewrite_urls', $node->body
);
681 $print = _print_var_generator($node, $query, $message);
682 $print['content'] = $node->body
;
689 * Prepare a Printer-friendly-ready node body for book pages
692 * node ID of the node to be rendered into a printer-friendly page
693 * @param array $query
694 * (optional) array of key/value pairs as used in the url() function for the
697 * format of the page being generated
699 * if set to TRUE, outputs only the node's teaser
701 * optional sender's message (used by the send email module)
703 * filled array ready to be used in the template
705 function _print_generate_book($nid, $query = NULL
, $format = PRINT_HTML_FORMAT
, $teaser = FALSE
, $message = NULL
) {
708 $node = node_load($nid);
714 elseif (!node_access('view', $node) || (!user_access('access printer-friendly version'))) {
716 drupal_access_denied();
720 $tree = book_menu_subtree_data($node->book
);
721 $node->body
= book_export_traverse($tree, 'book_node_export');
723 // Check URL list settings
724 $_print_urls = _print_url_list_enabled($node, $format);
726 // Convert the a href elements
727 $pattern = '!<(a\s[^>]*?)>(.*?)(</a>)!is';
728 $node->body
= preg_replace_callback($pattern, '_print_rewrite_urls', $node->body
);
730 $print = _print_var_generator($node, $query, $message);
731 $print['content'] = $node->body
;
733 // The title is already displayed by the book_recurse, so avoid duplication
734 $print['title'] = '';