Project

General

Profile

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

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

1
<?php
2

    
3
/**
4
 * @file
5
 * Displays and processes the mail send form.
6
 *
7
 * This file is included by the print_mail module and includes the
8
 * mail form display, validation and submit hooks.
9
 *
10
 * @ingroup print
11
 */
12

    
13
// Include MIME library, if available.
14
@include_once 'Mail/mime.php';
15

    
16
/**
17
 * Form constructor for the send by email form.
18
 *
19
 * @ingroup forms
20
 */
21
function print_mail_form($form, &$form_state) {
22
  // Remove the printmail/ prefix.
23
  $path_arr = explode('/', $_GET['q']);
24
  unset($path_arr[0]);
25
  $path = filter_xss(implode('/', $path_arr));
26
  if (empty($path)) {
27
    // If no path was provided, let's try to generate a page for the referer.
28
    global $base_url;
29
    $link = print_mail_print_link();
30

    
31
    $ref = $_SERVER['HTTP_REFERER'];
32
    $path = preg_replace("!^$base_url/!", '', $ref);
33
    if (($path === $ref) || empty($path)) {
34
      $path = variable_get('site_frontpage', 'node');
35
    }
36
    drupal_goto($link['path'] . '/' . $path);
37
  }
38
  elseif (ctype_digit($path_arr[1])) {
39
    if (drupal_lookup_path('source', $path)) {
40
      // This is a numeric alias.
41
      $path = drupal_get_normal_path($path);
42
    }
43
    else {
44
      // Normal nid.
45
      $path = 'node/' . $path;
46
    }
47
  }
48
  else {
49
    $path = drupal_get_normal_path($path);
50
  }
51

    
52
  // Handle the query.
53
  $query = $_GET;
54
  unset($query['q']);
55

    
56
  return print_mail_form_for_path($form, $form_state, $path, $query);
57
}
58

    
59
/**
60
 * Build email form for the page provided in the path argument.
61
 *
62
 * @param array $form
63
 *   Form.
64
 * @param array $form_state
65
 *   Form state.
66
 * @param string $path
67
 *   Path.
68
 * @param array $query
69
 *   Query.
70
 * @param Object $user
71
 *   Current user.
72
 *
73
 * @return array
74
 *   Modified form.
75
 *
76
 * @ingroup forms
77
 */
