]> git.donarmstrong.com Git - roundcube.git/blob - program/include/rcube_message.php
Imported Upstream version 0.3
[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     // notify plugins and let them analyze this structured message object
89     $this->app->plugins->exec_hook('message_load', array('object' => $this));
90   }
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   {
102     $value = $this->headers->$name;
103     return $raw ? $value : $this->imap->decode_header($value);
104   }
105   
106   /**
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   }
116   
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
136    * @param resource File pointer to save the message part
137    * @return string Part content
138    */
139   public function get_part_content($mime_id, $fp=NULL)
140   {
141     if ($part = $this->mime_parts[$mime_id])
142       return $this->imap->get_message_part($this->uid, $mime_id, $part, NULL, $fp);
143     else
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') {
176         return $this->imap->get_message_part($this->uid, $mime_id, $part);
177       }
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    */
228   private function parse_structure($structure, $recursive = false)
229   {
230     $message_ctype_primary = strtolower($structure->ctype_primary);
231     $message_ctype_secondary = strtolower($structure->ctype_secondary);
232
233     // show message headers
234     if ($recursive && is_array($structure->headers) && isset($structure->headers['subject'])) {
235       $c = new stdClass;
236       $c->type = 'headers';
237       $c->headers = &$structure->headers;
238       $this->parts[] = $c;
239     }
240
241     // print body if message doesn't have multiple parts
242     if ($message_ctype_primary == 'text' && !$recursive) {
243       $structure->type = 'content';
244       $this->parts[] = &$structure;
245     }
246     // the same for pgp signed messages
247     else if ($message_ctype_primary == 'application' && $message_ctype_secondary == 'pgp' && !$recursive) {
248       $structure->type = 'content';
249       $this->parts[] = &$structure;
250     }
251     // message contains alternative parts
252     else if ($message_ctype_primary == 'multipart' && ($message_ctype_secondary == 'alternative') && is_array($structure->parts)) {
253       // get html/plaintext parts
254       $plain_part = $html_part = $print_part = $related_part = null;
255
256       foreach ($structure->parts as $p => $sub_part) {
257         $rel_parts = $attachmnts = null;
258         $sub_ctype_primary = strtolower($sub_part->ctype_primary);
259         $sub_ctype_secondary = strtolower($sub_part->ctype_secondary);
260         
261         // check if sub part is 
262         if ($sub_ctype_primary=='text' && $sub_ctype_secondary=='plain')
263           $plain_part = $p;
264         else if ($sub_ctype_primary=='text' && $sub_ctype_secondary=='html')
265           $html_part = $p;
266         else if ($sub_ctype_primary=='text' && $sub_ctype_secondary=='enriched')
267           $enriched_part = $p;
268         else if ($sub_ctype_primary=='multipart' && in_array($sub_ctype_secondary, array('related', 'mixed', 'alternative')))
269           $related_part = $p;
270       }
271
272       // parse related part (alternative part could be in here)
273       if ($related_part !== null && !$this->parse_alternative) {
274         $this->parse_alternative = true;
275         $this->parse_structure($structure->parts[$related_part], true);
276         $this->parse_alternative = false;
277         
278         // if plain part was found, we should unset it if html is preferred
279         if ($this->opt['prefer_html'] && count($this->parts))
280           $plain_part = null;
281       }
282
283       // choose html/plain part to print
284       if ($html_part !== null && $this->opt['prefer_html']) {
285         $print_part = &$structure->parts[$html_part];
286       }
287       else if ($enriched_part !== null) {
288         $print_part = &$structure->parts[$enriched_part];
289       }
290       else if ($plain_part !== null) {
291         $print_part = &$structure->parts[$plain_part];
292       }
293
294       // add the right message body
295       if (is_object($print_part)) {
296         $print_part->type = 'content';
297         $this->parts[] = $print_part;
298       }
299       // show plaintext warning
300       else if ($html_part !== null && empty($this->parts)) {
301         $c = new stdClass;
302         $c->type = 'content';
303         $c->body = rcube_label('htmlmessage');
304         $c->ctype_primary = 'text';
305         $c->ctype_secondary = 'plain';
306
307         $this->parts[] = $c;
308       }
309
310       // add html part as attachment
311       if ($html_part !== null && $structure->parts[$html_part] !== $print_part) {
312         $html_part = &$structure->parts[$html_part];
313         $html_part->filename = rcube_label('htmlmessage');
314         $html_part->mimetype = 'text/html';
315
316         $this->attachments[] = $html_part;
317       }
318     }
319     // this is an ecrypted message -> create a plaintext body with the according message
320     else if ($message_ctype_primary == 'multipart' && $message_ctype_secondary == 'encrypted') {
321       $p = new stdClass;
322       $p->type = 'content';
323       $p->ctype_primary = 'text';
324       $p->ctype_secondary = 'plain';
325       $p->body = rcube_label('encryptedmessage');
326       $p->size = strlen($p->body);
327       
328       // maybe some plugins are able to decode this encrypted message part
329       $data = $this->app->plugins->exec_hook('message_part_encrypted', array('object' => $this, 'struct' => $structure, 'part' => $p));
330       if (is_array($data['parts'])) {
331         $this->parts = array_merge($this->parts, $data['parts']);
332       }
333       else if ($data['part']) {
334         $this->parts[] = $p;
335       }
336     }
337     // message contains multiple parts
338     else if (is_array($structure->parts) && !empty($structure->parts)) {
339       // iterate over parts
340       for ($i=0; $i < count($structure->parts); $i++) {
341         $mail_part = &$structure->parts[$i];
342         $primary_type = strtolower($mail_part->ctype_primary);
343         $secondary_type = strtolower($mail_part->ctype_secondary);
344
345         // multipart/alternative
346         if ($primary_type=='multipart') {
347           $this->parse_structure($mail_part, true);
348         }
349         // part text/[plain|html] OR message/delivery-status
350         else if (($primary_type == 'text' && ($secondary_type == 'plain' || $secondary_type == 'html') && $mail_part->disposition != 'attachment') ||
351                  ($primary_type == 'message' && ($secondary_type == 'delivery-status' || $secondary_type == 'disposition-notification'))) {
352
353           // add text part if we're not in alternative mode or if it matches the prefs
354           if (!$this->parse_alternative ||
355               ($secondary_type == 'html' && $this->opt['prefer_html']) ||
356               ($secondary_type == 'plain' && !$this->opt['prefer_html'])) {
357             $mail_part->type = 'content';
358             $this->parts[] = $mail_part;
359           }
360
361           // list as attachment as well
362           if (!empty($mail_part->filename))
363             $this->attachments[] = $mail_part;
364         }
365         // part message/*
366         else if ($primary_type=='message') {
367           $this->parse_structure($mail_part, true);
368           
369           // list as attachment as well (mostly .eml)
370           if (!empty($mail_part->filename))
371             $this->attachments[] = $mail_part;
372         }
373         // ignore "virtual" protocol parts
374         else if ($primary_type == 'protocol')
375           continue;
376           
377         // part is Microsoft Outlook TNEF (winmail.dat)
378         else if ($primary_type == 'application' && $secondary_type == 'ms-tnef') {
379           foreach ((array)$this->imap->tnef_decode($mail_part, $structure->headers['uid']) as $tnef_part) {
380             $this->mime_parts[$tnef_part->mime_id] = $tnef_part;
381             $this->attachments[] = $tnef_part;
382           }
383         }
384
385         // part is a file/attachment
386         else if (preg_match('/^(inline|attach)/', $mail_part->disposition) ||
387                  $mail_part->headers['content-id'] || (empty($mail_part->disposition) && $mail_part->filename)) {
388
389           // skip apple resource forks
390           if ($message_ctype_secondary == 'appledouble' && $secondary_type == 'applefile')
391             continue;
392
393           // part belongs to a related message and is linked
394           if ($message_ctype_secondary == 'related'
395               && preg_match('!^image/!', $mail_part->mimetype)
396               && ($mail_part->headers['content-id'] || $mail_part->headers['content-location'])) {
397             if ($mail_part->headers['content-id'])
398               $mail_part->content_id = preg_replace(array('/^</', '/>$/'), '', $mail_part->headers['content-id']);
399             if ($mail_part->headers['content-location'])
400               $mail_part->content_location = $mail_part->headers['content-base'] . $mail_part->headers['content-location'];
401               
402             $this->inline_parts[] = $mail_part;
403           }
404           // is a regular attachment
405           else if (preg_match('!^[a-z]+/[a-z0-9-.]+$!i', $mail_part->mimetype)) {
406             if (!$mail_part->filename)
407               $mail_part->filename = 'Part '.$mail_part->mime_id;
408             $this->attachments[] = $mail_part;
409           }
410         }
411       }
412
413       // if this was a related part try to resolve references
414       if ($message_ctype_secondary == 'related' && sizeof($this->inline_parts)) {
415         $a_replaces = array();
416
417         foreach ($this->inline_parts as $inline_object) {
418           $part_url = $this->get_part_url($inline_object->mime_id);
419           if ($inline_object->content_id)
420             $a_replaces['cid:'.$inline_object->content_id] = $part_url;
421           if ($inline_object->content_location)
422             $a_replaces[$inline_object->content_location] = $part_url;
423         }
424
425         // add replace array to each content part
426         // (will be applied later when part body is available)
427         foreach ($this->parts as $i => $part) {
428           if ($part->type == 'content')
429             $this->parts[$i]->replaces = $a_replaces;
430         }
431       }
432     }
433
434     // message is single part non-text
435     else if ($structure->filename) {
436       $this->attachments[] = $structure;
437     }
438   }
439
440
441   /**
442    * Fill aflat array with references to all parts, indexed by part numbers
443    *
444    * @param object rcube_message_part Message body structure
445    */
446   private function get_mime_numbers(&$part)
447   {
448     if (strlen($part->mime_id))
449       $this->mime_parts[$part->mime_id] = &$part;
450       
451     if (is_array($part->parts))
452       for ($i=0; $i<count($part->parts); $i++)
453         $this->get_mime_numbers($part->parts[$i]);
454   }
455
456
457 }
458