4 +-----------------------------------------------------------------------+
5 | program/include/rcube_message.php |
7 | This file is part of the RoundCube Webmail client |
8 | Copyright (C) 2008, 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);
69 $this->subject = rcube_imap::decode_mime_string($this->headers->subject, $this->headers->charset);
70 list(, $this->sender) = each($this->imap->decode_address_list($this->headers->from));
72 $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)) {
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()
171 // check all message parts
172 foreach ($this->mime_parts as $mime_id => $part) {
173 $mimetype = strtolower($part->ctype_primary . '/' . $part->ctype_secondary);
174 if ($mimetype == 'text/html') {
175 $html_part = $this->imap->get_message_part($this->uid, $mime_id, $part);
184 * Return the first text part of this message
186 * @return string Plain text message/part content
188 function first_text_part()
190 // no message structure, return complete body
191 if (empty($this->parts))
196 // check all message parts
197 foreach ($this->mime_parts as $mime_id => $part) {
198 $mimetype = strtolower($part->ctype_primary . '/' . $part->ctype_secondary);
200 if ($mimetype == 'text/plain') {
201 $out = $this->imap->get_message_part($this->uid, $mime_id, $part);
204 else if ($mimetype == 'text/html') {
205 $html_part = $this->imap->get_message_part($this->uid, $mime_id, $part);
207 // remove special chars encoding
208 $trans = array_flip(get_html_translation_table(HTML_ENTITIES));
209 $html_part = strtr($html_part, $trans);
211 // create instance of html2text class
212 $txt = new html2text($html_part);
213 $out = $txt->get_text();
223 * Raad the message structure returend by the IMAP server
224 * and build flat lists of content parts and attachments
226 * @param object rcube_message_part Message structure node
227 * @param bool True when called recursively
229 private function parse_structure($structure, $recursive = false)
231 $message_ctype_primary = strtolower($structure->ctype_primary);
232 $message_ctype_secondary = strtolower($structure->ctype_secondary);
234 // show message headers
235 if ($recursive && is_array($structure->headers) && isset($structure->headers['subject'])) {
237 $c->type = 'headers';
238 $c->headers = &$structure->headers;
242 // print body if message doesn't have multiple parts
243 if ($message_ctype_primary == 'text' && !$recursive) {
244 $structure->type = 'content';
245 $this->parts[] = &$structure;
247 // the same for pgp signed messages
248 else if ($message_ctype_primary == 'application' && $message_ctype_secondary == 'pgp' && !$recursive) {
249 $structure->type = 'content';
250 $this->parts[] = &$structure;
252 // message contains alternative parts
253 else if ($message_ctype_primary == 'multipart' && ($message_ctype_secondary == 'alternative') && is_array($structure->parts)) {
254 // get html/plaintext parts
255 $plain_part = $html_part = $print_part = $related_part = null;
257 foreach ($structure->parts as $p => $sub_part) {
258 $rel_parts = $attachmnts = null;
259 $sub_ctype_primary = strtolower($sub_part->ctype_primary);
260 $sub_ctype_secondary = strtolower($sub_part->ctype_secondary);
262 // check if sub part is
263 if ($sub_ctype_primary=='text' && $sub_ctype_secondary=='plain')
265 else if ($sub_ctype_primary=='text' && $sub_ctype_secondary=='html')
267 else if ($sub_ctype_primary=='text' && $sub_ctype_secondary=='enriched')
269 else if ($sub_ctype_primary=='multipart' && ($sub_ctype_secondary=='related' || $sub_ctype_secondary=='mixed'))
273 // parse related part (alternative part could be in here)
274 if ($related_part !== null && !$this->parse_alternative) {
275 $this->parse_alternative = true;
276 $this->parse_structure($structure->parts[$related_part], true);
277 $this->parse_alternative = false;
279 // if plain part was found, we should unset it if html is preferred
280 if ($this->opt['prefer_html'] && count($this->parts))
284 // choose html/plain part to print
285 if ($html_part !== null && $this->opt['prefer_html']) {
286 $print_part = &$structure->parts[$html_part];
288 else if ($enriched_part !== null) {
289 $print_part = &$structure->parts[$enriched_part];
291 else if ($plain_part !== null) {
292 $print_part = &$structure->parts[$plain_part];
295 // add the right message body
296 if (is_object($print_part)) {
297 $print_part->type = 'content';
298 $this->parts[] = $print_part;
300 // show plaintext warning
301 else if ($html_part !== null && empty($this->parts)) {
303 $c->type = 'content';
304 $c->body = rcube_label('htmlmessage');
305 $c->ctype_primary = 'text';
306 $c->ctype_secondary = 'plain';
311 // add html part as attachment
312 if ($html_part !== null && $structure->parts[$html_part] !== $print_part) {
313 $html_part = &$structure->parts[$html_part];
314 $html_part->filename = rcube_label('htmlmessage');
315 $html_part->mimetype = 'text/html';
317 $this->attachments[] = $html_part;
320 // this is an ecrypted message -> create a plaintext body with the according message
321 else if ($message_ctype_primary == 'multipart' && $message_ctype_secondary == 'encrypted') {
323 $p->type = 'content';
324 $p->ctype_primary = 'text';
325 $p->ctype_secondary = 'plain';
326 $p->body = rcube_label('encryptedmessage');
330 // message contains multiple parts
331 else if (is_array($structure->parts) && !empty($structure->parts)) {
332 // iterate over parts
333 for ($i=0; $i < count($structure->parts); $i++) {
334 $mail_part = &$structure->parts[$i];
335 $primary_type = strtolower($mail_part->ctype_primary);
336 $secondary_type = strtolower($mail_part->ctype_secondary);
338 // multipart/alternative
339 if ($primary_type=='multipart') {
340 $this->parse_structure($mail_part, true);
342 // part text/[plain|html] OR message/delivery-status
343 else if (($primary_type == 'text' && ($secondary_type == 'plain' || $secondary_type == 'html') && $mail_part->disposition != 'attachment') ||
344 ($primary_type == 'message' && ($secondary_type == 'delivery-status' || $secondary_type == 'disposition-notification'))) {
346 // add text part if we're not in alternative mode or if it matches the prefs
347 if (!$this->parse_alternative ||
348 ($secondary_type == 'html' && $this->opt['prefer_html']) ||
349 ($secondary_type == 'plain' && !$this->opt['prefer_html'])) {
350 $mail_part->type = 'content';
351 $this->parts[] = $mail_part;
354 // list as attachment as well
355 if (!empty($mail_part->filename))
356 $this->attachments[] = $mail_part;
359 else if ($primary_type=='message') {
360 $this->parse_structure($mail_part, true);
362 // list as attachment as well (mostly .eml)
363 if (!empty($mail_part->filename))
364 $this->attachments[] = $mail_part;
366 // ignore "virtual" protocol parts
367 else if ($primary_type == 'protocol')
370 // part is file/attachment
371 else if ($mail_part->disposition == 'attachment' || $mail_part->disposition == 'inline' ||
372 $mail_part->headers['content-id'] || (empty($mail_part->disposition) && $mail_part->filename)) {
373 // skip apple resource forks
374 if ($message_ctype_secondary == 'appledouble' && $secondary_type == 'applefile')
377 // part belongs to a related message
378 if ($message_ctype_secondary == 'related') {
379 if ($mail_part->headers['content-id'])
380 $mail_part->content_id = preg_replace(array('/^</', '/>$/'), '', $mail_part->headers['content-id']);
381 if ($mail_part->headers['content-location'])
382 $mail_part->content_location = $mail_part->headers['content-base'] . $mail_part->headers['content-location'];
384 if ($mail_part->content_id || $mail_part->content_location) {
385 $this->inline_parts[] = $mail_part;
388 // is regular attachment
390 if (!$mail_part->filename)
391 $mail_part->filename = 'Part '.$mail_part->mime_id;
392 $this->attachments[] = $mail_part;
397 // if this was a related part try to resolve references
398 if ($message_ctype_secondary == 'related' && sizeof($this->inline_parts)) {
399 $a_replaces = array();
401 foreach ($this->inline_parts as $inline_object) {
402 $part_url = $this->get_part_url($inline_object->mime_id);
403 if ($inline_object->content_id)
404 $a_replaces['cid:'.$inline_object->content_id] = $part_url;
405 if ($inline_object->content_location)
406 $a_replaces[$inline_object->content_location] = $part_url;
409 // add replace array to each content part
410 // (will be applied later when part body is available)
411 foreach ($this->parts as $i => $part) {
412 if ($part->type == 'content')
413 $this->parts[$i]->replaces = $a_replaces;
418 // message is single part non-text
419 else if ($structure->filename) {
420 $this->attachments[] = $structure;
426 * Fill aflat array with references to all parts, indexed by part numbers
428 * @param object rcube_message_part Message body structure
430 private function get_mime_numbers(&$part)
432 if (strlen($part->mime_id))
433 $this->mime_parts[$part->mime_id] = &$part;
435 if (is_array($part->parts))
436 for ($i=0; $i<count($part->parts); $i++)
437 $this->get_mime_numbers($part->parts[$i]);