78
function print_mail_form_for_path($form, &$form_state, $path, $query = NULL, $user = NULL) {
79
  if ($user === NULL) {
80
    global $user;
81
  }
82

    
83
  $print_mail_hourly_threshold = variable_get('print_mail_hourly_threshold', PRINT_MAIL_HOURLY_THRESHOLD);
84

    
85
  if ((!user_access('send unlimited emails')) && (!flood_is_allowed('print_mail', $print_mail_hourly_threshold))) {
86
    $form['flood'] = array(
87
      '#type' => 'markup',
88
      '#markup' => '<p>' . format_plural($print_mail_hourly_threshold, 'You cannot send more than 1 message per hour. Please try again later.', 'You cannot send more than @count messages per hour. Please try again later.') . '</p>',
89
    );
90
    return $form;
91
  }
92

    
93
  $print_mail_teaser_default = variable_get('print_mail_teaser_default', PRINT_MAIL_TEASER_DEFAULT_DEFAULT);
94
  $print_mail_teaser_choice = variable_get('print_mail_teaser_choice', PRINT_MAIL_TEASER_CHOICE_DEFAULT);
95
  $print_mail_user_recipients_default = variable_get('print_mail_user_recipients', PRINT_MAIL_USER_RECIPIENTS_DEFAULT);
96
  $form = array();
97

    
98
  $cid = isset($_GET['comment']) ? (int) $_GET['comment'] : NULL;
99
  $title = _print_get_title($path);
100

    
101
  $options = array();
102
  if ($print_mail_user_recipients_default) {
103
    if (module_exists('realname')) {
104
      $sql = "SELECT u.mail, r.realname AS name from {users} u LEFT JOIN {realname} r ON u.uid = r.uid WHERE u.uid <> :uid AND status = 1 ORDER BY name ASC";
105
    }
106
    else {
107
      $sql = "SELECT mail, name from {users} WHERE uid <> :uid AND status = 1 ORDER BY name ASC";
108
    }
109
    $recipients = db_query($sql, array(':uid' => drupal_anonymous_user()->uid));
110
    foreach ($recipients as $recipient) {
111
      $options[$recipient->mail] = $recipient->name;
112
    }
113
  }
114

    
115
  if (count($form_state['input']) == 0) {
116
    $nodepath = drupal_get_normal_path($path);
117
    db_merge('print_mail_page_counter')
118
      ->key(array('path' => $nodepath))
119
      ->fields(array(
120
        'totalcount' => 1,
121
        'timestamp' => REQUEST_TIME,
122
      ))
123
      ->expression('totalcount', 'totalcount + 1')
124
      ->execute();
125
  }
126

    
127
  $form['path'] = array('#type' => 'value', '#value' => $path);
128
  $form['query'] = array('#type' => 'value', '#value' => $query);
129
  $form['cid'] = array('#type' => 'value', '#value' => $cid);
130
  $form['title'] = array('#type' => 'value', '#value' => $title);
131

    
132
  $form['fld_from_addr'] = array(
133
    '#type' => 'textfield',
134
    '#title' => t('Your email'),
135
    '#size' => 62,
136
    '#required' => TRUE,
137
  );
138
  $form['fld_from_name'] = array(
139
    '#type' => 'textfield',
140
    '#title' => t('Your name'),
141
    '#size' => 62,
142
  );
143
  $form['txt_to'] = array(
144
    '#tree' => TRUE,
145
  );
146
  $form['txt_to']['addrs'] = array(
147
    '#type' => 'textarea',
148
    '#title' => t('Send to'),
149
    '#rows' => 3,
150
    '#resizable' => FALSE,
151
    '#description' => t('Enter multiple addresses separated by commas and/or different lines.'),
152
    '#required' => !$print_mail_user_recipients_default,
153
  );
154
  if ($print_mail_user_recipients_default) {
155
    $form['txt_to']['users'] = array(
156
      '#type' => 'select',
157
      '#title' => t('Send to users'),
158
      '#multiple' => TRUE,
159
      '#size' => 10,
160
      '#options' => $options,
161
    );
162
  }
163
  $form['fld_subject'] = array(
164
    '#type' => 'textfield',
165
    '#title' => t('Subject'),
166
    '#size' => 62,
167
    '#required' => TRUE,
168
  );
169
  if (!empty($title)) {
170
    // To prevent useless translation strings, translate only non-node titles.
171
    if (drupal_substr($path, 0, 5) != 'node/') {
172
      $title = t($title);
173
    }
174

    
175
    $form['fld_title'] = array(
176
      '#type' => 'item',
177
      '#title' => t('Page to be sent'),
178
      '#markup' => l($title, $path, array('attributes' => array('title' => t('View page')), 'query' => $query)),
179
    );
180
  }
181
  $form['txt_message'] = array(
182
    '#type' => 'textarea',
183
    '#title' => t('Your message'),
184
    '#rows' => 6,
185
    '#required' => TRUE,
186
  );
187

    
188
  if ($print_mail_teaser_choice) {
189
    $form['chk_teaser'] = array(
190
      '#type' => 'checkbox',
191
      '#title' => t('Send only the teaser'),
192
      '#default_value' => $print_mail_teaser_default,
193
    );
194
  }
195
  else {
196
    $form['chk_teaser'] = array('#type' => 'value', '#value' => $print_mail_teaser_default);
197
  }
198

    
199
  $form['actions'] = array(
200
    '#type' => 'actions',
201
  );
202

    
203
  $form['actions']['submit'] = array(
204
    '#name' => 'submit',
205
    '#type' => 'submit',
206
    '#value' => t('Send email'),
207
  );
208
  $form['actions']['cancel'] = array(
209
    '#name' => 'cancel',
210
    '#type' => 'submit',
211
    '#value' => t('Cancel'),
212
  );
213

    
214
  if ($user->uid != 0) {
215
    $user_name = check_plain(strip_tags(theme('username', array('account' => $user))));
216
    $form['fld_from_addr']['#default_value'] = $user->mail;
217
    $form['fld_from_addr']['#disabled'] = TRUE;
218
    $form['fld_from_addr']['#value'] = $user->mail;
219
    $form['fld_from_name']['#default_value'] = $user_name;
220
  }
221
  else {
222
    $user_name = t('Someone');
223
  }
224
  $site_name = variable_get('site_name', t('an interesting site'));
225
  $form['fld_subject']['#default_value'] = t('!user has sent you a message from !site', array(
226
    '!user' => $user_name,
227
    '!site' => $site_name,
228
    '!title' => $title,
229
  ));
230
  $form['txt_message']['#default_value'] = '';
231

    
232
  return $form;
233
}
234

    
235
/**
236
 * Returns HTML for the send by-email form.
237
 *
238
 * Adds a class to the form labels. This class is used to place the label on
239
 * the left of the input fields.
240
 *
241
 * @param array $variables
242
 *   Theme variables including the form.
243
 *
244
 * @return string
245
 *   Send by-email form HTML.
246
 *
247
 * @see print_mail_form()
248
 * @ingroup forms
249
 * @ingroup themeable
250
 * @ingroup print_themeable
251
 */
