Updated changelog and todo, working on saving sent
[squirrelmail.git] / functions / imap_messages.php
index 71492c53c46515b704212f6459dc70ee179a6a1e..fb996501d9158669df0eada5bcdd4e093294872f 100755 (executable)
@@ -9,7 +9,7 @@
     **  Copies specified messages to specified folder
     ******************************************************************************/
    function sqimap_messages_copy ($imap_stream, $start, $end, $mailbox) {
-      fputs ($imap_stream, "a001 COPY $start:$end \"$mailbox\"\n");
+      fputs ($imap_stream, "a001 COPY $start:$end \"$mailbox\"\r\n");
       $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
    }
 
@@ -31,7 +31,7 @@
     **  Sets the specified messages with specified flag
     ******************************************************************************/
    function sqimap_messages_flag ($imap_stream, $start, $end, $flag) {
-      fputs ($imap_stream, "a001 STORE $start:$end +FLAGS (\\$flag)\n");
+      fputs ($imap_stream, "a001 STORE $start:$end +FLAGS (\\$flag)\r\n");
       $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
    }
 
@@ -39,7 +39,7 @@
     **  Returns some general header information -- FROM, DATE, and SUBJECT
     ******************************************************************************/
    function sqimap_get_small_header ($imap_stream, $id, &$from, &$subject, &$date) {
-      fputs ($imap_stream, "a001 FETCH $id:$id BODY[HEADER.FIELDS (From Subject Date)]\n");
+      fputs ($imap_stream, "a001 FETCH $id:$id BODY[HEADER.FIELDS (From Subject Date)]\r\n");
       $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
 
       for ($i = 0; $i < count($read); $i++) {
@@ -59,7 +59,7 @@
     **  Returns the flags for the specified messages 
     ******************************************************************************/
    function sqimap_get_flags ($imap_stream, $start, $end) {
-      fputs ($imap_stream, "a001 FETCH $start:$end FLAGS\n");
+      fputs ($imap_stream, "a001 FETCH $start:$end FLAGS\r\n");
       $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
       $i = 0;
       while ($i < count($read)) {
@@ -86,7 +86,7 @@
       $message["INFO"]["ID"] = $id;
       $message["INFO"]["MAILBOX"] = $mailbox;
       $message["HEADER"] = sqimap_get_message_header($imap_stream, $id);
-      $message["ENTITIES"] = sqimap_get_message_body($imap_stream, $message["HEADER"]["BOUNDARY"], $id, $message["HEADER"]["TYPE0"], $message["HEADER"]["TYPE1"], $message["HEADER"]["ENCODING"]);
+      $message["ENTITIES"] = sqimap_get_message_body($imap_stream, $message["HEADER"]["BOUNDARY"], $id, $message["HEADER"]["TYPE0"], $message["HEADER"]["TYPE1"], $message["HEADER"]["ENCODING"], $message["HEADER"]["CHARSET"]);
       return $message;
    }
 
@@ -94,7 +94,7 @@
     **  Wrapper function that reformats the header information.
     ******************************************************************************/
    function sqimap_get_message_header ($imap_stream, $id) {
-      fputs ($imap_stream, "a001 FETCH $id:$id BODY[HEADER]\n");
+      fputs ($imap_stream, "a001 FETCH $id:$id BODY[HEADER]\r\n");
       $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
      
       return sqimap_get_header($imap_stream, $read); 
     ******************************************************************************/
    function sqimap_get_header ($imap_stream, $read) {
       $i = 0;
+      // Set up some defaults
+      $header["TYPE0"] = "text";
+      $header["TYPE1"] = "plain";
+      $header["CHARSET"] = "us-ascii";
+
       while ($i < count($read)) {
          if (substr($read[$i], 0, 17) == "MIME-Version: 1.0") {
             $header["MIME"] = true;
          }
 
          /** CONTENT-TYPE **/
-         else if (substr($read[$i], 0, 13) == "Content-Type:") {
+         else if (strtolower(substr($read[$i], 0, 13)) == "content-type:") {
             $cont = strtolower(trim(substr($read[$i], 13)));
             if (strpos($cont, ";"))
                $cont = substr($cont, 0, strpos($cont, ";"));
 
+
             if (strpos($cont, "/")) {
                $header["TYPE0"] = substr($cont, 0, strpos($cont, "/"));
                $header["TYPE1"] = substr($cont, strpos($cont, "/")+1);
                $header["TYPE0"] = $cont;
             }
 
+
             $line = $read[$i];
             $i++;
             while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
 
             /** Detect the boundary of a multipart message **/
             if (strpos(strtolower(trim($line)), "boundary=")) {
-               $pos = strpos($line, "boundary=") + 9;
+               $pos = strpos(strtolower($line), "boundary=") + 9;
                $bound = trim($line);
                if (strpos($line, " ", $pos) > 0) {
                   $bound = substr($bound, $pos, strpos($line, " ", $pos));
                $i++;
             }
          }
+         /** MESSAGE ID **/
+         else if (strtolower(substr($read[$i], 0, 11)) == "message-id:") {
+            $header["MESSAGE-ID"] = trim(substr($read[$i], 11));
+            $i++;
+         }
+
 
          /** ERROR CORRECTION **/
          else if (substr($read[$i], 0, 1) == ")") {
    /******************************************************************************
     **  Returns the body of a message.
     ******************************************************************************/
-   function sqimap_get_message_body ($imap_stream, $bound, $id, $type0, $type1, $encoding) {
-      fputs ($imap_stream, "a001 FETCH $id:$id BODY[TEXT]\n");
+   function sqimap_get_message_body ($imap_stream, $bound, $id, $type0, $type1, $encoding, $charset) {
+      fputs ($imap_stream, "a001 FETCH $id:$id BODY[TEXT]\r\n");
       $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
        
       $i = 0;
       }
       $body = $bodytmp;
 
-      return decodeMime($body, $bound, $type0, $type1, $encoding);
+      return decodeMime($body, $bound, $type0, $type1, $encoding, $charset);
    }
 ?>