Fix #623712: Remove unnecessary urldecode() calls and check URLs using html_entity_de...
[project/print.git] / print.pages.inc
1 <?php
2 // $Id$
3
4 /**
5 * @file
6 * Contains the functions to generate Printer-friendly pages.
7 *
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
10 * in HTML format.
11 *
12 * @ingroup print
13 */
14
15 $_print_urls = PRINT_URLS_DEFAULT;
16
17 /**
18 * Generate an HTML version of the printer-friendly page
19 *
20 * @see print_controller()
21 * @see _print_get_template()
22 */
23 function print_controller_html() {
24 $args = func_get_args();
25 $path = implode('/', $args);
26 $cid = isset($_GET['comment']) ? (int)$_GET['comment'] : NULL;
27
28 $print = print_controller($path, $cid, PRINT_HTML_FORMAT);
29 if ($print !== FALSE) {
30 $node = $print['node'];
31 include_once(DRUPAL_ROOT . '/' . _print_get_template(PRINT_HTML_FORMAT, $print['type']));
32
33 $nodepath = (isset($node->path)) ? drupal_get_normal_path($node->path) : 'node/' . $path;
34 db_merge('print_page_counter')
35 ->key(array('path' => $nodepath))
36 ->fields(array(
37 'totalcount' => 1,
38 'timestamp' => REQUEST_TIME,
39 ))
40 ->expression('totalcount', 'totalcount + :inc', array(':inc' => 1))
41 ->execute();
42 }
43 }
44
45 /**
46 * Select the print generator function based on the page type
47 *
48 * Depending on the type of node, this functions chooses the appropriate
49 * generator function.
50 *
51 * @param $path
52 * path of the original page
53 * @param $cid
54 * comment ID of the individual comment to be rendered
55 * @param $format
56 * format of the page being generated
57 * @param $teaser
58 * if set to TRUE, outputs only the node's teaser
59 * @param $message
60 * optional sender's message (used by the send e-mail module)
61 * @return
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()
66 */
67 function print_controller($path, $cid = NULL, $format = PRINT_HTML_FORMAT, $teaser = FALSE, $message = NULL) {
68 if (empty($path)) {
69 // If no path was provided, let's try to generate a page for the referer
70 global $base_url;
71
72 $ref = $_SERVER['HTTP_REFERER'];
73 $path = preg_replace("!^$base_url/!", '', $ref);
74 if ($path === $ref) {
75 $path = '';
76 }
77 }
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);
83 if ($ret == 1) {
84 $path = $matches[1];
85 }
86 }
87 $parts = explode('/', $path);
88 if (is_numeric($parts[0]) && (count($parts) == 1)) {
89 $print = _print_generate_node($path, $cid, $format, $teaser, $message);
90 }
91 else {
92 $ret = preg_match('!^book/export/html/(.*)!i', $path, $matches);
93 if ($ret == 1) {
94 // This is a book PF page link, handle trough the book handling functions
95 $print = _print_generate_book($matches[1], $format, $teaser, $message);
96 }
97 else {
98 // If no content node was found, handle the page printing with the 'printable' engine
99 $print = _print_generate_path($path, $format, $teaser, $message);
100 }
101 }
102
103 return $print;
104 }
105
106 /**
107 * Generates a robots meta tag to tell them what they may index
108 *
109 * @return
110 * string with the meta robots tag
111 */
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();
117
118 if (!empty($print_robots_noindex)) {
119 $robots_meta[] = 'noindex';
120 }
121 if (!empty($print_robots_nofollow)) {
122 $robots_meta[] = 'nofollow';
123 }
124 if (!empty($print_robots_noarchive)) {
125 $robots_meta[] = 'noarchive';
126 }
127
128 if (count($robots_meta) > 0) {
129 $robots_meta = implode(', ', $robots_meta);
130 $robots_meta = "<meta name='robots' content='$robots_meta' />\n";
131 }
132 else {
133 $robots_meta = '';
134 }
135
136 return $robots_meta;
137 }
138
139 /**
140 * Post-processor that fills the array for the template with common details
141 *
142 * @param $node
143 * generated node with a printer-friendly node body
144 * @param $message
145 * optional sender's message (used by the send e-mail module)
146 * @param $cid
147 * id of current comment being generated (NULL when not generating
148 * an individual comment)
149 * @return
150 * array with the fields to be used in the template
151 */
152 function _print_var_generator($node, $message = NULL, $cid = NULL) {
153 global $base_url, $language, $_print_urls;
154
155 $path = empty($node->nid) ? $node->path : "node/$node->nid";
156
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);
169
170 $print['language'] = $language->language;
171 $print['title'] = check_plain($node->title);
172 $print['head'] = drupal_get_html_head();
173 $print['scripts'] = drupal_get_js();
174 $print['robots_meta'] = _print_robots_meta_generator();
175 $print['url'] = url($path, array('absolute' => TRUE));
176 $print['base_href'] = "<base href='" . $print['url'] . "' />\n";
177 $print['favicon'] = theme_get_setting('toggle_favicon') ? "<link rel='shortcut icon' href='" . theme_get_setting('favicon') . "' type='image/x-icon' />\n" : '';
178
179 if (!empty($print_css)) {
180 $replace_pairs = array('%b' => base_path(), '%t' => path_to_theme());
181 $user_css = check_url(strtr($print_css, $replace_pairs));
182 }
183 else {
184 drupal_add_css(drupal_get_path('module', 'print') . '/css/print.css');
185 }
186 $drupal_css = drupal_add_css();
187 foreach ($drupal_css as $key => $types) {
188 // Unset the theme's CSS
189 $drupal_css[$key]['theme'] = array();
190 }
191
192 // If we are sending a message via e-mail, the CSS must be embedded
193 if (!empty($message)) {
194 $style = '';
195 $css_files = array();
196 foreach ($drupal_css as $types) {
197 foreach ($types as $values) {
198 $css_files = array_merge($css_files, array_keys($values));
199 }
200 }
201 if (!empty($print_css)) {
202 // Convert to a local path, by removing the base_path
203 $pattern = '!^' . base_path() . '!';
204 $css_files[] = preg_replace($pattern, '', $user_css);
205 }
206 foreach ($css_files as $filename) {
207 $res = file_exists($filename) ? file_get_contents($filename, TRUE) : FALSE;
208 if ($res != FALSE) {
209 $style .= $res;
210 }
211 }
212 $print['css'] = "<style type='text/css' media='all'>$style</style>\n";
213 }
214 else {
215 $print['css'] = drupal_get_css($drupal_css);
216 if (!empty($print_css)) {
217 $query_string = '?'. substr(variable_get('css_js_query_string', '0'), 0, 1);
218 $print['css'] .= "<link type='text/css' rel='stylesheet' media='all' href='${user_css}${query_string}' />\n";
219 }
220 }
221
222 $window_close = ($print_html_new_window && $print_html_windowclose) ? 'window.close();' : '';
223 $print['sendtoprinter'] = $print_html_sendtoprinter ? " onload='window.print();$window_close'" : '';
224
225 switch ($print_logo_options) {
226 case 0: // none
227 $logo_url = 0;
228 break;
229 case 1: // theme's
230 $logo_url = theme_get_setting('logo');
231 break;
232 case 2: // user-specifed
233 $logo_url = strip_tags($print_logo_url);
234 break;
235 }
236 $print['logo'] = $logo_url ? "<img class='print-logo' src='" . check_url($logo_url) . "' alt='' />\n" : '';
237
238 switch ($print_footer_options) {
239 case 0: // none
240 $footer = '';
241 break;
242 case 1: // theme's
243 $footer = variable_get('site_footer', FALSE) . "\n" . theme('blocks', 'footer');
244 break;
245 case 2: // user-specifed
246 $footer = $print_footer_user;
247 break;
248 }
249 $print['footer_message'] = filter_xss_admin($footer);
250
251 $published_site = variable_get('site_name', 0);
252 if ($published_site) {
253 $print_text_published = filter_xss(variable_get('print_text_published', t('Published on %site_name')));
254 $published = t($print_text_published, array('%site_name' => $published_site));
255 $print['site_name'] = $published . ' (' . l($base_url, $base_url) . ')';
256 }
257 else {
258 $print['site_name'] = '';
259 }
260
261 if ($print_sourceurl_enabled == 1) {
262 /* Grab and format the src URL */
263 if (empty($print_sourceurl_forcenode)) {
264 $url = $print['url'];
265 }
266 else {
267 $url = $base_url . '/' . (((bool)variable_get('clean_url', '0')) ? '' : '?q=') . $path;
268 }
269 if (is_int($cid)) {
270 $url .= '#comment-$cid';
271 }
272 $retrieved_date = format_date(REQUEST_TIME, 'small');
273 $print_text_retrieved = filter_xss(variable_get('print_text_retrieved', t('retrieved on %date')));
274 $retrieved = t($print_text_retrieved, array('%date' => $retrieved_date));
275 $print['printdate'] = $print_sourceurl_date ? " ($retrieved)" : '';
276
277 $source_url = filter_xss(variable_get('print_text_source_url', t('Source URL')));
278 $print['source_url'] = '<strong>' . $source_url . $print['printdate'] . ':</strong> ' . l($url, $url);
279 }
280 else {
281 $print['source_url'] = '';
282 }
283
284 if (isset($node->type)) {
285 $node_type = $node->type;
286
287 if (theme_get_setting("toggle_node_info_$node_type")) {
288 $print_text_by = filter_xss(variable_get('print_text_by', t('By %author')));
289 $by_author = ($node->name ? $node->name : variable_get('anonymous', t('Anonymous')));
290 $print['submitted'] = t($print_text_by, array('%author' => $by_author));
291
292 $print_text_created = filter_xss(variable_get('print_text_created', t('Created %date')));
293 $created_datetime = format_date($node->created, 'small');
294 $print['created'] = t($print_text_created, array('%date' => $created_datetime));
295 }
296 else {
297 $print['submitted'] = '';
298 $print['created'] = '';
299 }
300
301 $print['type'] = $node->type;
302 }
303 else {
304 $print['submitted'] = '';
305 $print['created'] = '';
306 $print['type'] = '';
307 }
308
309 menu_set_active_item($path);
310 $breadcrumb = drupal_get_breadcrumb();
311 if (!empty($breadcrumb)) {
312 $breadcrumb[] = menu_get_active_title();
313 $print['breadcrumb'] = filter_xss(implode(' > ', $breadcrumb));
314 }
315 else {
316 $print['breadcrumb'] = '';
317 }
318
319 // Display the collected links at the bottom of the page. Code once taken from Kjartan Mannes' project.module
320 $print['pfp_links'] = '';
321 if (!empty($_print_urls)) {
322 $urls = _print_friendly_urls();
323 $max = count($urls);
324 $pfp_links = '';
325 if ($max) {
326 for ($i = 0; $i < $max; $i++) {
327 $pfp_links .= '[' . ($i + 1) . '] ' . check_plain($urls[$i]) . "<br />\n";
328 }
329 $links = filter_xss(variable_get('print_text_links', t('Links')));
330 $print['pfp_links'] = "<p><strong>$links:</strong><br />$pfp_links</p>";
331 }
332 }
333
334 if (module_exists('taxonomy')) {
335 // TODO make this work when taxonomy works again
336 // $terms = taxonomy_link('taxonomy terms', $node);
337 $terms = taxonomy_preview_terms($node);
338 $print['taxonomy'] = theme('links', $terms);
339 }
340
341 $print['content'] = $node->body;
342 $print['node'] = $node;
343 $print['message'] = $message;
344
345 return $print;
346 }
347
348 /**
349 * Callback function for the preg_replace_callback for URL-capable patterns
350 *
351 * Manipulate URLs to make them absolute in the URLs list, and to add a
352 * [n] footnote marker.
353 *
354 * @param $matches
355 * array with the matched tag patterns, usually <a...>+text+</a>
356 * @return
357 * tag with re-written URL and when appropriate the [n] index to the
358 * URL list
359 */
360 function _print_rewrite_urls($matches) {
361 global $base_url, $base_root, $_print_urls;
362
363 // first, split the html into the different tag attributes
364 $pattern = '!\s*(\w+\s*=\s*"(?:\\\"|[^"])*")\s*|\s*(\w+\s*=\s*\'(?:\\\\\'|[^\'])*\')\s*|\s*(\w+\s*=\s*\w+)\s*|\s+!';
365 $attribs = preg_split($pattern, $matches[1], -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
366 foreach ($attribs as $key => $value) {
367 $attribs[$key] = preg_replace('!(\w)\s*=\s*(.*)!', '$1=$2', $value);
368 }
369
370 $size = count($attribs);
371 for ($i=1; $i < $size; $i++) {
372 // If the attribute is href or src, we may need to rewrite the URL in the value
373 if (preg_match('!^(?:href|src)\s*?=(.*)!i', $attribs[$i], $urls) > 0) {
374 $url = trim($urls[1], " \t\n\r\0\x0B\"'");
375
376 if (strpos(html_entity_decode($url), '://') || preg_match('!^mailto:.*?@.*?\..*?$!iu', html_entity_decode($url))) {
377 // URL is absolute, do nothing
378 $newurl = $url;
379 }
380 else {
381 if ($url[0] == '#') {
382 // URL is an anchor tag
383 if (!empty($_print_urls)) {
384 $path = explode('/', $_GET['q']);
385 unset($path[0]);
386 $path = implode('/', $path);
387 if (is_numeric($path)) {
388 $path = "node/$path";
389 }
390 // Printer-friendly URLs is on, so we need to make it absolute
391 $newurl = url($path, array('fragment' => substr($url, 1), 'absolute' => TRUE));
392 }
393 // Because base href is the original page, change the link to
394 // still be usable inside the print page
395 $matches[1] = str_replace($url, base_path() . $_GET['q'] . $url, $matches[1]);
396 }
397 else {
398 // URL is relative, convert it into absolute URL
399 if ($url[0] == '/') {
400 // If it starts with '/' just append it to the server name
401 $newurl = $base_root . '/' . trim($url, '/');
402 }
403 elseif (preg_match('!^(?:index.php)?\?q=!i', $url)) {
404 // If it starts with ?q=, just prepend with the base URL
405 $newurl = $base_url . '/' . trim($url, '/');
406 }
407 else {
408 $newurl = url(trim($url, '/'), array('absolute' => TRUE));
409 }
410 $matches[1] = str_replace($url, $newurl, $matches[1]);
411 }
412 }
413 }
414 }
415
416 $ret = '<' . $matches[1] . '>';
417 if (count($matches) == 4) {
418 $ret .= $matches[2] . $matches[3];
419 if ((!empty($_print_urls)) && (isset($newurl))) {
420 $ret .= ' <span class="print-footnote">[' . _print_friendly_urls(trim($newurl)) . ']</span>';
421 }
422 }
423
424 return $ret;
425 }
426
427 /**
428 * Auxiliary function to store the Printer-friendly URLs list as static.
429 *
430 * @param $url
431 * absolute URL to be inserted in the list
432 * @return
433 * list of URLs previously stored if $url is 0, or the current count
434 * otherwise.
435 */
436 function _print_friendly_urls($url = 0) {
437 static $urls = array();
438 if ($url) {
439 $url_idx = array_search($url, $urls);
440 if ($url_idx !== FALSE) {
441 return ($url_idx + 1);
442 }
443 else {
444 $urls[] = $url;
445 return count($urls);
446 }
447 }
448 $ret = $urls;
449 $urls = array();
450 return $ret;
451 }
452
453 /**
454 * Choose most appropriate template
455 *
456 * Auxiliary function to resolve the most appropriate template trying to find
457 * a content specific template in the theme or module dir before falling back
458 * on a generic template also in those dirs.
459 *
460 * @param format
461 * format of the PF page being rendered (html, pdf, etc.)
462 * @param $type
463 * name of the node type being rendered in a PF page
464 * @return
465 * filename of the most suitable template
466 */
467 function _print_get_template($format = NULL, $type = NULL) {
468 $filenames = array();
469 // First try to find a template defined both for the format and then the type
470 if (!empty($format) && !empty($type)) {
471 $filenames[] = "print_$format.node-$type.tpl.php";
472 }
473 // Then only for the format
474 if (!empty($format)) {
475 $filenames[] = "print_$format.tpl.php";
476 }
477 // If the node type is known, then try to find that type's template file
478 if (!empty($type)) {
479 $filenames[] = "print.node-$type.tpl.php";
480 }
481 // Finally search for a generic template file
482 $filenames[] = 'print.tpl.php';
483
484 foreach ($filenames as $value) {
485 // First in the theme directory
486 $file = drupal_get_path('theme', $GLOBALS['theme_key']) . '/' . $value;
487 if (file_exists($file)) {
488 return $file;
489 }
490 // Then in the module directory
491 $file = drupal_get_path('module', 'print') . '/' . $value;
492 if (file_exists($file)) {
493 return $file;
494 }
495 }
496 }
497
498 /**
499 * Check URL list settings for this node
500 *
501 * @param node
502 * node object
503 * @param $format
504 * format of the page being generated
505 * @return
506 * TRUE if URL list should be displayed, FALSE otherwise
507 */
508 function _print_url_list_enabled($node, $format = PRINT_HTML_FORMAT) {
509 switch ($format) {
510 case PRINT_HTML_FORMAT:
511 $node_urllist = isset($node->print_display_urllist) ? $node->print_display_urllist : TRUE;
512 $fmt = '';
513 break;
514 case PRINT_MAIL_FORMAT:
515 $node_urllist = isset($node->print_mail_display_urllist) ? $node->print_mail_display_urllist : TRUE;
516 $fmt = $format . '_';
517 break;
518 case PRINT_PDF_FORMAT:
519 $node_urllist = isset($node->print_pdf_display_urllist) ? $node->print_pdf_display_urllist : TRUE;
520 $fmt = $format . '_';
521 break;
522 }
523 if (!isset($node_urllist)) $node_urllist = TRUE;
524
525 // Get value of Printer-friendly URLs setting
526 return (variable_get('print_urls', PRINT_URLS_DEFAULT) && ($node_urllist) &&
527 (isset($node->type) ? variable_get('print_' . $fmt . 'display_urllist_' . $node->type, PRINT_TYPE_URLLIST_DEFAULT) : PRINT_TYPE_URLLIST_DEFAULT));
528 }
529
530 /**
531 * Prepare a Printer-friendly-ready node body for content nodes
532 *
533 * @param $nid
534 * node ID of the node to be rendered into a printer-friendly page
535 * @param $cid
536 * comment ID of the individual comment to be rendered
537 * @param $format
538 * format of the page being generated
539 * @param $teaser
540 * if set to TRUE, outputs only the node's teaser
541 * @param $message
542 * optional sender's message (used by the send e-mail module)
543 * @return
544 * filled array ready to be used in the template
545 */
546 function _print_generate_node($nid, $cid = NULL, $format = PRINT_HTML_FORMAT, $teaser = FALSE, $message = NULL) {
547 global $_print_urls;
548
549 // We can take a node id
550 $node = node_load($nid);
551 if (!$node) {
552 // Node not found
553 drupal_not_found();
554 return FALSE;
555 }
556 elseif (!node_access('view', $node)) {
557 // Access is denied
558 drupal_access_denied();
559 return FALSE;
560 }
561 drupal_set_title($node->title);
562
563 //alert other modules that we are generating a printer-friendly page, so they can choose to show/hide info
564 $node->printing = TRUE;
565
566 //use the proper build_mode
567 $node->build_mode = 'print';
568
569 // Turn off Pagination by the Paging module
570 unset($node->pages);
571 unset($node->page_count);
572
573 // Make this page a member of the original page's organic group
574 if (function_exists('og_set_group_context') && isset($node->og_groups)) {
575 og_set_group_context($node->og_groups);
576 }
577
578 if ($cid === NULL) {
579 // Adapted (simplified) version of node_view
580 //Render the node content
581 $node = node_build_content($node, $teaser, TRUE);
582 // Disable the links area
583 unset($node->content['links']);
584 // Disable fivestar widget output
585 unset($node->content['fivestar_widget']);
586 // Disable service links module output
587 unset($node->content['service_links']);
588
589 $content = drupal_render($node->content);
590
591 // Disable the AdSense module ads
592 $content = preg_replace('!<div class=[\'"]adsense[\'"].*?</div>!sim', '', $content);
593
594 if ($teaser) {
595 $node->teaser = $content;
596 unset($node->body);
597 }
598 else {
599 $node->body = $content;
600 unset($node->teaser);
601 }
602 }
603
604 $print_comments = variable_get('print_comments', PRINT_COMMENTS_DEFAULT);
605
606 if (function_exists('comment_render') && (($cid != NULL) || ($print_comments))) {
607 //Print only the requested comment (or if $cid is NULL, all of them)
608 $comments = comment_render($node, $cid);
609
610 //Remove the comment forms
611 $comments = preg_replace('!<form.*?id="comment-.*?">.*?</form>!sim', '', $comments);
612 //Remove the 'Post new comment' title
613 $comments = preg_replace('!<h2.*?>' . t('Post new comment') . '</h2>!', '', $comments);
614 //Remove the comment title hyperlink
615 $comments = preg_replace('!(<h3.*?>.*?)<a.*?>(.*?)</a>(.*?</h3>)!i', '$1$2$3', $comments);
616 //Remove the comment author link
617 $pattern = '!(<(?:span|div) class="submitted">.*?)<a.*?>(.*?)</a>(.*?</(?:span|div)>)!sim';
618 if (preg_match($pattern, $comments)) {
619 $comments = preg_replace($pattern , '$1$2$3', $comments);
620 }
621 //Remove the comment links
622 $comments = preg_replace('!\s*<ul class="links">.*?</ul>!sim', '', $comments);
623 if ($cid != NULL) {
624 // Single comment requested, output only the comment
625 unset($node->body);
626 }
627 $node->body .= $comments;
628 }
629
630 module_invoke_all('node_alter', $node, $teaser, TRUE);
631
632 if ($teaser) {
633 $node->body = $node->teaser;
634 unset($node->teaser);
635 }
636
637 //Check URL list settings
638 $_print_urls = _print_url_list_enabled($node, $format);
639
640 // Convert the a href elements
641 $pattern = '!<(a\s[^>]*?)>(.*?)(</a>)!is';
642 $node->body = preg_replace_callback($pattern, '_print_rewrite_urls', $node->body);
643
644 init_theme();
645
646 $print = _print_var_generator($node, $message, $cid);
647
648 return $print;
649 }
650
651 /**
652 * Prepare a Printer-friendly-ready node body for non-content pages
653 *
654 * @param $path
655 * path of the node to be rendered into a printer-friendly page
656 * @param $format
657 * format of the page being generated
658 * @param $teaser
659 * if set to TRUE, outputs only the node's teaser
660 * @param $message
661 * optional sender's message (used by the send e-mail module)
662 * @return
663 * filled array ready to be used in the template
664 */
665 function _print_generate_path($path, $format = PRINT_HTML_FORMAT, $teaser = FALSE, $message = NULL) {
666 global $_print_urls;
667
668 // Handle node tabs
669 $parts = explode('/', $path);
670 if (is_numeric($parts[0]) && (count($parts) > 1)) {
671 $path = 'node/' . $path;
672 }
673
674 $path = drupal_get_normal_path($path);
675
676 menu_set_active_item($path);
677 // Adapted from index.php.
678 $node = new stdClass();
679 $node->body = menu_execute_active_handler($path);
680
681 // It may happen that a drupal_not_found is called in the above call
682 if (preg_match('/404 Not Found/', drupal_get_headers()) == 1) {
683 return FALSE;
684 }
685
686 if (is_int($node->body)) {
687 switch ($node->body) {
688 case MENU_NOT_FOUND:
689 drupal_not_found();
690 return FALSE;
691 break;
692 case MENU_ACCESS_DENIED:
693 drupal_access_denied();
694 return FALSE;
695 break;
696 }
697 }
698
699 $node->title = drupal_get_title();
700 $node->path = $path;
701
702 // Delete any links area
703 $node->body = preg_replace('!\s*<div class="links">.*?</div>!sim', '', $node->body);
704
705 //Check URL list settings
706 $_print_urls = _print_url_list_enabled($node, $format);
707
708 // Convert the a href elements
709 $pattern = '!<(a\s[^>]*?)>(.*?)(</a>)!is';
710 $node->body = preg_replace_callback($pattern, '_print_rewrite_urls', $node->body);
711
712 init_theme();
713
714 $print = _print_var_generator($node, $message);
715
716 return $print;
717 }
718
719
720 /**
721 * Prepare a Printer-friendly-ready node body for book pages
722 *
723 * @param $nid
724 * node ID of the node to be rendered into a printer-friendly page
725 * @param $format
726 * format of the page being generated
727 * @param $teaser
728 * if set to TRUE, outputs only the node's teaser
729 * @param $message
730 * optional sender's message (used by the send e-mail module)
731 * @return
732 * filled array ready to be used in the template
733 */
734 function _print_generate_book($nid, $format = PRINT_HTML_FORMAT, $teaser = FALSE, $message = NULL) {
735 global $_print_urls;
736
737 $node = node_load($nid);
738 if (!$node) {
739 // Node not found
740 drupal_not_found();
741 return FALSE;
742 }
743 elseif (!node_access('view', $node) || (!user_access('access printer-friendly version'))) {
744 // Access is denied
745 drupal_access_denied();
746 return FALSE;
747 }
748
749 $tree = book_menu_subtree_data($node->book);
750 $node->body = book_export_traverse($tree, 'book_node_export');
751
752 //Check URL list settings
753 $_print_urls = _print_url_list_enabled($node, $format);
754
755 // Convert the a href elements
756 $pattern = '!<(a\s[^>]*?)>(.*?)(</a>)!is';
757 $node->body = preg_replace_callback($pattern, '_print_rewrite_urls', $node->body);
758
759 init_theme();
760
761 $print = _print_var_generator($node, $message);
762 // The title is already displayed by the book_recurse, so avoid duplication
763 $print['title'] = '';
764
765 return $print;
766 }