/[drupal]/contributions/modules/mail_api/contrib/imapwu_api/libs/imap_mail.class.php
ViewVC logotype

Diff of /contributions/modules/mail_api/contrib/imapwu_api/libs/imap_mail.class.php

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.1.2.1, Wed Jan 7 21:28:24 2009 UTC revision 1.1.2.2, Thu May 14 18:57:02 2009 UTC
# Line 0  Line 1 
1    <?php
2    /*
3    This is a wrapper around the IMAP protocol. It implements some functions which are not
4    native to IMAP but greately simply working with messages stored in IMAP.
5    */
6    class imap_mail extends imap
7    {
8            /*
9            returns an array with message parts indexed and objects attached
10            */
11            function get_map($mid)
12            {
13                    $structure = $this -> get_structure($mid);
14    
15                    if(!$structure->parts) {
16                            return FALSE;
17                    }
18    
19                    return $this->create_part_array($structure);
20            }
21    
22            function create_part_array($structure, $prefix="")
23            {
24                    $part_array = array();
25    
26                if (sizeof($structure->parts) > 0) {
27                    foreach ($structure->parts as $count => $part) {
28                        $this->add_part_to_array($part, $prefix.($count+1), $part_array);
29                    }
30                }
31    
32                    return $part_array;
33            }
34    
35    
36    
37            function add_part_to_array($obj, $partno, &$part_array)
38            {
39    
40                    if(!is_array($part_array) || empty($part_array)) $part_array=array();
41    
42                    if ($obj->type == TYPEMESSAGE) {
43               $this->add_part_to_array($obj->parts[0], $partno.".", $part_array);
44              }
45              else {
46               if (sizeof($obj->parts) > 0) {
47                 foreach ($obj->parts as $count => $p) {
48                   $this->add_part_to_array($p, $partno.".".($count+1), $part_array);
49                 }
50               }
51              }
52    
53                $part_array[] = array('part_number' => $partno, 'part_object' => $obj);
54            }
55    
56            /*
57            returns a string that contains the plain text part of the message
58            */
59            function get_plaintext($mid, $options="")
60            {
61                    $map = $this->get_map($mid);
62    
63                    //print_r($map);
64    
65                    if(!$map) {
66                            // this is a simple plain text message
67                            $content = $this -> get_body($mid, 1, $options);
68    
69                            $encoding = $this->get_plaintext_encoding($mid);
70    
71                            //echo "encoding is: $encoding<br>\n";
72    
73                            if($encoding=="base64") {
74                                    $content = base64_decode($content);
75                            } else {
76                                    $content = quoted_printable_decode($content);
77    
78                            }
79    
80    
81                    } else {
82                            // extract the PLAIN part out of all parts
83    
84                            foreach($map as $index=>$part_info) {
85    
86                                    if($part_info['part_object']->subtype == "PLAIN") {
87                                            $content  = $this->get_part($mid, $part_info['part_number']);
88    
89    
90                                            break;
91    
92                                    }
93    
94                            }
95    
96                    }
97    
98                    if(!empty($content)) {
99                            return $content;
100                    } else {
101                            return FALSE;
102                    }
103            }
104    
105            /*
106            returns the charset for the plaintext part of the message
107            */
108            function get_plaintext_charset($mid, $options="")
109            {
110                    $map = $this->get_map($mid);
111    
112    
113    
114                    if(!$map) {
115                            //echo "no map<br>\n";
116                            $header = $this->get_header($mid);
117    
118                            $body = $this->get_body($mid,0);
119    
120                            // see if there's a content type string
121                            if(preg_match("/Content-Type/", $body)) {
122                                    //echo "there's content type<br>";
123    
124                                    // charset may or may not be enclosed in quotes
125                                    preg_match("/charset=\"?(.*)\"?/", $body, $matches);
126    
127    
128                                    $charset = $matches[1];
129                                    return $charset;
130                            }
131    
132                            /*
133                            echo "<pre>";
134                            print_r($body);
135                            echo "</pre>";
136                            */
137                            return FALSE;
138    
139                    } else {
140                            //echo "have map";
141                            // extract the PLAIN part out of all parts
142    
143                            foreach($map as $index=>$part_info) {
144    
145                                    if($part_info['part_object']->subtype == "PLAIN" && is_array($part_info['part_object']->parameters)) {
146    
147                                            foreach($part_info['part_object']->parameters as $parameter_id=>$parameter_info) {
148                                                    if($parameter_info->attribute=="charset") {
149                                                            $charset = $parameter_info -> value;
150                                                            //echo "charset: $charset<br>";
151                                                            break;
152                                                    }
153                                            }
154    
155                                    }
156    
157                            }
158    
159                    }
160    
161                    if(!empty($charset)) {
162                            return $charset;
163                    } else {
164                            return FALSE;
165                    }
166            }
167    
168    
169            /*
170            returns the encoding with which a particular message was encoded
171            */
172            function get_plaintext_encoding($mid, $options="")
173            {
174                    $body = $this->get_body($mid,0);
175    
176                    // see if there's a content transfer string
177                    if(preg_match("/Content-Transfer-Encoding:/", $body)) {
178                            //echo "there's encoding<br>";
179    
180                            preg_match("/Content-Transfer-Encoding:(.*)/", $body, $matches);
181    
182                            $encoding = trim($matches[1]);
183                            return $encoding;
184                    }
185    
186                    return FALSE;
187    
188            }
189    
190            /*
191            returns an array with attachment details.
192            this makes it very easy to work with attachments
193            */
194            function get_attachment_overview($mid)
195            {
196                    $map = $this->get_map($mid);
197    
198                    // this message contains no attachemnts
199                    if(!$map) {
200                            return FALSE;
201                    }
202    
203                    //die('we are here');
204    
205                    foreach($map as $index=>$part_info) {
206                            if($part_info['part_object']->ifdisposition == 1) {
207                                    $attachments[] = array(
208                                            'part_number' => $part_info['part_number'],
209                                            'type' => $part_info['part_object']->subtype,
210                                            'size' => $part_info['part_object']->bytes,
211                                            'file' => $part_info['part_object']->dparameters[0]->value
212                                    );
213                            }
214                    }
215    
216                    return $attachments;
217            }
218    
219            function get_attachment_files($mid) {
220    
221                    // first get the overview
222                    $att_overview = $this -> get_attachment_overview($mid);
223    
224    
225                    // now return only those that have actual file names
226                    if(empty($att_overview)) return FALSE;
227                    if(sizeof($att_overview)<=0) return FALSE;
228    
229                    foreach($att_overview as $att_id => $att_info) {
230                            if(!empty($att_info['file'])) {
231                                    $rs[]=$att_info;
232                            }
233                    }
234    
235                    return $rs;
236    
237            }
238    
239            /*
240            this returns the part while decoding it
241            */
242            function get_part($mid, $part)
243            {
244    
245                    //echo "getting part $part of $mid<br>\n";
246    
247                    $map = $this -> get_map($mid);
248    
249                    //print_r($map);
250    
251                    if(!$map) {
252                            //echo "map is empty";
253                            return FALSE;
254                    }
255    
256                    foreach($map as $index=>$part_info) {
257                            if($part_info['part_number']==$part) {
258    
259    
260    
261                                    if($part_info['part_object']->type ==0 || $part_info['part_object']->type =="") {
262    
263    
264    
265    
266                                            if($part_info['part_object']->encoding==3) {
267                                                    $content = base64_decode($this->get_body($mid,$part));
268                                            } elseif ($part_info['part_object']->encoding==4) {
269                                                    $content = quoted_printable_decode($this->get_body($mid,$part));
270                                            } else {
271                                                    $content = $this->get_body($mid,$part);
272                                            }
273    
274                                            // handle plain text data
275                                            //$content = $this -> get_body($mid, $part);
276                                    } else {
277    
278    
279                                            // handle complex encoded data
280                                            //echo "handling complex encoded data<br>";
281    
282                                            // handle base64 encoded data
283                                            if($part_info['part_object']->encoding==3) {
284                                                    $content = base64_decode($this->get_body($mid,$part));
285                                            }
286    
287                                            // handle quoted data
288                                            if($part_info['part_object']->encoding==4) {
289                                                    $content = quoted_printable_decode($this->get_body($mid,$part));
290                                            }
291    
292                                    }
293    
294                                    return $content;
295    
296                            }
297                    }
298    
299    
300                    return FALSE;
301            }
302    
303    
304    
305    
306            /*
307            switches the active folder by recreating the connection
308            */
309            function active_folder($folder)
310            {
311                    if(empty($folder)) return FALSE;
312                    if($this->folder == $folder) return;
313    
314                    $this-> close();
315                    $this-> connect($this->username, $this->password, $this->host, $this->port, $folder, $this->options);
316    
317            }
318    
319            /*
320            this returns an object of the part of a message
321            */
322            function get_part_object($mid, $part)
323            {
324                    if(!$this->mbox) return FALSE;
325    
326                    $map = $this -> get_map($mid);
327    
328                    foreach($map as $map_id=>$part_info) {
329                            if($part_info['part_number']==$part) {
330                                    return $part_info['part_object'];
331                            }
332                    }
333    
334                    return FALSE;
335            }
336    
337            function get_part_mime_type($mid, $part) {
338                    $part = $this -> get_part_object($mid, $part);
339    
340    
341    
342                switch(strtoupper($part -> subtype)) {
343                  # Applications
344                  case 'PDF':
345                  case 'PS':
346                  case 'POST-SCRIPT':
347                    $mime = 'application/pdf';
348                    $img = "$path/page_white_acrobat.png";
349                    break;
350                  case 'MSWORD':
351                    $mime = 'application/msword';
352                    $img = "$path/page_word.png";
353                    break;
354                  case 'PNG':
355                  case 'GIF':
356                  case 'BMP':
357                  case 'JPG':
358                  case 'JPEG':
359                    $mime = "image/$type";
360                    break;
361                  case 'MP3':
362                  case 'WMA':
363                  case 'X-MS-WMA':
364                  case 'AAC':
365                    $mime = "audio/$type";
366                    $img = "$path/sound.png";
367                    break;
368                  case 'MPEG':
369                  case 'WMV':
370                  case 'AVI':
371                    $mime = "video/$type";
372                    $img = "$path/film.png";
373                    break;
374                  default:
375                    $mime = 'text/x-generic';
376                    $img = "$path/application.png";
377                }
378    
379                return $mime;
380            }
381    
382    
383            function mime_decode($data, $encoding)
384            {
385                    switch($encoding) {
386                            case ENC8BIT:
387                            $data = imap_8bit($data);
388                            $data = quoted_printable_decode($data);
389                            break;
390                    case ENCBASE64:
391                                    $data = base64_decode(stripslashes($data));
392                                    break;
393                            case ENCQUOTEDPRINTABLE:
394                            $data = quoted_printable_decode($data);
395                    break;
396            }
397                return $data;
398      }
399    
400    }
401    ?>

Legend:
Removed from v.1.1.2.1  
changed lines
  Added in v.1.1.2.2

  ViewVC Help
Powered by ViewVC 1.1.2