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



5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/**
 * Implementation of hook_theme().
 */
function email_theme() {
  return array(
    'email_textfield' => array(
      'arguments' => array('element' => NULL),
    ),
    'email_formatter_default' => array(
      'arguments' => array('element' => NULL),
    ),
    'email_formatter_spamspan' => array(
      'arguments' => array('element' => NULL),
    ),
    'email_formatter_contact' => array(
      'arguments' => array('element' => NULL),
    ),
22 23 24
    'email_formatter_plain' => array(
      'arguments' => array('element' => NULL),
    ),
25 26
  );
}
Matthias Hutterer's avatar
Matthias Hutterer committed
27 28 29 30 31 32

/**
 * Implementation of hook_field_info().
 */
function email_field_info() {
  return array(
33
    'email' => array(
34
      'label' => 'Email',
35 36 37 38 39 40
      'callbacks' => array(
        'tables' => CONTENT_CALLBACK_DEFAULT,
        'arguments' => CONTENT_CALLBACK_DEFAULT,
      ),
    ),
  );
Matthias Hutterer's avatar
Matthias Hutterer committed
41 42 43 44 45 46 47
}


/**
 * Implementation of hook_field_settings().
 */
function email_field_settings($op, $field) {
48
  switch ($op) {
Matthias Hutterer's avatar
Matthias Hutterer committed
49
    case 'database columns':
50
      $columns['email'] = array('type' => 'varchar', 'length' => 255, 'not null' => FALSE, 'sortable' => TRUE);
Matthias Hutterer's avatar
Matthias Hutterer committed
51 52 53 54
      return $columns;
  }
}

55 56 57
/**
 * Implementation of hook_field().
 */
58
function email_field($op, &$node, $field, &$items, $teaser, $page) {
59
  switch ($op) {
60 61
    case 'validate':
      if (is_array($items)) {
62 63 64
        foreach ($items 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'])));
65
          }
66 67 68
        }
     }
     break;
69 70 71

    case 'sanitize':
      foreach ($items as $delta => $item) {
72
        $items[$delta]['safe'] = check_plain($item['email']);
73 74 75 76 77 78 79 80 81 82 83 84 85 86
      }
      break;
  }
}

/**
 * Implementation of hook_content_is_empty().
 */
function email_content_is_empty($item, $field) {
  if (empty($item['email'])) {
    return TRUE;
  }
  return FALSE;
}
Matthias Hutterer's avatar
Matthias Hutterer committed
87

88 89 90 91 92
/**
 * Implementation of hook_field_formatter_info().
 *
 */
function email_field_formatter_info() {
93
  $formats = array(
94
    'default' => array(
95
      'label' => t('Default email link'),
96
      'field types' => array('email'),
97
      'multiple values' => CONTENT_HANDLE_CORE,
98 99
    ),
    'contact' => array(
100
      'label' => t('Email contact form'),
101
      'field types' => array('email'),
102
      'multiple values' => CONTENT_HANDLE_CORE,
103
    ),
104 105 106 107 108
    'plain' => array(
      'label' => t('Email plain text'),
      'field types' => array('email'),
      'multiple values' => CONTENT_HANDLE_CORE,
    ),
109
  );
110 111 112 113 114 115 116
  if (module_exists('spamspan')) {
    $formats += array(
      'spamspan' => array(
        'label' => t('Email SpamSpan'),
        'field types' => array('email'),
        'multiple values' => CONTENT_HANDLE_CORE,
      ),
117 118 119
    );
  }
  return $formats;
120
}
Matthias Hutterer's avatar
Matthias Hutterer committed
121

122 123 124 125
/**
 * Theme function for 'default' email field formatter.
 */
function theme_email_formatter_default($element) {
126
  return !empty($element['#item']['safe']) ? '<a href="mailto:'. $element['#item']['email'] .'">'. $element['#item']['safe'] .'</a>' : '';
127 128 129 130 131 132
}

/**
 * Theme function for 'spamspan' email field formatter.
 */
