Issue #1049892 by ogi: add missing > in ->setFontSubsetting
[project/print.git] / print.pages.inc
CommitLineData
872fe2ea 1<?php
872fe2ea 2
e8517dd5
JV
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.
2b011462
JV
10 *
11 * @ingroup print
e8517dd5 12 */
872fe2ea 13
dd3ed916
JV
14$_print_urls = PRINT_URLS_DEFAULT;
15
e8517dd5
JV
16/**
17 * Generate an HTML version of the printer-friendly page
18 *
19 * @see print_controller()
e8517dd5 20 */
9c35190f 21function print_controller_html() {
cc224831 22 $args = func_get_args();
cc224831 23 $path = implode('/', $args);
d17ec42a 24 $cid = isset($_GET['comment']) ? (int)$_GET['comment'] : NULL;
ded70136 25
dd3ed916 26 $print = print_controller($path, $cid, PRINT_HTML_FORMAT);
296d7e0c
JV
27 if ($print !== FALSE) {
28 $node = $print['node'];
60342b1c 29
eab820ce 30 $html = theme('print_page', $print, PRINT_HTML_FORMAT, $node);
60342b1c
JV
31 $html = drupal_final_markup($html);
32 print $html;
dd3ed916 33
440cb593 34 $nodepath = (isset($node->path)) ? drupal_get_normal_path($node->path) : 'node/'. $path;
dd3ed916
JV
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());
40 }
296d7e0c 41 }
9c35190f
JV
42}
43
872fe2ea 44/**
ded70136 45 * Select the print generator function based on the page type
e8517dd5
JV
46 *
47 * Depending on the type of node, this functions chooses the appropriate
48 * generator function.
49 *
ded70136
JV
50 * @param $path
51 * path of the original page
52 * @param $cid
53 * comment ID of the individual comment to be rendered
dd3ed916
JV
54 * @param $format
55 * format of the page being generated
ded70136
JV
56 * @param $teaser
57 * if set to TRUE, outputs only the node's teaser
58 * @param $message
59 * optional sender's message (used by the send e-mail module)
e8517dd5
JV
60 * @return
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()
872fe2ea 65 */
dd3ed916 66function print_controller($path, $cid = NULL, $format = PRINT_HTML_FORMAT, $teaser = FALSE, $message = NULL) {
716cfe58
JV
67 if (empty($path)) {
68 // If no path was provided, let's try to generate a page for the referer
69 global $base_url;
70
71 $ref = referer_uri();
72 $path = preg_replace("!^$base_url/!", '', $ref);
73 if ($path === $ref) {
74 $path = '';
75 }
76 }
ded70136 77 if (!is_numeric($path)) {
872fe2ea
JV
78 // Indirect call with print/alias
79 // If there is a path alias with these arguments, generate a printer-friendly version for it
ded70136 80 $path = drupal_get_normal_path($path);
cc224831 81 $ret = preg_match('!^node/(.*)!i', $path, $matches);
872fe2ea 82 if ($ret == 1) {
ded70136 83 $path = $matches[1];
872fe2ea
JV
84 }
85 }
cc224831 86 $parts = explode('/', $path);
30c1f319 87 if (is_numeric($parts[0]) && (count($parts) == 1)) {
dd3ed916 88 $print = _print_generate_node($path, $cid, $format, $teaser, $message);
872fe2ea
JV
89 }
90 else {
cc224831 91 $ret = preg_match('!^book/export/html/(.*)!i', $path, $matches);
872fe2ea
JV
92 if ($ret == 1) {
93 // This is a book PF page link, handle trough the book handling functions
dd3ed916 94 $print = _print_generate_book($matches[1], $format, $teaser, $message);
872fe2ea
JV
95 }
96 else {
97 // If no content node was found, handle the page printing with the 'printable' engine
dd3ed916 98 $print = _print_generate_path($path, $format, $teaser, $message);
872fe2ea
JV
99 }
100 }
9c35190f
JV
101
102 return $print;
872fe2ea
JV
103}
104
872fe2ea 105/**
e8517dd5 106 * Generates a robots meta tag to tell them what they may index
872fe2ea 107 *
e8517dd5 108 * @return
8d3c5d9d 109 * string with the meta robots tag
872fe2ea
JV
110 */
111function _print_robots_meta_generator() {
cc224831
JV
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);
872fe2ea
JV
115 $robots_meta = array();
116
cc224831 117 if (!empty($print_robots_noindex)) {
872fe2ea
JV
118 $robots_meta[] = 'noindex';
119 }
cc224831 120 if (!empty($print_robots_nofollow)) {
872fe2ea
JV
121 $robots_meta[] = 'nofollow';
122 }
cc224831 123 if (!empty($print_robots_noarchive)) {
872fe2ea
JV
124 $robots_meta[] = 'noarchive';
125 }
872fe2ea 126
e8517dd5
JV
127 if (count($robots_meta) > 0) {
128 $robots_meta = implode(', ', $robots_meta);
cc224831 129 $robots_meta = "<meta name='robots' content='$robots_meta' />\n";
872fe2ea
JV
130 }
131 else {
132 $robots_meta = '';
133 }
134
135 return $robots_meta;
136}
137
138/**
e8517dd5 139 * Post-processor that fills the array for the template with common details
872fe2ea 140 *
e8517dd5
JV
141 * @param $node
142 * generated node with a printer-friendly node body
ded70136
JV
143 * @param $message
144 * optional sender's message (used by the send e-mail module)
e8517dd5 145 * @param $cid
8d3c5d9d 146 * id of current comment being generated (NULL when not generating
e8517dd5
JV
147 * an individual comment)
148 * @return
149 * array with the fields to be used in the template
872fe2ea 150 */
ded70136 151function _print_var_generator($node, $message = NULL, $cid = NULL) {
dd3ed916 152 global $base_url, $language, $_print_urls;
872fe2ea 153
11747f00
JV
154 $path = empty($node->nid) ? $node->path : "node/$node->nid";
155
c212917a
JV
156 // Needed because of the theme_get_setting() calls below
157 init_theme();
158
872fe2ea 159 // print module settings
cc224831 160 $print_css = variable_get('print_css', PRINT_CSS_DEFAULT);
d0501fcc 161 $print_logo_options = variable_get('print_logo_options', PRINT_LOGO_OPTIONS_DEFAULT);
cc224831 162 $print_logo_url = variable_get('print_logo_url', PRINT_LOGO_URL_DEFAULT);
695dc0fb 163 $print_html_new_window = variable_get('print_html_new_window', PRINT_HTML_NEW_WINDOW_DEFAULT);
cc224831 164 $print_html_sendtoprinter = variable_get('print_html_sendtoprinter', PRINT_HTML_SENDTOPRINTER_DEFAULT);
695dc0fb 165 $print_html_windowclose = variable_get('print_html_windowclose', PRINT_HTML_WINDOWCLOSE_DEFAULT);
cc224831
JV
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);
2891fe2b
JV
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);
cc224831
JV
171
172 $print['language'] = $language->language;
7b6f691d 173 $print['title'] = check_plain($node->title);
cc224831 174 $print['head'] = drupal_get_html_head();
3c20d7b0
JV
175 if ($print_html_sendtoprinter) {
176 drupal_add_js('misc/drupal.js', 'core');
177 }
cc224831
JV
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" : '';
872fe2ea 183
cc224831 184 if (!empty($print_css)) {
4408679f 185 drupal_add_css(strtr($print_css, array('%t' => path_to_theme())));
872fe2ea
JV
186 }
187 else {
2891fe2b 188 drupal_add_css(drupal_get_path('module', 'print') .'/css/print.css');
ded70136 189 }
2891fe2b
JV
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();
ded70136
JV
194 }
195
196 // If we are sending a message via e-mail, the CSS must be embedded
197 if (!empty($message)) {
cc224831 198 $style = '';
2891fe2b
JV
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));
203 }
204 }
2891fe2b 205 foreach ($css_files as $filename) {
38df3d6a 206 $res = file_exists($filename) ? file_get_contents($filename, TRUE) : FALSE;
7f5aa4fa
JV
207 if ($res != FALSE) {
208 $style .= $res;
209 }
ded70136 210 }
cc224831 211 $print['css'] = "<style type='text/css' media='all'>$style</style>\n";
ded70136
JV
212 }
213 else {
2891fe2b 214 $print['css'] = drupal_get_css($drupal_css);
872fe2ea 215 }
872fe2ea 216
695dc0fb 217 $window_close = ($print_html_new_window && $print_html_windowclose) ? 'window.close();' : '';
3c20d7b0 218 $print['sendtoprinter'] = $print_html_sendtoprinter ? "<script type='text/javascript'>Drupal.behaviors.print = function(context) {window.print();$window_close}</script>" : '';
8d3c5d9d 219
d0501fcc
JV
220 switch ($print_logo_options) {
221 case 0: // none
222 $logo_url = 0;
223 break;
224 case 1: // theme's
225 $logo_url = theme_get_setting('logo');
226 break;
227 case 2: // user-specifed
228 $logo_url = strip_tags($print_logo_url);
229 break;
230 }
389389c4
JV
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" : '';
872fe2ea 233
2891fe2b
JV
234 switch ($print_footer_options) {
235 case 0: // none
236 $footer = '';
237 break;
238 case 1: // theme's
d9482703 239 $footer = variable_get('site_footer', FALSE) ."\n". theme('blocks', 'footer');
2891fe2b
JV
240 break;
241 case 2: // user-specifed
242 $footer = $print_footer_user;
243 break;
244 }
d9482703 245 $print['footer_message'] = filter_xss_admin($footer);
2891fe2b 246
e8517dd5 247 $published_site = variable_get('site_name', 0);
77d2b585 248 if ($published_site) {
d9482703 249 $print_text_published = filter_xss(variable_get('print_text_published', t('Published on %site_name')));
2e68fe74 250 $published = t($print_text_published, array('%site_name' => $published_site));
cc224831 251 $print['site_name'] = $published .' ('. l($base_url, $base_url) .')';
5a257a75
JV
252 }
253 else {
cc224831 254 $print['site_name'] = '';
5a257a75 255 }
e8517dd5 256
cc224831 257 if ($print_sourceurl_enabled == 1) {
e8517dd5 258 /* Grab and format the src URL */
cc224831
JV
259 if (empty($print_sourceurl_forcenode)) {
260 $url = $print['url'];
e8517dd5
JV
261 }
262 else {
77d2b585 263 $url = $base_url .'/'. (((bool)variable_get('clean_url', '0')) ? '' : '?q=') . $path;
e8517dd5 264 }
d17ec42a 265 if (is_int($cid)) {
cc224831 266 $url .= '#comment-$cid';
e8517dd5
JV
267 }
268 $retrieved_date = format_date(time(), 'small');
d9482703 269 $print_text_retrieved = filter_xss(variable_get('print_text_retrieved', t('retrieved on %date')));
2e68fe74 270 $retrieved = t($print_text_retrieved, array('%date' => $retrieved_date));
cc224831 271 $print['printdate'] = $print_sourceurl_date ? " ($retrieved)" : '';
e8517dd5 272
d9482703 273 $source_url = filter_xss(variable_get('print_text_source_url', t('Source URL')));
cc224831 274 $print['source_url'] = '<strong>'. $source_url . $print['printdate'] .':</strong> '. l($url, $url);
872fe2ea
JV
275 }
276 else {
cc224831 277 $print['source_url'] = '';
872fe2ea
JV
278 }
279
6e515575 280 $print['type'] = (isset($node->type)) ? $node->type : '';
872fe2ea 281
11747f00 282 menu_set_active_item($path);
872fe2ea
JV
283 $breadcrumb = drupal_get_breadcrumb();
284 if (!empty($breadcrumb)) {
285 $breadcrumb[] = menu_get_active_title();
d9482703 286 $print['breadcrumb'] = filter_xss(implode(' > ', $breadcrumb));
872fe2ea
JV
287 }
288 else {
cc224831 289 $print['breadcrumb'] = '';
872fe2ea
JV
290 }
291
292 // Display the collected links at the bottom of the page. Code once taken from Kjartan Mannes' project.module
527ae43d 293 $print['pfp_links'] = '';
dd3ed916 294 if (!empty($_print_urls)) {
872fe2ea
JV
295 $urls = _print_friendly_urls();
296 $max = count($urls);
e8517dd5 297 $pfp_links = '';
872fe2ea 298 if ($max) {
872fe2ea 299 for ($i = 0; $i < $max; $i++) {
45fb6397 300 $pfp_links .= '['. ($i + 1) .'] '. check_plain($urls[$i]) ."<br />\n";
872fe2ea 301 }
d9482703 302 $links = filter_xss(variable_get('print_text_links', t('Links')));
cc224831 303 $print['pfp_links'] = "<p><strong>$links:</strong><br />$pfp_links</p>";
872fe2ea
JV
304 }
305 }
306
cc224831
JV
307 $print['node'] = $node;
308 $print['message'] = $message;
9c35190f 309
872fe2ea
JV
310 return $print;
311}
312
313/**
e8517dd5 314 * Callback function for the preg_replace_callback for URL-capable patterns
872fe2ea 315 *
8d3c5d9d 316 * Manipulate URLs to make them absolute in the URLs list, and to add a
e8517dd5 317 * [n] footnote marker.
8d3c5d9d 318 *
e8517dd5
JV
319 * @param $matches
320 * array with the matched tag patterns, usually <a...>+text+</a>
321 * @return
8d3c5d9d 322 * tag with re-written URL and when appropriate the [n] index to the
e8517dd5 323 * URL list
872fe2ea
JV
324 */
325function _print_rewrite_urls($matches) {
dd3ed916 326 global $base_url, $base_root, $_print_urls;
872fe2ea 327
923c43d1
JV
328 $include_anchors = variable_get('print_urls_anchors', PRINT_URLS_ANCHORS_DEFAULT);
329
872fe2ea 330 // first, split the html into the different tag attributes
e7fb3470
JV
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);
335 }
872fe2ea 336
cc224831
JV
337 $size = count($attribs);
338 for ($i=1; $i < $size; $i++) {
872fe2ea 339 // If the attribute is href or src, we may need to rewrite the URL in the value
e7fb3470 340 if (preg_match('!^(?:href|src)\s*?=(.*)!i', $attribs[$i], $urls) > 0) {
23bbdbc4 341 $url = trim($urls[1], " \t\n\r\0\x0B\"'");
872fe2ea 342
a514b6b8 343 if (strpos(html_entity_decode($url), '://') || preg_match('!^mailto:.*?@.*?\..*?$!iu', html_entity_decode($url))) {
872fe2ea 344 // URL is absolute, do nothing
a514b6b8 345 $newurl = $url;
872fe2ea
JV
346 }
347 else {
e7a2acce 348 if ($url[0] == '#') {
872fe2ea 349 // URL is an anchor tag
923c43d1 350 if ($include_anchors && (!empty($_print_urls))) {
e7a2acce
JV
351 $path = explode('/', $_GET['q']);
352 unset($path[0]);
353 $path = implode('/', $path);
872fe2ea
JV
354 if (is_numeric($path)) {
355 $path = "node/$path";
356 }
357 // Printer-friendly URLs is on, so we need to make it absolute
a514b6b8 358 $newurl = url($path, array('fragment' => substr($url, 1), 'absolute' => TRUE));
872fe2ea
JV
359 }
360 // Because base href is the original page, change the link to
361 // still be usable inside the print page
55db59cd 362 $matches[1] = str_replace($url, base_path() . $_GET['q'] . $url, $matches[1]);
872fe2ea
JV
363 }
364 else {
365 // URL is relative, convert it into absolute URL
e7a2acce 366 if ($url[0] == '/') {
872fe2ea 367 // If it starts with '/' just append it to the server name
a514b6b8 368 $newurl = $base_root .'/'. trim($url, '/');
872fe2ea 369 }
bd7c1fee
JV
370 elseif (preg_match('!^(?:index.php)?\?q=!i', $url)) {
371 // If it starts with ?q=, just prepend with the base URL
a514b6b8 372 $newurl = $base_url .'/'. trim($url, '/');
872fe2ea
JV
373 }
374 else {
a514b6b8 375 $newurl = rawurldecode(url(trim($url, '/'), array('absolute' => TRUE)));
872fe2ea
JV
376 }
377 $matches[1] = str_replace($url, $newurl, $matches[1]);
378 }
379 }
380 }
381 }
382
872fe2ea 383 $ret = '<'. $matches[1] .'>';
a5000499
JV
384 if (count($matches) == 4) {
385 $ret .= $matches[2] . $matches[3];
dd3ed916 386 if ((!empty($_print_urls)) && (isset($newurl))) {
a514b6b8 387 $ret .= ' <span class="print-footnote">['. _print_friendly_urls(trim($newurl)) .']</span>';
872fe2ea
JV
388 }
389 }
390
391 return $ret;
392}
393
394/**
395 * Auxiliary function to store the Printer-friendly URLs list as static.
396 *
e8517dd5
JV
397 * @param $url
398 * absolute URL to be inserted in the list
8d3c5d9d
JV
399 * @return
400 * list of URLs previously stored if $url is 0, or the current count
e8517dd5 401 * otherwise.
872fe2ea
JV
402 */
403function _print_friendly_urls($url = 0) {
404 static $urls = array();
405 if ($url) {
406 $url_idx = array_search($url, $urls);
407 if ($url_idx !== FALSE) {
408 return ($url_idx + 1);
409 }
410 else {
411 $urls[] = $url;
412 return count($urls);
413 }
414 }
e7a2acce
JV
415 $ret = $urls;
416 $urls = array();
417 return $ret;
872fe2ea
JV
418}
419
420/**
dd3ed916
JV
421 * Check URL list settings for this node
422 *
423 * @param node
424 * node object
425 * @param $format
426 * format of the page being generated
427 * @return
428 * TRUE if URL list should be displayed, FALSE otherwise
429 */
430function _print_url_list_enabled($node, $format = PRINT_HTML_FORMAT) {
c212917a 431 if (!isset($node->type)) {
3c4ed6fa 432 return variable_get('print_urls', PRINT_URLS_DEFAULT);
c212917a
JV
433 }
434
dd3ed916
JV
435 switch ($format) {
436 case PRINT_HTML_FORMAT:
395b324c 437 $node_urllist = isset($node->print_display_urllist) ? $node->print_display_urllist : variable_get('print_display_urllist_'. $node->type, PRINT_TYPE_URLLIST_DEFAULT);
dd3ed916
JV
438 $fmt = '';
439 break;
440 case PRINT_MAIL_FORMAT:
395b324c 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);
dd3ed916
JV
442 $fmt = $format .'_';
443 break;
444 case PRINT_PDF_FORMAT:
395b324c 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);
dd3ed916
JV
446 $fmt = $format .'_';
447 break;
448 }
395b324c 449 if (!isset($node_urllist)) $node_urllist = PRINT_TYPE_URLLIST_DEFAULT;
dd3ed916
JV
450
451 // Get value of Printer-friendly URLs setting
395b324c 452 return (variable_get('print_urls', PRINT_URLS_DEFAULT) && ($node_urllist));
dd3ed916
JV
453}
454
455/**
e8517dd5 456 * Prepare a Printer-friendly-ready node body for content nodes
8d3c5d9d 457 *
e8517dd5
JV
458 * @param $nid
459 * node ID of the node to be rendered into a printer-friendly page
460 * @param $cid
461 * comment ID of the individual comment to be rendered
dd3ed916
JV
462 * @param $format
463 * format of the page being generated
ded70136
JV
464 * @param $teaser
465 * if set to TRUE, outputs only the node's teaser
466 * @param $message
467 * optional sender's message (used by the send e-mail module)
e8517dd5
JV
468 * @return
469 * filled array ready to be used in the template
872fe2ea 470 */
dd3ed916
JV
471function _print_generate_node($nid, $cid = NULL, $format = PRINT_HTML_FORMAT, $teaser = FALSE, $message = NULL) {
472 global $_print_urls;
473
e4583732
JV
474 //use the proper build_mode
475 $node->build_mode = NODE_BUILD_PRINT;
476
872fe2ea 477 // We can take a node id
d21a1360 478 $node = node_load($nid);
1a3d5ae9
JV
479 if (!$node) {
480 // Node not found
481 drupal_not_found();
296d7e0c 482 return FALSE;
1a3d5ae9
JV
483 }
484 elseif (!node_access('view', $node)) {
872fe2ea 485 // Access is denied
1a3d5ae9 486 drupal_access_denied();
296d7e0c 487 return FALSE;
872fe2ea 488 }
7b6f691d 489 drupal_set_title(check_plain($node->title));
872fe2ea
JV
490
491 //alert other modules that we are generating a printer-friendly page, so they can choose to show/hide info
e8517dd5 492 $node->printing = TRUE;
ac3e43b3 493
872fe2ea
JV
494 // Turn off Pagination by the Paging module
495 unset($node->pages);
496 unset($node->page_count);
497
23bea4c1
JV
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);
501 }
502
872fe2ea 503 if ($cid === NULL) {
1a3d5ae9 504 // Adapted (simplified) version of node_view
872fe2ea 505 //Render the node content
ded70136 506 $node = node_build_content($node, $teaser, TRUE);
872fe2ea 507 // Disable fivestar widget output
cc224831 508 unset($node->content['fivestar_widget']);
872fe2ea 509 // Disable service links module output
cc224831 510 unset($node->content['service_links']);
8d3c5d9d 511
1a3d5ae9 512 $content = drupal_render($node->content);
841a3cd3
JV
513
514 // Disable the AdSense module ads
515 $content = preg_replace('!<div class=[\'"]adsense[\'"].*?</div>!sim', '', $content);
516
1a3d5ae9
JV
517 if ($teaser) {
518 $node->teaser = $content;
519 unset($node->body);
520 }
521 else {
522 $node->body = $content;
523 unset($node->teaser);
524 }
872fe2ea
JV
525 }
526
cc224831 527 $print_comments = variable_get('print_comments', PRINT_COMMENTS_DEFAULT);
872fe2ea 528
cc224831 529 if (function_exists('comment_render') && (($cid != NULL) || ($print_comments))) {
872fe2ea
JV
530 //Print only the requested comment (or if $cid is NULL, all of them)
531 $comments = comment_render($node, $cid);
8d3c5d9d 532
872fe2ea 533 //Remove the comment forms
cc224831 534 $comments = preg_replace('!<form.*?id="comment-.*?">.*?</form>!sim', '', $comments);
872fe2ea 535 //Remove the 'Post new comment' title
4d7e0c29 536 $comments = preg_replace('!<h2.*?>'. t('Post new comment') .'</h2>!', '', $comments);
872fe2ea 537 //Remove the comment title hyperlink
4d7e0c29 538 $comments = preg_replace('!(<h3.*?>.*?)<a.*?>(.*?)</a>(.*?</h3>)!i', '$1$2$3', $comments);
643e4f42 539 //Remove the comment author link
4d7e0c29 540 $pattern = '!(<(?:span|div) class="submitted">.*?)<a.*?>(.*?)</a>(.*?</(?:span|div)>)!sim';
e8517dd5 541 if (preg_match($pattern, $comments)) {
4d7e0c29 542 $comments = preg_replace($pattern , '$1$2$3', $comments);
e8517dd5 543 }
872fe2ea 544 //Remove the comment links
cc224831 545 $comments = preg_replace('!\s*<ul class="links">.*?</ul>!sim', '', $comments);
872fe2ea
JV
546 if ($cid != NULL) {
547 // Single comment requested, output only the comment
548 unset($node->body);
549 }
550 $node->body .= $comments;
551 }
552
1a3d5ae9 553 node_invoke_nodeapi($node, 'alter', $teaser, TRUE);
872fe2ea 554
eab820ce
JV
555 $content = theme('print_node', $node, $teaser, TRUE, $format);
556
11415979 557 if ($teaser) {
395b324c 558 $content = $node->teaser;
11415979
JV
559 }
560
dd3ed916
JV
561 //Check URL list settings
562 $_print_urls = _print_url_list_enabled($node, $format);
563
872fe2ea 564 // Convert the a href elements
cc224831 565 $pattern = '!<(a\s[^>]*?)>(.*?)(</a>)!is';
395b324c 566 $content = preg_replace_callback($pattern, '_print_rewrite_urls', $content);
872fe2ea 567
ded70136 568 $print = _print_var_generator($node, $message, $cid);
4567b6ac 569 $print['content'] = $content;
872fe2ea 570
9c35190f 571 return $print;
872fe2ea
JV
572}
573
574/**
e8517dd5 575 * Prepare a Printer-friendly-ready node body for non-content pages
8d3c5d9d 576 *
e8517dd5
JV
577 * @param $path
578 * path of the node to be rendered into a printer-friendly page
dd3ed916
JV
579 * @param $format
580 * format of the page being generated
ded70136
JV
581 * @param $teaser
582 * if set to TRUE, outputs only the node's teaser
583 * @param $message
584 * optional sender's message (used by the send e-mail module)
e8517dd5
JV
585 * @return
586 * filled array ready to be used in the template
872fe2ea 587 */
dd3ed916
JV
588function _print_generate_path($path, $format = PRINT_HTML_FORMAT, $teaser = FALSE, $message = NULL) {
589 global $_print_urls;
590
30c1f319
JV
591 // Handle node tabs
592 $parts = explode('/', $path);
593 if (is_numeric($parts[0]) && (count($parts) > 1)) {
594 $path = 'node/'. $path;
595 }
596
872fe2ea
JV
597 $path = drupal_get_normal_path($path);
598
599 menu_set_active_item($path);
600 // Adapted from index.php.
601 $node = new stdClass();
602 $node->body = menu_execute_active_handler($path);
872fe2ea
JV
603
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) {
296d7e0c 606 return FALSE;
872fe2ea
JV
607 }
608
e7a2acce
JV
609 if (is_int($node->body)) {
610 switch ($node->body) {
611 case MENU_NOT_FOUND:
1a3d5ae9 612 drupal_not_found();
296d7e0c 613 return FALSE;
e7a2acce
JV
614 break;
615 case MENU_ACCESS_DENIED:
1a3d5ae9 616 drupal_access_denied();
296d7e0c 617 return FALSE;
e7a2acce
JV
618 break;
619 }
872fe2ea
JV
620 }
621
440cb593
JV
622 $node->title = drupal_get_title();
623 $node->path = $path;
624
872fe2ea 625 // Delete any links area
cc224831 626 $node->body = preg_replace('!\s*<div class="links">.*?</div>!sim', '', $node->body);
872fe2ea 627
dd3ed916
JV
628 //Check URL list settings
629 $_print_urls = _print_url_list_enabled($node, $format);
630
872fe2ea 631 // Convert the a href elements
cc224831
JV
632 $pattern = '!<(a\s[^>]*?)>(.*?)(</a>)!is';
633 $node->body = preg_replace_callback($pattern, '_print_rewrite_urls', $node->body);
872fe2ea 634
ded70136 635 $print = _print_var_generator($node, $message);
4567b6ac 636 $print['content'] = $node->body;
872fe2ea 637
9c35190f 638 return $print;
872fe2ea
JV
639}
640
641
642/**
e8517dd5 643 * Prepare a Printer-friendly-ready node body for book pages
8d3c5d9d 644 *
e8517dd5
JV
645 * @param $nid
646 * node ID of the node to be rendered into a printer-friendly page
dd3ed916
JV
647 * @param $format
648 * format of the page being generated
ded70136
JV
649 * @param $teaser
650 * if set to TRUE, outputs only the node's teaser
651 * @param $message
652 * optional sender's message (used by the send e-mail module)
e8517dd5
JV
653 * @return
654 * filled array ready to be used in the template
872fe2ea 655 */
dd3ed916
JV
656function _print_generate_book($nid, $format = PRINT_HTML_FORMAT, $teaser = FALSE, $message = NULL) {
657 global $_print_urls;
658
d21a1360 659 $node = node_load($nid);
1a3d5ae9
JV
660 if (!$node) {
661 // Node not found
662 drupal_not_found();
296d7e0c 663 return FALSE;
1a3d5ae9
JV
664 }
665 elseif (!node_access('view', $node) || (!user_access('access printer-friendly version'))) {
872fe2ea 666 // Access is denied
1a3d5ae9 667 drupal_access_denied();
296d7e0c 668 return FALSE;
872fe2ea
JV
669 }
670
671 $tree = book_menu_subtree_data($node->book);
672 $node->body = book_export_traverse($tree, 'book_node_export');
673
dd3ed916
JV
674 //Check URL list settings
675 $_print_urls = _print_url_list_enabled($node, $format);
676
872fe2ea 677 // Convert the a href elements
cc224831
JV
678 $pattern = '!<(a\s[^>]*?)>(.*?)(</a>)!is';
679 $node->body = preg_replace_callback($pattern, '_print_rewrite_urls', $node->body);
872fe2ea 680
ded70136 681 $print = _print_var_generator($node, $message);
4567b6ac
JV
682 $print['content'] = $node->body;
683
872fe2ea 684 // The title is already displayed by the book_recurse, so avoid duplication
cc224831 685 $print['title'] = '';
872fe2ea 686
9c35190f 687 return $print;
872fe2ea 688}