email.module 12.9 KB
Newer Older
Matthias Hutterer's avatar
Matthias Hutterer committed
1
2
3
<?php


4
/**
5
 * Implements hook_field_info().
6
 */
7
function email_field_info() {
8
  return array(
9
10
    'email' => array(
      'label' => 'Email',
11
12
13
      'description' => t('This field stores and renders email addresses.'),
      'default_widget' => 'email_textfield',
      'default_formatter' => 'email_default',
14
      'property_type' => 'text',
15
16
17
    ),
  );
}
Matthias Hutterer's avatar
Matthias Hutterer committed
18

19
20
21
22
23
24
25
/**
 * Implements hook_migrate_api().
 */
function email_migrate_api() {
  return array('api' => 2);
}

Matthias Hutterer's avatar
Matthias Hutterer committed
26
/**
27
28
29
30
 * Implements hook_field_validate().
 *
 * Possible error codes:
 * - 'email_invalid': The email address is not valid
Matthias Hutterer's avatar
Matthias Hutterer committed
31
 */
32
33
34
35
36
37
38
39
40
function email_field_validate($obj_type, $object, $field, $instance, $langcode, $items, &$errors) {
  foreach ($items as $delta => $item) {
    if ($item['email'] != '' && !valid_email_address(trim($item['email']))) {
      $message = t('"%mail" is not a valid email address', array('%mail' => $item['email']));
      $errors[$field['field_name']][$langcode][$delta][] = array(
        'error' => "email_invalid",
        'message' => $message,
      );
    }
Matthias Hutterer's avatar
Matthias Hutterer committed
41
42
  }
}
43

44
45
46
47
48
49
/**
 * Implements hook_field_widget_error().
 */
function email_field_widget_error($element, $error, $form, &$form_state) {
  form_error($element, $error['message']);
}
50
51

/**
Matthias Hutterer's avatar
Matthias Hutterer committed
52
 * Implements hook_content_is_empty().
53
 */
54
function email_field_is_empty($item, $field) {
55
56
57
58
59
  if (empty($item['email'])) {
    return TRUE;
  }
  return FALSE;
}
Matthias Hutterer's avatar
Matthias Hutterer committed
60

61
/**
62
 * Implements hook_field_formatter_info().
63
64
65
 *
 */
function email_field_formatter_info() {
66
  $formats = array(
67
    'email_default' => array(
68
      'label' => t('Default email link'),
69
      'description' => t('Display the email address as a mailto link.'),
70
71
      'field types' => array('email'),
    ),
72
    'email_contact' => array(
73
      'label' => t('Email contact form'),
74
      'description' => t('Display a contact form.'),
75
76
      'field types' => array('email'),
    ),
77
    'email_plain' => array(
Matthias Hutterer's avatar
Matthias Hutterer committed
78
      'label' => t('Email plain text'),
79
      'description' => t('Display the email address as plain text.'),
Matthias Hutterer's avatar
Matthias Hutterer committed
80
81
      'field types' => array('email'),
    ),
82
  );
83
84
  if (module_exists('spamspan')) {
    $formats += array(
85
      'email_spamspan' => array(
86
87
88
        'label' => t('Email SpamSpan'),
        'field types' => array('email'),
      ),
89
90
91
    );
  }
  return $formats;
92
}
Matthias Hutterer's avatar
Matthias Hutterer committed
93

94
/**
95
 * Implements hook_field_formatter_view().
96
 */
