You are here

email_contact.module in Email Contact 7

Same filename and directory in other branches
  1. 8 email_contact.module

File name: email_contact.module.

Provides display formatters for the email field to display email as a link to the contact form, or as an inline contact form.

File

email_contact.module
View source
<?php

/**
 * @file
 * File name: email_contact.module.
 *
 * Provides display formatters for the email field to display
 * email as a link to the contact form, or as an inline contact form.
 */

/**
 * Implements hook_field_formatter_info().
 */
function email_contact_field_formatter_info() {
  $formats = array(
    'email_contact_link' => array(
      'label' => t('Email contact form link'),
      'description' => t('Display a link to a contact form.'),
      'field types' => array(
        'email',
      ),
      'settings' => array(
        'redirection_to' => 'front',
        'custom_path' => '',
        'link_text' => 'Contact person by email',
        'default_message' => '[current-user:name] sent a message using the contact form at [current-page:url].',
      ),
    ),
    'email_contact_inline' => array(
      'label' => t('Email contact form inline'),
      'description' => t('Display a contact form.'),
      'field types' => array(
        'email',
      ),
      'settings' => array(
        'redirection_to' => 'front',
        'custom_path' => '',
        'default_message' => '[current-user:name] sent a message using the contact form at [current-page:url].',
      ),
    ),
  );
  return $formats;
}

/**
 * Implements hook_field_formatter_view().
 */
function email_contact_field_formatter_view($object_type, $object, $field, $instance, $langcode, $items, $display) {
  $element = array();
  $ids = entity_extract_ids($object_type, $object);
  if ($ids[0] !== NULL) {
    $settings = $display['settings'];
    switch ($display['type']) {
      case 'email_contact_link':
        $link_text = !empty($settings['link_text']) ? $settings['link_text'] : t('Contact person by email');
        foreach ($items as $delta => $item) {
          $element[$delta]['#markup'] = l($link_text, 'email-contact/' . $object_type . '/' . $ids[0] . '/' . $instance['field_name']);
          break;
        }
        break;
      case 'email_contact_inline':
        foreach ($items as $delta => $item) {
          $element[$delta] = email_contact_mail_page($object_type, $ids[0], $instance['field_name'], $settings);
          break;
        }
        break;
    }
  }
  return $element;
}

/**
 * Implements hook_field_formatter_settings_form().
 */
function email_contact_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
  $display = $instance['display'][$view_mode];
  $settings = $display['settings'];
  $element['redirection_to'] = array(
    '#title' => t('Redirection after form submit'),
    '#type' => 'radios',
    '#options' => array(
      'front' => t('To the frontpage'),
      'current' => t('To the current page'),
      'custom' => t('To a custom path'),
    ),
    '#default_value' => $settings['redirection_to'],
    '#required' => TRUE,
  );
  $element['custom_path'] = array(
    '#title' => t('Redirection path'),
    '#type' => 'textfield',
    '#states' => array(
      'visible' => array(
        'input[name="redirection_to"]' => array(
          'value' => 'custom',
        ),
      ),
    ),
    '#default_value' => $settings['custom_path'],
    '#element_validate' => array(
      '_email_contact_field_formatter_settings_form_validate',
    ),
  );
  if ($display['type'] == 'email_contact_link') {
    $element['link_text'] = array(
      '#title' => t('Text for the contact link'),
      '#type' => 'textfield',
      '#suffix' => t('Leaving it empty will cause the default value to appear as a translatable string.'),
      '#default_value' => $settings['link_text'],
    );
  }
  $element['default_message'] = array(
    '#title' => t('Additional message in email body'),
    '#type' => 'textfield',
    '#default_value' => $settings['default_message'],
  );
  if (module_exists('token')) {
    $element['token_help'] = array(
      '#theme' => 'token_tree',
      '#token_types' => array(
        'node',
      ),
    );
  }
  return $element;
}

/**
 * Helper function: validates the custom path.
 */
