From bdb40d39bd7921a97e530dac3ef0759a6752b5c6 Mon Sep 17 00:00:00 2001
From: Aleksander Machniak <alec@alec.pl>
Date: Wed, 06 Jun 2012 03:35:56 -0400
Subject: [PATCH] Add workaround for invalid BODYSTRUCTURE response - parse message with Mail_mimeDecode package (#1485585)

---
 CHANGELOG                      |    1 +
 program/include/rcube_imap.php |   17 +++++++++++++++--
 2 files changed, 16 insertions(+), 2 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG
index 576e07f..cb132db 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,6 +1,7 @@
 CHANGELOG Roundcube Webmail
 ===========================
 
+- Add workaround for invalid BODYSTRUCTURE response - parse message with Mail_mimeDecode package (#1485585)
 - Decode header value in rcube_mime::get() by default (#1488511)
 - Fix errors with enabled PHP magic_quotes_sybase option (#1488506)
 - Fix SQL query for contacts listing on MS SQL Server (#1488505)
diff --git a/program/include/rcube_imap.php b/program/include/rcube_imap.php
index 84b0fc1..00a4158 100644
--- a/program/include/rcube_imap.php
+++ b/program/include/rcube_imap.php
@@ -1635,11 +1635,24 @@
                 $structure[1] = $m[2];
             }
             else {
-                return $headers;
+                // Try to parse the message using Mail_mimeDecode package
+                // We need a better solution, Mail_mimeDecode parses message
+                // in memory, which wouldn't work for very big messages,
+                // (it uses up to 10x more memory than the message size)
+                // it's also buggy and not actively developed
+                if ($headers->size && rcube_utils::mem_check($headers->size * 10)) {
+                    $raw_msg = $this->get_raw_body($uid);
+                    $struct = rcube_mime::parse_message($raw_msg);
+                }
+                else {
+                    return $headers;
+                }
             }
         }
 
-        $struct = $this->structure_part($structure, 0, '', $headers);
+        if (empty($struct)) {
+            $struct = $this->structure_part($structure, 0, '', $headers);
+        }
 
         // don't trust given content-type
         if (empty($struct->parts) && !empty($headers->ctype)) {

--
Gitblit v1.9.1