Moving sm_print_r back to globals.php; tired of it not being available when developin...
[squirrelmail.git] / functions / imap_messages.php
index c6eb5846d96d9da9dc473a6cd4226d1e7b60b888..7f9f26309e1ffe28515337d53a20bb8380e0143a 100755 (executable)
@@ -119,29 +119,34 @@ function sqimap_toggle_flag($imap_stream, $id, $flag, $set, $handle_errors) {
 /**
  * Sort the message list and crunch to be as small as possible
  * (overflow could happen, so make it small if possible)
+ * @param array $aUid array with uid's
+ * @return string $s message set string
  */
-function sqimap_message_list_squisher($messages_array) {
-    if( !is_array( $messages_array ) ) {
-        return $messages_array;
-    }
-
-    sort($messages_array, SORT_NUMERIC);
-    $msgs_str = '';
-    while ($messages_array) {
-        $start = array_shift($messages_array);
-        $end = $start;
-        while (isset($messages_array[0]) && $messages_array[0] == $end + 1) {
-            $end = array_shift($messages_array);
-        }
-        if ($msgs_str != '') {
-            $msgs_str .= ',';
-        }
-        $msgs_str .= $start;
-        if ($start != $end) {
-            $msgs_str .= ':' . $end;
+function sqimap_message_list_squisher($aUid) {
+    if( !is_array( $aUid ) ) {
+        return $aUid;
+    }
+    sort($aUid, SORT_NUMERIC);
+
+    if (count($aUid)) {
+        $s = '';
+        for ($i=0,$iCnt=count($aUid);$i<$iCnt;++$i) {
+            $iStart = $aUid[$i];
+            $iEnd = $iStart;
+            while ($i<($iCnt-1) && $aUid[$i+1] == $iEnd +1) {
+                $iEnd = $aUid[$i+1];
+                ++$i;
+            }
+            if ($s) {
+                $s .= ',';
+            }
+            $s .= $iStart;
+            if ($iStart != $iEnd) {
+                $s .= ':' . $iEnd;
+            }
         }
     }
-    return $msgs_str;
+    return $s;
 }
 
 
@@ -395,6 +400,10 @@ function get_thread_sort($imap_stream, $search='ALL') {
             $cChar = $sThreadResponse{$i};
             switch ($cChar) {
                 case '(': // new sub thread
+                    // correction for a subthread of a thread with no parents in thread
+                    if (!count($aUidSubThread) && $j > 0) {
+                       --$l;
+                    }
                     $aDepthStack[$j] = $l;
                     ++$j;
                     break;
@@ -620,7 +629,7 @@ function sqimap_get_small_header_list($imap_stream, $msg_list,
  * @return array   $aMessageList associative array with messages. Key is the UID, value is an associative array
  * @author Marc Groot Koerkamp
  */
-function parseFetch($aResponse,$aMessageList = array()) {
+function parseFetch(&$aResponse,$aMessageList = array()) {
     for ($j=0,$iCnt=count($aResponse);$j<$iCnt;++$j) {
         $aMsg = array();
 
@@ -773,6 +782,7 @@ function parseFetch($aResponse,$aMessageList = array()) {
             $msgi = '';
        }
        $aMessageList[$msgi] = $aMsg;
+       $aResponse[$j] = NULL;
     }
     return $aMessageList;
 }
@@ -913,7 +923,7 @@ function sqimap_get_message($imap_stream, $id, $mailbox) {
     if ($read) {
         if (preg_match('/.+FLAGS\s\((.*)\)\s/AUi',$read[0],$regs)) {
             if (trim($regs[1])) {
-                $flags = preg_split('/ /', $regs[1],-1,'PREG_SPLIT_NI_EMPTY');
+                $flags = preg_split('/ /', $regs[1],-1,PREG_SPLIT_NO_EMPTY);
             }
         }
     } else {
@@ -934,5 +944,3 @@ function sqimap_get_message($imap_stream, $id, $mailbox) {
     $msg->rfc822_header = $rfc822_header;
     return $msg;
 }
-
-?>