source: sandbox/2.5.0-expresso1/expressoMail1_2/inc/class.message_components.inc.php @ 5509

Revision 5509, 14.1 KB checked in by gustavo, 12 years ago (diff)

Ticket #2488 - Adicionar cabecalho de licenca em arquivos que nao o possuem

  • Property svn:eol-style set to native
  • Property svn:executable set to *
Line 
1<?php
2                /***************************************************************************
3                * Expresso Livre                                                           *
4                * http://www.expressolivre.org                                             *
5                * --------------------------------------------                             *
6                *  This program is free software; you can redistribute it and/or modify it *
7                *  under the terms of the GNU General Public License as published by the   *
8                *  Free Software Foundation; either version 2 of the License, or (at your  *
9                *  option) any later version.                                              *
10                \**************************************************************************/
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     = (isset($parts[$p]->encoding) && !empty($parts[$p]->encoding) && isset($this->encoding_types[$parts[$p]->encoding]) )?      $this->encoding_types[$parts[$p]->encoding]  : $this->encoding_types[0];
118                 
119                    if(!preg_match("/5./",phpversion()))
120                            $charset      = $parts[$p]->parameters[0]->value;
121                    else
122                                                $charset      = ( isset( $parts->p->parameters[0]->value ) ) ? $parts->p->parameters[0]->value : NULL;
123                    $skip_next    = ($ftype == 'message/rfc822')?        true : false;
124
125                    if ($ftype == 'multipart/report' || $skip_part == true && ( $ftype == 'multipart/alternative' && strpos( strtolower( $parts[$p]->parts[0]->subtype ), array( 'html', 'plain' ) ) === false ) || ( $ftype == 'multipart/related' && strtolower( $parts[$p]->parts[0]->subtype ) == 'alternative' ) )
126                    {
127                        $n--;
128                    }
129                    else
130                    {
131                        $this->pid[$mid][$n]       = ($is_sub_part == false || $skip_part && $ftype == 'multipart/related' )? $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] = ( isset( $parts[$p]->disposition ) ) ? strtolower($parts[$p]->disposition) : NULL;
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] = ( isset( $this->structure->mid->parameters[0]->value ) ) ? $this->structure->mid->parameters[0]->value : NULL;
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 = ( isset( $this->structure[$mid]->dparameters ) ) ? $this->structure[$mid]->dparameters : NULL;
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                                                        $n++;
271                        }
272                                        }
273                                }
274                                $this->disposition[$mid][0] = ( isset( $this->structure[$mid]->disposition ) ) ? $this->structure[$mid]->disposition : NULL;
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.