function _email_contact_field_formatter_settings_form_validate($element, &$element_state) {
  if (isset($element_state['triggering_element']['#field_name'])) {
    $element_name = $element_state['triggering_element']['#field_name'];
    if (isset($element_state['input']['fields'][$element_name]['settings_edit_form']['settings'])) {
      $settings = $element_state['input']['fields'][$element_name]['settings_edit_form']['settings'];
      if ('custom' == $settings['redirection_to']) {
        if (empty($element['#value'])) {
          form_set_error('fields][' . $element_name . '][settings_edit_form][settings', t('The custom path is required!'));
        }
        if (!valid_url($element['#value'])) {
          form_set_error('fields][' . $element_name . '][settings_edit_form][settings', t('The given url is not valid!'));
        }
      }
    }
  }
}

/**
 * Implements hook_field_formatter_settings_summary().
 */
function email_contact_field_formatter_settings_summary($field, $instance, $view_mode) {
  $display = $instance['display'][$view_mode];
  $settings = $display['settings'];
  $summary = '';
  if ($display['type'] == 'email_contact_inline') {
    $summary = t('Displays a contact form for this email.');
  }
  else {
    $summary .= t('Displays a link to a contact form.');
  }
  $redir = t('Redirection is not set');
  if (!empty($settings['redirection_to'])) {
    switch ($settings['redirection_to']) {
      case 'front':
        $redir = t('Redirection after submit to the site front page.');
        break;
      case 'current':
        $redir = 'Redirection after submit to the current page.';
        break;
      case 'custom':
        $redir = t('Redirection after submit to a custom url: @url', array(
          '@url' => $settings['custom_path'],
        ));
        break;
    }
  }
  $summary .= '<br />' . $redir;
  return $summary;
}

/**
 * Implements hook_menu().
 */
function email_contact_menu() {
  $items['email-contact/%/%/%'] = array(
    'title' => 'Email Contact Form',
    'page callback' => 'email_contact_mail_page',
    'page arguments' => array(
      1,
      2,
      3,
    ),
    'access callback' => 'user_access',
    'access arguments' => array(
      'access content',
    ),
    'type' => MENU_CALLBACK,
  );
  $items['admin/config/content/email-contact'] = array(
    'title' => 'Email Contact Form Settings',
    'description' => 'Administer flood control settings for email contact forms',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'email_contact_admin_settings',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
    'type' => MENU_CALLBACK,
  );
  return $items;
}

/**
 * Access callback for the email contact page.
 *
 * Checks whether the current user has view access to the entity. Access checks
 * are performed for the fieldable core entity types, including nodes, users,
 * comments and taxonomy terms. Furthermore entity types using Entity API's
 * access system are supported. For custom entity types that are not using the
 * Entity API, at least the access content permission is checked in the menu
 * access callback.
 *
 * This function is called within the email page callback, as it takes care of
 * loading the entity itself. If the entity is found, access checks are
 * performed with this function.
 *
 * @param string $entity_type
 *   The entity type.
 * @param object $entity
 *   The entity for which the access should be checked.
 * @param array $field_info
 *   The field info for the email field.
 *
 * @return bool
 *   TRUE if the current user has view access, otherwise FALSE.
 *
 * @codingStandardsIgnoreStart
 */
function email_contact_mail_page_access($entity_type, $entity, $field_info) {

  // @codingStandardsIgnoreEnd
  // Check for field access.
  if (!field_access('view', $field_info, $entity_type, $entity)) {
    return FALSE;
  }

  // Check the access for fieldable core entities, including nodes, users,
  // comments and taxonomy terms.
  if ($entity_type == 'node') {
    return node_access('view', $entity);
  }
  elseif ($entity_type == 'user') {
    global $user;
    if ($entity->uid == $user->uid && $entity->uid) {
      return TRUE;
    }
    if (user_access('administer users') || user_access('access user profiles') && $entity->status) {
      return TRUE;
    }
    return FALSE;
  }
  elseif ($entity_type == 'comment') {
    return comment_access('view', $entity);
  }
  elseif ($entity_type == 'taxonomy_term') {
    if (user_access('administer taxonomy') || user_access('access content')) {
      return TRUE;
    }
    return FALSE;
  }

  // Use Entity API for checking the view access for non-core entity types, if
  // the module is installed.
  if (module_exists('entity')) {
    return entity_access('view', $entity_type, $entity);
  }
  return TRUE;
}

