]> git.donarmstrong.com Git - roundcube.git/blob - program/include/rcube_message.php
Imported Upstream version 0.2~alpha
[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, 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 $is_safe = false;
46   
47   
48   function __construct($uid)
49   {
50     $this->app = rcmail::get_instance();
51     $this->imap = $this->app->imap;
52     
53     $this->uid = $uid;
54     $this->headers = $this->imap->get_headers($uid);
55     $this->subject = rcube_imap::decode_mime_string($this->headers->subject, $this->headers->charset);
56     
57     $this->is_safe = (intval($_GET['_safe']) || $_SESSION['safe_messages'][$uid]) ? true : false;
58     $_SESSION['safe_messages'][$uid] = $this->is_safe;
59     
60     $this->opt = array(
61       'safe' => $this->is_safe,
62       'prefer_html' => $this->app->config->get('prefer_html'),
63       'get_url' => rcmail_url('get', array('_mbox' => $this->imap->get_mailbox_name(), '_uid' => $uid))
64     );
65     
66     if ($this->structure = $this->imap->get_structure($uid)) {
67       $this->get_mime_numbers($this->structure);
68       $this->parse_structure($this->structure);
69     }
70     else {
71       $this->body = $this->imap->get_body($uid);
72     }
73   }
74   
75   
76   /**
77    * Return a (decoded) message header
78    *
79    * @param string Header name
80    * @param bool   Don't mime-decode the value
81    * @return string Header value
82    */
83   public function get_header($name, $raw = false)
84   {
85     $value = $this->headers->$name;
86     return $raw ? $value : $this->imap->decode_header($value);
87   }
88   
89   
90   /**
91    * Compose a valid URL for getting a message part
92    *
93    * @param string Part MIME-ID
94    * @return string URL or false if part does not exist
95    */
96   public function get_part_url($mime_id)
97   {
98     if ($this->mime_parts[$mime_id])
99       return $this->opt['get_url'] . "&_part=" . $mime_id;
100     else
101       return false;
102   }
103   
104   
105   /**
106    * Get content of a specific part of this message
107    *
108    * @param string Part MIME-ID
109    * @return string Part content
110    */
111   public function get_part_content($mime_id)
112   {
113     if ($part = $this->mime_parts[$mime_id])
114       return $this->imap->get_message_part($this->uid, $mime_id, $part);
115     else
116       return null;
117   }
118   
119   
120   /**
121    * Determine if the message contains a HTML part
122    *
123    * @return bool True if a HTML is available, False if not
124    */
125   function has_html_part()
126   {
127      // check all message parts
128      foreach ($this->parts as $pid => $part) {
129         $mimetype = strtolower($part->ctype_primary . '/' . $part->ctype_secondary);
130         if ($mimetype == 'text/html')
131            return true;
132      }
133
134      return false;
135   }
136
137   /**
138    * Return the first HTML part of this message
139    *
140    * @return string HTML message part content
141    */
142   function first_html_part()
143     {
144     $html_part = null;
145
146     // check all message parts
147     foreach ($this->mime_parts as $mime_id => $part) {
148       $mimetype = strtolower($part->ctype_primary . '/' . $part->ctype_secondary);
149       if ($mimetype == 'text/html') {
150         $html_part = $this->imap->get_message_part($this->uid, $mime_id, $part);
151       }
152     }
153
154     return $html_part;
155   }
156
157
158   /**
159    * Return the first text part of this message
160    *
161    * @return string Plain text message/part content
162    */
163   function first_text_part()
164     {
165     // no message structure, return complete body
166     if (empty($this->parts))
167       return $this->body;
168       
169     $out = 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
175       if ($mimetype == 'text/plain') {
176         $out = $this->imap->get_message_part($this->uid, $mime_id, $part);
177         break;
178       }
179       else if ($mimetype == 'text/html') {
180         $html_part = $this->imap->get_message_part($this->uid, $mime_id, $part);
181
182         // remove special chars encoding
183         $trans = array_flip(get_html_translation_table(HTML_ENTITIES));
184         $html_part = strtr($html_part, $trans);
185
186         // create instance of html2text class
187         $txt = new html2text($html_part);
188         $out = $txt->get_text();
189         break;
190       }
191     }
192
193     return $out;
194   }
195
196
197   /**
198    * Raad the message structure returend by the IMAP server
199    * and build flat lists of content parts and attachments
200    *
201    * @param object rcube_message_part Message structure node
202    * @param bool  True when called recursively
203    */
204   private function parse_structure($structure, $recursive = false)
205   {
206     $message_ctype_primary = strtolower($structure->ctype_primary);
207     $message_ctype_secondary = strtolower($structure->ctype_secondary);
208
209     // show message headers
210     if ($recursive && is_array($structure->headers) && isset($structure->headers['subject'])) {
211       $c = new stdClass;
212       $c->type = 'headers';
213       $c->headers = &$structure->headers;
214       $this->parts[] = $c;
215     }
216
217     // print body if message doesn't have multiple parts
218     if ($message_ctype_primary == 'text' && !$recursive) {
219       $structure->type = 'content';
220       $this->parts[] = &$structure;
221     }
222     // message contains alternative parts
223     else if ($message_ctype_primary == 'multipart' && ($message_ctype_secondary == 'alternative') && is_array($structure->parts)) {
224       // get html/plaintext parts
225       $plain_part = $html_part = $print_part = $related_part = null;
226
227       foreach ($structure->parts as $p => $sub_part) {
228         $rel_parts = $attachmnts = null;
229         $sub_ctype_primary = strtolower($sub_part->ctype_primary);
230         $sub_ctype_secondary = strtolower($sub_part->ctype_secondary);
231         
232         // check if sub part is 
233         if ($sub_ctype_primary=='text' && $sub_ctype_secondary=='plain')
234           $plain_part = $p;
235         else if ($sub_ctype_primary=='text' && $sub_ctype_secondary=='html')
236           $html_part = $p;
237         else if ($sub_ctype_primary=='text' && $sub_ctype_secondary=='enriched')
238           $enriched_part = $p;
239         else if ($sub_ctype_primary=='multipart' && ($sub_ctype_secondary=='related' || $sub_ctype_secondary=='mixed'))
240           $related_part = $p;
241       }
242
243       // parse related part (alternative part could be in here)
244       if ($related_part !== null && !$this->parse_alternative) {
245         $this->parse_alternative = true;
246         $this->parse_structure($structure->parts[$related_part], true);
247         $this->parse_alternative = false;
248         
249         // if plain part was found, we should unset it if html is preferred
250         if ($this->opt['prefer_html'] && count($this->parts))
251           $plain_part = null;
252       }
253
254       // choose html/plain part to print
255       if ($html_part !== null && $this->opt['prefer_html']) {
256         $print_part = &$structure->parts[$html_part];
257       }
258       else if ($enriched_part !== null) {
259         $print_part = &$structure->parts[$enriched_part];
260       }
261       else if ($plain_part !== null) {
262         $print_part = &$structure->parts[$plain_part];
263       }
264
265       // add the right message body
266       if (is_object($print_part)) {
267         $print_part->type = 'content';
268         $this->parts[] = $print_part;
269       }
270       // show plaintext warning
271       else if ($html_part !== nullL && empty($this->parts)) {
272         $c = new stdClass;
273         $c->type = 'content';
274         $c->body = rcube_label('htmlmessage');
275         $c->ctype_primary = 'text';
276         $c->ctype_secondary = 'plain';
277
278         $this->parts[] = $c;
279       }
280
281       // add html part as attachment
282       if ($html_part !== null && $structure->parts[$html_part] !== $print_part) {
283         $html_part = &$structure->parts[$html_part];
284         $html_part->filename = rcube_label('htmlmessage');
285         $html_part->mimetype = 'text/html';
286
287         $this->attachments[] = $html_part;
288       }
289     }
290     // this is an ecrypted message -> create a plaintext body with the according message
291     else if ($message_ctype_primary == 'multipart' && $message_ctype_secondary == 'encrypted') {
292       $p = new stdClass;
293       $p->type = 'content';
294       $p->ctype_primary = 'text';
295       $p->ctype_secondary = 'plain';
296       $p->body = rcube_label('encryptedmessage');
297       
298       $this->parts[] = $p;
299     }
300     // message contains multiple parts
301     else if (is_array($structure->parts) && !empty($structure->parts)) {
302       // iterate over parts
303       for ($i=0; $i < count($structure->parts); $i++) {
304         $mail_part = &$structure->parts[$i];
305         $primary_type = strtolower($mail_part->ctype_primary);
306         $secondary_type = strtolower($mail_part->ctype_secondary);
307
308         // multipart/alternative
309         if ($primary_type=='multipart') {
310           $this->parse_structure($mail_part, true);
311         }
312         // part text/[plain|html] OR message/delivery-status
313         else if (($primary_type == 'text' && ($secondary_type == 'plain' || $secondary_type == 'html') && $mail_part->disposition != 'attachment') ||
314                  ($primary_type == 'message' && ($secondary_type == 'delivery-status' || $secondary_type == 'disposition-notification'))) {
315           
316           // add text part if we're not in alternative mode or if it matches the prefs
317           if (!$this->parse_alternative ||
318               ($secondary_type == 'html' && $this->opt['prefer_html']) ||
319               ($secondary_type == 'plain' && !$this->opt['prefer_html'])) {
320             $mail_part->type = 'content';
321             $this->parts[] = $mail_part;
322           }
323           
324           // list as attachment as well
325           if (!empty($mail_part->filename))
326             $this->attachments[] = $mail_part;
327         }
328         // part message/*
329         else if ($primary_type=='message') {
330           $this->parse_structure($mail_part, true);
331         }
332         // ignore "virtual" protocol parts
333         else if ($primary_type == 'protocol')
334           continue;
335
336         // part is file/attachment
337         else if ($mail_part->disposition == 'attachment' || $mail_part->disposition == 'inline' ||
338                  $mail_part->headers['content-id'] || (empty($mail_part->disposition) && $mail_part->filename)) {
339           // skip apple resource forks
340           if ($message_ctype_secondary == 'appledouble' && $secondary_type == 'applefile')
341             continue;
342
343           // part belongs to a related message
344           if ($message_ctype_secondary == 'related' && $mail_part->headers['content-id']) {
345             $mail_part->content_id = preg_replace(array('/^</', '/>$/'), '', $mail_part->headers['content-id']);
346             $this->inline_parts[] = $mail_part;
347           }
348           // is regular attachment
349           else {
350             if (!$mail_part->filename)
351               $mail_part->filename = 'Part '.$mail_part->mime_id;
352             $this->attachments[] = $mail_part;
353           }
354         }
355       }
356
357       // if this was a related part try to resolve references
358       if ($message_ctype_secondary == 'related' && sizeof($this->inline_parts)) {
359         $a_replaces = array();
360
361         foreach ($this->inline_parts as $inline_object) {
362           $a_replaces['cid:'.$inline_object->content_id] = $this->get_part_url($inline_object->mime_id);
363         }
364
365         // add replace array to each content part
366         // (will be applied later when part body is available)
367         foreach ($this->parts as $i => $part) {
368           if ($part->type == 'content')
369             $this->parts[$i]->replaces = $a_replaces;
370         }
371       }
372     }
373
374     // message is single part non-text
375     else if ($structure->filename) {
376       $this->attachments[] = $structure;
377     }
378   }
379
380
381   /**
382    * Fill aflat array with references to all parts, indexed by part numbers
383    *
384    * @param object rcube_message_part Message body structure
385    */
386   private function get_mime_numbers(&$part)
387   {
388     if (strlen($part->mime_id))
389       $this->mime_parts[$part->mime_id] = &$part;
390       
391     if (is_array($part->parts))
392       for ($i=0; $i<count($part->parts); $i++)
393         $this->get_mime_numbers($part->parts[$i]);
394   }
395
396
397 }
398