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