made more tolerant to malformed from field in message header
[squirrelmail.git] / functions / imap_messages.php
index 00ca7ef83582f04704bf87e34ea0394fce9b5578..a3b81d98c489279feb163a7f5b44c5170799829f 100755 (executable)
@@ -15,8 +15,8 @@
     **  Copies specified messages to specified folder
     ******************************************************************************/
    function sqimap_messages_copy ($imap_stream, $start, $end, $mailbox) {
-      fputs ($imap_stream, "a001 COPY $start:$end \"$mailbox\"\r\n");
-      $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
+      fputs ($imap_stream, sqimap_session_id() . " COPY $start:$end \"$mailbox\"\r\n");
+      $read = sqimap_read_data ($imap_stream, sqimap_session_id(), true, $response, $message);
    }
 
    /******************************************************************************
@@ -37,8 +37,8 @@
     **  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)\r\n");
-      $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
+      fputs ($imap_stream, sqimap_session_id() . " STORE $start:$end +FLAGS (\\$flag)\r\n");
+      $read = sqimap_read_data ($imap_stream, sqimap_session_id(), true, $response, $message);
    }
 
 
@@ -46,8 +46,8 @@
     **  Remove specified flag from specified messages
     ******************************************************************************/
    function sqimap_messages_remove_flag ($imap_stream, $start, $end, $flag) {
-      fputs ($imap_stream, "a001 STORE $start:$end -FLAGS (\\$flag)\r\n");
-      $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
+      fputs ($imap_stream, sqimap_session_id() . " STORE $start:$end -FLAGS (\\$flag)\r\n");
+      $read = sqimap_read_data ($imap_stream, sqimap_session_id(), true, $response, $message);
    }
 
 
