4 +-----------------------------------------------------------------------+
5 | program/include/rcube_message.php |
7 | This file is part of the RoundCube Webmail client |
8 | Copyright (C) 2008-2009, RoundCube Dev. - Switzerland |
9 | Licensed under the GNU GPL |
12 | Logical representation of a mail message with all its data |
13 | and related functions |
14 +-----------------------------------------------------------------------+
15 | Author: Thomas Bruederli <roundcube@gmail.com> |
16 +-----------------------------------------------------------------------+
18 $Id: rcube_imap.php 1344 2008-04-30 08:21:42Z thomasb $
24 * Logical representation of a mail message with all its data
25 * and related functions
28 * @author Thomas Bruederli <roundcube@gmail.com>
34 private $opt = array();
35 private $inline_parts = array();
36 private $parse_alternative = false;
41 public $parts = array();
42 public $mime_parts = array();
43 public $attachments = array();
45 public $sender = null;
46 public $is_safe = false;
52 * Provide a uid, and parse message structure.
54 * @param string $uid The message UID.
56 * @uses rcmail::get_instance()
57 * @uses rcube_imap::decode_mime_string()
58 * @uses self::set_safe()
60 * @see self::$app, self::$imap, self::$opt, self::$structure
62 function __construct($uid)
64 $this->app = rcmail::get_instance();
65 $this->imap = $this->app->imap;
68 $this->headers = $this->imap->get_headers($uid, NULL, true, true);
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));
73 $this->set_safe((intval($_GET['_safe']) || $_SESSION['safe_messages'][$uid]));
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))
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);
85 $this->body = $this->imap->get_body($uid);
91 * Return a (decoded) message header
93 * @param string Header name
94 * @param bool Don't mime-decode the value
95 * @return string Header value
97 public function get_header($name, $raw = false)
99 $value = $this->headers->$name;
100 return $raw ? $value : $this->imap->decode_header($value);
104 * Set is_safe var and session data
106 * @param bool enable/disable
108 public function set_safe($safe = true)
110 $this->is_safe = $safe;
111 $_SESSION['safe_messages'][$this->uid] = $this->is_safe;
115 * Compose a valid URL for getting a message part
117 * @param string Part MIME-ID
118 * @return string URL or false if part does not exist
120 public function get_part_url($mime_id)
122 if ($this->mime_parts[$mime_id])
123 return $this->opt['get_url'] . "&_part=" . $mime_id;
130 * Get content of a specific part of this message
132 * @param string Part MIME-ID
133 * @param resource File pointer to save the message part
134 * @return string Part content
136 public function get_part_content($mime_id, $fp=NULL)
138 if ($part = $this->mime_parts[$mime_id])
139 return $this->imap->get_message_part($this->uid, $mime_id, $part, NULL, $fp);
146 * Determine if the message contains a HTML part
148 * @return bool True if a HTML is available, False if not
150 function has_html_part()
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')
163 * Return the first HTML part of this message
165 * @return string HTML message part content
167 function first_html_part()
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);
180 * Return the first text part of this message
182 * @return string Plain text message/part content
184 function first_text_part()
186 // no message structure, return complete body
187 if (empty($this->parts))
192 // check all message parts
193 foreach ($this->mime_parts as $mime_id => $part) {
194 $mimetype = strtolower($part->ctype_primary . '/' . $part->ctype_secondary);
196 if ($mimetype == 'text/plain') {
197 $out = $this->imap->get_message_part($this->uid, $mime_id, $part);
200 else if ($mimetype == 'text/html') {
201 $html_part = $this->imap->get_message_part($this->uid, $mime_id, $part);
203 // remove special chars encoding
204 $trans = array_flip(get_html_translation_table(HTML_ENTITIES));
205 $html_part = strtr($html_part, $trans);
207 // create instance of html2text class
208 $txt = new html2text($html_part);
209 $out = $txt->get_text();
219 * Raad the message structure returend by the IMAP server
220 * and build flat lists of content parts and attachments
222 * @param object rcube_message_part Message structure node
223 * @param bool True when called recursively
225 private function parse_structure($structure, $recursive = false)
227 $message_ctype_primary = strtolower($structure->ctype_primary);
228 $message_ctype_secondary = strtolower($structure->ctype_secondary);
230 // show message headers
231 if ($recursive && is_array($structure->headers) && isset($structure->headers['subject'])) {
233 $c->type = 'headers';
234 $c->headers = &$structure->headers;
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;
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;
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;
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);
258 // check if sub part is
259 if ($sub_ctype_primary=='text' && $sub_ctype_secondary=='plain')
261 else if ($sub_ctype_primary=='text' && $sub_ctype_secondary=='html')
263 else if ($sub_ctype_primary=='text' && $sub_ctype_secondary=='enriched')
265 else if ($sub_ctype_primary=='multipart' && in_array($sub_ctype_secondary, array('related', 'mixed', 'alternative')))
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;
275 // if plain part was found, we should unset it if html is preferred
276 if ($this->opt['prefer_html'] && count($this->parts))
280 // choose html/plain part to print
281 if ($html_part !== null && $this->opt['prefer_html']) {
282 $print_part = &$structure->parts[$html_part];
284 else if ($enriched_part !== null) {
285 $print_part = &$structure->parts[$enriched_part];
287 else if ($plain_part !== null) {
288 $print_part = &$structure->parts[$plain_part];
291 // add the right message body
292 if (is_object($print_part)) {
293 $print_part->type = 'content';
294 $this->parts[] = $print_part;
296 // show plaintext warning
297 else if ($html_part !== null && empty($this->parts)) {
299 $c->type = 'content';
300 $c->body = rcube_label('htmlmessage');
301 $c->ctype_primary = 'text';
302 $c->ctype_secondary = 'plain';
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';
313 $this->attachments[] = $html_part;
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') {
319 $p->type = 'content';
320 $p->ctype_primary = 'text';
321 $p->ctype_secondary = 'plain';
322 $p->body = rcube_label('encryptedmessage');
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);
334 // multipart/alternative
335 if ($primary_type=='multipart') {
336 $this->parse_structure($mail_part, true);
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'))) {
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;
350 // list as attachment as well
351 if (!empty($mail_part->filename))
352 $this->attachments[] = $mail_part;
355 else if ($primary_type=='message') {
356 $this->parse_structure($mail_part, true);
358 // list as attachment as well (mostly .eml)
359 if (!empty($mail_part->filename))
360 $this->attachments[] = $mail_part;
362 // ignore "virtual" protocol parts
363 else if ($primary_type == 'protocol')
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;
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')
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'];
388 if ($mail_part->content_id || $mail_part->content_location) {
389 $this->inline_parts[] = $mail_part;
392 // is regular attachment
394 if (!$mail_part->filename)
395 $mail_part->filename = 'Part '.$mail_part->mime_id;
396 $this->attachments[] = $mail_part;
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();
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;
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;
422 // message is single part non-text
423 else if ($structure->filename) {
424 $this->attachments[] = $structure;
430 * Fill aflat array with references to all parts, indexed by part numbers
432 * @param object rcube_message_part Message body structure
434 private function get_mime_numbers(&$part)
436 if (strlen($part->mime_id))
437 $this->mime_parts[$part->mime_id] = &$part;
439 if (is_array($part->parts))
440 for ($i=0; $i<count($part->parts); $i++)
441 $this->get_mime_numbers($part->parts[$i]);