Validação de e-mail usando o DIG

Publicado por João Paulo de Oliveira Faria 16/09/2004

[ Hits: 7.443 ]

Homepage: http://www.jpfaria.com

Download validatemail.zip




Classe que valida email usando o DIG para conferir se o domínio digitado existe na internet dentre outras conferências.

Funciona somente em servidores *unix que tenha o DIG corretamente instalado.

Na maioria das versões Linux o dig ja vem instalado.

Contribuições e melhorias no código serão
bem vindas.

  



Esconder código-fonte

<? // validatemail.php

/* 
--------------------------------------------------------------------------
VALIDAÇÃO DE EMAIL USANDO O DIG

AUTOR: JOÃO PAULO DE OLIVEIRA FARIA
ICQ: 13041300
MSN: JOAOPAULO@AWM.COM.BR
EMAIL: JOAOPAULO@DEOLIVEIRA.COM.BR
VERSÃO: 0.1.0
--------------------------------------------------------------------------
*/

class validatemail
{

   var $config = array(

      "stop_error"       => 2,
      "show_message"      => 1,
      "enable_dig"      => 1

    );


   var $get = array(

      "field"       => 1,
      "value"       => 1,
      "message"       => 1,
      "status"       => 1

    );

   var $inf = array(

      "dominio"      => 1,
      "email"       => 1,
      "usuario"       => 1

    );

   function setConfig($config){

       foreach ($config as $key => $value) {
         if ($this->config[$key]){
               $this->config[$key] = "$config[$key]";
         }
      }

    }

   function setGet($get){

      foreach ($get as $key => $value) {
         if ($this->get[$key]){
               $this->get[$key] = "$get[$key]";
         }
      }

    }

    function setInf($inf){

      foreach ($inf as $key => $value) {
         if ($this->inf[$key]){
               $this->inf[$key] = "$inf[$key]";
         }
      }

    }


   function stringLow($text){

         $arrayLower=array('ç'
         ,'â','ã','à','á','ä'
         ,'é','è','ê','ë'
         ,'í','ì','î','ï'
         ,'ó','ò','ô','õ','ö'
         ,'ú','ù','û','ü');

         $arrayUpper=array('Ç'
         ,'Â','Ã','Á','À','Ä'
         ,'É','È','Ê','Ë'
         ,'Í','Ì','Î','Ï'
         ,'Ó','Ò','Õ','Ô','Ö'
         ,'Ú','Ù','Û','Ü');

         $arrayFinal=array('c'
         ,'a','a','a','a','a'
         ,'e','e','e','e'
         ,'i','i','i','i'
         ,'o','o','o','o'
         ,'u','u','u','u');


      $text=strtolower($text);
      $text=str_replace($arrayLower, $arrayFinal, $text);
      $text=str_replace($arrayUpper, $arrayFinal, $text);

         return $text;
   }



   function showMessage(){


      if ($this->config['show_message']=="1"){

         $get = $this->get['status'];

         if ($get=="1"){

            $msg = "OK    ->";
         }else{

            $msg = "ERROR ->";

         }
         
         $message = $this->get['message'];
         $value = $this->get['value'];
         $field = $this->get['field'];
         
         echo $msg." ".$message.' / Field:['.$field.'] Value:['.$value.']'."\n";

      }

   }

