Added BASIC mime support. Not fully working yet.
authorlkehresman <lkehresman@7612ce4b-ef26-0410-bec9-ea0150e637f0>
Mon, 13 Dec 1999 13:53:22 +0000 (13:53 +0000)
committerlkehresman <lkehresman@7612ce4b-ef26-0410-bec9-ea0150e637f0>
Mon, 13 Dec 1999 13:53:22 +0000 (13:53 +0000)
git-svn-id: https://svn.code.sf.net/p/squirrelmail/code/trunk/squirrelmail@74 7612ce4b-ef26-0410-bec9-ea0150e637f0

functions/date.php
functions/imap.php
functions/mailbox.php
functions/mime.php [new file with mode: 0644]

index c884c7180dcfee8cb90fee50bc1ff2b86cbbed86..b46802ae69d44dbb55b3917e3aa971db3d4724dd 100644 (file)
@@ -9,6 +9,33 @@
     **
     **/
 
+   function getMinutes($hour) {
+      $date = $hour;
+
+      if (($hour == 0) || ($hour == "00"))
+         $date = "00";
+      else if (($hour == 1) || ($hour == "01"))
+         $date = "01";
+      else if (($hour == 2) || ($hour == "02"))
+         $date = "02";
+      else if (($hour == 3) || ($hour == "03"))
+         $date = "03";
+      else if (($hour == 4) || ($hour == "04"))
+         $date = "04";
+      else if (($hour == 5) || ($hour == "05"))
+         $date = "05";
+      else if (($hour == 6) || ($hour == "06"))
+         $date = "06";
+      else if (($hour == 7) || ($hour == "07"))
+         $date = "07";
+      else if (($hour == 8) || ($hour == "08"))
+         $date = "08";
+      else if (($hour == 9) || ($hour == "09"))
+         $date = "09";
+
+      return $date;
+   }
+
    function getHour($hour) {
       $time = explode(":", $hour);
       return $time[0];
       return mktime($d[0], $d[1], $d[2], $d[3], $d[4], $d[5]);
    }
 
-   function getLongDateString($dateParts) {
-      if (eregi("mon|tue|wed|thu|fri|sat|sun", trim($dateParts[0]), $tmp)) {
-         $date[0] = getDayOfMonth(trim($dateParts[1]));
-         $date[1] = getMonth(trim($dateParts[2]));
-         $date[2] = getYear(trim($dateParts[3]));
-         $date[3] = getHour(trim($dateParts[4]));
-         $date[4] = getMinute(trim($dateParts[4]));
+   function getLongDateString($stamp) {
+      $dateArray = getDate($stamp);
+
+      if ($dateArray["hours"] >= 12) {
+         $am_pm = "p.m.";
       } else {
-         $date[0] = getDayOfMonth(trim($dateParts[0]));
-         $date[1] = getMonth(trim($dateParts[1]));
-         $date[2] = getYear(trim($dateParts[2]));
-         $date[3] = getHour(trim($dateParts[3]));
-         $date[4] = getMinute(trim($dateParts[4]));
+         $am_pm = "a.m.";
       }
 
-      if ($date[3] >= 12) {
-         $date[5] = "p.m.";
-         if ($date[3] >= 13)
-            $date[3] = $date[3] - 12;
-      } else {
-         $date[5] = "a.m.";
-         if ($date[3] < 1)
-            $date[3] = 12;
+      if ($dateArray["hours"] >= 13) {
+         $dateArray["hours"] = $dateArray["hours"] - 12;
       }
 
-      return "$date[1] $date[0], $date[2]&nbsp;&nbsp;&nbsp;$date[3]:$date[4] $date[5]";
+      $dateParts[0] = getDayOfMonth($dateArray["mday"]);
+      $dateParts[1] = getMonth($dateArray["month"]);
+      $dateParts[2] = getYear($dateArray["year"]);
+      $dateParts[3] = $dateArray["hours"]; // hour
+      $dateParts[4] = getMinutes($dateArray["minutes"]); // minute
+
+      return "$dateParts[1] $dateParts[0], $dateParts[2] at $dateParts[3]:$dateParts[4] $am_pm";
    }
 ?>
index 49e348d515b0733c94b5f10ce30374b5f129fe4b..4f11ed9072d7f43691a5f7232e6204fa280d4137 100644 (file)
          setMessageFlag($imapConnection, $a, $b, "Deleted");
       }
    }
+   function stripComments($line) {
+      if (strpos($line, ";")) {
+         $line = substr($line, 0, strpos($line, ";"));
+      }
+
+      if (strpos($line, "(") && strpos($line, ")")) {
+         $full_line = $full_line . substr($line, 0, strpos($line, "("));
+         $full_line = $full_line . substr($line, strpos($line, ")")+1, strlen($line) - strpos($line, ")"));
+      } else {
+         $full_line = $line;
+      }
+      return $full_line;
+   }
 ?>
index f8a71f873c8eeed96578f265adad417a89678e69..ea674fd4fc534f4a2a8848f7f46653686d65dfba 100644 (file)
@@ -62,7 +62,7 @@
          } else {
             $rel_end = $end;
          }
