thomascube
2009-02-08 21b160f38c98bf41ebc843e7639b5b1af588b489
commit | author | age
8fa58e 1 <?php
T 2
3 /*
4  +-----------------------------------------------------------------------+
5  | program/include/rcube_message.php                                     |
6  |                                                                       |
7  | This file is part of the RoundCube Webmail client                     |
cbbef3 8  | Copyright (C) 2008-2009, RoundCube Dev. - Switzerland                 |
8fa58e 9  | Licensed under the GNU GPL                                            |
T 10  |                                                                       |
11  | PURPOSE:                                                              |
12  |   Logical representation of a mail message with all its data          |
13  |   and related functions                                               |
14  +-----------------------------------------------------------------------+
15  | Author: Thomas Bruederli <roundcube@gmail.com>                        |
16  +-----------------------------------------------------------------------+
17
18  $Id: rcube_imap.php 1344 2008-04-30 08:21:42Z thomasb $
19
20 */
21
22
23 /**
45f56c 24  * Logical representation of a mail message with all its data
T 25  * and related functions
8fa58e 26  *
T 27  * @package    Mail
28  * @author     Thomas Bruederli <roundcube@gmail.com>
29  */
30 class rcube_message
31 {
32   private $app;
33   private $imap;
34   private $opt = array();
35   private $inline_parts = array();
36   private $parse_alternative = false;
37   
38   public $uid = null;
39   public $headers;
40   public $structure;
41   public $parts = array();
42   public $mime_parts = array();
43   public $attachments = array();
44   public $subject = '';
62e43d 45   public $sender = null;
8fa58e 46   public $is_safe = false;
T 47   
193fb4 48
T 49   /**
50    * __construct
51    *
52    * Provide a uid, and parse message structure.
53    *
54    * @param string $uid The message UID.
55    *
56    * @uses rcmail::get_instance()
57    * @uses rcube_imap::decode_mime_string()
58    * @uses self::set_safe()
59    *
60    * @see self::$app, self::$imap, self::$opt, self::$structure
61    */
8fa58e 62   function __construct($uid)
T 63   {
64     $this->app = rcmail::get_instance();
65     $this->imap = $this->app->imap;
66     
67     $this->uid = $uid;
7a229b 68     $this->headers = $this->imap->get_headers($uid, NULL, true, true);
A 69
8fa58e 70     $this->subject = rcube_imap::decode_mime_string($this->headers->subject, $this->headers->charset);
62e43d 71     list(, $this->sender) = each($this->imap->decode_address_list($this->headers->from));
8fa58e 72     
62e43d 73     $this->set_safe((intval($_GET['_safe']) || $_SESSION['safe_messages'][$uid]));
8fa58e 74     $this->opt = array(
T 75       'safe' => $this->is_safe,
76       'prefer_html' => $this->app->config->get('prefer_html'),
77       'get_url' => rcmail_url('get', array('_mbox' => $this->imap->get_mailbox_name(), '_uid' => $uid))
78     );
c9ca6a 79
7a229b 80     if ($this->structure = $this->imap->get_structure($uid, $this->headers->body_structure)) {
8fa58e 81       $this->get_mime_numbers($this->structure);
45f56c 82       $this->parse_structure($this->structure);
8fa58e 83     }
T 84     else {
85       $this->body = $this->imap->get_body($uid);
86     }
87   }
88   
89   
90   /**
91    * Return a (decoded) message header
92    *
93    * @param string Header name
94    * @param bool   Don't mime-decode the value
95    * @return string Header value
96    */
97   public function get_header($name, $raw = false)
98   {
5d6ec2 99     $value = $this->headers->$name;
8fa58e 100     return $raw ? $value : $this->imap->decode_header($value);
T 101   }
102   
712b30 103   /**
A 104    * Set is_safe var and session data
105    *
106    * @param bool enable/disable
107    */
108   public function set_safe($safe = true)
109   {
110     $this->is_safe = $safe;
111     $_SESSION['safe_messages'][$this->uid] = $this->is_safe;
112   }
8fa58e 113   
T 114   /**
115    * Compose a valid URL for getting a message part
116    *
117    * @param string Part MIME-ID
118    * @return string URL or false if part does not exist
119    */
120   public function get_part_url($mime_id)
121   {
122     if ($this->mime_parts[$mime_id])
123       return $this->opt['get_url'] . "&_part=" . $mime_id;
124     else
125       return false;
126   }
127   
128   
129   /**
130    * Get content of a specific part of this message
131    *
132    * @param string Part MIME-ID
81b573 133    * @param resource File pointer to save the message part
8fa58e 134    * @return string Part content
T 135    */
81b573 136   public function get_part_content($mime_id, $fp=NULL)
8fa58e 137   {
T 138     if ($part = $this->mime_parts[$mime_id])
81b573 139       return $this->imap->get_message_part($this->uid, $mime_id, $part, NULL, $fp);
8fa58e 140     else
T 141       return null;
142   }
143   
144   
145   /**
146    * Determine if the message contains a HTML part
147    *
148    * @return bool True if a HTML is available, False if not
149    */
150   function has_html_part()
151   {
152      // check all message parts
153      foreach ($this->parts as $pid => $part) {
154         $mimetype = strtolower($part->ctype_primary . '/' . $part->ctype_secondary);
155         if ($mimetype == 'text/html')
156            return true;
157      }
158
159      return false;
160   }
161
162   /**
163    * Return the first HTML part of this message
164    *
165    * @return string HTML message part content
166    */
167   function first_html_part()
168     {
169     $html_part = null;
170
171     // check all message parts
172     foreach ($this->mime_parts as $mime_id => $part) {
173       $mimetype = strtolower($part->ctype_primary . '/' . $part->ctype_secondary);
174       if ($mimetype == 'text/html') {
175         $html_part = $this->imap->get_message_part($this->uid, $mime_id, $part);
176       }
177     }
178
179     return $html_part;
180   }
181
182
183   /**
184    * Return the first text part of this message
185    *
186    * @return string Plain text message/part content
187    */
188   function first_text_part()
189     {
190     // no message structure, return complete body
191     if (empty($this->parts))
192       return $this->body;
193       
194     $out = null;
195
196     // check all message parts
197     foreach ($this->mime_parts as $mime_id => $part) {
198       $mimetype = strtolower($part->ctype_primary . '/' . $part->ctype_secondary);
199
200       if ($mimetype == 'text/plain') {
201         $out = $this->imap->get_message_part($this->uid, $mime_id, $part);
202         break;
203       }
204       else if ($mimetype == 'text/html') {
205         $html_part = $this->imap->get_message_part($this->uid, $mime_id, $part);
206
207         // remove special chars encoding
208         $trans = array_flip(get_html_translation_table(HTML_ENTITIES));
209         $html_part = strtr($html_part, $trans);
210
211         // create instance of html2text class
212         $txt = new html2text($html_part);
213         $out = $txt->get_text();
214         break;
215       }
216     }
217
218     return $out;
219   }
220
221
222   /**
223    * Raad the message structure returend by the IMAP server
224    * and build flat lists of content parts and attachments
225    *
226    * @param object rcube_message_part Message structure node
227    * @param bool  True when called recursively
228    */
229   private function parse_structure($structure, $recursive = false)
230   {
231     $message_ctype_primary = strtolower($structure->ctype_primary);
232     $message_ctype_secondary = strtolower($structure->ctype_secondary);
233
234     // show message headers
235     if ($recursive && is_array($structure->headers) && isset($structure->headers['subject'])) {
236       $c = new stdClass;
237       $c->type = 'headers';
238       $c->headers = &$structure->headers;
239       $this->parts[] = $c;
240     }
241
242     // print body if message doesn't have multiple parts
243     if ($message_ctype_primary == 'text' && !$recursive) {
244       $structure->type = 'content';
245       $this->parts[] = &$structure;
246     }
6695db 247     // the same for pgp signed messages
T 248     else if ($message_ctype_primary == 'application' && $message_ctype_secondary == 'pgp' && !$recursive) {
249       $structure->type = 'content';
250       $this->parts[] = &$structure;
251     }
8fa58e 252     // message contains alternative parts
T 253     else if ($message_ctype_primary == 'multipart' && ($message_ctype_secondary == 'alternative') && is_array($structure->parts)) {
254       // get html/plaintext parts
255       $plain_part = $html_part = $print_part = $related_part = null;
256
257       foreach ($structure->parts as $p => $sub_part) {
258         $rel_parts = $attachmnts = null;
259         $sub_ctype_primary = strtolower($sub_part->ctype_primary);
260         $sub_ctype_secondary = strtolower($sub_part->ctype_secondary);
261         
262         // check if sub part is 
263         if ($sub_ctype_primary=='text' && $sub_ctype_secondary=='plain')
264           $plain_part = $p;
265         else if ($sub_ctype_primary=='text' && $sub_ctype_secondary=='html')
266           $html_part = $p;
267         else if ($sub_ctype_primary=='text' && $sub_ctype_secondary=='enriched')
268           $enriched_part = $p;
9b4aaa 269         else if ($sub_ctype_primary=='multipart' && in_array($sub_ctype_secondary, array('related', 'mixed', 'alternative')))
8fa58e 270           $related_part = $p;
T 271       }
272
273       // parse related part (alternative part could be in here)
274       if ($related_part !== null && !$this->parse_alternative) {
275         $this->parse_alternative = true;
276         $this->parse_structure($structure->parts[$related_part], true);
277         $this->parse_alternative = false;
278         
279         // if plain part was found, we should unset it if html is preferred
280         if ($this->opt['prefer_html'] && count($this->parts))
281           $plain_part = null;
282       }
283
284       // choose html/plain part to print
285       if ($html_part !== null && $this->opt['prefer_html']) {
286         $print_part = &$structure->parts[$html_part];
287       }
288       else if ($enriched_part !== null) {
289         $print_part = &$structure->parts[$enriched_part];
290       }
291       else if ($plain_part !== null) {
292         $print_part = &$structure->parts[$plain_part];
293       }
294
295       // add the right message body
296       if (is_object($print_part)) {
297         $print_part->type = 'content';
298         $this->parts[] = $print_part;
299       }
300       // show plaintext warning
82bac8 301       else if ($html_part !== null && empty($this->parts)) {
8fa58e 302         $c = new stdClass;
T 303         $c->type = 'content';
304         $c->body = rcube_label('htmlmessage');
305         $c->ctype_primary = 'text';
306         $c->ctype_secondary = 'plain';
307
308         $this->parts[] = $c;
309       }
310
311       // add html part as attachment
312       if ($html_part !== null && $structure->parts[$html_part] !== $print_part) {
313         $html_part = &$structure->parts[$html_part];
314         $html_part->filename = rcube_label('htmlmessage');
315         $html_part->mimetype = 'text/html';
316
317         $this->attachments[] = $html_part;
318       }
319     }
320     // this is an ecrypted message -> create a plaintext body with the according message
321     else if ($message_ctype_primary == 'multipart' && $message_ctype_secondary == 'encrypted') {
322       $p = new stdClass;
323       $p->type = 'content';
324       $p->ctype_primary = 'text';
325       $p->ctype_secondary = 'plain';
326       $p->body = rcube_label('encryptedmessage');
327       
328       $this->parts[] = $p;
329     }
330     // message contains multiple parts
331     else if (is_array($structure->parts) && !empty($structure->parts)) {
332       // iterate over parts
333       for ($i=0; $i < count($structure->parts); $i++) {
334         $mail_part = &$structure->parts[$i];
335         $primary_type = strtolower($mail_part->ctype_primary);
336         $secondary_type = strtolower($mail_part->ctype_secondary);
337
338         // multipart/alternative
339         if ($primary_type=='multipart') {
340           $this->parse_structure($mail_part, true);
341         }
342         // part text/[plain|html] OR message/delivery-status
343         else if (($primary_type == 'text' && ($secondary_type == 'plain' || $secondary_type == 'html') && $mail_part->disposition != 'attachment') ||
344                  ($primary_type == 'message' && ($secondary_type == 'delivery-status' || $secondary_type == 'disposition-notification'))) {
345           
346           // add text part if we're not in alternative mode or if it matches the prefs
347           if (!$this->parse_alternative ||
348               ($secondary_type == 'html' && $this->opt['prefer_html']) ||
349               ($secondary_type == 'plain' && !$this->opt['prefer_html'])) {
350             $mail_part->type = 'content';
351             $this->parts[] = $mail_part;
352           }
9e8a17 353
8fa58e 354           // list as attachment as well
T 355           if (!empty($mail_part->filename))
356             $this->attachments[] = $mail_part;
357         }
358         // part message/*
359         else if ($primary_type=='message') {
360           $this->parse_structure($mail_part, true);
ae579c 361           
T 362           // list as attachment as well (mostly .eml)
363           if (!empty($mail_part->filename))
364             $this->attachments[] = $mail_part;
8fa58e 365         }
T 366         // ignore "virtual" protocol parts
367         else if ($primary_type == 'protocol')
368           continue;
21b160 369           
T 370         // part is Microsoft outlook TNEF (winmail.dat)
371         else if ($primary_type == 'application' && $secondary_type == 'ms-tnef') {
372           foreach ((array)$this->imap->tnef_decode($mail_part, $structure->headers['uid']) as $tnef_part) {
373             $this->mime_parts[$tnef_part->mime_id] = $tnef_part;
374             $this->attachments[] = $tnef_part;
375           }
376         }
8fa58e 377
T 378         // part is file/attachment
379         else if ($mail_part->disposition == 'attachment' || $mail_part->disposition == 'inline' ||
380                  $mail_part->headers['content-id'] || (empty($mail_part->disposition) && $mail_part->filename)) {
381           // skip apple resource forks
382           if ($message_ctype_secondary == 'appledouble' && $secondary_type == 'applefile')
383             continue;
9e8a17 384
ffbf91 385           // part belongs to a related message
9e8a17 386           if ($message_ctype_secondary == 'related') {
A 387             if ($mail_part->headers['content-id'])
388               $mail_part->content_id = preg_replace(array('/^</', '/>$/'), '', $mail_part->headers['content-id']);
389             if ($mail_part->headers['content-location'])
390               $mail_part->content_location = $mail_part->headers['content-base'] . $mail_part->headers['content-location'];
391             
21b160 392             if ($mail_part->content_id || $mail_part->content_location) {
9e8a17 393               $this->inline_parts[] = $mail_part;
A 394             }
21b160 395           }
8fa58e 396           // is regular attachment
T 397           else {
398             if (!$mail_part->filename)
399               $mail_part->filename = 'Part '.$mail_part->mime_id;
400             $this->attachments[] = $mail_part;
401           }
402         }
403       }
404
405       // if this was a related part try to resolve references
45f56c 406       if ($message_ctype_secondary == 'related' && sizeof($this->inline_parts)) {
8fa58e 407         $a_replaces = array();
T 408
409         foreach ($this->inline_parts as $inline_object) {
c505e5 410           $part_url = $this->get_part_url($inline_object->mime_id);
T 411           if ($inline_object->content_id)
412             $a_replaces['cid:'.$inline_object->content_id] = $part_url;
413           if ($inline_object->content_location)
414             $a_replaces[$inline_object->content_location] = $part_url;
8fa58e 415         }
T 416
417         // add replace array to each content part
418         // (will be applied later when part body is available)
45f56c 419         foreach ($this->parts as $i => $part) {
T 420           if ($part->type == 'content')
421             $this->parts[$i]->replaces = $a_replaces;
8fa58e 422         }
T 423       }
424     }
425
426     // message is single part non-text
427     else if ($structure->filename) {
428       $this->attachments[] = $structure;
429     }
430   }
431
432
433   /**
434    * Fill aflat array with references to all parts, indexed by part numbers
435    *
436    * @param object rcube_message_part Message body structure
437    */
438   private function get_mime_numbers(&$part)
439   {
440     if (strlen($part->mime_id))
441       $this->mime_parts[$part->mime_id] = &$part;
442       
443     if (is_array($part->parts))
444       for ($i=0; $i<count($part->parts); $i++)
445         $this->get_mime_numbers($part->parts[$i]);
446   }
447
448
449 }
450