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 $is_safe = false;
48 function __construct($uid)
50 $this->app = rcmail::get_instance();
51 $this->imap = $this->app->imap;
54 $this->headers = $this->imap->get_headers($uid);
55 $this->subject = rcube_imap::decode_mime_string($this->headers->subject, $this->headers->charset);
57 $this->is_safe = (intval($_GET['_safe']) || $_SESSION['safe_messages'][$uid]) ? true : false;
58 $_SESSION['safe_messages'][$uid] = $this->is_safe;
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))
66 if ($this->structure = $this->imap->get_structure($uid)) {
67 $this->get_mime_numbers($this->structure);
68 $this->parse_structure($this->structure);
71 $this->body = $this->imap->get_body($uid);
77 * Return a (decoded) message header
79 * @param string Header name
80 * @param bool Don't mime-decode the value
81 * @return string Header value
83 public function get_header($name, $raw = false)
85 $value = $this->headers->$name;
86 return $raw ? $value : $this->imap->decode_header($value);
91 * Compose a valid URL for getting a message part
93 * @param string Part MIME-ID
94 * @return string URL or false if part does not exist
96 public function get_part_url($mime_id)
98 if ($this->mime_parts[$mime_id])
99 return $this->opt['get_url'] . "&_part=" . $mime_id;
106 * Get content of a specific part of this message
108 * @param string Part MIME-ID
109 * @return string Part content
111 public function get_part_content($mime_id)
113 if ($part = $this->mime_parts[$mime_id])
114 return $this->imap->get_message_part($this->uid, $mime_id, $part);
121 * Determine if the message contains a HTML part
123 * @return bool True if a HTML is available, False if not
125 function has_html_part()
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')
138 * Return the first HTML part of this message
140 * @return string HTML message part content
142 function first_html_part()
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);
159 * Return the first text part of this message
161 * @return string Plain text message/part content
163 function first_text_part()
165 // no message structure, return complete body
166 if (empty($this->parts))
171 // check all message parts
172 foreach ($this->mime_parts as $mime_id => $part) {
173 $mimetype = strtolower($part->ctype_primary . '/' . $part->ctype_secondary);
175 if ($mimetype == 'text/plain') {
176 $out = $this->imap->get_message_part($this->uid, $mime_id, $part);
179 else if ($mimetype == 'text/html') {
180 $html_part = $this->imap->get_message_part($this->uid, $mime_id, $part);
182 // remove special chars encoding
183 $trans = array_flip(get_html_translation_table(HTML_ENTITIES));
184 $html_part = strtr($html_part, $trans);
186 // create instance of html2text class
187 $txt = new html2text($html_part);
188 $out = $txt->get_text();
198 * Raad the message structure returend by the IMAP server
199 * and build flat lists of content parts and attachments
201 * @param object rcube_message_part Message structure node
202 * @param bool True when called recursively
204 private function parse_structure($structure, $recursive = false)
206 $message_ctype_primary = strtolower($structure->ctype_primary);
207 $message_ctype_secondary = strtolower($structure->ctype_secondary);
209 // show message headers
210 if ($recursive && is_array($structure->headers) && isset($structure->headers['subject'])) {
212 $c->type = 'headers';
213 $c->headers = &$structure->headers;
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;
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;
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);
232 // check if sub part is
233 if ($sub_ctype_primary=='text' && $sub_ctype_secondary=='plain')
235 else if ($sub_ctype_primary=='text' && $sub_ctype_secondary=='html')
237 else if ($sub_ctype_primary=='text' && $sub_ctype_secondary=='enriched')
239 else if ($sub_ctype_primary=='multipart' && ($sub_ctype_secondary=='related' || $sub_ctype_secondary=='mixed'))
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;
249 // if plain part was found, we should unset it if html is preferred
250 if ($this->opt['prefer_html'] && count($this->parts))
254 // choose html/plain part to print
255 if ($html_part !== null && $this->opt['prefer_html']) {
256 $print_part = &$structure->parts[$html_part];
258 else if ($enriched_part !== null) {
259 $print_part = &$structure->parts[$enriched_part];
261 else if ($plain_part !== null) {
262 $print_part = &$structure->parts[$plain_part];
265 // add the right message body
266 if (is_object($print_part)) {
267 $print_part->type = 'content';
268 $this->parts[] = $print_part;
270 // show plaintext warning
271 else if ($html_part !== nullL && empty($this->parts)) {
273 $c->type = 'content';
274 $c->body = rcube_label('htmlmessage');
275 $c->ctype_primary = 'text';
276 $c->ctype_secondary = 'plain';
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';
287 $this->attachments[] = $html_part;
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') {
293 $p->type = 'content';
294 $p->ctype_primary = 'text';
295 $p->ctype_secondary = 'plain';
296 $p->body = rcube_label('encryptedmessage');
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);
308 // multipart/alternative
309 if ($primary_type=='multipart') {
310 $this->parse_structure($mail_part, true);
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'))) {
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;
324 // list as attachment as well
325 if (!empty($mail_part->filename))
326 $this->attachments[] = $mail_part;
329 else if ($primary_type=='message') {
330 $this->parse_structure($mail_part, true);
332 // ignore "virtual" protocol parts
333 else if ($primary_type == 'protocol')
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')
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;
348 // is regular attachment
350 if (!$mail_part->filename)
351 $mail_part->filename = 'Part '.$mail_part->mime_id;
352 $this->attachments[] = $mail_part;
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();
361 foreach ($this->inline_parts as $inline_object) {
362 $a_replaces['cid:'.$inline_object->content_id] = $this->get_part_url($inline_object->mime_id);
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;
374 // message is single part non-text
375 else if ($structure->filename) {
376 $this->attachments[] = $structure;
382 * Fill aflat array with references to all parts, indexed by part numbers
384 * @param object rcube_message_part Message body structure
386 private function get_mime_numbers(&$part)
388 if (strlen($part->mime_id))
389 $this->mime_parts[$part->mime_id] = &$part;
391 if (is_array($part->parts))
392 for ($i=0; $i<count($part->parts); $i++)
393 $this->get_mime_numbers($part->parts[$i]);