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

Matthias Hutterer's avatar
Matthias Hutterer committed
3
// $Id$
Matthias Hutterer's avatar
Matthias Hutterer committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

/**
 * Implementation of hook_help().
 */
function email_help($section) {
  switch ($section) {
    case 'admin/modules#description':
      return t('Defines a field type for email addresses. <em>Note: Requires content.module.</em>');
  }
}


/**
 * Implementation of hook_field_info().
 */
function email_field_info() {
  return array(
    'email' => array('label' => t('E-Mail')),
   );
}


/**
 * Implementation of hook_field_settings().
 */
function email_field_settings($op, $field) {
30
  switch ($op) {
Matthias Hutterer's avatar
Matthias Hutterer committed
31
32
33
34
35
36
37
38
39
40
41
42
43
44
    case 'database columns':
      $columns = array(
        'email' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE),
      );
      return $columns;
  }
}


/**
 * Implementation of hook_field().
 */
function email_field($op, &$node, $field, &$node_field, $teaser, $page) {
  switch ($op) {
45
    case 'view':     
Matthias Hutterer's avatar
Matthias Hutterer committed
46
      foreach ($node_field as $delta => $item) {
47
        $node_field[$delta]['view'] = content_format($field, $item, 'email', $node);
Matthias Hutterer's avatar
Matthias Hutterer committed
48
49
50
51
52
      }
      return theme('field', $node, $field, $node_field, $teaser, $page);
  }
}

53
54
55
56
57
/**
 * Implementation of hook_field_formatter_info().
 *
 */
function email_field_formatter_info() {
58
  $formats = array(
59
60
61
62
63
64
    'default' => array(
      'label' => 'Default Email-Link',
      'field types' => array('email'),
    ),
    'contact' => array(
      'label' => 'Email-Contact Form',
65
66
67
      'field types' => array('email'),
    ),
  );
68
69
70
71
72
73
74
75
  
  if (module_exists('invisimail')) {
    $formats['invisi'] = array(
      'label' => 'Email-Invisimail',
      'field types' => array('email'),
    );
  }
  return $formats;
76
}
Matthias Hutterer's avatar
Matthias Hutterer committed
77

78
function email_field_formatter($field, $item, $formatter, $node) {
79
  if (empty($item['email'])) {
Matthias Hutterer's avatar
Matthias Hutterer committed
80
81
82
    return '';
  }
  else {
83
    if ($formatter == 'contact') {
84
85
      $mailto = l(t('Email Contact Form'), 'email/'.$node->nid.'/'.$field['field_name']);
    }
86
    elseif ($formatter == 'invisi' && module_exists('invisimail')) {
Matthias Hutterer's avatar
Matthias Hutterer committed
87
      $format = $GLOBALS['invisimail_format'];
88
89
90
91
      if (!(variable_get('invisimail_link_'.$format, TRUE))) {     
        variable_set('invisimail_link_'.$format, TRUE);
        variable_set('invisimail_js_'.$format, TRUE);
      }
92
      $mailto = invisimail_ascii_encode($item['email']);
Matthias Hutterer's avatar
Matthias Hutterer committed
93
94
    }
    else {
95
      $mailto =  '<a href="mailto:'. $item['email']. '">'. check_plain($item['email']) .'</a>';
Matthias Hutterer's avatar
Matthias Hutterer committed
96
    }
Matthias Hutterer's avatar
Matthias Hutterer committed
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
    return $mailto;
  }
}


/**
 * Implementation of hook_widget_info().
 */
function email_widget_info() {
  return array(
    'email' => array(
      'label' => t('Textfield'),
      'field types' => array('email'),
    ),
  );
}


/**
 * Implementation of hook_widget_settings().
 */
function email_widget_settings($op, $widget) {
  switch ($op) {
    case 'form':
      $form = array();
      $form['size'] = array(
        '#type' => 'textfield',
        '#title' => t('Size'),
        '#default_value' => isset($widget['size']) ? $widget['size'] : 60,
        '#required' => FALSE,
        '#description' => t('Size of textfield'),
      );
129
      
Matthias Hutterer's avatar
Matthias Hutterer committed
130
      return $form;
Matthias Hutterer's avatar
Matthias Hutterer committed
131
      
Matthias Hutterer's avatar
Matthias Hutterer committed
132
133
134
135
136
137
138
    case 'validate':
      if (!empty($widget['size']) && (!is_numeric($widget['size']) || intval($widget['size']) != $widget['size'] || $widget['size'] <= 0)) {
        form_set_error('size', t('"Size" must be a positive integer.'));
      }
      break;

    case 'save':
139
      return array('size');
Matthias Hutterer's avatar
Matthias Hutterer committed
140
141
142
143
144
145
146
147
148
149
150
  }
}


