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