Made MIME much more efficient
authorlkehresman <lkehresman@7612ce4b-ef26-0410-bec9-ea0150e637f0>
Wed, 5 Jan 2000 16:48:14 +0000 (16:48 +0000)
committerlkehresman <lkehresman@7612ce4b-ef26-0410-bec9-ea0150e637f0>
Wed, 5 Jan 2000 16:48:14 +0000 (16:48 +0000)
git-svn-id: https://svn.code.sf.net/p/squirrelmail/code/trunk/squirrelmail@103 7612ce4b-ef26-0410-bec9-ea0150e637f0

functions/mailbox.php
functions/mime.php
functions/smtp.php
functions/strings.php
src/download.php
src/read_body.php

index bb28187..4cfec18 100644 (file)
          }
 
          /** ENCODING TYPE **/
-         else if (substr($read[$i], 0, 26) == "Content-Transfer-Encoding:") {
+         else if (substr(strtolower($read[$i]), 0, 26) == "content-transfer-encoding:") {
             $header["ENCODING"] = strtolower(trim(substr($read[$i], 26)));
          }
 
       $encoding = "us-ascii";
       $i = 0;
       while (trim($read[$i]) != "") {
-         if (substr($read[$i], 0, 26) == "Content-Transfer-Encoding:") {
+         if (substr(strtolower($read[$i]), 0, 26) == "content-transfer-encoding:") {
             $encoding = strtolower(trim(substr($read[$i], 26)));
 
          } else if (substr($read[$i], 0, 13) == "Content-Type:") {
                $i++;
                $read[$i] = trim($read[$i]);
             }
+            $i--;
 
             /** Detect the boundary of a multipart message **/
             if (strpos(strtolower(trim($line)), "boundary=")) {
          $i++;
       }
 
-      if ( ($encoding == "us-ascii") && ($type0 != "text") && ($type0 != "message") ) {
-         $encoding = "base64";
-      }
-
       /** remove the header from the entity **/
       $i = 0;
       while (trim($read[$i]) != "") {
index 8186b74..d6e98d8 100644 (file)
                   }
                   $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 = "";
             $i++;
          }
       } else {
+         /** If this isn't a multipart message **/
+         $j = 0;
+         $entity_body = "";
+         while ((substr(trim($body[$j]), 0, strlen("--$bound")) != "--$bound") && ($j < count($body))) {
+            $entity_body .= $body[$j];
+            $j++;
+         }
+
          $count = count($entities);
-         $entities[$count] = getEntity($body, $bound, $type0, $type1, $encoding, $charset, $filename);
+         $entities[$count] = getEntity($entity_body, $bound, $type0, $type1, $encoding, $charset, $filename);
       }
 
       return $entities;
       $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) == "")
-            $type1 = "plain";
-
-         if ($type1 == "plain") {
-            for ($p = 0;$p < count($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["BODY"] = $body;
-         }
-      } else {
-         $msg["BODY"][0] = $body;
-      }
+      $msg["BODY"] = $body;
 
       return $msg;
    }
        HTML Tags, Attachments at the bottom, etc.
     **/
    function formatBody($message) {
+      include ("../config/config.php");
+
       /** 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.
        **/
          $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"]);
+         $body = "<TT>" . nl2br($body) . "</TT>";
       }
       // 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"]);
+            $body = "<TT>" . nl2br($body) . "</TT>";
          } else if (containsType($message, "message", "any_type", $ent_num)) {
             $body = decodeBody($message["ENTITIES"][$ent_num]["BODY"], $message["ENTITIES"][$ent_num]["ENCODING"]);
+            $body = "<TT>" . nl2br($body) . "</TT>";
          }
       }
 
+      $body .= "<BR>";
+
       /** 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++) {
 
             $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>";
+            $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
-            }
-         }
-         for ($q=0;$q < count($body);$q++) {
-            $body[$q] = ereg_replace("=3D", "=", $body[$q]);
-         }
-         $newbody = $body;
+         echo "$body";
+         $body = ereg_replace("=3D", "=", $body);
+         $body = ereg_replace("=\n", "", $body);
+         $body = ereg_replace("=20", "\n", $body);
+         $newbody= $body;
+
       } else if ($encoding == "base64") {
          $newbody = base64_decode($body);
+
       } else {
          $newbody = $body;
       }
index ad6aa4c..2274bc1 100644 (file)
@@ -17,6 +17,8 @@
    }
 
    function sendMessage($smtpServerAddress, $smtpPort, $username, $domain, $t, $c, $b, $subject, $body, $version) {
+      include("../config/config.php");
+
       $to = parseAddrs($t);
       $cc = parseAddrs($c);
       $bcc = parseAddrs($b);
@@ -29,6 +31,8 @@
          echo "Error connecting to SMTP Server.<br>";
          echo "$errorNumber : $errorString<br>";
          exit;
+      } else {
+         $tmp = fgets($smtpConnection, 1024);
       }
 
       $to_list = getLineOfAddrs($to);
@@ -36,6 +40,7 @@
 
       /** Lets introduce ourselves */
       fputs($smtpConnection, "HELO $domain\n");
+
       /** Ok, who is sending the message? */
       fputs($smtpConnection, "MAIL FROM:<$from>\n");
 
 
       /** Lets start sending the actual message */
       fputs($smtpConnection, "DATA\n");
+
       fputs($smtpConnection, "Subject: $subject\n"); // Subject
       fputs($smtpConnection, "From: <$from>\n"); // Subject
       fputs($smtpConnection, "To: <$to_list>\n");    // Who it's TO
+
       if ($cc_list) {
          fputs($smtpConnection, "Cc: <$cc_list>\n"); // Who the CCs are
       }
