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