   function stop(){

      if ($this->config['stop_error']=="1"){

         exit();

      }

   }

   
   function chkLogin($login){




      if (strlen($login)>32){

         $get = array(

            "field"    => "usuario",
            "value"    => "$login",
            "message"    => "MAX LENGHT OF LOGIN IS 32",
                "status"     => "2"

          );

         $this->setGet($get);
         $this->showMessage();
         $this->stop();

         return FALSE;

      }else if (is_numeric(substr($login, 0, 1))){

         $get = array(

            "field"    => "usuario",
            "value"    => "$login",
            "message"    => "THE FIRST CHAR IS NUMBER",
                "status"     => "2"

          );

         $this->setGet($get);
         $this->showMessage();
         $this->stop();

         return FALSE;

      }else{

         $arrayIni=array(
            '"','\'','','\\','!','#',
            '$','%','¨','&','*',
            '(',')','=','+','{','}',
            '`','´','~','^',']','[',
            ',',':',';','/','?','|',' ','ñ',
         'ç','â','ã','à','á','ä'
            ,'é','è','ê','ë'
            ,'í','ì','î','ï'
            ,'ó','ò','ô','õ','ö'
            ,'ú','ù','û','ü','Ç'
            ,'Â','Ã','Á','À','Ä'
            ,'É','È','Ê','Ë'
            ,'Í','Ì','Î','Ï'
            ,'Ó','Ò','Õ','Ô','Ö'
            ,'Ú','Ù','Û','Ü'
            );


            $arrayFinal=array();


         $text=str_replace($arrayIni, $arrayFinal, $login);


         if($text!=$login){


            $get = array(

               "field"    => "usuario",
               "value"    => "$login",
               "message"    => "ILEGAL CHARS",
                   "status"     => "2"

             );

            $this->setGet($get);
            $this->showMessage();
            $this->stop();

            return FALSE;

         }else{

            return TRUE;

         }

      }




   }
   
   
   function chkDig($domain){

   
      
      $return = shell_exec("dig ".$domain." +trace");
      $lines = explode("\n",$return);

      $cont = 0;

      foreach ($lines as $line_num => $line) {

         if (ereg('Received',$line)){

            $cont++;
            

         }

      }



      if ( $cont <= "3" ){

         $get = array(

            "field"    => "dominio",
            "value"    => "$domain",
            "message"    => "DIG WARNING: INVALID DOMAIN",
                "status"     => "2"

          );

         $this->setGet($get);
         $this->showMessage();
         $this->stop();

         return FALSE;
         
         echo "aqui";

      }else{
      
         $inf = array(

            "dominio"   => "$domain"

          );

         $this->setInf($inf);

         return TRUE;
      
      
      }

   }


   function chkDomain($domain){

      $inf = array(

         "dominio"   => "$domain"

      );

      $this->setInf($inf);

      $domainlower = strtolower($domain);

      $arrayIni=array(
      '"','\'','','\\','!','#',
      '$','%','¨','&','*',
      '(',')','=','+','{','}',
      '`','´','~','^',']','[',
      ',',':',';','/','?','|','@',

      'ñ',
         'ç','â','ã','à','á','ä'
            ,'é','è','ê','ë'
            ,'í','ì','î','ï'
            ,'ó','ò','ô','õ','ö'
            ,'ú','ù','û','ü','Ç'
            ,'Â','Ã','Á','À','Ä'
            ,'É','È','Ê','Ë'
            ,'Í','Ì','Î','Ï'
            ,'Ó','Ò','Õ','Ô','Ö'
            ,'Ú','Ù','Û','Ü'
          );


         $arrayFinal=array();


      $newdomain=str_replace($arrayIni, $arrayFinal, $domain);

                if($domain!=$newdomain){

                   $get = array(

            "field"    => "dominio",
            "value"    => "$domain",
            "message"    => "ILEGAL CHAR DOMAIN",
                "status"     => "2"

          );

         $this->setGet($get);
         $this->showMessage();
         $this->stop();

         return FALSE;


                }elseif (!is_string(substr($domainlower, 0, 1))){

                   $get = array(

            "field"    => "dominio",
            "value"    => "$domain",
            "message"    => "ILEGAL CHAR IN FIRST CHAR DOMAIN",
                "status"     => "2"

          );

         $this->setGet($get);
         $this->showMessage();
         $this->stop();

         return FALSE;

      }elseif($this->config['enable_dig']==1 and $this->chkDig($domainlower)){

            return TRUE;

         }else{

            $get = array(

               "field"    => "email",
               "value"    => "$domain",
               "message"    => "INVALID DOMAIN",
                   "status"     => "2"

             );

            $this->setGet($get);
            $this->showMessage();
            $this->stop();

            return FALSE;

         }

      


   }


