Browse Source

temp fixes, as long as we didn't adapt our mailbox tree code, cache the
list.

Make use of the sorted list with id's from the session if the mailbox state
didn't change.

All performance related imap fixes were found after playing with thetereal.
If you want to try itt yourself:

tethereal -t a -ni lo -f 'port 143' -R 'imap.request'
with lo the interface.

stekkel 21 years ago
parent
commit
b171edd0dc
3 changed files with 84 additions and 19 deletions
  1. 55 16
      functions/imap_mailbox.php
  2. 5 1
      functions/mailbox_display.php
  3. 24 2
      src/right_main.php

+ 55 - 16
functions/imap_mailbox.php

@@ -294,13 +294,22 @@ function sqimap_mailbox_expunge_dmn($message_id)
 
 
     // And after all that mucking around, update the sort list!
     // And after all that mucking around, update the sort list!
     // Remind me why the hell we need those two arrays again?!
     // Remind me why the hell we need those two arrays again?!
+
+    sqgetGlobalVar('server_sort_array',$server_sort_array,SQ_SESSION);
+
+
     if ( $allow_thread_sort && $thread_sort_messages ) {
     if ( $allow_thread_sort && $thread_sort_messages ) {
         $server_sort_array = get_thread_sort($imapConnection);
         $server_sort_array = get_thread_sort($imapConnection);
     } elseif ( $allow_server_sort ) {
     } elseif ( $allow_server_sort ) {
-        $server_sort_array = sqimap_get_sort_order($imapConnection, $sort, $mbx_response);
-    } else {
+        $key = array_search($message_id,$server_sort_array,true);
+        if ($key !== false) {
+           unset($server_sort_array[$key]);
+           $server_sort_array = array_values($server_sort_array);
+        }
+     } else {
         $server_sort_array = sqimap_get_php_sort_order($imapConnection, $mbx_response);
         $server_sort_array = sqimap_get_php_sort_order($imapConnection, $mbx_response);
     }
     }
+    sqsession_register('server_sort_array',$server_sort_array);
     return $cnt;
     return $cnt;
 }
 }
 
 
