[Validator] added less-strict email host verification

New checkHost attribute in email constraint will
make the validator check for only one of MX, A or AAAA
DNS resource records to verify it as a valid
email address.
This commit is contained in:
Konrad Mohrfeldt 2012-04-06 02:33:41 +02:00
parent 0ccb6fa48d
commit f617e02a96
2 changed files with 17 additions and 2 deletions

View File

@ -22,4 +22,5 @@ class Email extends Constraint
{
public $message = 'This value is not a valid email address';
public $checkMX = false;
public $checkHost = false;
}

View File

@ -51,9 +51,11 @@ class EmailValidator extends ConstraintValidator
$valid = false;
}
// Check MX records
// Check for host DNS resource records
if ($valid && $constraint->checkMX) {
$valid = $this->checkMX($host);
} else if ($valid && $constraint->checkHost) {
$valid = $this->checkHost($host);
}
}
@ -69,7 +71,7 @@ class EmailValidator extends ConstraintValidator
/**
* Check DNS Records for MX type.
*
* @param string $host Host name
* @param string $host Hostname
*
* @return Boolean
*/
@ -77,4 +79,16 @@ class EmailValidator extends ConstraintValidator
{
return checkdnsrr($host, 'MX');
}
/**
* Check if one of MX, A or AAAA DNS RR exists.
*
* @param string $host Hostname
*
* @return Boolean
*/
private function checkHost($host)
{
return $this->checkMX($host) || (checkdnsrr($host, "A") || checkdnsrr($host, "AAAA"));
}
}