97
98
99
function email_field_formatter_view($object_type, $object, $field, $instance, $langcode, $items, $display) {
  $element = array();
  switch ($display['type']) {
100
    case 'email_default':
101
      foreach ($items as $delta => $item) {
Matthias Hutterer's avatar
Matthias Hutterer committed
102
        $output = l($item['email'], 'mailto:' . $item['email']);
103
104
105
        $element[$delta] = array('#markup' => $output);
      }
      break;
Matthias Hutterer's avatar
Matthias Hutterer committed
106

107
    case 'email_contact':
108
      $ids = entity_extract_ids($object_type, $object);
109
      foreach ($items as $delta => $item) {
Matthias Hutterer's avatar
Matthias Hutterer committed
110
111
        $element[$delta] = array('#markup' => l(t('Contact person by email'), 'email/' . $object_type . '/' . $ids[0] . '/' . $instance['field_name']));
        // Since email is always sent to first item's email, break after any email address found.
112
        break;
113
114
      }
      break;
Matthias Hutterer's avatar
Matthias Hutterer committed
115

116
    case 'email_plain':
Matthias Hutterer's avatar
Matthias Hutterer committed
117
118
119
120
      foreach ($items as $delta => $item) {
        $element[$delta] = array('#markup' => check_plain($item['email']));
      }
      break;
Matthias Hutterer's avatar
Matthias Hutterer committed
121

122
    case 'email_spamspan':
123
124
      foreach ($items as $delta => $item) {
        if (module_exists('spamspan')) {
125
          $element[$delta] = array('#markup' => spamspan($item['email']));
126
127
        }
        else {
Matthias Hutterer's avatar
Matthias Hutterer committed
128
          $output = l($item['email'], 'mailto:' . $item['email']);
129
130
131
132
          $element[$delta] = array('#markup' => $output);
        }
      }
      break;
Matthias Hutterer's avatar
Matthias Hutterer committed
133
  }
Matthias Hutterer's avatar
Matthias Hutterer committed
134
  return $element;
135
136
}

Matthias Hutterer's avatar
Matthias Hutterer committed
137
138

/**
139
 * Implements hook_field_widget_info().
Matthias Hutterer's avatar
Matthias Hutterer committed
140
 */
141
function email_field_widget_info() {
Matthias Hutterer's avatar
Matthias Hutterer committed
142
  return array(
143
144
    'email_textfield' => array(
      'label' => t('Text field'),
Matthias Hutterer's avatar
Matthias Hutterer committed
145
      'field types' => array('email'),
146
      'settings' => array('size' => 60),
Matthias Hutterer's avatar
Matthias Hutterer committed
147
148
149
150
    ),
  );
}

151
/**
152
 * Implements hook_field_widget_settings_form().
153
 */
154
155
156
157
158
159
160
161
162
163
function email_field_widget_settings_form($field, $instance) {
  $widget = $instance['widget'];
  $settings = $widget['settings'];

  $form['size'] = array(
    '#type' => 'textfield',
    '#title' => t('Size of textfield'),
    '#default_value' => $settings['size'],
    '#required' => TRUE,
    '#element_validate' => array('_element_validate_integer_positive'),
164
  );
165
  return $form;
166
}
Matthias Hutterer's avatar
Matthias Hutterer committed
167
168

/**
169
 * Implements hook_field_widget_form().
Matthias Hutterer's avatar
Matthias Hutterer committed
170
 */
171
172
173
174
175
176
177
178
179
180
function email_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $base) {
  $element = $base;
  $element['email'] = $base + array(
    '#type' => 'textfield',
    '#default_value' => isset($items[$delta]['email']) ? $items[$delta]['email'] : NULL,
    '#size' => $instance['widget']['settings']['size'],
    '#prefix' => '<div class="text-full-wrapper">',
    '#suffix' => '</div>',
  );
  return $element;
Matthias Hutterer's avatar
Matthias Hutterer committed
181
182
}

183
/**
Matthias Hutterer's avatar
Matthias Hutterer committed
184
 * Implements hook_menu().
185
 */
186
function email_menu() {
187
  $items['email/%/%/%'] = array(
188
    'title' => 'Email Contact Form',
189
    'page callback' => 'email_mail_page',
190
    'page arguments' => array(1, 2, 3),
191
192
    'access callback' => 'user_access',
    'access arguments' => array('access content'),
193
194
    'type' => MENU_CALLBACK,
  );
195
196
  $items['admin/config/content/email'] = array(
    'title' => 'Email Contact Form Settings',
197
198
199
200
    'description' => 'Administer flood control settings for email contact forms',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('email_admin_settings'),
    'access arguments' => array('administer site configuration'),
201
    'type' => MENU_CALLBACK,
202
  );
203
204
205
  return $items;
}

