bug fix of the 1.2.0 bug, with thanks to Dave Huang. Also some style police...
[squirrelmail.git] / functions / imap_messages.php
index bcc94e38e899252a67548c64e7e4aacd674d9606..dbb59f4e31fab9f79970327175c8ae2a841bd715 100755 (executable)
@@ -1,20 +1,36 @@
 <?php
-   /**
-    **  imap_messages.php
-    **
-    **  This implements functions that manipulate messages
-    **
-    **  $Id$
-    **/
-
-   if (defined ('imap_messages_php'))
-       return;
-   define ('imap_messages_php', true);
 
-   /******************************************************************************
-    **  Copies specified messages to specified folder
-    ******************************************************************************/
-   function sqimap_messages_copy ($imap_stream, $start, $end, $mailbox) {
+/**
+ * imap_messages.php
+ *
+ * Copyright (c) 1999-2002 The SquirrelMail Project Team
+ * Licensed under the GNU GPL. For full terms see the file COPYING.
+ *
+ * This implements functions that manipulate messages
+ *
+ * $Id$
+ */
+
+/*****************************************************************/
+/*** THIS FILE NEEDS TO HAVE ITS FORMATTING FIXED!!!           ***/
+/*** PLEASE DO SO AND REMOVE THIS COMMENT SECTION.             ***/
+/***    + Base level indent should begin at left margin, as    ***/
+/***      the function definition starts below.                ***/
+/***    + All identation should consist of four space blocks   ***/
+/***    + Tab characters are evil.                             ***/
+/***    + all comments should use "slash-star ... star-slash"  ***/
+/***      style -- no pound characters, no slash-slash style   ***/
+/***    + FLOW CONTROL STATEMENTS (if, while, etc) SHOULD      ***/
+/***      ALWAYS USE { AND } CHARACTERS!!!                     ***/
+/***    + Please use ' instead of ", when possible. Note "     ***/
+/***      should always be used in _( ) function calls.        ***/
+/*** Thank you for your help making the SM code more readable. ***/
+/*****************************************************************/
+
+/****************************************************
+ **  Copies specified messages to specified folder **
+ ****************************************************/
+function sqimap_messages_copy ($imap_stream, $start, $end, $mailbox) {
       fputs ($imap_stream, sqimap_session_id() . " COPY $start:$end \"$mailbox\"\r\n");
       $read = sqimap_read_data ($imap_stream, sqimap_session_id(), true, $response, $message);
    }
       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 = sqimap_session_id() . " 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, sqimap_session_id(), 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 = sqimap_session_id() . " 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, sqimap_session_id(), 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, sqimap_session_id() . " FETCH $msg_list[$msgi] INTERNALDATE\r\n");
-            $readdate = sqimap_read_data($imap_stream, sqimap_session_id(), 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;
                $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;
                 $hdr->date = time();
             $i++;
          }
+        /** X-PRIORITY **/
+         else if (strtolower(substr($read[$i], 0, 11)) == "x-priority:") {
+            $hdr->priority = trim(substr($read[$i], 11));
+            $i++;
+         }
          else {
             $i++;
          }