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

Revision 4414, 13.4 KB checked in by airton, 13 years ago (diff)

Ticket #1887 - Redefinicao do parser de email - Adicionando bibliotecas e arquivos

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