Commit d11a4b10 authored by Drew Webber's avatar Drew Webber
Browse files

Issue #2966195 by mpaler, poker10, ravi.shankar, chrisolof, klonos:...

Issue #2966195 by mpaler, poker10, ravi.shankar, chrisolof, klonos: valid_email_address() should be easily overridable
parent 2ee8c2ac
Loading
Loading
Loading
Loading
+7 −1
Changes for includes/common.inc: 7 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -1278,6 +1278,10 @@ function fix_gpc_magic() {
 *
 * This uses the
 * @link http://php.net/manual/filter.filters.validate.php PHP e-mail validation filter. @endlink
 * However, a module may override this by implementing
 * hook_valid_email_address_alter(&$valid, $mail).
 *
 * @see hook_valid_email_address_alter()
 *
 * @param $mail
 *   A string containing an e-mail address.
@@ -1286,7 +1290,9 @@ function fix_gpc_magic() {
 *   TRUE if the address is in a valid format.
 */
function valid_email_address($mail) {
  return (bool)filter_var($mail, FILTER_VALIDATE_EMAIL);
  $valid = (bool) filter_var($mail, FILTER_VALIDATE_EMAIL);
  drupal_alter('valid_email_address', $valid, $mail);
  return $valid;
}

/**
+27 −0
Changes for modules/system/system.api.php: 27 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -4817,6 +4817,33 @@ function hook_filetransfer_info_alter(&$filetransfer_info) {
  }
}

/**
 * Alter core e-mail validation.
 *
 * This hook is called immediately after core e-mail validation takes place and
 * gives other modules a chance to override it. This is useful in cases where
 * you want to have stricter or looser validation standards than that provided
 * by Drupal core.
 *
 * @param $valid
 *   Boolean value referencing the validation result.
 *
 * @param $mail
 *   E-mail address being validated.
 *
 * @see valid_email_address()
 */
function hook_valid_email_address_alter(&$valid, $mail) {
  if (preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/", $mail)) {
    list($username, $domain) = explode('@', $mail);
    // Check DNS records.
    if (checkdnsrr($domain, 'MX')) {
      $valid = TRUE;
    }
  }
  $valid = FALSE;
}

/**
 * @} End of "addtogroup hooks".
 */
+6 −0
Changes for modules/user/tests/user_email_validation_test.info: 6 added lines, 0 removed lines.
Original line number Diff line number Diff line
name = "User module e-mail validation tests"
description = "Support module for user e-mail validation testing."
package = Testing
version = VERSION
core = 7.x
hidden = TRUE
+17 −0
Changes for modules/user/tests/user_email_validation_test.module: 17 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php

/**
 * @file
 * Dummy module implementing hook_valid_email_address_alter.
 */

/**
 * Implements hook_valid_email_address_alter().
 */
function user_email_validation_test_valid_email_address_alter(&$valid, $mail) {
  $local = '.local';
  // Invalidate all e-mails ending with .local.
  if (substr_compare($mail, $local, -strlen($local)) === 0) {
    $valid = FALSE;
  }
}
+11 −0
Changes for modules/user/user.test: 11 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -308,6 +308,17 @@ class UserValidationTestCase extends DrupalWebTestCase {
      $this->$test($result, $description . ' (' . $name . ')');
    }
  }

  // Test altered e-mail validation.
  function testAlteredMailValidation() {
    // This e-mail is considered as valid.
    $mail = 'foo@example.local';
    $this->assertNull(user_validate_mail($mail), 'E-mail address is valid.');

    // Invalidate all emails ending with .local in alter hook.
    module_enable(array('user_email_validation_test'));
    $this->assertNotNull(user_validate_mail($mail), 'E-mail address is invalid.');
  }
}

/**