Issue #1191596: don't display links block when the user can't access the node.
[project/print.git] / print_pdf / print_pdf.module
CommitLineData
9c35190f 1<?php
9c35190f
JV
2
3/**
4 * @file
7934fadc 5 * Displays Printer-friendly versions of Drupal pages.
2b011462
JV
6 *
7 * @ingroup print
9c35190f
JV
8 */
9
cc224831 10define('PRINTPDF_PATH', 'printpdf');
9c35190f 11
dd3ed916
JV
12define('PRINT_PDF_FORMAT', 'pdf');
13
753bb4b0
JV
14define('PRINT_PDF_LIB_PATH', 'sites/all/libraries');
15
cc224831 16define('PRINT_PDF_LINK_POS_DEFAULT', 'link');
e725fb94 17define('PRINT_PDF_LINK_TEASER_DEFAULT', 0);
cc224831
JV
18define('PRINT_PDF_SHOW_LINK_DEFAULT', 1);
19define('PRINT_PDF_NODE_LINK_VISIBILITY_DEFAULT', 0);
20define('PRINT_PDF_NODE_LINK_PAGES_DEFAULT', '');
21define('PRINT_PDF_LINK_CLASS_DEFAULT', 'print-pdf');
22define('PRINT_PDF_SYS_LINK_VISIBILITY_DEFAULT', 1);
a1057bea 23define('PRINT_PDF_SYS_LINK_PAGES_DEFAULT', '');
16082a66 24define('PRINT_PDF_LINK_USE_ALIAS_DEFAULT', 0);
cc224831
JV
25define('PRINT_PDF_BOOK_LINK_DEFAULT', 1);
26define('PRINT_PDF_PDF_TOOL_DEFAULT', 0);
27define('PRINT_PDF_CONTENT_DISPOSITION_DEFAULT', 2);
28define('PRINT_PDF_PAPER_SIZE_DEFAULT', 'A4');
29define('PRINT_PDF_PAGE_ORIENTATION_DEFAULT', 'portrait');
ff7fd37e 30define('PRINT_PDF_AUTOCONFIG_DEFAULT', 1);
e7fb3470
JV
31define('PRINT_PDF_FONT_FAMILY_DEFAULT', 'dejavusans');
32define('PRINT_PDF_FONT_SIZE_DEFAULT', 10);
2403d9c4 33define('PRINT_PDF_FONT_SUBSETTING_DEFAULT', 0);
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(
9f09f6c9 168 'title' => $t('PDF generation library'),
ea946e81
JV
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 174 else {
9f09f6c9
JV
175 if (!is_file($print_pdf_pdf_tool) || !is_readable($print_pdf_pdf_tool)) {
176 $requirements['print_pdf_tool'] = array(
177 'title' => $t('PDF generation tool'),
178 'value' => $t('File not found'),
179 'description' => $t('The currently selected PDF generation library (%file) is no longer accessible.', array('%file' => $print_pdf_pdf_tool)),
180 'severity' => REQUIREMENT_ERROR,
181 );
182 }
183 elseif (basename($print_pdf_pdf_tool) == 'dompdf_config.inc.php') {
4a6ca975
JV
184 $directory = dirname($print_pdf_pdf_tool) .'/lib/fonts';
185 if (!is_dir($directory) || !is_writable($directory)) {
186 $requirements['print_pdf_tool'] = array(
187 'title' => $t('DOMPDF font cache directory'),
188 'value' => $t('Non-writable permissions'),
189 'description' => $t('You must change the %fontdir permissions to be writable, as dompdf requires write-access to that directory.', array('%fontdir' => $directory)),
190 'severity' => REQUIREMENT_ERROR,
191 );
192 }
193 }
c45f1fed
JV
194 elseif (basename($print_pdf_pdf_tool) == 'tcpdf.php') {
195 $version = _print_pdf_tcpdf_version();
196
197 if (version_compare($version, '5.9.012', '<')) {
198 $requirements['print_pdf_tool'] = array(
199 'title' => $t('TCPDF library'),
200 'value' => $t('Unsupported version'),
201 'description' => $t('The currently selected version of TCPDF (@version) is not supported. Please update to a <a href="@url">newer version</a>.', array('@version' => $version, '@url' => url('http://www.tcpdf.org'))),
202 'severity' => REQUIREMENT_ERROR,
203 );
204 }
205 }
a70bf289 206 elseif (substr(basename($print_pdf_pdf_tool, '.exe'), 0, 11) == 'wkhtmltopdf') {
9f09f6c9 207 if (function_exists('is_executable') && !is_executable($print_pdf_pdf_tool)) {
a70bf289 208 $requirements['print_pdf_tool'] = array(
9f09f6c9
JV
209 'title' => $t('wkhtmltopdf library'),
210 'value' => $t('Non-executable permissions'),
211 'description' => $t('You must modify the permissions of the wkhtmltopdf file (%file) to make it executable.', array('%file' => $print_pdf_pdf_tool)),
a70bf289
JV
212 'severity' => REQUIREMENT_ERROR,
213 );
214 }
9f09f6c9
JV
215 else {
216 $version = _print_pdf_wkhtmltopdf_version();
217 if (version_compare($version, '0.9.6', '<')) {
218 $requirements['print_pdf_tool'] = array(
219 'title' => $t('wkhtmltopdf library'),
220 'value' => $t('Unsupported version'),
221 '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/'))),
222 'severity' => REQUIREMENT_ERROR,
223 );
224 }
225 }
a70bf289 226 }
4a6ca975 227 }
8d3c5d9d
JV
228 break;
229 }
230 return $requirements;
231}
232
233/**
9c35190f
JV
234 * Implementation of hook_link().
235 */
236function print_pdf_link($type, $node = NULL, $teaser = FALSE) {
cc224831 237 $print_pdf_link_pos = variable_get('print_pdf_link_pos', array(PRINT_PDF_LINK_POS_DEFAULT => PRINT_PDF_LINK_POS_DEFAULT));
16082a66 238 $print_pdf_link_use_alias = variable_get('print_pdf_link_use_alias', PRINT_PDF_LINK_USE_ALIAS_DEFAULT);
cc224831
JV
239 $allowed_type = print_pdf_link_allowed(array('type' => $type, 'node' => $node, 'teaser' => $teaser));
240 if (($allowed_type) && !empty($print_pdf_link_pos['link'])) {
76faa6b6 241 drupal_add_css(drupal_get_path('module', 'print') .'/css/printlinks.css');
9c35190f 242 $links = array();
9c35190f 243 $format = theme('print_pdf_format_link');
3704b344 244
77d2b585 245 // Show book link
cc224831
JV
246 if ($allowed_type === PRINT_ALLOW_BOOK_LINK) {
247 $links['book_pdf'] = array('href' => PRINTPDF_PATH .'/book/export/html/'. $node->nid,
1c493781 248 'title' => $format['text'],
77d2b585 249 'attributes' => $format['attributes'],
1c493781 250 'html' => $format['html'],
f163b6a0 251 );
77d2b585
JV
252
253 return $links;
9c35190f 254 }
cc224831 255 elseif ($allowed_type === PRINT_ALLOW_NORMAL_LINK) {
77d2b585
JV
256 $query_arr = $_GET;
257 if ($type == 'comment') {
258 $query_arr['comment'] = $node->cid;
259 }
cc224831 260 $query = print_query_string_encode($query_arr, array('q'));
0e51bae4 261 if (empty($query)) $query = NULL;
9c35190f 262
16082a66
JV
263 if ($print_pdf_link_use_alias) {
264 $path = drupal_get_path_alias('node/'. $node->nid);
265 }
266 else {
267 $path = $node->nid;
268 }
269
270 $links['print_pdf'] = array('href' => PRINTPDF_PATH .'/'. $path,
77d2b585
JV
271 'title' => $format['text'],
272 'attributes' => $format['attributes'],
273 'html' => $format['html'],
274 'query' => $query,
f163b6a0 275 );
77d2b585
JV
276
277 return $links;
278 }
9c35190f 279 }
7934fadc
JV
280 else {
281 return;
282 }
9c35190f
JV
283}
284
285/**
e8517dd5 286 * Implementation of hook_help().
9c35190f
JV
287 */
288function print_pdf_help($path, $arg) {
cc224831 289 $print_pdf_link_pos = variable_get('print_pdf_link_pos', array(PRINT_PDF_LINK_POS_DEFAULT => PRINT_PDF_LINK_POS_DEFAULT));
43e7b5ef 290 if (($path !== 'node/%') && !(empty($print_pdf_link_pos['link']) && empty($print_pdf_link_pos['corner']))) {
9c35190f
JV
291 static $output = FALSE;
292
293 if ($output === FALSE) {
294 $output = TRUE;
7934fadc
JV
295
296 $link = print_pdf_insert_link();
297 if ($link) {
298 return "<span class='print-syslink'>$link</span>";
299 }
9c35190f
JV
300 }
301 }
302}
303
304/**
cc224831
JV
305 * Implementation of hook_nodeapi().
306 */
307function print_pdf_nodeapi(&$node, $op = 'view', $teaser, $page) {
308 switch ($op) {
309 case 'view':
dd3ed916 310 // Insert content corner links
cc224831 311 $print_pdf_link_pos = variable_get('print_pdf_link_pos', array(PRINT_PDF_LINK_POS_DEFAULT => PRINT_PDF_LINK_POS_DEFAULT));
7934fadc 312 if (($teaser === FALSE) && !empty($print_pdf_link_pos['corner']) &&
1b3133e4 313 isset($node->build_mode) && ($node->build_mode === NODE_BUILD_NORMAL)) {
ba3d1a0e 314 $node->content['print_links']['#value'] .= print_pdf_insert_link(NULL, $node);
cc224831 315 }
11415979 316 break;
ac3e43b3 317 case 'load':
11415979 318 _print_pdf_set_node_fields($node);
dd3ed916 319 break;
c63485b5 320 case 'insert':
dd3ed916 321 case 'update':
1523fcbc 322 if (user_access('administer print') || user_access('node-specific print configuration')) {
40a8243b
JV
323 if ($node->print_pdf_display === NULL) $node->print_pdf_display = variable_get('print_pdf_display_'. $node->type, PRINT_TYPE_SHOW_LINK_DEFAULT);
324 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);
325 if ($node->print_pdf_display_urllist === NULL) $node->print_pdf_display_urllist = variable_get('print_pdf_display_urllist_'. $node->type, PRINT_TYPE_URLLIST_DEFAULT);
9432e204
JV
326
327 _print_pdf_node_conf_modify($node->nid, $node->print_pdf_display, $node->print_pdf_display_comment, $node->print_pdf_display_urllist);
1523fcbc 328 }
dd3ed916
JV
329 break;
330 case 'delete':
331 db_query("DELETE FROM {print_pdf_node_conf} WHERE nid = %d", $node->nid);
440cb593 332 db_query("DELETE FROM {print_pdf_page_counter} WHERE path = 'node/%d'", $node->nid);
dd3ed916 333 break;
cc224831
JV
334 }
335}
336
337/**
e8517dd5 338 * Implementation of hook_form_alter().
9c35190f
JV
339 */
340function print_pdf_form_alter(&$form, $form_state, $form_id) {
4ed2260c 341 // Add the node-type settings option to activate the PDF version link
38df3d6a
JV
342 if ((user_access('administer print') || user_access('node-specific print configuration')) && (($form_id == 'node_type_form') ||
343 (isset($form['type']) && isset($form['#node']) && $form['type']['#value'] .'_node_form' == $form_id))) {
dd3ed916
JV
344 $form['print']['pdf_label'] = array(
345 '#type' => 'markup',
346 '#value' => '<p><strong>'. t('PDF version') .'</strong></p>',
347 );
348
8d3c5d9d 349 $form['print']['print_pdf_display'] = array(
9c35190f 350 '#type' => 'checkbox',
dd3ed916 351 '#title' => t('Show link'),
9c35190f 352 );
8d3c5d9d 353 $form['print']['print_pdf_display_comment'] = array(
9c35190f 354 '#type' => 'checkbox',
dd3ed916 355 '#title' => t('Show link in individual comments'),
9c35190f 356 );
dd3ed916
JV
357 $form['print']['print_pdf_display_urllist'] = array(
358 '#type' => 'checkbox',
359 '#title' => t('Show Printer-friendly URLs list'),
360 );
361
362 if ($form_id == 'node_type_form') {
363 $form['print']['print_pdf_display']['#default_value'] = variable_get('print_pdf_display_'. $form['#node_type']->type, PRINT_TYPE_SHOW_LINK_DEFAULT);
364 $form['print']['print_pdf_display_comment']['#default_value'] = variable_get('print_pdf_display_comment_'. $form['#node_type']->type, PRINT_TYPE_COMMENT_LINK_DEFAULT);
365 $form['print']['print_pdf_display_urllist']['#default_value'] = variable_get('print_pdf_display_urllist_'. $form['#node_type']->type, PRINT_TYPE_URLLIST_DEFAULT);
366 }
367 else {
b9615c27 368 $node = $form['#node'];
395b324c
JV
369 $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);
370 $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);
371 $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
372 }
373 }
374}
375
376/**
11415979
JV
377 * Auxiliary function to assign the per-node settings to the node object fields
378 *
379 * @param $node
380 * node to be modified
381 */
382function _print_pdf_set_node_fields(&$node) {
e26e64e6
JV
383 if (isset($node->nid)) {
384 $res = db_fetch_object(db_query("SELECT link, comments, url_list FROM {print_pdf_node_conf} WHERE nid = %d", $node->nid));
385 }
386 else {
387 $res = FALSE;
388 }
395b324c
JV
389 $node->print_pdf_display = $res ? intval($res->link) : variable_get('print_pdf_display_'. $node->type, PRINT_TYPE_SHOW_LINK_DEFAULT);
390 $node->print_pdf_display_comment = $res ? intval($res->comments) : variable_get('print_pdf_display_comment_'. $node->type, PRINT_TYPE_COMMENT_LINK_DEFAULT);
391 $node->print_pdf_display_urllist = $res ? intval($res->url_list) : variable_get('print_pdf_display_urllist_'. $node->type, PRINT_TYPE_URLLIST_DEFAULT);
11415979
JV
392}
393
394/**
dd3ed916
JV
395 * Update the print_pdf_node_conf table to reflect the given attributes
396 * If updating to the default values, delete the record.
397 *
398 * @param $nid
399 * value of the nid field (primary key)
400 * @param $link
401 * value of the link field (0 or 1)
402 * @param $comments
403 * value of the comments field (0 or 1)
404 * @param $url_list
405 * value of the url_list field (0 or 1)
406 */
407function _print_pdf_node_conf_modify($nid, $link, $comments, $url_list) {
dd3ed916
JV
408 db_query("UPDATE {print_pdf_node_conf} SET link = %d, comments = %d, url_list = %d WHERE nid = %d", $link, $comments, $url_list, $nid);
409 if (!db_affected_rows()) {
ed1cea24 410 @db_query("INSERT INTO {print_pdf_node_conf} (nid, link, comments, url_list) VALUES (%d, %d, %d, %d)", $nid, $link, $comments, $url_list);
dd3ed916 411 }
9c35190f
JV
412}
413
77d2b585
JV
414/**
415 * Format the PDF version link
416 *
417 * @return
418 * array of formatted attributes
419 * @ingroup themeable
420 */
9c35190f 421function theme_print_pdf_format_link() {
cc224831
JV
422 $print_pdf_link_class = variable_get('print_pdf_link_class', PRINT_PDF_LINK_CLASS_DEFAULT);
423 $print_pdf_content_disposition = variable_get('print_pdf_content_disposition', PRINT_PDF_CONTENT_DISPOSITION_DEFAULT);
424 $print_pdf_show_link = variable_get('print_pdf_show_link', PRINT_PDF_SHOW_LINK_DEFAULT);
d9482703 425 $print_pdf_link_text = filter_xss(variable_get('print_pdf_link_text', t('PDF version')));
cc224831 426
ded70136 427 $img = drupal_get_path('module', 'print') .'/icons/pdf_icon.gif';
67026c64 428 $title = t('Display a PDF version of this page.');
cc224831
JV
429 $class = strip_tags($print_pdf_link_class);
430 $new_window = ($print_pdf_content_disposition == 1);
cff875bc 431 $format = _print_format_link_aux($print_pdf_show_link, $print_pdf_link_text, $img);
67026c64
JV
432
433 return array('text' => $format['text'],
434 'html' => $format['html'],
77d2b585 435 'attributes' => print_fill_attributes($title, $class, $new_window),
e8517dd5 436 );
9c35190f
JV
437}
438
439/**
7934fadc 440 * Auxiliary function to display a formatted PDF version link
9c35190f 441 *
7934fadc
JV
442 * Function made available so that developers may call this function from
443 * their defined pages/blocks.
444 *
445 * @param $path
446 * path of the original page (optional). If not specified, the current URL
447 * is used
448 * @param $node
449 * an optional node object, to be used in defining the path, if used, the
450 * path argument is irrelevant
451 * @return
452 * string with the HTML link to the printer-friendly page
9c35190f 453 */
7934fadc
JV
454function print_pdf_insert_link($path = NULL, $node = NULL) {
455 if ($node !== NULL) {
456 $nid = $node->nid;
457 $path = 'node/'. $nid;
458 $allowed_type = print_pdf_link_allowed(array('node' => $node));
459 }
460 else {
9c35190f 461 if ($path === NULL) {
7934fadc
JV
462 $nid = preg_replace('!^node/!', '', $_GET['q']);
463 $path = $_GET['q'];
464 }
465 else {
466 $nid = NULL;
467 }
468 $allowed_type = print_pdf_link_allowed(array('path' => $path));
469 }
16082a66 470
7934fadc
JV
471 if ($allowed_type) {
472 if ($nid !== NULL) {
473 if ($allowed_type === PRINT_ALLOW_BOOK_LINK) {
474 $path = 'book/export/html/'. $nid;
16082a66
JV
475 }
476 else {
7934fadc
JV
477 if (variable_get('print_pdf_link_use_alias', PRINT_PDF_LINK_USE_ALIAS_DEFAULT)) {
478 $path = drupal_get_path_alias($path);
479 }
480 else {
481 $path = $nid;
482 }
16082a66
JV
483 }
484 $path = PRINTPDF_PATH .'/'. $path;
cc224831 485 $query = print_query_string_encode($_GET, array('q'));
9c35190f
JV
486 if (empty($query)) {
487 $query = NULL;
488 }
489 }
998eb98b
JV
490 else {
491 $query = NULL;
492 }
7934fadc 493 drupal_add_css(drupal_get_path('module', 'print') .'/css/printlinks.css');
9c35190f 494 $format = theme('print_pdf_format_link');
cc224831
JV
495 return '<span class="print_pdf">'. l($format['text'], $path, array('attributes' => $format['attributes'], 'query' => $query, 'absolute' => TRUE, 'html' => $format['html'])) .'</span>';
496 }
7934fadc
JV
497 else {
498 return FALSE;
499 }
cc224831
JV
500}
501
502/**
503 * Determine a the link to the PDF version is allowed depending on all possible settings
504 *
505 * @param $args
506 * array containing the possible parameters:
507 * teaser, node, type, path
508 * @return
509 * FALSE if not allowed
510 * PRINT_ALLOW_NORMAL_LINK if a normal link is allowed
511 * PRINT_ALLOW_BOOK_LINK if a link is allowed in a book node
512 */
513function print_pdf_link_allowed($args) {
e7fb3470 514 $print_pdf_pdf_tool = variable_get('print_pdf_pdf_tool', PRINT_PDF_PDF_TOOL_DEFAULT);
e725fb94
JV
515 if ((!empty($args['teaser']) && !variable_get('print_pdf_link_teaser', PRINT_PDF_LINK_TEASER_DEFAULT))
516 || !user_access('access PDF version') || (empty($print_pdf_pdf_tool))) {
517 // If the teaser link is disabled or the user is not allowed
cc224831
JV
518 return FALSE;
519 }
353f0d71 520 $is_view = FALSE;
7934fadc
JV
521 if (!empty($args['path'])) {
522 $nid = preg_replace('!^node/!', '', drupal_get_normal_path($args['path']));
deb5b942 523 if (ctype_digit($nid)) {
d21a1360 524 $args['node'] = node_load($nid);
7934fadc 525 }
353f0d71
JV
526 else { // Not a node - is it a view?
527 $router_item = menu_get_item($args['path']);
528 $is_view = ($router_item['access_callback'] == 'views_access');
529 }
7934fadc 530 }
5fc86047 531 if (!empty($args['node'])) {
cc224831 532 static $node_type = FALSE;
9eb5cf31
JV
533
534 $node = $args['node'];
353f0d71
JV
535 if (!empty($node->view) // node is inside view
536 && !variable_get('print_pdf_view_contained_nodes_link_visibility', 0)) {
537 return FALSE;
538 }
5236869f
JV
539 if (isset($node->type)) {
540 $node_type = $node->type;
cc224831 541 }
353f0d71 542
cc224831
JV
543 // Node
544 $print_pdf_node_link_visibility = variable_get('print_pdf_node_link_visibility', PRINT_PDF_NODE_LINK_VISIBILITY_DEFAULT);
545 $print_pdf_node_link_pages = variable_get('print_pdf_node_link_pages', PRINT_PDF_NODE_LINK_PAGES_DEFAULT);
546
371610ef 547 if (!_print_page_match($print_pdf_node_link_visibility, "node/". $node->nid, $print_pdf_node_link_pages)) {
440cb593 548 // Page not in visibility list
cc224831
JV
549 return FALSE;
550 }
5fc86047 551 elseif (isset($args['type']) && ($args['type'] == 'comment') && isset($node_type)) {
cc224831 552 // Link is for a comment, return the configured setting
05f76a66
JV
553 // Cache this statically to avoid duplicate queries for every comment.
554 static $res = array();
555 if (!isset($res[$node->nid])) {
556 $res[$node->nid] = db_fetch_object(db_query("SELECT comments FROM {print_pdf_node_conf} WHERE nid = %d", $node->nid));
557 }
558 $print_display_comment = $res ? intval($res[$node->nid]->comments) : variable_get('print_pdf_display_comment_'. $node_type, PRINT_TYPE_COMMENT_LINK_DEFAULT);
395b324c 559 if ($print_display_comment) {
dd3ed916
JV
560 return PRINT_ALLOW_NORMAL_LINK;
561 }
cc224831
JV
562 }
563 else {
564 // Node link
353f0d71 565 if (empty($node->print_pdf_display)) {
395b324c 566 // Link for this node is disabled
cc224831
JV
567 return FALSE;
568 }
2891fe2b 569 elseif (isset($node->book)) {
cc224831
JV
570 // Node is a book;
571 $print_pdf_book_link = variable_get('print_pdf_book_link', PRINT_PDF_BOOK_LINK_DEFAULT);
ea77807a
JV
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;
cc224831
JV
580 }
581 }
582 else {
583 return PRINT_ALLOW_NORMAL_LINK;
584 }
585 }
586 }
353f0d71
JV
587 elseif ($is_view && variable_get('print_pdf_views_link_visibility', 1)) {
588 return PRINT_ALLOW_NORMAL_LINK;
589 }
cc224831
JV
590 else {
591 // 'System' page
592 $print_pdf_sys_link_visibility = variable_get('print_pdf_sys_link_visibility', PRINT_PDF_SYS_LINK_VISIBILITY_DEFAULT);
593 $print_pdf_sys_link_pages = variable_get('print_pdf_sys_link_pages', PRINT_PDF_SYS_LINK_PAGES_DEFAULT);
594
371610ef 595 return _print_page_match($print_pdf_sys_link_visibility, $_GET['q'], $print_pdf_sys_link_pages);
9c35190f 596 }
ea77807a 597 return FALSE;
9c35190f 598}
a70bf289 599
c45f1fed
JV
600/**
601 * Find out the version of the TCPDF library
602 */
603function _print_pdf_tcpdf_version() {
604 $print_pdf_pdf_tool = variable_get('print_pdf_pdf_tool', PRINT_PDF_PDF_TOOL_DEFAULT);
c7c63725 605 require_once($print_pdf_pdf_tool);
c45f1fed
JV
606
607 $pdf = new TCPDF();
608
609 return $pdf->getTCPDFVersion();
610}
611
612/**
613 * Find out the version of the wkhtmltopdf library
614 */
a70bf289
JV
615function _print_pdf_wkhtmltopdf_version() {
616 $print_pdf_pdf_tool = variable_get('print_pdf_pdf_tool', PRINT_PDF_PDF_TOOL_DEFAULT);
617 $descriptor = array(0 => array('pipe', 'r'), 1 => array('pipe', 'w'), 2 => array('pipe', 'w'));
40a8243b
JV
618
619 $cmd = realpath($print_pdf_pdf_tool) .' --version';
a70bf289
JV
620 $process = proc_open($cmd, $descriptor, $pipes, NULL, NULL);
621 if (is_resource($process)) {
622 $out = preg_match('!.*?(\d+\.\d+\.\d+).*$!m', stream_get_contents($pipes[1]), $matches);
623 fclose($pipes[0]);
624 fclose($pipes[1]);
625 fclose($pipes[2]);
626 $retval = proc_terminate($process);
627 }
628
629 return ($matches[1]);
630}