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);
88 // notify plugins and let them analyze this structured message object
89 $this->app->plugins->exec_hook('message_load', array('object' => $this));
94 * Return a (decoded) message header
96 * @param string Header name
97 * @param bool Don't mime-decode the value
98 * @return string Header value
100 public function get_header($name, $raw = false)
102 $value = $this->headers->$name;
103 return $raw ? $value : $this->imap->decode_header($value);
107 * Set is_safe var and session data
109 * @param bool enable/disable
111 public function set_safe($safe = true)
113 $this->is_safe = $safe;
114 $_SESSION['safe_messages'][$this->uid] = $this->is_safe;
118 * Compose a valid URL for getting a message part
120 * @param string Part MIME-ID
121 * @return string URL or false if part does not exist
123 public function get_part_url($mime_id)
125 if ($this->mime_parts[$mime_id])
126 return $this->opt['get_url'] . "&_part=" . $mime_id;
133 * Get content of a specific part of this message
135 * @param string Part MIME-ID
136 * @param resource File pointer to save the message part
137 * @return string Part content
139 public function get_part_content($mime_id, $fp=NULL)
141 if ($part = $this->mime_parts[$mime_id])
142 return $this->imap->get_message_part($this->uid, $mime_id, $part, NULL, $fp);
149 * Determine if the message contains a HTML part
151 * @return bool True if a HTML is available, False if not
153 function has_html_part()
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')
166 * Return the first HTML part of this message
168 * @return string HTML message part content
170 function first_html_part()
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);
183 * Return the first text part of this message
185 * @return string Plain text message/part content
187 function first_text_part()
189 // no message structure, return complete body
190 if (empty($this->parts))
195 // check all message parts
196 foreach ($this->mime_parts as $mime_id => $part) {
197 $mimetype = strtolower($part->ctype_primary . '/' . $part->ctype_secondary);
199 if ($mimetype == 'text/plain') {
200 $out = $this->imap->get_message_part($this->uid, $mime_id, $part);
203 else if ($mimetype == 'text/html') {
204 $html_part = $this->imap->get_message_part($this->uid, $mime_id, $part);
206 // remove special chars encoding
207 $trans = array_flip(get_html_translation_table(HTML_ENTITIES));
208 $html_part = strtr($html_part, $trans);
210 // create instance of html2text class
211 $txt = new html2text($html_part);
212 $out = $txt->get_text();
222 * Raad the message structure returend by the IMAP server
223 * and build flat lists of content parts and attachments
225 * @param object rcube_message_part Message structure node
226 * @param bool True when called recursively
228 private function parse_structure($structure, $recursive = false)
230 $message_ctype_primary = strtolower($structure->ctype_primary);
231 $message_ctype_secondary = strtolower($structure->ctype_secondary);
233 // show message headers
234 if ($recursive && is_array($structure->headers) && isset($structure->headers['subject'])) {
236 $c->type = 'headers';
237 $c->headers = &$structure->headers;
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;
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;
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;
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);
261 // check if sub part is
262 if ($sub_ctype_primary=='text' && $sub_ctype_secondary=='plain')
264 else if ($sub_ctype_primary=='text' && $sub_ctype_secondary=='html')
266 else if ($sub_ctype_primary=='text' && $sub_ctype_secondary=='enriched')
268 else if ($sub_ctype_primary=='multipart' && in_array($sub_ctype_secondary, array('related', 'mixed', 'alternative')))
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;
278 // if plain part was found, we should unset it if html is preferred
279 if ($this->opt['prefer_html'] && count($this->parts))
283 // choose html/plain part to print
284 if ($html_part !== null && $this->opt['prefer_html']) {
285 $print_part = &$structure->parts[$html_part];
287 else if ($enriched_part !== null) {
288 $print_part = &$structure->parts[$enriched_part];
290 else if ($plain_part !== null) {
291 $print_part = &$structure->parts[$plain_part];
294 // add the right message body
295 if (is_object($print_part)) {
296 $print_part->type = 'content';
297 $this->parts[] = $print_part;
299 // show plaintext warning
300 else if ($html_part !== null && empty($this->parts)) {
302 $c->type = 'content';
303 $c->body = rcube_label('htmlmessage');
304 $c->ctype_primary = 'text';
305 $c->ctype_secondary = 'plain';
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';
316 $this->attachments[] = $html_part;
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') {
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);
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']);
333 else if ($data['part']) {
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);
345 // multipart/alternative
346 if ($primary_type=='multipart') {
347 $this->parse_structure($mail_part, true);
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'))) {
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;
361 // list as attachment as well
362 if (!empty($mail_part->filename))
363 $this->attachments[] = $mail_part;
366 else if ($primary_type=='message') {
367 $this->parse_structure($mail_part, true);
369 // list as attachment as well (mostly .eml)
370 if (!empty($mail_part->filename))
371 $this->attachments[] = $mail_part;
373 // ignore "virtual" protocol parts
374 else if ($primary_type == 'protocol')
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;
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)) {
389 // skip apple resource forks
390 if ($message_ctype_secondary == 'appledouble' && $secondary_type == 'applefile')
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'];
402 $this->inline_parts[] = $mail_part;
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;
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();
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;
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;
434 // message is single part non-text
435 else if ($structure->filename) {
436 $this->attachments[] = $structure;
442 * Fill aflat array with references to all parts, indexed by part numbers
444 * @param object rcube_message_part Message body structure
446 private function get_mime_numbers(&$part)
448 if (strlen($part->mime_id))
449 $this->mime_parts[$part->mime_id] = &$part;
451 if (is_array($part->parts))
452 for ($i=0; $i<count($part->parts); $i++)
453 $this->get_mime_numbers($part->parts[$i]);