Aleksander Machniak
2014-08-24 5f58127eae9ed8c54c190506e11af13e8ba57170
commit | author | age
8fa58e 1 <?php
T 2
3 /*
4  +-----------------------------------------------------------------------+
e019f2 5  | This file is part of the Roundcube Webmail client                     |
f5e7b3 6  | Copyright (C) 2008-2010, 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;
2ae58f 63
193fb4 64
d311d8 65     /**
A 66      * __construct
67      *
68      * Provide a uid, and parse message structure.
69      *
10562d 70      * @param string $uid    The message UID.
AM 71      * @param string $folder Folder name
d311d8 72      *
8b92d2 73      * @see self::$app, self::$storage, self::$opt, self::$parts
d311d8 74      */
10562d 75     function __construct($uid, $folder = null)
8fa58e 76     {
e8cb51 77         // decode combined UID-folder identifier
188247 78         if (preg_match('/^\d+-.+/', $uid)) {
1d1fdc 79             list($uid, $folder) = explode('-', $uid, 2);
e8cb51 80         }
TB 81
1c4f23 82         $this->uid  = $uid;
0c2596 83         $this->app  = rcube::get_instance();
c321a9 84         $this->storage = $this->app->get_storage();
10562d 85         $this->folder  = strlen($folder) ? $folder : $this->storage->get_folder();
c321a9 86         $this->storage->set_options(array('all_headers' => true));
10562d 87
AM 88         // Set current folder
89         $this->storage->set_folder($this->folder);
64e3e8 90
c321a9 91         $this->headers = $this->storage->get_message($uid);
64e3e8 92
4fdaa0 93         if (!$this->headers) {
64e3e8 94             return;
4fdaa0 95         }
8fa58e 96
1c4f23 97         $this->mime = new rcube_mime($this->headers->charset);
A 98
4fdaa0 99         $this->subject = $this->headers->get('subject');
1c4f23 100         list(, $this->sender) = each($this->mime->decode_address_list($this->headers->from, 1));
64e3e8 101
f11142 102         $this->set_safe((intval($_GET['_safe']) || $_SESSION['safe_messages'][$this->folder.':'.$uid]));
d311d8 103         $this->opt = array(
A 104             'safe' => $this->is_safe,
105             'prefer_html' => $this->app->config->get('prefer_html'),
0c2596 106             'get_url' => $this->app->url(array(
A 107                 'action' => 'get',
108                 'mbox'   => $this->storage->get_folder(),
109                 'uid'    => $uid))
d311d8 110         );
8fa58e 111
80152b 112         if (!empty($this->headers->structure)) {
A 113             $this->get_mime_numbers($this->headers->structure);
114             $this->parse_structure($this->headers->structure);
aa16b4 115         }
d311d8 116         else {
c321a9 117             $this->body = $this->storage->get_body($uid);
d311d8 118         }
A 119
120         // notify plugins and let them analyze this structured message object
121         $this->app->plugins->exec_hook('message_load', array('object' => $this));
122     }
2ae58f 123
A 124
d311d8 125     /**
A 126      * Return a (decoded) message header
127      *
5c461b 128      * @param string $name Header name
A 129      * @param bool   $row  Don't mime-decode the value
d311d8 130      * @return string Header value
A 131      */
132     public function get_header($name, $raw = false)
133     {
4fdaa0 134         if (empty($this->headers)) {
1c4f23 135             return null;
4fdaa0 136         }
1c4f23 137
4fdaa0 138         return $this->headers->get($name, !$raw);
d311d8 139     }
A 140
2ae58f 141
d311d8 142     /**
A 143      * Set is_safe var and session data
144      *
5c461b 145      * @param bool $safe enable/disable
d311d8 146      */
A 147     public function set_safe($safe = true)
148     {
f11142 149         $_SESSION['safe_messages'][$this->folder.':'.$this->uid] = $this->is_safe = $safe;
d311d8 150     }
A 151
152
153     /**
154      * Compose a valid URL for getting a message part
155      *
5c461b 156      * @param string $mime_id Part MIME-ID
a021d6 157      * @param mixed  $embed Mimetype class for parts to be embedded
d311d8 158      * @return string URL or false if part does not exist
A 159      */
57486f 160     public function get_part_url($mime_id, $embed = false)
d311d8 161     {
A 162         if ($this->mime_parts[$mime_id])
a021d6 163             return $this->opt['get_url'] . '&_part=' . $mime_id . ($embed ? '&_embed=1&_mimeclass=' . $embed : '');
aa16b4 164         else
d311d8 165             return false;
8fa58e 166     }
T 167
d311d8 168
A 169     /**
170      * Get content of a specific part of this message
171      *
71950d 172      * @param string   $mime_id           Part MIME-ID
A 173      * @param resource $fp File           pointer to save the message part
174      * @param boolean  $skip_charset_conv Disables charset conversion
dff2c7 175      * @param int      $max_bytes         Only read this number of bytes
ae8533 176      * @param boolean  $formatted         Enables formatting of text/* parts bodies
71950d 177      *
d311d8 178      * @return string Part content
A 179      */
ae8533 180     public function get_part_content($mime_id, $fp = null, $skip_charset_conv = false, $max_bytes = 0, $formatted = true)
d311d8 181     {
A 182         if ($part = $this->mime_parts[$mime_id]) {
183             // stored in message structure (winmail/inline-uuencode)
8b92d2 184             if (!empty($part->body) || $part->encoding == 'stream') {
d311d8 185                 if ($fp) {
A 186                     fwrite($fp, $part->body);
187                 }
188                 return $fp ? true : $part->body;
189             }
10562d 190
d311d8 191             // get from IMAP
10562d 192             $this->storage->set_folder($this->folder);
AM 193
ae8533 194             return $this->storage->get_message_part($this->uid, $mime_id, $part,
AM 195                 NULL, $fp, $skip_charset_conv, $max_bytes, $formatted);
10562d 196         }
8fa58e 197     }
T 198
199
d311d8 200     /**
5c26bd 201      * Determine if the message contains a HTML part. This must to be
AM 202      * a real part not an attachment (or its part)
d311d8 203      *
5c26bd 204      * @param bool $enriched Enables checking for text/enriched parts too
33423a 205      *
d311d8 206      * @return bool True if a HTML is available, False if not
A 207      */
5c26bd 208     function has_html_part($enriched = false)
d311d8 209     {
A 210         // check all message parts
574928 211         foreach ($this->mime_parts as $part) {
52d0d9 212             if ($part->mimetype == 'text/html' || ($enriched && $part->mimetype == 'text/enriched')) {
0ef894 213                 // Skip if part is an attachment, don't use is_attachment() here
AM 214                 if ($part->filename) {
5c26bd 215                     continue;
AM 216                 }
33423a 217
5c26bd 218                 $level = explode('.', $part->mime_id);
f8101f 219                 $depth = count($level);
5c26bd 220
5a2d2a 221                 // Check if the part belongs to higher-level's multipart part
f8101f 222                 // this can be alternative/related/signed/encrypted or mixed
5c26bd 223                 while (array_pop($level) !== null) {
f8101f 224                     $parent_depth = count($level);
TB 225                     if (!$parent_depth) {
5c26bd 226                         return true;
33423a 227                     }
A 228
5c26bd 229                     $parent = $this->mime_parts[join('.', $level)];
f8101f 230                     if (!preg_match('/^multipart\/(alternative|related|signed|encrypted|mixed)$/', $parent->mimetype)
TB 231                         || ($parent->mimetype == 'multipart/mixed' && $parent_depth < $depth - 1)) {
5c26bd 232                         continue 2;
33423a 233                     }
A 234                 }
235
5c26bd 236                 if ($part->size) {
AM 237                     return true;
238                 }
239             }
240         }
241
242         return false;
243     }
244
245
246     /**
247      * Determine if the message contains a text/plain part. This must to be
248      * a real part not an attachment (or its part)
249      *
250      * @return bool True if a plain text part is available, False if not
251      */
252     function has_text_part()
253     {
254         // check all message parts
574928 255         foreach ($this->mime_parts as $part) {
5c26bd 256             if ($part->mimetype == 'text/plain') {
0ef894 257                 // Skip if part is an attachment, don't use is_attachment() here
AM 258                 if ($part->filename) {
5c26bd 259                     continue;
AM 260                 }
261
262                 $level = explode('.', $part->mime_id);
263
264                 // Check if the part belongs to higher-level's alternative/related
265                 while (array_pop($level) !== null) {
266                     if (!count($level)) {
267                         return true;
268                     }
269
270                     $parent = $this->mime_parts[join('.', $level)];
271                     if ($parent->mimetype != 'multipart/alternative' && $parent->mimetype != 'multipart/related') {
272                         continue 2;
273                     }
274                 }
275
276                 if ($part->size) {
277                     return true;
278                 }
33423a 279             }
d311d8 280         }
A 281
282         return false;
283     }
284
285
286     /**
287      * Return the first HTML part of this message
288      *
289      * @return string HTML message part content
290      */
291     function first_html_part()
292     {
293         // check all message parts
33423a 294         foreach ($this->mime_parts as $pid => $part) {
A 295             if ($part->mimetype == 'text/html') {
296                 return $this->get_part_content($pid);
d311d8 297             }
A 298         }
299     }
300
301
302     /**
303      * Return the first text part of this message
304      *
868deb 305      * @param rcube_message_part $part Reference to the part if found
d311d8 306      * @return string Plain text message/part content
A 307      */
868deb 308     function first_text_part(&$part=null)
d311d8 309     {
A 310         // no message structure, return complete body
311         if (empty($this->parts))
312             return $this->body;
313
314         // check all message parts
315         foreach ($this->mime_parts as $mime_id => $part) {
33423a 316             if ($part->mimetype == 'text/plain') {
8b92d2 317                 return $this->get_part_content($mime_id);
d311d8 318             }
33423a 319             else if ($part->mimetype == 'text/html') {
8b92d2 320                 $out = $this->get_part_content($mime_id);
d311d8 321
A 322                 // create instance of html2text class
66afd7 323                 $txt = new rcube_html2text($out);
868deb 324                 return $txt->get_text();
d311d8 325             }
A 326         }
327
868deb 328         $part = null;
A 329         return null;
d311d8 330     }
A 331
332
333     /**
3efc74 334      * Checks if part of the message is an attachment (or part of it)
AM 335      *
336      * @param rcube_message_part $part Message part
337      *
338      * @return bool True if the part is an attachment part
339      */
340     public function is_attachment($part)
341     {
342         foreach ($this->attachments as $att_part) {
343             if ($att_part->mime_id == $part->mime_id) {
344                 return true;
345             }
346
347             // check if the part is a subpart of another attachment part (message/rfc822)
348             if ($att_part->mimetype == 'message/rfc822') {
349                 if (in_array($part, (array)$att_part->parts)) {
350                     return true;
351                 }
352             }
353         }
354
355         return false;
356     }
357
358
359     /**
8b92d2 360      * Read the message structure returend by the IMAP server
d311d8 361      * and build flat lists of content parts and attachments
A 362      *
5c461b 363      * @param rcube_message_part $structure Message structure node
A 364      * @param bool               $recursive True when called recursively
d311d8 365      */
A 366     private function parse_structure($structure, $recursive = false)
367     {
368         // real content-type of message/rfc822 part
a8a72e 369         if ($structure->mimetype == 'message/rfc822' && $structure->real_mimetype) {
5ced9c 370             $mimetype = $structure->real_mimetype;
a8a72e 371
TB 372             // parse headers from message/rfc822 part
7ae7cd 373             if (!isset($structure->headers['subject']) && !isset($structure->headers['from'])) {
3725cf 374                 list($headers, ) = explode("\r\n\r\n", $this->get_part_content($structure->mime_id, null, true, 32768));
a8a72e 375                 $structure->headers = rcube_mime::parse_headers($headers);
TB 376             }
377         }
5ced9c 378         else
A 379             $mimetype = $structure->mimetype;
d311d8 380
A 381         // show message headers
ddfdd8 382         if ($recursive && is_array($structure->headers) &&
TB 383                 (isset($structure->headers['subject']) || $structure->headers['from'] || $structure->headers['to'])) {
d311d8 384             $c = new stdClass;
A 385             $c->type = 'headers';
c5d7c9 386             $c->headers = $structure->headers;
d311d8 387             $this->parts[] = $c;
A 388         }
5ced9c 389
A 390         // Allow plugins to handle message parts
391         $plugin = $this->app->plugins->exec_hook('message_part_structure',
392             array('object' => $this, 'structure' => $structure,
393                 'mimetype' => $mimetype, 'recursive' => $recursive));
394
395         if ($plugin['abort'])
396             return;
397
398         $structure = $plugin['structure'];
399         list($message_ctype_primary, $message_ctype_secondary) = explode('/', $plugin['mimetype']);
d311d8 400
A 401         // print body if message doesn't have multiple parts
402         if ($message_ctype_primary == 'text' && !$recursive) {
c23dc8 403             // parts with unsupported type add to attachments list
AM 404             if (!in_array($message_ctype_secondary, array('plain', 'html', 'enriched'))) {
405                 $this->attachments[] = $structure;
406                 return;
407             }
408
d311d8 409             $structure->type = 'content';
c5d7c9 410             $this->parts[] = $structure;
8757f5 411
d311d8 412             // Parse simple (plain text) message body
c23dc8 413             if ($message_ctype_secondary == 'plain') {
d311d8 414                 foreach ((array)$this->uu_decode($structure) as $uupart) {
A 415                     $this->mime_parts[$uupart->mime_id] = $uupart;
416                     $this->attachments[] = $uupart;
417                 }
c23dc8 418             }
d311d8 419         }
A 420         // the same for pgp signed messages
421         else if ($mimetype == 'application/pgp' && !$recursive) {
422             $structure->type = 'content';
c5d7c9 423             $this->parts[] = $structure;
d311d8 424         }
e730cd 425         // message contains (more than one!) alternative parts
A 426         else if ($mimetype == 'multipart/alternative'
427             && is_array($structure->parts) && count($structure->parts) > 1
428         ) {
c5d7c9 429             $plain_part   = null;
AM 430             $html_part    = null;
431             $print_part   = null;
432             $related_part = null;
433             $attach_part  = null;
d311d8 434
c5d7c9 435             // get html/plaintext parts, other add to attachments list
d311d8 436             foreach ($structure->parts as $p => $sub_part) {
A 437                 $sub_mimetype = $sub_part->mimetype;
cb0f03 438                 $is_multipart = preg_match('/^multipart\/(related|relative|mixed|alternative)/', $sub_mimetype);
8757f5 439
5fbfde 440                 // skip empty text parts
c5d7c9 441                 if (!$sub_part->size && !$is_multipart) {
5f4095 442                     continue;
5fbfde 443                 }
5f4095 444
170702 445                 // We've encountered (malformed) messages with more than
AM 446                 // one text/plain or text/html part here. There's no way to choose
447                 // which one is better, so we'll display first of them and add
448                 // others as attachments (#1489358)
449
d311d8 450                 // check if sub part is
c5d7c9 451                 if ($is_multipart)
AM 452                     $related_part = $p;
170702 453                 else if ($sub_mimetype == 'text/plain' && !$plain_part)
d311d8 454                     $plain_part = $p;
170702 455                 else if ($sub_mimetype == 'text/html' && !$html_part)
d311d8 456                     $html_part = $p;
170702 457                 else if ($sub_mimetype == 'text/enriched' && !$enriched_part)
d311d8 458                     $enriched_part = $p;
170702 459                 else {
AM 460                     // add unsupported/unrecognized parts to attachments list
461                     $this->attachments[] = $sub_part;
462                 }
d311d8 463             }
A 464
465             // parse related part (alternative part could be in here)
466             if ($related_part !== null && !$this->parse_alternative) {
467                 $this->parse_alternative = true;
468                 $this->parse_structure($structure->parts[$related_part], true);
469                 $this->parse_alternative = false;
8757f5 470
d311d8 471                 // if plain part was found, we should unset it if html is preferred
A 472                 if ($this->opt['prefer_html'] && count($this->parts))
473                     $plain_part = null;
474             }
475
476             // choose html/plain part to print
477             if ($html_part !== null && $this->opt['prefer_html']) {
c5d7c9 478                 $print_part = $structure->parts[$html_part];
d311d8 479             }
A 480             else if ($enriched_part !== null) {
c5d7c9 481                 $print_part = $structure->parts[$enriched_part];
d311d8 482             }
A 483             else if ($plain_part !== null) {
c5d7c9 484                 $print_part = $structure->parts[$plain_part];
d311d8 485             }
A 486
487             // add the right message body
488             if (is_object($print_part)) {
489                 $print_part->type = 'content';
490                 $this->parts[] = $print_part;
491             }
492             // show plaintext warning
493             else if ($html_part !== null && empty($this->parts)) {
494                 $c = new stdClass;
495                 $c->type            = 'content';
496                 $c->ctype_primary   = 'text';
497                 $c->ctype_secondary = 'plain';
0c2596 498                 $c->mimetype        = 'text/plain';
A 499                 $c->realtype        = 'text/html';
d311d8 500
A 501                 $this->parts[] = $c;
502             }
503         }
504         // this is an ecrypted message -> create a plaintext body with the according message
505         else if ($mimetype == 'multipart/encrypted') {
506             $p = new stdClass;
507             $p->type            = 'content';
508             $p->ctype_primary   = 'text';
509             $p->ctype_secondary = 'plain';
0c2596 510             $p->mimetype        = 'text/plain';
A 511             $p->realtype        = 'multipart/encrypted';
c054ec 512
A 513             $this->parts[] = $p;
d311d8 514         }
ee89c6 515         // this is an S/MIME ecrypted message -> create a plaintext body with the according message
AM 516         else if ($mimetype == 'application/pkcs7-mime') {
517             $p = new stdClass;
518             $p->type            = 'content';
519             $p->ctype_primary   = 'text';
520             $p->ctype_secondary = 'plain';
521             $p->mimetype        = 'text/plain';
522             $p->realtype        = 'application/pkcs7-mime';
523
524             $this->parts[] = $p;
525         }
d311d8 526         // message contains multiple parts
A 527         else if (is_array($structure->parts) && !empty($structure->parts)) {
528             // iterate over parts
529             for ($i=0; $i < count($structure->parts); $i++) {
530                 $mail_part      = &$structure->parts[$i];
531                 $primary_type   = $mail_part->ctype_primary;
532                 $secondary_type = $mail_part->ctype_secondary;
8fa58e 533
d311d8 534                 // real content-type of message/rfc822
A 535                 if ($mail_part->real_mimetype) {
536                     $part_orig_mimetype = $mail_part->mimetype;
537                     $part_mimetype = $mail_part->real_mimetype;
538                     list($primary_type, $secondary_type) = explode('/', $part_mimetype);
539                 }
596301 540                 else {
TB 541                     $part_mimetype = $part_orig_mimetype = $mail_part->mimetype;
542                   }
6b6f2e 543
d311d8 544                 // multipart/alternative
A 545                 if ($primary_type == 'multipart') {
546                     $this->parse_structure($mail_part, true);
547
548                     // list message/rfc822 as attachment as well (mostly .eml)
549                     if ($part_orig_mimetype == 'message/rfc822' && !empty($mail_part->filename))
550                         $this->attachments[] = $mail_part;
551                 }
f22ea7 552                 // part text/[plain|html] or delivery status
d311d8 553                 else if ((($part_mimetype == 'text/plain' || $part_mimetype == 'text/html') && $mail_part->disposition != 'attachment') ||
f22ea7 554                     in_array($part_mimetype, array('message/delivery-status', 'text/rfc822-headers', 'message/disposition-notification'))
d311d8 555                 ) {
1a2f83 556                     // Allow plugins to handle also this part
A 557                     $plugin = $this->app->plugins->exec_hook('message_part_structure',
558                         array('object' => $this, 'structure' => $mail_part,
559                             'mimetype' => $part_mimetype, 'recursive' => true));
560
561                     if ($plugin['abort'])
562                         continue;
563
9c299e 564                     if ($part_mimetype == 'text/html' && $mail_part->size) {
63f9de 565                         $got_html_part = true;
A 566                     }
567
1a2f83 568                     $mail_part = $plugin['structure'];
A 569                     list($primary_type, $secondary_type) = explode('/', $plugin['mimetype']);
570
d311d8 571                     // add text part if it matches the prefs
A 572                     if (!$this->parse_alternative ||
573                         ($secondary_type == 'html' && $this->opt['prefer_html']) ||
574                         ($secondary_type == 'plain' && !$this->opt['prefer_html'])
575                     ) {
576                         $mail_part->type = 'content';
577                         $this->parts[] = $mail_part;
578                     }
fd371a 579
d311d8 580                     // list as attachment as well
f7c11e 581                     if (!empty($mail_part->filename)) {
AM 582                         $this->attachments[] = $mail_part;
583                     }
d311d8 584                 }
A 585                 // part message/*
8757f5 586                 else if ($primary_type == 'message') {
d311d8 587                     $this->parse_structure($mail_part, true);
A 588
589                     // list as attachment as well (mostly .eml)
590                     if (!empty($mail_part->filename))
591                         $this->attachments[] = $mail_part;
592                 }
593                 // ignore "virtual" protocol parts
594                 else if ($primary_type == 'protocol') {
595                     continue;
596                 }
597                 // part is Microsoft Outlook TNEF (winmail.dat)
598                 else if ($part_mimetype == 'application/ms-tnef') {
599                     foreach ((array)$this->tnef_decode($mail_part) as $tpart) {
600                         $this->mime_parts[$tpart->mime_id] = $tpart;
601                         $this->attachments[] = $tpart;
602                     }
603                 }
604                 // part is a file/attachment
605                 else if (preg_match('/^(inline|attach)/', $mail_part->disposition) ||
8794f1 606                     $mail_part->headers['content-id'] ||
A 607                     ($mail_part->filename &&
608                         (empty($mail_part->disposition) || preg_match('/^[a-z0-9!#$&.+^_-]+$/i', $mail_part->disposition)))
d311d8 609                 ) {
A 610                     // skip apple resource forks
611                     if ($message_ctype_secondary == 'appledouble' && $secondary_type == 'applefile')
612                         continue;
613
614                     // part belongs to a related message and is linked
cb0f03 615                     if (preg_match('/^multipart\/(related|relative)/', $mimetype)
d311d8 616                         && ($mail_part->headers['content-id'] || $mail_part->headers['content-location'])) {
A 617                         if ($mail_part->headers['content-id'])
618                             $mail_part->content_id = preg_replace(array('/^</', '/>$/'), '', $mail_part->headers['content-id']);
619                         if ($mail_part->headers['content-location'])
620                             $mail_part->content_location = $mail_part->headers['content-base'] . $mail_part->headers['content-location'];
621
622                         $this->inline_parts[] = $mail_part;
623                     }
624                     // attachment encapsulated within message/rfc822 part needs further decoding (#1486743)
625                     else if ($part_orig_mimetype == 'message/rfc822') {
626                         $this->parse_structure($mail_part, true);
fd371a 627
A 628                         // list as attachment as well (mostly .eml)
629                         if (!empty($mail_part->filename))
630                             $this->attachments[] = $mail_part;
d311d8 631                     }
b38925 632                     // regular attachment with valid content type
A 633                     // (content-type name regexp according to RFC4288.4.2)
2ae58f 634                     else if (preg_match('/^[a-z0-9!#$&.+^_-]+\/[a-z0-9!#$&.+^_-]+$/i', $part_mimetype)) {
b38925 635                         $this->attachments[] = $mail_part;
A 636                     }
637                     // attachment with invalid content type
638                     // replace malformed content type with application/octet-stream (#1487767)
639                     else if ($mail_part->filename) {
640                         $mail_part->ctype_primary   = 'application';
641                         $mail_part->ctype_secondary = 'octet-stream';
642                         $mail_part->mimetype        = 'application/octet-stream';
643
d311d8 644                         $this->attachments[] = $mail_part;
A 645                     }
646                 }
8757f5 647                 // attachment part as message/rfc822 (#1488026)
A 648                 else if ($mail_part->mimetype == 'message/rfc822') {
649                     $this->parse_structure($mail_part);
650                 }
d311d8 651             }
A 652
653             // if this was a related part try to resolve references
cb0f03 654             if (preg_match('/^multipart\/(related|relative)/', $mimetype) && sizeof($this->inline_parts)) {
d311d8 655                 $a_replaces = array();
89d19c 656                 $img_regexp = '/^image\/(gif|jpe?g|png|tiff|bmp|svg)/';
d311d8 657
A 658                 foreach ($this->inline_parts as $inline_object) {
a021d6 659                     $part_url = $this->get_part_url($inline_object->mime_id, $inline_object->ctype_primary);
8cfba1 660                     if (isset($inline_object->content_id))
d311d8 661                         $a_replaces['cid:'.$inline_object->content_id] = $part_url;
63f9de 662                     if ($inline_object->content_location) {
d311d8 663                         $a_replaces[$inline_object->content_location] = $part_url;
63f9de 664                     }
89d19c 665
A 666                     if (!empty($inline_object->filename)) {
667                         // MS Outlook sends sometimes non-related attachments as related
668                         // In this case multipart/related message has only one text part
669                         // We'll add all such attachments to the attachments list
670                         if (!isset($got_html_part) && empty($inline_object->content_id)) {
671                             $this->attachments[] = $inline_object;
672                         }
673                         // MS Outlook sometimes also adds non-image attachments as related
674                         // We'll add all such attachments to the attachments list
675                         // Warning: some browsers support pdf in <img/>
676                         else if (!preg_match($img_regexp, $inline_object->mimetype)) {
677                             $this->attachments[] = $inline_object;
678                         }
679                         // @TODO: we should fetch HTML body and find attachment's content-id
680                         // to handle also image attachments without reference in the body
681                         // @TODO: should we list all image attachments in text mode?
02b6e6 682                     }
d311d8 683                 }
A 684
685                 // add replace array to each content part
686                 // (will be applied later when part body is available)
687                 foreach ($this->parts as $i => $part) {
688                     if ($part->type == 'content')
689                         $this->parts[$i]->replaces = $a_replaces;
690                 }
691             }
692         }
693         // message is a single part non-text
694         else if ($structure->filename) {
695             $this->attachments[] = $structure;
696         }
3e58bf 697         // message is a single part non-text (without filename)
A 698         else if (preg_match('/application\//i', $mimetype)) {
699             $this->attachments[] = $structure;
c5d7c9 700         }
AM 701     }
702
703
704     /**
d311d8 705      * Fill aflat array with references to all parts, indexed by part numbers
A 706      *
5c461b 707      * @param rcube_message_part $part Message body structure
d311d8 708      */
A 709     private function get_mime_numbers(&$part)
710     {
711         if (strlen($part->mime_id))
712             $this->mime_parts[$part->mime_id] = &$part;
f19d86 713
d311d8 714         if (is_array($part->parts))
A 715             for ($i=0; $i<count($part->parts); $i++)
716                 $this->get_mime_numbers($part->parts[$i]);
717     }
718
719
720     /**
721      * Decode a Microsoft Outlook TNEF part (winmail.dat)
722      *
5c461b 723      * @param rcube_message_part $part Message part to decode
A 724      * @return array
d311d8 725      */
A 726     function tnef_decode(&$part)
727     {
728         // @TODO: attachment may be huge, hadle it via file
10562d 729         if (!isset($part->body)) {
AM 730             $this->storage->set_folder($this->folder);
c321a9 731             $part->body = $this->storage->get_message_part($this->uid, $part->mime_id, $part);
10562d 732         }
d311d8 733
A 734         $parts = array();
f19d86 735         $tnef = new tnef_decoder;
A 736         $tnef_arr = $tnef->decompress($part->body);
d311d8 737
A 738         foreach ($tnef_arr as $pid => $winatt) {
739             $tpart = new rcube_message_part;
740
741             $tpart->filename        = trim($winatt['name']);
742             $tpart->encoding        = 'stream';
f19d86 743             $tpart->ctype_primary   = trim(strtolower($winatt['type']));
A 744             $tpart->ctype_secondary = trim(strtolower($winatt['subtype']));
d311d8 745             $tpart->mimetype        = $tpart->ctype_primary . '/' . $tpart->ctype_secondary;
A 746             $tpart->mime_id         = 'winmail.' . $part->mime_id . '.' . $pid;
747             $tpart->size            = $winatt['size'];
748             $tpart->body            = $winatt['stream'];
749
750             $parts[] = $tpart;
751             unset($tnef_arr[$pid]);
752         }
f19d86 753
d311d8 754         return $parts;
A 755     }
756
757
758     /**
759      * Parse message body for UUencoded attachments bodies
760      *
5c461b 761      * @param rcube_message_part $part Message part to decode
A 762      * @return array
d311d8 763      */
A 764     function uu_decode(&$part)
765     {
766         // @TODO: messages may be huge, hadle body via file
10562d 767         if (!isset($part->body)) {
AM 768             $this->storage->set_folder($this->folder);
c321a9 769             $part->body = $this->storage->get_message_part($this->uid, $part->mime_id, $part);
10562d 770         }
d311d8 771
A 772         $parts = array();
773         // FIXME: line length is max.65?
14f22f 774         $uu_regexp = '/begin [0-7]{3,4} ([^\n]+)\n/s';
d311d8 775
A 776         if (preg_match_all($uu_regexp, $part->body, $matches, PREG_SET_ORDER)) {
777             // update message content-type
778             $part->ctype_primary   = 'multipart';
779             $part->ctype_secondary = 'mixed';
780             $part->mimetype        = $part->ctype_primary . '/' . $part->ctype_secondary;
14f22f 781             $uu_endstring = "`\nend\n";
d311d8 782
A 783             // add attachments to the structure
784             foreach ($matches as $pid => $att) {
14f22f 785                 $startpos = strpos($part->body, $att[1]) + strlen($att[1]) + 1; // "\n"
GB 786                 $endpos = strpos($part->body, $uu_endstring);
787                 $filebody = substr($part->body, $startpos, $endpos-$startpos);
788
789                 // remove attachments bodies from the message body
790                 $part->body = substr_replace($part->body, "", $startpos, $endpos+strlen($uu_endstring)-$startpos);
791
d311d8 792                 $uupart = new rcube_message_part;
A 793
794                 $uupart->filename = trim($att[1]);
795                 $uupart->encoding = 'stream';
14f22f 796                 $uupart->body     = convert_uudecode($filebody);
d311d8 797                 $uupart->size     = strlen($uupart->body);
A 798                 $uupart->mime_id  = 'uu.' . $part->mime_id . '.' . $pid;
799
bbd636 800                 $ctype = rcube_mime::file_content_type($uupart->body, $uupart->filename, 'application/octet-stream', true);
d311d8 801                 $uupart->mimetype = $ctype;
A 802                 list($uupart->ctype_primary, $uupart->ctype_secondary) = explode('/', $ctype);
803
804                 $parts[] = $uupart;
805                 unset($matches[$pid]);
806             }
14f22f 807
GB 808             // remove attachments bodies from the message body
809             $part->body = preg_replace($uu_regexp, '', $part->body);
d311d8 810         }
f19d86 811
d311d8 812         return $parts;
A 813     }
b91f04 814
T 815
816     /**
817      * Deprecated methods (to be removed)
818      */
819
820     public static function unfold_flowed($text)
821     {
822         return rcube_mime::unfold_flowed($text);
823     }
824
825     public static function format_flowed($text, $length = 72)
826     {
827         return rcube_mime::format_flowed($text, $length);
828     }
829
8fa58e 830 }