Explorar el Código

Made MIME much more efficient

Luke Ehresman hace 25 años
padre
commit
54bc4869dd
Se han modificado 6 ficheros con 92 adiciones y 73 borrados
  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 **/
          /** 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)));
             $header["ENCODING"] = strtolower(trim(substr($read[$i], 26)));
          }
          }
 
 
@@ -440,7 +440,7 @@
       $encoding = "us-ascii";
       $encoding = "us-ascii";
       $i = 0;
       $i = 0;
       while (trim($read[$i]) != "") {
       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)));
             $encoding = strtolower(trim(substr($read[$i], 26)));
 
 
          } else if (substr($read[$i], 0, 13) == "Content-Type:") {
          } else if (substr($read[$i], 0, 13) == "Content-Type:") {
@@ -465,6 +465,7 @@
                $i++;
                $i++;
                $read[$i] = trim($read[$i]);
                $read[$i] = trim($read[$i]);
             }
             }
+            $i--;
 
 
             /** Detect the boundary of a multipart message **/
             /** Detect the boundary of a multipart message **/
             if (strpos(strtolower(trim($line)), "boundary=")) {
             if (strpos(strtolower(trim($line)), "boundary=")) {
@@ -506,10 +507,6 @@
          $i++;
          $i++;
       }
       }
 
 
-      if ( ($encoding == "us-ascii") && ($type0 != "text") && ($type0 != "message") ) {
-         $encoding = "base64";
-      }
-
       /** remove the header from the entity **/
       /** remove the header from the entity **/
       $i = 0;
       $i = 0;
       while (trim($read[$i]) != "") {
       while (trim($read[$i]) != "") {

+ 31 - 47
functions/mime.php

@@ -45,15 +45,6 @@
                   }
                   }
                   $ent = decodeMime($ent_body, $ent_bound, $ent_type0, $ent_type1, $entities);
                   $ent = decodeMime($ent_body, $ent_bound, $ent_type0, $ent_type1, $entities);
                   $entities = $ent;
                   $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 {
                } else {
                   $j++;
                   $j++;
                   $entity_body = "";
                   $entity_body = "";
@@ -68,8 +59,16 @@
             $i++;
             $i++;
          }
          }
       } else {
       } 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);
          $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;
       return $entities;
@@ -83,26 +82,7 @@
       $msg["CHARSET"] = $charset;
       $msg["CHARSET"] = $charset;
       $msg["FILENAME"] = $filename;
       $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;
       return $msg;
    }
    }
