source: sandbox/2.2.0.2/expressoMail1_2/inc/class.attachment.inc.php @ 4436

Revision 4436, 15.5 KB checked in by airton, 13 years ago (diff)

Ticket #1887 - Redefinicao do parser de email - Adequacao ao phpdoc

  • 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                            $info['pid'] = $currentIndex;
245                            $info['name'] = $part->d_parameters['filename'];
246                            $info['encoding'] = $part->headers['content-transfer-encoding'];
247                            $info['fsize'] = mb_strlen($part->body, $part->headers['content-transfer-encoding']);
248                            break;
249                        }
250                    }
251                }
252            }
253        }else if($soughtIndex == '0')
254        {
255            $info['pid'] = '0';
256            $info['name'] = $structure->d_parameters['filename'];
257            $info['encoding'] = $structure->headers['content-transfer-encoding'];
258            $info['fsize'] = mb_strlen($structure->body, $structure->headers['content-transfer-encoding']);
259        }
260    }
261
262
263
264     /**
265     * Write in $attachments, array with the information of attachments
266     * @param <type> $structure
267     * @param <type> $attachments
268     */
269    private function _getAttachmentsInfo($structure, &$attachments, $pIndex = '0')
270    {
271
272        if($structure->parts)
273        {
274            foreach ($structure->parts  as $index => $part)
275            {
276                if(strtolower($part->ctype_primary) == 'multipart')
277                    $this->_getAttachmentsInfo($part,$attachments,$pIndex.'.'.$index);
278                else
279                {
280                    if(strtolower($part->ctype_primary) == 'message' && is_array($part->parts))
281                       $this->_getAttachmentsInfo($part,$attachments,$pIndex.'.'.$index);
282                    else
283                    {
284                        if($part->d_parameters['filename'])
285                        {
286                            $definition['pid'] = $pIndex.'.'.$index;
287                            $definition['name'] = $part->d_parameters['filename'];
288                            $definition['encoding'] = $part->headers['content-transfer-encoding'];
289                            $definition['fsize'] = mb_strlen($part->body, $part->headers['content-transfer-encoding']);
290
291                            array_push($attachments, $definition);
292                        }
293                        else if($part->ctype_parameters['name'])
294                        {
295                            $definition['pid'] = $pIndex.'.'.$index;
296                            $definition['name'] = $part->ctype_parameters['name'];
297                            $definition['encoding'] = $part->headers['content-transfer-encoding'];
298                            $definition['fsize'] = mb_strlen($part->body, $part->headers['content-transfer-encoding']);
299
300                            array_push($attachments, $definition);
301                        }
302                        else if(strtolower($part->ctype_primary) == 'text' &&  strtolower($part->ctype_secondary) == 'calendar')
303                        {
304                            $definition['pid'] = $pIndex.'.'.$index;
305                            $definition['name'] = 'calendar.ics';
306                            $definition['encoding'] = $part->headers['content-transfer-encoding'];
307                            $definition['fsize'] = mb_strlen($part->body, $part->headers['content-transfer-encoding']);
308
309                            array_push($attachments, $definition);
310                        }
311                     
312                    }
313                }
314            }
315        }
316        else
317        {
318            if($structure->d_parameters['filename'])
319            {
320                $definition['pid'] = '0';
321                $definition['name'] = $structure->d_parameters['filename'];
322                $definition['encoding'] = $structure->headers['content-transfer-encoding'];
323                $definition['fsize'] = mb_strlen($structure->body, $structure->headers['content-transfer-encoding']);
324
325                array_push($attachments, $definition);
326            }
327            else if($structure->ctype_parameters['name'])
328            {
329                $definition['pid'] = '0';
330                $definition['name'] = $structure->ctype_parameters['name'];
331                $definition['encoding'] = $structure->headers['content-transfer-encoding'];
332                $definition['fsize'] = mb_strlen($structure->body, $structure->headers['content-transfer-encoding']);
333
334                array_push($attachments, $definition);
335            }
336            else if(strtolower($structure->ctype_primary) == 'text' &&  strtolower($structure->ctype_secondary) == 'calendar')
337            {
338                $definition['pid'] = '0';
339                $definition['name'] = 'calendar.ics';
340                $definition['encoding'] = $structure->headers['content-transfer-encoding'];
341                $definition['fsize'] = mb_strlen($structure->body, $structure->headers['content-transfer-encoding']);
342
343                array_push($attachments, $definition);
344             }
345        }
346
347    }
348
349    /**
350     * Write in $images, array with the information of Embedded Images
351     * @param <type> $structure
352     * @param <type> $attachments
353     */
354     private function _getEmbeddedImagesInfo($structure, &$images, $pIndex = '0')
355     {
356        foreach ($structure->parts  as $index => $part)
357        {
358            if(strtolower($part->ctype_primary) == 'multipart')
359                    $this->_getEmbeddedImagesInfo($part,$images,$pIndex.'.'.$index);
360            else
361            {
362                if(strtolower($part->ctype_primary) == 'message' && is_array($part->parts))
363                        $this->_getEmbeddedImagesInfo($part,$images,$pIndex.'.'.$index);
364                else
365                {
366                    if(!$part->ctype_parameters['name'])
367                         $name = $part->d_parameters['filename'];
368                    else
369                         $name = $part->ctype_parameters['name'];
370
371                    $type =  strtolower(substr($name, -4));
372                    $ctype = strtolower($part->ctype_primary.'/'.$part->ctype_secondary);
373                    if(strtolower($part->ctype_primary) == 'image' ||  ($ctype == 'application/octet-stream' && ($type == '.png' || $type == '.jpg' || $type == '.gif')))
374                    {
375                        $definition['pid'] = $pIndex.'.'.$index;
376                        $definition['name'] = $name;
377                        $definition['type'] = 'image/'.strtolower($part->ctype_secondary);
378                        $definition['encoding'] = $part->headers['content-transfer-encoding'];
379                        $definition['cid'] = $part->headers['content-id'];
380                        array_push($images, $definition);
381                    }
382                }
383            }
384        }
385    }
386
387}
388
389?>
Note: See TracBrowser for help on using the repository browser.