-         fputs($imapConnection, "messageFetch FETCH $rel_start:$rel_end RFC822.HEADER.LINES (From Subject Date)\n");
+         fputs($imapConnection, "messageFetch FETCH $rel_start:$rel_end RFC822.HEADER.LINES (From Subject Date To Cc)\n");
          $read = fgets($imapConnection, 1024);
 
          while ((substr($read, 0, 15) != "messageFetch OK") && (substr($read, 0, 16) != "messageFetch BAD")) {
       return $box;
    }
 
-   /** This function will fetch the body of a given message and format
-       it into our standard format. **/
-   function fetchBody($imapConnection, $id) {
+   /** This function gets all the information about a message.  Including Header and body **/
+   function fetchMessage($imapConnection, $id) {
+      $message["HEADER"] = fetchHeader($imapConnection, $id);
+      $message["ENTITIES"] = fetchBody($imapConnection, $message["HEADER"]["BOUNDARY"], $id, $message["HEADER"]["TYPE"][0], $message["HEADER"]["TYPE"][1]);
+
+      return $message;
+   }
+
+   function fetchHeader($imapConnection, $id) {
+      fputs($imapConnection, "messageFetch FETCH $id:$id RFC822.HEADER.LINES (From Subject Date To Cc Content-Type MIME-Version)\n");
+      $read = fgets($imapConnection, 1024);
+
+      /** defaults... if the don't get overwritten, it will display text **/
+      $header["TYPE"][0] = "text";
+      $header["TYPE"][1] = "plain";
+      while ((substr($read, 0, 15) != "messageFetch OK") && (substr($read, 0, 16) != "messageFetch BAD")) {
+         /** MIME-VERSION **/
+         if (substr($read, 0, 17) == "MIME-Version: 1.0") {
+            $header["MIME"] = true;
+            $read = fgets($imapConnection, 1024);
+         }
+         /** CONTENT-TYPE **/
+         else if (substr($read, 0, 13) == "Content-Type:") {
+            $cont = trim(substr($read, 13));
+            $cont = substr($cont, 0, strpos($cont, ";"));
+            $header["TYPE"][0] = substr($cont, 0, strpos($cont, "/"));
+            $header["TYPE"][1] = substr($cont, strpos($cont, "/")+1);
+
+            $read = fgets($imapConnection, 1024);
+            if (substr(strtolower(trim($read)), 0, 9) == "boundary=") {
+               $bound = trim($read);
+               $bound = substr($bound, 9);
+               $bound = str_replace("\"", "", $bound);
+               $header["BOUNDARY"] = $bound;
+               $read = fgets($imapConnection, 1024);
+            }
+         }
+         /** FROM **/
+         else if (substr($read, 0, 5) == "From:") {
+            $header["FROM"] = trim(substr($read, 5, strlen($read) - 6));
+            $read = fgets($imapConnection, 1024);
+         }
+         /** DATE **/
+         else if (substr($read, 0, 5) == "Date:") {
+            $d = substr($read, 5, strlen($read) - 6);
+            $d = trim($d);
+            $d = ereg_replace("  ", " ", $d);
+            $d = explode(" ", $d);
+            $header["DATE"] = getTimeStamp($d);
+            $read = fgets($imapConnection, 1024);
+         }
+         /** SUBJECT **/
+         else if (substr($read, 0, 8) == "Subject:") {
+            $header["SUBJECT"] = trim(substr($read, 8, strlen($read) - 9));
+            if (strlen(Chop($header["SUBJECT"])) == 0)
+               $header["SUBJECT"] = "(no subject)";
+            $read = fgets($imapConnection, 1024);
+         }
+         /** CC **/
+         else if (substr($read, 0, 3) == "CC:") {
+            $pos = 0;
+            $header["CC"][$pos] = trim(substr($read, 4));
+            $read = fgets($imapConnection, 1024);
+            while ((substr($read, 0, 1) == " ") && (trim($read) != "")) {
+               $pos++;
+               $header["CC"][$pos] = trim($read);
+               $read = fgets($imapConnection, 1024);
+            }
+         }
+         /** TO **/
+         else if (substr($read, 0, 3) == "To:") {
+            $pos = 0;
+            $header["TO"][$pos] = trim(substr($read, 4));
+            $read = fgets($imapConnection, 1024);
+            while ((substr($read, 0, 1) == " ")  && (trim($read) != "")){
+               $pos++;
+               $header["TO"][$pos] = trim($read);
+               $read = fgets($imapConnection, 1024);
+            }
+         }
+
+         /** ERROR CORRECTION **/
+         else if (substr($read, 0, 1) == ")") {
+            if ($header["SUBJECT"] == "")
+                $header["SUBJECT"] = "(no subject)";
+
+            if ($header["FROM"] == "")
+                $header["FROM"] = "(unknown sender)";
+
+            if ($header["DATE"] == "")
+                $header["DATE"] = time();
+            $read = fgets($imapConnection, 1024);
+         }
+         else {
+            $read = fgets($imapConnection, 1024);
+         }
+      }
+      return $header;
+   }
+
+   function fetchBody($imapConnection, $bound, $id, $type0, $type1) {
+      /** This first part reads in the full body of the message **/
       fputs($imapConnection, "messageFetch FETCH $id:$id BODY[TEXT]\n");
+      $read = fgets($imapConnection, 1024);
+
       $count = 0;
-      $read[$count] = fgets($imapConnection, 1024);
-      while ((substr($read[$count], 0, 15) != "messageFetch OK") && (substr($read[$count], 0, 16) != "messageFetch BAD")) {
+      while ((substr($read, 0, 15) != "messageFetch OK") && (substr($read, 0, 16) != "messageFetch BAD")) {
+         $body[$count] = $read;
          $count++;
-         $read[$count] = fgets($imapConnection, 1024);
+
+         $read = fgets($imapConnection, 1024);
       }
 
-      /** this loop removes the first line, and the last two which
-          are IMAP information that we don't need. **/
+      /** this deletes the first line, and the last two (imap stuff we ignore) **/
       $i = 0;
       $j = 0;
-      while ($i < count($read)) {
-         if (($i != 0) && ($i != count($read) - 1) && ($i != count($read) - 2)){
-            $readtmp[$j] = $read[$i];
+      while ($i < count($body)) {
+         if ( ($i != 0) && ($i != count($body) - 1) && ($i != count($body)) ) {
+            $bodytmp[$j] = $body[$i];
             $j++;
          }
          $i++;
       }
-      $read = $readtmp;
-
-      /** This loop formats the text, creating links out of linkable stuff too **/
-      $count = 0;
-      $useHTML= false;
-      while ($count < count($read)) {
-         $read[$count] = "^^$read[$count]";
-
-         if (strpos(strtolower($read[$count]), "<html") == true) {
-            $useHTML = true;
-         } else if (strpos(strtolower($read[$count]), "</html>") == true) {
-            $useHTML = false;
-         }
+      $body = $bodytmp;
 
-         $read[$count] = substr($read[$count], 2, strlen($read[$count]));
+      /** Now, lets work out the MIME stuff **/
+      /** (needs mime.php included)         **/
+      return decodeMime($body, $bound, $type0, $type1);
+   }
 
-         if ($useHTML == false) {
-            $read[$count] = parsePlainBodyText($read[$count]);
-         } else {
-            $read[$count] = parseHTMLBodyText($read[$count]);
+   function fetchEntityHeader($imapConnection, &$read, &$type0, &$type1, &$bound) {
+      /** defaults... if the don't get overwritten, it will display text **/
+      $type0 = "text";
+      $type1 = "plain";
+      $i = 0;
+      while (trim($read[$i]) != "") {
+         if (substr($read[$i], 0, 13) == "Content-Type:") {
+            $cont = trim(substr($read[$i], 13));
+            $cont = substr($cont, 0, strpos($cont, ";"));
+            $type0 = substr($cont, 0, strpos($cont, "/"));
+            $type1 = substr($cont, strpos($cont, "/")+1);
+
+            if (substr(strtolower(trim($read[$i])), 0, 9) == "boundary=") {
+               $bound = trim($read[$i]);
+               $bound = substr($bound, 9);
+               $bound = str_replace("\"", "", $bound);
+            }
          }
+         $i++;
+      }
 
-         $count++;
+      /** remove the header from the entity **/
+      $i = 0;
+      while (trim($read[$i]) != "") {
+         $i++;
       }
-      return $read;
-   }
+      $i++;
 
-   function parseHTMLBodyText($line) {
-      return $line;
+      for ($p = 0; $i < count($read); $p++) {
+         $entity[$p] = $read[$i];
+         $i++;
+      }
+
+      $read = $entity;
    }
 
-   function parsePlainBodyText($line) {
+   function parsePlainTextMessage($line) {
       $line = "^^$line";
 
       if ((strpos(strtolower($line), "<!") == false) &&
 
       return $line;
    }
-
-   function getMessageHeadersTo($imapConnection, $i, &$to) {
-      $pos = 0;
-      fputs($imapConnection, "messageFetch FETCH $i:$i RFC822.HEADER.LINES (To)\n");
-      $read = fgets($imapConnection, 1024);
-
-      $firstline = true;
-      while ((substr($read, 0, 15) != "messageFetch OK") && (substr($read, 0, 16) != "messageFetch BAD")) {
-         if ($firstline == true) {
-            $firstline = false;
-            $read = fgets($imapConnection, 1024);
-         } else if (strlen(trim($read)) <= 1) {
-            $firstline = false;
-            $read = fgets($imapConnection, 1024);
-         } else if ($read == ")") {
-            $firstline = false;
-            $read = fgets($imapConnection, 1024);
-         } else {
-            $firstline = false;
-            $read = ereg_replace("<", "&lt;", $read);
-            $read = ereg_replace(">", "&gt;", $read);
-            if (strlen(trim($read)) != 0)
-               $to[$pos] = substr($read, 3, strlen($read));
-
-            $pos++;
-            $read = fgets($imapConnection, 1024);
-         }
-      }
-   }
-
-   function getMessageHeadersCc($imapConnection, $i, &$cc) {
-      $pos = 0;
-      fputs($imapConnection, "messageFetch FETCH $i:$i RFC822.HEADER.LINES (Cc)\n");
-      $read = fgets($imapConnection, 1024);
-
-      $firstline = true;
-      while ((strlen(trim($read)) > 0) && (substr($read, 0, 15) != "messageFetch OK") && (substr($read, 0, 16) != "messageFetch BAD")) {
-         if ($firstline == true) {
-            $firstline = false;
-            $read = fgets($imapConnection, 1024);
-         } else if (strlen(trim($read)) <= 1) {
-            $firstline = false;
-            $read = fgets($imapConnection, 1024);
-         } else if ($read == ")") {
-            $firstline = false;
-            $read = fgets($imapConnection, 1024);
-         } else {
-            $read = ereg_replace("<", "&lt;", $read);
-            $read = ereg_replace(">", "&gt;", $read);
-            if (strlen(trim($read)) != 0)
-               $cc[$pos] = substr($read, 3, strlen($read));
-
-            $pos++;
-            $read = fgets($imapConnection, 1024);
-         }
-      }
-      $read = fgets($imapConnection, 1024); // get rid of the last line
-   }
-
-
 ?>
diff --git a/functions/mime.php b/functions/mime.php
new file mode 100644 (file)
index 0000000..f89cdfc
--- /dev/null
@@ -0,0 +1,52 @@
+<?
+   /** mime.php
+    **
+    ** This contains the functions necessary to detect and decode MIME messages.
+    **/
+
+
+   function decodeMime($body, $bound, $type0, $type1) {
+      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;
+               $p = 0;
+
+               while (substr(trim($body[$j]), 0, strlen($bound)) != $bound) {
+                  $entity[$p] = $body[$j];
+                  $j++;
+                  $p++;
+               }
+               fetchEntityHeader($imapConnection, $entity, $ent_type0, $ent_type1, $ent_bound);
+               $entity = decodeMime($entity, $ent_bound, $ent_type0, $ent_type1);
+
+               $q = count($full_message);
+               $full_message[$q] = $entity;
+            }
+            $i++;
+         }
+      } else if ($type0 == "text") {
+         $entity_msg["TYPE0"] = "text";
+         if ($type1 == "plain") {
+            $entity_msg["TYPE1"] = "plain";
+            for ($p = 0;$p < count($body);$p++) {
+               $entity_msg["BODY"][$p] = parsePlainTextMessage($body[$p]);
+            }
+
+         } else if ($type1 == "html") {
+            $entity_msg["TYPE1"] = "html";
+            $entity_msg["BODY"] = $body;
+         }
+         $full_message[0] = $entity_msg;
+      }
+
+      return $full_message;
+   }
+?>
\ No newline at end of file