Fixed bug that ignored lowercase quoted printable in headers.
[squirrelmail.git] / functions / mime.php
index 4cf74790ff96360841df2a915af0f503f84e9a36..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);
-      $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
@@ -74,7 +80,7 @@
 
       if ($debug_mime) echo "<tt>$read</tt><br><br>";
 
-      $msg = mime_parse_structure ($read);
+      $msg = mime_parse_structure ($read, 0);
       $msg->header = $header;
       return $msg;
    }
             if ($debug_mime) echo "<tt>".$properties[$i]["name"]." = " . $properties[$i]["value"] . "</tt><br>";
          }
       }
+
       return $msg;
    }
 
    /** This is the first function called.  It decides if this is a multipart
        message or if it should be handled as a single entity
     **/
-   function decodeMime ($body, $header) {
+   function decodeMime ($imap_stream, $body, $header) {
       global $username, $key, $imapServerAddress, $imapPort;
-      $imap_stream = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
-      sqimap_mailbox_select($imap_stream, $header->mailbox);
-
       return mime_structure ($imap_stream, $header);
    }
 
        everything needed, including HTML Tags, Attachments at the
        bottom, etc.
     **/
-   function formatBody($message, $color, $wrap_at) {
+   function formatBody($imap_stream, $message, $color, $wrap_at) {
       // this if statement checks for the entity to show as the
       // primary message. To add more of them, just put them in the
       // order that is their priority.
       $id = $message->header->id;
       $urlmailbox = urlencode($message->header->mailbox);
 
-      $imap_stream = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
-      sqimap_mailbox_select($imap_stream, $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, $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, $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);
          }