2010-06-24 09:40:05 +01:00
|
|
|
<?php
|
|
|
|
|
2010-08-20 22:09:55 +01:00
|
|
|
namespace Symfony\Component\Validator\Constraints;
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-10-02 11:42:31 +01:00
|
|
|
/*
|
|
|
|
* This file is part of the Symfony framework.
|
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
|
*
|
|
|
|
* This source file is subject to the MIT license that is bundled
|
|
|
|
* with this source code in the file LICENSE.
|
|
|
|
*/
|
|
|
|
|
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-06-24 10:24:08 +01:00
|
|
|
if ($value === null) {
|
|
|
|
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
|
|
|
/**
|
|
|
|
* Check DNA Records for MX type (from Doctrine EmailValidator)
|
|
|
|
*
|
|
|
|
* @param string $host Host name
|
|
|
|
* @return boolean
|
|
|
|
* @licence This software consists of voluntary contributions made by many individuals
|
|
|
|
* and is licensed under the LGPL. For more information, see
|
|
|
|
* <http://www.phpdoctrine.org>.
|
|
|
|
*/
|
|
|
|
private function checkMX($host)
|
2010-06-24 09:40:05 +01:00
|
|
|
{
|
2010-06-24 10:24:08 +01:00
|
|
|
// We have different behavior here depending of OS and PHP version
|
|
|
|
if (strtolower(substr(PHP_OS, 0, 3)) == 'win' && version_compare(PHP_VERSION, '5.3.0', '<')) {
|
|
|
|
$output = array();
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
@exec('nslookup -type=MX '.escapeshellcmd($host) . ' 2>&1', $output);
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
if (empty($output)) {
|
|
|
|
throw new ValidatorError('Unable to execute DNS lookup. Are you sure PHP can call exec()?');
|
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
foreach ($output as $line) {
|
|
|
|
if (preg_match('/^'.$host.'/', $line)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
} else if (function_exists('checkdnsrr')) {
|
|
|
|
return checkdnsrr($host, 'MX');
|
2010-06-24 09:40:05 +01:00
|
|
|
}
|
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
throw new ValidatorError('Could not retrieve DNS record information. Remove check_mx = true to prevent this warning');
|
2010-06-24 09:40:05 +01:00
|
|
|
}
|
|
|
|
}
|