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

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

Ticket #559 - Atualização de segurança

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