Permalink
Browse files

Merge pull request #718 from DaSourcerer/164-CEmailValidator

Fix for #164: CEmailValidator
  • Loading branch information...
2 parents bb69f36 + c5387e8 commit 5a25008c9fe6e3543db6fac028d4ff927a756eb0 Maurizio Domba committed May 22, 2012
Showing with 45 additions and 3 deletions.
  1. +1 −0 CHANGELOG
  2. +44 −3 framework/validators/CEmailValidator.php
View
@@ -4,6 +4,7 @@
Version 1.1.11 work in progress
-------------------------------
- Bug #145: CGettextMoFile now can parse strings with no context (eagleoneraptor)
+- Bug #164: CEmailValidator.checkPort now checks the port 25 of listed MX servers (DaSourcerer)
- Bug #193: Changed datetime column type for postgresql from 'time' to 'timestamp' (cebe)
- Bug #238: Fixed the problem that empty row could be selected in CGridView when there was no data (mdomba)
- Bug #295: Sometimes CJSON::decode returns null because native json_encode has bugs and returns null. Workaround to continue decoding when result of json_decode is null (luislobo)
@@ -42,7 +42,8 @@ class CEmailValidator extends CValidator
public $checkMX=false;
/**
* @var boolean whether to check port 25 for the email address.
- * Defaults to false.
+ * Defaults to false. To enable it, ensure that the PHP functions 'dns_get_record' and
+ * 'fsockopen' are available in your PHP installation.
*/
public $checkPort=false;
/**
@@ -85,8 +86,8 @@ public function validateValue($value)
$domain=rtrim(substr($value,strpos($value,'@')+1),'>');
if($valid && $this->checkMX && function_exists('checkdnsrr'))
$valid=checkdnsrr($domain,'MX');
- if($valid && $this->checkPort && function_exists('fsockopen'))
- $valid=fsockopen($domain,25)!==false;
+ if($valid && $this->checkPort && function_exists('fsockopen') && function_exists('dns_get_record'))
+ $valid=$this->checkMxPorts($domain);
return $valid;
}
@@ -115,4 +116,44 @@ public function clientValidateAttribute($object,$attribute)
}
";
}
+
+ /**
+ * Retrieves the list of MX records for $domain and checks if port 25
+ * is opened on any of these.
+ * @since 1.1.11
+ * @param string $domain domain to be checked
+ * @return boolean true if a reachable MX server has been found
+ */
+ protected function checkMxPorts($domain)
+ {
+ $records=dns_get_record($domain, DNS_MX);
+ if($records===false || empty($records))
+ return false;
+ usort($records,array($this,'mxSort'));
+ foreach($records as $record)
+ {
+ $handle=fsockopen($record['target'],25);
+ if($handle!==false)
+ {
+ fclose($handle);
+ return true;
+ }
+ }
+ return false;
+ }
+
+ /**
+ * Determines if one MX record has higher priority as another
+ * (i.e. 'pri' is lower). Used by {@link checkMxPorts}.
+ * @since 1.1.11
+ * @param mixed $a
+ * @param mixed $b
+ * @return boolean
+ */
+ protected function mxSort($a, $b)
+ {
+ if($a['pri']==$b['pri'])
+ return 0;
+ return ($a['pri']<$b['pri'])?-1:1;
+ }
}

0 comments on commit 5a25008

Please sign in to comment.