function theme_email_formatter_spamspan($element) {
133 134 135
  if (empty($element['#item']['safe'])) {
    return '';
  }
136 137
  if (module_exists('spamspan')) {
    return spamspan($element['#item']['email']);
Matthias Hutterer's avatar
Matthias Hutterer committed
138 139
  }
  else {
140
    return '<a href="mailto:'. $element['#item']['email'] .'">'. $element['#item']['safe'] .'</a>';
Matthias Hutterer's avatar
Matthias Hutterer committed
141 142 143
  }
}

144 145 146 147
/**
 * Theme function for 'contact' email field formatter.
 */
function theme_email_formatter_contact($element) {
148
  return !empty($element['#item']['safe']) ? l(t('Email contact form'), 'email/'. $element['#node']->nid .'/'. $element['#field_name']) : '';
149 150
}

151 152 153 154 155 156 157
/**
 * Theme function for 'plain' email field formatter.
 */
function theme_email_formatter_plain($element) {
  return !empty($element['#item']['safe']) ? $element['#item']['safe'] : '';
}

Matthias Hutterer's avatar
Matthias Hutterer committed
158 159 160 161 162 163

/**
 * Implementation of hook_widget_info().
 */
function email_widget_info() {
  return array(
164 165
    'email_textfield' => array(
      'label' => t('Text field'),
Matthias Hutterer's avatar
Matthias Hutterer committed
166
      'field types' => array('email'),
167 168 169 170
      'multiple values' => CONTENT_HANDLE_CORE,
      'callbacks' => array(
        'default value' => CONTENT_CALLBACK_DEFAULT,
      ),
Matthias Hutterer's avatar
Matthias Hutterer committed
171 172 173 174
    ),
  );
}

175 176 177 178 179 180 181 182 183 184 185 186 187
/**
 * Implementation of FAPI hook_elements().
 */
function email_elements() {
  return array(
    'email_textfield' => array(
      '#input' => TRUE,
      '#columns' => array('email'),
      '#delta' => 0,
      '#process' => array('email_textfield_process'),
    ),
  );
}
Matthias Hutterer's avatar
Matthias Hutterer committed
188 189 190 191 192 193 194

/**
 * Implementation of hook_widget_settings().
 */
function email_widget_settings($op, $widget) {
  switch ($op) {
    case 'form':
195
      $size = (isset($widget['size']) && is_numeric($widget['size'])) ? $widget['size'] : 60;
Matthias Hutterer's avatar
Matthias Hutterer committed
196 197
      $form['size'] = array(
        '#type' => 'textfield',
198 199 200 201
        '#title' => t('Size of textfield'),
        '#default_value' => $size,
        '#element_validate' => array('_email_widget_settings_size_validate'),
        '#required' => TRUE,
Matthias Hutterer's avatar
Matthias Hutterer committed
202 203
      );
      return $form;
Matthias Hutterer's avatar
Matthias Hutterer committed
204
      
Matthias Hutterer's avatar
Matthias Hutterer committed
205
    case 'save':
206
      return array('size');
Matthias Hutterer's avatar
Matthias Hutterer committed
207 208 209
  }
}

210 211 212 213 214 215 216
function _email_widget_settings_size_validate($element, &$form_state) {
  $value = $form_state['values']['size'];
  if (!is_numeric($value) || intval($value) != $value || $value <= 0) {
    form_error($element, t('"Size" must be a positive integer.'));
  }
}

Matthias Hutterer's avatar
Matthias Hutterer committed
217 218 219
/**
 * Implementation of hook_widget().
 */
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
function email_widget(&$form, &$form_state, $field, $items, $delta = 0) {
  $element = array(
    '#type' => $field['widget']['type'],
    '#default_value' => isset($items[$delta]) ? $items[$delta] : '',
  );
  return $element;
}

/**
 * Process an individual element.
 */
function email_textfield_process($element, $edit, $form_state, $form) {
  $field = $form['#field_info'][$element['#field_name']];
  $field_key = $element['#columns'][0];
  $delta = $element['#delta'];
  $element[$field_key] = array(
    '#type' => 'textfield',
237 238
    '#title' => $element['#title'],
    '#description' => content_filter_xss($field['widget']['description']),
239
    '#required' => $element['#required'],
240
    '#maxlength' => 255,
241
    '#size' => !empty($field['widget']['size']) ? $field['widget']['size'] : 60,
242
    '#attributes' => array('class' => 'text', 'dir' => 'ltr'),
243 244 245 246 247 248 249 250 251 252
    '#default_value' => isset($element['#value'][$field_key]) ? $element['#value'][$field_key] : NULL,
  );
  return $element;
}

