1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Module file for the email module, which creates a email address field.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Implements hook_field_info().
|
10
|
*/
|
11
|
function email_field_info() {
|
12
|
return array(
|
13
|
'email' => array(
|
14
|
'label' => 'Email',
|
15
|
'description' => t('This field stores and renders email addresses.'),
|
16
|
'default_widget' => 'email_textfield',
|
17
|
'default_formatter' => 'email_default',
|
18
|
'property_type' => 'text',
|
19
|
),
|
20
|
);
|
21
|
}
|
22
|
|
23
|
/**
|
24
|
* Implements hook_migrate_api().
|
25
|
*/
|
26
|
function email_migrate_api() {
|
27
|
return array(
|
28
|
'api' => 2,
|
29
|
'field handlers' => array('MigrateEmailFieldHandler'),
|
30
|
);
|
31
|
}
|
32
|
|
33
|
/**
|
34
|
* Implements hook_field_validate().
|
35
|
*
|
36
|
* Possible error codes:
|
37
|
* - 'email_invalid': The email address is not valid
|
38
|
*/
|
39
|
function email_field_validate($obj_type, $object, $field, $instance, $langcode, $items, &$errors) {
|
40
|
foreach ($items as $delta => $item) {
|
41
|
if ($item['email'] != '' && !valid_email_address(trim($item['email']))) {
|
42
|
$message = t('"%mail" is not a valid email address', array('%mail' => $item['email']));
|
43
|
$errors[$field['field_name']][$langcode][$delta][] = array(
|
44
|
'error' => "email_invalid",
|
45
|
'message' => $message,
|
46
|
);
|
47
|
}
|
48
|
}
|
49
|
}
|
50
|
|
51
|
/**
|
52
|
* Implements hook_field_widget_error().
|
53
|
*/
|
54
|
function email_field_widget_error($element, $error, $form, &$form_state) {
|
55
|
form_error($element, $error['message']);
|
56
|
}
|
57
|
|
58
|
/**
|
59
|
* Implements hook_content_is_empty().
|
60
|
*/
|
61
|
function email_field_is_empty($item, $field) {
|
62
|
if (empty($item['email'])) {
|
63
|
return TRUE;
|
64
|
}
|
65
|
return FALSE;
|
66
|
}
|
67
|
|
68
|
/**
|
69
|
* Implements hook_field_formatter_info().
|
70
|
*
|
71
|
*/
|
72
|
function email_field_formatter_info() {
|
73
|
$formats = array(
|
74
|
'email_default' => array(
|
75
|
'label' => t('Default email link'),
|
76
|
'description' => t('Display the email address as a mailto link.'),
|
77
|
'field types' => array('email'),
|
78
|
),
|
79
|
'email_contact' => array(
|
80
|
'label' => t('Email contact form'),
|
81
|
'description' => t('Display a contact form.'),
|
82
|
'field types' => array('email'),
|
83
|
),
|
84
|
'email_plain' => array(
|
85
|
'label' => t('Email plain text'),
|
86
|
'description' => t('Display the email address as plain text.'),
|
87
|
'field types' => array('email'),
|
88
|
),
|
89
|
);
|
90
|
if (module_exists('spamspan')) {
|
91
|
$formats += array(
|
92
|
'email_spamspan' => array(
|
93
|
'label' => t('Email SpamSpan'),
|
94
|
'field types' => array('email'),
|
95
|
),
|
96
|
);
|
97
|
}
|
98
|
return $formats;
|
99
|
}
|
100
|
|
101
|
/**
|
102
|
* Implements hook_field_formatter_view().
|
103
|
*/
|
104
|
function email_field_formatter_view($object_type, $object, $field, $instance, $langcode, $items, $display) {
|
105
|
$element = array();
|
106
|
switch ($display['type']) {
|
107
|
case 'email_default':
|
108
|
foreach ($items as $delta => $item) {
|
109
|
$output = l($item['email'], 'mailto:' . $item['email']);
|
110
|
$element[$delta] = array('#markup' => $output);
|
111
|
}
|
112
|
break;
|
113
|
|
114
|
case 'email_contact':
|
115
|
$ids = entity_extract_ids($object_type, $object);
|
116
|
foreach ($items as $delta => $item) {
|
117
|
$element[$delta] = array('#markup' => l(t('Contact person by email'), 'email/' . $object_type . '/' . $ids[0] . '/' . $instance['field_name']));
|
118
|
// Since email is always sent to first item's email, break after any email address found.
|
119
|
break;
|
120
|
}
|
121
|
break;
|
122
|
|
123
|
case 'email_plain':
|
124
|
foreach ($items as $delta => $item) {
|
125
|
$element[$delta] = array('#markup' => check_plain($item['email']));
|
126
|
}
|
127
|
break;
|
128
|
|
129
|
case 'email_spamspan':
|
130
|
foreach ($items as $delta => $item) {
|
131
|
if (module_exists('spamspan')) {
|
132
|
$element[$delta] = array('#markup' => spamspan($item['email']));
|
133
|
}
|
134
|
else {
|
135
|
$output = l($item['email'], 'mailto:' . $item['email']);
|
136
|
$element[$delta] = array('#markup' => $output);
|
137
|
}
|
138
|
}
|
139
|
break;
|
140
|
}
|
141
|
return $element;
|
142
|
}
|
143
|
|
144
|
|
145
|
/**
|
146
|
* Implements hook_field_widget_info().
|
147
|
*/
|
148
|
function email_field_widget_info() {
|
149
|
return array(
|
150
|
'email_textfield' => array(
|
151
|
'label' => t('Text field'),
|
152
|
'field types' => array('email'),
|
153
|
'settings' => array('size' => 60),
|
154
|
),
|
155
|
);
|
156
|
}
|
157
|
|
158
|
/**
|
159
|
* Implements hook_field_widget_settings_form().
|
160
|
*/
|
161
|
function email_field_widget_settings_form($field, $instance) {
|
162
|
$widget = $instance['widget'];
|
163
|
$settings = $widget['settings'];
|
164
|
|
165
|
$form['size'] = array(
|
166
|
'#type' => 'textfield',
|
167
|
'#title' => t('Size of textfield'),
|
168
|
'#default_value' => $settings['size'],
|
169
|
'#required' => TRUE,
|
170
|
'#element_validate' => array('_element_validate_integer_positive'),
|
171
|
);
|
172
|
return $form;
|
173
|
}
|
174
|
|
175
|
/**
|
176
|
* Implements hook_field_widget_form().
|
177
|
*/
|
178
|
function email_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $base) {
|
179
|
$element = $base;
|
180
|
$element['email'] = $base + array(
|
181
|
'#type' => 'textfield',
|
182
|
'#default_value' => isset($items[$delta]['email']) ? $items[$delta]['email'] : NULL,
|
183
|
'#size' => $instance['widget']['settings']['size'],
|
184
|
'#prefix' => '<div class="text-full-wrapper">',
|
185
|
'#suffix' => '</div>',
|
186
|
);
|
187
|
return $element;
|
188
|
}
|
189
|
|
190
|
/**
|
191
|
* Implements hook_menu().
|
192
|
*/
|
193
|
function email_menu() {
|
194
|
$items['email/%/%/%'] = array(
|
195
|
'title' => 'Email Contact Form',
|
196
|
'page callback' => 'email_mail_page',
|
197
|
'page arguments' => array(1, 2, 3),
|
198
|
'access callback' => 'user_access',
|
199
|
'access arguments' => array('access content'),
|
200
|
'type' => MENU_CALLBACK,
|
201
|
);
|
202
|
$items['admin/config/content/email'] = array(
|
203
|
'title' => 'Email Contact Form Settings',
|
204
|
'description' => 'Administer flood control settings for email contact forms',
|
205
|
'page callback' => 'drupal_get_form',
|
206
|
'page arguments' => array('email_admin_settings'),
|
207
|
'access arguments' => array('administer site configuration'),
|
208
|
'type' => MENU_NORMAL_ITEM,
|
209
|
);
|
210
|
return $items;
|
211
|
}
|
212
|
|
213
|
/**
|
214
|
* Access callback for the email contact page.
|
215
|
*
|
216
|
* Checks whether the current user has view access to the entity. Access checks
|
217
|
* are performed for the fieldable core entity types, including nodes, users,
|
218
|
* comments and taxonomy terms. Furthermore entity types using Entity API's
|
219
|
* access system are supported. For custom entity types that are not using the
|
220
|
* Entity API, at least the access content permission is checked in the menu
|
221
|
* access callback.
|
222
|
*
|
223
|
* This function is called within the email page callback, as it takes care of
|
224
|
* loading the entity itself. If the entity is found, access checks are
|
225
|
* performed with this function.
|
226
|
*
|
227
|
* @param $entity_type
|
228
|
* The entity type
|
229
|
* @param $entity
|
230
|
* The entity for which the access should be checked
|
231
|
* @param $field_info
|
232
|
* The field info for the email field.
|
233
|
*
|
234
|
* @return TRUE if the current user has view access, otherwise FALSE.
|
235
|
*/
|
236
|
function email_mail_page_access($entity_type, $entity, $field_info) {
|
237
|
// Check for field access.
|
238
|
if (!field_access('view', $field_info, $entity_type, $entity)) {
|
239
|
return FALSE;
|
240
|
}
|
241
|
|
242
|
// Check the access for fieldable core entities, including nodes, users,
|
243
|
// comments and taxonomy terms.
|
244
|
if ($entity_type == 'node') {
|
245
|
return node_access('view', $entity);
|
246
|
}
|
247
|
elseif ($entity_type == 'user') {
|
248
|
global $user;
|
249
|
if ($entity->uid == $user->uid && $entity->uid) {
|
250
|
return TRUE;
|
251
|
}
|
252
|
if (user_access('administer users') || (user_access('access user profiles') && $entity->status)) {
|
253
|
return TRUE;
|
254
|
}
|
255
|
return FALSE;
|
256
|
}
|
257
|
elseif ($entity_type == 'comment') {
|
258
|
return comment_access('view', $entity);
|
259
|
}
|
260
|
elseif ($entity_type == 'taxonomy_term') {
|
261
|
if (user_access('administer taxonomy') || user_access('access content')) {
|
262
|
return TRUE;
|
263
|
}
|
264
|
return FALSE;
|
265
|
}
|
266
|
|
267
|
// Use Entity API for checking the view access for non-core entity types, if
|
268
|
// the module is installed.
|
269
|
if (module_exists('entity')) {
|
270
|
return entity_access('view', $entity_type, $entity);
|
271
|
}
|
272
|
return TRUE;
|
273
|
}
|
274
|
|
275
|
/**
|
276
|
* The contact form page.
|
277
|
*/
|
278
|
function email_mail_page($object_type, $object_id, $field_name) {
|
279
|
if (!is_numeric($object_id)) {
|
280
|
return MENU_NOT_FOUND;
|
281
|
}
|
282
|
// Verify this is an email field.
|
283
|
$field_info = field_info_field($field_name);
|
284
|
if (!isset($field_info) || $field_info['type'] != 'email') {
|
285
|
return MENU_NOT_FOUND;
|
286
|
}
|
287
|
|
288
|
// Check that the entity exists.
|
289
|
$objects = entity_load($object_type, array($object_id));
|
290
|
if (!isset($objects[$object_id])) {
|
291
|
return MENU_NOT_FOUND;
|
292
|
}
|
293
|
$object = $objects[$object_id];
|
294
|
|
295
|
// Check that the entity has the email field.
|
296
|
if (!isset($object->$field_name)) {
|
297
|
return MENU_NOT_FOUND;
|
298
|
}
|
299
|
|
300
|
// Check if the current user has access to the entity and to the field.
|
301
|
if (!email_mail_page_access($object_type, $object, $field_info)) {
|
302
|
return MENU_ACCESS_DENIED;
|
303
|
}
|
304
|
|
305
|
//use the first email address as receiver
|
306
|
$field = array_pop($object->$field_name);
|
307
|
foreach ($field as $delta => $item) {
|
308
|
if (!empty($item['email'])) {
|
309
|
$email = $item['email'];
|
310
|
break;
|
311
|
}
|
312
|
}
|
313
|
|
314
|
//verify that the email address is not empty
|
315
|
if (empty($email)) {
|
316
|
return MENU_NOT_FOUND;
|
317
|
}
|
318
|
|
319
|
$entity_info = entity_extract_ids($object_type, $object);
|
320
|
$settings = field_info_instance($object_type, $field_name, $entity_info[2]);
|
321
|
$found = FALSE;
|
322
|
foreach ($settings['display'] as $display_name => $display_data) {
|
323
|
if (isset($display_data['type']) && ($display_data['type'] === 'email_contact')) {
|
324
|
$found = TRUE;
|
325
|
break;
|
326
|
}
|
327
|
}
|
328
|
if (!$found) {
|
329
|
return MENU_NOT_FOUND;
|
330
|
}
|
331
|
|
332
|
if (!flood_is_allowed('email', variable_get('email_hourly_threshold', 3))) {
|
333
|
return t("You cannot send more than %number messages per hour. Please try again later.", array('%number' => variable_get('email_hourly_threshold', 3)));
|
334
|
}
|
335
|
|
336
|
return drupal_get_form('email_mail_page_form', $object_type, $object_id, $field_name, $email);
|
337
|
}
|
338
|
|
339
|
/**
|
340
|
* Contact form
|
341
|
*/
|
342
|
function email_mail_page_form($form, $form_state, $object_type, $object_id, $field_name, $email) {
|
343
|
global $user;
|
344
|
|
345
|
$form['object_id'] = array(
|
346
|
'#type' => 'value',
|
347
|
'#value' => $object_id,
|
348
|
);
|
349
|
$form['object_type'] = array(
|
350
|
'#type' => 'value',
|
351
|
'#value' => $object_type,
|
352
|
);
|
353
|
$form['field_name'] = array(
|
354
|
'#type' => 'value',
|
355
|
'#value' => $field_name,
|
356
|
);
|
357
|
$form['email'] = array(
|
358
|
'#type' => 'value',
|
359
|
'#value' => $email,
|
360
|
);
|
361
|
$form['name'] = array(
|
362
|
'#type' => 'textfield',
|
363
|
'#title' => t('Your name'),
|
364
|
'#maxlength' => 255,
|
365
|
'#default_value' => $user->uid ? $user->name : '',
|
366
|
'#required' => TRUE,
|
367
|
);
|
368
|
$form['mail'] = array(
|
369
|
'#type' => 'textfield',
|
370
|
'#title' => t('Your e-mail address'),
|
371
|
'#maxlength' => 255,
|
372
|
'#default_value' => $user->uid ? $user->mail : '',
|
373
|
'#required' => TRUE,
|
374
|
);
|
375
|
$form['subject'] = array(
|
376
|
'#type' => 'textfield',
|
377
|
'#title' => t('Subject'),
|
378
|
'#maxlength' => 255,
|
379
|
'#required' => TRUE,
|
380
|
);
|
381
|
$form['message'] = array(
|
382
|
'#type' => 'textarea',
|
383
|
'#title' => t('Message'),
|
384
|
'#required' => TRUE,
|
385
|
);
|
386
|
$form['submit'] = array(
|
387
|
'#type' => 'submit',
|
388
|
'#value' => t('Send e-mail'),
|
389
|
'#validate' => array('email_mail_page_form_validate'),
|
390
|
'#submit' => array('email_mail_page_form_submit'),
|
391
|
);
|
392
|
return $form;
|
393
|
}
|
394
|
|
395
|
/**
|
396
|
* Validate the site-wide contact page form submission.
|
397
|
*/
|
398
|
function email_mail_page_form_validate($form, &$form_state) {
|
399
|
if (!valid_email_address($form_state['values']['mail'])) {
|
400
|
form_set_error('mail', t('You must enter a valid e-mail address.'));
|
401
|
}
|
402
|
if (preg_match("/\r|\n/", $form_state['values']['subject'])) {
|
403
|
form_set_error('subject', t('The subject cannot contain linebreaks.'));
|
404
|
watchdog('mail', 'Email injection exploit attempted in email form subject: ' . check_plain($form_state['values']['subject']), WATCHDOG_NOTICE);
|
405
|
}
|
406
|
}
|
407
|
|
408
|
/**
|
409
|
* Process the site-wide contact page form submission.
|
410
|
*/
|
411
|
function email_mail_page_form_submit($form, &$form_state) {
|
412
|
$object_type = $form_state['values']['object_type'];
|
413
|
$object_id = $form_state['values']['object_id'];
|
414
|
$field_name = $form_state['values']['field_name'];
|
415
|
$email = $form_state['values']['email'];
|
416
|
|
417
|
// Load entity
|
418
|
$objects = entity_load($object_type, array($object_id));
|
419
|
$object = $objects[$object_id];
|
420
|
$object_info = entity_get_info($object_type);
|
421
|
|
422
|
// E-mail address of the sender: as the form field is a text field,
|
423
|
// all instances of \r and \n have been automatically stripped from it.
|
424
|
|
425
|
$from = $form_state['values']['mail'];
|
426
|
|
427
|
$params['object'] = $object;
|
428
|
$params['subject'] = $form_state['values']['subject'];
|
429
|
$params['name'] = $form_state['values']['name'];
|
430
|
$params['message'] = $form_state['values']['message'];
|
431
|
|
432
|
$path = "";
|
433
|
if (isset($object_info['path callback']) && function_exists($object_info['path callback'])) {
|
434
|
$path = $object_info['path callback']($object);
|
435
|
}
|
436
|
$params['url'] = url($path, array('absolute' => TRUE));
|
437
|
|
438
|
// Send the e-mail to the recipients:
|
439
|
drupal_mail('email', 'contact', $email, language_default(), $params, $from);
|
440
|
|
441
|
// Log the operation:
|
442
|
flood_register_event('email');
|
443
|
watchdog('mail', '%name-from sent an e-mail at %form.', array('%name-from' => $form_state['values']['name'], '%form' => url($_GET['q'], array('absolute' => TRUE))));
|
444
|
|
445
|
drupal_set_message(t('Your message has been sent.'));
|
446
|
$form_state['redirect'] = $path;
|
447
|
}
|
448
|
|
449
|
/**
|
450
|
* Implements hook_mail().
|
451
|
*/
|
452
|
function email_mail($key, &$message, $params) {
|
453
|
$language = $message['language'];
|
454
|
switch ($key) {
|
455
|
case 'contact':
|
456
|
// Compose the body:
|
457
|
$message['body'][] = t('@name sent a message using the contact form at @url.', array('@name' => $params['name'], '@url' => $params['url']), array('langcode' => $language->language));
|
458
|
$message['body'][] = $params['message'];
|
459
|
$message['subject'] = "";
|
460
|
|
461
|
// Include the title of the entity, if one exists
|
462
|
$object = $params['object'];
|
463
|
if (isset($object->title) && !empty($object->title)) {
|
464
|
$message['subject'] = "[" . check_plain(preg_replace("/\r|\n/", '', $object->title)) . "]";
|
465
|
}
|
466
|
$message['subject'] .= " " . check_plain($params['subject']);
|
467
|
break;
|
468
|
}
|
469
|
}
|
470
|
|
471
|
|
472
|
//TODO Token support
|
473
|
|
474
|
/**
|
475
|
* Implements hook_token_list().
|
476
|
*
|
477
|
function email_token_list($type = 'all') {
|
478
|
if ($type == 'field' || $type == 'all') {
|
479
|
$tokens['email']['raw'] = t('Raw email address');
|
480
|
$tokens['email']['formatted'] = t('Formatted email address');
|
481
|
return $tokens;
|
482
|
}
|
483
|
}
|
484
|
|
485
|
/**
|
486
|
* Implements hook token_values().
|
487
|
*
|
488
|
function email_token_values($type, $object = NULL, $options = array()) {
|
489
|
if ($type == 'field') {
|
490
|
$item = $object[0];
|
491
|
$tokens['raw'] = $item['email'];
|
492
|
$tokens['formatted'] = $item['view'];
|
493
|
return $tokens;
|
494
|
}
|
495
|
}
|
496
|
*/
|
497
|
|
498
|
/**
|
499
|
* Settings for contact form
|
500
|
*/
|
501
|
function email_admin_settings() {
|
502
|
$form['email_hourly_threshold'] = array('#type' => 'select',
|
503
|
'#title' => t('Hourly threshold for a CCK Email contact form'),
|
504
|
'#options' => drupal_map_assoc(array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 30, 40, 50)),
|
505
|
'#default_value' => variable_get('email_hourly_threshold', 3),
|
506
|
'#description' => t('The maximum number of contact form submissions a user can perform per hour.'),
|
507
|
);
|
508
|
return system_settings_form($form);
|
509
|
}
|
510
|
|
511
|
|
512
|
/**
|
513
|
* Implements hook_content_migrate_instance_alter().
|
514
|
*
|
515
|
* D6-D7 upgrade
|
516
|
* fixes new formatter names
|
517
|
*/
|
518
|
function email_content_migrate_instance_alter(&$instance_value, &$field_value) {
|
519
|
if ($field_value['module'] == 'email') {
|
520
|
foreach ($instance_value['display'] as $context => $settings) {
|
521
|
if (in_array($instance_value['display'][$context]['type'], array('default', 'plain', 'contact', 'spamspan'))) {
|
522
|
$instance_value['display'][$context]['type'] = 'email_' . $instance_value['display'][$context]['type'];
|
523
|
}
|
524
|
}
|
525
|
}
|
526
|
}
|