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 = implode('/', $args);
24 $cid = isset($_GET['comment']) ?
(int)$_GET['comment'] : NULL
;
26 $print = print_controller($path, $cid, PRINT_HTML_FORMAT
);
27 if ($print !== FALSE
) {
28 $node = $print['node'];
30 $html = theme('print_page', $print, PRINT_HTML_FORMAT
, $node);
31 $html = drupal_final_markup($html);
34 $nodepath = (isset($node->path
)) ?
drupal_get_normal_path($node->path
) : 'node/'.
$path;
35 db_query("UPDATE {print_page_counter} SET totalcount = totalcount + 1, timestamp = %d WHERE path = '%s'", time(), $nodepath);
36 // If we affected 0 rows, this is the first time viewing the node.
37 if (!db_affected_rows()) {
38 // We must create a new row to store counters for the new node.
39 db_query("INSERT INTO {print_page_counter} (path, totalcount, timestamp) VALUES ('%s', 1, %d)", $nodepath, time());
45 * Select the print generator function based on the page type
47 * Depending on the type of node, this functions chooses the appropriate
51 * path of the original page
53 * comment ID of the individual comment to be rendered
55 * format of the page being generated
57 * if set to TRUE, outputs only the node's teaser
59 * optional sender's message (used by the send e-mail module)
61 * array with the fields to be used in the template
62 * @see _print_generate_node()
63 * @see _print_generate_path()
64 * @see _print_generate_book()
66 function print_controller($path, $cid = NULL
, $format = PRINT_HTML_FORMAT
, $teaser = FALSE
, $message = NULL
) {
68 // If no path was provided, let's try to generate a page for the referer
72 $path = preg_replace("!^$base_url/!", '', $ref);
77 if (!is_numeric($path)) {
78 // Indirect call with print/alias
79 // If there is a path alias with these arguments, generate a printer-friendly version for it
80 $path = drupal_get_normal_path($path);
81 $ret = preg_match('!^node/(.*)!i', $path, $matches);
86 $parts = explode('/', $path);
87 if (is_numeric($parts[0]) && (count($parts) == 1)) {
88 $print = _print_generate_node($path, $cid, $format, $teaser, $message);
91 $ret = preg_match('!^book/export/html/(.*)!i', $path, $matches);
93 // This is a book PF page link, handle trough the book handling functions
94 $print = _print_generate_book($matches[1], $format, $teaser, $message);
97 // If no content node was found, handle the page printing with the 'printable' engine
98 $print = _print_generate_path($path, $format, $teaser, $message);
106 * Generates a robots meta tag to tell them what they may index
109 * string with the meta robots tag
111 function _print_robots_meta_generator() {
112 $print_robots_noindex = variable_get('print_robots_noindex', PRINT_ROBOTS_NOINDEX_DEFAULT
);
113 $print_robots_nofollow = variable_get('print_robots_nofollow', PRINT_ROBOTS_NOFOLLOW_DEFAULT
);
114 $print_robots_noarchive = variable_get('print_robots_noarchive', PRINT_ROBOTS_NOARCHIVE_DEFAULT
);
115 $robots_meta = array();
117 if (!empty($print_robots_noindex)) {
118 $robots_meta[] = 'noindex';
120 if (!empty($print_robots_nofollow)) {
121 $robots_meta[] = 'nofollow';
123 if (!empty($print_robots_noarchive)) {
124 $robots_meta[] = 'noarchive';
127 if (count($robots_meta) > 0) {
128 $robots_meta = implode(', ', $robots_meta);
129 $robots_meta = "<meta name='robots' content='$robots_meta' />\n";
139 * Post-processor that fills the array for the template with common details
142 * generated node with a printer-friendly node body
144 * optional sender's message (used by the send e-mail module)
146 * id of current comment being generated (NULL when not generating
147 * an individual comment)
149 * array with the fields to be used in the template
151 function _print_var_generator($node, $message = NULL
, $cid = NULL
) {
152 global $base_url, $language, $_print_urls;
154 $path = empty($node->nid
) ?
$node->path
: "node/$node->nid";
156 // Needed because of the theme_get_setting() calls below
159 // print module settings
160 $print_css = variable_get('print_css', PRINT_CSS_DEFAULT
);
161 $print_logo_options = variable_get('print_logo_options', PRINT_LOGO_OPTIONS_DEFAULT
);
162 $print_logo_url = variable_get('print_logo_url', PRINT_LOGO_URL_DEFAULT
);
163 $print_html_new_window = variable_get('print_html_new_window', PRINT_HTML_NEW_WINDOW_DEFAULT
);
164 $print_html_sendtoprinter = variable_get('print_html_sendtoprinter', PRINT_HTML_SENDTOPRINTER_DEFAULT
);
165 $print_html_windowclose = variable_get('print_html_windowclose', PRINT_HTML_WINDOWCLOSE_DEFAULT
);
166 $print_sourceurl_enabled = variable_get('print_sourceurl_enabled', PRINT_SOURCEURL_ENABLED_DEFAULT
);
167 $print_sourceurl_forcenode = variable_get('print_sourceurl_forcenode', PRINT_SOURCEURL_FORCENODE_DEFAULT
);
168 $print_sourceurl_date = variable_get('print_sourceurl_date', PRINT_SOURCEURL_DATE_DEFAULT
);
169 $print_footer_options = variable_get('print_footer_options', PRINT_FOOTER_OPTIONS_DEFAULT
);
170 $print_footer_user = variable_get('print_footer_user', PRINT_FOOTER_USER_DEFAULT
);
172 $print['language'] = $language->language
;
173 $print['title'] = check_plain($node->title
);
174 $print['head'] = drupal_get_html_head();
175 if ($print_html_sendtoprinter) {
176 drupal_add_js('misc/drupal.js', 'core');
178 $print['scripts'] = drupal_get_js();
179 $print['robots_meta'] = _print_robots_meta_generator();
180 $print['url'] = url($path, array('absolute' => TRUE
));
181 $print['base_href'] = "<base href='".
$print['url'] .
"' />\n";
182 $print['favicon'] = theme_get_setting('toggle_favicon') ?
"<link rel='shortcut icon' href='".
theme_get_setting('favicon') .
"' type='image/x-icon' />\n" : '';
184 if (!empty($print_css)) {
185 drupal_add_css(strtr($print_css, array('%t' => path_to_theme())));
188 drupal_add_css(drupal_get_path('module', 'print') .
'/css/print.css');
190 $drupal_css = drupal_add_css();
191 foreach ($drupal_css as
$key => $types) {
192 // Unset the theme's CSS
193 $drupal_css[$key]['theme'] = array();
196 // If we are sending a message via e-mail, the CSS must be embedded
197 if (!empty($message)) {
199 $css_files = array();
200 foreach ($drupal_css as
$types) {
201 foreach ($types as
$values) {
202 $css_files = array_merge($css_files, array_keys($values));
205 foreach ($css_files as
$filename) {
206 $res = file_exists($filename) ?
file_get_contents($filename, TRUE
) : FALSE
;
211 $print['css'] = "<style type='text/css' media='all'>$style</style>\n";
214 $print['css'] = drupal_get_css($drupal_css);
217 $window_close = ($print_html_new_window && $print_html_windowclose) ?
'window.close();' : '';
218 $print['sendtoprinter'] = $print_html_sendtoprinter ?
"<script type='text/javascript'>Drupal.behaviors.print = function(context) {window.print();$window_close}</script>" : '';
220 switch ($print_logo_options) {
225 $logo_url = theme_get_setting('logo');
227 case
2: // user-specifed
228 $logo_url = strip_tags($print_logo_url);
231 $site_name = variable_get('site_name', 'Drupal');
232 $print['logo'] = $logo_url ?
"<img class='print-logo' src='".
check_url($logo_url) .
"' alt='$site_name' id='logo' />\n" : '';
234 switch ($print_footer_options) {
239 $footer = variable_get('site_footer', FALSE
) .
"\n".
theme('blocks', 'footer');
241 case
2: // user-specifed
242 $footer = $print_footer_user;
245 $print['footer_message'] = filter_xss_admin($footer);
247 $published_site = variable_get('site_name', 0);
248 if ($published_site) {
249 $print_text_published = filter_xss(variable_get('print_text_published', t('Published on %site_name')));
250 $published = t($print_text_published, array('%site_name' => $published_site));
251 $print['site_name'] = $published .
' ('.
l($base_url, $base_url) .
')';
254 $print['site_name'] = '';
257 if ($print_sourceurl_enabled == 1) {
258 /* Grab and format the src URL */
259 if (empty($print_sourceurl_forcenode)) {
260 $url = $print['url'];
263 $url = $base_url .
'/'.
(((bool
)variable_get('clean_url', '0')) ?
'' : '?q=') .
$path;
266 $url .
= '#comment-$cid';
268 $retrieved_date = format_date(time(), 'small');
269 $print_text_retrieved = filter_xss(variable_get('print_text_retrieved', t('retrieved on %date')));
270 $retrieved = t($print_text_retrieved, array('%date' => $retrieved_date));
271 $print['printdate'] = $print_sourceurl_date ?
" ($retrieved)" : '';
273 $source_url = filter_xss(variable_get('print_text_source_url', t('Source URL')));
274 $print['source_url'] = '<strong>'.
$source_url .
$print['printdate'] .
':</strong> '.
l($url, $url);
277 $print['source_url'] = '';
280 $print['type'] = (isset($node->type
)) ?
$node->type
: '';
282 menu_set_active_item($path);
283 $breadcrumb = drupal_get_breadcrumb();
284 if (!empty($breadcrumb)) {
285 $breadcrumb[] = menu_get_active_title();
286 $print['breadcrumb'] = filter_xss(implode(' > ', $breadcrumb));
289 $print['breadcrumb'] = '';
292 // Display the collected links at the bottom of the page. Code once taken from Kjartan Mannes' project.module
293 $print['pfp_links'] = '';
294 if (!empty($_print_urls)) {
295 $urls = _print_friendly_urls();
299 for ($i = 0; $i < $max; $i++) {
300 $pfp_links .
= '['.
($i + 1) .
'] '.
check_plain($urls[$i]) .
"<br />\n";
302 $links = filter_xss(variable_get('print_text_links', t('Links')));
303 $print['pfp_links'] = "<p><strong>$links:</strong><br />$pfp_links</p>";
307 $print['node'] = $node;
308 $print['message'] = $message;
314 * Callback function for the preg_replace_callback for URL-capable patterns
316 * Manipulate URLs to make them absolute in the URLs list, and to add a
317 * [n] footnote marker.
320 * array with the matched tag patterns, usually <a...>+text+</a>
322 * tag with re-written URL and when appropriate the [n] index to the
325 function _print_rewrite_urls($matches) {
326 global $base_url, $base_root, $_print_urls;
328 $include_anchors = variable_get('print_urls_anchors', PRINT_URLS_ANCHORS_DEFAULT
);
330 // first, split the html into the different tag attributes
331 $pattern = '!\s*(\w+\s*=\s*"(?:\\\"|[^"])*")\s*|\s*(\w+\s*=\s*\'(?:\\\\\'|[^\'])*\')\s*|\s*(\w+\s*=\s*\w+)\s*|\s+!';
332 $attribs = preg_split($pattern, $matches[1], -1, PREG_SPLIT_NO_EMPTY
| PREG_SPLIT_DELIM_CAPTURE
);
333 foreach ($attribs as
$key => $value) {
334 $attribs[$key] = preg_replace('!(\w)\s*=\s*(.*)!', '$1=$2', $value);
337 $size = count($attribs);
338 for ($i=1; $i < $size; $i++) {
339 // If the attribute is href or src, we may need to rewrite the URL in the value
340 if (preg_match('!^(?:href|src)\s*?=(.*)!i', $attribs[$i], $urls) > 0) {
341 $url = trim($urls[1], " \t\n\r\0\x0B\"'");
343 if (strpos(html_entity_decode($url), '://') || preg_match('!^mailto:.*?@.*?\..*?$!iu', html_entity_decode($url))) {
344 // URL is absolute, do nothing
348 if ($url[0] == '#') {
349 // URL is an anchor tag
350 if ($include_anchors && (!empty($_print_urls))) {
351 $path = explode('/', $_GET['q']);
353 $path = implode('/', $path);
354 if (is_numeric($path)) {
355 $path = "node/$path";
357 // Printer-friendly URLs is on, so we need to make it absolute
358 $newurl = url($path, array('fragment' => substr($url, 1), 'absolute' => TRUE
));
360 // Because base href is the original page, change the link to
361 // still be usable inside the print page
362 $matches[1] = str_replace($url, base_path() .
$_GET['q'] .
$url, $matches[1]);
365 // URL is relative, convert it into absolute URL
366 if ($url[0] == '/') {
367 // If it starts with '/' just append it to the server name
368 $newurl = $base_root .
'/'.
trim($url, '/');
370 elseif (preg_match('!^(?:index.php)?\?q=!i', $url)) {
371 // If it starts with ?q=, just prepend with the base URL
372 $newurl = $base_url .
'/'.
trim($url, '/');
375 $newurl = rawurldecode(url(trim($url, '/'), array('absolute' => TRUE
)));
377 $matches[1] = str_replace($url, $newurl, $matches[1]);
383 $ret = '<'.
$matches[1] .
'>';
384 if (count($matches) == 4) {
385 $ret .
= $matches[2] .
$matches[3];
386 if ((!empty($_print_urls)) && (isset($newurl))) {
387 $ret .
= ' <span class="print-footnote">['.
_print_friendly_urls(trim($newurl)) .
']</span>';
395 * Auxiliary function to store the Printer-friendly URLs list as static.
398 * absolute URL to be inserted in the list
400 * list of URLs previously stored if $url is 0, or the current count
403 function _print_friendly_urls($url = 0) {
404 static
$urls = array();
406 $url_idx = array_search($url, $urls);
407 if ($url_idx !== FALSE
) {
408 return ($url_idx + 1);
421 * Check URL list settings for this node
426 * format of the page being generated
428 * TRUE if URL list should be displayed, FALSE otherwise
430 function _print_url_list_enabled($node, $format = PRINT_HTML_FORMAT
) {
431 if (!isset($node->type
)) {
436 case PRINT_HTML_FORMAT
:
437 $node_urllist = isset($node->print_display_urllist
) ?
$node->print_display_urllist
: variable_get('print_display_urllist_'.
$node->type
, PRINT_TYPE_URLLIST_DEFAULT
);
440 case PRINT_MAIL_FORMAT
:
441 $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
);
444 case PRINT_PDF_FORMAT
:
445 $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
);
449 if (!isset($node_urllist)) $node_urllist = PRINT_TYPE_URLLIST_DEFAULT
;
451 // Get value of Printer-friendly URLs setting
452 return (variable_get('print_urls', PRINT_URLS_DEFAULT
) && ($node_urllist));
456 * Prepare a Printer-friendly-ready node body for content nodes
459 * node ID of the node to be rendered into a printer-friendly page
461 * comment ID of the individual comment to be rendered
463 * format of the page being generated
465 * if set to TRUE, outputs only the node's teaser
467 * optional sender's message (used by the send e-mail module)
469 * filled array ready to be used in the template
471 function _print_generate_node($nid, $cid = NULL
, $format = PRINT_HTML_FORMAT
, $teaser = FALSE
, $message = NULL
) {
474 //use the proper build_mode
475 $node->build_mode
= NODE_BUILD_PRINT
;
477 // We can take a node id
478 $node = node_load($nid);
484 elseif (!node_access('view', $node)) {
486 drupal_access_denied();
489 drupal_set_title(check_plain($node->title
));
491 //alert other modules that we are generating a printer-friendly page, so they can choose to show/hide info
492 $node->printing
= TRUE
;
494 // Turn off Pagination by the Paging module
496 unset($node->page_count
);
498 // Make this page a member of the original page's organic group
499 if (function_exists('og_set_group_context') && isset($node->og_groups
)) {
500 og_set_group_context($node->og_groups
);
504 // Adapted (simplified) version of node_view
505 //Render the node content
506 $node = node_build_content($node, $teaser, TRUE
);
507 // Disable fivestar widget output
508 unset($node->content
['fivestar_widget']);
509 // Disable service links module output
510 unset($node->content
['service_links']);
512 $content = drupal_render($node->content
);
514 // Disable the AdSense module ads
515 $content = preg_replace('!<div class=[\'"]adsense[\'"].*?</div>!sim', '', $content);
518 $node->teaser
= $content;
522 $node->body
= $content;
523 unset($node->teaser
);
527 $print_comments = variable_get('print_comments', PRINT_COMMENTS_DEFAULT
);
529 if (function_exists('comment_render') && (($cid != NULL
) || ($print_comments))) {
530 //Print only the requested comment (or if $cid is NULL, all of them)
531 $comments = comment_render($node, $cid);
533 //Remove the comment forms
534 $comments = preg_replace('!<form.*?id="comment-.*?">.*?</form>!sim', '', $comments);
535 //Remove the 'Post new comment' title
536 $comments = preg_replace('!<h2.*?>'.
t('Post new comment') .
'</h2>!', '', $comments);
537 //Remove the comment title hyperlink
538 $comments = preg_replace('!(<h3.*?>.*?)<a.*?>(.*?)</a>(.*?</h3>)!i', '$1$2$3', $comments);
539 //Remove the comment author link
540 $pattern = '!(<(?:span|div) class="submitted">.*?)<a.*?>(.*?)</a>(.*?</(?:span|div)>)!sim';
541 if (preg_match($pattern, $comments)) {
542 $comments = preg_replace($pattern , '$1$2$3', $comments);
544 //Remove the comment links
545 $comments = preg_replace('!\s*<ul class="links">.*?</ul>!sim', '', $comments);
547 // Single comment requested, output only the comment
550 $node->body .
= $comments;
553 node_invoke_nodeapi($node, 'alter', $teaser, TRUE
);
555 $content = theme('print_node', $node, $teaser, TRUE
, $format);
558 $content = $node->teaser
;
561 //Check URL list settings
562 $_print_urls = _print_url_list_enabled($node, $format);
564 // Convert the a href elements
565 $pattern = '!<(a\s[^>]*?)>(.*?)(</a>)!is';
566 $content = preg_replace_callback($pattern, '_print_rewrite_urls', $content);
568 $print = _print_var_generator($node, $message, $cid);
569 $print['content'] = $content;
575 * Prepare a Printer-friendly-ready node body for non-content pages
578 * path of the node to be rendered into a printer-friendly page
580 * format of the page being generated
582 * if set to TRUE, outputs only the node's teaser
584 * optional sender's message (used by the send e-mail module)
586 * filled array ready to be used in the template
588 function _print_generate_path($path, $format = PRINT_HTML_FORMAT
, $teaser = FALSE
, $message = NULL
) {
592 $parts = explode('/', $path);
593 if (is_numeric($parts[0]) && (count($parts) > 1)) {
594 $path = 'node/'.
$path;
597 $path = drupal_get_normal_path($path);
599 menu_set_active_item($path);
600 // Adapted from index.php.
601 $node = new
stdClass();
602 $node->body
= menu_execute_active_handler($path);
604 // It may happen that a drupal_not_found is called in the above call
605 if (preg_match('/404 Not Found/', drupal_get_headers()) == 1) {
609 if (is_int($node->body
)) {
610 switch ($node->body
) {
615 case MENU_ACCESS_DENIED
:
616 drupal_access_denied();
622 $node->title
= drupal_get_title();
625 // Delete any links area
626 $node->body
= preg_replace('!\s*<div class="links">.*?</div>!sim', '', $node->body
);
628 //Check URL list settings
629 $_print_urls = _print_url_list_enabled($node, $format);
631 // Convert the a href elements
632 $pattern = '!<(a\s[^>]*?)>(.*?)(</a>)!is';
633 $node->body
= preg_replace_callback($pattern, '_print_rewrite_urls', $node->body
);
635 $print = _print_var_generator($node, $message);
636 $print['content'] = $node->body
;
643 * Prepare a Printer-friendly-ready node body for book pages
646 * node ID of the node to be rendered into a printer-friendly page
648 * format of the page being generated
650 * if set to TRUE, outputs only the node's teaser
652 * optional sender's message (used by the send e-mail module)
654 * filled array ready to be used in the template
656 function _print_generate_book($nid, $format = PRINT_HTML_FORMAT
, $teaser = FALSE
, $message = NULL
) {
659 $node = node_load($nid);
665 elseif (!node_access('view', $node) || (!user_access('access printer-friendly version'))) {
667 drupal_access_denied();
671 $tree = book_menu_subtree_data($node->book
);
672 $node->body
= book_export_traverse($tree, 'book_node_export');
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, $message);
682 $print['content'] = $node->body
;
684 // The title is already displayed by the book_recurse, so avoid duplication
685 $print['title'] = '';