/**
 * FAPI theme for an individual text elements.
 */
function theme_email_textfield($element) {
  return $element['#children'];
Matthias Hutterer's avatar
Matthias Hutterer committed
253 254
}

255 256 257
/**
 * Implementation of hook_menu().
 */
258
function email_menu() {
259
  $items['email/%node/%'] = array(
260
    'title' => 'Email Contact Form',
261 262
    'page callback' => 'email_mail_page',
    'page arguments' => array(1, 2),
263 264
    'access callback' => 'node_access',
    'access arguments' => array('view', 1),
265 266
    'type' => MENU_CALLBACK,
  );
267 268 269 270 271 272 273
  $items['admin/settings/email'] = array(
    'title' => 'CCK Email Contact Form Settings',
    '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'),
  );
274 275 276
  return $items;
}

277

278 279
/**
 * The contact form page.
280 281 282 283 284 285 286 287
 *
 * @param $node
 *   The node object on which the email address is stored.
 * @param $field_name
 *   The name of the CCK field which holds the email address.
 *
 * @return
 *  Rendered page output containing either the contact form or a flood warning.
288
 */
289
function email_mail_page($node, $field_name) {
290 291 292 293 294 295
  if (module_exists('content_permissions')) {
    if (!user_access('view '. $field_name)) {
      drupal_not_found();
      return;
    }
  }
296 297 298 299
  if (!$node) {
    drupal_not_found();
    return;
  }
300 301
  $field = $node->$field_name;
  $email = $field[0]['email'];
302
  $types = content_types($node->type);
303 304
  // Validate field name
  $types = content_types($node->type);
305 306 307 308 309
  if (empty($email) ||
      !isset($types['fields'][$field_name]) ||
      $types['fields'][$field_name]['type'] != 'email' ||
      ($types['fields'][$field_name]['display_settings']['teaser']['format'] != 'contact' &&
      $types['fields'][$field_name]['display_settings']['full']['format'] != 'contact')) {
310 311 312
    drupal_not_found();
    return;
  }
313

314 315 316 317 318

  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 {
319
    $output = drupal_get_form('email_mail_page_form', $node, $field_name, $email);
320 321 322 323 324
  }

  return $output;
}

325 326 327
/**
 * Contact form
 */
328
function email_mail_page_form($form_state, $node, $field_name, $email) {
Matthias Hutterer's avatar
Matthias Hutterer committed
329 330
  global $user;

331 332 333 334
  $form['node'] = array(
    '#type' => 'value',
    '#value' => $node,
  );
335
  $form['field_name'] = array(
336
    '#type' => 'value',
337 338 339 340 341
    '#value' => $field_name,
  );
  $form['email'] = array(
    '#type' => 'value',
    '#value' => $email,
342 343 344
  );
  $form['name'] = array(
    '#type' => 'textfield',
Matthias Hutterer's avatar
Matthias Hutterer committed
345 346
    '#title' => t('Your name'),
    '#maxlength' => 255,
347
    '#default_value' => $user->uid ? $user->name : '',
Matthias Hutterer's avatar
Matthias Hutterer committed
348 349
    '#required' => TRUE,
  );
350 351
  $form['mail'] = array(
    '#type' => 'textfield',
Matthias Hutterer's avatar
Matthias Hutterer committed
352 353
    '#title' => t('Your e-mail address'),
    '#maxlength' => 255,
354
    '#default_value' => $user->uid ? $user->mail : '',
Matthias Hutterer's avatar
Matthias Hutterer committed
355 356
    '#required' => TRUE,
  );
357 358
  $form['subject'] = array(
    '#type' => 'textfield',
Matthias Hutterer's avatar
Matthias Hutterer committed
359 360 361 362
    '#title' => t('Subject'),
    '#maxlength' => 255,
    '#required' => TRUE,
  );
363 364
  $form['message'] = array(
    '#type' => 'textarea',
Matthias Hutterer's avatar
Matthias Hutterer committed
365 366 367
    '#title' => t('Message'),
    '#required' => TRUE,
  );
368 369
  $form['submit'] = array(
    '#type' => 'submit',
Matthias Hutterer's avatar
Matthias Hutterer committed
370
    '#value' => t('Send e-mail'),
371 372
    '#validate' => array('email_mail_page_form_validate'),
    '#submit' => array('email_mail_page_form_submit'),
Matthias Hutterer's avatar
Matthias Hutterer committed
373 374 375 376
  );
  return $form;
}