/**
 * Implementation of hook_widget().
 */
function email_widget($op, &$node, $field, &$node_field) {
  switch ($op) {   
    case 'form':
      $form = array();
151
152
153
154
      $form[$field['field_name']] = array(
        '#tree' => TRUE, 
        '#weight' => $field['widget']['weight'],
      );
Matthias Hutterer's avatar
Matthias Hutterer committed
155
156
157
158
159
      
      if ($field['multiple']) {
        $form[$field['field_name']]['#type'] = 'fieldset';
        $form[$field['field_name']]['#title'] = t($field['widget']['label']);
        foreach (range(0,2) as $delta) {
Matthias Hutterer's avatar
Matthias Hutterer committed
160
161
162
163
          $default_value = "";
          if (isset($field['widget']['default_value'][$delta]['email'])) {
            $default_value = $field['widget']['default_value'][$delta]['email'];
          }
Matthias Hutterer's avatar
Matthias Hutterer committed
164
165
166
          $form[$field['field_name']][$delta]['email'] = array(
            '#type' => 'textfield',
            '#title' => '',
Matthias Hutterer's avatar
Matthias Hutterer committed
167
            '#default_value' => isset($node_field[$delta]['email']) ? $node_field[$delta]['email'] : $default_value,
Matthias Hutterer's avatar
Matthias Hutterer committed
168
169
170
            '#required' => $field['required'] ? $field['required'] : FALSE,
            '#maxlength' => 255,
            '#size' => isset($field['widget']['size']) ? $field['widget']['size'] : 60,
171
            '#description' => isset($field['widget']['description']) ? $field['widget']['description'] : '',
Matthias Hutterer's avatar
Matthias Hutterer committed
172
173
174
175
          );
        }
      }
      else {
Matthias Hutterer's avatar
Matthias Hutterer committed
176
177
178
179
        $default_value = "";
        if (isset($field['widget']['default_value'][0]['email'])) {
          $default_value = $field['widget']['default_value'][0]['email'];
        }
Matthias Hutterer's avatar
Matthias Hutterer committed
180
181
182
        $form[$field['field_name']][0]['email'] = array(
          '#type' => 'textfield',
          '#title' => $field['widget']['label'],
Matthias Hutterer's avatar
Matthias Hutterer committed
183
          '#default_value' => isset($node_field[0]['email']) ? $node_field[0]['email'] : $default_value,
Matthias Hutterer's avatar
Matthias Hutterer committed
184
185
186
          '#required' => $field['required'] ? $field['required'] : FALSE,
          '#maxlength' => 255,
          '#size' => isset($field['widget']['size']) ? $field['widget']['size'] : 60,
187
          '#description' => isset($field['widget']['description']) ? $field['widget']['description'] : '',
Matthias Hutterer's avatar
Matthias Hutterer committed
188
189
190
191
192
193
194
        );
      }
      
      return $form;
    
    case 'validate':
      if (is_array($node_field)) {
195
196
197
        foreach ($node_field as $delta => $item) {
          if ($item['email'] != '' && !valid_email_address(trim($item['email']))) {
            form_set_error($field['field_name'],t('"%mail" is not a valid email address',array('%mail' => $item['email'])));
Matthias Hutterer's avatar
Matthias Hutterer committed
198
          }
199
        }
Matthias Hutterer's avatar
Matthias Hutterer committed
200
201
202
203
204
      }
      break;
  }           
}

205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/**
 * Implementation of hook_menu().
 */
function email_menu($may_cache) {
  $items = array();

  if ($may_cache) {
    $items[] = array('path' => 'email',
      'title' => t('Email Contact Form'),
      'callback' => 'email_mail_page',
      'access' => user_access('access content'),
      'type' => MENU_CALLBACK,
    );
  }
  return $items;
}

/**
 * The contact form page.
 */
function email_mail_page($nid=null, $fieldname=null) {
  if (empty($nid) || empty($fieldname)) {
    drupal_not_found();
    return;
  }
Matthias Hutterer's avatar
Matthias Hutterer committed
230
  $node = node_load(intval($nid));
231
232
233
234
235
236
237
238
  if (!$node) {
    drupal_not_found();
    return;
  }
  // Validate field name
  $types = content_types($node->type);
  if (!isset($types['fields'][$fieldname]) ||
      $types['fields'][$fieldname]['type'] != 'email' ||
239
240
      ($types['fields'][$fieldname]['display_settings']['teaser']['format'] != 'contact' &&
      $types['fields'][$fieldname]['display_settings']['full']['format'] != 'contact')) {
241
242
243
244
245
246
247
248
249
250
251
252
253
    drupal_not_found();
    return;
  }
  $field = $node->$fieldname;
  if (empty($field) || empty($field[0]['email'])) {
    drupal_not_found();
    return;
  }

  if (!flood_is_allowed('email', variable_get('email_hourly_threshold', 3))) {
    $output = t("You cannot send more than %number messages per hour. Please try again later.", array('%number' => variable_get('email_hourly_threshold', 3)));
  }
  else {
Matthias Hutterer's avatar
Matthias Hutterer committed
254
    $output = drupal_get_form('email_mail_page_form');
255
256
257
258
259
  }

  return $output;
}

