Rewrote multipart/* algorithm to be recursive
authorlkehresman <lkehresman@7612ce4b-ef26-0410-bec9-ea0150e637f0>
Tue, 4 Jan 2000 19:35:19 +0000 (19:35 +0000)
committerlkehresman <lkehresman@7612ce4b-ef26-0410-bec9-ea0150e637f0>
Tue, 4 Jan 2000 19:35:19 +0000 (19:35 +0000)
git-svn-id: https://svn.code.sf.net/p/squirrelmail/code/trunk/squirrelmail@102 7612ce4b-ef26-0410-bec9-ea0150e637f0

ChangeLog
functions/mailbox.php
functions/mime.php
functions/smtp.php
src/read_body.php

index 3e2a8a32e7763905797bff7307f1ecc6a19b8b30..739ed346d9a91127e51f18d157e9d3b127ff78c7 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,6 +1,6 @@
 Version 0.2 -- January 02, 2000
 -------------------------------
-- Attachment support (better MIME support in general)
+- Attachment support (much better MIME support in general)
 - Themeable support replaced Custom Colors.  Themes are pluggable.
 
 
index 38fe457e9a92a73c0fbdb51ef040042da22f8092..bb281879ab2f75af62657e237546dd5160e588f7 100644 (file)
       $message["INFO"]["MAILBOX"] = $mailbox;
       $message["HEADER"] = fetchHeader($imapConnection, $id);
       $message["ENTITIES"] = fetchBody($imapConnection, $message["HEADER"]["BOUNDARY"], $id, $message["HEADER"]["TYPE0"], $message["HEADER"]["TYPE1"]);
-
       return $message;
    }
 
       $read = $entity;
    }
 
+   function parseHTMLMessage($line) {
+      /** Add any parsing you want to in here **/
+      return $line;
+   }
+
    function parsePlainTextMessage($line) {
+      /** Add any parsing you want to in here */
+
       $line = "^^$line";
 
       if ((strpos(strtolower($line), "<!") == false) &&
       $line = str_replace("\t", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;", $line);
       $line = str_replace("\n", "", $line);
 
-      /** if >> or > are found at the beginning of a line, I'll assume that was
-          replied text, so make it different colors **/
       if (strpos(trim(str_replace("&nbsp;", "", $line)), "&gt;&gt;") == 2) {
          $line = substr($line, 2, strlen($line));
          $line = "<TT><FONT COLOR=FF0000>$line</FONT></TT><BR>\n";
          $line = "<TT><FONT COLOR=000000>$line</FONT></TT><BR>\n";
       }
 
-      /** This translates "http://" into a link.  It could be made better to accept
-          "www" and "mailto" also.  That should probably be added later. **/
       if (strpos(strtolower($line), "http://") != false) {
          $line = ereg_replace("<BR>", "", $line);
          $start = strpos(strtolower($line), "http://");
index 33148ada28f46a7996e777abc8d9d48ca5dbe1e2..8186b747961b2dc1f2fa98a930de0f5ce9eebd8d 100644 (file)
@@ -5,64 +5,85 @@
     **/
 
 
-   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;
+         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) {
+               /** 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++;
+                  }
+                  $ent = decodeMime($ent_body, $ent_bound, $ent_type0, $ent_type1, $entities);
+                  $entities = $ent;
+               } else if ($ent_type0 == "text") {
+                  while (substr(trim($body[$j]), 0, strlen("--$bound")) != "--$bound") {
                      $entity_body[$p] = $body[$j];
                      $j++;
                      $p++;
                   }
+                  $count = count($entities);
+                  $entities[$count] = getEntity($entity_body, $ent_bound, $ent_type0, $ent_type1, $encoding, $charset, $filename);
+
                } 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);
+         $count = count($entities);
+         $entities[$count] = getEntity($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;
+      $msg["TYPE0"] = $type0;
+      $msg["TYPE1"] = $type1;
+      $msg["ENCODING"] = $encoding;
+      $msg["CHARSET"] = $charset;
+      $msg["FILENAME"] = $filename;
 
+      $msg["BODY"][0] = $body;
       if ($type0 == "text") {
          // error correcting if they didn't follow RFC standards
          if (trim($type1) == "")
 
          if ($type1 == "plain") {
             for ($p = 0;$p < count($body);$p++) {
-               $msg[0]["BODY"][$p] = parsePlainTextMessage($body[$p]);
+               $msg["BODY"][$p] = parsePlainTextMessage($body[$p]);
+            }
+         } else if ($type1 == "html") {
+            for ($p = 0;$p < count($body);$p++) {
+               $msg["BODY"][$p] = parseHTMLMessage($body[$p]);
             }
          } else {
-            $msg[0]["BODY"] = $body;
+            $msg["BODY"] = $body;
          }
       } else {
-         $msg[0]["BODY"][0] = $body;
+         $msg["BODY"][0] = $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;
    }
 
+   /** 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) {
+      /** 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.
+       **/
       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)) {
          }
       }
 
-
       /** Display the ATTACHMENTS: message if there's more than one part **/
       if (count($message["ENTITIES"]) > 1) {
          $pos = count($body);
                $display_filename = $filename;
             }
 
-
             $urlMailbox = urlencode($message["INFO"]["MAILBOX"]);
             $id = $message["INFO"]["ID"];
             $body[$pos] .= "<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>";
-
          }
       }
