Project

General

Profile

Paste
Download (12.2 KB) Statistics
| Branch: | Revision:

root / drupal7 / sites / all / modules / print / print_mail / print_mail.module @ 76bdcd04

1
<?php
2

    
3
/**
4
 * @file
5
 * Displays Printer-friendly versions of Drupal pages.
6
 *
7
 * @ingroup print
8
 */
9

    
10
define('PRINT_MAIL_HOURLY_THRESHOLD', 3);
11
define('PRINT_MAIL_USE_REPLY_TO', TRUE);
12
define('PRINT_MAIL_TEASER_DEFAULT_DEFAULT', 1);
13
define('PRINT_MAIL_TEASER_CHOICE_DEFAULT', 0);
14
define('PRINT_MAIL_SEND_OPTION_DEFAULT', 'sendpage');
15
define('PRINT_MAIL_JOB_QUEUE_DEFAULT', 0);
16
define('PRINT_MAIL_USER_RECIPIENTS_DEFAULT', 0);
17

    
18
/**
19
 * Implements hook_print_link().
20
 */
21
function print_mail_print_link() {
22
  return array(
23
    'format' => 'mail',
24
    'text' => t('Send by email'),
25
    'description' => t('Send this page by email.'),
26
    'path' => 'printmail',
27
    'class' => 'print-mail',
28
    'icon' => 'mail_icon.png',
29
    'module' => 'print_mail',
30
  );
31
}
32

    
33
/**
34
 * Implements hook_permission().
35
 */
36
function print_mail_permission() {
37
  return array(
38
    'access send by email' => array(
39
      'title' => t('Access the Send by email functionality'),
40
      'description' => t('Provides the ability to send pages by email and the links to them in the original pages.'),
41
    ),
42
    'send unlimited emails' => array(
43
      'title' => t('Send unlimited emails'),
44
      'description' => t("Overrides the built-in hourly threshold limits when sending emails. This permission should only be granted to trusted users, due to it's potential in enabling the use of your site as a source of email spam."),
45
    ),
46
  );
47
}
48

    
49
/**
50
 * Implements hook_theme().
51
 */
52
function print_mail_theme() {
53
  return array(
54
    'print_mail_form' => array(
55
      'render element' => 'form',
56
      'file' => 'print_mail.inc',
57
    ),
58
    'print_mail_sendlink_html' => array(
59
      'variables' => array('params' => NULL),
60
      'file' => 'print_mail.inc',
61
    ),
62
    'print_mail_sendlink_plain' => array(
63
      'variables' => array('params' => NULL),
64
      'file' => 'print_mail.inc',
65
    ),
66
  );
67
}
68

    
69
/**
70
 * Implements hook_menu().
71
 */
72
function print_mail_menu() {
73
  $link = print_mail_print_link();
74
  $items = array();
75

    
76
  $items[$link['path']] = array(
77
    'title' => 'Send by email',
78
    'page callback' => 'drupal_get_form',
79
    'page arguments' => array('print_mail_form'),
80
    'access callback' => '_print_mail_access',
81
    'access arguments' => array('access send by email'),
82
    'type' => MENU_CALLBACK,
83
    'file' => 'print_mail.inc',
84
  );
85
  $items[$link['path'] . '/' . $link['path']] = array(
86
    'access callback' => FALSE,
87
  );
88
  $items['admin/config/user-interface/print/email'] = array(
89
    'title' => 'email',
90
    'description' => 'Configure the settings of the send by email functionality.',
91
    'page callback' => 'drupal_get_form',
92
    'page arguments' => array('print_mail_settings'),
93
    'access arguments'  => array('administer print'),
94
    'weight' => 2,
95
    'type' => MENU_LOCAL_TASK,
96
    'file' => 'print_mail.admin.inc',
97
  );
98

    
99
  return $items;
100
}
101

    
102
/**
103
 * Implements hook_variable_info().
104
 */
105
function print_mail_variable_info($options) {
106
  $link = print_mail_print_link();
107

    
108
  $variable['print_mail_link_text'] = array(
109
    'title' => t('Send by email'),
110
    'description' => t('Text used in the link to the send by email form.'),
111
    'type' => 'string',
112
    'default' => t($link['text']),
113
  );
114

    
115
  return $variable;
116
}
117

    
118
/**
119
 * Implements hook_block_info().
120
 */
121
function print_mail_block_info() {
122
  $block['print_mail-top']['info'] = t('Most emailed');
123
  $block['print_mail-top']['cache'] = DRUPAL_CACHE_GLOBAL;
124
  return $block;
125
}
126

    
127
/**
128
 * Implements hook_block_view().
129
 */
130
function print_mail_block_view($delta = 0) {
131
  $block = array();
132
  switch ($delta) {
133
    case 'print_mail-top':
134
      $block['subject'] = t('Most emailed');
135
      $result = db_query_range("SELECT path FROM {print_mail_page_counter} LEFT JOIN {node} n ON path = CONCAT('node/', n.nid) WHERE status <> 0 OR status IS NULL ORDER BY sentcount DESC", 0, 3)
136
        ->fetchAll();
137
      if (count($result)) {
138
        $items = array();
139
        foreach ($result as $obj) {
140
          $items[] = l(_print_get_title($obj->path), $obj->path);
141
        }
142
        $block['content'] = theme('item_list', array('items' => $items, 'type' => 'ul'));
143
      }
144
      break;
145
  }
146
  return $block;
147
}
148

    
149
/**
150
 * Implements hook_node_delete().
151
 */
