Ignore:
Timestamp:
06/20/09 01:07:18 (15 years ago)
Author:
rafaelraymundo
Message:

Ticket #558 - Adicionada funcionalidade de assinatura e criptografia de e-mails.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/expressoMail1_2/inc/class.phpmailer.php

    r614 r1035  
    101101 
    102102    /** 
     103     * Sets the signed body of the message.  This automatically sets the 
     104     * email to multipart/signed. 
     105     * @var string 
     106     */ 
     107    var $SignedBody           = false; 
     108    var $SMIME                  = false; 
     109    var $Certs_crypt            = array(); 
     110    /** 
     111     * Sets the encrypted body of the message.  This automatically sets the 
     112     * email to multipart/encript. 
     113     * @var string 
     114     */ 
     115    var $CryptedBody           = ""; 
     116 
     117    /** 
    103118     * Sets word wrapping on the body of the message to a given number of  
    104119     * characters. 
     
    148163        var $SaveMessageInFolder = ""; 
    149164        var $SaveMessageAsDraft = ""; 
     165 
     166    var $xMailer           = ""; 
    150167 
    151168    ///////////////////////////////////////////////// 
     
    372389        $this->SetMessageType(); 
    373390        $header .= $this->CreateHeader(); 
    374         $body = $this->CreateBody(); 
    375                  
    376         if($body == "") { return false; } 
     391 
     392        if ($this->SMIME == false) 
     393        { 
     394            $body = $this->CreateBody(); 
     395            if($body == "") 
     396            { 
     397                return false; 
     398            } 
     399        } 
    377400                 
    378401        // Choose the mailer 
    379402        switch($this->Mailer) 
    380403        { 
     404            // Usado para processar o email e retornar para a applet 
     405                case "smime": 
     406                $retorno['body'] = $header.$this->LE.$body; 
     407                $retorno['type'] =  $this->write_message_type(); 
     408                        return $retorno; 
    381409            case "sendmail": 
    382410                $result = $this->SendmailSend($header, $body); 
     
    473501        include_once($this->PluginDir . "class.smtp.php"); 
    474502        $error = ""; 
     503 
    475504        $bad_rcpt = array(); 
    476  
     505        $errorx = ''; 
    477506        if(!$this->SmtpConnect()) 
    478507            return false; 
     508 
     509        if($this->SMIME) 
     510        { 
     511            $header=''; 
     512            $body = $this->Body; 
     513        } 
    479514 
    480515        $smtp_from = ($this->Sender == "") ? $this->From : $this->Sender; 
     
    490525        for($i = 0; $i < count($this->to); $i++) 
    491526        { 
    492             if(!$this->smtp->Recipient($this->to[$i][0])) 
    493                 $bad_rcpt[] = $this->to[$i][0]; 
     527            if($this->valEm($this->to[$i][0])) 
     528            { 
     529                if(!$this->smtp->Recipient($this->to[$i][0]))  $bad_rcpt[] = $this->to[$i][0]; 
     530            } 
     531            else 
     532            { 
     533                $errorx .= $this->to[$i][0] . ', '; 
     534            } 
    494535        } 
    495536        for($i = 0; $i < count($this->cc); $i++) 
    496537        { 
    497             if(!$this->smtp->Recipient($this->cc[$i][0])) 
    498                 $bad_rcpt[] = $this->cc[$i][0]; 
     538            if($this->valEm($this->cc[$i][0])) 
     539            { 
     540                if(!$this->smtp->Recipient($this->cc[$i][0])) $bad_rcpt[] = $this->cc[$i][0]; 
     541            } 
     542                        else 
     543            { 
     544                $errorx .= $this->cc[$i][0] . ', '; 
     545            } 
    499546        } 
    500547        for($i = 0; $i < count($this->bcc); $i++) 
    501548        { 
    502             if(!$this->smtp->Recipient($this->bcc[$i][0])) 
    503                 $bad_rcpt[] = $this->bcc[$i][0]; 
    504         } 
     549            if($this->valEm($this->bcc[$i][0])) 
     550            { 
     551                if(!$this->smtp->Recipient($this->bcc[$i][0])) $bad_rcpt[] = $this->bcc[$i][0]; 
     552            } 
     553                        else 
     554            { 
     555                $errorx .= $this->bcc[$i][0] . ', '; 
     556            } 
     557        } 
     558        if($errorx != '') 
     559                { 
     560                        $error = $errorx; 
     561                        $error = $this->Lang("recipients_failed")  . '  ' . $errorx; 
     562                        $this->SetError($error); 
     563                        $this->smtp->Reset(); 
     564                        return false; 
     565                } 
    505566 
    506567        if(count($bad_rcpt) > 0) // Create error message 
     
    524585            return false; 
    525586        } 
     587 
     588        // Vai verificar se deve cifrar a msg ...... 
     589        if(count($this->Certs_crypt) > 0) 
     590                { 
     591            // Vai cifrar a msg antes de enviar ...... 
     592                        include_once("../seguranca/classes/CertificadoB.php"); 
     593 
     594            $teste1 = array(); 
     595            $aux_cifra1 = $header . $body; 
     596 
     597            // Início relocação dos headers 
     598            // Esta relocação dos headers podem causar problemas. 
     599 
     600            $match = 0; 
     601            $pattern = '/^Disposition\-Notification\-To:.*\n/m'; 
     602            $match = preg_match($pattern, $aux_cifra1, $teste1); 
     603 
     604            if (!empty($match)){ 
     605                $aux_cifra1 = preg_replace($pattern, '', $aux_cifra1, 1); // retira o Disposition-Notification-To 
     606 
     607                $match = 0; 
     608                $teste2 = array(); 
     609                $pattern = '/^MIME\-Version:.*\n/m'; 
     610                $match = preg_match($pattern, $aux_cifra1, $teste2); 
     611                $aux_cifra1 = preg_replace($pattern, $teste1[0].$teste2[0], $aux_cifra1, 1); // Adiciona Disposition-Notification-To logo acima de MIME-Version 
     612 
     613            } 
     614            // Fim relocação dos headers 
     615 
     616           // Vai partir em duas partes a msg.  A primeira parte he a dos headers, e a segunda vai ser criptografada ... 
     617            $pos_content_type = strpos($aux_cifra1,'Content-Type:'); 
     618            $pos_MIME_Version = strpos($aux_cifra1,'MIME-Version: 1.0' . chr(0x0D) . chr(0x0A)); 
     619            $valx_len = 19; 
     620            if($pos_MIME_Version === False) 
     621                    { 
     622                $pos_MIME_Version = strpos($aux_cifra1,'MIME-Version: 1.0' . chr(0x0A)); 
     623                $valx_len = 18; 
     624                    } 
     625 
     626            if($pos_MIME_Version >= $pos_content_type) 
     627            { 
     628                // nao deve enviar a msg..... O header MIME-Version com posicao invalida ...... 
     629                $this->SetError('Formato dos headers da msg estao invalidos.(CD-17) - A'); 
     630                $this->smtp->Reset(); 
     631                return false; 
     632            } 
     633 
     634            $aux_cifra2 = array(); 
     635            $aux_cifra2[] = substr($aux_cifra1,0,$pos_MIME_Version - 1); 
     636            $aux_cifra2[] = substr($aux_cifra1,$pos_MIME_Version + $valx_len); 
     637 
     638               /* 
     639                        // este explode pode ser fonte de problemas ....... 
     640                        $aux_cifra2 = explode('MIME-Version: 1.0' . chr(0x0A), $aux_cifra1); 
     641                        // Pode ocorrer um erro se nao tiver o header MIME-Version ..... 
     642                        if(count($aux_cifra2)  != 2 ) 
     643                                { 
     644                                        $aux_cifra2 = explode('MIME-Version: 1.0' . chr(0x0D) . chr(0x0A), $aux_cifra1); 
     645                                        if(count($aux_cifra2)  != 2 ) 
     646                                                { 
     647                                                        // nao deve enviar a msg..... nao tem o header MIME-Version ...... 
     648                                                        $this->SetError('Formato dos headers da msg estao invalidos.(CD-17) - ' . count($aux_cifra2)); 
     649                                                        $this->smtp->Reset(); 
     650                                                        return false; 
     651                                                } 
     652                                } 
     653                */ 
     654                        $certificado = new certificadoB(); 
     655                        $h = array(); 
     656                        $aux_body = $certificado->encriptar($aux_cifra2[1], $this->Certs_crypt, $h); 
     657                        if(!$aux_body) 
     658            { 
     659                $this->SetError('Ocorreu um erro. A msg nao foi enviada. (CD-18)'); 
     660                $this->smtp->Reset(); 
     661                return false; 
     662            } 
     663                        // salvar sem cifra...... 
     664                        //$smtpSent = $this->smtp->Data($aux_cifra2[0] . $aux_body); 
     665 
     666                        // salva a msg sifrada. neste caso deve ter sido adicionado o certificado do autor da msg...... 
     667                        $header = $aux_cifra2[0]; 
     668                        $body = $aux_body; 
    526669        $smtpSent = $this->smtp->Data($header . $body); 
     670        } 
     671        else 
     672                { 
     673                        $smtpSent = $this->smtp->Data($header . $body); 
     674                } 
     675 
    527676        if(!$smtpSent) 
    528677        { 
     
    563712                                $header = substr($header, 0, $target) . $this->AddrAppend("Bcc", $this->bcc) . substr($header, $target); 
    564713                        } 
    565                  
    566                         $new_header = str_replace("\n", "\r\n", $header); 
    567                         $new_body = str_replace("\n", "\r\n", $body); 
     714            $new_headerx = str_replace(chr(0x0A),chr(0x0D).chr(0x0A), $header); 
     715                        $new_bodyx = str_replace(chr(0x0A),chr(0x0D).chr(0x0A), $body); 
     716                        $new_header = str_replace(chr(0x0D).chr(0x0D).chr(0x0A), chr(0x0D).chr(0x0A),$new_headerx); 
     717                        $new_body = str_replace(chr(0x0D).chr(0x0D).chr(0x0A), chr(0x0D).chr(0x0A), $new_bodyx); 
    568718                         
    569719                        if ($this->SaveMessageAsDraft){ 
     
    576726         
    577727        return $smtpSent; 
     728    } 
     729 
     730    function valEm($email) 
     731    { 
     732        $mail_retorno = FALSE; 
     733        if ((strlen($email) >= 6) && (substr_count($email,"@") == 1) && (substr($email,0,1) != "@") && (substr($email,strlen($email)-1,1) != "@")) 
     734        { 
     735            if ((!strstr($email,"'")) && (!strstr($email,"\"")) && (!strstr($email,"\\")) && (!strstr($email,"\$")) && (!strstr($email," "))) 
     736            { 
     737                //testa se tem caracter . 
     738                if (substr_count($email,".")>= 1) 
     739                { 
     740                    //obtem a terminação do dominio 
     741                    $term_dom = substr(strrchr ($email, '.'),1); 
     742                    //verifica se terminação do dominio esta correcta 
     743                    if (strlen($term_dom)>1 && strlen($term_dom)<5 && (!strstr($term_dom,"@")) ) 
     744                    { 
     745                        $antes_dom = substr($email,0,strlen($email) - strlen($term_dom) - 1); 
     746                        $caracter_ult = substr($antes_dom,strlen($antes_dom)-1,1); 
     747                        if ($caracter_ult != "@" && $caracter_ult != ".") 
     748                        { 
     749                            $mail_retorno = TRUE; 
     750                        } 
     751                    } 
     752                } 
     753            } 
     754        } 
     755        return $mail_retorno; 
    578756    } 
    579757 
     
    8851063        $result .= $this->HeaderLine("MIME-Version", "1.0"); 
    8861064 
     1065        $result .= $this->write_message_type(); 
     1066 
     1067        if($this->Mailer != "mail") 
     1068            $result .= $this->LE.$this->LE; 
     1069 
     1070        return $result; 
     1071    } 
     1072 
     1073 
     1074    function write_message_type() 
     1075        { 
     1076                $result = ""; 
    8871077        switch($this->message_type) 
    8881078        { 
     
    8911081                $result .= sprintf("Content-Type: %s; charset=\"%s\"", 
    8921082                                    $this->ContentType, $this->CharSet); 
    893                 break; 
     1083                return $result; 
    8941084            case "attachments": 
    8951085                // fall through 
     
    9061096                    $result .= $this->TextLine("\tboundary=\"" . $this->boundary[1] . '"'); 
    9071097                } 
    908                 break; 
     1098                return $result; 
    9091099            case "alt": 
    9101100                $result .= $this->HeaderLine("Content-Type", "multipart/alternative;"); 
    9111101                $result .= $this->TextLine("\tboundary=\"" . $this->boundary[1] . '"'); 
    912                 break; 
     1102                return $result; 
    9131103        } 
    9141104 
Note: See TracChangeset for help on using the changeset viewer.