Issue #1049168: fix access denied when using URL alias with Send to Friend
[project/print.git] / print_mail / print_mail.module
1 <?php
2
3 /**
4 * @file
5 * Displays Printer-friendly versions of Drupal pages.
6 *
7 * @ingroup print
8 */
9
10 define('PRINTMAIL_PATH', 'printmail');
11
12 // Defined in print.module
13 //define('PRINT_MAIL_FORMAT', 'mail');
14
15 define('PRINT_MAIL_LINK_POS_DEFAULT', 'link');
16 define('PRINT_MAIL_LINK_TEASER_DEFAULT', 0);
17 define('PRINT_MAIL_SHOW_LINK_DEFAULT', 1);
18 define('PRINT_MAIL_NODE_LINK_VISIBILITY_DEFAULT', 0);
19 define('PRINT_MAIL_NODE_LINK_PAGES_DEFAULT', '');
20 define('PRINT_MAIL_LINK_CLASS_DEFAULT', 'print-mail');
21 define('PRINT_MAIL_SYS_LINK_VISIBILITY_DEFAULT', 1);
22 define('PRINT_MAIL_SYS_LINK_PAGES_DEFAULT', '');
23 define('PRINT_MAIL_LINK_USE_ALIAS_DEFAULT', 0);
24 define('PRINT_MAIL_BOOK_LINK_DEFAULT', 1);
25 define('PRINT_MAIL_HOURLY_THRESHOLD', 3);
26 define('PRINT_MAIL_TEASER_DEFAULT_DEFAULT', 1);
27 define('PRINT_MAIL_TEASER_CHOICE_DEFAULT', 1);
28 define('PRINT_MAIL_SEND_OPTION_DEFAULT', 'sendpage');
29 define('PRINT_MAIL_JOB_QUEUE_DEFAULT', 0);
30
31 /**
32 * Implements hook_permission().
33 */
34 function print_mail_permission() {
35 return array(
36 'access send to friend' => array(
37 'title' => t('Access the Send To Friend functionality'),
38 'description' => t('Provides the ability to send pages to a friend and the links to them in the original pages.'),
39 ),
40 );
41 }
42
43 /**
44 * Implements hook_theme().
45 */
46 function print_mail_theme() {
47 return array(
48 'print_mail_format_link' => array(
49 'variables' => array(),
50 ),
51 'print_mail_form' => array(
52 'variables' => array('form' => NULL),
53 'file' => 'print_mail.inc',
54 ),
55 );
56 }
57
58 /**
59 * Implements hook_menu().
60 */
61 function print_mail_menu() {
62 $items = array();
63
64 $items[PRINTMAIL_PATH] = array(
65 'title' => variable_get('print_mail_text_title', 'Send page by e-mail'),
66 'page callback' => 'drupal_get_form',
67 'page arguments' => array('print_mail_form'),
68 'access callback' => '_print_mail_access',
69 'access arguments' => array('access send to friend'),
70 'type' => MENU_CALLBACK,
71 'file' => 'print_mail.inc',
72 );
73 $items[PRINTMAIL_PATH . '/' . PRINTMAIL_PATH] = array(
74 'access callback' => FALSE,
75 );
76 $items['admin/config/user-interface/print/email'] = array(
77 'title' => 'e-mail',
78 'page callback' => 'drupal_get_form',
79 'page arguments' => array('print_mail_settings'),
80 'access arguments' => array('administer print'),
81 'weight' => 2,
82 'type' => MENU_LOCAL_TASK,
83 'file' => 'print_mail.admin.inc',
84 );
85 $items['admin/config/user-interface/print/email/options'] = array(
86 'title' => 'Options',
87 'weight' => 1,
88 'type' => MENU_DEFAULT_LOCAL_TASK,
89 );
90 $items['admin/config/user-interface/print/email/strings'] = array(
91 'title' => 'Text strings',
92 'page callback' => 'drupal_get_form',
93 'page arguments' => array('print_mail_strings_settings'),
94 'access arguments' => array('administer print'),
95 'weight' => 2,
96 'type' => MENU_LOCAL_TASK,
97 'file' => 'print_mail.admin.inc',
98 );
99
100 return $items;
101 }
102
103 /**
104 * Implements hook_block_info().
105 */
106 function print_mail_block_info() {
107 $block['print_mail-top']['info'] = t('Most emailed');
108 $block['print_mail-top']['cache'] = DRUPAL_CACHE_GLOBAL;
109 return $block;
110 }
111
112 /**
113 * Implements hook_block_view().
114 */
115 function print_mail_block_view($delta = 0) {
116 switch ($delta) {
117 case 'print_mail-top':
118 $block['subject'] = t('Most emailed');
119 $result = db_query_range("SELECT path FROM {print_mail_page_counter} ORDER BY sentcount DESC", 0, 3)
120 ->fetchAll();
121 if (count($result)) {
122 $block['content'] = '<div class="item-list"><ul>';
123 foreach ($result as $obj) {
124 $block['content'] .= '<li>' . l(_print_get_title($obj->path), $obj->path) . '</li>';
125 }
126 $block['content'] .= '</ul></div>';
127 }
128 break;
129 }
130 return $block;
131 }
132
133 /**
134 * Implements hook_node_view().
135 */
136 function print_mail_node_view($node, $view_mode) {
137 $print_mail_link_pos = variable_get('print_mail_link_pos', array(PRINT_MAIL_LINK_POS_DEFAULT => PRINT_MAIL_LINK_POS_DEFAULT));
138 $print_mail_link_use_alias = variable_get('print_mail_link_use_alias', PRINT_MAIL_LINK_USE_ALIAS_DEFAULT);
139 $allowed_type = print_mail_link_allowed(array('type' => 'node', 'node' => $node, 'teaser' => ($view_mode == 'teaser')));
140 if (($allowed_type) && !empty($print_mail_link_pos['link'])) {
141 drupal_add_css(drupal_get_path('module', 'print') . '/css/printlinks.css');
142 $links = array();
143 $format = theme('print_mail_format_link');
144
145 // Show book link
146 if ($allowed_type === PRINT_ALLOW_BOOK_LINK) {
147 $links['book_mail'] = array('href' => PRINTMAIL_PATH . '/book/export/html/' . $node->nid,
148 'title' => $format['text'],
149 'attributes' => $format['attributes'],
150 'html' => $format['html'],
151 );
152 }
153 elseif ($allowed_type === PRINT_ALLOW_NORMAL_LINK) {
154 $query_arr = $_GET;
155 if ('node' == 'comment') {
156 $query_arr['comment'] = $node->cid;
157 }
158 $query = print_query_string_encode($query_arr, array('q'));
159 if (empty($query)) $query = NULL;
160
161 if ($print_mail_link_use_alias) {
162 $path = drupal_get_path_alias('node/' . $node->nid);
163 }
164 else {
165 $path = $node->nid;
166 }
167
168 $links['print_mail'] = array('href' => PRINTMAIL_PATH . '/' . $path,
169 'title' => $format['text'],
170 'attributes' => $format['attributes'],
171 'html' => $format['html'],
172 'query' => $query,
173 );
174 }
175
176 $node->content['links']['print_mail'] = array(
177 '#theme' => 'links',
178 '#links' => $links,
179 '#attributes' => array('class' => array('links', 'inline')),
180 );
181 }
182
183 // Insert content corner links
184 if (!empty($print_mail_link_pos['corner']) && ($view_mode == 'full')) {
185 $node->content['print_links']['#markup'] .= print_mail_insert_link(NULL, $node);
186 }
187 }
188
189 /**
190 * Implements hook_help().
191 */
192 function print_mail_help($path, $arg) {
193 $print_mail_link_pos = variable_get('print_mail_link_pos', array(PRINT_MAIL_LINK_POS_DEFAULT => PRINT_MAIL_LINK_POS_DEFAULT));
194 if (($path !== 'node/%') && !(empty($print_mail_link_pos['link']) && empty($print_mail_link_pos['corner']))) {
195 static $output = FALSE;
196
197 if ($output === FALSE) {
198 $output = TRUE;
199
200 $link = print_mail_insert_link();
201 if ($link) {
202 return "<span class='print-syslink'>$link</span>";
203 }
204 }
205 }
206 }
207
208 /**
209 * Implements hook_node_load().
210 */
211 function print_mail_node_load($nodes, $types) {
212 foreach ($nodes as $node) {
213 _print_mail_set_node_fields($node);
214 }
215 }
216
217 /**
218 * Implements hook_node_insert().
219 */
220 function print_mail_node_insert($node) {
221 if (user_access('administer print') || user_access('node-specific print configuration')) {
222 if ($node->print_mail_display === NULL) $node->print_mail_display = variable_get('print_mail_display_' . $node->type, PRINT_TYPE_SHOW_LINK_DEFAULT);
223 if ($node->print_mail_display_comment === NULL) $node->print_mail_display_comment = variable_get('print_mail_display_comment_' . $node->type, PRINT_TYPE_COMMENT_LINK_DEFAULT);
224 if ($node->print_mail_display_urllist === NULL) $node->print_mail_display_urllist = variable_get('print_mail_display_urllist_' . $node->type, PRINT_TYPE_URLLIST_DEFAULT);
225
226 _print_mail_node_conf_modify($node->nid, $node->print_mail_display, $node->print_mail_display_comment, $node->print_mail_display_urllist);
227 }
228 }
229
230 /**
231 * Implements hook_node_update().
232 */
233 function print_mail_node_update($node) {
234 if (user_access('administer print') || user_access('node-specific print configuration')) {
235 if ($node->print_mail_display === NULL) $node->print_mail_display = variable_get('print_mail_display_' . $node->type, PRINT_TYPE_SHOW_LINK_DEFAULT);
236 if ($node->print_mail_display_comment === NULL) $node->print_mail_display_comment = variable_get('print_mail_display_comment_' . $node->type, PRINT_TYPE_COMMENT_LINK_DEFAULT);
237 if ($node->print_mail_display_urllist === NULL) $node->print_mail_display_urllist = variable_get('print_mail_display_urllist_' . $node->type, PRINT_TYPE_URLLIST_DEFAULT);
238
239 _print_mail_node_conf_modify($node->nid, $node->print_mail_display, $node->print_mail_display_comment, $node->print_mail_display_urllist);
240 }
241 }
242
243 /**
244 * Implements hook_node_delete().
245 */
246 function print_mail_node_delete($node) {
247 db_delete('print_mail_node_conf')
248 ->condition('nid', $node->nid)
249 ->execute();
250 db_delete('print_mail_page_counter')
251 ->condition('path', 'node/' . $node->nid)
252 ->execute();
253 }
254
255 /**
256 * Implements hook_form_alter().
257 */
258 function print_mail_form_alter(&$form, &$form_state, $form_id) {
259 // Add the node-type settings option to activate the mail version link
260 if ((user_access('administer print') || user_access('node-specific print configuration')) &&
261 (($form_id == 'node_type_form') || !empty($form['#node_edit_form']))) {
262 $form['print']['mail_label'] = array(
263 '#type' => 'markup',
264 '#markup' => '<p><strong>' . t('Send by e-mail') . '</strong></p>',
265 );
266
267 $form['print']['print_mail_display'] = array(
268 '#type' => 'checkbox',
269 '#title' => t('Show link'),
270 );
271 $form['print']['print_mail_display_comment'] = array(
272 '#type' => 'checkbox',
273 '#title' => t('Show link in individual comments'),
274 );
275 $form['print']['print_mail_display_urllist'] = array(
276 '#type' => 'checkbox',
277 '#title' => t('Show Printer-friendly URLs list'),
278 );
279
280 if ($form_id == 'node_type_form') {
281 $form['print']['print_mail_display']['#default_value'] = variable_get('print_mail_display_' . $form['#node_type']->type, PRINT_TYPE_SHOW_LINK_DEFAULT);
282 $form['print']['print_mail_display_comment']['#default_value'] = variable_get('print_mail_display_comment_' . $form['#node_type']->type, PRINT_TYPE_COMMENT_LINK_DEFAULT);
283 $form['print']['print_mail_display_urllist']['#default_value'] = variable_get('print_mail_display_urllist_' . $form['#node_type']->type, PRINT_TYPE_URLLIST_DEFAULT);
284 }
285 else {
286 $node = $form['#node'];
287 $form['print']['print_mail_display']['#default_value'] = isset($node->print_mail_display) ? $node->print_mail_display : variable_get('print_mail_display_' . $node->type, PRINT_TYPE_SHOW_LINK_DEFAULT);
288 $form['print']['print_mail_display_comment']['#default_value'] = isset($node->print_mail_display_comment) ? $node->print_mail_display_comment : variable_get('print_mail_display_comment_' . $node->type, PRINT_TYPE_COMMENT_LINK_DEFAULT);
289 $form['print']['print_mail_display_urllist']['#default_value'] = isset($node->print_mail_display_urllist) ? $node->print_mail_display_urllist : variable_get('print_mail_display_urllist_' . $node->type, PRINT_TYPE_URLLIST_DEFAULT);
290 }
291 }
292 }
293
294 /**
295 * Implements hook_job_queue_functions().
296 */
297 function print_mail_job_queue_functions() {
298 $functions['print_mail'] = array(
299 'title' => t('Send to friend'),
300 );
301 return $functions;
302 }
303
304 /**
305 * Access callback to check a combination of user_acess() and page access
306 *
307 * @param $permission
308 * permission required to view the page
309 * @return
310 * TRUE if the user has permission to view the page, FALSE otherwise
311 */
312 function _print_mail_access($permission) {
313 $page_access = TRUE;
314 $parts = explode('/', $_GET['q']);
315 if ($parts[0] == PRINTMAIL_PATH) {
316 if (count($parts) > 1) {
317 unset($parts[0]);
318 $path = implode('/', $parts);
319 if (ctype_digit($parts[1])) {
320 if (drupal_lookup_path('source', $path)) {
321 // This is a numeric alias
322 $path = drupal_get_normal_path($path);
323 }
324 else {
325 // normal nid
326 $path = 'node/' . $path;
327 }
328 }
329 else {
330 $path = drupal_get_normal_path($path);
331 }
332 // If the destination page is not accessible, don't show the form
333 if (!($router_item = menu_get_item($path)) || (!$router_item['access'])) {
334 $page_access = FALSE;
335 }
336 }
337 }
338
339 return (user_access($permission) && $page_access);
340 }
341
342 /**
343 * Auxiliary function to assign the per-node settings to the node object fields
344 *
345 * @param $node
346 * node to be modified
347 */
348 function _print_mail_set_node_fields(&$node) {
349 if (isset($node->nid)) {
350 $res = db_query("SELECT link, comments, url_list FROM {print_mail_node_conf} WHERE nid = :nid", array(':nid' => $node->nid))
351 ->fetch();
352 }
353 else {
354 $res = FALSE;
355 }
356 $node->print_mail_display = $res ? intval($res->link) : variable_get('print_mail_display_' . $node->type, PRINT_TYPE_SHOW_LINK_DEFAULT);
357 $node->print_mail_display_comment = $res ? intval($res->comments) : variable_get('print_mail_display_comment_' . $node->type, PRINT_TYPE_COMMENT_LINK_DEFAULT);
358 $node->print_mail_display_urllist = $res ? intval($res->url_list) : variable_get('print_mail_display_urllist_' . $node->type, PRINT_TYPE_URLLIST_DEFAULT);
359 }
360
361 /**
362 * Update the print_mail_node_conf table to reflect the given attributes
363 * If updating to the default values, delete the record.
364 *
365 * @param $nid
366 * value of the nid field (primary key)
367 * @param $link
368 * value of the link field (0 or 1)
369 * @param $comments
370 * value of the comments field (0 or 1)
371 * @param $url_list
372 * value of the url_list field (0 or 1)
373 */
374 function _print_mail_node_conf_modify($nid, $link, $comments, $url_list) {
375 db_merge('print_mail_node_conf')
376 ->key(array('nid' => $nid))
377 ->fields(array(
378 'link' => $link,
379 'comments' => $comments,
380 'url_list' => $url_list,
381 ))
382 ->execute();
383 }
384
385 /**
386 * Format the send by e-mail link
387 *
388 * @return
389 * array of formatted attributes
390 * @ingroup themeable
391 */
392 function theme_print_mail_format_link() {
393 $print_mail_link_class = variable_get('print_mail_link_class', PRINT_MAIL_LINK_CLASS_DEFAULT);
394 $print_mail_show_link = variable_get('print_mail_show_link', PRINT_MAIL_SHOW_LINK_DEFAULT);
395 $print_mail_link_text = filter_xss(variable_get('print_mail_link_text', t('Send to friend')));
396
397 $img = drupal_get_path('module', 'print') . '/icons/mail_icon.gif';
398 $title = t('Send this page by e-mail.');
399 $class = strip_tags($print_mail_link_class);
400 $new_window = FALSE;
401 $format = _print_format_link_aux($print_mail_show_link, $print_mail_link_text, $img);
402
403 return array('text' => $format['text'],
404 'html' => $format['html'],
405 'attributes' => print_fill_attributes($title, $class, $new_window),
406 );
407 }
408
409 /**
410 * Auxiliary function to display a formatted send by e-mail link
411 *
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
423 */
424 function print_mail_insert_link($path = NULL, $node = NULL) {
425 if ($node !== NULL) {
426 $nid = $node->nid;
427 $path = 'node/' . $nid;
428 $allowed_type = print_mail_link_allowed(array('node' => $node));
429 }
430 else {
431 if ($path === NULL) {
432 $nid = preg_replace('!^node/!', '', $_GET['q']);
433 $path = $_GET['q'];
434 }
435 else {
436 $nid = NULL;
437 }
438 $allowed_type = print_mail_link_allowed(array('path' => $path));
439 }
440
441 if ($allowed_type) {
442 if ($nid !== NULL) {
443 if ($allowed_type === PRINT_ALLOW_BOOK_LINK) {
444 $path = 'book/export/html/' . $nid;
445 }
446 else {
447 if (variable_get('print_mail_link_use_alias', PRINT_MAIL_LINK_USE_ALIAS_DEFAULT)) {
448 $path = drupal_get_path_alias($path);
449 }
450 else {
451 $path = $nid;
452 }
453 }
454 $path = PRINTMAIL_PATH . '/' . $path;
455 $query = print_query_string_encode($_GET, array('q'));
456 if (empty($query)) {
457 $query = NULL;
458 }
459 }
460 else {
461 $query = NULL;
462 }
463 drupal_add_css(drupal_get_path('module', 'print') . '/css/printlinks.css');
464 $format = theme('print_mail_format_link');
465 return '<span class="print_mail">' . l($format['text'], $path, array('attributes' => $format['attributes'], 'query' => $query, 'absolute' => TRUE, 'html' => $format['html'])) . '</span>';
466 }
467 else {
468 return FALSE;
469 }
470 }
471
472 /**
473 * Determine a the link to send by e-mail 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 */
483 function print_mail_link_allowed($args) {
484 if ((!empty($args['teaser']) && !variable_get('print_mail_link_teaser', PRINT_MAIL_LINK_TEASER_DEFAULT))
485 || !user_access('access send to friend')) {
486 // If the teaser link is disabled or the user is not allowed
487 return FALSE;
488 }
489 if (!empty($args['path'])) {
490 $nid = preg_replace('!^node/!', '', drupal_get_normal_path($args['path']));
491 if (ctype_digit($nid)) {
492 $args['node'] = node_load($nid);
493 }
494 }
495 if (!empty($args['node'])) {
496 static $node_type = FALSE;
497
498 $node = $args['node'];
499 if (isset($node->type)) {
500 $node_type = $node->type;
501 }
502 // Node
503 $print_mail_node_link_visibility = variable_get('print_mail_node_link_visibility', PRINT_MAIL_NODE_LINK_VISIBILITY_DEFAULT);
504 $print_mail_node_link_pages = variable_get('print_mail_node_link_pages', PRINT_MAIL_NODE_LINK_PAGES_DEFAULT);
505
506 if (!_print_page_match($print_mail_node_link_visibility, "node/" . $node->nid, $print_mail_node_link_pages)) {
507 // Page not in visibility list
508 return FALSE;
509 }
510 elseif (isset($args['type']) && ($args['type'] == 'comment') && isset($node_type)) {
511 // Link is for a comment, return the configured setting
512 // Cache this statically to avoid duplicate queries for every comment.
513 static $res = array();
514 if (!isset($res[$node->nid])) {
515 $res[$node->nid] = db_query("SELECT comments FROM {print_mail_node_conf} WHERE nid = :nid", array(':nid' => $node->nid))
516 ->fetch();
517 }
518 $print_display_comment = $res ? intval($res[$node->nid]->comments) : variable_get('print_mail_display_comment_' . $node_type, PRINT_TYPE_COMMENT_LINK_DEFAULT);
519 if ($print_display_comment) {
520 return PRINT_ALLOW_NORMAL_LINK;
521 }
522 }
523 else {
524 // Node link
525 if (!$node->print_mail_display) {
526 // Link for this node is disabled
527 return FALSE;
528 }
529 elseif (isset($node->book)) {
530 // Node is a book;
531 $print_mail_book_link = variable_get('print_mail_book_link', PRINT_MAIL_BOOK_LINK_DEFAULT);
532 switch ($print_mail_book_link) {
533 case 1:
534 if (user_access('access printer-friendly version')) {
535 return PRINT_ALLOW_BOOK_LINK;
536 }
537 break;
538 case 2:
539 return PRINT_ALLOW_NORMAL_LINK;
540 }
541 }
542 else {
543 return PRINT_ALLOW_NORMAL_LINK;
544 }
545 }
546 }
547 else {
548 // 'System' page
549 $print_mail_sys_link_visibility = variable_get('print_mail_sys_link_visibility', PRINT_MAIL_SYS_LINK_VISIBILITY_DEFAULT);
550 $print_mail_sys_link_pages = variable_get('print_mail_sys_link_pages', PRINT_MAIL_SYS_LINK_PAGES_DEFAULT);
551
552 return _print_page_match($print_mail_sys_link_visibility, $_GET['q'], $print_mail_sys_link_pages);
553 }
554 return FALSE;
555 }