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