Fix parsing of messages containing UUEncoded data.
[banana.git] / banana / mimepart.inc.php
index 0446640..577af46 100644 (file)
@@ -201,7 +201,14 @@ class BananaMimePart
             $parts = $this->findUUEncoded();
             if (count($parts)) {
                 $this->convertToMultiPart();
-                $this->multipart    = array_merge(array($textpart), $parts);
+                $this->multipart = array_merge($this->multipart, $parts);
+                // Restore "message" headers to the previous level"
+                $this->headers = array();
+                foreach (Banana::$msgshow_headers as $hdr) {
+                    if (isset($this->multipart[0]->headers[$hdr])) {
+                        $this->headers[$hdr] = $this->multipart[0]->headers[$hdr];
+                    }
+                }
             }
         }
     }
@@ -238,6 +245,9 @@ class BananaMimePart
     {
         if ($is_filename) {
             $type = mime_content_type($data);
+            if ($type == 'text/plain') { // XXX Workaround a bug of php 5.2.0+etch10 (fallback for mime_content_type is text/plain)
+                $type = preg_replace('/;.*/', '', trim(shell_exec('file -bi ' . escapeshellarg($data))));
+            }
         } else {
             $arg = escapeshellarg($data);
             $type = preg_replace('/;.*/', '', trim(shell_exec("echo $arg | file -bi -")));
@@ -257,11 +267,12 @@ class BananaMimePart
                 if ($mime != 'application/x-empty') {
                     $this->body = trim(str_replace($match[0], '', $this->body));
                     $newpart = new BananaMimePart;
+                    self::decodeHeader($match[2]);
                     $newpart->makeDataPart($data, $mime, '8bit', $match[2], 'attachment');
                     $parts[] = $newpart;
                 }
-            }   
-        } 
+            }
+        }
         return $parts;
     }
 
@@ -272,7 +283,7 @@ class BananaMimePart
         return str_replace('_', ' ', $s);
     }
 
-    static public function decodeHeader(&$val, $key)
+    static public function decodeHeader(&$val, $key = null)
     {
         if (preg_match('/[\x80-\xff]/', $val)) {
             if (!is_utf8($val)) {
@@ -337,7 +348,7 @@ class BananaMimePart
         }
 
         if (!is_null($this->charset)) {
-            $body = iconv($this->charset, 'UTF-8//IGNORE', $this->body);
+            $body = @iconv($this->charset, 'UTF-8//IGNORE', $this->body);
             if (empty($body)) {
                 return;
             }
@@ -433,7 +444,7 @@ class BananaMimePart
         $signed =& $this->getSignedPart(); 
         if ($signed !== $this) { 
             return $signed->getText(); 
-        } 
+        }
         $this->decodeContent();
         return $this->body;
     }
@@ -452,6 +463,15 @@ class BananaMimePart
                                                                     'artid' => Banana::$artid,
                                                                     'part'  => $part)))
                  . '" alt="' . banana_htmlentities($this->filename) . '" />';
+        } else if ($type == 'multipart' && $subtype == 'alternative') {
+            $types =& Banana::$msgshow_mimeparts;
+            foreach ($types as $type) {
+                @list($type, $subtype) = explode('/', $type);
+                $part = $this->getParts($type, $subtype);
+                if (count($part) > 0) {
+                    return $part[0]->toHtml();
+                }
+            }
         } elseif ((!in_array($type, Banana::$msgshow_mimeparts)
                   && !in_array($this->content_type, Banana::$msgshow_mimeparts))
                   || $this->disposition == 'attachment') {