source: trunk/expressoMail1_2/inc/class.attachment.inc.php @ 4774

Revision 4774, 13.8 KB checked in by airton, 13 years ago (diff)

Ticket #2131 - Problema de codificacao de caracteres na visualizacao de nome de anexos no Expresso

  • Property svn:executable set to *
Line 
1<?php
2/**
3* Classe que manipula e gerencia toda a parte de anexos.
4*
5* @package    ExpressoMail
6* @license    http://www.gnu.org/copyleft/gpl.html GPL
7* @author     Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br)
8* @author     Cristiano Correa
9*/
10
11class attachment
12{
13    /*
14     * Globals variables
15     */
16    var $mbox;
17    var $imap_port;
18    var $has_cid;
19    var $imap_options;
20    var $imap_sentfolder;
21    var $msgNumber;
22    var $folder;
23    var $structure;
24    var $decodeConf;
25    //------------------------------------------//
26
27    /**
28     * Constructor
29         * @license   http://www.gnu.org/copyleft/gpl.html GPL
30         * @author    Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br)
31     * @param string $folder Imap folder name
32     * @param integer $msgNumber Imap menssagem number
33     */
34    function attachment($rootPath = false)
35    {
36        /*
37         * Requires
38         */
39        if($rootPath)
40        {
41            require_once $rootPath.'/library/mime/mimePart.php';
42            require_once $rootPath.'/library/mime/mimeDecode.php';
43        }
44        else
45        {
46            require_once $_SESSION['rootPath'].'/library/mime/mimePart.php';
47            require_once $_SESSION['rootPath'].'/library/mime/mimeDecode.php';
48        }
49        //----------------------------------------------------------//
50
51        $this->decodeConf['include_bodies'] = true;
52        $this->decodeConf['decode_bodies']  = true;
53        $this->decodeConf['decode_headers'] = true;
54    }
55    //----------------------------------------------------------------------------//
56
57
58
59    /**
60     * Open mail from Imap and parse structure
61     * @license   http://www.gnu.org/copyleft/gpl.html GPL
62         * @author    Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br)
63         * @return string  menssagem
64     */
65    public function setStructureFromMail($folder,$msgNumber)
66    {
67        $this->folder       = mb_convert_encoding($folder, 'UTF7-IMAP',mb_detect_encoding($folder.'x', 'UTF-8, ISO-8859-1'));
68        $this->msgNumber    = $msgNumber;
69        $this->username     = $_SESSION['phpgw_info']['expressomail']['user']['userid'];
70        $this->password     = $_SESSION['phpgw_info']['expressomail']['user']['passwd'];
71        $this->imap_server  = $_SESSION['phpgw_info']['expressomail']['email_server']['imapServer'];
72        $this->imap_port    = $_SESSION['phpgw_info']['expressomail']['email_server']['imapPort'];
73        $this->imap_delimiter = $_SESSION['phpgw_info']['expressomail']['email_server']['imapDelimiter'];
74        $this->imap_sentfolder = $_SESSION['phpgw_info']['expressomail']['email_server']['imapDefaultSentFolder']   ? $_SESSION['phpgw_info']['expressomail']['email_server']['imapDefaultSentFolder']   : str_replace("*","", $this->functions->getLang("Sent"));
75        $this->has_cid = false;
76
77        if ($_SESSION['phpgw_info']['expressomail']['email_server']['imapTLSEncryption'] == 'yes')
78            $this->imap_options = '/tls/novalidate-cert';
79        else
80            $this->imap_options = '/notls/novalidate-cert';
81
82        $this->mbox = @imap_open("{".$this->imap_server.":".$this->imap_port.$this->imap_options."}".$this->folder , $this->username, $this->password) or die('Error');
83
84        $rawMessageData = $this->_getRaw();
85        $decoder = new Mail_mimeDecode($rawMessageData);
86        $this->structure = $decoder->decode($this->decodeConf);
87
88        /*
89         * Clean memory and close imap connection
90         */
91        $rawMessageData = null;
92        $decoder = null;
93        @imap_close($this->mbox);
94        //-----------------------------------------//
95    }
96
97    /**
98     * Set Stucture from Mail_mimeDecode Structure
99     * @license   http://www.gnu.org/copyleft/gpl.html GPL
100         * @author    Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br)
101         * @param Mail_mimeDecode $structure
102     */
103    public function setStructure($structure)
104    {
105          $this->structure = $structure;
106    }
107
108    /**
109     *  Set Stucture from raw mail code
110         * @license   http://www.gnu.org/copyleft/gpl.html GPL
111         * @author    Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br)
112     * @param Mail_mimeDecode $structure
113     */
114    public function setStructureFromRawMail($rawMail)
115    {
116        $decoder = new Mail_mimeDecode($rawMail);
117        $this->structure = $decoder->decode($this->decodeConf);
118    }
119
120    /**
121     * Returns Attachment Decoded
122     * @license   http://www.gnu.org/copyleft/gpl.html GPL
123         * @author    Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br)
124         * @param string $partNumber Index part
125     * @return string Attachment Decoded
126     */
127    public function getAttachment($partNumber)
128    {
129       $partContent = '';
130       $this->_getPartContent($this->structure, $partNumber, $partContent);
131       return $partContent;
132    }
133
134     /**
135     * Returns EmbeddedImages Infos
136     * @license   http://www.gnu.org/copyleft/gpl.html GPL
137         * @author    Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br)
138         * @return array EmbeddedImages
139     */
140    public function getEmbeddedImagesInfo()
141    {
142        $imagesEmbedded = array();
143        $this->_getEmbeddedImagesInfo($this->structure,$imagesEmbedded);
144        return $imagesEmbedded;
145    }
146
147    /**
148     * Returns Attachments Infos
149     * @license   http://www.gnu.org/copyleft/gpl.html GPL
150         * @author    Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br)
151         * @return array
152     */
153    public function getAttachmentsInfo()
154    {
155        $attachments = array();
156        $this->_getAttachmentsInfo($this->structure,$attachments);
157        return $attachments;
158    }
159
160    /**
161     * Returns Attachment Info
162     * @license   http://www.gnu.org/copyleft/gpl.html GPL
163         * @author    Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br)
164         * @param string $partNumber Index part
165     * @return array
166     */
167    public function getAttachmentInfo($partNumber)
168    {
169        $attachment = array();
170        $this->_getPartInfo($this->structure,$partNumber,$attachment);
171        return $attachment;
172    }
173
174    /**
175     * returns the source code menssagem
176     * @license   http://www.gnu.org/copyleft/gpl.html GPL
177         * @author    Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br)
178         * @return string  menssagem
179     */
180    private function _getRaw()
181    {
182        return imap_fetchheader($this->mbox, $this->msgNumber, FT_UID).imap_body($this->mbox, $this->msgNumber, FT_UID);
183    }
184
185    /**
186     * Returns content from the searched
187     * @license   http://www.gnu.org/copyleft/gpl.html GPL
188         * @author    Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br)
189         * @param pointer $structure Structure object
190     * @param string $soughtIndex
191     * @param pointer $body Content
192     * @param string $pIndex
193     */
194    private function _getPartContent(&$structure, $soughtIndex,&$body,$pIndex = '0')
195    {
196        if($structure->parts)
197        {
198            foreach ($structure->parts  as $index => $part)
199            {
200                if(strtolower($part->ctype_primary) == 'multipart')
201                        $this->_getPartContent($part,$soughtIndex,$body,$pIndex.'.'.$index);
202                else
203                {
204                    if(strtolower($part->ctype_primary) == 'message' && is_array($part->parts))
205                            $this->_getPartContent($part,$soughtIndex,$body,$pIndex.'.'.$index);
206                    else
207                    {
208                        $currentIndex = $pIndex.'.'.$index;
209                        if($currentIndex == $soughtIndex)
210                        {
211                            $body = $part->body;
212                            break;
213                        }
214                    }
215                }
216            }
217        }
218        else if($soughtIndex == '0')
219           $body = $structure->body;
220    }
221
222        /**
223     * @license   http://www.gnu.org/copyleft/gpl.html GPL
224         * @author    Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br)
225     */
226    private function _getPartInfo(&$structure, $soughtIndex,&$info,$pIndex = '0')
227    {
228
229        if($structure->parts)
230        {
231            foreach ($structure->parts  as $index => $part)
232            {
233                if(strtolower($part->ctype_primary) == 'multipart')
234                    $this->_getPartInfo($part,$soughtIndex,$info,$pIndex.'.'.$index);
235                else
236                {
237                    if(strtolower($part->ctype_primary) == 'message' && is_array($part->parts))
238                         $this->_getPartInfo($part,$soughtIndex,$info,$pIndex.'.'.$index);
239                    else
240                    {
241                        $currentIndex = $pIndex.'.'.$index;
242                        if($currentIndex == $soughtIndex)
243                        {
244                                                        $this->_pushAttachmentsInfo($part,$info,$currentIndex);
245                            break;
246                        }
247                    }
248                }
249            }
250        }else if($soughtIndex == '0')
251                        $this->_pushAttachmentsInfo($structure,$info);
252    }
253
254
255
256     /**
257     * Write in $attachments, array with the information of attachments
258     * @param <type> $structure
259     * @param <type> $attachments
260     */
261    private function _getAttachmentsInfo($structure, &$attachments, $pIndex = '0')
262    {
263
264        if($structure->parts)
265        {
266            foreach ($structure->parts  as $index => $part)
267            {
268                if(strtolower($part->ctype_primary) == 'multipart')
269                    $this->_getAttachmentsInfo($part,$attachments,$pIndex.'.'.$index);
270                else
271                {
272                    if(strtolower($part->ctype_primary) == 'message' && is_array($part->parts))
273                       $this->_getAttachmentsInfo($part,$attachments,$pIndex.'.'.$index);
274                    else
275                    {
276                        if(!$part->headers['content-transfer-encoding']) //Caso não esteja especificado a codificação
277                                    $part->headers['content-transfer-encoding'] = mb_detect_encoding ($part->body,array('BASE64','Quoted-Printable','7bit','8bit','ASCII'));
278                                                if($part->headers['content-transfer-encoding'] === ('ASCII' || false)) //Caso a codificação retorne ascii ou false especifica como base64
279                                                        $part->headers['content-transfer-encoding'] = 'base64';
280                                               
281                                                        $this->_pushAttachmentsInfo($part,$attachments,$pIndex.'.'.$index);
282                     
283                    }
284                }
285            }
286        }
287        else
288                        $this->_pushAttachmentsInfo($structure,$attachments);
289
290    }
291
292       
293        /**
294        * Write in $$attachments, array with the information of attachment
295        * @param <type> $structure
296        * @param <type> $attachments
297        * @param <type> $pIndex
298        */
299        private function _pushAttachmentsInfo(&$structure, &$attachments, $pIndex = '0')
300        {
301                        $name = false;
302                        if($structure->d_parameters['filename']) $name = $structure->d_parameters['filename'];
303                        else if($structure->ctype_parameters['name']) $name = $structure->ctype_parameters['name'];
304                        else if(strtolower($structure->ctype_primary) == 'text' &&  strtolower($structure->ctype_secondary) == 'calendar') $name = 'calendar.ics';
305                 
306                        if($name)
307                        {
308                            $codificao =  mb_detect_encoding($name.'x', 'UTF-8, ISO-8859-1');
309                            if($codificao == 'UTF-8') $name = utf8_decode($name);
310                             
311                            $definition['pid'] = $pIndex;
312                            $definition['name'] = addslashes(mb_convert_encoding($name, "ISO-8859-1"));
313                            $definition['encoding'] = $structure->headers['content-transfer-encoding'];
314                            $definition['fsize'] = mb_strlen($structure->body, $structure->headers['content-transfer-encoding']);
315                 
316                            array_push($attachments, $definition);
317                        }     
318        }
319       
320       
321       
322       
323       
324       
325    /**
326     * Write in $images, array with the information of Embedded Images
327     * @param <type> $structure
328     * @param <type> $attachments
329     */
330     private function _getEmbeddedImagesInfo($structure, &$images, $pIndex = '0')
331     {
332        foreach ($structure->parts  as $index => $part)
333        {
334            if(strtolower($part->ctype_primary) == 'multipart')
335                    $this->_getEmbeddedImagesInfo($part,$images,$pIndex.'.'.$index);
336            else
337            {
338                if(strtolower($part->ctype_primary) == 'message' && is_array($part->parts))
339                        $this->_getEmbeddedImagesInfo($part,$images,$pIndex.'.'.$index);
340                else
341                {
342                    if(!$part->ctype_parameters['name'])
343                         $name = $part->d_parameters['filename'];
344                    else
345                         $name = $part->ctype_parameters['name'];
346
347                    $type =  strtolower(substr($name, -4));
348                    $ctype = strtolower($part->ctype_primary.'/'.$part->ctype_secondary);
349                    if(strtolower($part->ctype_primary) == 'image' ||  ($ctype == 'application/octet-stream' && ($type == '.png' || $type == '.jpg' || $type == '.gif')))
350                    {
351                        $definition['pid'] = $pIndex.'.'.$index;
352                        $definition['name'] = addslashes($name);
353                        $definition['type'] = 'image/'.strtolower($part->ctype_secondary);
354                        $definition['encoding'] = $part->headers['content-transfer-encoding'];
355                        $definition['cid'] = $part->headers['content-id'];
356                        array_push($images, $definition);
357                    }
358                }
359            }
360        }
361    }
362
363}
364
365?>
Note: See TracBrowser for help on using the repository browser.