Thomas Bruederli
2014-12-28 e8b82c2e7b0ae2e5d45ecb600813b8990568feb9
commit | author | age
8fa58e 1 <?php
T 2
3 /*
4  +-----------------------------------------------------------------------+
e019f2 5  | This file is part of the Roundcube Webmail client                     |
48ba44 6  | Copyright (C) 2008-2014, The Roundcube Dev Team                       |
7fe381 7  |                                                                       |
T 8  | Licensed under the GNU General Public License version 3 or            |
9  | any later version with exceptions for skins & plugins.                |
10  | See the README file for a full license statement.                     |
8fa58e 11  |                                                                       |
T 12  | PURPOSE:                                                              |
13  |   Logical representation of a mail message with all its data          |
14  |   and related functions                                               |
15  +-----------------------------------------------------------------------+
16  | Author: Thomas Bruederli <roundcube@gmail.com>                        |
17  +-----------------------------------------------------------------------+
18 */
19
20 /**
45f56c 21  * Logical representation of a mail message with all its data
T 22  * and related functions
8fa58e 23  *
9ab346 24  * @package    Framework
AM 25  * @subpackage Storage
8fa58e 26  * @author     Thomas Bruederli <roundcube@gmail.com>
T 27  */
28 class rcube_message
29 {
5c461b 30     /**
be98df 31      * Instace of framework class.
5c461b 32      *
be98df 33      * @var rcube
5c461b 34      */
d311d8 35     private $app;
5c461b 36
A 37     /**
8b92d2 38      * Instance of storage class
5c461b 39      *
8b92d2 40      * @var rcube_storage
5c461b 41      */
8b92d2 42     private $storage;
1c4f23 43
A 44     /**
45      * Instance of mime class
46      *
47      * @var rcube_mime
48      */
49     private $mime;
d311d8 50     private $opt = array();
A 51     private $parse_alternative = false;
2ae58f 52
10562d 53     public $uid;
AM 54     public $folder;
d311d8 55     public $headers;
A 56     public $parts = array();
57     public $mime_parts = array();
287eff 58     public $inline_parts = array();
d311d8 59     public $attachments = array();
A 60     public $subject = '';
61     public $sender = null;
62     public $is_safe = false;
48ba44 63
AM 64     const BODY_MAX_SIZE = 1048576; // 1MB
2ae58f 65
193fb4 66
d311d8 67     /**
A 68      * __construct
69      *
70      * Provide a uid, and parse message structure.
71      *
10562d 72      * @param string $uid    The message UID.
AM 73      * @param string $folder Folder name
d311d8 74      *
8b92d2 75      * @see self::$app, self::$storage, self::$opt, self::$parts
d311d8 76      */
10562d 77     function __construct($uid, $folder = null)
8fa58e 78     {
e8cb51 79         // decode combined UID-folder identifier
188247 80         if (preg_match('/^\d+-.+/', $uid)) {
1d1fdc 81             list($uid, $folder) = explode('-', $uid, 2);
e8cb51 82         }
TB 83
1c4f23 84         $this->uid  = $uid;
0c2596 85         $this->app  = rcube::get_instance();
c321a9 86         $this->storage = $this->app->get_storage();
10562d 87         $this->folder  = strlen($folder) ? $folder : $this->storage->get_folder();
c321a9 88         $this->storage->set_options(array('all_headers' => true));
10562d 89
AM 90         // Set current folder
91         $this->storage->set_folder($this->folder);
64e3e8 92
c321a9 93         $this->headers = $this->storage->get_message($uid);
64e3e8 94
4fdaa0 95         if (!$this->headers) {
64e3e8 96             return;
4fdaa0 97         }
8fa58e 98
1c4f23 99         $this->mime = new rcube_mime($this->headers->charset);
A 100
4fdaa0 101         $this->subject = $this->headers->get('subject');
1c4f23 102         list(, $this->sender) = each($this->mime->decode_address_list($this->headers->from, 1));
64e3e8 103
f11142 104         $this->set_safe((intval($_GET['_safe']) || $_SESSION['safe_messages'][$this->folder.':'.$uid]));
d311d8 105         $this->opt = array(
A 106             'safe' => $this->is_safe,
107             'prefer_html' => $this->app->config->get('prefer_html'),
0c2596 108             'get_url' => $this->app->url(array(
A 109                 'action' => 'get',
110                 'mbox'   => $this->storage->get_folder(),
111                 'uid'    => $uid))
d311d8 112         );
8fa58e 113
80152b 114         if (!empty($this->headers->structure)) {
A 115             $this->get_mime_numbers($this->headers->structure);
116             $this->parse_structure($this->headers->structure);
aa16b4 117         }
d311d8 118         else {
c321a9 119             $this->body = $this->storage->get_body($uid);
d311d8 120         }
A 121
122         // notify plugins and let them analyze this structured message object
123         $this->app->plugins->exec_hook('message_load', array('object' => $this));
124     }
2ae58f 125
A 126
d311d8 127     /**
A 128      * Return a (decoded) message header
129      *
5c461b 130      * @param string $name Header name
A 131      * @param bool   $row  Don't mime-decode the value
d311d8 132      * @return string Header value
A 133      */
134     public function get_header($name, $raw = false)
135     {
4fdaa0 136         if (empty($this->headers)) {
1c4f23 137             return null;
4fdaa0 138         }
1c4f23 139
4fdaa0 140         return $this->headers->get($name, !$raw);
d311d8 141     }
A 142
2ae58f 143
d311d8 144     /**
A 145      * Set is_safe var and session data
146      *
5c461b 147      * @param bool $safe enable/disable
d311d8 148      */
A 149     public function set_safe($safe = true)
150     {
f11142 151         $_SESSION['safe_messages'][$this->folder.':'.$this->uid] = $this->is_safe = $safe;
d311d8 152     }
A 153
154
155     /**
156      * Compose a valid URL for getting a message part
157      *
5c461b 158      * @param string $mime_id Part MIME-ID
a021d6 159      * @param mixed  $embed Mimetype class for parts to be embedded
d311d8 160      * @return string URL or false if part does not exist
A 161      */
57486f 162     public function get_part_url($mime_id, $embed = false)
d311d8 163     {
A 164         if ($this->mime_parts[$mime_id])
a021d6 165             return $this->opt['get_url'] . '&_part=' . $mime_id . ($embed ? '&_embed=1&_mimeclass=' . $embed : '');
aa16b4 166         else
d311d8 167             return false;
8fa58e 168     }
T 169
d311d8 170
A 171     /**
172      * Get content of a specific part of this message
173      *
71950d 174      * @param string   $mime_id           Part MIME-ID
A 175      * @param resource $fp File           pointer to save the message part
176      * @param boolean  $skip_charset_conv Disables charset conversion
dff2c7 177      * @param int      $max_bytes         Only read this number of bytes
ae8533 178      * @param boolean  $formatted         Enables formatting of text/* parts bodies
71950d 179      *
d311d8 180      * @return string Part content
48ba44 181      * @deprecated
d311d8 182      */
ae8533 183     public function get_part_content($mime_id, $fp = null, $skip_charset_conv = false, $max_bytes = 0, $formatted = true)
d311d8 184     {
A 185         if ($part = $this->mime_parts[$mime_id]) {
186             // stored in message structure (winmail/inline-uuencode)
8b92d2 187             if (!empty($part->body) || $part->encoding == 'stream') {
d311d8 188                 if ($fp) {
A 189                     fwrite($fp, $part->body);
190                 }
191                 return $fp ? true : $part->body;
192             }
10562d 193
d311d8 194             // get from IMAP
10562d 195             $this->storage->set_folder($this->folder);
AM 196
ae8533 197             return $this->storage->get_message_part($this->uid, $mime_id, $part,
AM 198                 NULL, $fp, $skip_charset_conv, $max_bytes, $formatted);
10562d 199         }
48ba44 200     }
AM 201
202
203     /**
204      * Get content of a specific part of this message
205      *
206      * @param string  $mime_id   Part ID
207      * @param boolean $formatted Enables formatting of text/* parts bodies
208      * @param int     $max_bytes Only return/read this number of bytes
209      * @param mixed   $mode      NULL to return a string, -1 to print body
210      *                           or file pointer to save the body into
211      *
212      * @return string|bool Part content or operation status
213      */
214     public function get_part_body($mime_id, $formatted = false, $max_bytes = 0, $mode = null)
215     {
216         if (!($part = $this->mime_parts[$mime_id])) {
217             return;
218         }
219
220         // only text parts can be formatted
221         $formatted = $formatted && $part->ctype_primary == 'text';
222
223         // part body not fetched yet... save in memory if it's small enough
224         if ($part->body === null && is_numeric($mime_id) && $part->size < self::BODY_MAX_SIZE) {
d93019 225             $this->storage->set_folder($this->folder);
48ba44 226             // Warning: body here should be always unformatted
AM 227             $part->body = $this->storage->get_message_part($this->uid, $mime_id, $part,
228                 null, null, true, 0, false);
229         }
230
231         // body stored in message structure (winmail/inline-uuencode)
232         if ($part->body !== null || $part->encoding == 'stream') {
233             $body = $part->body;
234
235             if ($formatted && $body) {
236                 $body = self::format_part_body($body, $part, $this->headers->charset);
237             }
238
239             if ($max_bytes && strlen($body) > $max_bytes) {
240                 $body = substr($body, 0, $max_bytes);
241             }
242
243             if (is_resource($mode)) {
244                 if ($body !== false) {
245                     fwrite($mode, $body);
246                     rewind($mode);
247                 }
248
249                 return $body !== false;
250             }
251
252             if ($mode === -1) {
253                 if ($body !== false) {
254                     print($body);
255                 }
256
257                 return $body !== false;
258             }
259
260             return $body;
261         }
262
263         // get the body from IMAP
264         $this->storage->set_folder($this->folder);
265
266         $body = $this->storage->get_message_part($this->uid, $mime_id, $part,
68c41f 267             $mode === -1, is_resource($mode) ? $mode : null,
AM 268             !($mode && $formatted), $max_bytes, $mode && $formatted);
48ba44 269
AM 270         if (is_resource($mode)) {
271             rewind($mode);
272             return $body !== false;
273         }
274
68c41f 275         if (!$mode && $body && $formatted) {
AM 276             $body = self::format_part_body($body, $part, $this->headers->charset);
277         }
278
48ba44 279         return $body;
AM 280     }
281
282
283     /**
284      * Format text message part for display
285      *
286      * @param string             $body            Part body
287      * @param rcube_message_part $part            Part object
288      * @param string             $default_charset Fallback charset if part charset is not specified
289      *
290      * @return string Formatted body
291      */
292     public static function format_part_body($body, $part, $default_charset = null)
293     {
294         // remove useless characters
295         $body = preg_replace('/[\t\r\0\x0B]+\n/', "\n", $body);
296
297         // remove NULL characters if any (#1486189)
298         if (strpos($body, "\x00") !== false) {
299             $body = str_replace("\x00", '', $body);
300         }
301
302         // detect charset...
303         if (!$part->charset || strtoupper($part->charset) == 'US-ASCII') {
304             // try to extract charset information from HTML meta tag (#1488125)
305             if ($part->ctype_secondary == 'html' && preg_match('/<meta[^>]+charset=([a-z0-9-_]+)/i', $body, $m)) {
306                 $part->charset = strtoupper($m[1]);
307             }
308             else if ($default_charset) {
309                 $part->charset = $default_charset;
310             }
311             else {
312                 $rcube         = rcube::get_instance();
313                 $part->charset = $rcube->config->get('default_charset', RCUBE_CHARSET);
314             }
315         }
316
317         // ..convert charset encoding
318         $body = rcube_charset::convert($body, $part->charset);
319
320         return $body;
8fa58e 321     }
T 322
323
d311d8 324     /**
5c26bd 325      * Determine if the message contains a HTML part. This must to be
AM 326      * a real part not an attachment (or its part)
d311d8 327      *
5c26bd 328      * @param bool $enriched Enables checking for text/enriched parts too
33423a 329      *
d311d8 330      * @return bool True if a HTML is available, False if not
A 331      */
5c26bd 332     function has_html_part($enriched = false)
d311d8 333     {
A 334         // check all message parts
574928 335         foreach ($this->mime_parts as $part) {
52d0d9 336             if ($part->mimetype == 'text/html' || ($enriched && $part->mimetype == 'text/enriched')) {
0ef894 337                 // Skip if part is an attachment, don't use is_attachment() here
AM 338                 if ($part->filename) {
5c26bd 339                     continue;
AM 340                 }
33423a 341
5c26bd 342                 $level = explode('.', $part->mime_id);
f8101f 343                 $depth = count($level);
5c26bd 344
5a2d2a 345                 // Check if the part belongs to higher-level's multipart part
f8101f 346                 // this can be alternative/related/signed/encrypted or mixed
5c26bd 347                 while (array_pop($level) !== null) {
f8101f 348                     $parent_depth = count($level);
TB 349                     if (!$parent_depth) {
5c26bd 350                         return true;
33423a 351                     }
A 352
5c26bd 353                     $parent = $this->mime_parts[join('.', $level)];
f8101f 354                     if (!preg_match('/^multipart\/(alternative|related|signed|encrypted|mixed)$/', $parent->mimetype)
TB 355                         || ($parent->mimetype == 'multipart/mixed' && $parent_depth < $depth - 1)) {
5c26bd 356                         continue 2;
33423a 357                     }
A 358                 }
359
5c26bd 360                 if ($part->size) {
AM 361                     return true;
362                 }
363             }
364         }
365
366         return false;
367     }
368
369
370     /**
371      * Determine if the message contains a text/plain part. This must to be
372      * a real part not an attachment (or its part)
373      *
374      * @return bool True if a plain text part is available, False if not
375      */
376     function has_text_part()
377     {
378         // check all message parts
574928 379         foreach ($this->mime_parts as $part) {
5c26bd 380             if ($part->mimetype == 'text/plain') {
0ef894 381                 // Skip if part is an attachment, don't use is_attachment() here
AM 382                 if ($part->filename) {
5c26bd 383                     continue;
AM 384                 }
385
386                 $level = explode('.', $part->mime_id);
387
388                 // Check if the part belongs to higher-level's alternative/related
389                 while (array_pop($level) !== null) {
390                     if (!count($level)) {
391                         return true;
392                     }
393
394                     $parent = $this->mime_parts[join('.', $level)];
395                     if ($parent->mimetype != 'multipart/alternative' && $parent->mimetype != 'multipart/related') {
396                         continue 2;
397                     }
398                 }
399
400                 if ($part->size) {
401                     return true;
402                 }
33423a 403             }
d311d8 404         }
A 405
406         return false;
407     }
408
409
410     /**
411      * Return the first HTML part of this message
412      *
413      * @return string HTML message part content
414      */
415     function first_html_part()
416     {
417         // check all message parts
33423a 418         foreach ($this->mime_parts as $pid => $part) {
A 419             if ($part->mimetype == 'text/html') {
48ba44 420                 return $this->get_part_body($pid, true);
d311d8 421             }
A 422         }
423     }
424
425
426     /**
427      * Return the first text part of this message
428      *
868deb 429      * @param rcube_message_part $part Reference to the part if found
d311d8 430      * @return string Plain text message/part content
A 431      */
868deb 432     function first_text_part(&$part=null)
d311d8 433     {
A 434         // no message structure, return complete body
435         if (empty($this->parts))
436             return $this->body;
437
438         // check all message parts
439         foreach ($this->mime_parts as $mime_id => $part) {
33423a 440             if ($part->mimetype == 'text/plain') {
48ba44 441                 return $this->get_part_body($mime_id, true);
d311d8 442             }
33423a 443             else if ($part->mimetype == 'text/html') {
48ba44 444                 $out = $this->get_part_body($mime_id, true);
d311d8 445
A 446                 // create instance of html2text class
66afd7 447                 $txt = new rcube_html2text($out);
868deb 448                 return $txt->get_text();
d311d8 449             }
A 450         }
451
868deb 452         $part = null;
A 453         return null;
d311d8 454     }
A 455
456
457     /**
3efc74 458      * Checks if part of the message is an attachment (or part of it)
AM 459      *
460      * @param rcube_message_part $part Message part
461      *
462      * @return bool True if the part is an attachment part
463      */
464     public function is_attachment($part)
465     {
466         foreach ($this->attachments as $att_part) {
467             if ($att_part->mime_id == $part->mime_id) {
468                 return true;
469             }
470
471             // check if the part is a subpart of another attachment part (message/rfc822)
472             if ($att_part->mimetype == 'message/rfc822') {
473                 if (in_array($part, (array)$att_part->parts)) {
474                     return true;
475                 }
476             }
477         }
478
479         return false;
480     }
481
482
483     /**
8b92d2 484      * Read the message structure returend by the IMAP server
d311d8 485      * and build flat lists of content parts and attachments
A 486      *
5c461b 487      * @param rcube_message_part $structure Message structure node
A 488      * @param bool               $recursive True when called recursively
d311d8 489      */
A 490     private function parse_structure($structure, $recursive = false)
491     {
492         // real content-type of message/rfc822 part
a8a72e 493         if ($structure->mimetype == 'message/rfc822' && $structure->real_mimetype) {
5ced9c 494             $mimetype = $structure->real_mimetype;
a8a72e 495
TB 496             // parse headers from message/rfc822 part
7ae7cd 497             if (!isset($structure->headers['subject']) && !isset($structure->headers['from'])) {
48ba44 498                 list($headers, ) = explode("\r\n\r\n", $this->get_part_body($structure->mime_id, false, 32768));
a8a72e 499                 $structure->headers = rcube_mime::parse_headers($headers);
TB 500             }
501         }
5ced9c 502         else
A 503             $mimetype = $structure->mimetype;
d311d8 504
A 505         // show message headers
ddfdd8 506         if ($recursive && is_array($structure->headers) &&
TB 507                 (isset($structure->headers['subject']) || $structure->headers['from'] || $structure->headers['to'])) {
d311d8 508             $c = new stdClass;
A 509             $c->type = 'headers';
c5d7c9 510             $c->headers = $structure->headers;
d311d8 511             $this->parts[] = $c;
A 512         }
5ced9c 513
A 514         // Allow plugins to handle message parts
515         $plugin = $this->app->plugins->exec_hook('message_part_structure',
516             array('object' => $this, 'structure' => $structure,
517                 'mimetype' => $mimetype, 'recursive' => $recursive));
518
519         if ($plugin['abort'])
520             return;
521
522         $structure = $plugin['structure'];
523         list($message_ctype_primary, $message_ctype_secondary) = explode('/', $plugin['mimetype']);
d311d8 524
A 525         // print body if message doesn't have multiple parts
526         if ($message_ctype_primary == 'text' && !$recursive) {
c23dc8 527             // parts with unsupported type add to attachments list
AM 528             if (!in_array($message_ctype_secondary, array('plain', 'html', 'enriched'))) {
529                 $this->attachments[] = $structure;
530                 return;
531             }
532
d311d8 533             $structure->type = 'content';
c5d7c9 534             $this->parts[] = $structure;
8757f5 535
d311d8 536             // Parse simple (plain text) message body
c23dc8 537             if ($message_ctype_secondary == 'plain') {
d311d8 538                 foreach ((array)$this->uu_decode($structure) as $uupart) {
A 539                     $this->mime_parts[$uupart->mime_id] = $uupart;
540                     $this->attachments[] = $uupart;
541                 }
c23dc8 542             }
d311d8 543         }
A 544         // the same for pgp signed messages
545         else if ($mimetype == 'application/pgp' && !$recursive) {
546             $structure->type = 'content';
c5d7c9 547             $this->parts[] = $structure;
d311d8 548         }
e730cd 549         // message contains (more than one!) alternative parts
A 550         else if ($mimetype == 'multipart/alternative'
551             && is_array($structure->parts) && count($structure->parts) > 1
552         ) {
c5d7c9 553             // get html/plaintext parts, other add to attachments list
d311d8 554             foreach ($structure->parts as $p => $sub_part) {
A 555                 $sub_mimetype = $sub_part->mimetype;
cb0f03 556                 $is_multipart = preg_match('/^multipart\/(related|relative|mixed|alternative)/', $sub_mimetype);
8757f5 557
5fbfde 558                 // skip empty text parts
c5d7c9 559                 if (!$sub_part->size && !$is_multipart) {
5f4095 560                     continue;
5fbfde 561                 }
5f4095 562
170702 563                 // We've encountered (malformed) messages with more than
AM 564                 // one text/plain or text/html part here. There's no way to choose
565                 // which one is better, so we'll display first of them and add
566                 // others as attachments (#1489358)
567
d311d8 568                 // check if sub part is
c5d7c9 569                 if ($is_multipart)
AM 570                     $related_part = $p;
170702 571                 else if ($sub_mimetype == 'text/plain' && !$plain_part)
d311d8 572                     $plain_part = $p;
170702 573                 else if ($sub_mimetype == 'text/html' && !$html_part)
d311d8 574                     $html_part = $p;
170702 575                 else if ($sub_mimetype == 'text/enriched' && !$enriched_part)
d311d8 576                     $enriched_part = $p;
170702 577                 else {
AM 578                     // add unsupported/unrecognized parts to attachments list
579                     $this->attachments[] = $sub_part;
580                 }
d311d8 581             }
A 582
583             // parse related part (alternative part could be in here)
584             if ($related_part !== null && !$this->parse_alternative) {
585                 $this->parse_alternative = true;
586                 $this->parse_structure($structure->parts[$related_part], true);
587                 $this->parse_alternative = false;
8757f5 588
d311d8 589                 // if plain part was found, we should unset it if html is preferred
A 590                 if ($this->opt['prefer_html'] && count($this->parts))
591                     $plain_part = null;
592             }
593
594             // choose html/plain part to print
595             if ($html_part !== null && $this->opt['prefer_html']) {
c5d7c9 596                 $print_part = $structure->parts[$html_part];
d311d8 597             }
A 598             else if ($enriched_part !== null) {
c5d7c9 599                 $print_part = $structure->parts[$enriched_part];
d311d8 600             }
A 601             else if ($plain_part !== null) {
c5d7c9 602                 $print_part = $structure->parts[$plain_part];
d311d8 603             }
A 604
605             // add the right message body
606             if (is_object($print_part)) {
607                 $print_part->type = 'content';
608                 $this->parts[] = $print_part;
609             }
610             // show plaintext warning
611             else if ($html_part !== null && empty($this->parts)) {
612                 $c = new stdClass;
613                 $c->type            = 'content';
614                 $c->ctype_primary   = 'text';
615                 $c->ctype_secondary = 'plain';
0c2596 616                 $c->mimetype        = 'text/plain';
A 617                 $c->realtype        = 'text/html';
d311d8 618
A 619                 $this->parts[] = $c;
620             }
621         }
622         // this is an ecrypted message -> create a plaintext body with the according message
623         else if ($mimetype == 'multipart/encrypted') {
624             $p = new stdClass;
625             $p->type            = 'content';
626             $p->ctype_primary   = 'text';
627             $p->ctype_secondary = 'plain';
0c2596 628             $p->mimetype        = 'text/plain';
A 629             $p->realtype        = 'multipart/encrypted';
c054ec 630
A 631             $this->parts[] = $p;
d311d8 632         }
ee89c6 633         // this is an S/MIME ecrypted message -> create a plaintext body with the according message
AM 634         else if ($mimetype == 'application/pkcs7-mime') {
635             $p = new stdClass;
636             $p->type            = 'content';
637             $p->ctype_primary   = 'text';
638             $p->ctype_secondary = 'plain';
639             $p->mimetype        = 'text/plain';
640             $p->realtype        = 'application/pkcs7-mime';
641
642             $this->parts[] = $p;
643         }
d311d8 644         // message contains multiple parts
A 645         else if (is_array($structure->parts) && !empty($structure->parts)) {
646             // iterate over parts
647             for ($i=0; $i < count($structure->parts); $i++) {
648                 $mail_part      = &$structure->parts[$i];
649                 $primary_type   = $mail_part->ctype_primary;
650                 $secondary_type = $mail_part->ctype_secondary;
8fa58e 651
d311d8 652                 // real content-type of message/rfc822
A 653                 if ($mail_part->real_mimetype) {
654                     $part_orig_mimetype = $mail_part->mimetype;
655                     $part_mimetype = $mail_part->real_mimetype;
656                     list($primary_type, $secondary_type) = explode('/', $part_mimetype);
657                 }
596301 658                 else {
TB 659                     $part_mimetype = $part_orig_mimetype = $mail_part->mimetype;
660                   }
6b6f2e 661
d311d8 662                 // multipart/alternative
A 663                 if ($primary_type == 'multipart') {
664                     $this->parse_structure($mail_part, true);
665
666                     // list message/rfc822 as attachment as well (mostly .eml)
667                     if ($part_orig_mimetype == 'message/rfc822' && !empty($mail_part->filename))
668                         $this->attachments[] = $mail_part;
669                 }
f22ea7 670                 // part text/[plain|html] or delivery status
d311d8 671                 else if ((($part_mimetype == 'text/plain' || $part_mimetype == 'text/html') && $mail_part->disposition != 'attachment') ||
f22ea7 672                     in_array($part_mimetype, array('message/delivery-status', 'text/rfc822-headers', 'message/disposition-notification'))
d311d8 673                 ) {
1a2f83 674                     // Allow plugins to handle also this part
A 675                     $plugin = $this->app->plugins->exec_hook('message_part_structure',
676                         array('object' => $this, 'structure' => $mail_part,
677                             'mimetype' => $part_mimetype, 'recursive' => true));
678
679                     if ($plugin['abort'])
680                         continue;
681
9c299e 682                     if ($part_mimetype == 'text/html' && $mail_part->size) {
63f9de 683                         $got_html_part = true;
A 684                     }
685
1a2f83 686                     $mail_part = $plugin['structure'];
A 687                     list($primary_type, $secondary_type) = explode('/', $plugin['mimetype']);
688
d311d8 689                     // add text part if it matches the prefs
A 690                     if (!$this->parse_alternative ||
691                         ($secondary_type == 'html' && $this->opt['prefer_html']) ||
692                         ($secondary_type == 'plain' && !$this->opt['prefer_html'])
693                     ) {
694                         $mail_part->type = 'content';
695                         $this->parts[] = $mail_part;
696                     }
fd371a 697
d311d8 698                     // list as attachment as well
f7c11e 699                     if (!empty($mail_part->filename)) {
AM 700                         $this->attachments[] = $mail_part;
701                     }
d311d8 702                 }
A 703                 // part message/*
8757f5 704                 else if ($primary_type == 'message') {
d311d8 705                     $this->parse_structure($mail_part, true);
A 706
707                     // list as attachment as well (mostly .eml)
708                     if (!empty($mail_part->filename))
709                         $this->attachments[] = $mail_part;
710                 }
711                 // ignore "virtual" protocol parts
712                 else if ($primary_type == 'protocol') {
713                     continue;
714                 }
715                 // part is Microsoft Outlook TNEF (winmail.dat)
716                 else if ($part_mimetype == 'application/ms-tnef') {
717                     foreach ((array)$this->tnef_decode($mail_part) as $tpart) {
718                         $this->mime_parts[$tpart->mime_id] = $tpart;
719                         $this->attachments[] = $tpart;
720                     }
721                 }
722                 // part is a file/attachment
723                 else if (preg_match('/^(inline|attach)/', $mail_part->disposition) ||
8794f1 724                     $mail_part->headers['content-id'] ||
A 725                     ($mail_part->filename &&
726                         (empty($mail_part->disposition) || preg_match('/^[a-z0-9!#$&.+^_-]+$/i', $mail_part->disposition)))
d311d8 727                 ) {
A 728                     // skip apple resource forks
729                     if ($message_ctype_secondary == 'appledouble' && $secondary_type == 'applefile')
730                         continue;
731
732                     // part belongs to a related message and is linked
cb0f03 733                     if (preg_match('/^multipart\/(related|relative)/', $mimetype)
d311d8 734                         && ($mail_part->headers['content-id'] || $mail_part->headers['content-location'])) {
A 735                         if ($mail_part->headers['content-id'])
736                             $mail_part->content_id = preg_replace(array('/^</', '/>$/'), '', $mail_part->headers['content-id']);
737                         if ($mail_part->headers['content-location'])
738                             $mail_part->content_location = $mail_part->headers['content-base'] . $mail_part->headers['content-location'];
739
740                         $this->inline_parts[] = $mail_part;
741                     }
742                     // attachment encapsulated within message/rfc822 part needs further decoding (#1486743)
743                     else if ($part_orig_mimetype == 'message/rfc822') {
744                         $this->parse_structure($mail_part, true);
fd371a 745
A 746                         // list as attachment as well (mostly .eml)
747                         if (!empty($mail_part->filename))
748                             $this->attachments[] = $mail_part;
d311d8 749                     }
b38925 750                     // regular attachment with valid content type
A 751                     // (content-type name regexp according to RFC4288.4.2)
2ae58f 752                     else if (preg_match('/^[a-z0-9!#$&.+^_-]+\/[a-z0-9!#$&.+^_-]+$/i', $part_mimetype)) {
b38925 753                         $this->attachments[] = $mail_part;
A 754                     }
755                     // attachment with invalid content type
756                     // replace malformed content type with application/octet-stream (#1487767)
757                     else if ($mail_part->filename) {
758                         $mail_part->ctype_primary   = 'application';
759                         $mail_part->ctype_secondary = 'octet-stream';
760                         $mail_part->mimetype        = 'application/octet-stream';
761
d311d8 762                         $this->attachments[] = $mail_part;
A 763                     }
764                 }
8757f5 765                 // attachment part as message/rfc822 (#1488026)
A 766                 else if ($mail_part->mimetype == 'message/rfc822') {
767                     $this->parse_structure($mail_part);
768                 }
d311d8 769             }
A 770
771             // if this was a related part try to resolve references
cb0f03 772             if (preg_match('/^multipart\/(related|relative)/', $mimetype) && sizeof($this->inline_parts)) {
d311d8 773                 $a_replaces = array();
89d19c 774                 $img_regexp = '/^image\/(gif|jpe?g|png|tiff|bmp|svg)/';
d311d8 775
A 776                 foreach ($this->inline_parts as $inline_object) {
a021d6 777                     $part_url = $this->get_part_url($inline_object->mime_id, $inline_object->ctype_primary);
8cfba1 778                     if (isset($inline_object->content_id))
d311d8 779                         $a_replaces['cid:'.$inline_object->content_id] = $part_url;
63f9de 780                     if ($inline_object->content_location) {
d311d8 781                         $a_replaces[$inline_object->content_location] = $part_url;
63f9de 782                     }
89d19c 783
A 784                     if (!empty($inline_object->filename)) {
785                         // MS Outlook sends sometimes non-related attachments as related
786                         // In this case multipart/related message has only one text part
787                         // We'll add all such attachments to the attachments list
788                         if (!isset($got_html_part) && empty($inline_object->content_id)) {
789                             $this->attachments[] = $inline_object;
790                         }
791                         // MS Outlook sometimes also adds non-image attachments as related
792                         // We'll add all such attachments to the attachments list
793                         // Warning: some browsers support pdf in <img/>
794                         else if (!preg_match($img_regexp, $inline_object->mimetype)) {
795                             $this->attachments[] = $inline_object;
796                         }
797                         // @TODO: we should fetch HTML body and find attachment's content-id
798                         // to handle also image attachments without reference in the body
799                         // @TODO: should we list all image attachments in text mode?
02b6e6 800                     }
d311d8 801                 }
A 802
803                 // add replace array to each content part
804                 // (will be applied later when part body is available)
805                 foreach ($this->parts as $i => $part) {
806                     if ($part->type == 'content')
807                         $this->parts[$i]->replaces = $a_replaces;
808                 }
809             }
810         }
811         // message is a single part non-text
812         else if ($structure->filename) {
813             $this->attachments[] = $structure;
814         }
3e58bf 815         // message is a single part non-text (without filename)
A 816         else if (preg_match('/application\//i', $mimetype)) {
817             $this->attachments[] = $structure;
c5d7c9 818         }
AM 819     }
820
821
822     /**
d311d8 823      * Fill aflat array with references to all parts, indexed by part numbers
A 824      *
5c461b 825      * @param rcube_message_part $part Message body structure
d311d8 826      */
A 827     private function get_mime_numbers(&$part)
828     {
829         if (strlen($part->mime_id))
830             $this->mime_parts[$part->mime_id] = &$part;
f19d86 831
d311d8 832         if (is_array($part->parts))
A 833             for ($i=0; $i<count($part->parts); $i++)
834                 $this->get_mime_numbers($part->parts[$i]);
835     }
836
837
838     /**
839      * Decode a Microsoft Outlook TNEF part (winmail.dat)
840      *
5c461b 841      * @param rcube_message_part $part Message part to decode
A 842      * @return array
d311d8 843      */
A 844     function tnef_decode(&$part)
845     {
48ba44 846         // @TODO: attachment may be huge, handle body via file
AM 847         $body     = $this->get_part_body($part->mime_id);
2883fc 848         $tnef     = new rcube_tnef_decoder;
48ba44 849         $tnef_arr = $tnef->decompress($body);
AM 850         $parts    = array();
d311d8 851
48ba44 852         unset($body);
d311d8 853
A 854         foreach ($tnef_arr as $pid => $winatt) {
855             $tpart = new rcube_message_part;
856
2da830 857             $tpart->filename        = $this->fix_attachment_name(trim($winatt['name']), $part);
d311d8 858             $tpart->encoding        = 'stream';
f19d86 859             $tpart->ctype_primary   = trim(strtolower($winatt['type']));
A 860             $tpart->ctype_secondary = trim(strtolower($winatt['subtype']));
d311d8 861             $tpart->mimetype        = $tpart->ctype_primary . '/' . $tpart->ctype_secondary;
A 862             $tpart->mime_id         = 'winmail.' . $part->mime_id . '.' . $pid;
863             $tpart->size            = $winatt['size'];
864             $tpart->body            = $winatt['stream'];
865
866             $parts[] = $tpart;
867             unset($tnef_arr[$pid]);
868         }
f19d86 869
d311d8 870         return $parts;
A 871     }
872
873
874     /**
875      * Parse message body for UUencoded attachments bodies
876      *
5c461b 877      * @param rcube_message_part $part Message part to decode
A 878      * @return array
d311d8 879      */
A 880     function uu_decode(&$part)
881     {
48ba44 882         // @TODO: messages may be huge, handle body via file
AM 883         $part->body = $this->get_part_body($part->mime_id);
884         $parts      = array();
885         $pid        = 0;
d311d8 886
A 887         // FIXME: line length is max.65?
48ba44 888         $uu_regexp_begin = '/begin [0-7]{3,4} ([^\r\n]+)\r?\n/s';
AM 889         $uu_regexp_end   = '/`\r?\nend((\r?\n)|($))/s';
d311d8 890
48ba44 891         while (preg_match($uu_regexp_begin, $part->body, $matches, PREG_OFFSET_CAPTURE)) {
AM 892             $startpos = $matches[0][1];
d311d8 893
48ba44 894             if (!preg_match($uu_regexp_end, $part->body, $m, PREG_OFFSET_CAPTURE, $startpos)) {
AM 895                 break;
d311d8 896             }
14f22f 897
48ba44 898             $endpos    = $m[0][1];
AM 899             $begin_len = strlen($matches[0][0]);
900             $end_len   = strlen($m[0][0]);
901
902             // extract attachment body
903             $filebody = substr($part->body, $startpos + $begin_len, $endpos - $startpos - $begin_len - 1);
904             $filebody = str_replace("\r\n", "\n", $filebody);
905
906             // remove attachment body from the message body
907             $part->body = substr_replace($part->body, '', $startpos, $endpos + $end_len - $startpos);
2268aa 908             // mark body as modified so it will not be cached by rcube_imap_cache
AM 909             $part->body_modified = true;
48ba44 910
AM 911             // add attachments to the structure
912             $uupart = new rcube_message_part;
913             $uupart->filename = trim($matches[1][0]);
914             $uupart->encoding = 'stream';
915             $uupart->body     = convert_uudecode($filebody);
916             $uupart->size     = strlen($uupart->body);
917             $uupart->mime_id  = 'uu.' . $part->mime_id . '.' . $pid;
918
919             $ctype = rcube_mime::file_content_type($uupart->body, $uupart->filename, 'application/octet-stream', true);
920             $uupart->mimetype = $ctype;
921             list($uupart->ctype_primary, $uupart->ctype_secondary) = explode('/', $ctype);
922
923             $parts[] = $uupart;
924             $pid++;
d311d8 925         }
f19d86 926
d311d8 927         return $parts;
A 928     }
b91f04 929
2da830 930     /**
AM 931      * Fix attachment name encoding if needed/possible
932      */
933     protected function fix_attachment_name($name, $part)
934     {
935         if ($name == rcube_charset::clean($name)) {
936             return $name;
937         }
938
939         // find charset from part or its parent(s)
940         if ($part->charset) {
941             $charsets[] = $part->charset;
942         }
943         else {
944             // check first part (common case)
945             $n = strpos($part->mime_id, '.') ? preg_replace('/\.[0-9]+$/', '', $part->mime_id) . '.1' : 1;
946             if (($_part = $this->mime_parts[$n]) && $_part->charset) {
947                 $charsets[] = $_part->charset;
948             }
949
950             // check parents' charset
951             $items = explode('.', $part->mime_id);
952             for ($i = count($items)-1; $i > 0; $i--) {
953                 $last   = array_pop($items);
954                 $parent = $this->mime_parts[join('.', $items)];
955
956                 if ($parent && $parent->charset) {
957                     $charsets[] = $parent->charset;
958                 }
959             }
960         }
961
962         if ($this->headers->charset) {
963             $charsets[] = $this->headers->charset;
964         }
965
966         if (empty($charsets)) {
967             $rcube      = rcube::get_instance();
968             $charsets[] = rcube_charset::detect($name, $rcube->config->get('default_charset', RCUBE_CHARSET));
969         }
970
971         foreach (array_unique($charsets) as $charset) {
972             $_name = rcube_charset::convert($name, $charset);
973
974             if ($_name == rcube_charset::clean($_name)) {
975                 if (!$part->charset) {
976                     $part->charset = $charset;
977                 }
978
979                 return $_name;
980             }
981         }
982
983         return $name;
984     }
b91f04 985
T 986     /**
987      * Deprecated methods (to be removed)
988      */
989
990     public static function unfold_flowed($text)
991     {
992         return rcube_mime::unfold_flowed($text);
993     }
994
995     public static function format_flowed($text, $length = 72)
996     {
997         return rcube_mime::format_flowed($text, $length);
998     }
999
8fa58e 1000 }