252
function theme_print_mail_form($variables) {
253
  $form = $variables['form'];
254

    
255
  drupal_add_css(drupal_get_path('module', 'print_mail') . '/css/print_mail.theme.css');
256
  $content = '';
257
  foreach (element_children($form) as $key) {
258
    $tmp = drupal_render($form[$key]);
259
    switch ($key) {
260
      case 'fld_from_addr':
261
      case 'fld_from_name':
262
      case 'txt_to':
263
      case 'fld_subject':
264
      case 'fld_title':
265
        $tmp = str_replace('<label', '<label class ="printmail-label"', $tmp);
266
        break;
267
    }
268
    $content .= $tmp;
269
  }
270
  return $content;
271
}
272

    
273
/**
274
 * Theme function for the email sending just the link.
275
 *
276
 * Allows themes and modules to override the default sendlink plain text format.
277
 *
278
 * @param array $params
279
 *   Value of $params as passed to print_mail_mail().
280
 *
281
 * @return string
282
 *   Plain text containing the message and a simple link to the content.
283
 *
284
 * @ingroup themeable
285
 * @ingroup print_themeable
286
 */
287
function theme_print_mail_sendlink_plain($params) {
288
  return $params['message'] . '\n\n' . $params['link'];
289
}
290

    
291
/**
292
 * Theme function for the email sending just the link.
293
 *
294
 * Allows themes and modules to override the default sendlink HTML format.
295
 *
296
 * @param array $params
297
 *   Value of $params as passed to print_mail_mail().
298
 *
299
 * @return string
300
 *   HTML text containing the message and a simple link to the content.
301
 *
302
 * @ingroup themeable
303
 * @ingroup print_themeable
304
 */
305
function theme_print_mail_sendlink_html($params) {
306
  return $params['message'] . '<br/><br/>' . l($params['title'], $params['link']);
307
}
308

    
309
/**
310
 * Form validation handler for print_mail_form().
311
 *
312
 * @see print_mail_form()
313
 * @ingroup forms
314
 */