377 378 379
/**
 * Validate the site-wide contact page form submission.
 */
380 381
function email_mail_page_form_validate($form, &$form_state) {
  if (!valid_email_address($form_state['values']['mail'])) {
382 383
    form_set_error('mail', t('You must enter a valid e-mail address.'));
  }
384
  if (preg_match("/\r|\n/", $form_state['values']['subject'])) {
Matthias Hutterer's avatar
Matthias Hutterer committed
385
    form_set_error('subject', t('The subject cannot contain linebreaks.'));
386
    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
387
  }
388 389 390 391 392
}

/**
 * Process the site-wide contact page form submission.
 */
393 394
function email_mail_page_form_submit($form, &$form_state) {
  $node = $form_state['values']['node'];
395 396 397
  $field_name = $form_state['values']['field_name'];
  $email = $form_state['values']['email'];
  
398 399 400
  // 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.

401 402 403 404 405 406 407 408
  $from = $form_state['values']['mail'];
 
  $params['node'] = $node;
  $params['subject'] = $form_state['values']['subject'];
  $params['name'] = $form_state['values']['name'];
  $params['message'] = $form_state['values']['message'];
  $params['url'] = url('node/' . $node->nid, array('absolute' => TRUE));
   
409
  // Send the e-mail to the recipients:
410 411
  drupal_mail('email', 'contact', $email, language_default(), $params, $from);
 
412 413
  // Log the operation:
  flood_register_event('email');
414 415
  watchdog('mail', t('%name-from sent an e-mail at %form.', array('%name-from' => theme('placeholder', $form_state['values']['name'] ." <$from>"), '%form' => url($_GET['q'], array('absolute' => TRUE)))));
 
416 417
  // Update user:
  drupal_set_message(t('Your message has been sent.'));
418
 
419
  // Jump to home page rather than back to contact page to avoid contradictory messages if flood control has been activated.
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
  $form_state['redirect'] = 'node/'. $node->nid;
}

/**
 * Implementation of hook_mail()
 */
function email_mail($key, &$message, $params) {
  $language = $message['language'];
  switch($key) {
    case 'contact':
      $node = $params['node'];
      // Compose the body:
      $msg[] = t('@name sent a message using the contact form at @node.', array('@name' => $params['name'], '@node' => $params['url']), $language->language);
      $msg[] = $params['message'];

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

      // Prepare the body:
      $message['body'] = implode("\n\n", $msg);
      
      $message['subject'] = t('[@title] @subject', array('@title' => preg_replace("/\r|\n/",'', $node->title), '@subject' => $params['subject']), $language->language);
      break;
  }
446
}
Matthias Hutterer's avatar
Matthias Hutterer committed
447 448 449 450 451 452 453


/**
 * Implementation of hook token_list
 */
function email_token_list($type = 'all') {
  if ($type == 'field' || $type == 'all') {
454 455
    $tokens['email']['raw']       = t('Raw email address');
    $tokens['email']['formatted'] = t('Formatted email address');
Matthias Hutterer's avatar
Matthias Hutterer committed
456 457 458 459 460 461 462 463 464 465
    return $tokens;
  }
}

/**
 * Implementation of hook token_values
 */ 
function email_token_values($type, $object = NULL, $options = array()) {
  if ($type == 'field') {
    $item = $object[0];
466 467
    $tokens['raw']       = $item['email'];
    $tokens['formatted'] = $item['view'];
Matthias Hutterer's avatar
Matthias Hutterer committed
468 469 470
    return $tokens;
  }
}
471 472 473 474 475 476 477 478 479 480 481 482 483

/**
 * 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);
}