Browse Source

Made MIME much more efficient

Luke Ehresman 25 năm trước cách đây
mục cha
commit
54bc4869dd
6 tập tin đã thay đổi với 92 bổ sung73 xóa
  1. 3 6
      functions/mailbox.php
  2. 31 47
      functions/mime.php
  3. 8 1
      functions/smtp.php
  4. 3 0
      functions/strings.php
  5. 44 14
      src/download.php
  6. 3 5
      src/read_body.php

+ 3 - 6
functions/mailbox.php

@@ -264,7 +264,7 @@
          }
 
          /** 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)));
          }
 
@@ -440,7 +440,7 @@
       $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:") {
@@ -465,6 +465,7 @@
                $i++;
                $read[$i] = trim($read[$i]);
             }
+            $i--;
 
             /** Detect the boundary of a multipart message **/
             if (strpos(strtolower(trim($line)), "boundary=")) {
@@ -506,10 +507,6 @@
          $i++;
       }
 
-      if ( ($encoding == "us-ascii") && ($type0 != "text") && ($type0 != "message") ) {
-         $encoding = "base64";
-      }
-
       /** remove the header from the entity **/
       $i = 0;
       while (trim($read[$i]) != "") {

+ 31 - 47
functions/mime.php

@@ -45,15 +45,6 @@
                   }
                   $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 = "";
@@ -68,8 +59,16 @@
             $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;
@@ -83,26 +82,7 @@
       $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;
    }
@@ -137,6 +117,8 @@
        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.
        **/
@@ -144,21 +126,27 @@
          $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++) {
@@ -180,8 +168,9 @@
 
             $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;
    }
@@ -191,25 +180,20 @@
    /** 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;
       }

+ 8 - 1
functions/smtp.php

@@ -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");
 
@@ -52,9 +57,11 @@
 
       /** 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>";
    }
 ?>

+ 3 - 0
functions/strings.php

@@ -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);

+ 44 - 14
src/download.php

@@ -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);
 
@@ -17,28 +30,45 @@
    $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");
-?>
+?>

+ 3 - 5
src/read_body.php

@@ -147,12 +147,10 @@
    $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";
 
 ?>