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