summaryrefslogtreecommitdiff
path: root/includes/general.php
diff options
context:
space:
mode:
Diffstat (limited to 'includes/general.php')
-rw-r--r--includes/general.php84
1 files changed, 67 insertions, 17 deletions
diff --git a/includes/general.php b/includes/general.php
index 065daaa..17b449b 100644
--- a/includes/general.php
+++ b/includes/general.php
@@ -538,12 +538,12 @@
if(preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\+\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/" , $email))
{
list($username,$domain)=explode('@',$email,2);
- $mxhosts = array();
+ $mxhostrr = array();
$mxweight = array();
- if( !getmxrr($domain, $mxhosts, $mxweight) ) {
+ if( !getmxrr($domain, $mxhostrr, $mxweight) ) {
$mxhostrr = array($domain);
$mxweight = array(0);
- } else if ( empty($mxhosts) ) {
+ } else if ( empty($mxhostrr) ) {
$mxhostrr = array($domain);
$mxweight = array(0);
}
@@ -553,12 +553,13 @@
$mx_host = trim($mxhostrr[$i], '.');
$mx_prio = $mxweight[$i];
if(empty($mxhostprio[$mx_prio])) {
- $mxhostprio[$mx_prio] = arraY();
+ $mxhostprio[$mx_prio] = array();
}
$mxhostprio[$mx_prio][] = $mx_host;
}
array_walk($mxhostprio, function(&$mx) { shuffle($mx); } );
+ ksort($mxhostprio);
$mxhosts = array();
foreach($mxhostprio as $mx_prio => $mxhostnames) {
@@ -569,28 +570,77 @@
foreach($mxhosts as $key => $domain)
{
- $fp = @fsockopen($domain,25,$errno,$errstr,5);
+ $fp_opt = array(
+ 'ssl' => array(
+ 'verify_peer' => false, // Opportunistic Encryption
+ )
+ );
+ $fp_ctx = stream_context_create($fp_opt);
+ $fp = @stream_socket_client("tcp://$domain:25",$errno,$errstr,5,STREAM_CLIENT_CONNECT,$fp_ctx);
if($fp)
{
+ stream_set_blocking($fp, true);
+
+ $has_starttls = false;
- $line = fgets($fp, 4096);
- while(substr($line, 0, 4) == "220-")
- $line = fgets($fp, 4096);
- if(substr($line, 0, 3) != "220")
+ do {
+ $line = fgets($fp, 4096);
+ } while(substr($line, 0, 4) == "220-");
+ if(substr($line, 0, 3) != "220") {
+ fclose($fp);
continue;
- fputs($fp, "HELO www.cacert.org\r\n");
- $line = fgets($fp, 4096);
- while(substr($line, 0, 3) == "220")
+ }
+
+ fputs($fp, "EHLO www.cacert.org\r\n");
+ do {
$line = fgets($fp, 4096);
- if(substr($line, 0, 3) != "250")
+ $has_starttls |= substr(trim($line),4) == "STARTTLS";
+ } while(substr($line, 0, 4) == "250-");
+ if(substr($line, 0, 3) != "250") {
+ fclose($fp);
continue;
- fputs($fp, "MAIL FROM:<returns@cacert.org>\r\n");
- $line = fgets($fp, 4096);
+ }
+
+ if($has_starttls) {
+ fputs($fp, "STARTTLS\r\n");
+ do {
+ $line = fgets($fp, 4096);
+ } while(substr($line, 0, 4) == "220-");
+ if(substr($line, 0, 3) != "220") {
+ fclose($fp);
+ continue;
+ }
+
+ stream_socket_enable_crypto($fp, true, STREAM_CRYPTO_METHOD_TLS_CLIENT);
+
+ fputs($fp, "EHLO www.cacert.org\r\n");
+ do {
+ $line = fgets($fp, 4096);
+ } while(substr($line, 0, 4) == "250-");
+ if(substr($line, 0, 3) != "250") {
+ fclose($fp);
+ continue;
+ }
+ }
- if(substr($line, 0, 3) != "250")
+ fputs($fp, "MAIL FROM:<returns@cacert.org>\r\n");
+ do {
+ $line = fgets($fp, 4096);
+ } while(substr($line, 0, 4) == "250-");
+ if(substr($line, 0, 3) != "250") {
+ fclose($fp);
continue;
+ }
+
fputs($fp, "RCPT TO:<$email>\r\n");
- $line = trim(fgets($fp, 4096));
+ do {
+ $line = fgets($fp, 4096);
+ } while(substr($line, 0, 4) == "250-");
+ if(substr($line, 0, 3) != "250") {
+ fclose($fp);
+ continue;
+ }
+
fputs($fp, "QUIT\r\n");
fclose($fp);