Better error reporting
[squirrelmail.git] / functions / imap_messages.php
index 47bbff657e01396287d7c93c65cda3954e9fa5a6..05fc99d74f3e0278c1d24ba526569d6649cafd89 100755 (executable)
@@ -3,12 +3,11 @@
 /**
  * imap_messages.php
  *
- * Copyright (c) 1999-2005 The SquirrelMail Project Team
- * Licensed under the GNU GPL. For full terms see the file COPYING.
- *
  * This implements functions that manipulate messages
  * NOTE: Quite a few functions in this file are obsolete
  *
+ * @copyright © 1999-2006 The SquirrelMail Project Team
+ * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  * @version $Id$
  * @package squirrelmail
  * @subpackage imap
@@ -40,9 +39,14 @@ function sqimap_msgs_list_copy($imap_stream, $id, $mailbox, $handle_errors = tru
  * @param string $id The list of messages to move
  * @param string $mailbox The destination to move to
  * @param bool $handle_errors Show error messages in case of a NO, BAD or BYE response
+ * @param string $source_mailbox (since 1.5.1) name of source mailbox. It is used to
+ *  validate that target mailbox != source mailbox.
  * @return bool If the move completed without errors
  */
-function sqimap_msgs_list_move($imap_stream, $id, $mailbox, $handle_errors = true) {
+function sqimap_msgs_list_move($imap_stream, $id, $mailbox, $handle_errors = true, $source_mailbox = false) {
+    if ($source_mailbox!==false && $source_mailbox==$mailbox) {
+        return false;
+    }
     $msgs_id = sqimap_message_list_squisher($id);
     if (sqimap_msgs_list_copy ($imap_stream, $id, $mailbox, $handle_errors)) {
         return sqimap_toggle_flag($imap_stream, $id, '\\Deleted', true, true);
@@ -90,9 +94,25 @@ function sqimap_msgs_list_delete($imap_stream, $mailbox, $id, $bypass_trash=fals
 function sqimap_toggle_flag($imap_stream, $id, $flag, $set, $handle_errors) {
     $msgs_id = sqimap_message_list_squisher($id);
     $set_string = ($set ? '+' : '-');
+
+    for ($i=0; $i<sizeof($id); $i++) {
+        $aMessageList["$id[$i]"] = array();
+    }
+
     $aResponse = sqimap_run_command_list($imap_stream, "STORE $msgs_id ".$set_string."FLAGS ($flag)", $handle_errors, $response, $message, TRUE);
+
     // parse the fetch response
-    return parseFetch($aResponse);
+    $parseFetchResults=parseFetch($aResponse,$aMessageList);
+
+    // some broken IMAP servers do not return UID elements on UID STORE
+    // if this is the case, then we need to do a UID FETCH
+    $testkey=$id[0];
+    if (!isset($parseFetchResults[$testkey]['UID'])) {
+        $aResponse = sqimap_run_command_list($imap_stream, "FETCH $msgs_id (FLAGS)", $handle_errors, $response, $message, TRUE);
+        $parseFetchResults = parseFetch($aResponse,$aMessageList);
+    }
+
+    return ($parseFetchResults);
 }
 
 
@@ -142,11 +162,20 @@ function sqimap_get_sort_order($imap_stream, $sSortField, $reverse, $search='ALL
         }
         $query = "SORT ($sSortField) ".strtoupper($default_charset)." $search";
         // FIX ME sqimap_run_command should return the parsed data accessible by $aDATA['SORT']
-        $aData = sqimap_run_command ($imap_stream, $query, false, $response, $message, TRUE);
+        // use sqimap_run_command_list in case of unsollicited responses. If we don't we could loose the SORT response
+        $aData = sqimap_run_command_list ($imap_stream, $query, false, $response, $message, TRUE);
         /* fallback to default charset */
-        if ($response == 'NO' && strpos($message,'[BADCHARSET]') !== false) {
-            $query = "SORT ($sSortField) US-ASCII $search";
-            $aData = sqimap_run_command ($imap_stream, $query, true, $response, $message, TRUE);
+        if ($response == 'NO') {
+            if (strpos($message,'[BADCHARSET]') !== false ||
+                strpos($message,'Unrecognized character set') !== false) {
+                sqm_trigger_imap_error('SQM_IMAP_BADCHARSET',$query, $response, $message);
+                $query = "SORT ($sSortField) US-ASCII $search";
+                $aData = sqimap_run_command_list ($imap_stream, $query, true, $response, $message, TRUE);
+            } else {
+                sqm_trigger_imap_error('SQM_IMAP_ERROR',$query, $response, $message);
+            }
+        } else if ($response == 'BAD') {
+            sqm_trigger_imap_error('SQM_IMAP_NO_SORT',$query, $response, $message);
         }
     }
 
@@ -160,7 +189,7 @@ function sqimap_get_sort_order($imap_stream, $sSortField, $reverse, $search='ALL
 
 /**
  * Parses a UID list returned on a SORT or SEARCH request
- * @param array $aData imap response
+ * @param array $aData imap response (retrieved from sqimap_run_command_list)
  * @param string $sCommand issued imap command (SEARCH or SORT)
  * @return array $aUid uid list
  */
@@ -168,8 +197,10 @@ function parseUidList($aData,$sCommand) {
     $aUid = array();
     if (isset($aData) && count($aData)) {
         for ($i=0,$iCnt=count($aData);$i<$iCnt;++$i) {
-            if (preg_match("/^\* $sCommand (.+)$/", $aData[$i], $aMatch)) {
-                $aUid += preg_split("/ /", trim($aMatch[1]));
+            for ($j=0,$jCnt=count($aData[$i]);$j<$jCnt;++$j) {
+                if (preg_match("/^\* $sCommand (.+)$/", $aData[$i][$j], $aMatch)) {
+                    $aUid += preg_split("/ /", trim($aMatch[1]));
+                }
             }
         }
     }
@@ -404,11 +435,21 @@ function get_thread_sort($imap_stream, $search='ALL') {
     }
     $query = "THREAD $sort_type ".strtoupper($default_charset)." $search";
 
+    // TODO use sqimap_run_command_list as we do in get_server_sort()
     $thread_test = sqimap_run_command ($imap_stream, $query, false, $response, $message, TRUE);
     /* fallback to default charset */
-    if ($response == 'NO' && strpos($message,'[BADCHARSET]') !== false) {
-        $query = "THREAD $sort_type US-ASCII $search";
-        $thread_test = sqimap_run_command ($imap_stream, $query, true, $response, $message, TRUE);
+
+    if ($response == 'NO') {
+        if (strpos($message,'[BADCHARSET]') !== false ||
+            strpos($message,'Unrecognized character set') !== false) {
+            sqm_trigger_imap_error('SQM_IMAP_BADCHARSET',$query, $response, $message);
+            $query = "THREAD $sort_type US-ASCII $search";
+            $thread_test = sqimap_run_command ($imap_stream, $query, true, $response, $message, TRUE);
+        } else {
+            sqm_trigger_imap_error('SQM_IMAP_ERROR',$query, $response, $message);
+        }
+    } elseif ($response == 'BAD') {
+        sqm_trigger_imap_error('SQM_IMAP_NO_THREAD',$query, $response, $message);
     }
     if (isset($thread_test[0])) {
         for ($i=0,$iCnt=count($thread_test);$i<$iCnt;++$i) {
@@ -421,7 +462,7 @@ function get_thread_sort($imap_stream, $search='ALL') {
         $thread_list = "";
     }
     if (!preg_match("/OK/", $response)) {
-        $server_sort_array = 'no';
+        $server_sort_array = false;
         return $server_sort_array;
     }
     if (isset($thread_list)) {
@@ -800,9 +841,13 @@ function parseFetch($aResponse,$aMessageList = array()) {
                 break;
             }
         }
-        $msgi ="$unique_id";
-        $aMsg['UID'] = $unique_id;
-        $aMessageList[$msgi] = $aMsg;
+        if (!empty($unique_id)) {
+            $msgi = "$unique_id";
+            $aMsg['UID'] = $unique_id;
+       } else {
+            $msgi = '';
+       }
+       $aMessageList[$msgi] = $aMsg;
     }
     return $aMessageList;
 }