major speed improvements
[squirrelmail.git] / functions / mime.php
index dc5ef1aec08ec1b3d5433023260369753a61fa8c..ecbc6abcf217047c0cfd5e3986afa34e64b77222 100644 (file)
@@ -1,14 +1,20 @@
 <?
    /** mime.php
     **
-    ** This contains the functions necessary to detect and decode MIME messages.
+    ** This contains the functions necessary to detect and decode MIME
+    ** messages.
+    **
     **/
 
+   $mime_php = true;
+
+   if (!isset($i18n_php))
+      include "../functions/i18n.php";
 
    /** 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, $bound, $type0, $type1, &$entities) {
+   function decodeMime($body, $bound, $type0, $type1, $encoding, $charset, &$entities) {
       if ($type0 == "multipart") {
          $bound = trim($bound);
          $i = 0;
                $p = 0;
 
                /** Lets find the header for this entity **/
-               /** If the first line after the boundary is blank, we use default values **/
+               /** If the first line after the boundary is blank, we
+                   use default values **/
                if (trim($body[$j]) == "") {
                   $ent_type0 = "text";
                   $ent_type1 = "plain";
                   $charset = "us-ascii";
                   $j++;
-               /** If the first line ISNT blank, read in the header for this entity **/
+               /** If the first line ISNT blank, read in the header
+                   for this entity **/
                } else {
                   while ((substr(trim($body[$j]), 0, strlen("--$bound")) != "--$bound") && (trim($body[$j]) != "")) {
                      $entity_header[$p] = $body[$j];
@@ -36,7 +44,8 @@
                }
 
 
-               /** OK, we have the header information, now lets decide what to do with it **/
+               /** OK, we have the header information, now lets decide
+                   what to do with it **/
                if ($ent_type0 == "multipart") {
                   $y = 0;
                   while (substr($body[$j], 0, strlen("--$bound--")) != "--$bound--") {
@@ -44,7 +53,7 @@
                      $y++;
                      $j++;
                   }
-                  $ent = decodeMime($ent_body, $ent_bound, $ent_type0, $ent_type1, $entities);
+                  $ent = decodeMime($ent_body, $ent_bound, $ent_type0, $ent_type1, $charset, $entities);
                   $entities = $ent;
                } else {
                   $j++;
       return false;
    }
 
-   /** This returns a parsed string called $body.  That string can then be displayed
-       as the actual message in the HTML.   It contains everything needed, including
-       HTML Tags, Attachments at the bottom, etc.
+   /** This returns a parsed string called $body. That string can then
+       be displayed as the actual message in the HTML. It contains
+       everything needed, including HTML Tags, Attachments at the
+       bottom, etc.
     **/
    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.
+      /** 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"]);
 
       if (containsType($message, "text", "html", $ent_num)) {
          $body = decodeBody($message["ENTITIES"][$ent_num]["BODY"], $message["ENTITIES"][$ent_num]["ENCODING"]);
+         $charset = $message["ENTITIES"][$ent_num]["CHARSET"];
       } else if (containsType($message, "text", "plain", $ent_num)) {
          $body = decodeBody($message["ENTITIES"][$ent_num]["BODY"], $message["ENTITIES"][$ent_num]["ENCODING"]);
+         $charset = $message["ENTITIES"][$ent_num]["CHARSET"];
       }
       // add other primary displaying message types here
       else {
          // find any type that's displayable
          if (containsType($message, "text", "any_type", $ent_num)) {
             $body = decodeBody($message["ENTITIES"][$ent_num]["BODY"], $message["ENTITIES"][$ent_num]["ENCODING"]);
+            $charset = $message["ENTITIES"][$ent_num]["CHARSET"];
          } else if (containsType($message, "message", "any_type", $ent_num)) {
             $body = decodeBody($message["ENTITIES"][$ent_num]["BODY"], $message["ENTITIES"][$ent_num]["ENCODING"]);
+            $charset = $message["ENTITIES"][$ent_num]["CHARSET"];
          }
       }
 
-      /** If there are other types that shouldn't be formatted, add them here **/
+      /** If there are other types that shouldn't be formatted, add
+          them here **/
       if ($message["ENTITIES"][$ent_num]["TYPE1"] != "html")
-         $body = translateText($body, $wrap_at);
+         $body = translateText($body, $wrap_at, $charset);
 
 
       $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>";
    function decodeBody($body, $encoding) {
       $encoding = strtolower($encoding);
 
-      if ($encoding == "us-ascii") {
-         $newbody = $body; // if only they all were this easy
+      if ($encoding == "quoted-printable") {
+         $body = quoted_printable_decode($body);
 
-      } else if ($encoding == "quoted-printable") {
-         $body_ary = explode("\n", $body);
+         while (ereg("=\n", $body))
+            $body = ereg_replace ("=\n", "", $body);
+      } else if ($encoding == "base64") {
+         $body = base64_decode($body);
+      }
 
-         for ($q=0; $q < count($body_ary); $q++) {
-            if (substr(trim($body_ary[$q]), -1) == "=") {
-               $body_ary[$q] = trim($body_ary[$q]);
-               $body_ary[$q] = substr($body_ary[$q], 0, strlen($body_ary[$q])-1);
-            } else if (substr(trim($body_ary[$q]), -3) == "=20") {
-               $body_ary[$q] = trim($body_ary[$q]);
-               $body_ary[$q] = substr($body_ary[$q], 0, strlen($body_ary[$q])-3);
-               $body_ary[$q] = "$body_ary[$q]\n";
-            }
-         }
+      // All other encodings are returned raw.
+      return $body;
+   }
 
-         for ($q=0;$q < count($body_ary);$q++) {
-            $body_ary[$q] = ereg_replace("=3D", "=", $body_ary[$q]);
-         }
 
-         $body = "";
-         for ($i = 0; $i < count($body_ary); $i++) {
-            $body .= "$body_ary[$i]\n";
+   // This functions decode strings that is encoded according to 
+   // RFC1522 (MIME Part Two: Message Header Extensions for Non-ASCII Text).
+   function decodeHeader ($string) {
+      if (eregi('=\?([^?]+)\?(q|b)\?([^?]+)\?=', 
+                $string, $res)) {
+         if (ucfirst($res[2]) == "B") {
+            $replace = base64_decode($res[3]);
+         } else {
+            $replace = ereg_replace("_", " ", $res[3]);
+            $replace = quoted_printable_decode($replace);
          }
 
-         $newbody = $body;
-      } else if ($encoding == "base64") {
-         $newbody = base64_decode($body);
+         $replace = charset_decode ($res[1], $replace);
 
-      } else {
-         $newbody = $body;
-      }
-      return $newbody;
+         $string = eregi_replace
+            ('=\?([^?]+)\?(q|b)\?([^?]+)\?=',
+             $replace, $string);
+         // In case there should be more encoding in the string: recurse
+         return (decodeHeader($string));
+      } else         
+         return ($string);
    }
+
 ?>