/**
 * The contact form page.
 */
function email_contact_mail_page($object_type, $object_id, $field_name, $widget_settings = array()) {
  global $user;
  if (!is_numeric($object_id)) {
    return MENU_NOT_FOUND;
  }

  // Verify this is an email field.
  $field_info = field_info_field($field_name);
  if (!isset($field_info) || $field_info['type'] != 'email') {
    return MENU_NOT_FOUND;
  }

  // Check that the entity exists.
  $objects = entity_load($object_type, array(
    $object_id,
  ));
  if (!isset($objects[$object_id])) {
    return MENU_NOT_FOUND;
  }
  $object = $objects[$object_id];

  // Check that the entity has the email field.
  if (!isset($object->{$field_name})) {
    return MENU_NOT_FOUND;
  }

  // Check if the current user has access to the entity and to the field.
  if (!email_contact_mail_page_access($object_type, $object, $field_info)) {
    return MENU_ACCESS_DENIED;
  }

  // Use the first email address as receiver.
  $field = array_pop($object->{$field_name});
  $emails = array();
  foreach ($field as $delta => $item) {
    if (!empty($item['email']) && strpos($item['email'], '@') !== FALSE) {
      $emails[] = $item['email'];
    }
  }

  // Verify that the email address is not empty.
  if (empty($emails)) {
    return MENU_NOT_FOUND;
  }
  $entity_info = entity_extract_ids($object_type, $object);
  $settings = field_info_instance($object_type, $field_name, $entity_info[2]);
  $found = FALSE;
  foreach ($settings['display'] as $display_name => $display_data) {
    if (isset($display_data['type']) && ($display_data['type'] === 'email_contact_link' || $display_data['type'] === 'email_contact_inline')) {
      if (empty($widget_settings)) {
        $widget_settings = $display_data['settings'];
      }
      $found = TRUE;
      break;
    }
  }
  if (!$found) {
    return MENU_NOT_FOUND;
  }
  if ($GLOBALS['user']->uid != 1 && !flood_is_allowed('email', variable_get('email_hourly_threshold', 3))) {
    return t("You cannot send more than %number messages per hour. Please try again later.", array(
      '%number' => variable_get('email_hourly_threshold', 3),
    ));
  }
  return drupal_get_form('email_contact_mail_page_form', $object_type, $object_id, $field_name, $emails, $widget_settings);
}

/**
 * Contact form.
 */
function email_contact_mail_page_form($form, &$form_state, $object_type, $object_id, $field_name, $emails, $widget_settings = array()) {
  global $user;
  $form['object_id'] = array(
    '#type' => 'value',
    '#value' => $object_id,
  );
  $form['object_type'] = array(
    '#type' => 'value',
    '#value' => $object_type,
  );
  $form['field_name'] = array(
    '#type' => 'value',
    '#value' => $field_name,
  );
  $form['emails'] = array(
    '#type' => 'value',
    '#value' => serialize($emails),
  );
  $form['name'] = array(
    '#type' => 'textfield',
    '#title' => t('Your name'),
    '#maxlength' => 255,
    '#default_value' => $user->uid ? $user->name : '',
    '#required' => TRUE,
  );
  $form['mail'] = array(
    '#type' => 'textfield',
    '#title' => t('Your e-mail address'),
    '#maxlength' => 255,
    '#default_value' => $user->uid ? $user->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'),
    '#validate' => array(
      'email_contact_mail_page_form_validate',
    ),
    '#submit' => array(
      'email_contact_mail_page_form_submit',
    ),
  );
  if (!isset($form_state['widget_settings'])) {
    $form_state['widget_settings'] = $widget_settings;
  }
  return $form;
}