-
       return $body;
    }
 
+
+
+   /** this function decodes the body depending on the encoding type. **/
    function decodeBody($body, $encoding) {
       $encoding = strtolower($encoding);
       if ($encoding == "us-ascii") {
index 6241c02f0cf5a08abdc75a26e8edf87c31a47b97..ad6aa4c275a5f2d8c05abdcb087615c736af7d48 100644 (file)
@@ -61,7 +61,7 @@
       fputs($smtpConnection, "X-Mailer: SquirrelMail (version $version)\n"); // Identify SquirrelMail
       fputs($smtpConnection, "Reply-To: $from\n");
       fputs($smtpConnection, "MIME-Version: 1.0\n");
-      fputs($smtpConnection, "Content-Type: text\n");
+      fputs($smtpConnection, "Content-Type: text/plain\n");
 
       fputs($smtpConnection, "$body\n"); // send the body of the message
       fputs($smtpConnection, ".\n"); // end the DATA part
index 050f1c5ae7b815528aa3a14442a0c51fb6412a1a..1ec73d19434832d97deeefda48711cbdd92dedf6 100644 (file)
@@ -13,7 +13,6 @@
    // $message contains all information about the message
    // including header and body
    $message = fetchMessage($imapConnection, $passed_id, $mailbox);
-   echo "$mailbox, $passed_id<BR>";
 
    echo "<HTML>";
    echo "<BODY TEXT=\"$color[8]\" BGCOLOR=\"$color[4]\" LINK=\"$color[7]\" VLINK=\"$color[7]\" ALINK=\"$color[7]\">\n";
@@ -79,9 +78,9 @@
    $subject = htmlspecialchars(stripslashes($message["HEADER"]["SUBJECT"]));
 
    echo "<BR>";
-   echo "<TABLE COLS=1 WIDTH=98% BORDER=0 ALIGN=CENTER CELLPADDING=2>\n";
+   echo "<TABLE COLS=1 CELLSPACING=0 WIDTH=98% BORDER=0 ALIGN=CENTER CELLPADDING=0>\n";
    echo "   <TR><TD BGCOLOR=\"$color[0]\" WIDTH=100%>";
-   echo "      <TABLE WIDTH=100% BORDER=0 COLS=2>";
+   echo "      <TABLE WIDTH=100% CELLSPACING=0 BORDER=0 COLS=2 CELLPADDING=3>";
    echo "         <TR>";
    echo "            <TD ALIGN=LEFT WIDTH=50%>";
    echo "               <FONT FACE=\"Arial,Helvetica\" SIZE=2>";
    echo "         </TR>";
    echo "      </TABLE>";
    echo "   </TD></TR>";
-   echo "   <TR><TD BGCOLOR=\"$color[9]\" WIDTH=100%>";
+   echo "   <TR><TD CELLSPACING=0 WIDTH=100%>";
    echo "   <TABLE COLS=2 WIDTH=100% BORDER=0 CELLSPACING=0 CELLPADDING=3>\n";
    echo "      <TR>\n";
    /** subject **/
-   echo "         <TD BGCOLOR=\"$color[0]\" WIDTH=15% ALIGN=RIGHT>\n";
+   echo "         <TD BGCOLOR=\"$color[4]\" WIDTH=15% ALIGN=RIGHT>\n";
    echo "            <FONT FACE=\"Arial,Helvetica\">Subject:</FONT>\n";
    echo "         </TD><TD BGCOLOR=\"$color[4]\" WIDTH=85%>\n";
    echo "            <FONT FACE=\"Arial,Helvetica\"><B>$subject</B></FONT>\n";
    echo "      </TR>\n";
    /** from **/
    echo "      <TR>\n";
-   echo "         <TD BGCOLOR=\"$color[0]\" WIDTH=15% ALIGN=RIGHT>\n";
+   echo "         <TD BGCOLOR=\"$color[4]\" WIDTH=15% ALIGN=RIGHT>\n";
    echo "            <FONT FACE=\"Arial,Helvetica\">From:</FONT>\n";
    echo "         </TD><TD BGCOLOR=\"$color[4]\" WIDTH=85%>\n";
    echo "            <FONT FACE=\"Arial,Helvetica\"><B>$from_name</B></FONT>\n";
    echo "      </TR>\n";
    /** date **/
    echo "      <TR>\n";
-   echo "         <TD BGCOLOR=\"$color[0]\" WIDTH=15% ALIGN=RIGHT>\n";
+   echo "         <TD BGCOLOR=\"$color[4]\" WIDTH=15% ALIGN=RIGHT>\n";
    echo "            <FONT FACE=\"Arial,Helvetica\">Date:</FONT>\n";
    echo "         </TD><TD BGCOLOR=\"$color[4]\" WIDTH=85%>\n";
    echo "            <FONT FACE=\"Arial,Helvetica\"><B>$dateString</B></FONT>\n";
    echo "      </TR>\n";
    /** to **/
    echo "      <TR>\n";
-   echo "         <TD BGCOLOR=\"$color[0]\" WIDTH=15% ALIGN=RIGHT VALIGN=TOP>\n";
+   echo "         <TD BGCOLOR=\"$color[4]\" WIDTH=15% ALIGN=RIGHT VALIGN=TOP>\n";
    echo "            <FONT FACE=\"Arial,Helvetica\">To:</FONT>\n";
    echo "         </TD><TD BGCOLOR=\"$color[4]\" WIDTH=85% VALIGN=TOP>\n";
    echo "            <FONT FACE=\"Arial,Helvetica\"><B>$to_string</B></FONT>\n";
    /** cc **/
    if ($message["HEADER"]["CC"][0]) {
       echo "      <TR>\n";
-      echo "         <TD BGCOLOR=\"$color[0]\" WIDTH=15% ALIGN=RIGHT VALIGN=TOP>\n";
+      echo "         <TD BGCOLOR=\"$color[4]\" WIDTH=15% ALIGN=RIGHT VALIGN=TOP>\n";
       echo "            <FONT FACE=\"Arial,Helvetica\">Cc:</FONT>\n";
       echo "         </TD><TD BGCOLOR=\"$color[4]\" WIDTH=85% VALIGN=TOP>\n";
       echo "            <FONT FACE=\"Arial,Helvetica\"><B>$cc_string</B></FONT>\n";
       echo "         </TD>\n";
       echo "      </TR>\n";
    }
-   echo "   </TABLE></TD></TR>\n";
+   echo "</TABLE>";
+   echo "   </TD></TR>";
 
    echo "   <TR><TD BGCOLOR=\"$color[4]\" WIDTH=100%>\n";
    $body = formatBody($message);
+   echo "<BR>";
 
    for ($i = 0; $i < count($body); $i++) {
       echo "$body[$i]";