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