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