/**
 * Validate the site-wide contact page form submission.
 */
function email_contact_mail_page_form_validate($form, &$form_state) {
  if (!valid_email_address($form_state['values']['mail'])) {
    form_set_error('mail', t('You must enter a valid e-mail address.'));
  }
  if (preg_match("/\r|\n/", $form_state['values']['subject'])) {
    form_set_error('subject', t('The subject cannot contain linebreaks.'));
    watchdog('mail', 'Email injection exploit attempted in email form subject: @subject', array(
      '@subject' => $form_state['values']['subject'],
    ), WATCHDOG_NOTICE);
  }
}

/**
 * Process the site-wide contact page form submission.
 */
function email_contact_mail_page_form_submit($form, &$form_state) {
  $object_type = $form_state['values']['object_type'];
  $object_id = $form_state['values']['object_id'];
  $field_name = $form_state['values']['field_name'];
  $emails = unserialize($form_state['values']['emails']);

  // Load entity.
  $objects = entity_load($object_type, array(
    $object_id,
  ));
  $object = $objects[$object_id];
  $object_info = entity_get_info($object_type);

  // 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 = $form_state['values']['mail'];
  $params['object'] = $object;
  $params['subject'] = $form_state['values']['subject'];
  $params['name'] = $form_state['values']['name'];
  $params['default_message'] = $form_state['widget_settings']['default_message'];
  $params['message'] = $form_state['values']['message'];
  $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,
  ));

  // Send the e-mail to the recipients.
  drupal_mail('email_contact', 'contact', implode(', ', $emails), language_default(), $params, $from);

  // Log the operation.
  if ($GLOBALS['user']->uid != 1) {
    flood_register_event('email');
  }
  watchdog('mail', '%name-from sent an e-mail at %form.', array(
    '%name-from' => $form_state['values']['name'],
    '%form' => url($_GET['q'], array(
      'absolute' => TRUE,
    )),
  ));
  drupal_set_message(t('Your message has been sent.'));
  $form_state['redirect'] = '<front>';
  if (!empty($form_state['widget_settings']['redirection_to'])) {
    switch ($form_state['widget_settings']['redirection_to']) {
      case 'current':
        $form_state['redirect'] = current_path();
        break;
      case 'custom':
        $form_state['redirect'] = $form_state['widget_settings']['custom_path'];
        break;
      default:
        $form_state['redirect'] = $path;
        break;
    }
  }
}

/**
 * Implements hook_mail().
 */
function email_contact_mail($key, &$message, $params) {
  $language = $message['language'];
  switch ($key) {
    case 'contact':

      // Compose the body.
      if (module_exists('token')) {
        $message['body'][] = token_replace($params['default_message']);
      }
      else {
        $message['body'][] = $params['default_message'];
      }
      $message['body'][] = $params['message'];
      $message['subject'] = "";

      // Include the title of the entity, if one exists.
      $object = $params['object'];
      if (isset($object->title) && !empty($object->title)) {
        $message['subject'] = "[" . check_plain(preg_replace("/\r|\n/", '', $object->title)) . "]";
      }
      $message['subject'] .= " " . check_plain($params['subject']);
      $message['headers']['Reply-to'] = $message['headers']['From'];
      $message['headers']['From'] = $message['headers']['Sender'] = $message['headers']['Return-Path'] = variable_get('site_mail', ini_get('sendmail_from'));
      break;
  }
}

/**
 * Settings for contact form.
 */
function email_contact_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);
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function email_contact_form_honeypot_admin_form_alter(&$form, &$form_state) {
  $form['enabled_forms']['email_contact_forms'] = array(
    '#markup' => '<h5>' . t('Email Contact Forms') . '</h5>',
  );
  $form['enabled_forms']['honeypot_form_email_contact_mail_page_form'] = array(
    '#type' => 'checkbox',
    '#title' => t('Email Contact forms (all)'),
    '#default_value' => variable_get('honeypot_form_email_contact_mail_page_form', 0),
  );
}