315
function print_mail_form_validate($form, &$form_state) {
316
  $print_mail_user_recipients_default = variable_get('print_mail_user_recipients', PRINT_MAIL_USER_RECIPIENTS_DEFAULT);
317

    
318
  if (array_key_exists('cancel', $form_state['input'])) {
319
    form_set_error(NULL, '', TRUE);
320
    drupal_get_messages('error');
321

    
322
    drupal_goto(preg_replace('!^book/export/html/!', 'node/', $form_state['values']['path']), array('query' => $form_state['values']['query']));
323
    return;
324
  }
325
  $from_addr = trim($form_state['values']['fld_from_addr']);
326
  $test = user_validate_mail($from_addr);
327
  if ($test) {
328
    form_set_error('fld_from_addr', $test);
329
  }
330

    
331
  $to_array = array();
332
  if (!empty($form_state['values']['txt_to']['users'])) {
333
    $to_array = array_values($form_state['values']['txt_to']['users']);
334
  }
335

    
336
  if (!empty($form_state['values']['txt_to']['addrs'])) {
337
    // All new-lines are replaced by commas.
338
    $to_addrs = preg_replace('![\r|\n|,]+!', ',', trim($form_state['values']['txt_to']['addrs']));
339
    // Create an array from the string.
340
    $to_array = array_merge($to_array, explode(',', $to_addrs));
341
  }
342

    
343
  if (empty($to_array) && $print_mail_user_recipients_default) {
344
    form_set_error('txt_to', t('You must specify at least one email address or user as a recipient.'));
345
  }
346

    
347
  // Verify each element of the array.
348
  foreach ($to_array as $key => $address) {
349
    $address = trim($address);
350
    if (preg_match('/(.*?) <(.*)>/s', $address, $matches)) {
351
      // Address is of the type User Name <user@domain.tld>.
352
      $test = user_validate_mail($matches[2]);
353
      $to_array[$key] = trim($matches[1]) . ' <' . $matches[2] . '>';
354
    }
355
    else {
356
      // Address must be user@domain.tld.
357
      $test = user_validate_mail($address);
358
    }
359
    if ($test) {
360
      form_set_error('txt_to', $test);
361
    }
362
  }
363

    
364
  $print_mail_hourly_threshold = variable_get('print_mail_hourly_threshold', PRINT_MAIL_HOURLY_THRESHOLD);
365

    
366
  if ((!user_access('send unlimited emails')) && (!flood_is_allowed('print_mail', $print_mail_hourly_threshold - count($to_array) + 1))) {
367
    form_set_error('txt_to', t('You cannot send more than %number messages per hour. Please reduce the number of recipients.', array('%number' => $print_mail_hourly_threshold)));
368
  }
369

    
370
  // In all fields, prevent insertion of custom headers.
371
  foreach ($form_state['values'] as $key => $string) {
372
    if ((drupal_substr($key, 0, 4) == 'fld_') && ((strpos($string, "\n") !== FALSE) || (strpos($string, "\r") !== FALSE))) {
373
      form_set_error($key, 'Found invalid character');
374
    }
375
  }
376

    
377
  $form_state['values']['fld_from_addr'] = $from_addr;
378
  $form_state['values']['fld_from_name'] = trim($form_state['values']['fld_from_name']);
379
  // Re-create the string from the re-organized array.
380
  $form_state['values']['txt_to']['addrs'] = implode(', ', $to_array);
381
}
382

    
383
/**
384
 * Form submission handler for print_mail_form().
385
 *
386
 * @see print_mail_form()
387
 * @see print_controller()
388
 * @ingroup forms
389
 */
