Jelajahi Sumber

Fix for bad regexp used in PHP <= 5.3.2. Closes #897

Joseph Bielawski 14 tahun lalu
induk
melakukan
d235570653
1 mengubah file dengan 16 tambahan dan 12 penghapusan
  1. 16 12
      src/Symfony/Component/Validator/Constraints/EmailValidator.php

+ 16 - 12
src/Symfony/Component/Validator/Constraints/EmailValidator.php

@@ -17,7 +17,6 @@ use Symfony\Component\Validator\Exception\UnexpectedTypeException;
 
 class EmailValidator extends ConstraintValidator
 {
-
     public function isValid($value, Constraint $constraint)
     {
         if (null === $value || '' === $value) {
@@ -29,23 +28,28 @@ class EmailValidator extends ConstraintValidator
         }
 
         $value = (string) $value;
+        $valid = filter_var($value, FILTER_VALIDATE_EMAIL);
 
-        if (!filter_var($value, FILTER_VALIDATE_EMAIL)) {
-            $this->setMessage($constraint->message, array('{{ value }}' => $value));
-
-            return false;
-        }
-
-        if ($constraint->checkMX) {
+        if ($valid) {
             $host = substr($value, strpos($value, '@') + 1);
 
-            if (!$this->checkMX($host)) {
-                $this->setMessage($constraint->message, array('{{ value }}' => $value));
+            // Likely not a FQDN, bug in PHP FILTER_VALIDATE_EMAIL prior to PHP 5.3.3
+            if (version_compare(PHP_VERSION, '5.3.3', '<') && strpos($host, '.') === false) {
+                $valid = false;
+            }
 
-                return false;
+            // Do not check MX records if host is invalid
+            if ($valid && $constraint->checkMX) {
+                $valid = $this->checkMX($host));
             }
         }
 
+        if (!$valid) {
+            $this->setMessage($constraint->message, array('{{ value }}' => $value));
+
+            return false;
+        }
+
         return true;
     }
 
@@ -64,4 +68,4 @@ class EmailValidator extends ConstraintValidator
 
         throw new \LogicException('Could not retrieve DNS record information. Remove check_mx = true to prevent this warning');
     }
-}
+}