2010-06-24 09:40:05 +01:00
|
|
|
<?php
|
|
|
|
|
2010-10-02 11:42:31 +01:00
|
|
|
/*
|
2011-01-15 13:29:43 +00:00
|
|
|
* This file is part of the Symfony package.
|
2010-10-02 11:42:31 +01:00
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
|
*
|
2011-01-15 13:29:43 +00:00
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
2010-10-02 11:42:31 +01:00
|
|
|
*/
|
|
|
|
|
2011-01-15 13:29:43 +00:00
|
|
|
namespace Symfony\Component\Validator\Constraints;
|
|
|
|
|
2010-08-20 22:09:55 +01:00
|
|
|
use Symfony\Component\Validator\Constraint;
|
|
|
|
use Symfony\Component\Validator\ConstraintValidator;
|
|
|
|
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
|
2010-06-24 09:40:05 +01:00
|
|
|
|
|
|
|
class EmailValidator extends ConstraintValidator
|
|
|
|
{
|
2010-06-24 10:24:08 +01:00
|
|
|
const PATTERN = '/^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i';
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
public function isValid($value, Constraint $constraint)
|
2010-06-24 09:40:05 +01:00
|
|
|
{
|
2010-12-21 02:59:17 +00:00
|
|
|
if (null === $value || '' === $value) {
|
2010-06-24 10:24:08 +01:00
|
|
|
return true;
|
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString()'))) {
|
|
|
|
throw new UnexpectedTypeException($value, 'string');
|
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
$value = (string)$value;
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
if (!preg_match(self::PATTERN, $value)) {
|
2010-09-29 06:59:45 +01:00
|
|
|
$this->setMessage($constraint->message, array('{{ value }}' => $value));
|
2010-06-24 10:24:08 +01:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($constraint->checkMX) {
|
2010-09-10 19:27:17 +01:00
|
|
|
$host = substr($value, strpos($value, '@') + 1);
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
if (!$this->checkMX($host)) {
|
2010-09-29 06:59:45 +01:00
|
|
|
$this->setMessage($constraint->message, array('{{ value }}' => $value));
|
2010-06-24 10:24:08 +01:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2010-06-24 09:40:05 +01:00
|
|
|
}
|
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
/**
|
2010-12-10 14:42:29 +00:00
|
|
|
* Check DNS Records for MX type.
|
2010-06-24 10:24:08 +01:00
|
|
|
*
|
|
|
|
* @param string $host Host name
|
2010-10-15 22:49:09 +01:00
|
|
|
*
|
2011-01-21 01:39:28 +00:00
|
|
|
* @return Boolean
|
2010-06-24 10:24:08 +01:00
|
|
|
*/
|
|
|
|
private function checkMX($host)
|
2010-06-24 09:40:05 +01:00
|
|
|
{
|
2010-12-10 14:42:29 +00:00
|
|
|
if (function_exists('checkdnsrr')) {
|
2010-06-24 10:24:08 +01:00
|
|
|
return checkdnsrr($host, 'MX');
|
2010-06-24 09:40:05 +01:00
|
|
|
}
|
|
|
|
|
2011-02-04 18:10:13 +00:00
|
|
|
throw new \LogicException('Could not retrieve DNS record information. Remove check_mx = true to prevent this warning');
|
2010-06-24 09:40:05 +01:00
|
|
|
}
|
2011-02-04 18:10:13 +00:00
|
|
|
}
|