Issue #1940732: apply filters to the in-page anchor tags.
[project/print.git] / print.pages.inc
1 <?php
2
3 /**
4 * @file
5 * Contains the functions to generate Printer-friendly pages.
6 *
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
9 * in HTML format.
10 *
11 * @ingroup print
12 */
13
14 $_print_urls = PRINT_URLS_DEFAULT;
15
16 /**
17 * Generate an HTML version of the printer-friendly page
18 *
19 * @see print_controller()
20 */
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;
25
26 // Handle the query
27 $query = $_GET;
28 unset($query['q']);
29
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();
36 print $html;
37
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))
41 ->fields(array(
42 'totalcount' => 1,
43 'timestamp' => REQUEST_TIME,
44 ))
45 ->expression('totalcount', 'totalcount + 1')
46 ->execute();
47 }
48 }
49
50 /**
51 * Select the print generator function based on the page type
52 *
53 * Depending on the type of node, this functions chooses the appropriate
54 * generator function.
55 *
56 * @param $path
57 * path of the original page
58 * @param array $query
59 * (optional) array of key/value pairs as used in the url() function for the
60 * query
61 * @param $cid
62 * comment ID of the individual comment to be rendered
63 * @param $format
64 * format of the page being generated
65 * @param $teaser
66 * if set to TRUE, outputs only the node's teaser
67 * @param $message
68 * optional sender's message (used by the send email module)
69 * @return
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()
74 */
75 function print_controller($path, $query = NULL, $cid = NULL, $format = PRINT_HTML_FORMAT, $teaser = FALSE, $message = NULL) {
76 if (empty($path)) {
77 // If no path was provided, let's try to generate a page for the referer
78 global $base_url;
79
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');
84 }
85 }
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
89 $path = $alias;
90 }
91 $parts = explode('/', $path);
92 if (($parts[0] == 'node') && (count($parts) > 1) && ctype_digit($parts[1])) {
93 array_shift($parts);
94 $path = implode('/', $parts);
95 }
96 if (ctype_digit($parts[0]) && (count($parts) == 1)) {
97 $print = _print_generate_node($path, $query, $cid, $format, $teaser, $message);
98 }
99 else {
100 $ret = preg_match('!^book/export/html/(.*)!i', $path, $matches);
101 if ($ret == 1) {
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);
104 }
105 else {
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);
108 }
109 }
110
111 return $print;
112 }
113
114 /**
115 * Generates a robots meta tag to tell them what they may index
116 *
117 * @return
118 * string with the meta robots tag
119 */
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();
125
126 if (!empty($print_robots_noindex)) {
127 $robots_meta[] = 'noindex';
128 }
129 if (!empty($print_robots_nofollow)) {
130 $robots_meta[] = 'nofollow';
131 }
132 if (!empty($print_robots_noarchive)) {
133 $robots_meta[] = 'noarchive';
134 }
135
136 if (count($robots_meta) > 0) {
137 $robots_meta = implode(', ', $robots_meta);
138 $robots_meta = "<meta name='robots' content='$robots_meta' />\n";
139 }
140 else {
141 $robots_meta = '';
142 }
143
144 return $robots_meta;
145 }
146
147 /**
148 * Post-processor that fills the array for the template with common details
149 *
150 * @param $node
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
154 * query
155 * @param $message
156 * optional sender's message (used by the send email module)
157 * @param $cid
158 * id of current comment being generated (NULL when not generating
159 * an individual comment)
160 * @return
161 * array with the fields to be used in the template
162 */
163 function _print_var_generator($node, $query = NULL, $message = NULL, $cid = NULL) {
164 global $base_url, $language, $_print_urls;
165
166 $path = empty($node->nid) ? $node->path : "node/$node->nid";
167
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);
181
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));
187 }
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" : '';
194
195 if (!empty($print_css)) {
196 drupal_add_css(strtr($print_css, array('%t' => drupal_get_path('theme', variable_get('theme_default')))));
197 }
198 else {
199 drupal_add_css(drupal_get_path('module', 'print') . '/css/print.css');
200 }
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]);
207 }
208 }
209 }
210
211 // If we are sending a message via email, the CSS must be embedded
212 if (!empty($message)) {
213 $style = '';
214 $css_files = array_keys($drupal_css);
215 foreach ($css_files as $filename) {
216 $res = file_exists($filename) ? file_get_contents($filename, TRUE) : FALSE;
217 if ($res != FALSE) {
218 $style .= $res;
219 }
220 }
221 $print['css'] = "<style type='text/css' media='all'>$style</style>\n";
222 }
223 else {
224 $print['css'] = drupal_get_css($drupal_css);
225 }
226
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>' : '';
229
230 switch ($print_logo_options) {
231 case 0: // none
232 $logo_url = 0;
233 break;
234 case 1: // theme's
235 $logo_url = theme_get_setting('logo');
236 break;
237 case 2: // user-specifed
238 $logo_url = strip_tags($print_logo_url);
239 break;
240 }
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'))) : '';
244
245 switch ($print_footer_options) {
246 case 0: // none
247 $footer = '';
248 break;
249 case 1: // theme's
250 $footer_blocks = block_get_blocks_by_region('footer');
251 $footer = variable_get('site_footer', FALSE) . "\n" . drupal_render($footer_blocks);
252 break;
253 case 2: // user-specifed
254 $footer = $print_footer_user;
255 break;
256 }
257 // Delete the contextual links
258 $footer = preg_replace('!\s*<div class="contextual-links-wrapper">.*?</div>!sim', '', $footer);
259
260 $print['footer_message'] = filter_xss_admin($footer);
261
262 $published_site = variable_get('site_name', 0);
263 if ($published_site) {
264 $print_text_published = filter_xss(variable_get('print_text_published', t('Published on %site_name')));
265 $published = t($print_text_published, array('%site_name' => $published_site));
266 $print['site_name'] = $published . ' (' . l($base_url, $base_url) . ')';
267 }
268 else {
269 $print['site_name'] = '';
270 }
271
272 if ($print_sourceurl_enabled == 1) {
273 /* Grab and format the src URL */
274 if (empty($print_sourceurl_forcenode)) {
275 $url = $print['url'];
276 }
277 else {
278 $url = $base_url . '/' . (((bool)variable_get('clean_url', '0')) ? '' : '?q=') . $path;
279 }
280 if (is_int($cid)) {
281 $url .= "#comment-$cid";
282 }
283 $retrieved_date = format_date(REQUEST_TIME, 'short');
284 $print_text_retrieved = filter_xss(variable_get('print_text_retrieved', t('retrieved on %date')));
285 $retrieved = t($print_text_retrieved, array('%date' => $retrieved_date));
286 $print['printdate'] = $print_sourceurl_date ? " ($retrieved)" : '';
287
288 $source_url = filter_xss(variable_get('print_text_source_url', t('Source URL')));
289 $print['source_url'] = '<strong>' . $source_url . $print['printdate'] . ':</strong> ' . l($url, $url);
290 }
291 else {
292 $print['source_url'] = '';
293 }
294
295 $print['type'] = (isset($node->type)) ? $node->type : '';
296
297 menu_set_active_item($path);
298 $breadcrumb = drupal_get_breadcrumb();
299 if (!empty($breadcrumb)) {
300 $breadcrumb[] = menu_get_active_title();
301 $print['breadcrumb'] = filter_xss(implode(' > ', $breadcrumb));
302 }
303 else {
304 $print['breadcrumb'] = '';
305 }
306
307 // Display the collected links at the bottom of the page. Code once taken from Kjartan Mannes' project.module
308 $print['pfp_links'] = '';
309 if (!empty($_print_urls)) {
310 $urls = _print_friendly_urls();
311 $max = count($urls);
312 $pfp_links = '';
313 if ($max) {
314 for ($i = 0; $i < $max; $i++) {
315 $pfp_links .= '[' . ($i + 1) . '] ' . check_plain($urls[$i]) . "<br />\n";
316 }
317 $links = filter_xss(variable_get('print_text_links', t('Links')));
318 $print['pfp_links'] = "<p><strong>$links:</strong><br />$pfp_links</p>";
319 }
320 }
321
322 $print['node'] = $node;
323 $print['message'] = $message;
324
325 return $print;
326 }
327
328 /**
329 * Callback function for the preg_replace_callback for URL-capable patterns
330 *
331 * Manipulate URLs to make them absolute in the URLs list, and to add a
332 * [n] footnote marker.
333 *
334 * @param $matches
335 * array with the matched tag patterns, usually <a...>+text+</a>
336 * @return
337 * tag with re-written URL and when appropriate the [n] index to the
338 * URL list
339 */
340 function _print_rewrite_urls($matches) {
341 global $base_url, $base_root, $_print_urls;
342
343 $include_anchors = variable_get('print_urls_anchors', PRINT_URLS_ANCHORS_DEFAULT);
344
345 // first, split the html into the different tag attributes
346 $pattern = '!\s*(\w+\s*=\s*"(?:\\\"|[^"])*")\s*|\s*(\w+\s*=\s*\'(?:\\\\\'|[^\'])*\')\s*|\s*(\w+\s*=\s*\w+)\s*|\s+!';
347 $attribs = preg_split($pattern, $matches[1], -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
348 foreach ($attribs as $key => $value) {
349 $attribs[$key] = preg_replace('!(\w)\s*=\s*(.*)!', '$1=$2', $value);
350 }
351
352 $size = count($attribs);
353 for ($i=1; $i < $size; $i++) {
354 // If the attribute is href or src, we may need to rewrite the URL in the value
355 if (preg_match('!^(?:href|src)\s*?=(.*)!i', $attribs[$i], $urls) > 0) {
356 $url = trim($urls[1], " \t\n\r\0\x0B\"'");
357
358 if (empty($url)) {
359 // If URL is empty, use current_url
360 $path = explode('/', $_GET['q']);
361 unset($path[0]);
362 $path = implode('/', $path);
363 if (ctype_digit($path)) {
364 $path = "node/$path";
365 }
366 // Printer-friendly URLs is on, so we need to make it absolute
367 $newurl = url($path, array('fragment' => drupal_substr($url, 1), 'absolute' => TRUE));
368 }
369 elseif (strpos(html_entity_decode($url), '://') || preg_match('!^mailto:.*?@.*?\..*?$!iu', html_entity_decode($url))) {
370 // URL is absolute, do nothing
371 $newurl = $url;
372 }
373 elseif (strpos(html_entity_decode($url), '//') === 0) {
374 // URL is 'almost absolute', but it does not contain protocol; replace with base_path protocol
375 $newurl = (empty($_SERVER['HTTPS']) ? 'http' : 'https') . ":" . $url;
376 $matches[1] = str_replace($url, $newurl, $matches[1]);
377 }
378 else {
379 if ($url[0] == '#') {
380 // URL is an anchor tag
381 if ($include_anchors && (!empty($_print_urls))) {
382 $path = explode('/', $_GET['q']);
383 unset($path[0]);
384 $path = implode('/', $path);
385 if (ctype_digit($path)) {
386 $path = "node/$path";
387 }
388 // Printer-friendly URLs is on, so we need to make it absolute
389 $newurl = url($path, array('fragment' => drupal_substr($url, 1), 'absolute' => TRUE));
390 }
391 // Because base href is the original page, change the link to
392 // still be usable inside the print page
393 $matches[1] = str_replace($url, check_plain(base_path() . $_GET['q'] . $url), $matches[1]);
394 }
395 else {
396 // URL is relative, convert it into absolute URL
397 if ($url[0] == '/') {
398 // If it starts with '/' just append it to the server name
399 $newurl = $base_root . '/' . trim($url, '/');
400 }
401 elseif (preg_match('!^(?:index.php)?\?q=!i', $url)) {
402 // If it starts with ?q=, just prepend with the base URL
403 $newurl = $base_url . '/' . trim($url, '/');
404 }
405 else {
406 $newurl = url(trim($url, '/'), array('absolute' => TRUE));
407 }
408 $matches[1] = str_replace($url, $newurl, $matches[1]);
409 }
410 }
411 }
412 }
413
414 $ret = '<' . $matches[1] . '>';
415 if (count($matches) == 4) {
416 $ret .= $matches[2] . $matches[3];
417 if ((!empty($_print_urls)) && (isset($newurl))) {
418 $ret .= ' <span class="print-footnote">[' . _print_friendly_urls(trim($newurl)) . ']</span>';
419 }
420 }
421
422 return filter_xss_admin($ret);
423 }
424
425 /**
426 * Auxiliary function to store the Printer-friendly URLs list as static.
427 *
428 * @param $url
429 * absolute URL to be inserted in the list
430 * @return
431 * list of URLs previously stored if $url is 0, or the current count
432 * otherwise.
433 */
434 function _print_friendly_urls($url = 0) {
435 static $urls = array();
436 if ($url !== 0) {
437 $url_idx = array_search($url, $urls);
438 if ($url_idx !== FALSE) {
439 return ($url_idx + 1);
440 }
441 else {
442 $urls[] = $url;
443 return count($urls);
444 }
445 }
446 $ret = $urls;
447 $urls = array();
448 return $ret;
449 }
450
451 /**
452 * Check URL list settings for this node
453 *
454 * @param node
455 * node object
456 * @param $format
457 * format of the page being generated
458 * @return
459 * TRUE if URL list should be displayed, FALSE otherwise
460 */
461 function _print_url_list_enabled($node, $format = PRINT_HTML_FORMAT) {
462 if (!isset($node->type)) {
463 switch ($format) {
464 case PRINT_HTML_FORMAT:
465 $node_urllist = variable_get('print_display_sys_urllist', PRINT_TYPE_SYS_URLLIST_DEFAULT);
466 break;
467 case PRINT_MAIL_FORMAT:
468 $node_urllist = variable_get('print_mail_display_sys_urllist', PRINT_TYPE_SYS_URLLIST_DEFAULT);
469 break;
470 case PRINT_PDF_FORMAT:
471 $node_urllist = variable_get('print_pdf_display_sys_urllist', PRINT_TYPE_SYS_URLLIST_DEFAULT);
472 break;
473 default:
474 $node_urllist = PRINT_TYPE_SYS_URLLIST_DEFAULT;
475 }
476 }
477 else {
478 switch ($format) {
479 case PRINT_HTML_FORMAT:
480 $node_urllist = isset($node->print_display_urllist) ? $node->print_display_urllist : variable_get('print_display_urllist_' . $node->type, PRINT_TYPE_URLLIST_DEFAULT);
481 break;
482 case PRINT_MAIL_FORMAT:
483 $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);
484 break;
485 case PRINT_PDF_FORMAT:
486 $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);
487 break;
488 default:
489 $node_urllist = PRINT_TYPE_URLLIST_DEFAULT;
490 }
491 }
492
493 // Get value of Printer-friendly URLs setting
494 return (variable_get('print_urls', PRINT_URLS_DEFAULT) && ($node_urllist));
495 }
496
497 /**
498 * Prepare a Printer-friendly-ready node body for content nodes
499 *
500 * @param $nid
501 * node ID of the node to be rendered into a printer-friendly page
502 * @param array $query
503 * (optional) array of key/value pairs as used in the url() function for the
504 * query
505 * @param $cid
506 * comment ID of the individual comment to be rendered
507 * @param $format
508 * format of the page being generated
509 * @param $teaser
510 * if set to TRUE, outputs only the node's teaser
511 * @param $message
512 * optional sender's message (used by the send email module)
513 * @return
514 * filled array ready to be used in the template
515 */
516 function _print_generate_node($nid, $query = NULL, $cid = NULL, $format = PRINT_HTML_FORMAT, $teaser = FALSE, $message = NULL) {
517 global $_print_urls;
518
519 if (!isset($langcode)) {
520 $langcode = $GLOBALS['language_content']->language;
521 }
522
523 // We can take a node id
524 $node = node_load($nid);
525 if (!$node) {
526 // Node not found
527 drupal_not_found();
528 return FALSE;
529 }
530 elseif (!node_access('view', $node)) {
531 // Access is denied
532 drupal_access_denied();
533 return FALSE;
534 }
535 drupal_set_title($node->title);
536
537 $view_mode = $teaser ? 'teaser' : 'print';
538
539 // Turn off Pagination by the Paging module
540 unset($node->pages);
541 unset($node->page_count);
542
543 // Make this page a member of the original page's organic group
544 if (function_exists('og_set_group_context') && isset($node->og_groups)) {
545 og_set_group_context($node->og_groups);
546 }
547
548 if ($cid === NULL) {
549 // Adapted (simplified) version of node_view
550 // Render the node content
551 node_build_content($node, $view_mode);
552
553 // Disable the links area
554 unset($node->content['links']);
555 // Disable fivestar widget output
556 unset($node->content['fivestar_widget']);
557 // Disable service links module output
558 unset($node->content['service_links']);
559
560 $build = $node->content;
561 unset($node->content);
562 }
563
564 $print_comments = variable_get('print_comments', PRINT_COMMENTS_DEFAULT);
565
566 if (function_exists('comment_node_page_additions') && (($cid != NULL) || ($print_comments))) {
567 // Print only the requested comment (or if $cid is NULL, all of them)
568
569 $comments = comment_node_page_additions($node);
570 if (!empty($comments)) {
571 unset($comments['comment_form']);
572 foreach ($comments['comments'] as $key => &$comment) {
573 if (is_numeric($key)) {
574 if (($cid != NULL) && ($key != $cid)) {
575 unset($comments['comments'][$key]);
576 }
577 else {
578 unset($comment['links']);
579 }
580 }
581 }
582
583 $build['comments'] = $comments;
584 }
585 }
586
587 $build += array(
588 '#theme' => 'node',
589 '#node' => $node,
590 '#view_mode' => $view_mode,
591 '#language' => $langcode,
592 '#print_format' => $format,
593 );
594
595 $type = 'node';
596 drupal_alter(array('node_view', 'entity_view'), $build, $type);
597
598 $content = render($build);
599
600 // Get rid of any links before the content
601 $parts = explode('<div class="content', $content, 2);
602 if (count($parts) == 2) {
603 $pattern = '!(.*?)<a [^>]*?>(.*?)</a>(.*?)!mis';
604 $parts[0] = preg_replace($pattern, '$1$2$3', $parts[0]);
605 $content = implode('<div class="content', $parts);
606 }
607
608 // Check URL list settings
609 $_print_urls = _print_url_list_enabled($node, $format);
610
611 // Convert the a href elements
612 $pattern = '!<(a\s[^>]*?)>(.*?)(</a>)!is';
613 $content = preg_replace_callback($pattern, '_print_rewrite_urls', $content);
614
615 $print = _print_var_generator($node, $query, $message, $cid);
616 $print['content'] = $content;
617
618 return $print;
619 }
620
621 /**
622 * Prepare a Printer-friendly-ready node body for non-content pages
623 *
624 * @param $path
625 * path of the node to be rendered into a printer-friendly page
626 * @param array $query
627 * (optional) array of key/value pairs as used in the url() function for the
628 * query
629 * @param $format
630 * format of the page being generated
631 * @param $teaser
632 * if set to TRUE, outputs only the node's teaser
633 * @param $message
634 * optional sender's message (used by the send email module)
635 * @return
636 * filled array ready to be used in the template
637 */
638 function _print_generate_path($path, $query = NULL, $format = PRINT_HTML_FORMAT, $teaser = FALSE, $message = NULL) {
639 global $_print_urls;
640
641 // Handle node tabs
642 $parts = explode('/', $path);
643 if (ctype_digit($parts[0]) && (count($parts) > 1)) {
644 $path = 'node/' . $path;
645 }
646
647 $path = drupal_get_normal_path($path);
648
649 menu_set_active_item($path);
650 // Adapted from index.php.
651 $node = new stdClass();
652 $node->body = menu_execute_active_handler($path, FALSE);
653 if (is_array($node->body)) {
654 $node->body = drupal_render($node->body);
655 }
656
657 if (is_int($node->body)) {
658 switch ($node->body) {
659 case MENU_NOT_FOUND:
660 drupal_not_found();
661 return FALSE;
662 break;
663 case MENU_ACCESS_DENIED:
664 drupal_access_denied();
665 return FALSE;
666 break;
667 }
668 }
669
670 $node->title = drupal_get_title();
671 $node->path = $path;
672 $node->changed = 0;
673
674 // Delete any links area
675 $node->body = preg_replace('!\s*<div class="links">.*?</div>!sim', '', $node->body);
676
677 // Delete the contextual links also
678 $node->content = preg_replace('!\s*<div class="contextual-links-wrapper">.*?</div>!sim', '', $node->content);
679
680 // Check URL list settings
681 $_print_urls = _print_url_list_enabled($node, $format);
682
683 // Convert the a href elements
684 $pattern = '!<(a\s[^>]*?)>(.*?)(</a>)!is';
685 $node->body = preg_replace_callback($pattern, '_print_rewrite_urls', $node->body);
686
687 $print = _print_var_generator($node, $query, $message);
688 $print['content'] = $node->body;
689
690 return $print;
691 }
692
693
694 /**
695 * Prepare a Printer-friendly-ready node body for book pages
696 *
697 * @param $nid
698 * node ID of the node to be rendered into a printer-friendly page
699 * @param array $query
700 * (optional) array of key/value pairs as used in the url() function for the
701 * query
702 * @param $format
703 * format of the page being generated
704 * @param $teaser
705 * if set to TRUE, outputs only the node's teaser
706 * @param $message
707 * optional sender's message (used by the send email module)
708 * @return
709 * filled array ready to be used in the template
710 */
711 function _print_generate_book($nid, $query = NULL, $format = PRINT_HTML_FORMAT, $teaser = FALSE, $message = NULL) {
712 global $_print_urls;
713
714 $node = node_load($nid);
715 if (!$node) {
716 // Node not found
717 drupal_not_found();
718 return FALSE;
719 }
720 elseif (!node_access('view', $node) || (!user_access('access printer-friendly version'))) {
721 // Access is denied
722 drupal_access_denied();
723 return FALSE;
724 }
725
726 $tree = book_menu_subtree_data($node->book);
727 $node->body = book_export_traverse($tree, 'book_node_export');
728
729 // Check URL list settings
730 $_print_urls = _print_url_list_enabled($node, $format);
731
732 // Convert the a href elements
733 $pattern = '!<(a\s[^>]*?)>(.*?)(</a>)!is';
734 $node->body = preg_replace_callback($pattern, '_print_rewrite_urls', $node->body);
735
736 $print = _print_var_generator($node, $query, $message);
737 $print['content'] = $node->body;
738
739 // The title is already displayed by the book_recurse, so avoid duplication
740 $print['title'] = '';
741
742 return $print;
743 }