   function chkEmail($email){

      
   
      if (!empty($email)){
      
         $field_email = explode('@',$email);


         $email = $this->stringLow($email);
         $usuario = $this->stringLow($field_email[0]);
         $domain = $this->stringLow($field_email[1]);


                   $inf = array(

            "email"    => "$email",
            "usuario"   => "$usuario"

         );

         $this->setInf($inf);


         if ((!empty($field_email['0']) and $this->chkLogin($field_email['0'])) and (!empty($field_email['1']) and $this->chkDomain($field_email['1']))){

      
            $get = array(

                  "field"    => "email",
                  "value"    => "$email",
                  "message"    => "VALIDATE SUCESSED!!!",
                  "status"    => "1"
   
                );
   
            $this->get['status'] = "1";
   
            $this->setGet($get);
            $this->showMessage();


            return TRUE;

         }else{

            return FALSE;
            
         }

      }else{
      
         $get = array(

            "field"    => "email",
            "message"    => "EMPTY REQUIRE FIELD",
                "value"     => " ",
            "status"    => "2",


          );

         $this->setGet($get);
         $this->showMessage();
         $this->stop();


                   return FALSE;
      
      }


   }

   
}

$validatemail = new validatemail;

?>

<?  // programa.php

require_once("validatemail.php");
 
 
 /* CONFIGURAÇÃO */
 
 
 $config = array(

       /* Para o script caso de erro 1=Sim 2=Nao */
      "stop_error"       => 2, 
      /* Mostra mensagens 1=Sim 2=Nao */
      "show_message"      => 2,
      /* Usa o programa DIG para validar 1=Sim 2=Nao */
      "enable_dig"      => 1

    );
 
 $validatemail->setConfig($config);
 

 /* EXEC */
 
 $email = "joaopaulo@deoliveira.com.br";
 
 

 
 
 /* RETORNA INFORMACOES VALIDADAS e FORMATADAS*/
 
 
 
 
 
 if($validatemail->chkEmail($email)){
 
    $email =  $validatemail->inf['email'];
    $dominio =  $validatemail->inf['dominio'];
    $usuario =  $validatemail->inf['usuario'];
   $mensagem =  $validatemail->get['message'];
   
 
   echo "OK---------- $mensagem<br>";
    echo "DOMINIO: $dominio<br>";
    echo "USUARIO: $usuario<br>";
    echo "EMAIL: $email<br>";
 
 }else{
 
    $email =  $validatemail->inf['email'];
    $dominio =  $validatemail->inf['dominio'];
    $usuario =  $validatemail->inf['usuario'];
   $mensagem =  $validatemail->get['message'];
 
   echo "ERROR---------- $mensagem<br>"
    echo "DOMINIO: $dominio<br>";
    echo "USUARIO: $usuario<br>";
    echo "EMAIL: $email<br>";
 
    echo "<pre>";
    print_r($validatemail->get);
   echo "</pre>";
    
   
 }
 
 
 
 ?>

Scripts recomendados

Cria gráficos para monitoramento de consumo de CPU e Memória a partir de arquivos [Atualização].

Backup de arquivos na Cloud com AWS Amazon S3 e PHP

Monitoramento simples de servidores [versão 1.0]

Mostra a quantidade dos dias da semana em intervalo

Cron - Classe PHP para cálculo da diferença entre duas datas


  

Comentários

Nenhum comentário foi encontrado.


Contribuir com comentário




Patrocínio

Site hospedado pelo provedor RedeHost.
Linux banner

Destaques

Artigos

Dicas

Tópicos

Top 10 do mês

Scripts