@@ -137,6 +117,8 @@
        HTML Tags, Attachments at the bottom, etc.
        HTML Tags, Attachments at the bottom, etc.
     **/
     **/
    function formatBody($message) {
    function formatBody($message) {
+      include ("../config/config.php");
+
       /** this if statement checks for the entity to show as the primary message.  To
       /** 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.
           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"]);
          $body = decodeBody($message["ENTITIES"][$ent_num]["BODY"], $message["ENTITIES"][$ent_num]["ENCODING"]);
       } else if (containsType($message, "text", "plain", $ent_num)) {
       } else if (containsType($message, "text", "plain", $ent_num)) {
          $body = decodeBody($message["ENTITIES"][$ent_num]["BODY"], $message["ENTITIES"][$ent_num]["ENCODING"]);
          $body = decodeBody($message["ENTITIES"][$ent_num]["BODY"], $message["ENTITIES"][$ent_num]["ENCODING"]);
+         $body = "<TT>" . nl2br($body) . "</TT>";
       }
       }
       // add other primary displaying message types here
       // add other primary displaying message types here
       else {
       else {
          // find any type that's displayable
          // find any type that's displayable
          if (containsType($message, "text", "any_type", $ent_num)) {
          if (containsType($message, "text", "any_type", $ent_num)) {
             $body = decodeBody($message["ENTITIES"][$ent_num]["BODY"], $message["ENTITIES"][$ent_num]["ENCODING"]);
             $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)) {
          } else if (containsType($message, "message", "any_type", $ent_num)) {
             $body = decodeBody($message["ENTITIES"][$ent_num]["BODY"], $message["ENTITIES"][$ent_num]["ENCODING"]);
             $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 **/
       /** Display the ATTACHMENTS: message if there's more than one part **/
       if (count($message["ENTITIES"]) > 1) {
       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;
          $num = 0;
 
 
          for ($i = 0; $i < count($message["ENTITIES"]); $i++) {
          for ($i = 0; $i < count($message["ENTITIES"]); $i++) {
@@ -180,8 +168,9 @@
 
 
             $urlMailbox = urlencode($message["INFO"]["MAILBOX"]);
             $urlMailbox = urlencode($message["INFO"]["MAILBOX"]);
             $id = $message["INFO"]["ID"];
             $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;
       return $body;
    }
    }
@@ -191,25 +180,20 @@
    /** this function decodes the body depending on the encoding type. **/
    /** this function decodes the body depending on the encoding type. **/
    function decodeBody($body, $encoding) {
    function decodeBody($body, $encoding) {
       $encoding = strtolower($encoding);
       $encoding = strtolower($encoding);
+
       if ($encoding == "us-ascii") {
       if ($encoding == "us-ascii") {
          $newbody = $body; // if only they all were this easy
          $newbody = $body; // if only they all were this easy
+
       } else if ($encoding == "quoted-printable") {
       } 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") {
       } else if ($encoding == "base64") {
          $newbody = base64_decode($body);
          $newbody = base64_decode($body);
+
       } else {
       } else {
          $newbody = $body;
          $newbody = $body;
       }
       }

+ 8 - 1
functions/smtp.php

@@ -17,6 +17,8 @@
    }
    }
 
 
    function sendMessage($smtpServerAddress, $smtpPort, $username, $domain, $t, $c, $b, $subject, $body, $version) {
    function sendMessage($smtpServerAddress, $smtpPort, $username, $domain, $t, $c, $b, $subject, $body, $version) {
+      include("../config/config.php");
+
       $to = parseAddrs($t);
       $to = parseAddrs($t);
       $cc = parseAddrs($c);
       $cc = parseAddrs($c);
       $bcc = parseAddrs($b);
       $bcc = parseAddrs($b);
@@ -29,6 +31,8 @@
          echo "Error connecting to SMTP Server.<br>";
          echo "Error connecting to SMTP Server.<br>";
          echo "$errorNumber : $errorString<br>";
          echo "$errorNumber : $errorString<br>";
          exit;
          exit;
+      } else {
+         $tmp = fgets($smtpConnection, 1024);
       }
       }
 
 
       $to_list = getLineOfAddrs($to);
       $to_list = getLineOfAddrs($to);
@@ -36,6 +40,7 @@
 
 
       /** Lets introduce ourselves */
       /** Lets introduce ourselves */
       fputs($smtpConnection, "HELO $domain\n");
       fputs($smtpConnection, "HELO $domain\n");
+
       /** Ok, who is sending the message? */
       /** Ok, who is sending the message? */
       fputs($smtpConnection, "MAIL FROM:<$from>\n");
       fputs($smtpConnection, "MAIL FROM:<$from>\n");
 
 
@@ -52,9 +57,11 @@
 
 
       /** Lets start sending the actual message */
       /** Lets start sending the actual message */
       fputs($smtpConnection, "DATA\n");
       fputs($smtpConnection, "DATA\n");
+
       fputs($smtpConnection, "Subject: $subject\n"); // Subject
       fputs($smtpConnection, "Subject: $subject\n"); // Subject
       fputs($smtpConnection, "From: <$from>\n"); // Subject
       fputs($smtpConnection, "From: <$from>\n"); // Subject
       fputs($smtpConnection, "To: <$to_list>\n");    // Who it's TO
       fputs($smtpConnection, "To: <$to_list>\n");    // Who it's TO
+
       if ($cc_list) {
       if ($cc_list) {
          fputs($smtpConnection, "Cc: <$cc_list>\n"); // Who the CCs are
          fputs($smtpConnection, "Cc: <$cc_list>\n"); // Who the CCs are
       }
       }
@@ -64,9 +71,9 @@
       fputs($smtpConnection, "Content-Type: text/plain\n");
       fputs($smtpConnection, "Content-Type: text/plain\n");
 
 
       fputs($smtpConnection, "$body\n"); // send the body of the message
       fputs($smtpConnection, "$body\n"); // send the body of the message
+
       fputs($smtpConnection, ".\n"); // end the DATA part
       fputs($smtpConnection, ".\n"); // end the DATA part
       fputs($smtpConnection, "QUIT\n"); // log off
       fputs($smtpConnection, "QUIT\n"); // log off
-
       echo "</FONT>";
       echo "</FONT>";
    }
    }
 ?>
 ?>

+ 3 - 0
functions/strings.php

@@ -59,6 +59,9 @@
 
 
    /** Returns an array of email addresses **/
    /** Returns an array of email addresses **/
    function parseAddrs($text) {
    function parseAddrs($text) {
+      if (trim($text) == "") {
+         return;
+      }
       $text = str_replace(" ", "", $text);
       $text = str_replace(" ", "", $text);
       $text = str_replace(",", ";", $text);
       $text = str_replace(",", ";", $text);
       $array = explode(";", $text);
       $array = explode(";", $text);

+ 44 - 14
src/download.php

@@ -7,6 +7,19 @@
    include("../functions/mailbox.php");
    include("../functions/mailbox.php");
    include("../functions/date.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);
    $imapConnection = loginToImapServer($username, $key, $imapServerAddress);
    selectMailbox($imapConnection, $mailbox, $numMessages);
    selectMailbox($imapConnection, $mailbox, $numMessages);
 
 
@@ -17,28 +30,45 @@
    $type0 = $message["ENTITIES"][$passed_ent_id]["TYPE0"];
    $type0 = $message["ENTITIES"][$passed_ent_id]["TYPE0"];
    $type1 = $message["ENTITIES"][$passed_ent_id]["TYPE1"];
    $type1 = $message["ENTITIES"][$passed_ent_id]["TYPE1"];
    $filename = $message["ENTITIES"][$passed_ent_id]["FILENAME"];
    $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-type: $type0/$type1");
             header("Content-Disposition: attachment; filename=\"$filename\"");
             header("Content-Disposition: attachment; filename=\"$filename\"");
             echo $body;
             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-type: $type0/$type1");
             header("Content-Disposition: attachment; filename=\"$filename\"");
             header("Content-Disposition: attachment; filename=\"$filename\"");
             echo $body;
             echo $body;
-         }
-         break;
-      default:
-         header("Content-type: $type0/$type1");
-         header("Content-Disposition: attachment; filename=\"$filename\"");
-         echo $body;
-         break;
+            break;
+      }
    }
    }
 
 
    fputs($imapConnection, "1 logout\n");
    fputs($imapConnection, "1 logout\n");
-?>
+?>

+ 3 - 5
src/read_body.php

@@ -147,12 +147,10 @@
    $body = formatBody($message);
    $body = formatBody($message);
    echo "<BR>";
    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";
    echo "</TABLE>\n";
 
 
 ?>
 ?>