@@ -64,9 +71,9 @@
       fputs($smtpConnection, "Content-Type: text/plain\n");
 
       fputs($smtpConnection, "$body\n"); // send the body of the message
+
       fputs($smtpConnection, ".\n"); // end the DATA part
       fputs($smtpConnection, "QUIT\n"); // log off
-
       echo "</FONT>";
    }
 ?>
\ No newline at end of file
index 66cd5de..dce7f1a 100644 (file)
@@ -59,6 +59,9 @@
 
    /** Returns an array of email addresses **/
    function parseAddrs($text) {
+      if (trim($text) == "") {
+         return;
+      }
       $text = str_replace(" ", "", $text);
       $text = str_replace(",", ";", $text);
       $array = explode(";", $text);
index b69c01b..07e6e14 100644 (file)
@@ -7,6 +7,19 @@
    include("../functions/mailbox.php");
    include("../functions/date.php");
 
+   function viewText($color, $body, $id, $entid, $mailbox) {
+      echo "<HTML><BODY TEXT=\"$color[8]\" BGCOLOR=\"$color[4]\" LINK=\"$color[7]\" VLINK=\"$color[7]\" ALINK=\"$color[7]\">\n";
+      displayPageHeader($color, "None");
+
+      echo "<BR><TABLE WIDTH=90% BORDER=0 CELLSPACING=0 CELLPADDING=2 ALIGN=CENTER><TR><TD BGCOLOR=\"$color[0]\">";
+      echo "<B><CENTER>Viewing a plain text attachment</CENTER></B>";
+      echo "</TD></TR><TR><TD BGCOLOR=\"$color[4]\">";
+      $urlmailbox = urlencode($mailbox);
+      echo "<FONT FACE=\"Arial, Helvetica\"><A HREF=\"../src/download.php?absolute_dl=true&passed_id=$id&passed_ent_id=$entid&mailbox=$urlmailbox\">Download this as a file</A><BR><BR></FONT><TT>";
+      echo nl2br($body);
+      echo "</TT></TD></TR></TABLE>";
+   }
+
    $imapConnection = loginToImapServer($username, $key, $imapServerAddress);
    selectMailbox($imapConnection, $mailbox, $numMessages);
 
    $type0 = $message["ENTITIES"][$passed_ent_id]["TYPE0"];
    $type1 = $message["ENTITIES"][$passed_ent_id]["TYPE1"];
    $filename = $message["ENTITIES"][$passed_ent_id]["FILENAME"];
-   $body = decodeBody($message["ENTITIES"][$passed_ent_id]["BODY"][0], $message["ENTITIES"][$passed_ent_id]["ENCODING"]);
 
+   if (strlen($filename) < 1) {
+      $filename = "message" . time();
+   }
 
-   switch ($type0) {
-      case "image":
-         if (($type1 == "jpeg") || ($type1 == "jpg") || ($type1 == "gif") || ($type1 == "png")) {
-            /** Add special instructions to view images inline here **/
+   if ($absolute_dl == "true") {
+      switch($type0) {
+         case "text":
+            $body = decodeBody($message["ENTITIES"][$passed_ent_id]["BODY"], $message["ENTITIES"][$passed_ent_id]["ENCODING"]);
+            header("Content-type: $type0/$type1");
+            header("Content-Disposition: attachment; filename=\"$filename\"");
+            if ($type1 != "html")
+               echo nl2br($body);
+            break;
+         default:
+            $body = decodeBody($message["ENTITIES"][$passed_ent_id]["BODY"], $message["ENTITIES"][$passed_ent_id]["ENCODING"]);
             header("Content-type: $type0/$type1");
             header("Content-Disposition: attachment; filename=\"$filename\"");
             echo $body;
-         } else {
+            break;
+      }
+   } else {
+      switch ($type0) {
+         case "text":
+            $body = decodeBody($message["ENTITIES"][$passed_ent_id]["BODY"], $message["ENTITIES"][$passed_ent_id]["ENCODING"]);
+            viewText($color, $body, $passed_id, $passed_ent_id, $mailbox);
+            break;
+         case "message":
+            $body = decodeBody($message["ENTITIES"][$passed_ent_id]["BODY"], $message["ENTITIES"][$passed_ent_id]["ENCODING"]);
+            viewText($color, $body, $passed_id, $passed_ent_id, $mailbox);
+            break;
+         default:
+            $body = decodeBody($message["ENTITIES"][$passed_ent_id]["BODY"], $message["ENTITIES"][$passed_ent_id]["ENCODING"]);
             header("Content-type: $type0/$type1");
             header("Content-Disposition: attachment; filename=\"$filename\"");
             echo $body;
-         }
-         break;
-      default:
-         header("Content-type: $type0/$type1");
-         header("Content-Disposition: attachment; filename=\"$filename\"");
-         echo $body;
-         break;
+            break;
+      }
    }
 
    fputs($imapConnection, "1 logout\n");
-?>
\ No newline at end of file
+?>
index 1ec73d1..32fd6ca 100644 (file)
    $body = formatBody($message);
    echo "<BR>";
 
-   for ($i = 0; $i < count($body); $i++) {
-      echo "$body[$i]";
-   }
+   echo "$body";
 
-   echo "   <BR></TD></TR>\n";
-   echo "   <TR><TD BGCOLOR=\"$color[0]\">&nbsp;</TD></TR>";
+   echo "   </TD></TR>\n";
+   echo "   <TR><TD BGCOLOR=\"$color[9]\">&nbsp;</TD></TR>";
    echo "</TABLE>\n";
 
 ?>
\ No newline at end of file