Skip to content

Commit

Permalink
fixed some problems in DNSCheckValidation (#116)
Browse files Browse the repository at this point in the history
  • Loading branch information
issei-m authored and egulias committed Jun 24, 2016
1 parent afde3d3 commit 7701238
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 16 deletions.
18 changes: 4 additions & 14 deletions EmailValidator/Validation/DNSCheckValidation.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ public function isValid($email, EmailLexer $emailLexer)
{
// use the input to check DNS if we cannot extract something similar to a domain
$host = $email;

// Arguable pattern to extract the domain. Not aiming to validate the domain nor the email
$pattern = "/^[a-z'0-9]+([+._-][a-z'0-9]+)*@([a-z0-9]+([._-][a-z0-9]+)+)+$/";
if (preg_match($pattern, $email, $result)) {
$host = $this->extractHost($result);
if (false !== $lastAtPos = strrpos($email, '@')) {
$host = substr($email, $lastAtPos + 1);
}

return $this->checkDNS($host);
Expand All @@ -46,24 +46,14 @@ public function getWarnings()
return $this->warnings;
}

private function extractHost(array $result)
{
foreach ($result as $match) {
$onlyDomainPattern = "/^([a-z0-9]+([._-][a-z0-9]+))+$/";
if (preg_match($onlyDomainPattern, $match, $domainResult)) {
return $domainResult[0];
}
}
}

protected function checkDNS($host)
{
$Aresult = true;
$MXresult = checkdnsrr($host, 'MX');

if (!$MXresult) {
$this->warnings[NoDNSMXRecord::CODE] = new NoDNSMXRecord();
$Aresult = checkdnsrr($host, 'A');
$Aresult = checkdnsrr($host, 'A') || checkdnsrr($host, 'AAAA');
if (!$Aresult) {
$this->error = new NoDNSRecord();
}
Expand Down
13 changes: 11 additions & 2 deletions Tests/EmailValidator/Validation/DNSCheckValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,17 @@ class DNSCheckValidationTest extends \PHPUnit_Framework_TestCase
public function validEmailsProvider()
{
return [
["example@example.com"],
["example+foo@example.com"],
// dot-atom
['Abc@example.com'],
['ABC@EXAMPLE.COM'],
['Abc.123@example.com'],
['user+mailbox/department=shipping@example.com'],
['!#$%&\'*+-/=?^_`.{|}~@example.com'],

// quoted string
['"Abc@def"@example.com'],
['"Fred\ Bloggs"@example.com'],
['"Joe.\\Blow"@example.com'],
];
}

Expand Down

0 comments on commit 7701238

Please sign in to comment.