Fixed bug that ignored lowercase quoted printable in headers.
[squirrelmail.git] / functions / mime.php
index fecaac8e1edecc2d6d4a5e16898bd82051382b6f..ce634671d5882d1e64e8885c9041b86ac14a5389 100644 (file)
@@ -26,7 +26,7 @@
       var $type0, $type1, $boundary, $charset, $encoding;
       var $to, $from, $date, $cc, $bcc, $reply_to, $subject;
       var $id, $mailbox, $description;
-      var $entity_id, $message_id;
+      var $entity_id, $message_id, $charset;
    }
    
    class message {
       $id = $header->id;
       fputs ($imap_stream, "a001 FETCH $id BODYSTRUCTURE\r\n");
       $read = fgets ($imap_stream, 10000);
-      $endline = fgets($imap_stream, 1024);
-      $read = strtolower($read);
+      $response = substr($read, 0, 4);
+      while ($response != "a001") {
+         $bodystructure = $read;
+         $read = fgets ($imap_stream, 10000);
+         $response = substr($read, 0, 4);
+      }
+      $read = strtolower($bodystructure);
 
       if ($debug_mime) echo "<tt>$read</tt><br><br>";
       // isolate the body structure and remove beginning and end parenthesis
             if ($debug_mime) echo "<tt>".$properties[$i]["name"]." = " . $properties[$i]["value"] . "</tt><br>";
          }
       }
+
       return $msg;
    }
 
       $id = $message->header->id;
       $urlmailbox = urlencode($message->header->mailbox);
 
+      // Get the right entity and redefine message to be this entity
       $ent_num = findDisplayEntity ($message);
+      $body_message = getEntity($message, $ent_num);
+
       $body = mime_fetch_body ($imap_stream, $id, $ent_num); 
-      $body = decodeBody($body, $message->header->encoding);
+      $body = decodeBody($body, $body_message->header->encoding);
 
       // If there are other types that shouldn't be formatted, add
       // them here 
       if ($message->header->type1 != "html") {   
-         $body = translateText($body, $wrap_at, $message->header->charset);
+         $body = translateText($body, $wrap_at, $body_message->header->charset);
       }   
 
       $body .= "<SMALL><CENTER><A HREF=\"../src/download.php?absolute_dl=true&passed_id=$id&passed_ent_id=$ent_num&mailbox=$urlmailbox\">". _("Download this as a file") ."</A></CENTER><BR></SMALL>";
             $type1 = strtolower($message->header->type1);
             
             if ($message->header->entity_id != $ent_id) {
-               $filename = $message->header->filename;
+               $filename = decodeHeader($message->header->filename);
                if (trim($filename) == "") {
                   $display_filename = "untitled-".$message->header->entity_id;
                } else {
                $body .= "<TT>&nbsp;&nbsp;&nbsp;<A HREF=\"../src/download.php?passed_id=$id&mailbox=$urlMailbox&passed_ent_id=$ent\">" . $display_filename . "</A>&nbsp;&nbsp;(TYPE: $type0/$type1)";
                if ($message->header->description)
                   $body .= "&nbsp;&nbsp;<b>" . htmlspecialchars($message->header->description)."</b>";
-               if ($message->header->type0 == "image" &&
-                   ($message->header->type1 == "jpg" ||
-                    $message->header->type1 == "jpeg" ||
-                    $message->header->type1 == "gif" ||
-                    $message->header->type1 == "png"))
-                  $body .= "&nbsp;(<a href=\"../src/download.php?passed_id=$id&mailbox=$urlMailbox&passed_ent_id=$ent&view=true\">"._("view")."</a>)\n";     
+               $body .= "&nbsp;(<a href=\"../src/download.php?absolute_dl=true&passed_id=$id&mailbox=$urlMailbox&passed_ent_id=$ent\">"._("download")."</a>)\n";     
                $body .= "</TT><BR>";
                $num++;
             }
 
    /** this function decodes the body depending on the encoding type. **/
    function decodeBody($body, $encoding) {
+      $body = str_replace("\r\n", "\n", $body);
       $encoding = strtolower($encoding);
 
       if ($encoding == "quoted-printable") {
             $replace = base64_decode($res[3]);
          } else {
             $replace = ereg_replace("_", " ", $res[3]);
+           // Convert lowercase Quoted Printable to uppercase for
+           // quoted_printable_decode to understand it.
+           while (ereg("(=([0-9][a-f])|([a-f][0-9]))", $replace, $res)) {
+              $replace = str_replace($res[1], strtoupper($res[1]), $replace);
+           }
             $replace = quoted_printable_decode($replace);
          }