206

207
208
209
/**
 * The contact form page.
 */
210
211
function email_mail_page($object_type, $object_id, $field_name) {
  global $user;
212

213
  if (!is_numeric($object_id)) {
214
    return MENU_NOT_FOUND;
215
  }
Matthias Hutterer's avatar
Matthias Hutterer committed
216

217
218
  //verify this is an email field
  $field_info = field_info_field($field_name);
219
220
  if (!isset($field_info) || $field_info['type'] != 'email') {
    return MENU_NOT_FOUND;
221
  }
Matthias Hutterer's avatar
Matthias Hutterer committed
222

223
224
  $objects = entity_load($object_type, array($object_id));
  $object = $objects[$object_id];
Matthias Hutterer's avatar
Matthias Hutterer committed
225

226
227
  //verify the object really exists
  if (!$object) {
228
    return MENU_NOT_FOUND;
229
  }
Matthias Hutterer's avatar
Matthias Hutterer committed
230

231
232
233
  // Check for field access.
  if (!field_access('view', $field_info, $object_type, $object, $user)) {
    return MENU_ACCESS_DENIED;
234
  }
Matthias Hutterer's avatar
Matthias Hutterer committed
235

236
237
  //use the first email address as receiver
  $field = array_pop($object->$field_name);
238
239
240
241
242
243
  foreach ($field as $delta => $item) {
    if (!empty($item['email'])) {
      $email = $item['email'];
      break;
    }
  }
244
245
246

  //verify that the email address is not empty
  if (empty($email)) {
247
    return MENU_NOT_FOUND;
248
  }
Matthias Hutterer's avatar
Matthias Hutterer committed
249

250
251
  $entity_info = entity_extract_ids($object_type, $object);
  $settings = field_info_instance($object_type, $field_name, $entity_info[2]);
Matthias Hutterer's avatar
Matthias Hutterer committed
252
  $found = FALSE;
253
254
  foreach ($settings['display'] as $display_name => $display_data) {
    if (isset($display_data['type']) && ($display_data['type'] === 'email_contact')) {
Matthias Hutterer's avatar
Matthias Hutterer committed
255
      $found = TRUE;
256
257
258
259
      break;
    }
  }
  if (!$found) {
260
    return MENU_NOT_FOUND;
261
  }
262
263

  if (!flood_is_allowed('email', variable_get('email_hourly_threshold', 3))) {
264
    return t("You cannot send more than %number messages per hour. Please try again later.", array('%number' => variable_get('email_hourly_threshold', 3)));
265
  }
Matthias Hutterer's avatar
Matthias Hutterer committed
266

267
  return drupal_get_form('email_mail_page_form', $object_type, $object_id, $field_name, $email);
268
269
}

270
271
272
/**
 * Contact form
 */
