You are here

email_verify.module in Email Verify 7.2

Verifies thoroughly that email addresses are correctly entered.

Copyright: Daniel Bonniot <bonniot@users.sourceforge.net>. License: GNU GPL v2 or later.

File

email_verify.module
View source
<?php

/**
 * @file
 * Verifies thoroughly that email addresses are correctly entered.
 *
 * Copyright: Daniel Bonniot <bonniot@users.sourceforge.net>.
 * License: GNU GPL v2 or later.
 */

/**
 * Implements hook_help().
 */
function email_verify_help($path, $arg) {
  if ($path == 'admin/help#email_verify') {
    return '<p>' . t('This module verifies that email addresses are valid during account registration or edit.') . '</p>';
  }
}

/**
 * Implements hook_permission().
 */
function email_verify_permission() {
  return array(
    'bypass email verification' => array(
      'title' => t('Bypass email verification'),
      'description' => t('Allow email verification to be bypassed when performing user operations.'),
    ),
  );
}

/**
 * Implements hook_menu().
 */
function email_verify_menu() {
  $items = array();

  // The route to the system configuration page.
  $items['admin/config/system/email_verify'] = array(
    'title' => 'Email Verify',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'email_verify_admin_settings',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
    'description' => "Configure the Email Verify module's administrative settings.",
    'file' => 'email_verify.admin.inc',
  );

  // The route to the user email address check page.
  $items['admin/people/email_verify'] = array(
    'title' => 'Email Verify',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'email_verify_user_check_form',
    ),
    'access callback' => 'email_verify_access_people_email_verify',
    'type' => MENU_LOCAL_TASK,
    'file' => 'email_verify.check.inc',
  );
  return $items;
}

/**
 * Access callback for the page at admin/people/email_verify.
 */
function email_verify_access_people_email_verify() {
  if (email_verify_activated() && user_access('administer users')) {
    return TRUE;
  }
  return FALSE;
}

/**
 * Implements hook_form_alter().
 */
function email_verify_form_alter(&$form, &$form_state, $form_id) {
  if (email_verify_activated() && !user_access('bypass email verification')) {

    // Get the list of fields.
    $fields = email_verify_get_form_fields($form_id);
    if (!empty($fields)) {

      // Add the email verification validation callback.
      if (empty($form['#validate'])) {
        $form['#validate'] = array(
          'email_verify_verify_address',
        );
      }
      else {
        array_unshift($form['#validate'], 'email_verify_verify_address');
      }
      if (empty($form['submit']['#validate'])) {
        $form['submit']['#validate'] = array(
          'email_verify_verify_address',
        );
      }
      else {
        array_unshift($form['submit']['#validate'], 'email_verify_verify_address');
      }
    }
  }
}

/**
 * Additional validation for the form to verify the email address.
 */
function email_verify_verify_address($form, &$form_state) {
  if (isset($form_state['values']['op']) && isset($form_state['values']['submit']) && $form_state['values']['op'] == $form_state['values']['submit']) {

    // Get the list of fields.
    $fields = email_verify_get_form_fields($form_state['values']['form_id']);
    if (!empty($fields)) {
      foreach ($fields as $field) {
        if (!empty($form_state['values'][$field])) {

          // Get the data from the field.
          if (is_string($form_state['values'][$field])) {

            // Verify the e-mail address.
            $results = email_verify_check($form_state['values'][$field]);
            if (!empty($results['debugging_text'])) {

              // Log and/or display the debugging information.
              email_verify_process_debug_information($results['debugging_text']);
            }

            // Report the error and flag the form field.
            if (!empty($results['verification_message'])) {
              form_set_error($field, $results['verification_message']);
            }
          }
          elseif (is_array($form_state['values'][$field])) {
            foreach ($form_state['values'][$field] as $lang_code) {
              foreach ($lang_code as $value) {
                $address = reset($value);
                if (!empty($address)) {

                  // Verify the e-mail address.
                  $results = email_verify_check($address);
                  if (!empty($results['debugging_text'])) {

                    // Log and/or display the debugging information.
                    email_verify_process_debug_information($results['debugging_text']);
                  }

                  // Report the error and flag the form field.
                  if (!empty($results['verification_message'])) {
                    form_set_error($field, $results['verification_message']);
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

/**
 * Retrieves the forms and fields the admin specified to verify.
 *
 * @param string $form_id
 *   The ID of the form to retrieve the fields for.
 *
 * @return array
 *   An associative array with the key being the form and the
 */
function email_verify_get_form_fields($form_id) {
  $verify_form_fields = array();

  // Get the list of forms and fields.
  $email_verify_forms = variable_get('email_verify_forms', '');
  if (!empty($email_verify_forms)) {

    // Convert the text data into an array of strings, each representing one
    // form/field combination.
    $forms_and_fields = explode("\n", $email_verify_forms);

    // Process each form/field pair.
    foreach ($forms_and_fields as $form_and_field) {

      // Make sure it isn't an empty array element.
      if (!empty($form_and_field)) {
        $form_and_field = explode(',', $form_and_field);
        $form = trim($form_and_field[0]);
        $field = trim($form_and_field[1]);

        // If the form and field variables are not empty, and the form variable
        // matches $form_id, save the field ID.
        if (!empty($form) && !empty($field) && $form == $form_id) {
          $verify_form_fields[] = $field;
        }
      }
    }
  }
  return $verify_form_fields;
}

/**
 * Verifies whether the given mail address exists.
 *
 * @param string $mail
 *   Email address to verify.
 *
 * @return string
 *   NULL if the address exists, or an error message if we found a problem with
 *   the address.
 */
function email_verify_check($mail) {
  module_load_include('inc', 'email_verify');
  return _email_verify_check($mail);
}

/**
 * Determine if Email Verify is activated.
 *
 * @return bool
 *   Indicates whether the module is activated or not.
 */
function email_verify_activated() {
  return variable_get('email_verify_active', FALSE);
}

/**
 * Displays and/or records the collected debugging information.
 *
 * @param array $debug_information
 *   An array of text strings reported during the debugging process.
 */
function email_verify_process_debug_information(array $debug_information) {
  if (!empty($debug_information)) {

    // Log and/or display the debugging information.
    $message = implode('<br />', $debug_information);
    if (variable_get('email_verify_debug_mode_record_log', FALSE)) {
      watchdog('email_verify debug', $message, array(), WATCHDOG_DEBUG);
    }
    if (variable_get('email_verify_debug_mode_display_page', FALSE)) {
      drupal_set_message($message);
    }
  }
}

Functions

Namesort descending Description
email_verify_access_people_email_verify Access callback for the page at admin/people/email_verify.
email_verify_activated Determine if Email Verify is activated.
email_verify_check Verifies whether the given mail address exists.
email_verify_form_alter Implements hook_form_alter().
email_verify_get_form_fields Retrieves the forms and fields the admin specified to verify.
email_verify_help Implements hook_help().
email_verify_menu Implements hook_menu().
email_verify_permission Implements hook_permission().
email_verify_process_debug_information Displays and/or records the collected debugging information.
email_verify_verify_address Additional validation for the form to verify the email address.