Browse Source

added attachment support

Luke Ehresman 25 years ago
parent
commit
2e7bbc9712
3 changed files with 47 additions and 23 deletions
  1. 29 1
      functions/mailbox.php
  2. 17 21
      functions/mime.php
  3. 1 1
      src/read_body.php

+ 29 - 1
functions/mailbox.php

@@ -317,6 +317,18 @@
                $header["CHARSET"] = "us-ascii";
                $header["CHARSET"] = "us-ascii";
             }
             }
 
 
+            /** Detects filename if any **/
+            if (strpos(strtolower(trim($line)), "name=")) {
+               $pos = strpos($line, "name=") + 5;
+               $name = trim($line);
+               if (strpos($line, " ", $pos) > 0) {
+                  $name = substr($name, $pos, strpos($line, " ", $pos));
+               } else {
+                  $name = substr($name, $pos);
+               }
+               $name = str_replace("\"", "", $name);
+               $header["FILENAME"] = $name;
+            }
          }
          }
 
 
          /** REPLY-TO **/
          /** REPLY-TO **/
@@ -420,7 +432,7 @@
       return decodeMime($body, $bound, $type0, $type1);
       return decodeMime($body, $bound, $type0, $type1);
    }
    }
 
 
-   function fetchEntityHeader($imapConnection, &$read, &$type0, &$type1, &$bound, &$encoding, &$charset) {
+   function fetchEntityHeader($imapConnection, &$read, &$type0, &$type1, &$bound, &$encoding, &$charset, &$filename) {
       /** defaults... if the don't get overwritten, it will display text **/
       /** defaults... if the don't get overwritten, it will display text **/
       $type0 = "text";
       $type0 = "text";
       $type1 = "plain";
       $type1 = "plain";
@@ -442,12 +454,15 @@
                $type0 = $cont;
                $type0 = $cont;
             }
             }
 
 
+            $read[$i] = trim($read[$i]);
             $line = $read[$i];
             $line = $read[$i];
+            $i++;
             while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
             while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
                str_replace("\n", "", $line);
                str_replace("\n", "", $line);
                str_replace("\n", "", $read[$i]);
                str_replace("\n", "", $read[$i]);
                $line = "$line $read[$i]";
                $line = "$line $read[$i]";
                $i++;
                $i++;
+               $read[$i] = trim($read[$i]);
             }
             }
 
 
             /** Detect the boundary of a multipart message **/
             /** Detect the boundary of a multipart message **/
@@ -473,6 +488,19 @@
                }
                }
                $charset = str_replace("\"", "", $charset);
                $charset = str_replace("\"", "", $charset);
             }
             }
+
+            /** Detects filename if any **/
+            if (strpos(strtolower(trim($line)), "name=")) {
+               $pos = strpos($line, "name=") + 5;
+               $name = trim($line);
+               if (strpos($line, " ", $pos) > 0) {
+                  $name = substr($name, $pos, strpos($line, " ", $pos));
+               } else {
+                  $name = substr($name, $pos);
+               }
+               $name = str_replace("\"", "", $name);
+               $filename = $name;
+            }
          }
          }
          $i++;
          $i++;
       }
       }

+ 17 - 21
functions/mime.php

@@ -6,7 +6,6 @@
 
 
 
 
    function decodeMime($body, $bound, $type0, $type1) {
    function decodeMime($body, $bound, $type0, $type1) {
-//      echo "<TT>decodeMime: $type0/$type1</TT><BR>";
       if ($type0 == "multipart") {
       if ($type0 == "multipart") {
          if ($body[0] == "")
          if ($body[0] == "")
             $i = 1;
             $i = 1;
@@ -26,7 +25,7 @@
                   $p++;
                   $p++;
                }
                }
 
 
-               fetchEntityHeader($imapConnection, $entity_header, $ent_type0, $ent_type1, $ent_bound, $encoding, $charset);
+               fetchEntityHeader($imapConnection, $entity_header, $ent_type0, $ent_type1, $ent_bound, $encoding, $charset, $filename);
 
 
                if ($ent_type0 == "text") {
                if ($ent_type0 == "text") {
                   while (substr(trim($body[$j]), 0, strlen($bound)) != $bound) {
                   while (substr(trim($body[$j]), 0, strlen($bound)) != $bound) {
@@ -35,14 +34,14 @@
                      $p++;
                      $p++;
                   }
                   }
                } else {
                } else {
-                  if (trim($body[$j]) == "")
-                     $j++;
+                  $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];
                      $entity_body .= $body[$j];
                      $j++;
                      $j++;
                   }
                   }
                }
                }
-               $entity = getEntity($entity_body, $ent_bound, $ent_type0, $ent_type1, $encoding, $charset);
+               $entity = getEntity($entity_body, $ent_bound, $ent_type0, $ent_type1, $encoding, $charset, $filename);
 
 
                $q = count($full_message);
                $q = count($full_message);
                $full_message[$q] = $entity[0];
                $full_message[$q] = $entity[0];
@@ -57,13 +56,14 @@
    }
    }
 
 
    /** This gets one entity's properties **/
    /** This gets one entity's properties **/
