source: trunk/expressoMail1_2/inc/class.message_components.inc.php @ 2727

Revision 2727, 14.0 KB checked in by niltonneto, 14 years ago (diff)

Ticket #1065 - Corrigido problema ao visualizar mensagem 'multipart/report'.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
Line 
1<?php
2
3/***************************************************************************
4        * Expresso Livre                                                           *
5        * http://www.expressolivre.org                                             *
6        * --------------------------------------------                             *
7        *  This program is free software; you can redistribute it and/or modify it *
8        *  under the terms of the GNU General Public License as published by the   *
9        *  Free Software Foundation; either version 2 of the License, or (at your  *
10        *  option) any later version.                                              *
11        \**************************************************************************/
12    class message_components {
13
14        /**
15         *+----------------------------------------------------------------------------------------------------+
16         *| IMAP message scanner - scans information provided by imap_fetchstructure()                         |
17         *|                                                                                                    |
18         *| Author: Richard York                                                                               |
19         *| mailto:richy at smilingsouls.net                                                                    |
20         *| http://www.smilingsouls.net                                                                        |
21         *|                                                                                                    |
22         *| (c) Copyright 2004, Richard York, All Rights Reseverd                                              |
23         *+----------------------------------------------------------------------------------------------------+
24         **
25        */
26
27        var $mailbox;           // (resource)              Imap stream
28
29        var $data_types;        // (array)(string)         Various message part types
30        var $encoding_types;    // (array)(string)         Various encoding types
31
32        // first array uses message id as key
33        // nested array is offset corresponding with the number of parts
34       
35        var $structure;         // (array)(object)         Contains the complete body structure
36        var $pid;               // (array)(array)(str)     part id
37        var $file_type;         // (array)(array)(str)     mime type
38        var $disposition;       // (array)(array)(str)     inline | attachment
39        var $fsize;             // (array)(array)(int)     part size in bytes
40        var $encoding;          // (array)(array)(str)     message encoding
41        var $charset;           // (array)(array)(int)     message charset
42        var $fname;             // (array)(array)(str)     original file name
43        var $inline_id;         // (array)(array)(str)     string containing the id for multipart/related
44        var $has_attachments;   // (array)(array)(bool)
45
46        /**
47         * CONSTRUCTOR
48         *
49         * void message_components(resource imap stream)
50         **
51        */
52       
53        function message_components($mailbox)
54        {
55            $this->data_types = array();
56
57                $this->data_types[0] = 'text';
58                $this->data_types[1] = 'multipart';
59                $this->data_types[2] = 'message';
60                $this->data_types[3] = 'application';
61                $this->data_types[4] = 'audio';
62                $this->data_types[5] = 'image';
63                $this->data_types[6] = 'video';
64                $this->data_types[7] = 'other';
65
66            $this->encoding_types = array();
67
68                $this->encoding_types[0] = '7bit';
69                $this->encoding_types[1] = '8bit';
70                $this->encoding_types[2] = 'binary';
71                $this->encoding_types[3] = 'base64';
72                $this->encoding_types[4] = 'quoted-printable';
73                $this->encoding_types[5] = 'other';
74
75            $this->mailbox = $mailbox;
76
77            return;
78        }
79       
80        /**
81         * void fetch_structure(int message id[, array recursive subpart[, array recursive parent part id[, int recursive counter[, bool recursive is a sub part[, bool recursive skip part]]]]])
82         * Indexes the structure of a message body.
83         *
84         * Gather message information returned by imap_fetchstructure()
85         * Recursively iterate through each parts array
86         * Concatenate part numbers in the following format `1.1` each part id is separated by a period, each referring
87         * to a part or subpart of a multipart message.  Create part numbers as such that they are compatible with imap_fetchbody()
88         **
89        */
90
91        function fetch_structure($mid, $sub_part = null, $sub_pid = null, $n = 0, $is_sub_part = false, $skip_part = false)
92        {
93            if (!is_array($sub_part))
94            {
95                $this->structure[$mid] = imap_fetchstructure($this->mailbox, $mid, FT_UID);
96            }
97            if (isset($this->structure[$mid]->parts) || is_array($sub_part))
98            {
99                if ($is_sub_part == false)
100                {
101                    $parts = $this->structure[$mid]->parts;
102                }
103                else
104                {
105                    $parts = $sub_part;
106                    $n++;
107                }
108
109                for($p = 0, $i = 1; $p < count($parts); $n++, $p++, $i++)
110                {
111                    // Skip the following...
112                    // Skip multipart/mixed!
113                    // Skip subsequent multipart/alternative if this part is message/rfc822
114                    // Skip multipart/related
115
116                    $ftype        = (empty($parts[$p]->type))?           $this->data_types[0].'/'.strtolower($parts[$p]->subtype) : $this->data_types[$parts[$p]->type].'/'.strtolower($parts[$p]->subtype);
117                    $encoding     = (empty($parts[$p]->encoding))?       $this->encoding_types[0] : $this->encoding_types[$parts[$p]->encoding];
118                    if(!preg_match("/5./",phpversion()))
119                            $charset      = $parts[$p]->parameters[0]->value;
120                    else
121                                                $charset      = ( isset( $parts->p->parameters[0]->value ) ) ? $parts->p->parameters[0]->value : NULL;
122                    $skip_next    = ($ftype == 'message/rfc822')?        true : false;
123
124                    if ($ftype == 'multipart/report' || $ftype == 'multipart/mixed' || $skip_part == true && $ftype == 'multipart/alternative' || ( $ftype == 'multipart/related' && strtolower( $parts[$p]->parts[0]->subtype ) == 'alternative' ) )
125                    {
126                        $n--;
127                    }
128                    else
129                    {
130                        $this->pid[$mid][$n]       = ($is_sub_part == false || $skip_part && $ftype == 'multipart/related' )? $i : ($sub_pid == '' ? '1' : $sub_pid).'.'.$i;
131                        $this->file_type[$mid][$n] = $ftype;
132                        $this->encoding[$mid][$n]  = $encoding;
133                                                $this->charset[$mid][$n]   = $charset;
134                        $this->fsize[$mid][$n]     = (!isset($parts[$p]->bytes) || empty($parts[$p]->bytes))? 0 : $parts[$p]->bytes;
135                                                $hasAttachment = false;
136                        # Force inline disposition if none is present
137                        //if ($parts[$p]->ifdisposition == true)
138                        //{ por niltonneto, as vezes, inline anexos nao eram exibidos.
139                            $this->disposition[$mid][$n] = ( isset( $parts[$p]->disposition ) ) ? strtolower($parts[$p]->disposition) : NULL;
140
141                            //if (strtolower($parts[$p]->disposition) == 'attachment')
142                            //{ por jakjr, as vezes, inline anexos nao eram exibidos.
143                                if ($parts[$p]->ifdparameters == true)
144                                {
145                                    $params = $parts[$p]->dparameters;
146
147                                    foreach ($params as $param)
148                                    {
149                                        if((strtolower($param->attribute) == 'filename') || (strtolower($param->attribute) == 'name'))
150                                        {
151                                            $this->fname[$mid][$n] = $param->value;
152                                            $hasAttachment = true;
153                                            break;
154                                        }                                       
155                                    }
156                                }
157                               
158                                // Alguns web-mails utilizam o parameters
159                                if ($parts[$p]->ifparameters == true && !$hasAttachment)
160                                {
161                                    $params = $parts[$p]->parameters;
162                                    foreach ($params as $param)
163                                    {
164                                        if((strtolower($param->attribute) == 'filename') || (strtolower($param->attribute) == 'name'
165                                         || strtolower($param->attribute) == 'filename*') || strtolower($param->attribute) == 'name*')
166                                        {
167                                                if(strstr(strtolower($param->value), "iso-8859-1''")){
168                                                       
169                                                        $value = explode("''",$param->value);
170                                                        $this->fname[$mid][$n] = rawurldecode($value[1]);
171                                                }
172                                                else
173                                                $this->fname[$mid][$n] = $param->value;
174                                               
175                                            break;
176                                        }
177                                        if(strtolower($param->attribute) == 'charset'){
178                                                if($this->charset[$mid][$n] == '')
179                                                        $this->charset[$mid][$n] = $param->value;                                               
180                                        }
181                                    }
182                                                                }
183                            //}
184                        /*}
185                        else
186                        {
187                            $this->disposition[$mid][$n] = 'inline';
188                        }*/
189
190                        if ($parts[$p]->ifid == true)
191                        {
192                            $this->inline_id[$mid][$n] = $parts[$p]->id;
193                        }
194                    }
195
196                    if (isset($parts[$p]->parts) && is_array($parts[$p]->parts))
197                    {
198                        $this->has_attachments[$mid][$n] = true;
199                        $n = $this->fetch_structure($mid, $parts[$p]->parts, $this->pid[$mid][$n], $n, true, $skip_next);
200                    }
201                    else
202                    {
203                        $this->has_attachments[$mid][$n] = false;
204                    }
205                }
206
207                if ($is_sub_part == true)
208                {
209                    return $n;
210                }
211            }
212
213            // $parts is not an array... message is flat
214            else
215            {
216                $this->pid[$mid][0] = 1;
217
218                if (empty($this->structure[$mid]->type))
219                {
220                    $this->structure[$mid]->type        = (int) 0;
221                }
222               
223                if (isset($this->structure[$mid]->subtype))
224                {
225                    $this->file_type[$mid][0]            = $this->data_types[$this->structure[$mid]->type].'/'.strtolower($this->structure[$mid]->subtype);
226                }
227           
228                if (empty($this->structure[$mid]->encoding))
229                {
230                    $this->structure[$mid]->encoding    = (int) 0;
231                }
232               
233                $this->encoding[$mid][0]              = $this->encoding_types[$this->structure[$mid]->encoding];
234                if(!preg_match("/5./",phpversion()))
235                                        $this->charset[$mid][0] = $this->structure[$mid]->parameters[0]->value;
236                                else
237                                        $this->charset[$mid][0] = ( isset( $this->structure->mid->parameters[0]->value ) ) ? $this->structure->mid->parameters[0]->value : NULL;
238
239                if (isset($this->structure[$mid]->bytes))
240                {
241                    $this->fsize[$mid][0]                = strtolower($this->structure[$mid]->bytes);
242                }
243               
244                                if (isset($this->structure[$mid]->ifdparameters))
245                                {
246                                        $params = ( isset( $this->structure[$mid]->dparameters ) ) ? $this->structure[$mid]->dparameters : NULL;
247                                        $n = 0;
248                                        if($params)
249                                        foreach ($params as $param)
250                                        {
251                                                if((strtolower($param->attribute) == 'filename') || (strtolower($param->attribute) == 'name'))
252                                                {
253                                                        $this->fname[$mid][$n] = $param->value;
254                                                        break;
255                                                }
256                                                $n++;
257                                        }
258                                }
259                                if (isset($this->structure[$mid]->ifparameters))
260                                {
261                                        $params = $this->structure[$mid]->parameters;
262                                        $n = 0;
263                                        if($params)
264                                        foreach ($params as $param)
265                                        {
266                                                if(strtolower($param->attribute) == 'charset'){
267                                if($this->charset[$mid][$n] == '')
268                                $this->charset[$mid][$n] = $param->value;
269                        }
270                                                $n++;
271                                        }
272                                }
273                                $this->disposition[$mid][0] = ( isset( $this->structure[$mid]->disposition ) ) ? $this->structure[$mid]->disposition : NULL;
274                //$this->disposition[$mid][0] = 'inline';
275            }
276
277            return;
278        }               
279    }
280   
281/*
282    // Example usage -- dump part ids for the specified message..
283
284    $msg =& new message_components($mb);
285    $msg->fetch_structure(12905);
286
287    echo '<pre>';   
288    //print_r($msg->pid[12905]);
289    echo count ($msg->fname[12905]);
290    echo '</pre>';
291
292    // also important to note that the offset numbering in the sub array isn't precise... $msg->pid[$mid][0]..
293    // I have a bug somewhere in there.. but I use foreach when accessing these arrays anyway.
294*/
295?>
Note: See TracBrowser for help on using the repository browser.