152
function print_mail_node_delete($node) {
153
  db_delete('print_mail_page_counter')
154
    ->condition('path', 'node/' . $node->nid)
155
    ->execute();
156
}
157

    
158
/**
159
 * Implements hook_cron_queue_info().
160
 */
161
function print_mail_cron_queue_info() {
162
  $queues['print_mail_send'] = array(
163
    'worker callback' => 'print_mail_send',
164
    'time' => 60,
165
  );
166
  return $queues;
167
}
168

    
169
/**
170
 * Worker callback for print_mail_cron_queue_info()
171
 *
172
 * @param array $data
173
 *   An associative array containing:
174
 *   - module: A module name to invoke hook_mail() on.
175
 *   - key: A key to identify the e-mail sent.
176
 *   - to: The e-mail address or addresses where the message will be sent to.
177
 *   - language: Language object to use to compose the e-mail.
178
 *   - params: Optional parameters to build the e-mail.
179
 *   - from: Sets From to this value, if given.
180
 *   These are the input arguments of the drupal_mail() function.
181
 */
182
function print_mail_send($data) {
183
  drupal_mail($data['module'], $data['key'], $data['to'], $data['language'], $data['params'], $data['from']);
184
}
185

    
186
/**
187
 * Implements hook_mail().
188
 */
189
function print_mail_mail($key, &$message, $params) {
190
  $message['subject'] = $params['subject'];
191

    
192
  if (isset($params['from'])) {
193
    $message['headers']['Reply-To'] = $params['from'];
194
  }
195

    
196
  $sendlink_plain = '';
197
  $sendlink_html = '';
198
  switch ($key) {
199
    case 'sendpage':
200
      $message['body'][] = check_plain($params['body']);
201
      $message['headers']['Content-Type'] = 'text/html; charset=utf-8';
202
      break;
203

    
204
    case 'sendlink':
205
      // Generate plain-text and html versions of message with link.
206
      $sendlink_plain = theme('print_mail_sendlink_plain', $params);
207
      $sendlink_html = theme('print_mail_sendlink_html', $params);
208

    
209
      // Send HTML-only version if MIME library not present.
210
      if (!class_exists('Mail_mime')) {
211
        $message['body'][] = check_plain($sendlink_html);
212
        $message['headers']['Content-Type'] = 'text/html; charset=utf-8';
213
        break;
214
      }
215
      // No break on purpose.
216
    case 'plain-attachment':
217
    case 'inline-attachment':
218
      // Configure new MIME object.
219
      $mime = new Mail_mime("\n");
220
      $mime_params['html_encoding'] = '7bit';
221
      $mime_params['html_charset'] = 'utf-8';
222
      $mime_params['text_charset'] = 'utf-8';
223

    
224
      // Pass message contents into MIME object.
225
      switch ($key) {
226
        case 'sendlink':
227
          $mime->setTxtBody($sendlink_plain);
228
          $mime->setHTMLBody($sendlink_html);
229
          break;
230

    
231
        case 'inline-attachment':
232
          $mime->setHTMLBody($params['body']);
233
          // No break on purpose.
234
        case 'plain-attachment':
235
          $mime->setTxtBody($params['message']);
236
          $mime->addAttachment($params['body'], 'text/html', 'Attachment.html', FALSE);
237
          break;
238
      }
239

    
240
      // Store MIME message output in message array.
241
      $message['body'][] = check_plain($mime->get($mime_params));
242
      $message['headers'] = $mime->headers($message['headers']);
243

    
244
      // Strip special characters from Content-Type header. Required to prevent
245
      // mime_header_encode() from disrupting Content-Type header.
246
      $message['headers']['Content-Type'] = preg_replace('/[^\x20-\x7E]/', '', $message['headers']['Content-Type']);
247
      break;
248
  }
249
}
250

    
251
/**
252
 * Access callback to check a combination of user_acess() and page access.
253
 *
254
 * @param string $permission
255
 *   Permission required to view the page.
256
 *
257
 * @return bool
258
 *   TRUE if the user has permission to view the page, FALSE otherwise
259
 */
