6 * Contains the functions to generate Printer-friendly pages.
8 * This file is included by the core PF module, and includes all the
9 * functions necessary to generate a PF version of the original page
15 $_print_urls = PRINT_URLS_DEFAULT
;
18 * Generate an HTML version of the printer-friendly page
20 * @see print_controller()
22 function print_controller_html() {
23 $args = func_get_args();
24 $path = implode('/', $args);
25 $cid = isset($_GET['comment']) ?
(int)$_GET['comment'] : NULL
;
27 $print = print_controller($path, $cid, PRINT_HTML_FORMAT
);
28 if ($print !== FALSE
) {
29 $node = $print['node'];
30 $html = theme('print_page', $print, PRINT_HTML_FORMAT
, $node);
33 $nodepath = (isset($node->path
)) ?
drupal_get_normal_path($node->path
) : 'node/' .
$path;
34 db_merge('print_page_counter')
35 ->key(array('path' => $nodepath))
38 'timestamp' => REQUEST_TIME
,
40 ->expression('totalcount', 'totalcount + :inc', array(':inc' => 1))
46 * Select the print generator function based on the page type
48 * Depending on the type of node, this functions chooses the appropriate
52 * path of the original page
54 * comment ID of the individual comment to be rendered
56 * format of the page being generated
58 * if set to TRUE, outputs only the node's teaser
60 * optional sender's message (used by the send e-mail module)
62 * array with the fields to be used in the template
63 * @see _print_generate_node()
64 * @see _print_generate_path()
65 * @see _print_generate_book()
67 function print_controller($path, $cid = NULL
, $format = PRINT_HTML_FORMAT
, $teaser = FALSE
, $message = NULL
) {
69 // If no path was provided, let's try to generate a page for the referer
72 $ref = $_SERVER['HTTP_REFERER'];
73 $path = preg_replace("!^$base_url/!", '', $ref);
78 if (!is_numeric($path)) {
79 // Indirect call with print/alias
80 // If there is a path alias with these arguments, generate a printer-friendly version for it
81 $path = drupal_get_normal_path($path);
82 $ret = preg_match('!^node/(.*)!i', $path, $matches);
87 $parts = explode('/', $path);
88 if (is_numeric($parts[0]) && (count($parts) == 1)) {
89 $print = _print_generate_node($path, $cid, $format, $teaser, $message);
92 $ret = preg_match('!^book/export/html/(.*)!i', $path, $matches);
94 // This is a book PF page link, handle trough the book handling functions
95 $print = _print_generate_book($matches[1], $format, $teaser, $message);
98 // If no content node was found, handle the page printing with the 'printable' engine
99 $print = _print_generate_path($path, $format, $teaser, $message);
107 * Generates a robots meta tag to tell them what they may index
110 * string with the meta robots tag
112 function _print_robots_meta_generator() {
113 $print_robots_noindex = variable_get('print_robots_noindex', PRINT_ROBOTS_NOINDEX_DEFAULT
);
114 $print_robots_nofollow = variable_get('print_robots_nofollow', PRINT_ROBOTS_NOFOLLOW_DEFAULT
);
115 $print_robots_noarchive = variable_get('print_robots_noarchive', PRINT_ROBOTS_NOARCHIVE_DEFAULT
);
116 $robots_meta = array();
118 if (!empty($print_robots_noindex)) {
119 $robots_meta[] = 'noindex';
121 if (!empty($print_robots_nofollow)) {
122 $robots_meta[] = 'nofollow';
124 if (!empty($print_robots_noarchive)) {
125 $robots_meta[] = 'noarchive';
128 if (count($robots_meta) > 0) {
129 $robots_meta = implode(', ', $robots_meta);
130 $robots_meta = "<meta name='robots' content='$robots_meta' />\n";
140 * Post-processor that fills the array for the template with common details
143 * generated node with a printer-friendly node body
145 * optional sender's message (used by the send e-mail module)
147 * id of current comment being generated (NULL when not generating
148 * an individual comment)
150 * array with the fields to be used in the template
152 function _print_var_generator($node, $message = NULL
, $cid = NULL
) {
153 global $base_url, $language, $_print_urls;
155 $path = empty($node->nid
) ?
$node->path
: "node/$node->nid";
157 // print module settings
158 $print_css = variable_get('print_css', PRINT_CSS_DEFAULT
);
159 $print_logo_options = variable_get('print_logo_options', PRINT_LOGO_OPTIONS_DEFAULT
);
160 $print_logo_url = variable_get('print_logo_url', PRINT_LOGO_URL_DEFAULT
);
161 $print_html_new_window = variable_get('print_html_new_window', PRINT_HTML_NEW_WINDOW_DEFAULT
);
162 $print_html_sendtoprinter = variable_get('print_html_sendtoprinter', PRINT_HTML_SENDTOPRINTER_DEFAULT
);
163 $print_html_windowclose = variable_get('print_html_windowclose', PRINT_HTML_WINDOWCLOSE_DEFAULT
);
164 $print_sourceurl_enabled = variable_get('print_sourceurl_enabled', PRINT_SOURCEURL_ENABLED_DEFAULT
);
165 $print_sourceurl_forcenode = variable_get('print_sourceurl_forcenode', PRINT_SOURCEURL_FORCENODE_DEFAULT
);
166 $print_sourceurl_date = variable_get('print_sourceurl_date', PRINT_SOURCEURL_DATE_DEFAULT
);
167 $print_footer_options = variable_get('print_footer_options', PRINT_FOOTER_OPTIONS_DEFAULT
);
168 $print_footer_user = variable_get('print_footer_user', PRINT_FOOTER_USER_DEFAULT
);
170 $print['language'] = $language->language
;
171 $print['title'] = check_plain($node->title
);
172 $print['head'] = drupal_get_html_head();
173 if ($print_html_sendtoprinter) {
174 drupal_add_js('misc/drupal.js', array('weight' => JS_LIBRARY
));
176 $print['scripts'] = drupal_get_js();
177 $print['robots_meta'] = _print_robots_meta_generator();
178 $print['url'] = url($path, array('absolute' => TRUE
));
179 $print['base_href'] = "<base href='" .
$print['url'] .
"' />\n";
180 $print['favicon'] = theme_get_setting('toggle_favicon') ?
"<link rel='shortcut icon' href='" .
theme_get_setting('favicon') .
"' type='image/x-icon' />\n" : '';
182 if (!empty($print_css)) {
183 $replace_pairs = array('%b' => base_path(), '%t' => path_to_theme());
184 $user_css = check_url(strtr($print_css, $replace_pairs));
187 drupal_add_css(drupal_get_path('module', 'print') .
'/css/print.css');
189 $drupal_css = drupal_add_css();
190 foreach ($drupal_css as
$key => $types) {
191 // Unset the theme's CSS
192 $drupal_css[$key]['theme'] = array();
195 // If we are sending a message via e-mail, the CSS must be embedded
196 if (!empty($message)) {
198 $css_files = array();
199 foreach ($drupal_css as
$types) {
200 foreach ($types as
$values) {
201 $css_files = array_merge($css_files, array_keys($values));
204 if (!empty($print_css)) {
205 // Convert to a local path, by removing the base_path
206 $pattern = '!^' .
base_path() .
'!';
207 $css_files[] = preg_replace($pattern, '', $user_css);
209 foreach ($css_files as
$filename) {
210 $res = file_exists($filename) ?
file_get_contents($filename, TRUE
) : FALSE
;
215 $print['css'] = "<style type='text/css' media='all'>$style</style>\n";
218 $print['css'] = drupal_get_css($drupal_css);
219 if (!empty($print_css)) {
220 $query_string = '?' .
substr(variable_get('css_js_query_string', '0'), 0, 1);
221 $print['css'] .
= "<link type='text/css' rel='stylesheet' media='all' href='${user_css}${query_string}' />\n";
225 $window_close = ($print_html_new_window && $print_html_windowclose) ?
'window.close();' : '';
226 $print['sendtoprinter'] = $print_html_sendtoprinter ?
"<script type='text/javascript'>Drupal.behaviors.print = function(context) {window.print();$window_close}</script>" : '';
228 switch ($print_logo_options) {
233 $logo_url = theme_get_setting('logo');
235 case
2: // user-specifed
236 $logo_url = strip_tags($print_logo_url);
239 $print['logo'] = $logo_url ?
"<img class='print-logo' src='" .
check_url($logo_url) .
"' alt='' />\n" : '';
241 switch ($print_footer_options) {
246 $footer = variable_get('site_footer', FALSE
) .
"\n" .
theme('blocks', 'footer');
248 case
2: // user-specifed
249 $footer = $print_footer_user;
252 $print['footer_message'] = filter_xss_admin($footer);
254 $published_site = variable_get('site_name', 0);
255 if ($published_site) {
256 $print_text_published = filter_xss(variable_get('print_text_published', t('Published on %site_name')));
257 $published = t($print_text_published, array('%site_name' => $published_site));
258 $print['site_name'] = $published .
' (' .
l($base_url, $base_url) .
')';
261 $print['site_name'] = '';
264 if ($print_sourceurl_enabled == 1) {
265 /* Grab and format the src URL */
266 if (empty($print_sourceurl_forcenode)) {
267 $url = $print['url'];
270 $url = $base_url .
'/' .
(((bool
)variable_get('clean_url', '0')) ?
'' : '?q=') .
$path;
273 $url .
= '#comment-$cid';
275 $retrieved_date = format_date(REQUEST_TIME
, 'small');
276 $print_text_retrieved = filter_xss(variable_get('print_text_retrieved', t('retrieved on %date')));
277 $retrieved = t($print_text_retrieved, array('%date' => $retrieved_date));
278 $print['printdate'] = $print_sourceurl_date ?
" ($retrieved)" : '';
280 $source_url = filter_xss(variable_get('print_text_source_url', t('Source URL')));
281 $print['source_url'] = '<strong>' .
$source_url .
$print['printdate'] .
':</strong> ' .
l($url, $url);
284 $print['source_url'] = '';
287 if (isset($node->type
)) {
288 $node_type = $node->type
;
290 if (theme_get_setting("toggle_node_info_$node_type")) {
291 $print_text_by = filter_xss(variable_get('print_text_by', t('By %author')));
292 $by_author = ($node->name ?
$node->name
: variable_get('anonymous', t('Anonymous')));
293 $print['submitted'] = t($print_text_by, array('%author' => $by_author));
295 $print_text_created = filter_xss(variable_get('print_text_created', t('Created %date')));
296 $created_datetime = format_date($node->created
, 'small');
297 $print['created'] = t($print_text_created, array('%date' => $created_datetime));
300 $print['submitted'] = '';
301 $print['created'] = '';
304 $print['type'] = $node->type
;
307 $print['submitted'] = '';
308 $print['created'] = '';
312 menu_set_active_item($path);
313 $breadcrumb = drupal_get_breadcrumb();
314 if (!empty($breadcrumb)) {
315 $breadcrumb[] = menu_get_active_title();
316 $print['breadcrumb'] = filter_xss(implode(' > ', $breadcrumb));
319 $print['breadcrumb'] = '';
322 // Display the collected links at the bottom of the page. Code once taken from Kjartan Mannes' project.module
323 $print['pfp_links'] = '';
324 if (!empty($_print_urls)) {
325 $urls = _print_friendly_urls();
329 for ($i = 0; $i < $max; $i++) {
330 $pfp_links .
= '[' .
($i + 1) .
'] ' .
check_plain($urls[$i]) .
"<br />\n";
332 $links = filter_xss(variable_get('print_text_links', t('Links')));
333 $print['pfp_links'] = "<p><strong>$links:</strong><br />$pfp_links</p>";
337 if (module_exists('taxonomy')) {
338 // TODO make this work when taxonomy works again
339 // $terms = taxonomy_link('taxonomy terms', $node);
340 $terms = taxonomy_preview_terms($node);
341 $print['taxonomy'] = theme('links', $terms);
344 $print['node'] = $node;
345 $print['message'] = $message;
351 * Callback function for the preg_replace_callback for URL-capable patterns
353 * Manipulate URLs to make them absolute in the URLs list, and to add a
354 * [n] footnote marker.
357 * array with the matched tag patterns, usually <a...>+text+</a>
359 * tag with re-written URL and when appropriate the [n] index to the
362 function _print_rewrite_urls($matches) {
363 global $base_url, $base_root, $_print_urls;
365 $include_anchors = variable_get('print_urls_anchors', PRINT_URLS_ANCHORS_DEFAULT
);
367 // first, split the html into the different tag attributes
368 $pattern = '!\s*(\w+\s*=\s*"(?:\\\"|[^"])*")\s*|\s*(\w+\s*=\s*\'(?:\\\\\'|[^\'])*\')\s*|\s*(\w+\s*=\s*\w+)\s*|\s+!';
369 $attribs = preg_split($pattern, $matches[1], -1, PREG_SPLIT_NO_EMPTY
| PREG_SPLIT_DELIM_CAPTURE
);
370 foreach ($attribs as
$key => $value) {
371 $attribs[$key] = preg_replace('!(\w)\s*=\s*(.*)!', '$1=$2', $value);
374 $size = count($attribs);
375 for ($i=1; $i < $size; $i++) {
376 // If the attribute is href or src, we may need to rewrite the URL in the value
377 if (preg_match('!^(?:href|src)\s*?=(.*)!i', $attribs[$i], $urls) > 0) {
378 $url = trim($urls[1], " \t\n\r\0\x0B\"'");
380 if (strpos(html_entity_decode($url), '://') || preg_match('!^mailto:.*?@.*?\..*?$!iu', html_entity_decode($url))) {
381 // URL is absolute, do nothing
385 if ($url[0] == '#') {
386 // URL is an anchor tag
387 if ($include_anchors && (!empty($_print_urls))) {
388 $path = explode('/', $_GET['q']);
390 $path = implode('/', $path);
391 if (is_numeric($path)) {
392 $path = "node/$path";
394 // Printer-friendly URLs is on, so we need to make it absolute
395 $newurl = url($path, array('fragment' => substr($url, 1), 'absolute' => TRUE
));
397 // Because base href is the original page, change the link to
398 // still be usable inside the print page
399 $matches[1] = str_replace($url, base_path() .
$_GET['q'] .
$url, $matches[1]);
402 // URL is relative, convert it into absolute URL
403 if ($url[0] == '/') {
404 // If it starts with '/' just append it to the server name
405 $newurl = $base_root .
'/' .
trim($url, '/');
407 elseif (preg_match('!^(?:index.php)?\?q=!i', $url)) {
408 // If it starts with ?q=, just prepend with the base URL
409 $newurl = $base_url .
'/' .
trim($url, '/');
412 $newurl = url(trim($url, '/'), array('absolute' => TRUE
));
414 $matches[1] = str_replace($url, $newurl, $matches[1]);
420 $ret = '<' .
$matches[1] .
'>';
421 if (count($matches) == 4) {
422 $ret .
= $matches[2] .
$matches[3];
423 if ((!empty($_print_urls)) && (isset($newurl))) {
424 $ret .
= ' <span class="print-footnote">[' .
_print_friendly_urls(trim($newurl)) .
']</span>';
432 * Auxiliary function to store the Printer-friendly URLs list as static.
435 * absolute URL to be inserted in the list
437 * list of URLs previously stored if $url is 0, or the current count
440 function _print_friendly_urls($url = 0) {
441 static
$urls = array();
443 $url_idx = array_search($url, $urls);
444 if ($url_idx !== FALSE
) {
445 return ($url_idx + 1);
458 * Check URL list settings for this node
463 * format of the page being generated
465 * TRUE if URL list should be displayed, FALSE otherwise
467 function _print_url_list_enabled($node, $format = PRINT_HTML_FORMAT
) {
469 case PRINT_HTML_FORMAT
:
470 $node_urllist = isset($node->print_display_urllist
) ?
$node->print_display_urllist
: TRUE
;
473 case PRINT_MAIL_FORMAT
:
474 $node_urllist = isset($node->print_mail_display_urllist
) ?
$node->print_mail_display_urllist
: TRUE
;
475 $fmt = $format .
'_';
477 case PRINT_PDF_FORMAT
:
478 $node_urllist = isset($node->print_pdf_display_urllist
) ?
$node->print_pdf_display_urllist
: TRUE
;
479 $fmt = $format .
'_';
482 if (!isset($node_urllist)) $node_urllist = TRUE
;
484 // Get value of Printer-friendly URLs setting
485 return (variable_get('print_urls', PRINT_URLS_DEFAULT
) && ($node_urllist) &&
486 (isset($node->type
) ?
variable_get('print_' .
$fmt .
'display_urllist_' .
$node->type
, PRINT_TYPE_URLLIST_DEFAULT
) : PRINT_TYPE_URLLIST_DEFAULT
));
490 * Prepare a Printer-friendly-ready node body for content nodes
493 * node ID of the node to be rendered into a printer-friendly page
495 * comment ID of the individual comment to be rendered
497 * format of the page being generated
499 * if set to TRUE, outputs only the node's teaser
501 * optional sender's message (used by the send e-mail module)
503 * filled array ready to be used in the template
505 function _print_generate_node($nid, $cid = NULL
, $format = PRINT_HTML_FORMAT
, $teaser = FALSE
, $message = NULL
) {
508 //var_dump($language);
510 // We can take a node id
511 $node = node_load($nid);
517 elseif (!node_access('view', $node)) {
519 drupal_access_denied();
522 $view_mode = $teaser ?
'teaser' : 'full';
523 drupal_set_title($node->title
);
525 // Turn off Pagination by the Paging module
527 unset($node->page_count
);
529 // Make this page a member of the original page's organic group
530 if (function_exists('og_set_group_context') && isset($node->og_groups
)) {
531 og_set_group_context($node->og_groups
);
535 // Adapted (simplified) version of node_view
536 //Render the node content
537 node_build_content($node, $view_mode);
539 // Disable the links area
540 unset($node->content
['links']);
541 // Disable fivestar widget output
542 unset($node->content
['fivestar_widget']);
543 // Disable service links module output
544 unset($node->content
['service_links']);
546 $build = $node->content
;
547 unset($node->content
);
549 // Disable the AdSense module ads
550 // $content = preg_replace('!<div class=[\'"]adsense[\'"].*?</div>!sim', '', $content);
553 $print_comments = variable_get('print_comments', PRINT_COMMENTS_DEFAULT
);
555 if (function_exists('comment_render') && (($cid != NULL
) || ($print_comments))) {
556 //Print only the requested comment (or if $cid is NULL, all of them)
557 $comments = comment_render($node, $cid);
559 //Remove the comment forms
560 $comments = preg_replace('!<form.*?id="comment-.*?">.*?</form>!sim', '', $comments);
561 //Remove the 'Post new comment' title
562 $comments = preg_replace('!<h2.*?>' .
t('Post new comment') .
'</h2>!', '', $comments);
563 //Remove the comment title hyperlink
564 $comments = preg_replace('!(<h3.*?>.*?)<a.*?>(.*?)</a>(.*?</h3>)!i', '$1$2$3', $comments);
565 //Remove the comment author link
566 $pattern = '!(<(?:span|div) class="submitted">.*?)<a.*?>(.*?)</a>(.*?</(?:span|div)>)!sim';
567 if (preg_match($pattern, $comments)) {
568 $comments = preg_replace($pattern , '$1$2$3', $comments);
570 //Remove the comment links
571 $comments = preg_replace('!\s*<ul class="links">.*?</ul>!sim', '', $comments);
573 // Single comment requested, output only the comment
576 $node->body .
= $comments;
580 '#theme' => 'print_node',
582 '#view_mode' => $view_mode,
585 drupal_alter('node_view', $build);
587 //Check URL list settings
588 $_print_urls = _print_url_list_enabled($node, $format);
590 // Convert the a href elements
591 $pattern = '!<(a\s[^>]*?)>(.*?)(</a>)!is';
592 $node->body
['und'][0]['value'] = preg_replace_callback($pattern, '_print_rewrite_urls', $node->body
['und'][0]['value']);
594 $print = _print_var_generator($node, $message, $cid);
595 $print['content'] = $node->body
['und'][0]['value'];
601 * Prepare a Printer-friendly-ready node body for non-content pages
604 * path of the node to be rendered into a printer-friendly page
606 * format of the page being generated
608 * if set to TRUE, outputs only the node's teaser
610 * optional sender's message (used by the send e-mail module)
612 * filled array ready to be used in the template
614 function _print_generate_path($path, $format = PRINT_HTML_FORMAT
, $teaser = FALSE
, $message = NULL
) {
618 $parts = explode('/', $path);
619 if (is_numeric($parts[0]) && (count($parts) > 1)) {
620 $path = 'node/' .
$path;
623 $path = drupal_get_normal_path($path);
625 menu_set_active_item($path);
626 // Adapted from index.php.
627 $node = new
stdClass();
628 $node->body
= menu_execute_active_handler($path);
630 // It may happen that a drupal_not_found is called in the above call
631 if (preg_match('/404 Not Found/', drupal_get_headers()) == 1) {
635 if (is_int($node->body
)) {
636 switch ($node->body
) {
641 case MENU_ACCESS_DENIED
:
642 drupal_access_denied();
648 $node->title
= drupal_get_title();
651 // Delete any links area
652 $node->body
= preg_replace('!\s*<div class="links">.*?</div>!sim', '', $node->body
);
654 //Check URL list settings
655 $_print_urls = _print_url_list_enabled($node, $format);
657 // Convert the a href elements
658 $pattern = '!<(a\s[^>]*?)>(.*?)(</a>)!is';
659 $node->body
= preg_replace_callback($pattern, '_print_rewrite_urls', $node->body
);
661 $print = _print_var_generator($node, $message);
662 $print['content'] = $node->body
;
669 * Prepare a Printer-friendly-ready node body for book pages
672 * node ID of the node to be rendered into a printer-friendly page
674 * format of the page being generated
676 * if set to TRUE, outputs only the node's teaser
678 * optional sender's message (used by the send e-mail module)
680 * filled array ready to be used in the template
682 function _print_generate_book($nid, $format = PRINT_HTML_FORMAT
, $teaser = FALSE
, $message = NULL
) {
685 $node = node_load($nid);
691 elseif (!node_access('view', $node) || (!user_access('access printer-friendly version'))) {
693 drupal_access_denied();
697 $tree = book_menu_subtree_data($node->book
);
698 $node->body
= book_export_traverse($tree, 'book_node_export');
700 //Check URL list settings
701 $_print_urls = _print_url_list_enabled($node, $format);
703 // Convert the a href elements
704 $pattern = '!<(a\s[^>]*?)>(.*?)(</a>)!is';
705 $node->body
= preg_replace_callback($pattern, '_print_rewrite_urls', $node->body
);
707 $print = _print_var_generator($node, $message);
708 $print['content'] = $node->body
;
710 // The title is already displayed by the book_recurse, so avoid duplication
711 $print['title'] = '';