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

Revision 1040, 13.2 KB checked in by amuller, 15 years ago (diff)

Ticket #559 - Atualização de download de arquivos e sessão

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