273
function email_mail_page_form($form, $form_state, $object_type, $object_id, $field_name, $email) {
Matthias Hutterer's avatar
Matthias Hutterer committed
274
  global $user;
275
276
277
278
279
280

  $form['object_id'] = array(
    '#type' => 'value',
    '#value' => $object_id,
  );
  $form['object_type'] = array(
281
    '#type' => 'value',
282
    '#value' => $object_type,
283
  );
284
  $form['field_name'] = array(
285
    '#type' => 'value',
286
287
288
289
290
    '#value' => $field_name,
  );
  $form['email'] = array(
    '#type' => 'value',
    '#value' => $email,
291
292
293
  );
  $form['name'] = array(
    '#type' => 'textfield',
Matthias Hutterer's avatar
Matthias Hutterer committed
294
295
    '#title' => t('Your name'),
    '#maxlength' => 255,
296
    '#default_value' => $user->uid ? $user->name : '',
Matthias Hutterer's avatar
Matthias Hutterer committed
297
298
    '#required' => TRUE,
  );
299
300
  $form['mail'] = array(
    '#type' => 'textfield',
Matthias Hutterer's avatar
Matthias Hutterer committed
301
302
    '#title' => t('Your e-mail address'),
    '#maxlength' => 255,
303
    '#default_value' => $user->uid ? $user->mail : '',
Matthias Hutterer's avatar
Matthias Hutterer committed
304
305
    '#required' => TRUE,
  );
306
307
  $form['subject'] = array(
    '#type' => 'textfield',
Matthias Hutterer's avatar
Matthias Hutterer committed
308
309
310
311
    '#title' => t('Subject'),
    '#maxlength' => 255,
    '#required' => TRUE,
  );
312
313
  $form['message'] = array(
    '#type' => 'textarea',
Matthias Hutterer's avatar
Matthias Hutterer committed
314
315
316
    '#title' => t('Message'),
    '#required' => TRUE,
  );
317
318
  $form['submit'] = array(
    '#type' => 'submit',
Matthias Hutterer's avatar
Matthias Hutterer committed
319
    '#value' => t('Send e-mail'),
320
321
    '#validate' => array('email_mail_page_form_validate'),
    '#submit' => array('email_mail_page_form_submit'),
Matthias Hutterer's avatar
Matthias Hutterer committed
322
323
324
325
  );
  return $form;
}

326
327
328
/**
 * Validate the site-wide contact page form submission.
 */
329
330
function email_mail_page_form_validate($form, &$form_state) {
  if (!valid_email_address($form_state['values']['mail'])) {
331
332
    form_set_error('mail', t('You must enter a valid e-mail address.'));
  }
333
  if (preg_match("/\r|\n/", $form_state['values']['subject'])) {
Matthias Hutterer's avatar
Matthias Hutterer committed
334
    form_set_error('subject', t('The subject cannot contain linebreaks.'));
Matthias Hutterer's avatar
Matthias Hutterer committed
335
    watchdog('mail', 'Email injection exploit attempted in email form subject: ' . check_plain($form_state['values']['subject']), WATCHDOG_NOTICE);
Matthias Hutterer's avatar
Matthias Hutterer committed
336
  }
337
338
339
340
341
}

/**
 * Process the site-wide contact page form submission.
 */
342
function email_mail_page_form_submit($form, &$form_state) {
343
344
  $object_type = $form_state['values']['object_type'];
  $object_id = $form_state['values']['object_id'];
345
346
  $field_name = $form_state['values']['field_name'];
  $email = $form_state['values']['email'];
Matthias Hutterer's avatar
Matthias Hutterer committed
347
348

  // Load entity
349
350
351
  $objects = entity_load($object_type, array($object_id));
  $object = $objects[$object_id];
  $object_info = entity_get_info($object_type);
Matthias Hutterer's avatar
Matthias Hutterer committed
352

353
354
355
  // E-mail address of the sender: as the form field is a text field,
  // all instances of \r and \n have been automatically stripped from it.

356
  $from = $form_state['values']['mail'];
Matthias Hutterer's avatar
Matthias Hutterer committed
357

358
  $params['object'] = $object;
359
360
361
  $params['subject'] = $form_state['values']['subject'];
  $params['name'] = $form_state['values']['name'];
  $params['message'] = $form_state['values']['message'];
Matthias Hutterer's avatar
Matthias Hutterer committed
362

363
364
365
366
367
  $path = "";
  if (isset($object_info['path callback']) && function_exists($object_info['path callback'])) {
    $path = $object_info['path callback']($object);
  }
  $params['url'] = url($path, array('absolute' => TRUE));
Matthias Hutterer's avatar
Matthias Hutterer committed
368

369
  // Send the e-mail to the recipients:
370
  drupal_mail('email', 'contact', $email, language_default(), $params, $from);
Matthias Hutterer's avatar
Matthias Hutterer committed
371

372
373
  // Log the operation:
  flood_register_event('email');
374
  watchdog('mail', t('%name-from sent an e-mail at %form.', array('%name-from' => $form_state['values']['name'], '%form' => url($_GET['q'], array('absolute' => TRUE)))));
Matthias Hutterer's avatar
Matthias Hutterer committed
375

376
  drupal_set_message(t('Your message has been sent.'));
377
  $form_state['redirect'] = $path;
378
379
380
}