@@ -608,15 +617,16 @@ function sqimap_mailbox_option_list($imap_stream, $show_selected = 0, $folder_sk
  * Returns sorted mailbox lists in several different ways. 
  * Returns sorted mailbox lists in several different ways. 
  * See comment on sqimap_mailbox_parse() for info about the returned array.
  * See comment on sqimap_mailbox_parse() for info about the returned array.
  */
  */
-function sqimap_mailbox_list($imap_stream) {
+
+
+function sqimap_mailbox_list($imap_stream, $force=false) {
     global $default_folder_prefix;
     global $default_folder_prefix;
 
 
-    if (!isset($boxesnew)) {
+    if (!sqgetGlobalVar('boxesnew',$boxesnew,SQ_SESSION) || $force) {
         global $data_dir, $username, $list_special_folders_first,
         global $data_dir, $username, $list_special_folders_first,
                $folder_prefix, $trash_folder, $sent_folder, $draft_folder,
                $folder_prefix, $trash_folder, $sent_folder, $draft_folder,
                $move_to_trash, $move_to_sent, $save_as_draft,
                $move_to_trash, $move_to_sent, $save_as_draft,
                $delimiter, $noselect_fix_enable;
                $delimiter, $noselect_fix_enable;
-
         $inbox_in_list = false;
         $inbox_in_list = false;
         $inbox_subscribed = false;
         $inbox_subscribed = false;
 
 
@@ -630,20 +640,33 @@ function sqimap_mailbox_list($imap_stream) {
         /* LSUB array */
         /* LSUB array */
         $lsub_ary = sqimap_run_command ($imap_stream, $lsub_args,
         $lsub_ary = sqimap_run_command ($imap_stream, $lsub_args,
                                         true, $response, $message);
                                         true, $response, $message);
-        $lsub_ary = compact_mailboxes_response($lsub_ary);
 
 
         $sorted_lsub_ary = array();
         $sorted_lsub_ary = array();
         for ($i = 0, $cnt = count($lsub_ary);$i < $cnt; $i++) {
         for ($i = 0, $cnt = count($lsub_ary);$i < $cnt; $i++) {
+            /*
+             * Workaround for mailboxes returned as literal
+             * Doesn't work if the mailbox name is multiple lines
+             * (larger then fgets buffer)
+             */
+            if (isset($lsub_ary[$i + 1]) && substr($lsub_ary[$i],-3) == "}\r\n") {
+                if (ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
+                     $lsub_ary[$i], $regs)) {
+                    $i++;
+                    $lsub_ary[$i] = $regs[1] . '"' . addslashes(trim($lsub_ary[$i])) . '"' . $regs[2];
+                }
+            }
             $temp_mailbox_name = find_mailbox_name($lsub_ary[$i]);
             $temp_mailbox_name = find_mailbox_name($lsub_ary[$i]);
             $sorted_lsub_ary[] = $temp_mailbox_name;
             $sorted_lsub_ary[] = $temp_mailbox_name;
             if (!$inbox_subscribed && strtoupper($temp_mailbox_name) == 'INBOX') {
             if (!$inbox_subscribed && strtoupper($temp_mailbox_name) == 'INBOX') {
                 $inbox_subscribed = true;
                 $inbox_subscribed = true;
             }
             }
         }
         }
+        /* remove duplicates */
+        $sorted_lsub_ary = array_unique($sorted_lsub_ary);
 
 
         /* natural sort mailboxes */
         /* natural sort mailboxes */
         if (isset($sorted_lsub_ary)) {
         if (isset($sorted_lsub_ary)) {
-            usort($sorted_lsub_ary, 'strnatcasecmp');
+            usort($sorted_lsub_ary, 'user_strcasecmp');
         }
         }
         /*
         /*
          * The LSUB response doesn't provide us information about \Noselect
          * The LSUB response doesn't provide us information about \Noselect
@@ -651,7 +674,7 @@ function sqimap_mailbox_list($imap_stream) {
          * call to retrieve the flags for the mailbox
          * call to retrieve the flags for the mailbox
            * Note: according RFC2060 an imap server may provide \NoSelect flags in the LSUB response.
            * Note: according RFC2060 an imap server may provide \NoSelect flags in the LSUB response.
            * in other words, we cannot rely on it.
            * in other words, we cannot rely on it.
-        */
+         */
         $sorted_list_ary = array();
         $sorted_list_ary = array();
         for ($i=0; $i < count($sorted_lsub_ary); $i++) {
         for ($i=0; $i < count($sorted_lsub_ary); $i++) {
             if (substr($sorted_lsub_ary[$i], -1) == $delimiter) {
             if (substr($sorted_lsub_ary[$i], -1) == $delimiter) {
@@ -660,10 +683,19 @@ function sqimap_mailbox_list($imap_stream) {
             else {
             else {
                 $mbx = $sorted_lsub_ary[$i];
                 $mbx = $sorted_lsub_ary[$i];
             }
             }
-            $mbx = stripslashes($mbx);
-            $read = sqimap_run_command ($imap_stream, 'LIST "" ' . sqimap_encode_mailbox_name($mbx),
+
+            $read = sqimap_run_command ($imap_stream, "LIST \"\" \"$mbx\"",
                                         true, $response, $message);
                                         true, $response, $message);
-            $read = compact_mailboxes_response($read);
+
+            /* Another workaround for literals */
+
+            if (isset($read[1]) && substr($read[1],-3) == "}\r\n") {
+                if (ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
+                     $read[0], $regs)) {
+                    $read[0] = $regs[1] . '"' . addslashes(trim($read[1])) . '"' . $regs[2];
+                }
+            }
+
             if (isset($read[0])) {
             if (isset($read[0])) {
                 $sorted_list_ary[$i] = $read[0];
                 $sorted_list_ary[$i] = $read[0];
             } else {
             } else {
@@ -675,9 +707,17 @@ function sqimap_mailbox_list($imap_stream) {
          * we'll get it for them anyway
          * we'll get it for them anyway
          */
          */
         if (!$inbox_subscribed) {
         if (!$inbox_subscribed) {
-            $inbox_ary = sqimap_run_command ($imap_stream, 'LIST "" INBOX',
+            $inbox_ary = sqimap_run_command ($imap_stream, "LIST \"\" \"INBOX\"",
                                              true, $response, $message);
                                              true, $response, $message);
-            $sorted_list_ary[] = implode('', compact_mailboxes_response($inbox_ary));
+            /* Another workaround for literals */
+            if (isset($inbox_ary[1]) && substr($inbox_ary[0],-3) == "}\r\n") {
+                if (ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
+                     $inbox_ary[0], $regs)) {
+                    $inbox_ary[0] = $regs[1] . '"' . addslashes(trim($inbox_ary[1])) .
+                                '"' . $regs[2];
+                }
+            }
+            $sorted_list_ary[] = $inbox_ary[0];
             $sorted_lsub_ary[] = find_mailbox_name($inbox_ary[0]);
             $sorted_lsub_ary[] = find_mailbox_name($inbox_ary[0]);
         }
         }
 
 
@@ -705,15 +745,14 @@ function sqimap_mailbox_list($imap_stream) {
                 }
                 }
             }
             }
         }
         }
-
-        /* Rest of the folders */
+       /* Rest of the folders */
         for($k = 0; $k < $cnt; $k++) {
         for($k = 0; $k < $cnt; $k++) {
             if (!$used[$k]) {
             if (!$used[$k]) {
                 $boxesnew[] = $boxesall[$k];
                 $boxesnew[] = $boxesall[$k];
             }
             }
         }
         }
+        sqsession_register($boxesnew,'boxesnew');
     }
     }
-    
     return $boxesnew;
     return $boxesnew;
 }
 }
 
 

+ 5 - 1
functions/mailbox_display.php

@@ -460,7 +460,11 @@ function getThreadMessages($imapConnection, $start_msg, $show_num, $num_msgs) {
  */
  */
 function getServerSortMessages($imapConnection, $start_msg, $show_num,
 function getServerSortMessages($imapConnection, $start_msg, $show_num,
                                $num_msgs, $server_sort_order, $mbxresponse) {
                                $num_msgs, $server_sort_order, $mbxresponse) {
-    $id = sqimap_get_sort_order($imapConnection, $server_sort_order,$mbxresponse);
+    if (isset($mbxresponse['SORT_ARRAY']) && $mbxresponse['SORT_ARRAY']) {
+        $id = $mbxresponse['SORT_ARRAY'];
+    } else {
+        $id = sqimap_get_sort_order($imapConnection, $server_sort_order,$mbxresponse);
+    }
     return getServerMessages($imapConnection, $start_msg, $show_num, $num_msgs, $id);
     return getServerMessages($imapConnection, $start_msg, $show_num, $num_msgs, $id);
 }
 }
 
 

+ 24 - 2
src/right_main.php

@@ -152,6 +152,27 @@ else {
 do_hook ('generic_header');
 do_hook ('generic_header');
 
 
 $aMbxResponse = sqimap_mailbox_select($imapConnection, $mailbox);
 $aMbxResponse = sqimap_mailbox_select($imapConnection, $mailbox);
+$aMbxResponse['SORT_ARRAY'] = false;
+
+sqgetGlobalVar('aLastSelectedMailbox',$aLastSelectedMailbox,SQ_SESSION);
+
+if ($aLastSelectedMailbox && !isset($newsort)) {
+  // check if we deal with the same mailbox
+  if ($aLastSelectedMailbox['NAME'] == $mailbox) {
+     if ($aLastSelectedMailbox['EXISTS'] == $aMbxResponse['EXISTS'] &&
+         $aLastSelectedMailbox['UIDVALIDITY'] == $aMbxResponse['UIDVALIDITY'] &&
+         $aLastSelectedMailbox['UIDNEXT'] == $aMbxResponse['UIDNEXT']) {
+         // sort is still valid
+         sqgetGlobalVar('server_sort_array',$server_sort_array,SQ_SESSION);
+         $aMbxResponse['SORT_ARRAY'] = $server_sort_array;
+     }
+  } 
+}
+ 
+$aLastSelectedMailbox['NAME'] = $mailbox;
+$aLastSelectedMailbox['EXISTS'] = $aMbxResponse['EXISTS'];
+$aLastSelectedMailbox['UIDVALIDITY'] = $aMbxResponse['UIDVALIDITY'];
+$aLastSelectedMailbox['UIDNEXT'] = $aMbxResponse['UIDNEXT'];
 
 
 if ($composenew) {
 if ($composenew) {
     $comp_uri = SM_PATH . 'src/compose.php?mailbox='. urlencode($mailbox).
     $comp_uri = SM_PATH . 'src/compose.php?mailbox='. urlencode($mailbox).
@@ -219,7 +240,7 @@ if ($use_mailbox_cache && sqsession_is_registered('msgs')) {
         unset($numMessages);
         unset($numMessages);
     }
     }
 
 
-    $numMessages = sqimap_get_num_messages ($imapConnection, $mailbox);
+    $numMessages = $aMbxResponse['EXISTS'];
 
 
     showMessagesForMailbox($imapConnection, $mailbox, $numMessages,
     showMessagesForMailbox($imapConnection, $mailbox, $numMessages,
                            $startMessage, $sort, $color, $show_num,
                            $startMessage, $sort, $color, $show_num,
@@ -237,7 +258,8 @@ if ($use_mailbox_cache && sqsession_is_registered('msgs')) {
 }
 }
 do_hook('right_main_bottom');
 do_hook('right_main_bottom');
 sqimap_logout ($imapConnection);
 sqimap_logout ($imapConnection);
-
 echo '</body></html>';
 echo '</body></html>';
 
 
+sqsession_register($aLastSelectedMailbox,'aLastSelectedMailbox');
+
 ?>
 ?>