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