/**
Matthias Hutterer's avatar
Matthias Hutterer committed
381
 * Implements hook_mail().
382
383
384
 */
function email_mail($key, &$message, $params) {
  $language = $message['language'];
Matthias Hutterer's avatar
Matthias Hutterer committed
385
  switch ($key) {
386
387
    case 'contact':
      // Compose the body:
388
389
      $message['body'][] = t('@name sent a message using the contact form at @url.', array('@name' => $params['name'], '@url' => $params['url']), array('langcode' =>$language->language));
      $message['body'][] = $params['message'];
Matthias Hutterer's avatar
Matthias Hutterer committed
390
391
392
      $message['subject'] = "";

      // Include the title of the entity, if one exists
393
394
      $object = $params['object'];
      if (isset($object->title) && !empty($object->title)) {
Matthias Hutterer's avatar
Matthias Hutterer committed
395
        $message['subject'] = "[" . check_plain(preg_replace("/\r|\n/", '', $object->title)) . "]";
396
397
      }
      $message['subject'] .= " " . check_plain($params['subject']);
398
399
      break;
  }
400
}
Matthias Hutterer's avatar
Matthias Hutterer committed
401
402


403
404
//TODO Token support

Matthias Hutterer's avatar
Matthias Hutterer committed
405
/**
Matthias Hutterer's avatar
Matthias Hutterer committed
406
 * Implements hook_token_list().
407
 *
Matthias Hutterer's avatar
Matthias Hutterer committed
408
409
function email_token_list($type = 'all') {
  if ($type == 'field' || $type == 'all') {
410
411
    $tokens['email']['raw']       = t('Raw email address');
    $tokens['email']['formatted'] = t('Formatted email address');
Matthias Hutterer's avatar
Matthias Hutterer committed
412
413
414
415
416
    return $tokens;
  }
}

/**
Matthias Hutterer's avatar
Matthias Hutterer committed
417
 * Implements hook token_values().
418
 *
Matthias Hutterer's avatar
Matthias Hutterer committed
419
420
421
function email_token_values($type, $object = NULL, $options = array()) {
  if ($type == 'field') {
    $item = $object[0];
422
423
    $tokens['raw']       = $item['email'];
    $tokens['formatted'] = $item['view'];
Matthias Hutterer's avatar
Matthias Hutterer committed
424
425
426
    return $tokens;
  }
}
Matthias Hutterer's avatar
Matthias Hutterer committed
427
428
 */

429
430
431
432
433
434
435
436
437
438
439
440
/**
 * Settings for contact form
 */
function email_admin_settings() {
  $form['email_hourly_threshold'] = array('#type' => 'select',
    '#title' => t('Hourly threshold for a CCK Email contact form'),
    '#options' => drupal_map_assoc(array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 30, 40, 50)),
    '#default_value' => variable_get('email_hourly_threshold', 3),
    '#description' => t('The maximum number of contact form submissions a user can perform per hour.'),
  );
  return system_settings_form($form);
}
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457


/**
 * Implements hook_content_migrate_instance_alter().
 *
 * D6-D7 upgrade
 * fixes new formatter names
 */
function email_content_migrate_instance_alter(&$instance_value, &$field_value) {
  if ($field_value['module'] == 'email') {
    foreach ($instance_value['display'] as $context => $settings) {
      if (in_array($instance_value['display'][$context]['type'], array('default', 'plain', 'contact', 'spamspan'))) {
        $instance_value['display'][$context]['type'] = 'email_' . $instance_value['display'][$context]['type'];
      }
    }
  }
}