From ae7716c9009dc74b0dcc3a914be12fdda8cfe08a Mon Sep 17 00:00:00 2001 From: Jabari Hunt Date: Wed, 13 Dec 2017 12:30:45 -0600 Subject: [PATCH 1/7] Improved the email validator by making sure the domain name is valid. --- src/Valitron/Validator.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Valitron/Validator.php b/src/Valitron/Validator.php index c629ca4..13b8e6d 100644 --- a/src/Valitron/Validator.php +++ b/src/Valitron/Validator.php @@ -497,7 +497,12 @@ class Validator */ protected function validateEmail($field, $value) { - return filter_var($value, \FILTER_VALIDATE_EMAIL) !== false; + if (filter_var($value, \FILTER_VALIDATE_EMAIL) !== false) { + $domain = idn_to_ascii(ltrim(stristr($value, '@'), '@'), 0, INTL_IDNA_VARIANT_UTS46) . '.'; + if (checkdnsrr($domain, 'ANY')) {return true;} + } + + return false; } /** From 255384baa80793d06fc6222687655ab9c2bdb22c Mon Sep 17 00:00:00 2001 From: Jabari Hunt Date: Wed, 13 Dec 2017 14:52:24 -0600 Subject: [PATCH 2/7] Made the domain check an option (via third method param). --- src/Valitron/Validator.php | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/Valitron/Validator.php b/src/Valitron/Validator.php index 13b8e6d..ecbca4e 100644 --- a/src/Valitron/Validator.php +++ b/src/Valitron/Validator.php @@ -84,7 +84,7 @@ class Validator * @param array $fields * @param string $lang * @param string $langDir - * @throws \InvalidArgumentException + * @throws emInvalidArgumentException */ public function __construct($data = array(), $fields = array(), $lang = null, $langDir = null) { @@ -495,14 +495,18 @@ class Validator * @param mixed $value * @return bool */ - protected function validateEmail($field, $value) + protected function validateEmail($field, $value, $checkDomain = false) { + $emailIsValid = false; if (filter_var($value, \FILTER_VALIDATE_EMAIL) !== false) { - $domain = idn_to_ascii(ltrim(stristr($value, '@'), '@'), 0, INTL_IDNA_VARIANT_UTS46) . '.'; - if (checkdnsrr($domain, 'ANY')) {return true;} + $emailIsValid = true; + if ($checkDomain) { + $domain = idn_to_ascii(ltrim(stristr($value, '@'), '@'), 0, INTL_IDNA_VARIANT_UTS46) . '.'; + if (!checkdnsrr($domain, 'ANY')) {$emailIsValid = false;} + } } - return false; + return $emailIsValid; } /** From 55154359dc022c380deaa1ba8569c42f854f32db Mon Sep 17 00:00:00 2001 From: Jabari Hunt Date: Wed, 13 Dec 2017 16:06:31 -0600 Subject: [PATCH 3/7] Fixed a typo that was introduces in an unrelated comment. --- src/Valitron/Validator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Valitron/Validator.php b/src/Valitron/Validator.php index ecbca4e..dfc594d 100644 --- a/src/Valitron/Validator.php +++ b/src/Valitron/Validator.php @@ -84,7 +84,7 @@ class Validator * @param array $fields * @param string $lang * @param string $langDir - * @throws emInvalidArgumentException + * @throws \InvalidArgumentException */ public function __construct($data = array(), $fields = array(), $lang = null, $langDir = null) { From 28ef275cde1c8738eaa8baa1b728107daeaac311 Mon Sep 17 00:00:00 2001 From: Jabari Hunt Date: Mon, 1 Jan 2018 15:11:21 -0600 Subject: [PATCH 4/7] Restored the original validateEmail() method. Created a new method called validateEmailDNS() that validates both the email address itself (using the ValidateEmail() method) as well as the domain name in the email address. --- src/Valitron/Validator.php | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/src/Valitron/Validator.php b/src/Valitron/Validator.php index dfc594d..f085bb5 100644 --- a/src/Valitron/Validator.php +++ b/src/Valitron/Validator.php @@ -495,18 +495,26 @@ class Validator * @param mixed $value * @return bool */ - protected function validateEmail($field, $value, $checkDomain = false) + protected function validateEmail($field, $value) { - $emailIsValid = false; - if (filter_var($value, \FILTER_VALIDATE_EMAIL) !== false) { - $emailIsValid = true; - if ($checkDomain) { - $domain = idn_to_ascii(ltrim(stristr($value, '@'), '@'), 0, INTL_IDNA_VARIANT_UTS46) . '.'; - if (!checkdnsrr($domain, 'ANY')) {$emailIsValid = false;} - } + return filter_var($value, \FILTER_VALIDATE_EMAIL) !== false; + } + + /** + * Validate that a field is a valid e-mail address and the domain name is active + * + * @param string $field + * @param mixed $value + * @return bool + */ + protected function validateEmailDNS($field, $value) + { + if ($this->validateEmail($field, $value)) { + $domain = ltrim(stristr($value, '@'), '@') . '.'; + return checkdnsrr($domain, 'ANY'); } - return $emailIsValid; + return false; } /** From 8603b2bec385fc6c7cfd27c73280ee06f0164d95 Mon Sep 17 00:00:00 2001 From: Jabari Date: Fri, 5 Jan 2018 22:28:47 -0600 Subject: [PATCH 5/7] Added idn_to_ascii() with a check to make sure the function itself exists as well as a check for a required constant that it needs to function. --- src/Valitron/Validator.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Valitron/Validator.php b/src/Valitron/Validator.php index f085bb5..4eae399 100644 --- a/src/Valitron/Validator.php +++ b/src/Valitron/Validator.php @@ -511,6 +511,9 @@ class Validator { if ($this->validateEmail($field, $value)) { $domain = ltrim(stristr($value, '@'), '@') . '.'; + if (function_exists('idn_to_ascii') && defined('INTL_IDNA_VARIANT_UTS46')) { + $domain = idn_to_ascii($domain, 0, INTL_IDNA_VARIANT_UTS46); + } return checkdnsrr($domain, 'ANY'); } From 7dcc8a5089b0e40b492a1ebf932ee9eaa18f7285 Mon Sep 17 00:00:00 2001 From: Willem Wollebrants Date: Wed, 31 Jan 2018 16:19:05 +0100 Subject: [PATCH 6/7] add test for new emailDNS validator --- tests/Valitron/ValidateTest.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/Valitron/ValidateTest.php b/tests/Valitron/ValidateTest.php index adf296e..d599cb6 100644 --- a/tests/Valitron/ValidateTest.php +++ b/tests/Valitron/ValidateTest.php @@ -560,6 +560,18 @@ class ValidateTest extends BaseTestCase $this->assertFalse($v->validate()); } + public function testEmailDnsValid(){ + $v = new Validator(array('name' => 'Chester Tester', 'email' => 'chester@tester.com')); + $v->rule('emailDNS', 'email'); + $this->assertTrue($v->validate()); + } + + public function testEmailDnsInvalid(){ + $v = new Validator(array('name' => 'Chester Tester', 'email' => 'chester@tester.zyx')); + $v->rule('emailDNS', 'email'); + $this->assertFalse($v->validate()); + } + public function testUrlValid() { $v = new Validator(array('website' => 'http://google.com')); From f8e69683afa1af97a5e6c035dbaf5452c843eb89 Mon Sep 17 00:00:00 2001 From: Willem Wollebrants Date: Wed, 31 Jan 2018 16:20:58 +0100 Subject: [PATCH 7/7] add emailDNS validator to documentation --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 0d520fd..bb7a744 100644 --- a/README.md +++ b/README.md @@ -121,6 +121,7 @@ V::lang('ar'); * `notIn` - Negation of `in` rule (not in array of values) * `ip` - Valid IP address * `email` - Valid email address + * `emailDNS` - Valid email address with active DNS record * `url` - Valid URL * `urlActive` - Valid URL with active DNS record * `alpha` - Alphabetic characters only