Added partial support for iso-8859-15 to rfc1522Decode.
[squirrelmail.git] / functions / mime.php
index f81d08f07efb21981ff5044b810f0c3ee22084a3..f891a3cdce97125482c0e6841292deb3ed016fde 100644 (file)
@@ -11,6 +11,7 @@
    function decodeMime($body, $bound, $type0, $type1, &$entities) {
       if ($type0 == "multipart") {
          $bound = trim($bound);
+         $i = 0;
          while (($i < count($body)) && (substr($body[$i], 0, strlen("--$bound--")) != "--$bound--")) {
             if (trim($body[$i]) == "--$bound") {
                $j = $i+1;
@@ -31,7 +32,7 @@
                      $p++;
                   }
                   /** All of these values are getting passed back to us **/
-                  fetchEntityHeader($imapConnection, $entity_header, $ent_type0, $ent_type1, $ent_bound, $encoding, $charset, $filename);
+                  sqimap_get_entity_header($imapConnection, $entity_header, $ent_type0, $ent_type1, $ent_bound, $encoding, $charset, $filename);
                }
 
 
        as the actual message in the HTML.   It contains everything needed, including
        HTML Tags, Attachments at the bottom, etc.
     **/
-   function formatBody($message) {
-      include ("../config/config.php");
+   function formatBody($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["INFO"]["ID"];
       $urlmailbox = urlencode($message["INFO"]["MAILBOX"]);
-      $body = "";
 
       if (containsType($message, "text", "html", $ent_num)) {
-         $body .= decodeBody($message["ENTITIES"][$ent_num]["BODY"], $message["ENTITIES"][$ent_num]["ENCODING"]);
+         $body = decodeBody($message["ENTITIES"][$ent_num]["BODY"], $message["ENTITIES"][$ent_num]["ENCODING"]);
       } else if (containsType($message, "text", "plain", $ent_num)) {
-         $tmpbody = decodeBody($message["ENTITIES"][$ent_num]["BODY"], $message["ENTITIES"][$ent_num]["ENCODING"]);
-         $body .= "<TT>" . nl2br($tmpbody) . "</TT>";
+         $body = decodeBody($message["ENTITIES"][$ent_num]["BODY"], $message["ENTITIES"][$ent_num]["ENCODING"]);
       }
       // add other primary displaying message types here
       else {
          // find any type that's displayable
          if (containsType($message, "text", "any_type", $ent_num)) {
-            $tmpbody = decodeBody($message["ENTITIES"][$ent_num]["BODY"], $message["ENTITIES"][$ent_num]["ENCODING"]);
-            $body .= "<TT>" . nl2br($tmpbody) . "</TT>";
+            $body = decodeBody($message["ENTITIES"][$ent_num]["BODY"], $message["ENTITIES"][$ent_num]["ENCODING"]);
          } else if (containsType($message, "message", "any_type", $ent_num)) {
-            $tmpbody = decodeBody($message["ENTITIES"][$ent_num]["BODY"], $message["ENTITIES"][$ent_num]["ENCODING"]);
-            $body .= "<TT>" . nl2br($tmpbody) . "</TT>";
+            $body = decodeBody($message["ENTITIES"][$ent_num]["BODY"], $message["ENTITIES"][$ent_num]["ENCODING"]);
          }
       }
 
       /** If there are other types that shouldn't be formatted, add them here **/
       if ($message["ENTITIES"][$ent_num]["TYPE1"] != "html")
-         $body = translateText($body);
+         $body = translateText($body, $wrap_at);
 
-      $body .= "<BR><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>";
+
+      $body .= "<BR><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>";
 
       /** Display the ATTACHMENTS: message if there's more than one part **/
       if (count($message["ENTITIES"]) > 1) {
    function decodeBody($body, $encoding) {
       $encoding = strtolower($encoding);
 
-      if ($encoding == "us-ascii") {
-         $newbody = $body; // if only they all were this easy
-
-      } else if ($encoding == "quoted-printable") {
-         echo "$body";
-         $body = ereg_replace("=3D", "=", $body);
-         $body = ereg_replace("=\n", "", $body);
-         $body = ereg_replace("=20", "\n", $body);
-         $newbody= $body;
+      if ($encoding == "quoted-printable") {
+         $body = quoted_printable_decode($body);
 
+         while (ereg("=\n", $body))
+            $body = ereg_replace ("=\n", "", $body);
       } else if ($encoding == "base64") {
-         $newbody = base64_decode($body);
-
-      } else {
-         $newbody = $body;
+         $body = base64_decode($body);
       }
-      return $newbody;
+
+      // All other encodings are returned raw.
+      return $body;
+   }
+
+
+   // This functions decode strings that is encoded according to 
+   // RFC1522 (MIME Part Two: Message Header Extensions for Non-ASCII Text).
+   function rfc1522Decode ($string) {
+      // Recognizing only US-ASCII and ISO-8859. Other charsets should
+      // probably be recognized as well.
+      if (eregi('=\?(us-ascii|iso-8859-([0-9])+)\?(q|b)\?([^?]+)\?=', 
+                $string, $res)) {
+         if (ucfirst($res[3]) == "B") {
+            $replace = base64_decode($res[4]);
+         } else {
+            $replace = ereg_replace("_", " ", $res[4]);
+            $replace = quoted_printable_decode($replace);
+         }
+
+         if ($res[2] != "" && $res[2] == "15") {
+            // Remove all chararacters in iso-8859-15 that is not the same
+            // as in iso-8859-1
+            $replace = strtr($replace, "\244\246\250\255\264\270\274\275".
+                             "\276", "??????????");
+         } else if ($res[2] != "" && $res[2] != "1") {
+            // This gets rid of all characters with over 0x9F for other
+            // iso-8859 charsets.
+            $replace = strtr($replace, "\240\241\242\243\244\245\246\247".
+                             "\250\251\252\253\254\255\256\257".
+                             "\260\261\262\263\264\265\266\267".
+                             "\270\271\272\273\274\275\276\277".
+                             "\300\301\302\303\304\305\306\307".
+                             "\310\311\312\313\314\315\316\317".
+                             "\320\321\322\323\324\325\326\327".
+                             "\330\331\332\333\334\335\336\337".
+                             "\340\341\342\343\344\345\346\347".
+                             "\350\351\352\353\354\355\356\357".
+                             "\360\361\362\363\364\365\366\367".
+                             "\370\371\372\373\374\375\376\377", 
+                             "????????????????????????????????????????".
+                             "????????????????????????????????????????".
+                             "????????????????????????????????????????".
+                             "????????");
+         }
+
+         $string = eregi_replace
+            ('=\?(us-ascii|iso-8859-([0-9])+)\?(q|b)\?([^?]+)\?=',
+             $replace, $string);
+
+         return (rfc1522Decode($string));
+      } else         
+         return ($string);
    }
-?>
\ No newline at end of file
+
+?>