[ Index ]
 

Code source de e107 0.7.8

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/e107_handlers/ -> mail_validation_class.php (source)

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


Généré le : Sun Apr 1 01:23:32 2007 par Balluche grâce à PHPXref 0.7