Source for file email_validation.php

Documentation is available at email_validation.php

  1. <?php
  2. /*
  3.  * email_validation.php
  4.  *
  5.  * @(#) $Header: /srv/cvs/framework/09-Implementacao/terceiros/email_validation.php,v 1.1 2007/01/15 12:56:58 mmerlone Exp $
  6.  *
  7.  */
  8.  
  9. {
  10.     var $email_regular_expression="^([-!#\$%&'*+./0-9=?A-Z^_`a-z{|}~])+@([-!#\$%&'*+/0-9=?A-Z^_`a-z{|}~]+\\.)+[a-zA-Z]{2,6}\$";
  11.     var $timeout=0;
  12.     var $data_timeout=0;
  13.     var $localhost="";
  14.     var $localuser="";
  15.     var $debug=0;
  16.     var $html_debug=0;
  17.     var $exclude_address="";
  18.     var $getmxrr="GetMXRR";
  19.  
  20.     var $next_token="";
  21.     var $preg;
  22.     var $last_code="";
  23.  
  24.     Function Tokenize($string,$separator="")
  25.     {
  26.         if(!strcmp($separator,""))
  27.         {
  28.             $separator=$string;
  29.             $string=$this->next_token;
  30.         }
  31.         for($character=0;$character<strlen($separator);$character++)
  32.         {
  33.             if(GetType($position=strpos($string,$separator[$character]))=="integer")
  34.                 $found=(IsSet($foundmin($found,$position$position);
  35.         }
  36.         if(IsSet($found))
  37.         {
  38.             $this->next_token=substr($string,$found+1);
  39.             return(substr($string,0,$found));
  40.         }
  41.         else
  42.         {
  43.             $this->next_token="";
  44.             return($string);
  45.         }
  46.     }
  47.  
  48.     Function OutputDebug($message)
  49.     {
  50.         $message.="\n";
  51.         if($this->html_debug)
  52.             $message=str_replace("\n","<br />\n",HtmlEntities($message));
  53.         echo $message;
  54.         flush();
  55.     }
  56.  
  57.     Function GetLine($connection)
  58.     {
  59.         for($line="";;)
  60.         {
  61.             if(feof($connection))
  62.                 return(0);
  63.             $line.=fgets($connection,100);
  64.             $length=strlen($line);
  65.             if($length>=2
  66.             && substr($line,$length-2,2)=="\r\n")
  67.             {
  68.                 $line=substr($line,0,$length-2);
  69.                 if($this->debug)
  70.                     $this->OutputDebug("$line");
  71.                 return($line);
  72.             }
  73.         }
  74.     }
  75.  
  76.     Function PutLine($connection,$line)
  77.     {
  78.         if($this->debug)
  79.             $this->OutputDebug("$line");
  80.         return(fputs($connection,"$line\r\n"));
  81.     }
  82.  
  83.     Function ValidateEmailAddress($email)
  84.     {
  85.         if(IsSet($this->preg))
  86.         {
  87.             if(strlen($this->preg))
  88.                 return(preg_match($this->preg,$email));
  89.         }
  90.         else
  91.         {
  92.             $this->preg=(function_exists("preg_match""/".str_replace("/""\\/"$this->email_regular_expression)."/" "");
  93.             return($this->ValidateEmailAddress($email));
  94.         }
  95.         return(eregi($this->email_regular_expression,$email)!=0);
  96.     }
  97.  
  98.     Function ValidateEmailHost($email)
  99.     {
  100.         if(!$this->ValidateEmailAddress($email))
  101.             return(0);
  102.         $user=$this->Tokenize($email,"@");
  103.         $domain=$this->Tokenize("");
  104.         $hosts=$weights=array();
  105.         $getmxrr=$this->getmxrr;
  106.         if(function_exists($getmxrr)
  107.         && $getmxrr($domain,$hosts,$weights))
  108.         {
  109.             $mxhosts=array();
  110.             for($host=0;$host<count($hosts);$host++)
  111.                 $mxhosts[$weights[$host]]=$hosts[$host];
  112.             KSort($mxhosts);
  113.             for(Reset($mxhosts),$host=0;$host<count($mxhosts);Next($mxhosts),$host++)
  114.                 $hosts[$host]=$mxhosts[Key($mxhosts)];
  115.         }
  116.         else
  117.         {
  118.             if(strcmp($ip=@gethostbyname($domain),$domain)
  119.             && (strlen($this->exclude_address)==0
  120.             || strcmp(@gethostbyname($this->exclude_address),$ip)))
  121.                 $hosts[]=$domain;
  122.         }
  123.         return(count($hosts)!=0);
  124.     }
  125.  
  126.     Function VerifyResultLines($connection,$code)
  127.     {
  128.         while(($line=$this->GetLine($connection)))
  129.         {
  130.             $this->last_code=$this->Tokenize($line," -");
  131.             if(strcmp($this->last_code,$code))
  132.                 return(0);
  133.             if(!strcmp(substr($linestrlen($this->last_code)1)," "))
  134.                 return(1);
  135.         }
  136.         return(-1);
  137.     }
  138.  
  139.     Function ValidateEmailBox($email)
  140.     {
  141.         if(!$this->ValidateEmailHost($email,$hosts))
  142.             return(0);
  143.         if(!strcmp($localhost=$this->localhost,"")
  144.         && !strcmp($localhost=getenv("SERVER_NAME"),"")
  145.         && !strcmp($localhost=getenv("HOST"),""))
  146.            $localhost="localhost";
  147.         if(!strcmp($localuser=$this->localuser,"")
  148.         && !strcmp($localuser=getenv("USERNAME"),"")
  149.         && !strcmp($localuser=getenv("USER"),""))
  150.            $localuser="root";
  151.         for($host=0;$host<count($hosts);$host++)
  152.         {
  153.             $domain=$hosts[$host];
  154.             if(ereg('^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$',$domain))
  155.                 $ip=$domain;
  156.             else
  157.             {
  158.                 if($this->debug)
  159.                     $this->OutputDebug("Resolving host name \"".$hosts[$host]."\"...");
  160.                 if(!strcmp($ip=@gethostbyname($domain),$domain))
  161.                 {
  162.                     if($this->debug)
  163.                         $this->OutputDebug("Could not resolve host name \"".$hosts[$host]."\".");
  164.                     continue;
  165.                 }
  166.             }
  167.             if(strlen($this->exclude_address)
  168.             && !strcmp(@gethostbyname($this->exclude_address),$ip))
  169.             {
  170.                 if($this->debug)
  171.                     $this->OutputDebug("Host address of \"".$hosts[$host]."\" is the exclude address");
  172.                 continue;
  173.             }
  174.             if($this->debug)
  175.                 $this->OutputDebug("Connecting to host address \"".$ip."\"...");
  176.             if(($connection=($this->timeout ? @fsockopen($ip,25,$errno,$error,$this->timeout@fsockopen($ip,25))))
  177.             {
  178.                 $timeout=($this->data_timeout ? $this->data_timeout : $this->timeout);
  179.                 if($timeout
  180.                 && function_exists("socket_set_timeout"))
  181.                     socket_set_timeout($connection,$timeout,0);
  182.                 if($this->debug)
  183.                     $this->OutputDebug("Connected.");
  184.                 if($this->VerifyResultLines($connection,"220")>0
  185.                 && $this->PutLine($connection,"HELO $localhost")
  186.                 && $this->VerifyResultLines($connection,"250")>0
  187.                 && $this->PutLine($connection,"MAIL FROM: <$localuser@$localhost>")
  188.                 && $this->VerifyResultLines($connection,"250")>0
  189.                 && $this->PutLine($connection,"RCPT TO: <$email>")
  190.                 && ($result=$this->VerifyResultLines($connection,"250"))>=0)
  191.                 {
  192.                     if($result)
  193.                     {
  194.                         if($this->PutLine($connection,"DATA"))
  195.                             $result=($this->VerifyResultLines($connection,"354")!=0);
  196.                     }
  197.                     else
  198.                     {
  199.                         if(strlen($this->last_code)
  200.                         && !strcmp($this->last_code[0],"4"))
  201.                             $result=-1;
  202.                     }
  203.                     if($this->debug)
  204.                         $this->OutputDebug("This host states that the address is ".($result ($result>"valid" "undetermined""not valid").".");
  205.                     fclose($connection);
  206.                     if($this->debug)
  207.                         $this->OutputDebug("Disconnected.");
  208.                     return($result);
  209.                 }
  210.                 if($this->debug)
  211.                     $this->OutputDebug("Unable to validate the address with this host.");
  212.                 fclose($connection);
  213.                 if($this->debug)
  214.                     $this->OutputDebug("Disconnected.");
  215.             }
  216.             else
  217.             {
  218.                 if($this->debug)
  219.                     $this->OutputDebug("Failed.");
  220.             }
  221.         }
  222.         return(-1);
  223.     }
  224. };
  225.  
  226. ?>

Documentation generated on Sun, 09 Mar 2008 23:51:42 -0300 by phpDocumentor 1.4.0

SourceForge.net Logo Support This Project