Skip to content

Commit

Permalink
Merge pull request #21169 from eileenmcnaughton/email
Browse files Browse the repository at this point in the history
dev/core#2769 use php email validation not hacked & bad quickform function
  • Loading branch information
seamuslee001 authored Aug 24, 2021
2 parents ebd125c + 5ae4628 commit 0239643
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 3 deletions.
1 change: 1 addition & 0 deletions CRM/Core/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ public function registerRules() {
'settingPath',
'autocomplete',
'validContact',
'email',
];

foreach ($rules as $rule) {
Expand Down
38 changes: 36 additions & 2 deletions CRM/Utils/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -639,12 +639,46 @@ public static function boolean($value) {
*
* @return bool
*/
public static function email($value) {
public static function email($value): bool {
if (function_exists('idn_to_ascii')) {
$parts = explode('@', $value);
foreach ($parts as &$part) {
// if the function returns FALSE then let filter_var have at it.
$part = self::idnToAsci($part) ?: $part;
if ($part === 'localhost') {
// if we are in a dev environment add .com to trick it into accepting localhost.
// this is a bit best-effort - ie we don't really care that it's in a bigger if.
$part .= '.com';
}
}
$value = implode('@', $parts);
}
return (bool) filter_var($value, FILTER_VALIDATE_EMAIL);
}

/**
* @param $list
* Convert domain string to ascii.
*
* See https://lab.civicrm.org/dev/core/-/issues/2769
* and also discussion over in guzzle land
* https://github.com/guzzle/guzzle/pull/2454
*
* @param string $string
*
* @return string|false
*/
private static function idnToAsci(string $string) {
if (!\extension_loaded('intl')) {
return $string;
}
if (defined('INTL_IDNA_VARIANT_UTS46')) {
return idn_to_ascii($string, 0, INTL_IDNA_VARIANT_UTS46);
}
return idn_to_ascii($string);
}

/**
* @param string $list
*
* @return bool
*/
Expand Down
29 changes: 28 additions & 1 deletion tests/phpunit/CRM/Utils/RuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,8 @@ public function testCreditCardValidation($number, $type): void {
}

/**
* Test cvvs
* Test cvvs.
*
* @return array
*/
public static function cvvs(): array {
Expand Down Expand Up @@ -343,4 +344,30 @@ public function testCvvRule($cvv, $type, $expected): void {
$this->assertEquals($expected, CRM_Utils_Rule::cvv($cvv, $type));
}

/**
* Test CVV rule
*
* @param string $email
* @param bool $expected expected outcome of the rule validation
*
* @dataProvider emails
*/
public function testEmailRule(string $email, bool $expected): void {
$this->assertEquals($expected, CRM_Utils_Rule::email($email));
}

/**
* Test emails.
*
* @return array
*/
public static function emails(): array {
$cases = [];
$cases['name.-o-.i.10@example.com'] = ['name.-o-.i.10@example.com', TRUE];
$cases['test@ēxāmplē.co.nz'] = ['test@ēxāmplē.co.nz', TRUE];
$cases['test@localhost'] = ['test@localhost', TRUE];
$cases['test@ēxāmplē.co'] = ['test@exāmple', FALSE];
return $cases;
}

}

0 comments on commit 0239643

Please sign in to comment.