added step for copying config_default.php to config.php
[squirrelmail.git] / functions / mime.php
index 4ddc0e5e8ce38a41ede87605ab8903eb5d372abb..417f89873163b00aefc8971c636d0191ae4d627e 100644 (file)
@@ -5,83 +5,92 @@
     **/
 
 
-   function decodeMime($body, $bound, $type0, $type1) {
+   /** 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) {
       if ($type0 == "multipart") {
-         if ($body[0] == "")
-            $i = 1;
-         else
-            $i = 0;
-
          $bound = trim($bound);
-         $bound = "--$bound";
-         while ($i < count($body)) {
-            if (trim($body[$i]) == $bound) {
-               $j = $i + 1;
+         $i = 0;
+         while (($i < count($body)) && (substr($body[$i], 0, strlen("--$bound--")) != "--$bound--")) {
+            if (trim($body[$i]) == "--$bound") {
+               $j = $i+1;
                $p = 0;
 
-               while ((substr(trim($body[$j]), 0, strlen($bound)) != $bound) && (trim($body[$j]) != "")) {
-                  $entity_header[$p] = $body[$j];
+               /** Lets find the header for this entity **/
+               /** 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++;
-                  $p++;
+               /** 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];
+                     $j++;
+                     $p++;
+                  }
+                  /** All of these values are getting passed back to us **/
+                  fetchEntityHeader($imapConnection, $entity_header, $ent_type0, $ent_type1, $ent_bound, $encoding, $charset, $filename);
                }
 
-               fetchEntityHeader($imapConnection, $entity_header, $ent_type0, $ent_type1, $ent_bound, $encoding, $charset, $filename);
 
-               if ($ent_type0 == "text") {
-                  while (substr(trim($body[$j]), 0, strlen($bound)) != $bound) {
-                     $entity_body[$p] = $body[$j];
+               /** 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--") {
+                     $ent_body[$y] = $body[$j];
+                     $y++;
                      $j++;
-                     $p++;
                   }
+                  $ent = decodeMime($ent_body, $ent_bound, $ent_type0, $ent_type1, $entities);
+                  $entities = $ent;
                } else {
                   $j++;
                   $entity_body = "";
-                  while (substr(trim($body[$j]), 0, strlen($bound)) != $bound) {
+                  while (substr(trim($body[$j]), 0, strlen("--$bound")) != "--$bound") {
                      $entity_body .= $body[$j];
                      $j++;
                   }
+                  $count = count($entities);
+                  $entities[$count] = getEntity($entity_body, $ent_bound, $ent_type0, $ent_type1, $encoding, $charset, $filename);
                }
-               $entity = getEntity($entity_body, $ent_bound, $ent_type0, $ent_type1, $encoding, $charset, $filename);
-
-               $q = count($full_message);
-               $full_message[$q] = $entity[0];
             }
             $i++;
          }
       } else {
-         $full_message = getEntity($body, $bound, $type0, $type1);
+         /** If this isn't a multipart message **/
+         $j = 0;
+         $entity_body = "";
+         while ($j < count($body)) {
+            $entity_body .= $body[$j];
+            $j++;
+         }
+
+         $count = count($entities);
+         $entities[$count] = getEntity($entity_body, $bound, $type0, $type1, $encoding, $charset, $filename);
       }
 
-      return $full_message;
+      return $entities;
    }
 
    /** This gets one entity's properties **/
    function getEntity($body, $bound, $type0, $type1, $encoding, $charset, $filename) {
-      $msg[0]["TYPE0"] = $type0;
-      $msg[0]["TYPE1"] = $type1;
-      $msg[0]["ENCODING"] = $encoding;
-      $msg[0]["CHARSET"] = $charset;
-      $msg[0]["FILENAME"] = $filename;
-
-      if ($type0 == "text") {
-         // error correcting if they didn't follow RFC standards
-         if (trim($type1) == "")
-            $type1 = "plain";
-
-         if ($type1 == "plain") {
-            for ($p = 0;$p < count($body);$p++) {
-               $msg[0]["BODY"][$p] = parsePlainTextMessage($body[$p]);
-            }
-         } else {
-            $msg[0]["BODY"] = $body;
-         }
-      } else {
-         $msg[0]["BODY"][0] = $body;
-      }
+      $msg["TYPE0"] = $type0;
+      $msg["TYPE1"] = $type1;
+      $msg["ENCODING"] = $encoding;
+      $msg["CHARSET"] = $charset;
+      $msg["FILENAME"] = $filename;
+
+      $msg["BODY"] = $body;
 
       return $msg;
    }
 
