Ignore:
Timestamp:
11/03/11 13:24:28 (12 years ago)
Author:
wmerlotto
Message:

Ticket #2305 - Enviando alteracoes, desenvolvidas internamente na Prognus, do modulo ExpressoMail?.

File:
1 edited

Legend:

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

    r5079 r5134  
    3232     * @param integer $msgNumber Imap menssagem number 
    3333     */ 
    34     function attachment($rootPath = false) 
     34    function attachment() 
    3535    { 
    3636        /* 
    3737         * Requires 
    3838         */ 
    39         if($rootPath) 
    40         { 
    41             require_once $rootPath.'/library/mime/mimePart.php'; 
    42             require_once $rootPath.'/library/mime/mimeDecode.php'; 
    43         } 
    44         else 
    45         { 
    4639            require_once $_SESSION['rootPath'].'/library/mime/mimePart.php'; 
    4740            require_once $_SESSION['rootPath'].'/library/mime/mimeDecode.php'; 
    48         } 
    4941        //----------------------------------------------------------// 
    5042 
     
    5244        $this->decodeConf['decode_bodies']  = true; 
    5345        $this->decodeConf['decode_headers'] = true; 
     46         
     47        if( array_key_exists('nested_messages_are_shown', $_SESSION['phpgw_info']['user']['preferences']['expressoMail']) && ($_SESSION['phpgw_info']['user']['preferences']['expressoMail']['nested_messages_are_shown'] == '1')) 
     48            $this->decodeConf['rfc_822bodies']  = true; 
    5449    } 
    5550    //----------------------------------------------------------------------------// 
     
    8681        $this->structure = $decoder->decode($this->decodeConf); 
    8782 
     83                //TODO: Descartar código após atualização do módulo de segurança da SERPRO  
     84                if($this->isSignedMenssage())  
     85                        $this->convertSignedMenssage($rawMessageData);  
     86                ////////////////////////////////////////////////////// 
     87                         
     88                         
    8889        /* 
    8990         * Clean memory and close imap connection 
     
    9596    } 
    9697 
     98        //TODO: Descartar código após atualização do módulo de segurança da SERPRO  
     99        private function isSignedMenssage()  
     100        {  
     101                if(strtolower($this->structure->ctype_primary) == 'application' && strtolower($this->structure->ctype_secondary) == 'x-pkcs7-mime' )  
     102                        return true;  
     103                else  
     104                        return false;       
     105        }  
     106    ////////////////////////////////////////////////////////////////////////// 
     107 
     108   //TODO: Descartar código após atualização do módulo de segurança da SERPRO  
     109   private function convertSignedMenssage($rawMessageData)  
     110        {  
     111                $decoder = new Mail_mimeDecode($this->extractSignedContents($rawMessageData));  
     112                $this->structure = $decoder->decode($this->decodeConf);           
     113        }  
     114        /////////////////////////////////////////////////////////////////////////// 
     115         
     116        //TODO: Descartar código após atualização do módulo de segurança da SERPRO  
     117        private function extractSignedContents( $data )  
     118                    {  
     119                        $pipes_desc = array(  
     120                            0 => array('pipe', 'r'),  
     121                            1 => array('pipe', 'w')  
     122                        );  
     123                  
     124                        $fp = proc_open( 'openssl smime -verify -noverify -nochain', $pipes_desc, $pipes);  
     125                        if (!is_resource($fp)) {  
     126                           return false;  
     127                        }  
     128                  
     129                        $output = '';  
     130                  
     131                        /* $pipes[0] => writeable handle connected to child stdin  
     132                           $pipes[1] => readable handle connected to child stdout */  
     133                        fwrite($pipes[0], $data);  
     134                        fclose($pipes[0]);  
     135                  
     136                        while (!feof($pipes[1])) {  
     137                            $output .= fgets($pipes[1], 1024);  
     138                        }  
     139                        fclose($pipes[1]);  
     140                        proc_close($fp);  
     141                  
     142                        return $output;  
     143                    }  
     144        ///////////////////////////////////////////////////////////////////////////////////// 
     145         
    97146    /** 
    98147     * Set Stucture from Mail_mimeDecode Structure 
     
    116165        $decoder = new Mail_mimeDecode($rawMail); 
    117166        $this->structure = $decoder->decode($this->decodeConf); 
     167                 
     168                //TODO: Descartar código após atualização do módulo de segurança da SERPRO  
     169                if($this->isSignedMenssage())  
     170                        $this->convertSignedMenssage($rawMessageData);   
     171                ////////////////////////////////////////////////////////////////////////// 
    118172    } 
    119173 
     
    169223        $attachment = array(); 
    170224        $this->_getPartInfo($this->structure,$partNumber,$attachment); 
    171         return $attachment; 
     225        return $attachment[0]; 
    172226    } 
    173227 
     
    180234    private function _getRaw() 
    181235    { 
    182         return imap_fetchheader($this->mbox, $this->msgNumber, FT_UID).imap_body($this->mbox, $this->msgNumber, FT_UID); 
     236                        /*  
     237                 * Chamada original imap_fetchheader($this->mbox, $this->msgNumber, FT_UID)."\r\n".imap_body($this->mbox, $this->msgNumber, FT_UID);  
     238                 * Inserido replace para corrigir um bug que acontece raramente em mensagens vindas do outlook com muitos destinatarios  
     239                 */  
     240                return str_replace("\r\n\t", '', imap_fetchheader($this->mbox, $this->msgNumber, FT_UID))."\r\n".imap_body($this->mbox, $this->msgNumber, FT_UID); 
    183241    } 
    184242 
     
    202260                else 
    203261                { 
    204                     if(strtolower($part->ctype_primary) == 'message' && is_array($part->parts)) 
     262                    if(strtolower($part->ctype_primary) == 'message' && is_array($part->parts) && $this->decodeConf['rfc_822bodies'] !== true ) 
    205263                            $this->_getPartContent($part,$soughtIndex,$body,$pIndex.'.'.$index); 
    206264                    else 
    207265                    { 
    208266                        $currentIndex = $pIndex.'.'.$index; 
    209                         if($currentIndex == $soughtIndex) 
     267                        if($currentIndex === $soughtIndex) 
    210268                        { 
    211269                            $body = $part->body; 
     
    235293                else 
    236294                { 
    237                     if(strtolower($part->ctype_primary) == 'message' && is_array($part->parts)) 
     295                    if(strtolower($part->ctype_primary) == 'message' && is_array($part->parts) && $this->decodeConf['rfc_822bodies'] !== true) 
    238296                         $this->_getPartInfo($part,$soughtIndex,$info,$pIndex.'.'.$index); 
    239297                    else 
    240298                    { 
    241299                        $currentIndex = $pIndex.'.'.$index; 
    242                         if($currentIndex == $soughtIndex) 
     300                        if($currentIndex === $soughtIndex) 
    243301                        { 
    244302                                                        $this->_pushAttachmentsInfo($part,$info,$currentIndex); 
     
    262320    { 
    263321 
    264         if($structure->parts) 
     322        if(isset($structure->parts)) 
    265323        { 
    266324            foreach ($structure->parts  as $index => $part) 
     
    270328                else 
    271329                { 
    272                     if(strtolower($part->ctype_primary) == 'message' && is_array($part->parts)) 
     330                    if(strtolower($part->ctype_primary) == 'message' && is_array($part->parts) && $this->decodeConf['rfc_822bodies'] !== true) 
    273331                       $this->_getAttachmentsInfo($part,$attachments,$pIndex.'.'.$index); 
    274332                    else 
     
    293351        /** 
    294352        * Write in $$attachments, array with the information of attachment  
    295         * 
    296         * @license    http://www.gnu.org/copyleft/gpl.html GPL 
    297         * @author     Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) 
    298         * @sponsor    Caixa Econômica Federal 
    299         * @param      $structure 
    300         * @param      $attachments 
    301         * @param      $pIndex 
     353        * @param <type> $structure  
     354        * @param <type> $attachments  
     355        * @param <type> $pIndex  
    302356        */       
    303357        private function _pushAttachmentsInfo(&$structure, &$attachments, $pIndex = '0')  
    304358        {  
    305                         $name = false;  
    306                         if($structure->d_parameters['filename']) $name = $structure->d_parameters['filename'];  
    307                         else if($structure->ctype_parameters['name']) $name = $structure->ctype_parameters['name'];  
     359                        $name = '';                                 
     360                        if((isset($structure->d_parameters['filename']))&& ($structure->d_parameters['filename'])) $name = $structure->d_parameters['filename'];  
     361                        else if((isset($structure->ctype_parameters['name']))&&($structure->ctype_parameters['name'])) $name = $structure->ctype_parameters['name']; 
    308362                        else if(strtolower($structure->ctype_primary) == 'text' &&  strtolower($structure->ctype_secondary) == 'calendar') $name = 'calendar.ics';  
    309363                  
     364                            //Attachments com nomes grandes são quebrados em varias partes VER RFC2231 
     365                            if( !$name && isset($structure->disposition) && (strtolower($structure->disposition) === 'attachment' || strtolower($structure->ctype_primary) == 'image')) 
     366                                        foreach ($structure->d_parameters as $i => $v) 
     367                                                if(strpos($i , 'filename') !== false) 
     368                                                        $name .= urldecode(str_ireplace(array('ISO-8859-1','UTF-8','US-ASCII'),'',$v)); 
     369                             if( !$name && isset($structure->disposition) && (strtolower($structure->disposition) === 'attachment' || strtolower($structure->ctype_primary) == 'image') ) 
     370                                        foreach ($structure->ctype_parameters as $i => $v) 
     371                                                if(strpos($i , 'name') !== false) 
     372                                                        $name .= urldecode(str_ireplace(array('ISO-8859-1','UTF-8','US-ASCII'),'',$v));          
     373                           //////////////////////////////////////////////////////////////////////////////////// 
     374                                                 
     375 
     376                        if($structure->ctype_primary == 'message') { 
     377                                $attach = new attachment(); 
     378                                $attach->setStructureFromRawMail($structure->body); 
     379 
     380                                if (!$name) 
     381                                        $name = ($attach->structure->headers['subject']) ? $attach->structure->headers['subject'] : 'no title'; 
     382 
     383                                if (!preg_match("/\.eml$/", $name)) 
     384                                        $name .= '.eml'; 
     385                        }                                               
     386 
     387                        if(!$name && strtolower($structure->ctype_primary) == 'image') 
     388                        { 
     389                            if(strlen($structure->ctype_secondary) === 3) 
     390                                $ext = strtolower($structure->ctype_secondary); 
     391 
     392                            $name = 'Embedded-Image.'.$ext; 
     393                        } 
     394                                 
    310395                        if($name)  
    311396                        {  
     
    316401                            $definition['name'] = addslashes(mb_convert_encoding($name, "ISO-8859-1"));  
    317402                            $definition['encoding'] = $structure->headers['content-transfer-encoding'];  
     403                            $definition['type'] = strtolower($structure->ctype_primary).'/'.strtolower($structure->ctype_secondary);                                        
    318404                            $definition['fsize'] = mb_strlen($structure->body, $structure->headers['content-transfer-encoding']);  
    319405                  
     
    334420     private function _getEmbeddedImagesInfo($structure, &$images, $pIndex = '0') 
    335421     { 
     422                if(isset($structure->parts)) 
    336423        foreach ($structure->parts  as $index => $part) 
    337424        { 
     
    340427            else 
    341428            { 
    342                 if(strtolower($part->ctype_primary) == 'message' && is_array($part->parts)) 
     429                if(strtolower($part->ctype_primary) == 'message' && is_array($part->parts) && $this->decodeConf['rfc_822bodies'] !== true) 
    343430                        $this->_getEmbeddedImagesInfo($part,$images,$pIndex.'.'.$index); 
    344431                else 
    345432                { 
    346                     if(!$part->ctype_parameters['name']) 
     433                    if(is_array($part->ctype_parameters) && !array_key_exists('name', $part->ctype_parameters)) 
     434                        if(isset($part->d_parameters['filename'])) 
    347435                         $name = $part->d_parameters['filename']; 
    348436                    else 
     437                          $name = null; 
     438                    else 
    349439                         $name = $part->ctype_parameters['name']; 
    350440 
    351                     $type =  strtolower(substr($name, -4)); 
     441                    if($name && (strlen($name) - (strrpos($name, '.')+1) === 3 ))  
     442                        $ext = strtolower(substr ( $name , (strrpos($name, '.')+1) ));           
     443                    else if(strlen($structure->ctype_secondary) === 3) 
     444                        $ext = strtolower($structure->ctype_secondary); 
     445                     
     446                    if(!$name && strtolower($structure->ctype_primary) == 'image') $name = 'Embedded-Image.'.$ext; 
     447                                         
    352448                    $ctype = strtolower($part->ctype_primary.'/'.$part->ctype_secondary); 
    353                     if(strtolower($part->ctype_primary) == 'image' ||  ($ctype == 'application/octet-stream' && ($type == '.png' || $type == '.jpg' || $type == '.gif'))) 
     449         
     450                    if(strtolower($part->ctype_primary) == 'image' ||  ($ctype == 'application/octet-stream' && ($ext == 'png' || $ext == 'jpg' || $ext == 'gif' || $ext == 'bmp' || $ext == 'tif'))) 
    354451                    { 
    355452                        $definition['pid'] = $pIndex.'.'.$index; 
    356453                        $definition['name'] = addslashes($name); 
    357                         $definition['type'] = 'image/'.strtolower($part->ctype_secondary); 
     454                        $definition['type'] = $ctype; 
    358455                        $definition['encoding'] = $part->headers['content-transfer-encoding']; 
    359456                        $definition['cid'] = $part->headers['content-id']; 
Note: See TracChangeset for help on using the changeset viewer.