-   function getEntity($body, $bound, $type0, $type1, $encoding, $charset) {
-//      echo "<TT>getEntity: $type0/$type1</TT><BR>";
+   function getEntity($body, $bound, $type0, $type1, $encoding, $charset, $filename) {
       $msg[0]["TYPE0"] = $type0;
       $msg[0]["TYPE0"] = $type0;
       $msg[0]["TYPE1"] = $type1;
       $msg[0]["TYPE1"] = $type1;
       $msg[0]["ENCODING"] = $encoding;
       $msg[0]["ENCODING"] = $encoding;
       $msg[0]["CHARSET"] = $charset;
       $msg[0]["CHARSET"] = $charset;
+      $msg[0]["FILENAME"] = $filename;
 
 
+      echo "$type0 / $type1<BR>";
       if ($type0 == "text") {
       if ($type0 == "text") {
          // error correcting if they didn't follow RFC standards
          // error correcting if they didn't follow RFC standards
          if (trim($type1) == "")
          if (trim($type1) == "")
@@ -85,11 +85,9 @@
                $msg[0]["BODY"][$p] = $body[$q];
                $msg[0]["BODY"][$p] = $body[$q];
             }
             }
          }
          }
-      } else if ($type0 == "image") {
+      } else {
          $msg[0]["PRIORITY"] == 5;
          $msg[0]["PRIORITY"] == 5;
          $msg[0]["BODY"][0] = $body;
          $msg[0]["BODY"][0] = $body;
-      } else {
-         $msg[0]["BODY"][0] = "<B><FONT COLOR=DD0000>This attachment is of an unknown format:  $type0/$type1</FONT></B>";
       }
       }
 
 
       return $msg;
       return $msg;
@@ -137,22 +135,20 @@
          $pos = count($body);
          $pos = count($body);
          if ($message["ENTITIES"][$i]["TYPE0"] != "text") {
          if ($message["ENTITIES"][$i]["TYPE0"] != "text") {
             $body[$pos] = "<BR><TT><U><B>ATTACHMENTS:</B></U></TT><BR>";
             $body[$pos] = "<BR><TT><U><B>ATTACHMENTS:</B></U></TT><BR>";
+            $i = count($message["ENTITIES"]);
          }
          }
       }
       }
 
 
       for ($i = 0; $i < count($message["ENTITIES"]); $i++) {
       for ($i = 0; $i < count($message["ENTITIES"]); $i++) {
          $pos = count($body);
          $pos = count($body);
-         if ($message["ENTITIES"][$i]["TYPE0"] != "text") {
-            if ($message["ENTITIES"][$i]["TYPE0"] == "image") {
-               $body[$pos] = "<TT>&nbsp;&nbsp;&nbsp;Image: " . $message["ENTITIES"][$i]["TYPE0"] . "/" . $message["ENTITIES"][$i]["TYPE1"] . "</TT><BR>";
-
-/*               $file = fopen("../data/tmp.png", "w");
-               fwrite($file, base64_decode($message["ENTITIES"][$i]["BODY"][0]));
-               fclose($file);
-*/
-            } else {
-               $body[$pos] = "<TT>&nbsp;&nbsp;&nbsp;Unknown Type: " . $message["ENTITIES"][$i]["TYPE0"] . "/" . $message["ENTITIES"][$i]["TYPE1"] . "</TT><BR>";
-            }
+         if (($message["ENTITIES"][$i]["TYPE0"] == "image") || ($message["ENTITIES"][$i]["TYPE0"] == "application")){
+            $filename = $message["ENTITIES"][$i]["FILENAME"];
+            $body[$pos] = "<TT>&nbsp;&nbsp;&nbsp;<A HREF=\"../data/$filename\">" . $filename . "</A></TT><BR>";
+
+            $file = fopen("../data/$filename", "w");
+            $image = base64_decode($message["ENTITIES"][$i]["BODY"][0]);
+            fwrite($file, $image);
+            fclose($file);
          }
          }
       }
       }
 
 

+ 1 - 1
src/read_body.php

@@ -79,7 +79,7 @@
    $subject = htmlspecialchars(stripslashes($message["HEADER"]["SUBJECT"]));
    $subject = htmlspecialchars(stripslashes($message["HEADER"]["SUBJECT"]));
 
 
    echo "<BR>";
    echo "<BR>";
-   echo "<TABLE COLS=1 WIDTH=95% BORDER=0 ALIGN=CENTER CELLPADDING=2>\n";
+   echo "<TABLE COLS=1 WIDTH=98% BORDER=0 ALIGN=CENTER CELLPADDING=2>\n";
    echo "   <TR><TD BGCOLOR=\"$color[0]\" WIDTH=100%>";
    echo "   <TR><TD BGCOLOR=\"$color[0]\" WIDTH=100%>";
    echo "      <TABLE WIDTH=100% BORDER=0 COLS=2>";
    echo "      <TABLE WIDTH=100% BORDER=0 COLS=2>";
    echo "         <TR>";
    echo "         <TR>";