Projet

Général

Profil

Paste
Télécharger (13,2 ko) Statistiques
| Branche: | Révision:

root / htmltest / sites / all / modules / print / print_mail / print_mail.inc @ dc45a079

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
require_once(DRUPAL_ROOT . '/' . drupal_get_path('module', 'print') . '/print.pages.inc');
14

    
15
// Include MIME library
16
@include_once('Mail/mime.php');
17

    
18
/**
19
 * Menu callback for the send by email form.
20
 *
21
 * @ingroup forms
22
 */
23
function print_mail_form($form, &$form_state) {
24
  global $user;
25

    
26
  // Remove the printmail/ prefix
27
  $path_arr = explode('/', $_GET['q']);
28
  unset($path_arr[0]);
29
  $path = filter_xss(implode('/', $path_arr));
30
  if (empty($path)) {
31
    // If no path was provided, let's try to generate a page for the referer
32
    global $base_url;
33

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

    
55
  // Handle the query
56
  $query = $_GET;
57
  unset($query['q']);
58

    
59
  $print_mail_hourly_threshold = variable_get('print_mail_hourly_threshold', PRINT_MAIL_HOURLY_THRESHOLD);
60

    
61
  if ((!user_access('send unlimited emails')) && (!flood_is_allowed('print_mail', $print_mail_hourly_threshold))) {
62
    $form['flood'] = array(
63
      '#type' => 'markup',
64
      '#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>',
65
    );
66
    return $form;
67
  }
68

    
69
  $print_mail_teaser_default = variable_get('print_mail_teaser_default', PRINT_MAIL_TEASER_DEFAULT_DEFAULT);
70
  $print_mail_teaser_choice = variable_get('print_mail_teaser_choice', PRINT_MAIL_TEASER_CHOICE_DEFAULT);
71
  $form = array();
72

    
73
  $cid = isset($_GET['comment']) ? (int)$_GET['comment'] : NULL;
74
  $title = _print_get_title($path);
75

    
76
  if (count($form_state['input']) == 0) {
77
    $nodepath = drupal_get_normal_path($path);
78
    db_merge('print_mail_page_counter')
79
      ->key(array('path' => $nodepath))
80
      ->fields(array(
81
          'totalcount' => 1,
82
          'timestamp' => REQUEST_TIME,
83
      ))
84
      ->expression('totalcount', 'totalcount + 1')
85
      ->execute();
86
  }
87

    
88
  $form['path'] = array('#type' => 'value', '#value' => $path);
89
  $form['query'] = array('#type' => 'value', '#value' => $query);
90
  $form['cid'] =  array('#type' => 'value', '#value' => $cid);
91
  $form['title'] = array('#type' => 'value', '#value' => $title);
92

    
93
  $form['fld_from_addr'] = array(
94
    '#type' => 'textfield',
95
    '#title' => t('Your email'),
96
    '#size' => 62,
97
    '#required' => TRUE,
98
  );
99
  $form['fld_from_name'] = array(
100
    '#type' => 'textfield',
101
    '#title' => t('Your name'),
102
    '#size' => 62,
103
  );
104
  $form['txt_to_addrs'] = array(
105
    '#type' => 'textarea',
106
    '#title' => t('Send to'),
107
    '#rows' => 3,
108
    '#resizable' => FALSE,
109
    '#description' => t('Enter multiple addresses separated by commas and/or different lines.'),
110
    '#required' => TRUE,
111
  );
112
  $form['fld_subject'] = array(
113
    '#type' => 'textfield',
114
    '#title' => t('Subject'),
115
    '#size' => 62,
116
    '#required' => TRUE,
117
  );
118
  if (!empty($title)) {
119
    // To prevent useless translation strings, try to translate only node titles
120
    if (drupal_substr($path, 0, 5) == 'node/') {
121
      $title = t($title);
122
    }
123

    
124
    $form['fld_title'] = array(
125
      '#type' => 'item',
126
      '#title' => t('Page to be sent'),
127
      '#markup' => l($title, $path, array('attributes' => array('title' => t('View page')), 'query' => $query)),
128
    );
129
  }
130
  $form['txt_message'] = array(
131
    '#type' => 'textarea',
132
    '#title' => t('Your message'),
133
    '#rows' => 6,
134
    '#required' => TRUE,
135
  );
136

    
137
  if ($print_mail_teaser_choice) {
138
    $form['chk_teaser'] = array(
139
      '#type' => 'checkbox',
140
      '#title' => t('Send only the teaser'),
141
      '#default_value' => $print_mail_teaser_default,
142
    );
143
  }
144
  else {
145
    $form['chk_teaser'] = array('#type' => 'value', '#value' => $print_mail_teaser_default);
146
  }
147

    
148
  $form['btn_submit'] = array(
149
    '#name' => 'submit',
150
    '#type' => 'submit',
151
    '#value' => t('Send email'),
152
  );
153
  $form['btn_clear'] = array(
154
    '#type' => 'markup',
155
    '#markup' => '<input type="reset" name="clear" id="edit-btn-clear" value="' . t('Clear form') . '" class="form-submit" />',
156
  );
157
  $form['btn_cancel'] = array(
158
    '#name' => 'cancel',
159
    '#type' => 'submit',
160
    '#value' => t('Cancel'),
161
  );
162

    
163
  if ($user->uid != 0) {
164
    $user_name = check_plain(strip_tags(theme('username', array('account' => $user))));
165
    $form['fld_from_addr']['#default_value'] = $user->mail;
166
    $form['fld_from_addr']['#disabled'] = TRUE;
167
    $form['fld_from_addr']['#value'] = $user->mail;
168
    $form['fld_from_name']['#default_value'] = $user_name;
169
  }
170
  else {
171
    $user_name = t('Someone');
172
  }
173
  $site_name = variable_get('site_name', t('an interesting site'));
174
  $print_mail_text_subject = filter_xss(variable_get('print_mail_text_subject', t('!user has sent you a message from !site')));
175
  $form['fld_subject']['#default_value'] = t($print_mail_text_subject, array('!user' => $user_name, '!site' => $site_name, '!title' => $title));
176
  $print_mail_text_content = filter_xss(variable_get('print_mail_text_content', ''));
177
  $form['txt_message']['#default_value'] = t($print_mail_text_content);
178

    
179
  return $form;
180
}
181

    
182
/**
183
 * Theme function for the send by-email form submission.
184
 *
185
 * Adds a class to the form labels. This class is used to place the label on
186
 * the left of the input fields.
187
 *
188
 * @ingroup forms
189
 */
190
function theme_print_mail_form($variables) {
191
  $form = $variables['form'];
192

    
193
  drupal_add_css(drupal_get_path('module', 'print') . '/css/printlinks.css');
194
  $content = '';
195
  foreach (element_children($form) as $key) {
196
    $tmp = drupal_render($form[$key]);
197
    switch ($key) {
198
      case 'fld_from_addr':
199
      case 'fld_from_name':
200
      case 'txt_to_addrs':
201
      case 'fld_subject':
202
      case 'fld_title':
203
        $tmp = str_replace('<label', '<label class ="printmail-label"', $tmp);
204
        break;
205
    }
206
    $content .= $tmp;
207
  }
208
  return $content;
209
}
210

    
211
/**
212
 * Validate the send by-email form submission.
213
 *
214
 * @ingroup forms
215
 */
216
function print_mail_form_validate($form, &$form_state) {
217
  if (array_key_exists('cancel', $form_state['input'])) {
218
    form_set_error(NULL, '', TRUE);
219
    drupal_get_messages('error');
220

    
221
    drupal_goto(preg_replace('!^book/export/html/!', 'node/', $form_state['values']['path']), array('query' => $form_state['values']['query']));
222
    return;
223
  }
224
  $from_addr = trim($form_state['values']['fld_from_addr']);
225
  $test = user_validate_mail($from_addr);
226
  if ($test) {
227
    form_set_error('fld_from_addr', $test);
228
  }
229

    
230
  // All new-lines are replaced by commas
231
  $to_addrs = preg_replace('![\r|\n|,]+!', ',', trim($form_state['values']['txt_to_addrs']));
232
  // Create an array from the string
233
  $to_array = explode(',', $to_addrs);
234
  // Verify each element of the array
235
  foreach ($to_array as $key => $address) {
236
    $address = trim($address);
237
    if (preg_match('/(.*?) <(.*)>/s', $address, $matches)) {
238
      // Address is of the type User Name <user@domain.tld>
239
      $test = user_validate_mail($matches[2]);
240
      $to_array[$key] = trim($matches[1]) . ' <' . $matches[2] . '>';
241
    }
242
    else {
243
      // Address must be user@domain.tld
244
      $test = user_validate_mail($address);
245
    }
246
    if ($test) {
247
      form_set_error('txt_to_addrs', $test);
248
    }
249
  }
250

    
251
  $print_mail_hourly_threshold = variable_get('print_mail_hourly_threshold', PRINT_MAIL_HOURLY_THRESHOLD);
252

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

    
257
  // In all fields, prevent insertion of custom headers
258
  foreach ($form_state['values'] as $key => $string) {
259
    if ( (drupal_substr($key, 0, 4) == 'fld_') && ((strpos($string, "\n") !== FALSE) || (strpos($string, "\r") !== FALSE)) ) {
260
      form_set_error($key, 'Found invalid character');
261
    }
262
  }
263

    
264
  $form_state['values']['fld_from_addr'] = $from_addr;
265
  $form_state['values']['fld_from_name'] = trim($form_state['values']['fld_from_name']);
266
  // Re-create the string from the re-organized array
267
  $form_state['values']['txt_to_addrs'] = implode(', ', $to_array);
268
}
269

    
270
/**
271
 * Process the send by-email form submission.
272
 *
273
 * @ingroup forms
274
 */
275
function print_mail_form_submit($form, &$form_state) {
276
  if (!array_key_exists('cancel', $form_state['values'])) {
277
    $cid = isset($form_state['values']['cid']) ? $form_state['values']['cid'] : NULL;
278
    $print_mail_text_message = filter_xss_admin(variable_get('print_mail_text_message', t('Message from sender')));
279
    $sender_message = $print_mail_text_message . ':<br /><br /><em>' . nl2br(check_plain($form_state['values']['txt_message'])) . '</em>';
280

    
281
    $print = print_controller($form_state['values']['path'], $form_state['values']['query'], $cid, PRINT_MAIL_FORMAT, $form_state['values']['chk_teaser'], $sender_message);
282

    
283
    if ($print !== FALSE) {
284
      $print_mail_send_option_default = variable_get('print_mail_send_option_default', PRINT_MAIL_SEND_OPTION_DEFAULT);
285

    
286
      $params = array();
287
      $params['subject'] = $form_state['values']['fld_subject'];
288
      $params['message'] = $sender_message;
289
      $params['link'] = $print['url'];
290
      $params['title'] = $form_state['values']['title'];
291

    
292
      // If a name is provided, make From: in the format Common Name <address>
293
      if (!empty($form_state['values']['fld_from_name'])) {
294
        $from = '"' . mime_header_encode($form_state['values']['fld_from_name']) . '" <' . $form_state['values']['fld_from_addr'] . '>';
295
      }
296
      else {
297
        $from = $form_state['values']['fld_from_addr'];
298
      }
299

    
300
      // If using reply-to, move the From: info to the params array, so that it is passed to hook_mail later
301
      if (variable_get('print_mail_use_reply_to', PRINT_MAIL_USE_REPLY_TO)) {
302
        $params['from'] = $from;
303
        $from = NULL;
304
      }
305

    
306
      // Spaces in img URLs must be replaced with %20
307
      $pattern = '!<(img\s[^>]*?)>!is';
308
      $print['content'] = preg_replace_callback($pattern, '_print_replace_spaces', $print['content']);
309

    
310
      $node = $print['node'];
311
      $params['body'] = theme('print', array('print' => $print, 'type' => PRINT_MAIL_FORMAT, 'node' => $node));
312

    
313
      // Img elements must be set to absolute
314
      $pattern = '!<(img\s[^>]*?)>!is';
315
      $params['body'] = preg_replace_callback($pattern, '_print_rewrite_urls', $params['body']);
316

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

    
321
      $ok = FALSE;
322
      $use_job_queue = variable_get('print_mail_job_queue', PRINT_MAIL_JOB_QUEUE_DEFAULT);
323
      if ($use_job_queue) {
324
        $queue = DrupalQueue::get('print_mail_send');
325
      }
326

    
327
      $addresses = explode(', ', $form_state['values']['txt_to_addrs']);
328
      foreach ($addresses as $to) {
329
        if ($use_job_queue) {
330
          // Use job queue to send mails during cron runs
331
          $queue->createItem(array('module' => 'print_mail', 'key' => $print_mail_send_option_default, 'to' => $to, 'language' => language_default(), 'params' => $params, 'from' => $from));
332
        }
333
        else {
334
          // Send mail immediately using Drupal's mail handler
335
          $ret = drupal_mail('print_mail', $print_mail_send_option_default, $to, language_default(), $params, $from);
336
        }
337
        if ($use_job_queue || $ret['result']) {
338
          flood_register_event('print_mail');
339
          $ok = TRUE;
340
        }
341
      }
342
      if ($ok) {
343
        $query = empty($form_state['values']['query']) ? '' : '?' . rawurldecode(drupal_http_build_query($form_state['values']['query']));
344
        watchdog('print_mail', '%name [%from] sent %page to [%to]', array('%name' => $form_state['values']['fld_from_name'], '%from' => $form_state['values']['fld_from_addr'], '%page' => $form_state['values']['path'] . $query, '%to' => $form_state['values']['txt_to_addrs']));
345
        $site_name = variable_get('site_name', t('us'));
346
        $print_mail_text_confirmation = variable_get('print_mail_text_confirmation', t('Thank you for spreading the word about !site.'));
347
        drupal_set_message(check_plain(t($print_mail_text_confirmation, array('!site' => $site_name))));
348

    
349
        $nodepath = drupal_get_normal_path($form_state['values']['path']);
350
        db_update('print_mail_page_counter')
351
          ->fields(array(
352
              'sentcount' => 1,
353
              'sent_timestamp' => REQUEST_TIME,
354
          ))
355
          ->condition('path', $nodepath, '=')
356
          ->expression('sentcount', 'sentcount + :inc', array(':inc' => count($addresses)))
357
          ->execute();
358
      }
359
    }
360
  }
361

    
362
  $form_state['redirect'] = array(preg_replace('!^book/export/html/!', 'node/', $form_state['values']['path']), array('query' => $form_state['values']['query']));
363
}