alecpl
2010-04-23 373747becc9324f294980923a77b5ccffb5f3124
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
638fb8 18  $Id$
8fa58e 19
T 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     }
cc97ea 87     
T 88     // notify plugins and let them analyze this structured message object
89     $this->app->plugins->exec_hook('message_load', array('object' => $this));
8fa58e 90   }
T 91   
92   
93   /**
94    * Return a (decoded) message header
95    *
96    * @param string Header name
97    * @param bool   Don't mime-decode the value
98    * @return string Header value
99    */
100   public function get_header($name, $raw = false)
101   {
5d6ec2 102     $value = $this->headers->$name;
8fa58e 103     return $raw ? $value : $this->imap->decode_header($value);
T 104   }
105   
712b30 106   /**
A 107    * Set is_safe var and session data
108    *
109    * @param bool enable/disable
110    */
111   public function set_safe($safe = true)
112   {
113     $this->is_safe = $safe;
114     $_SESSION['safe_messages'][$this->uid] = $this->is_safe;
115   }
8fa58e 116   
T 117   /**
118    * Compose a valid URL for getting a message part
119    *
120    * @param string Part MIME-ID
121    * @return string URL or false if part does not exist
122    */
123   public function get_part_url($mime_id)
124   {
125     if ($this->mime_parts[$mime_id])
126       return $this->opt['get_url'] . "&_part=" . $mime_id;
127     else
128       return false;
129   }
130   
131   
132   /**
133    * Get content of a specific part of this message
134    *
135    * @param string Part MIME-ID
81b573 136    * @param resource File pointer to save the message part
8fa58e 137    * @return string Part content
T 138    */
81b573 139   public function get_part_content($mime_id, $fp=NULL)
8fa58e 140   {
T 141     if ($part = $this->mime_parts[$mime_id])
81b573 142       return $this->imap->get_message_part($this->uid, $mime_id, $part, NULL, $fp);
8fa58e 143     else
T 144       return null;
145   }
146   
147   
148   /**
149    * Determine if the message contains a HTML part
150    *
151    * @return bool True if a HTML is available, False if not
152    */
153   function has_html_part()
154   {
155      // check all message parts
156      foreach ($this->parts as $pid => $part) {
157         $mimetype = strtolower($part->ctype_primary . '/' . $part->ctype_secondary);
158         if ($mimetype == 'text/html')
159            return true;
160      }
161
162      return false;
163   }
164
165   /**
166    * Return the first HTML part of this message
167    *
168    * @return string HTML message part content
169    */
170   function first_html_part()
171     {
172     // check all message parts
173     foreach ($this->mime_parts as $mime_id => $part) {
174       $mimetype = strtolower($part->ctype_primary . '/' . $part->ctype_secondary);
175       if ($mimetype == 'text/html') {
19d1b2 176         return $this->imap->get_message_part($this->uid, $mime_id, $part);
8fa58e 177       }
T 178     }
179   }
180
181
182   /**
183    * Return the first text part of this message
184    *
185    * @return string Plain text message/part content
186    */
187   function first_text_part()
188     {
189     // no message structure, return complete body
190     if (empty($this->parts))
191       return $this->body;
192       
193     $out = null;
194
195     // check all message parts
196     foreach ($this->mime_parts as $mime_id => $part) {
197       $mimetype = strtolower($part->ctype_primary . '/' . $part->ctype_secondary);
198
199       if ($mimetype == 'text/plain') {
200         $out = $this->imap->get_message_part($this->uid, $mime_id, $part);
201         break;
202       }
203       else if ($mimetype == 'text/html') {
204         $html_part = $this->imap->get_message_part($this->uid, $mime_id, $part);
205
206         // remove special chars encoding
207         $trans = array_flip(get_html_translation_table(HTML_ENTITIES));
208         $html_part = strtr($html_part, $trans);
209
210         // create instance of html2text class
211         $txt = new html2text($html_part);
212         $out = $txt->get_text();
213         break;
214       }
215     }
216
217     return $out;
218   }
219
220
221   /**
222    * Raad the message structure returend by the IMAP server
223    * and build flat lists of content parts and attachments
224    *
225    * @param object rcube_message_part Message structure node
226    * @param bool  True when called recursively
227    */
aa16b4 228   private function parse_structure($structure, $recursive = false)
8fa58e 229   {
aa16b4 230     $message_ctype_primary = $structure->ctype_primary;
A 231     $message_ctype_secondary = $structure->ctype_secondary;
232     $mimetype = $structure->mimetype;
233
234     // real content-type of message/rfc822 part
235     if ($mimetype == 'message/rfc822') {
236       if ($structure->real_mimetype) {
237         $mimetype = $structure->real_mimetype;
238         list($message_ctype_primary, $message_ctype_secondary) = explode('/', $mimetype);
239       }
240     }
8fa58e 241
T 242     // show message headers
243     if ($recursive && is_array($structure->headers) && isset($structure->headers['subject'])) {
244       $c = new stdClass;
245       $c->type = 'headers';
246       $c->headers = &$structure->headers;
247       $this->parts[] = $c;
248     }
249
250     // print body if message doesn't have multiple parts
251     if ($message_ctype_primary == 'text' && !$recursive) {
252       $structure->type = 'content';
253       $this->parts[] = &$structure;
254     }
6695db 255     // the same for pgp signed messages
aa16b4 256     else if ($mimetype == 'application/pgp' && !$recursive) {
6695db 257       $structure->type = 'content';
T 258       $this->parts[] = &$structure;
259     }
8fa58e 260     // message contains alternative parts
aa16b4 261     else if ($mimetype == 'multipart/alternative' && is_array($structure->parts)) {
8fa58e 262       // get html/plaintext parts
T 263       $plain_part = $html_part = $print_part = $related_part = null;
264
265       foreach ($structure->parts as $p => $sub_part) {
aa16b4 266         $sub_mimetype = $sub_part->mimetype;
8fa58e 267         
aa16b4 268         // check if sub part is
A 269         if ($sub_mimetype == 'text/plain')
8fa58e 270           $plain_part = $p;
aa16b4 271         else if ($sub_mimetype == 'text/html')
8fa58e 272           $html_part = $p;
aa16b4 273         else if ($sub_mimetype == 'text/enriched')
8fa58e 274           $enriched_part = $p;
aa16b4 275         else if (in_array($sub_mimetype, array('multipart/related', 'multipart/mixed', 'multipart/alternative')))
8fa58e 276           $related_part = $p;
T 277       }
278
279       // parse related part (alternative part could be in here)
280       if ($related_part !== null && !$this->parse_alternative) {
281         $this->parse_alternative = true;
282         $this->parse_structure($structure->parts[$related_part], true);
283         $this->parse_alternative = false;
284         
285         // if plain part was found, we should unset it if html is preferred
286         if ($this->opt['prefer_html'] && count($this->parts))
287           $plain_part = null;
288       }
289
290       // choose html/plain part to print
291       if ($html_part !== null && $this->opt['prefer_html']) {
292         $print_part = &$structure->parts[$html_part];
293       }
294       else if ($enriched_part !== null) {
295         $print_part = &$structure->parts[$enriched_part];
296       }
297       else if ($plain_part !== null) {
298         $print_part = &$structure->parts[$plain_part];
299       }
300
301       // add the right message body
302       if (is_object($print_part)) {
303         $print_part->type = 'content';
304         $this->parts[] = $print_part;
305       }
306       // show plaintext warning
82bac8 307       else if ($html_part !== null && empty($this->parts)) {
8fa58e 308         $c = new stdClass;
T 309         $c->type = 'content';
310         $c->body = rcube_label('htmlmessage');
311         $c->ctype_primary = 'text';
312         $c->ctype_secondary = 'plain';
313
314         $this->parts[] = $c;
315       }
316
317       // add html part as attachment
318       if ($html_part !== null && $structure->parts[$html_part] !== $print_part) {
319         $html_part = &$structure->parts[$html_part];
320         $html_part->filename = rcube_label('htmlmessage');
321         $html_part->mimetype = 'text/html';
322
323         $this->attachments[] = $html_part;
324       }
325     }
326     // this is an ecrypted message -> create a plaintext body with the according message
aa16b4 327     else if ($mimetype == 'multipart/encrypted') {
8fa58e 328       $p = new stdClass;
T 329       $p->type = 'content';
330       $p->ctype_primary = 'text';
331       $p->ctype_secondary = 'plain';
332       $p->body = rcube_label('encryptedmessage');
330ef6 333       $p->size = strlen($p->body);
8fa58e 334       
330ef6 335       // maybe some plugins are able to decode this encrypted message part
T 336       $data = $this->app->plugins->exec_hook('message_part_encrypted', array('object' => $this, 'struct' => $structure, 'part' => $p));
337       if (is_array($data['parts'])) {
338         $this->parts = array_merge($this->parts, $data['parts']);
339       }
340       else if ($data['part']) {
341         $this->parts[] = $p;
342       }
8fa58e 343     }
T 344     // message contains multiple parts
345     else if (is_array($structure->parts) && !empty($structure->parts)) {
346       // iterate over parts
347       for ($i=0; $i < count($structure->parts); $i++) {
348         $mail_part = &$structure->parts[$i];
aa16b4 349         $primary_type = $mail_part->ctype_primary;
A 350         $secondary_type = $mail_part->ctype_secondary;
351
352         // real content-type of message/rfc822
353         if ($mail_part->real_mimetype) {
354           $part_orig_mimetype = $mail_part->mimetype;
355           $part_mimetype = $mail_part->real_mimetype;
356           list($primary_type, $secondary_type) = explode('/', $part_mimetype);
357         }
358         else
359           $part_mimetype = $mail_part->mimetype;
8fa58e 360
T 361         // multipart/alternative
362         if ($primary_type=='multipart') {
363           $this->parse_structure($mail_part, true);
aa16b4 364
A 365           // list message/rfc822 as attachment as well (mostly .eml)
366           if ($part_orig_mimetype == 'message/rfc822' && !empty($mail_part->filename))
367             $this->attachments[] = $mail_part;
8fa58e 368         }
T 369         // part text/[plain|html] OR message/delivery-status
aa16b4 370         else if ((($part_mimetype == 'text/plain' || $part_mimetype == 'text/html') && $mail_part->disposition != 'attachment') ||
A 371             $part_mimetype == 'message/delivery-status' || $part_mimetype == 'message/disposition-notification') {
b0d56e 372
3815e0 373           // add text part if it matches the prefs
aa16b4 374           if (!$this->parse_alternative ||
19d6a8 375               ($secondary_type == 'html' && $this->opt['prefer_html']) ||
8fa58e 376               ($secondary_type == 'plain' && !$this->opt['prefer_html'])) {
T 377             $mail_part->type = 'content';
378             $this->parts[] = $mail_part;
379           }
3815e0 380           
8fa58e 381           // list as attachment as well
T 382           if (!empty($mail_part->filename))
383             $this->attachments[] = $mail_part;
384         }
385         // part message/*
386         else if ($primary_type=='message') {
aa16b4 387           $this->parse_structure($mail_part, true);
19d6a8 388
ae579c 389           // list as attachment as well (mostly .eml)
T 390           if (!empty($mail_part->filename))
391             $this->attachments[] = $mail_part;
8fa58e 392         }
T 393         // ignore "virtual" protocol parts
394         else if ($primary_type == 'protocol')
395           continue;
21b160 396           
b0d56e 397         // part is Microsoft Outlook TNEF (winmail.dat)
aa16b4 398         else if ($part_mimetype == 'application/ms-tnef') {
21b160 399           foreach ((array)$this->imap->tnef_decode($mail_part, $structure->headers['uid']) as $tnef_part) {
T 400             $this->mime_parts[$tnef_part->mime_id] = $tnef_part;
401             $this->attachments[] = $tnef_part;
402           }
403         }
8fa58e 404
b0d56e 405         // part is a file/attachment
A 406         else if (preg_match('/^(inline|attach)/', $mail_part->disposition) ||
8fa58e 407                  $mail_part->headers['content-id'] || (empty($mail_part->disposition) && $mail_part->filename)) {
b0d56e 408
8fa58e 409           // skip apple resource forks
T 410           if ($message_ctype_secondary == 'appledouble' && $secondary_type == 'applefile')
411             continue;
9e8a17 412
b26e1d 413           // part belongs to a related message and is linked
aa16b4 414           if ($mimetype == 'multipart/related'
A 415               && preg_match('!^image/!', $part_mimetype)
081656 416               && ($mail_part->headers['content-id'] || $mail_part->headers['content-location'])) {
9e8a17 417             if ($mail_part->headers['content-id'])
A 418               $mail_part->content_id = preg_replace(array('/^</', '/>$/'), '', $mail_part->headers['content-id']);
419             if ($mail_part->headers['content-location'])
420               $mail_part->content_location = $mail_part->headers['content-base'] . $mail_part->headers['content-location'];
081656 421               
b26e1d 422             $this->inline_parts[] = $mail_part;
21b160 423           }
b0d56e 424           // is a regular attachment
373747 425           else if (preg_match('!^[a-z0-9-.+]+/[a-z0-9-.+]+$!i', $part_mimetype)) {
8fa58e 426             if (!$mail_part->filename)
T 427               $mail_part->filename = 'Part '.$mail_part->mime_id;
428             $this->attachments[] = $mail_part;
429           }
430         }
431       }
432
433       // if this was a related part try to resolve references
aa16b4 434       if ($mimetype == 'multipart/related' && sizeof($this->inline_parts)) {
8fa58e 435         $a_replaces = array();
T 436
437         foreach ($this->inline_parts as $inline_object) {
c505e5 438           $part_url = $this->get_part_url($inline_object->mime_id);
T 439           if ($inline_object->content_id)
440             $a_replaces['cid:'.$inline_object->content_id] = $part_url;
441           if ($inline_object->content_location)
442             $a_replaces[$inline_object->content_location] = $part_url;
8fa58e 443         }
T 444
445         // add replace array to each content part
446         // (will be applied later when part body is available)
45f56c 447         foreach ($this->parts as $i => $part) {
T 448           if ($part->type == 'content')
449             $this->parts[$i]->replaces = $a_replaces;
8fa58e 450         }
T 451       }
452     }
453
aa16b4 454     // message is a single part non-text
8fa58e 455     else if ($structure->filename) {
T 456       $this->attachments[] = $structure;
457     }
458   }
459
460
461   /**
462    * Fill aflat array with references to all parts, indexed by part numbers
463    *
464    * @param object rcube_message_part Message body structure
465    */
466   private function get_mime_numbers(&$part)
467   {
468     if (strlen($part->mime_id))
469       $this->mime_parts[$part->mime_id] = &$part;
470       
471     if (is_array($part->parts))
472       for ($i=0; $i<count($part->parts); $i++)
473         $this->get_mime_numbers($part->parts[$i]);
474   }
475
476
477 }
478