Matthias Hutterer's avatar
Matthias Hutterer committed
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
function email_mail_page_form() {
  global $user;
  
  if ($user->uid) {
    $edit['name'] = $user->name;
    $edit['mail'] = $user->mail;
  }

  $form['#token'] = $user->name . $user->mail;
  $form['name'] = array('#type' => 'textfield',
    '#title' => t('Your name'),
    '#maxlength' => 255,
    '#default_value' => $edit['name'],
    '#required' => TRUE,
  );
  $form['mail'] = array('#type' => 'textfield',
    '#title' => t('Your e-mail address'),
    '#maxlength' => 255,
    '#default_value' => $edit['mail'],
    '#required' => TRUE,
  );
  $form['subject'] = array('#type' => 'textfield',
    '#title' => t('Subject'),
    '#maxlength' => 255,
    '#required' => TRUE,
  );
  $form['message'] = array('#type' => 'textarea',
    '#title' => t('Message'),
    '#required' => TRUE,
  );
  $form['submit'] = array('#type' => 'submit',
    '#value' => t('Send e-mail'),
  );
  return $form;
}

296
297
298
/**
 * Validate the site-wide contact page form submission.
 */
Matthias Hutterer's avatar
Matthias Hutterer committed
299
function email_mail_page_form_validate($form_id, $form_values) {
300
301
302
  if (!valid_email_address($form_values['mail'])) {
    form_set_error('mail', t('You must enter a valid e-mail address.'));
  }
303
  if (preg_match("/\r|\n/", $form_values['subject'])) {
Matthias Hutterer's avatar
Matthias Hutterer committed
304
305
306
    form_set_error('subject', t('The subject cannot contain linebreaks.'));
    watchdog('mail', 'Email injection exploit attempted in email form subject: '.check_plain($form_values['subject']), WATCHDOG_NOTICE);
  }
307
308
309
310
311
}

/**
 * Process the site-wide contact page form submission.
 */
Matthias Hutterer's avatar
Matthias Hutterer committed
312
function email_mail_page_form_submit($form_id, $edit) {
Matthias Hutterer's avatar
Matthias Hutterer committed
313
  $nid = intval(arg(1));
314
315
316
317
318
319
320
321
322
323
324
325
326
  $fieldname = arg(2);
  if (empty($nid) || empty($fieldname)) {
    drupal_not_found();
    return;
  }
  $node = node_load($nid);
  if (!$node) {
    drupal_not_found();
    return;
  }
  // Validate field name
  $types = content_types($node->type);
  if (!isset($types['fields'][$fieldname]) ||
327
      $types['fields'][$fieldname]['type'] != 'email') {
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
    drupal_not_found();
    return;
  }
  $field = $node->$fieldname;
  if (empty($field) || empty($field[0]['email'])) {
    drupal_not_found();
    return;
  }
  $email = $field[0]['email'];

  // 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.
  $from = $edit['mail'];

  // Compose the body:
  $message[] = t("%name sent a message using the contact form at %form.", array('%name' => $edit['name'], '%form' => url($_GET['q'], NULL, NULL, TRUE)));
  $message[] = $edit['message'];

  // Tidy up the body:
  foreach ($message as $key => $value) {
    $message[$key] = wordwrap($value);
  }

  // Format the category:
352
  $subject = t('[%title - %contact] %subject', array('%title' => preg_replace("/\r|\n/",'',$node->title), '%contact' => $types['fields'][$fieldname]['widget']['label'], '%subject' => $edit['subject']));
353
354
355
356
357

  // Prepare the body:
  $body = implode("\n\n", $message);

  // Send the e-mail to the recipients:
Matthias Hutterer's avatar
Matthias Hutterer committed
358
  drupal_mail($fieldname, $email, $subject, $body, $from);
359
360
361
362
363
364
365
366
367
368
369

  // Log the operation:
  flood_register_event('email');
  watchdog('mail', t('%name-from sent an e-mail at %form.', array('%name-from' => theme('placeholder', $edit['name'] ." <$from>"), '%form' => url($_GET['q'], NULL, NULL, TRUE))));

  // Update user:
  drupal_set_message(t('Your message has been sent.'));

  // Jump to home page rather than back to contact page to avoid contradictory messages if flood control has been activated.
  return 'node/'.$node->nid;
}
Matthias Hutterer's avatar
Matthias Hutterer committed
370
?>