260
function _print_mail_access($permission) {
261
  $link = print_mail_print_link();
262
  $page_access = TRUE;
263
  $parts = explode('/', $_GET['q']);
264
  if ($parts[0] == $link['path']) {
265
    if (count($parts) > 1) {
266
      unset($parts[0]);
267
      $path = implode('/', $parts);
268
      if (ctype_digit($parts[1])) {
269
        if (drupal_lookup_path('source', $path)) {
270
          // This is a numeric alias.
271
          $path = drupal_get_normal_path($path);
272
        }
273
        else {
274
          // Normal nid.
275
          $path = 'node/' . $path;
276
        }
277
      }
278
      else {
279
        $path = drupal_get_normal_path($path);
280
      }
281
      // If the destination page is not accessible, don't show the form.
282
      if (!($router_item = menu_get_item($path)) || (!$router_item['access'])) {
283
        $page_access = FALSE;
284
      }
285
    }
286
  }
287

    
288
  return (user_access($permission) && $page_access);
289
}
290

    
291
/**
292
 * Auxiliary function to display a formatted send by email link.
293
 *
294
 * Function made available so that developers may call this function from
295
 * their defined pages/blocks.
296
 *
297
 * @param string $path
298
 *   path to be used in the link. If not specified, the current URL is used.
299
 * @param object $node
300
 *   node object, to be used in checking node access. If the path argument is
301
 *   not provided, the path used will be node/nid.
302
 * @param string $location
303
 *   Where in the page where the link is being inserted ('link', 'corner',
304
 *   'block', 'help').
305
 *
306
 * @return string
307
 *   string with the HTML link to the printer-friendly page
308
 *
309
 * @ingroup print_api
310
 */
311
function print_mail_insert_link($path = NULL, $node = NULL, $location = '') {
312
  if (function_exists('print_ui_insert_link')) {
313
    return print_ui_insert_link(print_mail_print_link(),
314
      array('path' => $path, 'node' => $node, 'location' => $location));
315
  }
316
  else {
317
    return FALSE;
318
  }
319
}
320

    
321
/**
322
 * Check if the link to send by email is allowed depending on the settings.
323
 *
324
 * @param array $args
325
 *   Array containing the possible parameters:
326
 *    view_mode, node, type, path.
327
 *
328
 * @return bool
329
 *   FALSE if not allowed, TRUE otherwise.
330
 */
331
function print_mail_link_allowed($args) {
332
  return (user_access('access send by email'));
333
}
334

    
335
/**
336
 * Implements hook_nollom_form_list().
337
 */
338
function print_mail_mollom_form_list() {
339
  $forms['print_mail_form'] = array(
340
    'title' => t('Send by email form'),
341
    'entity' => 'print_mail',
342
  );
343
  return $forms;
344
}
345

    
346
/**
347
 * Implemenents hook_mollom_form_info().
348
 */
349
function print_mail_mollom_form_info($form_id) {
350
  $form_info = array();
351
  switch ($form_id) {
352
    case 'print_mail_form':
353
      $form_info = array(
354
        'elements' => array(
355
          'fld_from_addr' => t('Sender email'),
356
          'fld_from_name' => t('Sender name'),
357
          'txt_to' => t('Recipients'),
358
          'fld_subject' => t('Subject'),
359
          'fld_title' => t('Page to be sent'),
360
          'txt_message' => t('Your message'),
361
        ),
362
        'mapping' => array(
363
          'post_title' => 'fld_title',
364
          'author_name' => 'fld_from_name',
365
          'author_mail' => 'fld_from_addr',
366
        ),
367
      );
368
      break;
369
  }
370
  return $form_info;
371
}
372

    
373
/**
374
 * Implements hook_views_api().
375
 */
376
function print_mail_views_api() {
377
  return array(
378
    'api' => 2.0,
379
    'path' => drupal_get_path('module', 'print_mail'),
380
  );
381
}
382

    
383
/**
384
 * Implements hook_rules_action_info().
385
 *
386
 * @ingroup rules
387
 */
388
function print_mail_rules_action_info() {
389
  return array(
390
    'print_mail_action_submit' => array(
391
      'label' => t('Send node as HTML formatted email'),
392
      'group' => t('Send by email'),
393
      'parameter' => array(
394
        'from'      => array('type' => 'text', 'label' => t('From email adress')),
395
        'from_name' => array('type' => 'text', 'label' => t('From name')),
396
        'to'        => array('type' => 'text', 'label' => t('Send email to')),
397
        'subject'   => array('type' => 'text', 'label' => t('Subject')),
398
        'message'   => array(
399
          'type' => 'text',
400
          'label' => t('Message'),
401
          'description' => t('The message that should be displayed (optional).'),
402
          'optional' => TRUE,
403
        ),
404
        'node'      => array('type' => 'node', 'label' => t('Content')),
405
      ),
406
    ),
407
  );
408
}
409

    
410
/**
411
 * Action handler for the print_mail_action_submit.
412
 *
413
 * @ingroup rules
414
 */
415
function print_mail_action_submit($from, $from_name, $to, $subject, $message, $node) {
416
  module_load_include('inc', 'print_mail', 'print_mail');
417

    
418
  $form_state['values'] = array(
419
    'path' => 'node/' . $node->nid,
420
    'query' => NULL,
421
    'cid' => NULL,
422
    'title' => $node->title,
423
    'fld_from_addr' => $from,
424
    'fld_from_name' => $from_name,
425
    'txt_to' => array('addrs' => $to),
426
    'fld_subject' => $subject,
427
    'txt_message' => $message,
428
    'chk_teaser' => FALSE,
429
  );
430

    
431
  print_mail_form_submit(NULL, $form_state);
432
}