+   /** This will check whether or not the message contains a certain type.  It
+       searches through all the entities for a match.
+    **/
    function containsType($message, $type0, $type1, &$ent_num) {
       $type0 = strtolower($type0);
       $type1 = strtolower($type1);
       return false;
    }
 
-   function formatBody($message) {
+   /** 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.
+       **/
+      $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"]);
       } else if (containsType($message, "text", "plain", $ent_num)) {
          $body = decodeBody($message["ENTITIES"][$ent_num]["BODY"], $message["ENTITIES"][$ent_num]["ENCODING"]);
-      } // add other primary displaying message types here
-
+      }
+      // add other primary displaying message types here
       else {
          // find any type that's displayable
          if (containsType($message, "text", "any_type", $ent_num)) {
          }
       }
 
+      /** 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 .= "<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) {
-         $pos = count($body);
-         $body[$pos] .= "<BR><TT><U><B>ATTACHMENTS:</B></U></TT><BR>";
+         $body .= "<TABLE WIDTH=100% CELLSPACING=0 CELLPADDING=4 BORDER=0><TR><TD BGCOLOR=\"$color[0]\">";
+         $body .= "<TT><B>ATTACHMENTS:</B></TT>";
+         $body .= "</TD></TR><TR><TD BGCOLOR=\"$color[0]\">";
          $num = 0;
 
          for ($i = 0; $i < count($message["ENTITIES"]); $i++) {
             $num++;
             $filename = $message["ENTITIES"][$i]["FILENAME"];
             if (trim($filename) == "") {
-               $filename = "UNKNOWN_FORMAT_" . time() . $i;
-               $display_filename = "Attachment $i";
+               $display_filename = "untitled$i";
             } else {
                $display_filename = $filename;
             }
 
-            $body[$pos] .= "<TT>&nbsp;&nbsp;&nbsp;<A HREF=\"../data/$filename\">" . $display_filename . "</A>&nbsp;&nbsp;<SMALL>(TYPE: $type0/$type1)</SMALL></TT><BR>";
-            $file = fopen("../data/$filename", "w");
-
-            /** Determine what encoding type is used **/
-            if ($message["ENTITIES"][$i]["ENCODING"] == "base64") {
-               $thefile = base64_decode($message["ENTITIES"][$i]["BODY"][0]);
-            } else {
-               $thefile = $message["ENTITIES"][$i]["BODY"][0];
-            }
-
-            fwrite($file, $thefile);
-            fclose($file);
+            $urlMailbox = urlencode($message["INFO"]["MAILBOX"]);
+            $id = $message["INFO"]["ID"];
+            $body .= "<TT>&nbsp;&nbsp;&nbsp;<A HREF=\"../src/download.php?passed_id=$id&mailbox=$urlMailbox&passed_ent_id=$i\">" . $display_filename . "</A>&nbsp;&nbsp;<SMALL>(TYPE: $type0/$type1)</SMALL></TT><BR>";
          }
+         $body .= "</TD></TR></TABLE>";
       }
-
       return $body;
    }
 
+
+
+   /** this function decodes the body depending on the encoding type. **/
    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") {
-         for ($q=0; $q < count($body); $q++) {
-            if (substr(trim($body[$q]), -1) == "=") {
-               $body[$q] = trim($body[$q]);
-               $body[$q] = substr($body[$q], 0, strlen($body[$q])-1);
-            } else if (substr(trim($body[$q]), -3) == "=20") {
-               $body[$q] = trim($body[$q]);
-               $body[$q] = substr($body[$q], 0, strlen($body[$q])-3);
-               $body[$q] = "$body[$q]\n"; // maybe should be \n.. dunno
+         $body_ary = explode("\n", $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";
             }
          }
-         for ($q=0;$q < count($body);$q++) {
-            $body[$q] = ereg_replace("=3D", "=", $body[$q]);
+
+         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";
+         }
+
          $newbody = $body;
+      } else if ($encoding == "base64") {
+         $newbody = base64_decode($body);
+
       } else {
          $newbody = $body;
       }
       return $newbody;
    }
-?>
\ No newline at end of file
+?>