390
function print_mail_form_submit($form, &$form_state) {
391
  if (!array_key_exists('cancel', $form_state['values'])) {
392
    module_load_include('inc', 'print', 'print.pages');
393
    module_load_include('inc', 'print', 'includes/print');
394

    
395
    $link = print_mail_print_link();
396
    $cid = isset($form_state['values']['cid']) ? $form_state['values']['cid'] : NULL;
397
    $view_mode = $form_state['values']['chk_teaser'] ? 'teaser' : PRINT_VIEW_MODE;
398
    $node = print_controller($form_state['values']['path'], $link['format'], $cid, $view_mode);
399

    
400
    if ($node) {
401
      $print_mail_send_option_default = variable_get('print_mail_send_option_default', PRINT_MAIL_SEND_OPTION_DEFAULT);
402

    
403
      $params = array();
404
      $params['subject'] = $form_state['values']['fld_subject'];
405
      $params['message'] = t('Message from sender') . ':<br /><br /><em>' . nl2br(check_plain($form_state['values']['txt_message'])) . '</em>';
406
      $params['link'] = url($form_state['values']['path'], array('absolute' => TRUE, 'query' => $form_state['values']['query']));
407
      $params['title'] = $form_state['values']['title'];
408

    
409
      // If a name is provided, make From: in the format Common Name <address>.
410
      if (!empty($form_state['values']['fld_from_name'])) {
411
        $from = '"' . mime_header_encode($form_state['values']['fld_from_name']) . '" <' . $form_state['values']['fld_from_addr'] . '>';
412
      }
413
      else {
414
        $from = $form_state['values']['fld_from_addr'];
415
      }
416

    
417
      // If using reply-to, move the From: info to the params array, so that it
418
      // is passed to hook_mail later.
419
      if (variable_get('print_mail_use_reply_to', PRINT_MAIL_USE_REPLY_TO)) {
420
        $params['from'] = $from;
421
        $from = NULL;
422
      }
423

    
424
      // Spaces in img URLs must be replaced with %20.
425
      $pattern = '!<(img\s[^>]*?)>!is';
426
      $node->content = preg_replace_callback($pattern, '_print_replace_spaces', $node->content);
427

    
428
      $params['body'] = theme('print', array(
429
        'node' => $node,
430
        'query' => $form_state['values']['query'],
431
        'format' => $link['format'],
432
        'expand_css' => TRUE,
433
        'message' => $params['message'],
434
      ));
435

    
436
      // Img elements must be set to absolute.
437
      $pattern = '!<(img\s[^>]*?)>!is';
438
      $params['body'] = preg_replace_callback($pattern, '_print_rewrite_urls', $params['body']);
439

    
440
      // Convert the a href elements, to make sure no relative links remain.
441
      $pattern = '!<(a\s[^>]*?)>!is';
442
      $params['body'] = preg_replace_callback($pattern, '_print_rewrite_urls', $params['body']);
443

    
444
      // Enable support for third-party modules to alter the e-mail before
445
      // being sent.
446
      drupal_alter('print_mail', $params, $to);
447

    
448
      $ok = FALSE;
449
      $use_job_queue = variable_get('print_mail_job_queue', PRINT_MAIL_JOB_QUEUE_DEFAULT);
450
      $queue = NULL;
451
      if ($use_job_queue) {
452
        $queue = DrupalQueue::get('print_mail_send');
453
      }
454

    
455
      $addresses = explode(', ', $form_state['values']['txt_to']['addrs']);
456
      foreach ($addresses as $to) {
457
        $ret = array();
458
        if ($use_job_queue) {
459
          // Use job queue to send mails during cron runs.
460
          $queue->createItem(array(
461
            'module' => 'print_mail',
462
            'key' => $print_mail_send_option_default,
463
            'to' => $to,
464
            'language' => language_default(),
465
            'params' => $params,
466
            'from' => $from,
467
          ));
468
        }
469
        else {
470
          // Send mail immediately using Drupal's mail handler.
471
          $ret = drupal_mail('print_mail', $print_mail_send_option_default, $to, language_default(), $params, $from);
472
        }
473
        if ($use_job_queue || $ret['result']) {
474
          flood_register_event('print_mail');
475
          $ok = TRUE;
476
        }
477
      }
478
      if ($ok) {
479
        $query = empty($form_state['values']['query']) ? '' : '?' . rawurldecode(drupal_http_build_query($form_state['values']['query']));
480
        watchdog('print_mail', '%name [%from] sent %page to [%to]', array(
481
          '%name' => $form_state['values']['fld_from_name'],
482
          '%from' => $form_state['values']['fld_from_addr'],
483
          '%page' => $form_state['values']['path'] . $query,
484
          '%to' => $form_state['values']['txt_to']['addrs'],
485
        ));
486
        $site_name = variable_get('site_name', t('us'));
487
        drupal_set_message(check_plain(t('Thank you for spreading the word about !site.', array('!site' => $site_name))));
488

    
489
        $nodepath = drupal_get_normal_path($form_state['values']['path']);
490
        db_update('print_mail_page_counter')
491
          ->fields(array(
492
            'sentcount' => 1,
493
            'sent_timestamp' => REQUEST_TIME,
494
          ))
495
          ->condition('path', $nodepath, '=')
496
          ->expression('sentcount', 'sentcount + :inc', array(':inc' => count($addresses)))
497
          ->execute();
498
      }
499
    }
500
  }
501

    
502
  $form_state['redirect'] = array(
503
    preg_replace('!^book/export/html/!', 'node/', $form_state['values']['path']), array(
504
      'query' => $form_state['values']['query'],
505
    ),
506
  );
507
}