]> git.donarmstrong.com Git - roundcube.git/blob - program/include/rcube_message.php
Imported Upstream version 0.2.1
[roundcube.git] / program / include / rcube_message.php
1 <?php
2
3 /*
4  +-----------------------------------------------------------------------+
5  | program/include/rcube_message.php                                     |
6  |                                                                       |
7  | This file is part of the RoundCube Webmail client                     |
8  | Copyright (C) 2008-2009, RoundCube Dev. - Switzerland                 |
9  | Licensed under the GNU GPL                                            |
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 /**
24  * Logical representation of a mail message with all its data
25  * and related functions
26  *
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 = '';
45   public $sender = null;
46   public $is_safe = false;
47   
48
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    */
62   function __construct($uid)
63   {
64     $this->app = rcmail::get_instance();
65     $this->imap = $this->app->imap;
66     
67     $this->uid = $uid;
68     $this->headers = $this->imap->get_headers($uid, NULL, true, true);
69
70     $this->subject = rcube_imap::decode_mime_string($this->headers->subject, $this->headers->charset);
71     list(, $this->sender) = each($this->imap->decode_address_list($this->headers->from));
72     
73     $this->set_safe((intval($_GET['_safe']) || $_SESSION['safe_messages'][$uid]));
74     $this->opt = array(
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     );
79
80     if ($this->structure = $this->imap->get_structure($uid, $this->headers->body_structure)) {
81       $this->get_mime_numbers($this->structure);
82       $this->parse_structure($this->structure);
83     }
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   {
99     $value = $this->headers->$name;
100     return $raw ? $value : $this->imap->decode_header($value);
101   }
102   
103   /**
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   }
113   
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
133    * @param resource File pointer to save the message part
134    * @return string Part content
135    */
136   public function get_part_content($mime_id, $fp=NULL)
137   {
138     if ($part = $this->mime_parts[$mime_id])
139       return $this->imap->get_message_part($this->uid, $mime_id, $part, NULL, $fp);
140     else
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     // check all message parts
170     foreach ($this->mime_parts as $mime_id => $part) {
171       $mimetype = strtolower($part->ctype_primary . '/' . $part->ctype_secondary);
172       if ($mimetype == 'text/html') {
173         return $this->imap->get_message_part($this->uid, $mime_id, $part);
174       }
175     }
176   }
177
178
179   /**
180    * Return the first text part of this message
181    *
182    * @return string Plain text message/part content
183    */
184   function first_text_part()
185     {
186     // no message structure, return complete body
187     if (empty($this->parts))
188       return $this->body;
189       
190     $out = null;
191
192     // check all message parts
193     foreach ($this->mime_parts as $mime_id => $part) {
194       $mimetype = strtolower($part->ctype_primary . '/' . $part->ctype_secondary);
195
196       if ($mimetype == 'text/plain') {
197         $out = $this->imap->get_message_part($this->uid, $mime_id, $part);
198         break;
199       }
200       else if ($mimetype == 'text/html') {
201         $html_part = $this->imap->get_message_part($this->uid, $mime_id, $part);
202
203         // remove special chars encoding
204         $trans = array_flip(get_html_translation_table(HTML_ENTITIES));
205         $html_part = strtr($html_part, $trans);
206
207         // create instance of html2text class
208         $txt = new html2text($html_part);
209         $out = $txt->get_text();
210         break;
211       }
212     }
213
214     return $out;
215   }
216
217
218   /**
219    * Raad the message structure returend by the IMAP server
220    * and build flat lists of content parts and attachments
221    *
222    * @param object rcube_message_part Message structure node
223    * @param bool  True when called recursively
224    */
225   private function parse_structure($structure, $recursive = false)
226   {
227     $message_ctype_primary = strtolower($structure->ctype_primary);
228     $message_ctype_secondary = strtolower($structure->ctype_secondary);
229
230     // show message headers
231     if ($recursive && is_array($structure->headers) && isset($structure->headers['subject'])) {
232       $c = new stdClass;
233       $c->type = 'headers';
234       $c->headers = &$structure->headers;
235       $this->parts[] = $c;
236     }
237
238     // print body if message doesn't have multiple parts
239     if ($message_ctype_primary == 'text' && !$recursive) {
240       $structure->type = 'content';
241       $this->parts[] = &$structure;
242     }
243     // the same for pgp signed messages
244     else if ($message_ctype_primary == 'application' && $message_ctype_secondary == 'pgp' && !$recursive) {
245       $structure->type = 'content';
246       $this->parts[] = &$structure;
247     }
248     // message contains alternative parts
249     else if ($message_ctype_primary == 'multipart' && ($message_ctype_secondary == 'alternative') && is_array($structure->parts)) {
250       // get html/plaintext parts
251       $plain_part = $html_part = $print_part = $related_part = null;
252
253       foreach ($structure->parts as $p => $sub_part) {
254         $rel_parts = $attachmnts = null;
255         $sub_ctype_primary = strtolower($sub_part->ctype_primary);
256         $sub_ctype_secondary = strtolower($sub_part->ctype_secondary);
257         
258         // check if sub part is 
259         if ($sub_ctype_primary=='text' && $sub_ctype_secondary=='plain')
260           $plain_part = $p;
261         else if ($sub_ctype_primary=='text' && $sub_ctype_secondary=='html')
262           $html_part = $p;
263         else if ($sub_ctype_primary=='text' && $sub_ctype_secondary=='enriched')
264           $enriched_part = $p;
265         else if ($sub_ctype_primary=='multipart' && in_array($sub_ctype_secondary, array('related', 'mixed', 'alternative')))
266           $related_part = $p;
267       }
268
269       // parse related part (alternative part could be in here)
270       if ($related_part !== null && !$this->parse_alternative) {
271         $this->parse_alternative = true;
272         $this->parse_structure($structure->parts[$related_part], true);
273         $this->parse_alternative = false;
274         
275         // if plain part was found, we should unset it if html is preferred
276         if ($this->opt['prefer_html'] && count($this->parts))
277           $plain_part = null;
278       }
279
280       // choose html/plain part to print
281       if ($html_part !== null && $this->opt['prefer_html']) {
282         $print_part = &$structure->parts[$html_part];
283       }
284       else if ($enriched_part !== null) {
285         $print_part = &$structure->parts[$enriched_part];
286       }
287       else if ($plain_part !== null) {
288         $print_part = &$structure->parts[$plain_part];
289       }
290
291       // add the right message body
292       if (is_object($print_part)) {
293         $print_part->type = 'content';
294         $this->parts[] = $print_part;
295       }
296       // show plaintext warning
297       else if ($html_part !== null && empty($this->parts)) {
298         $c = new stdClass;
299         $c->type = 'content';
300         $c->body = rcube_label('htmlmessage');
301         $c->ctype_primary = 'text';
302         $c->ctype_secondary = 'plain';
303
304         $this->parts[] = $c;
305       }
306
307       // add html part as attachment
308       if ($html_part !== null && $structure->parts[$html_part] !== $print_part) {
309         $html_part = &$structure->parts[$html_part];
310         $html_part->filename = rcube_label('htmlmessage');
311         $html_part->mimetype = 'text/html';
312
313         $this->attachments[] = $html_part;
314       }
315     }
316     // this is an ecrypted message -> create a plaintext body with the according message
317     else if ($message_ctype_primary == 'multipart' && $message_ctype_secondary == 'encrypted') {
318       $p = new stdClass;
319       $p->type = 'content';
320       $p->ctype_primary = 'text';
321       $p->ctype_secondary = 'plain';
322       $p->body = rcube_label('encryptedmessage');
323       
324       $this->parts[] = $p;
325     }
326     // message contains multiple parts
327     else if (is_array($structure->parts) && !empty($structure->parts)) {
328       // iterate over parts
329       for ($i=0; $i < count($structure->parts); $i++) {
330         $mail_part = &$structure->parts[$i];
331         $primary_type = strtolower($mail_part->ctype_primary);
332         $secondary_type = strtolower($mail_part->ctype_secondary);
333
334         // multipart/alternative
335         if ($primary_type=='multipart') {
336           $this->parse_structure($mail_part, true);
337         }
338         // part text/[plain|html] OR message/delivery-status
339         else if (($primary_type == 'text' && ($secondary_type == 'plain' || $secondary_type == 'html') && $mail_part->disposition != 'attachment') ||
340                  ($primary_type == 'message' && ($secondary_type == 'delivery-status' || $secondary_type == 'disposition-notification'))) {
341           
342           // add text part if we're not in alternative mode or if it matches the prefs
343           if (!$this->parse_alternative ||
344               ($secondary_type == 'html' && $this->opt['prefer_html']) ||
345               ($secondary_type == 'plain' && !$this->opt['prefer_html'])) {
346             $mail_part->type = 'content';
347             $this->parts[] = $mail_part;
348           }
349
350           // list as attachment as well
351           if (!empty($mail_part->filename))
352             $this->attachments[] = $mail_part;
353         }
354         // part message/*
355         else if ($primary_type=='message') {
356           $this->parse_structure($mail_part, true);
357           
358           // list as attachment as well (mostly .eml)
359           if (!empty($mail_part->filename))
360             $this->attachments[] = $mail_part;
361         }
362         // ignore "virtual" protocol parts
363         else if ($primary_type == 'protocol')
364           continue;
365           
366         // part is Microsoft outlook TNEF (winmail.dat)
367         else if ($primary_type == 'application' && $secondary_type == 'ms-tnef') {
368           foreach ((array)$this->imap->tnef_decode($mail_part, $structure->headers['uid']) as $tnef_part) {
369             $this->mime_parts[$tnef_part->mime_id] = $tnef_part;
370             $this->attachments[] = $tnef_part;
371           }
372         }
373
374         // part is file/attachment
375         else if ($mail_part->disposition == 'attachment' || $mail_part->disposition == 'inline' ||
376                  $mail_part->headers['content-id'] || (empty($mail_part->disposition) && $mail_part->filename)) {
377           // skip apple resource forks
378           if ($message_ctype_secondary == 'appledouble' && $secondary_type == 'applefile')
379             continue;
380
381           // part belongs to a related message
382           if ($message_ctype_secondary == 'related') {
383             if ($mail_part->headers['content-id'])
384               $mail_part->content_id = preg_replace(array('/^</', '/>$/'), '', $mail_part->headers['content-id']);
385             if ($mail_part->headers['content-location'])
386               $mail_part->content_location = $mail_part->headers['content-base'] . $mail_part->headers['content-location'];
387             
388             if ($mail_part->content_id || $mail_part->content_location) {
389               $this->inline_parts[] = $mail_part;
390             }
391           }
392           // is regular attachment
393           else {
394             if (!$mail_part->filename)
395               $mail_part->filename = 'Part '.$mail_part->mime_id;
396             $this->attachments[] = $mail_part;
397           }
398         }
399       }
400
401       // if this was a related part try to resolve references
402       if ($message_ctype_secondary == 'related' && sizeof($this->inline_parts)) {
403         $a_replaces = array();
404
405         foreach ($this->inline_parts as $inline_object) {
406           $part_url = $this->get_part_url($inline_object->mime_id);
407           if ($inline_object->content_id)
408             $a_replaces['cid:'.$inline_object->content_id] = $part_url;
409           if ($inline_object->content_location)
410             $a_replaces[$inline_object->content_location] = $part_url;
411         }
412
413         // add replace array to each content part
414         // (will be applied later when part body is available)
415         foreach ($this->parts as $i => $part) {
416           if ($part->type == 'content')
417             $this->parts[$i]->replaces = $a_replaces;
418         }
419       }
420     }
421
422     // message is single part non-text
423     else if ($structure->filename) {
424       $this->attachments[] = $structure;
425     }
426   }
427
428
429   /**
430    * Fill aflat array with references to all parts, indexed by part numbers
431    *
432    * @param object rcube_message_part Message body structure
433    */
434   private function get_mime_numbers(&$part)
435   {
436     if (strlen($part->mime_id))
437       $this->mime_parts[$part->mime_id] = &$part;
438       
439     if (is_array($part->parts))
440       for ($i=0; $i<count($part->parts); $i++)
441         $this->get_mime_numbers($part->parts[$i]);
442   }
443
444
445 }
446