@@ -67,6 +67,7 @@
    // Sort the message list and crunch to be as small as possible
    // (overflow could happen, so make it small if possible)
    function sqimap_message_list_squisher($messages_array) {
+      if( !is_array( $messages_array ) ) return;
       sort($messages_array, SORT_NUMERIC);
       $msgs_str = '';
       while ($messages_array) {
       global $squirrelmail_language, $color;
 
       /* Get the small headers for each message in $msg_list */
-
+      $sid = sqimap_session_id();
       $maxmsg = sizeof($msg_list);
       $msgs_str = sqimap_message_list_squisher($msg_list);
       $results = array();
       $read_list = array();
       $sizes_list = array();
 
-      /* We need to return the data in the same order as the caller supplied
-         in $msg_list, but IMAP servers are free to return responses in
-         whatever order they wish... So we need to re-sort manually */
-
+      /**
+       * We need to return the data in the same order as the caller supplied
+       * in $msg_list, but IMAP servers are free to return responses in
+       * whatever order they wish... So we need to re-sort manually
+       */
       for ($i = 0; $i < sizeof($msg_list); $i++) {
          $id2index[$msg_list[$i]] = $i;
       }
 
-
-      $query = "a001 FETCH $msgs_str BODY.PEEK[HEADER.FIELDS (Date To From Cc Subject Message-Id X-Priority Content-Type)]\r\n";
+      $query = "$sid FETCH $msgs_str BODY.PEEK[HEADER.FIELDS (Date To From Cc Subject Message-Id X-Priority Content-Type)]\r\n";
       fputs ($imap_stream, $query);
-      $readin_list = sqimap_read_data_list($imap_stream, "a001", true, $response, $message);
-
+      $readin_list = sqimap_read_data_list($imap_stream, $sid, true, $response, $message);
 
       foreach ($readin_list as $r) {
          if (!eregi("^\\* ([0-9]+) FETCH", $r[0], $regs)) {
             set_up_language($squirrelmail_language);
-            echo "<br><b><font color=$color[2]>\n";
-            echo _("ERROR : Could not complete request.");
-            echo "</b><br>\n";
-            echo _("Unknown response from IMAP server: ");
-            echo $r[0] . "</font><br>\n";
-            exit;
-         }
-         if (! isset($id2index[$regs[1]]) || !count($id2index[$regs[1]])) {
+            echo '<br><b><font color=$color[2]>' .
+                 _("ERROR : Could not complete request.") .
+                 '</b><br>' .
+                 _("Unknown response from IMAP server: ") . ' 1.' .
+                 $r[0] . "</font><br>\n";
+            // exit;
+         } else if (! isset($id2index[$regs[1]]) || !count($id2index[$regs[1]])) {
             set_up_language($squirrelmail_language);
-            echo "<br><b><font color=$color[2]>\n";
-            echo _("ERROR : Could not complete request.");
-            echo "</b><br>\n";
-            echo _("Unknown message number in reply from server: ");
-            echo $regs[1] . "</font><br>\n";
-            exit;
+            echo '<br><b><font color=$color[2]>' .
+                 _("ERROR : Could not complete request.") .
+                 '</b><br>' .
+                 _("Unknown message number in reply from server: ") .
+                 $regs[1] . "</font><br>\n";
+            // exit;
+         } else {
+            $read_list[$id2index[$regs[1]]] = $r;
          }
-         $read_list[$id2index[$regs[1]]] = $r;
       }
       arsort($read_list);
       
-      $query = "a002 FETCH $msgs_str RFC822.SIZE\r\n";
+      $query = "$sid FETCH $msgs_str RFC822.SIZE\r\n";
       fputs ($imap_stream, $query);
-      $sizesin_list = sqimap_read_data_list($imap_stream, "a002", true, $response, $message);
+      $sizesin_list = sqimap_read_data_list($imap_stream, $sid, true, $response, $message);
       
       foreach ($sizesin_list as $r) {
          if (!eregi("^\\* ([0-9]+) FETCH", $r[0], $regs)) {
             echo "<br><b><font color=$color[2]>\n";
             echo _("ERROR : Could not complete request.");
             echo "</b><br>\n";
-            echo _("Unknown response from IMAP server: ");
+            echo _("Unknown response from IMAP server: ") . ' 2.';
             echo $r[0] . "</font><br>\n";
             exit;
          }
             
          }
          if (trim($date) == "") {
-            fputs($imap_stream, "a002 FETCH $msg_list[$msgi] INTERNALDATE\r\n");
-            $readdate = sqimap_read_data($imap_stream, "a002", true, $response, $message);
+            fputs($imap_stream, "$sid FETCH $msg_list[$msgi] INTERNALDATE\r\n");
+            $readdate = sqimap_read_data($imap_stream, $sid, true, $response, $message);
             if (eregi(".*INTERNALDATE \"(.*)\".*", $readdate[0], $regs)) {
                $date_list = explode(" ", trim($regs[1]));
                $date_list[0] = str_replace("-", " ", $date_list[0]);
          $size = $regs[1];
          
          $header = new small_header;
-         if ($issent == true)
-            $header->from = (trim($to) != '')? $to : _("(only Cc/Bcc)");
-         else   
+         if ($issent == true) {
+            $header->from = (trim($to) != '' ? $to : '(' ._("No To Address") . ')');
+         } else {
             $header->from = $from;
+         }
 
          $header->date = $date;
          $header->subject = $subject;
     **  Returns the flags for the specified messages 
     ******************************************************************************/
    function sqimap_get_flags ($imap_stream, $i) {
-      fputs ($imap_stream, "a001 FETCH $i:$i FLAGS\r\n");
-      $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
+      fputs ($imap_stream, sqimap_session_id() . " FETCH $i:$i FLAGS\r\n");
+      $read = sqimap_read_data ($imap_stream, sqimap_session_id(), true, $response, $message);
       if (ereg("FLAGS(.*)", $read[0], $regs))
           return explode(" ", trim(ereg_replace('[\\(\\)\\\\]', '', $regs[1])));
       return Array('None');
       for ($i = 0; $i < sizeof($msg_list); $i++) {
          $id2index[$msg_list[$i]] = $i;
       }
-      fputs ($imap_stream, "a001 FETCH $msgs_str FLAGS\r\n");
-      $result_list = sqimap_read_data_list ($imap_stream, "a001", true, $response, $message);
+      fputs ($imap_stream, sqimap_session_id() . " FETCH $msgs_str FLAGS\r\n");
+      $result_list = sqimap_read_data_list ($imap_stream, sqimap_session_id(), true, $response, $message);
       $result_flags = array();
 
       for ($i = 0; $i < sizeof($result_list); $i++) {
     **  Wrapper function that reformats the header information.
     ******************************************************************************/
    function sqimap_get_message_header ($imap_stream, $id, $mailbox) {
-      fputs ($imap_stream, "a001 FETCH $id:$id BODY[HEADER]\r\n");
-      $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
+      fputs ($imap_stream, sqimap_session_id() . " FETCH $id:$id BODY[HEADER]\r\n");
+      $read = sqimap_read_data ($imap_stream, sqimap_session_id(), true, $response, $message);
      
       $header = sqimap_get_header($imap_stream, $read); 
       $header->id = $id;
                $i++;
             }
          }
+         /** BCC **/
+         else if (strtolower(substr($read[$i], 0, 4)) == "bcc:") {
+            $pos = 0;
+            $hdr->bcc[$pos] = trim(substr($read[$i], 5));
+            $i++;
+            while (((substr($read[$i], 0, 1) == " ") || (substr($read[$i], 0, 1) == "\t"))  && (trim($read[$i]) != "")){
+               $pos++;
+               $hdr->bcc[$pos] = trim($read[$i]);
+               $i++;
+            }
+         }
          /** TO **/
          else if (strtolower(substr($read[$i], 0, 3)) == "to:") {
             $pos = 0;