Issue #938838: move checkbox settings lower
[project/print.git] / print_pdf / print_pdf.module
1 <?php
2
3 /**
4 * @file
5 * Displays Printer-friendly versions of Drupal pages.
6 *
7 * @ingroup print
8 */
9
10 define('PRINTPDF_PATH', 'printpdf');
11
12 // Defined in print.module
13 //define('PRINT_PDF_FORMAT', 'pdf');
14
15 define('PRINT_PDF_LIB_PATH', 'sites/all/libraries');
16
17 define('PRINT_PDF_LINK_POS_DEFAULT', 'link');
18 define('PRINT_PDF_LINK_TEASER_DEFAULT', 0);
19 define('PRINT_PDF_SHOW_LINK_DEFAULT', 1);
20 define('PRINT_PDF_NODE_LINK_VISIBILITY_DEFAULT', 0);
21 define('PRINT_PDF_NODE_LINK_PAGES_DEFAULT', '');
22 define('PRINT_PDF_LINK_CLASS_DEFAULT', 'print-pdf');
23 define('PRINT_PDF_SYS_LINK_VISIBILITY_DEFAULT', 1);
24 define('PRINT_PDF_SYS_LINK_PAGES_DEFAULT', '');
25 define('PRINT_PDF_LINK_USE_ALIAS_DEFAULT', 0);
26 define('PRINT_PDF_BOOK_LINK_DEFAULT', 1);
27 define('PRINT_PDF_PDF_TOOL_DEFAULT', 0);
28 define('PRINT_PDF_CONTENT_DISPOSITION_DEFAULT', 2);
29 define('PRINT_PDF_PAPER_SIZE_DEFAULT', 'A4');
30 define('PRINT_PDF_PAGE_ORIENTATION_DEFAULT', 'portrait');
31 define('PRINT_PDF_AUTOCONFIG_DEFAULT', 1);
32 define('PRINT_PDF_FONT_FAMILY_DEFAULT', 'dejavusans');
33 define('PRINT_PDF_FONT_SIZE_DEFAULT', 10);
34 define('PRINT_PDF_FONT_SUBSETTING_DEFAULT', 0);
35 define('PRINT_PDF_FILENAME_DEFAULT', '[site:name] - [node:title] - [node:changed:custom:Y-m-d]');
36 define('PRINT_PDF_DOMPDF_UNICODE_DEFAULT', 0);
37 define('PRINT_PDF_WKHTMLTOPDF_OPTIONS', "--footer-font-size 7 --footer-right '[page]'");
38
39 /**
40 * Implements hook_permission().
41 */
42 function print_pdf_permission() {
43 return array(
44 'access PDF version' => array(
45 'title' => t('Access the PDF version'),
46 'description' => t('View the PDF versions and the links to them in the original pages.'),
47 ),
48 );
49 }
50
51 /**
52 * Implements hook_theme().
53 */
54 function print_pdf_theme() {
55 return array(
56 'print_pdf_format_link' => array(
57 'variables' => array(),
58 ),
59 'print_pdf_dompdf_footer' => array(
60 'variables' => array('html' => ''),
61 'file' => 'print_pdf.pages.inc',
62 ),
63 'print_pdf_tcpdf_header' => array(
64 'variables' => array('pdf' => NULL, 'html' => '', 'font' => array()),
65 'file' => 'print_pdf.pages.inc',
66 ),
67 'print_pdf_tcpdf_page' => array(
68 'variables' => array('pdf' => NULL),
69 'file' => 'print_pdf.pages.inc',
70 ),
71 'print_pdf_tcpdf_content' => array(
72 'variables' => array('pdf' => NULL, 'html' => '', 'font' => array()),
73 'file' => 'print_pdf.pages.inc',
74 ),
75 'print_pdf_tcpdf_footer' => array(
76 'variables' => array('pdf' => NULL, 'html' => '', 'font' => array()),
77 'file' => 'print_pdf.pages.inc',
78 ),
79 'print_pdf_tcpdf_footer2' => array(
80 'variables' => array('pdf' => NULL),
81 'file' => 'print_pdf.pages.inc',
82 ),
83 );
84 }
85
86 /**
87 * Implements hook_menu().
88 */
89 function print_pdf_menu() {
90 $items = array();
91
92 $items[PRINTPDF_PATH] = array(
93 'title' => 'Printer-friendly PDF',
94 'page callback' => 'print_pdf_controller',
95 'access arguments' => array('access PDF version'),
96 'type' => MENU_CALLBACK,
97 'file' => 'print_pdf.pages.inc',
98 );
99 $items[PRINTPDF_PATH . '/' . PRINTPDF_PATH] = array(
100 'access callback' => FALSE,
101 );
102 $items['admin/config/print/pdf'] = array(
103 'title' => 'PDF',
104 'page callback' => 'drupal_get_form',
105 'page arguments' => array('print_pdf_settings'),
106 'access arguments' => array('administer print'),
107 'weight' => 3,
108 'type' => MENU_LOCAL_TASK,
109 'file' => 'print_pdf.admin.inc',
110 );
111 $items['admin/config/print/pdf/options'] = array(
112 'title' => 'Options',
113 'weight' => 1,
114 'type' => MENU_DEFAULT_LOCAL_TASK,
115 );
116 $items['admin/config/print/pdf/strings'] = array(
117 'title' => 'Text strings',
118 'page callback' => 'drupal_get_form',
119 'page arguments' => array('print_pdf_strings_settings'),
120 'access arguments' => array('administer print'),
121 'weight' => 2,
122 'type' => MENU_LOCAL_TASK,
123 'file' => 'print_pdf.admin.inc',
124 );
125
126 return $items;
127 }
128
129 /**
130 * Implements hook_block_info().
131 */
132 function print_pdf_block_info() {
133 $block['print_pdf-top']['info'] = t('Most PDFd');
134 $block['print_pdf-top']['cache'] = DRUPAL_CACHE_GLOBAL;
135 return $block;
136 }
137
138 /**
139 * Implements hook_block_view().
140 */
141 function print_pdf_block_view($delta = 0) {
142 switch ($delta) {
143 case 'print_pdf-top':
144 $block['subject'] = t('Most PDFd');
145 $result = db_query_range("SELECT path FROM {print_pdf_page_counter} ORDER BY totalcount DESC", 0, 3)
146 ->fetchAll();
147 if (count($result)) {
148 $block['content'] = '<div class="item-list"><ul>';
149 foreach ($result as $obj) {
150 $block['content'] .= '<li>' . l(_print_get_title($obj->path), $obj->path) . '</li>';
151 }
152 $block['content'] .= '</ul></div>';
153 }
154 break;
155 }
156 return $block;
157 }
158
159 /**
160 * Implements hook_requirements().
161 */
162 function print_pdf_requirements($phase) {
163 $requirements = array();
164 $t = get_t();
165 switch ($phase) {
166 // At runtime, make sure that a PDF generation tool is selected
167 case 'runtime':
168 $print_pdf_pdf_tool = variable_get('print_pdf_pdf_tool', PRINT_PDF_PDF_TOOL_DEFAULT);
169 if (empty($print_pdf_pdf_tool)) {
170 $requirements['print_pdf_tool'] = array(
171 'title' => $t('PDF generation library'),
172 'value' => $t('No PDF tool selected'),
173 'description' => $t('Please configure it in the <a href="@url">PDF settings page</a>.', array('@url' => url('admin/config/print/pdf'))),
174 'severity' => REQUIREMENT_ERROR,
175 );
176 }
177 else {
178 if (!is_file($print_pdf_pdf_tool) || !is_readable($print_pdf_pdf_tool)) {
179 $requirements['print_pdf_tool'] = array(
180 'title' => $t('PDF generation tool'),
181 'value' => $t('File not found'),
182 'description' => $t('The currently selected PDF generation library (%file) is no longer accessible.', array('%file' => $print_pdf_pdf_tool)),
183 'severity' => REQUIREMENT_ERROR,
184 );
185 }
186 elseif (basename($print_pdf_pdf_tool) == 'dompdf_config.inc.php') {
187 $directory = dirname($print_pdf_pdf_tool) . '/lib/fonts';
188 if (!is_dir($directory) || !is_writable($directory)) {
189 $requirements['print_pdf_tool'] = array(
190 'title' => $t('DOMPDF font cache directory'),
191 'value' => $t('Non-writable permissions'),
192 'description' => $t('You must change the %fontdir permissions to be writable, as dompdf requires write-access to that directory.', array('%fontdir' => $directory)),
193 'severity' => REQUIREMENT_ERROR,
194 );
195 }
196 }
197 elseif (substr(basename($print_pdf_pdf_tool, '.exe'), 0, 11) == 'wkhtmltopdf') {
198 if (function_exists('is_executable') && !is_executable($print_pdf_pdf_tool)) {
199 $requirements['print_pdf_tool'] = array(
200 'title' => $t('wkhtmltopdf library'),
201 'value' => $t('Non-executable permissions'),
202 'description' => $t('You must modify the permissions of the wkhtmltopdf file (%file) to make it executable.', array('%file' => $print_pdf_pdf_tool)),
203 'severity' => REQUIREMENT_ERROR,
204 );
205 }
206 else {
207 $version = _print_pdf_wkhtmltopdf_version();
208 if (version_compare($version, '0.9.6', '<')) {
209 $requirements['print_pdf_tool'] = array(
210 'title' => $t('wkhtmltopdf library'),
211 'value' => $t('Unsupported version'),
212 'description' => $t('The currently selected version of wkhtmltopdf (@version) is not supported. Please update to a <a href="@url">newer version</a>.', array('@version' => $version, '@url' => url('http://code.google.com/p/wkhtmltopdf/'))),
213 'severity' => REQUIREMENT_ERROR,
214 );
215 }
216 }
217 }
218 }
219 break;
220 }
221 return $requirements;
222 }
223
224 /**
225 * Implements hook_node_view().
226 */
227 function print_pdf_node_view($node, $build_mode) {
228 $print_pdf_link_pos = variable_get('print_pdf_link_pos', array(PRINT_PDF_LINK_POS_DEFAULT => PRINT_PDF_LINK_POS_DEFAULT));
229 $print_pdf_link_use_alias = variable_get('print_pdf_link_use_alias', PRINT_PDF_LINK_USE_ALIAS_DEFAULT);
230 $allowed_type = print_pdf_link_allowed(array('type' => 'node', 'node' => $node, 'teaser' => ($build_mode == 'teaser')));
231 if (($allowed_type) && !empty($print_pdf_link_pos['link'])) {
232 drupal_add_css(drupal_get_path('module', 'print') . '/css/printlinks.css');
233 $links = array();
234 $format = theme('print_pdf_format_link');
235
236 // Show book link
237 if ($allowed_type === PRINT_ALLOW_BOOK_LINK) {
238 $links['book_pdf'] = array('href' => PRINTPDF_PATH . '/book/export/html/' . $node->nid,
239 'title' => $format['text'],
240 'attributes' => $format['attributes'],
241 'html' => $format['html'],
242 );
243 }
244 elseif ($allowed_type === PRINT_ALLOW_NORMAL_LINK) {
245 $query_arr = $_GET;
246 if ('node' == 'comment') {
247 $query_arr['comment'] = $node->cid;
248 }
249 $query = print_query_string_encode($query_arr, array('q'));
250 if (empty($query)) $query = NULL;
251
252 if ($print_pdf_link_use_alias) {
253 $path = drupal_get_path_alias('node/' . $node->nid);
254 }
255 else {
256 $path = $node->nid;
257 }
258
259 $links['print_pdf'] = array('href' => PRINTPDF_PATH . '/' . $path,
260 'title' => $format['text'],
261 'attributes' => $format['attributes'],
262 'html' => $format['html'],
263 'query' => $query,
264 );
265 }
266 $node->content['links']['print_pdf'] = array(
267 '#theme' => 'links',
268 '#links' => $links,
269 '#attributes' => array('class' => array('links', 'inline')),
270 );
271 }
272
273 // Insert content corner links
274 if ((!empty($print_pdf_link_pos['corner'])) && ($build_mode == 'full')) {
275 $node->content['print_links']['#markup'] .= print_pdf_insert_link(NULL, $node);
276 }
277 }
278
279 /**
280 * Implements hook_help().
281 */
282 function print_pdf_help($path, $arg) {
283 $print_pdf_link_pos = variable_get('print_pdf_link_pos', array(PRINT_PDF_LINK_POS_DEFAULT => PRINT_PDF_LINK_POS_DEFAULT));
284 if (($path !== 'node/%') && !(empty($print_pdf_link_pos['link']) && empty($print_pdf_link_pos['corner']))) {
285 static $output = FALSE;
286
287 if ($output === FALSE) {
288 $output = TRUE;
289
290 $link = print_pdf_insert_link();
291 if ($link) {
292 return "<span class='print-syslink'>$link</span>";
293 }
294 }
295 }
296 }
297
298 /**
299 * Implements hook_node_load().
300 */
301 function print_pdf_node_load($nodes, $types) {
302 foreach ($nodes as $node) {
303 _print_pdf_set_node_fields($node);
304 }
305 }
306
307 /**
308 * Implements hook_node_insert().
309 */
310 function print_pdf_node_insert($node) {
311 if (user_access('administer print') || user_access('node-specific print configuration')) {
312 if ($node->print_pdf_display === NULL) $node->print_pdf_display = variable_get('print_pdf_display_' . $node->type, PRINT_TYPE_SHOW_LINK_DEFAULT);
313 if ($node->print_pdf_display_comment === NULL) $node->print_pdf_display_comment = variable_get('print_pdf_display_comment_' . $node->type, PRINT_TYPE_COMMENT_LINK_DEFAULT);
314 if ($node->print_pdf_display_urllist === NULL) $node->print_pdf_display_urllist = variable_get('print_pdf_display_urllist_' . $node->type, PRINT_TYPE_URLLIST_DEFAULT);
315
316 _print_pdf_node_conf_modify($node->nid, $node->print_pdf_display, $node->print_pdf_display_comment, $node->print_pdf_display_urllist);
317 }
318 }
319
320 /**
321 * Implements hook_node_update().
322 */
323 function print_pdf_node_update($node) {
324 if (user_access('administer print') || user_access('node-specific print configuration')) {
325 if ($node->print_pdf_display === NULL) $node->print_pdf_display = variable_get('print_pdf_display_' . $node->type, PRINT_TYPE_SHOW_LINK_DEFAULT);
326 if ($node->print_pdf_display_comment === NULL) $node->print_pdf_display_comment = variable_get('print_pdf_display_comment_' . $node->type, PRINT_TYPE_COMMENT_LINK_DEFAULT);
327 if ($node->print_pdf_display_urllist === NULL) $node->print_pdf_display_urllist = variable_get('print_pdf_display_urllist_' . $node->type, PRINT_TYPE_URLLIST_DEFAULT);
328
329 _print_pdf_node_conf_modify($node->nid, $node->print_pdf_display, $node->print_pdf_display_comment, $node->print_pdf_display_urllist);
330 }
331 }
332
333 /**
334 * Implements hook_node_delete().
335 */
336 function print_pdf_node_delete($node) {
337 db_delete('print_pdf_node_conf')
338 ->condition('nid', $node->nid)
339 ->execute();
340 db_delete('print_pdf_page_counter')
341 ->condition('path', 'node/' . $node->nid)
342 ->execute();
343 }
344
345 /**
346 * Implements hook_form_alter().
347 */
348 function print_pdf_form_alter(&$form, &$form_state, $form_id) {
349 // Add the node-type settings option to activate the PDF version link
350 if ((user_access('administer print') || user_access('node-specific print configuration')) &&
351 (($form_id == 'node_type_form') || !empty($form['#node_edit_form']))) {
352 $form['print']['pdf_label'] = array(
353 '#type' => 'markup',
354 '#markup' => '<p><strong>' . t('PDF version') . '</strong></p>',
355 );
356
357 $form['print']['print_pdf_display'] = array(
358 '#type' => 'checkbox',
359 '#title' => t('Show link'),
360 );
361 $form['print']['print_pdf_display_comment'] = array(
362 '#type' => 'checkbox',
363 '#title' => t('Show link in individual comments'),
364 );
365 $form['print']['print_pdf_display_urllist'] = array(
366 '#type' => 'checkbox',
367 '#title' => t('Show Printer-friendly URLs list'),
368 );
369
370 if ($form_id == 'node_type_form') {
371 $form['print']['print_pdf_display']['#default_value'] = variable_get('print_pdf_display_' . $form['#node_type']->type, PRINT_TYPE_SHOW_LINK_DEFAULT);
372 $form['print']['print_pdf_display_comment']['#default_value'] = variable_get('print_pdf_display_comment_' . $form['#node_type']->type, PRINT_TYPE_COMMENT_LINK_DEFAULT);
373 $form['print']['print_pdf_display_urllist']['#default_value'] = variable_get('print_pdf_display_urllist_' . $form['#node_type']->type, PRINT_TYPE_URLLIST_DEFAULT);
374 }
375 else {
376 $node = $form['#node'];
377 $form['print']['print_pdf_display']['#default_value'] = isset($node->print_pdf_display) ? $node->print_pdf_display : variable_get('print_pdf_display_' . $node->type, PRINT_TYPE_SHOW_LINK_DEFAULT);
378 $form['print']['print_pdf_display_comment']['#default_value'] = isset($node->print_pdf_display_comment) ? $node->print_pdf_display_comment : variable_get('print_pdf_display_comment_' . $node->type, PRINT_TYPE_COMMENT_LINK_DEFAULT);
379 $form['print']['print_pdf_display_urllist']['#default_value'] = isset($node->print_pdf_display_urllist) ? $node->print_pdf_display_urllist : variable_get('print_pdf_display_urllist_' . $node->type, PRINT_TYPE_URLLIST_DEFAULT);
380 }
381 }
382 }
383
384 /**
385 * Auxiliary function to assign the per-node settings to the node object fields
386 *
387 * @param $node
388 * node to be modified
389 */
390 function _print_pdf_set_node_fields(&$node) {
391 if (isset($node->nid)) {
392 $res = db_query("SELECT link, comments, url_list FROM {print_pdf_node_conf} WHERE nid = :nid", array(':nid' => $node->nid))
393 ->fetch();
394 }
395 else {
396 $res = FALSE;
397 }
398 $node->print_pdf_display = $res ? intval($res->link) : variable_get('print_pdf_display_' . $node->type, PRINT_TYPE_SHOW_LINK_DEFAULT);
399 $node->print_pdf_display_comment = $res ? intval($res->comments) : variable_get('print_pdf_display_comment_' . $node->type, PRINT_TYPE_COMMENT_LINK_DEFAULT);
400 $node->print_pdf_display_urllist = $res ? intval($res->url_list) : variable_get('print_pdf_display_urllist_' . $node->type, PRINT_TYPE_URLLIST_DEFAULT);
401 }
402
403 /**
404 * Update the print_pdf_node_conf table to reflect the given attributes
405 * If updating to the default values, delete the record.
406 *
407 * @param $nid
408 * value of the nid field (primary key)
409 * @param $link
410 * value of the link field (0 or 1)
411 * @param $comments
412 * value of the comments field (0 or 1)
413 * @param $url_list
414 * value of the url_list field (0 or 1)
415 */
416 function _print_pdf_node_conf_modify($nid, $link, $comments, $url_list) {
417 db_merge('print_pdf_node_conf')
418 ->key(array('nid' => $nid))
419 ->fields(array(
420 'link' => $link,
421 'comments' => $comments,
422 'url_list' => $url_list,
423 ))
424 ->execute();
425 }
426
427 /**
428 * Format the PDF version link
429 *
430 * @return
431 * array of formatted attributes
432 * @ingroup themeable
433 */
434 function theme_print_pdf_format_link() {
435 $print_pdf_link_class = variable_get('print_pdf_link_class', PRINT_PDF_LINK_CLASS_DEFAULT);
436 $print_pdf_content_disposition = variable_get('print_pdf_content_disposition', PRINT_PDF_CONTENT_DISPOSITION_DEFAULT);
437 $print_pdf_show_link = variable_get('print_pdf_show_link', PRINT_PDF_SHOW_LINK_DEFAULT);
438 $print_pdf_link_text = filter_xss(variable_get('print_pdf_link_text', t('PDF version')));
439
440 $img = drupal_get_path('module', 'print') . '/icons/pdf_icon.gif';
441 $title = t('Display a PDF version of this page.');
442 $class = strip_tags($print_pdf_link_class);
443 $new_window = ($print_pdf_content_disposition == 1);
444 $format = _print_format_link_aux($print_pdf_show_link, $print_pdf_link_text, $img);
445
446 return array('text' => $format['text'],
447 'html' => $format['html'],
448 'attributes' => print_fill_attributes($title, $class, $new_window),
449 );
450 }
451
452 /**
453 * Auxiliary function to display a formatted PDF version link
454 *
455 * Function made available so that developers may call this function from
456 * their defined pages/blocks.
457 *
458 * @param $path
459 * path of the original page (optional). If not specified, the current URL
460 * is used
461 * @param $node
462 * an optional node object, to be used in defining the path, if used, the
463 * path argument is irrelevant
464 * @return
465 * string with the HTML link to the printer-friendly page
466 */
467 function print_pdf_insert_link($path = NULL, $node = NULL) {
468 if ($node !== NULL) {
469 $nid = $node->nid;
470 $path = 'node/' . $nid;
471 $allowed_type = print_pdf_link_allowed(array('node' => $node));
472 }
473 else {
474 if ($path === NULL) {
475 $nid = preg_replace('!^node/!', '', $_GET['q']);
476 $path = $_GET['q'];
477 }
478 else {
479 $nid = NULL;
480 }
481 $allowed_type = print_pdf_link_allowed(array('path' => $path));
482 }
483
484 if ($allowed_type) {
485 if ($nid !== NULL) {
486 if ($allowed_type === PRINT_ALLOW_BOOK_LINK) {
487 $path = 'book/export/html/' . $nid;
488 }
489 else {
490 if (variable_get('print_pdf_link_use_alias', PRINT_PDF_LINK_USE_ALIAS_DEFAULT)) {
491 $path = drupal_get_path_alias($path);
492 }
493 else {
494 $path = $nid;
495 }
496 }
497 $path = PRINTPDF_PATH . '/' . $path;
498 $query = print_query_string_encode($_GET, array('q'));
499 if (empty($query)) {
500 $query = NULL;
501 }
502 }
503 else {
504 $query = NULL;
505 }
506 drupal_add_css(drupal_get_path('module', 'print') . '/css/printlinks.css');
507 $format = theme('print_pdf_format_link');
508 return '<span class="print_pdf">' . l($format['text'], $path, array('attributes' => $format['attributes'], 'query' => $query, 'absolute' => TRUE, 'html' => $format['html'])) . '</span>';
509 }
510 else {
511 return FALSE;
512 }
513 }
514
515 /**
516 * Determine a the link to the PDF version is allowed depending on all possible settings
517 *
518 * @param $args
519 * array containing the possible parameters:
520 * teaser, node, type, path
521 * @return
522 * FALSE if not allowed
523 * PRINT_ALLOW_NORMAL_LINK if a normal link is allowed
524 * PRINT_ALLOW_BOOK_LINK if a link is allowed in a book node
525 */
526 function print_pdf_link_allowed($args) {
527 $print_pdf_pdf_tool = variable_get('print_pdf_pdf_tool', PRINT_PDF_PDF_TOOL_DEFAULT);
528 if ((!empty($args['teaser']) && !variable_get('print_pdf_link_teaser', PRINT_PDF_LINK_TEASER_DEFAULT))
529 || !user_access('access PDF version') || (empty($print_pdf_pdf_tool))) {
530 // If the teaser link is disabled or the user is not allowed
531 return FALSE;
532 }
533 if (!empty($args['path'])) {
534 $nid = preg_replace('!^node/!', '', drupal_get_normal_path($args['path']));
535 if (is_numeric($nid)) {
536 $args['node'] = node_load($nid);
537 }
538 }
539 if (!empty($args['node'])) {
540 static $node_type = FALSE;
541
542 $node = $args['node'];
543 if (isset($node->type)) {
544 $node_type = $node->type;
545 }
546 // Node
547 $print_pdf_node_link_visibility = variable_get('print_pdf_node_link_visibility', PRINT_PDF_NODE_LINK_VISIBILITY_DEFAULT);
548 $print_pdf_node_link_pages = variable_get('print_pdf_node_link_pages', PRINT_PDF_NODE_LINK_PAGES_DEFAULT);
549
550 if (!_print_page_match($print_pdf_node_link_visibility, "node/" . $node->nid, $print_pdf_node_link_pages)) {
551 // Page not in visibility list
552 return FALSE;
553 }
554 elseif (isset($args['type']) && ($args['type'] == 'comment') && isset($node_type)) {
555 // Link is for a comment, return the configured setting
556 $res = db_query("SELECT comments FROM {print_pdf_node_conf} WHERE nid = :nid", array(':nid' => $node->nid))
557 ->fetch();
558 $print_display_comment = $res ? intval($res->comments) : variable_get('print_pdf_display_comment_' . $node_type, PRINT_TYPE_COMMENT_LINK_DEFAULT);
559 if ($print_display_comment) {
560 return PRINT_ALLOW_NORMAL_LINK;
561 }
562 }
563 else {
564 // Node link
565 if (!$node->print_pdf_display) {
566 // Link for this node is disabled
567 return FALSE;
568 }
569 elseif (isset($node->book)) {
570 // Node is a book;
571 $print_pdf_book_link = variable_get('print_pdf_book_link', PRINT_PDF_BOOK_LINK_DEFAULT);
572 switch ($print_pdf_book_link) {
573 case 1:
574 if (user_access('access printer-friendly version')) {
575 return PRINT_ALLOW_BOOK_LINK;
576 }
577 break;
578 case 2:
579 return PRINT_ALLOW_NORMAL_LINK;
580 }
581 }
582 else {
583 return PRINT_ALLOW_NORMAL_LINK;
584 }
585 }
586 }
587 else {
588 // 'System' page
589 $print_pdf_sys_link_visibility = variable_get('print_pdf_sys_link_visibility', PRINT_PDF_SYS_LINK_VISIBILITY_DEFAULT);
590 $print_pdf_sys_link_pages = variable_get('print_pdf_sys_link_pages', PRINT_PDF_SYS_LINK_PAGES_DEFAULT);
591
592 return _print_page_match($print_pdf_sys_link_visibility, $_GET['q'], $print_pdf_sys_link_pages);
593 }
594 return FALSE;
595 }
596
597 function _print_pdf_wkhtmltopdf_version() {
598 $print_pdf_pdf_tool = variable_get('print_pdf_pdf_tool', PRINT_PDF_PDF_TOOL_DEFAULT);
599 $descriptor = array(0 => array('pipe', 'r'), 1 => array('pipe', 'w'), 2 => array('pipe', 'w'));
600
601 $cmd = realpath($print_pdf_pdf_tool) . ' --version';
602 $process = proc_open($cmd, $descriptor, $pipes, NULL, NULL);
603 if (is_resource($process)) {
604 $out = preg_match('!.*?(\d+\.\d+\.\d+).*$!m', stream_get_contents($pipes[1]), $matches);
605 fclose($pipes[0]);
606 fclose($pipes[1]);
607 fclose($pipes[2]);
608 $retval = proc_terminate($process);
609 }
610
611 return ($matches[1]);
612 }