SORT and THREAD handling in search
[squirrelmail.git] / functions / imap_asearch.php
index 7a515cec585c1a1d7c7c953c5ce9a868587dceb6..d1c5edfeb14ef0ea2b1a0e2deb6054c24382a066 100644 (file)
@@ -7,15 +7,17 @@
  * Licensed under the GNU GPL. For full terms see the file COPYING.
  *
  * IMAP asearch routines
- * Alex Lemaresquier - Brainstorm - alex at brainstorm.fr
+ * @author Alex Lemaresquier - Brainstorm - alex at brainstorm.fr
  * See README file for infos.
+ * @package squirrelmail
  *
  */
 
+/** This functionality requires the IMAP and date functions */
 require_once(SM_PATH . 'functions/imap_general.php');
 require_once(SM_PATH . 'functions/date.php');
 
-/* Set to TRUE to dump the imap dialogue */
+/** Set to TRUE to dump the imap dialogue */
 $imap_asearch_debug_dump = FALSE;
 
 $imap_asearch_opcodes = array(
@@ -79,6 +81,8 @@ $imap_error_titles = array(
        'BYE' => _("ERROR : Imap server closed the connection.")
 );
 
+// why can't this just use sqimap_error_box() ?
+// It does, indeed I isolated sqimap_error_box() as a stand-alone function just for this purpose ;)
 function sqimap_asearch_error_box($response, $query, $message)
 {
        global $imap_error_titles;
@@ -92,6 +96,7 @@ function sqimap_asearch_error_box($response, $query, $message)
        if (function_exists('sqimap_error_box'))
                sqimap_error_box($title, $query, $message_title, $message);
        else {  //Straight copy of 1.5 imap_general.php:sqimap_error_box(). Can be removed at a later time
+               global $color;
     require_once(SM_PATH . 'functions/display_messages.php');
     $string = "<font color=$color[2]><b>\n" . $title . "</b><br>\n";
     if ($query != '')
@@ -105,7 +110,9 @@ function sqimap_asearch_error_box($response, $query, $message)
        }
 }
 
-/* This is to avoid the E_NOTICE warnings signaled by marc AT squirrelmail.org. Thanks Marc! */
+/**
+ * This is to avoid the E_NOTICE warnings signaled by marc AT squirrelmail.org. Thanks Marc!
+ */
 function asearch_nz(&$var)
 {
        if (isset($var))
@@ -113,7 +120,10 @@ function asearch_nz(&$var)
        return '';
 }
 
-/* This should give the same results as PHP 4 >= 4.3.0's html_entity_decode(), except it doesn't handle hex constructs */
+/**
+ * This should give the same results as PHP 4 >= 4.3.0's html_entity_decode(),
+ * except it doesn't handle hex constructs
+ */
 function asearch_unhtmlentities($string) {
        $trans_tbl = array_flip(get_html_translation_table(HTML_ENTITIES));
        for ($i=127; $i<255; $i++)      /* Add &#<dec>; entities */
@@ -153,15 +163,17 @@ function sqimap_asearch_encode_string($what, $charset)
                $what = mb_convert_encoding($what, 'JIS', 'auto');
 //if (ereg("[\"\\\r\n\x80-\xff]", $what))
        if (preg_match('/["\\\\\r\n\x80-\xff]/', $what))
-               return '{' . strlen($what) . "}\r\n" . $what;   /* 4.3 literal form */
-       return '"' . $what . '"';       /* 4.3 quoted string form */
+               return '{' . strlen($what) . "}\r\n" . $what;   // 4.3 literal form
+       return '"' . $what . '"';       // 4.3 quoted string form
 }
 
-/*
- Parses a user date string into an rfc2060 date string (<day number>-<US month TLA>-<4 digit year>)
- Returns a preg_match-style array: [0]: fully formatted date, [1]: day, [2]: month, [3]: year
- Handles space, slash, backslash, dot and comma as separators (and dash of course ;=)
-*/
+/**
+ * Parses a user date string into an rfc2060 date string
+ * (<day number>-<US month TLA>-<4 digit year>).
+ * Returns a preg_match-style array: [0]: fully formatted date,
+ * [1]: day, [2]: month, [3]: year
+ * Handles space, slash, backslash, dot and comma as separators (and dash of course ;=)
+ */
 function sqimap_asearch_parse_date($what)
 {
        global $imap_asearch_months;
@@ -258,10 +270,9 @@ function sqimap_run_search($imapConnection, $search_string, $search_charset)
 
        unset($messagelist);
 
-       /* Keep going till we find the SEARCH response */
+       /* Keep going till we find the SEARCH response */
        foreach ($readin as $readin_part) {
                s_debug_dump('S:', $readin_part);
-               /* Check to see if a SEARCH response was received */
                if (substr($readin_part, 0, 9) == '* SEARCH ') {
                        $messagelist = preg_split("/ /", substr($readin_part, 9));
                        break;  // Should be the last anyway
@@ -290,6 +301,129 @@ function sqimap_run_search($imapConnection, $search_string, $search_charset)
        return $id;
 }
 
+function sqimap_run_sort($imapConnection, $search_string, $search_charset, $sort_criteria)
+{
+       global $uid_support;
+
+       if ($search_charset == '')
+               $search_charset = 'US-ASCII';
+       $query = 'SORT (' . $sort_criteria . ') ' . strtoupper($search_charset) . ' ALL ' . $search_string;
+       s_debug_dump('C:', $query);
+       $readin = sqimap_run_command($imapConnection, $query, false, $response, $message, $uid_support);
+
+       /* 6.4 try US-ASCII charset if we received a tagged NO response (SHOULD be [BADCHARSET]) */
+       if (($search_charset != 'US-ASCII')  && (strtoupper($response) == 'NO')) {
+               $query = 'SORT (' . $sort_criteria . ') US-ASCII ALL ' . $search_string;
+               s_debug_dump('C:', $query);
+               $readin = sqimap_run_command($imapConnection, $query, false, $response, $message, $uid_support);
+       }
+
+       if (strtoupper($response) != 'OK') {
+//             sqimap_asearch_error_box($response, $query, $message);
+//             return array();
+                       return sqimap_run_search($imapConnection, $search_string, $search_charset);     // Fell back to standard search
+       }
+
+       /* Keep going till we find the * SORT response */
+       foreach ($readin as $readin_part) {
+               s_debug_dump('S:', $readin_part);
+               if (substr($readin_part, 0, 7) == '* SORT ') {
+                       $messagelist = preg_split("/ /", substr($readin_part, 7));
+                       break;  // Should be the last anyway
+               }
+       }
+
+       if (empty($messagelist))        //Empty search response, ie '* SORT'
+               return array();
+
+       $cnt = count($messagelist);
+       for ($q = 0; $q < $cnt; $q++)
+               $id[$q] = trim($messagelist[$q]);
+       return $id;
+}
+
+function sqimap_run_thread($imapConnection, $search_string, $search_charset, $thread_algorithm)
+{
+       global $thread_new, $server_sort_array;
+
+       if (sqsession_is_registered('thread_new'))
+               sqsession_unregister('thread_new');
+       if (sqsession_is_registered('server_sort_array'))
+               sqsession_unregister('server_sort_array');
+
+       $thread_new = array();
+       $thread_new[0] = "";
+
+       $server_sort_array = array();
+
+       global $uid_support;
+
+       if ($search_charset == '')
+               $search_charset = 'US-ASCII';
+       $query = 'THREAD ' . $thread_algorithm . ' ' . strtoupper($search_charset) . ' ALL ' . $search_string;
+       s_debug_dump('C:', $query);
+       $readin = sqimap_run_command($imapConnection, $query, false, $response, $message, $uid_support);
+
+       /* 6.4 try US-ASCII charset if we received a tagged NO response (SHOULD be [BADCHARSET]) */
+       if (($search_charset != 'US-ASCII')  && (strtoupper($response) == 'NO')) {
+               $query = 'THREAD ' . $thread_algorithm . ' US-ASCII ALL ' . $search_string;
+               s_debug_dump('C:', $query);
+               $readin = sqimap_run_command($imapConnection, $query, false, $response, $message, $uid_support);
+       }
+
+       if (strtoupper($response) != 'OK') {
+//             sqimap_asearch_error_box($response, $query, $message);
+//             return array();
+                       return sqimap_run_search($imapConnection, $search_string, $search_charset);     // Fell back to standard search
+       }
+
+       /* Keep going till we find the * THREAD response */
+       foreach ($readin as $readin_part) {
+               s_debug_dump('S:', $readin_part);
+               if (substr($readin_part, 0, 9) == '* THREAD ') {
+                       $thread_temp = preg_split("//", substr($readin_part, 9), -1, PREG_SPLIT_NO_EMPTY);
+                       break;  // Should be the last anyway
+               }
+       }
+
+       if (empty($thread_temp))        //Empty search response, ie '* THREAD'
+               return array();
+
+       $char_count = count($thread_temp);
+       $counter = 0;
+       $k = 0;
+       for ($i=0;$i<$char_count;$i++) {
+        if ($thread_temp[$i] != ')' && $thread_temp[$i] != '(') {
+                $thread_new[$k] = $thread_new[$k] . $thread_temp[$i];
+        }
+        elseif ($thread_temp[$i] == '(') {
+                $thread_new[$k] .= $thread_temp[$i];
+                $counter++;
+        }
+        elseif ($thread_temp[$i] == ')') {
+                if ($counter > 1) {
+                        $thread_new[$k] .= $thread_temp[$i];
+                        $counter = $counter - 1;
+                }
+                else {
+                        $thread_new[$k] .= $thread_temp[$i];
+                        $k++;
+                        $thread_new[$k] = "";
+                        $counter = $counter - 1;
+                }
+        }
+       }
+       sqsession_register($thread_new, 'thread_new');
+       $thread_new = array_reverse($thread_new);
+       $thread_list = implode(" ", $thread_new);
+       $thread_list = str_replace("(", " ", $thread_list);
+       $thread_list = str_replace(")", " ", $thread_list);
+       $thread_list = preg_split("/\s/", $thread_list, -1, PREG_SPLIT_NO_EMPTY);
+       $server_sort_array = $thread_list;
+       sqsession_register($server_sort_array, 'server_sort_array');
+       return $thread_list;
+}
+
 function sqimap_asearch_get_charset()
 {
        global $allow_charset_search, $languages, $squirrelmail_language;
@@ -299,6 +433,20 @@ function sqimap_asearch_get_charset()
        return '';
 }
 
+function sqimap_asearch_get_sort_criteria($mailbox, $sort_by)
+{
+       global $internal_date_sort, $sent_folder;
+
+       $sort_opcodes = array ('DATE', 'FROM', 'SUBJECT');
+       if ($internal_date_sort == true)
+               $sort_opcodes[0] = 'ARRIVAL';
+//     if (handleAsSent($mailbox))
+//     if (isSentFolder($mailbox))
+       if ($mailbox == $sent_folder)
+               $sort_opcodes[1] = 'TO';
+       return (($sort_by % 2) ? '' : 'REVERSE ') . $sort_opcodes[$sort_by >> 1];
+}
+
 /* replaces $mbox_msgs[$search_mailbox] = array_values(array_unique(array_merge($mbox_msgs[$search_mailbox], sqimap_run_search($imapConnection, $search_string, $search_charset))));*/
 function sqimap_array_merge_unique($to, $from)
 {
@@ -313,6 +461,9 @@ function sqimap_array_merge_unique($to, $from)
 
 function sqimap_asearch($imapConnection, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $mboxes_array)
 {
+       global $allow_server_sort, $sort, $allow_thread_sort, $thread_sort_messages;
+       global $data_dir, $username;
+
        $search_charset = sqimap_asearch_get_charset();
        $mbox_msgs = array();
        $search_string = '';
@@ -331,14 +482,26 @@ function sqimap_asearch($imapConnection, $mailbox_array, $biop_array, $unop_arra
                                foreach ($search_mboxes as $cur_mailbox) {
                                        s_debug_dump('C:SELECT:', $cur_mailbox);
                                        sqimap_mailbox_select($imapConnection, $cur_mailbox);
+                                       $thread_sort_messages = $allow_thread_sort && getPref($data_dir, $username, 'thread_' . $cur_mailbox);
+                                       if ($thread_sort_messages) {
+                                               $thread_algorithm = 'REFERENCES';
+                                               $found_msgs = sqimap_run_thread($imapConnection, $search_string, $search_charset, $thread_algorithm);
+                                       }
+                                       else
+                                       if (($allow_server_sort) && ($sort < 6)) {
+                                               $sort_criteria = sqimap_asearch_get_sort_criteria($cur_mailbox, $sort);
+                                               $found_msgs = sqimap_run_sort($imapConnection, $search_string, $search_charset, $sort_criteria);
+                                       }
+                                       else
+                                               $found_msgs = sqimap_run_search($imapConnection, $search_string, $search_charset);
                                        if (isset($mbox_msgs[$cur_mailbox])) {
                                                if ($cur_biop == 'OR')  /* Merge with previous results */
-                                                       $mbox_msgs[$cur_mailbox] = sqimap_array_merge_unique($mbox_msgs[$cur_mailbox], sqimap_run_search($imapConnection, $search_string, $search_charset));
+                                                       $mbox_msgs[$cur_mailbox] = sqimap_array_merge_unique($mbox_msgs[$cur_mailbox], $found_msgs);
                                                else    /* Intersect previous results */
-                                                       $mbox_msgs[$cur_mailbox] = array_values(array_intersect(sqimap_run_search($imapConnection, $search_string, $search_charset), $mbox_msgs[$cur_mailbox]));
+                                                       $mbox_msgs[$cur_mailbox] = array_values(array_intersect($found_msgs, $mbox_msgs[$cur_mailbox]));
                                        }
                                        else /* No previous results */
-                                               $mbox_msgs[$cur_mailbox] = sqimap_run_search($imapConnection, $search_string, $search_charset);
+                                               $mbox_msgs[$cur_mailbox] = $found_msgs;
                                        if (empty($mbox_msgs[$cur_mailbox]))    /* Can happen with intersect, and we need at the end a contiguous array */
                                                unset($mbox_msgs[$cur_mailbox]);
                                }
@@ -371,4 +534,4 @@ function sqimap_asearch($imapConnection, $mailbox_array, $biop_array, $unop_arra
        return $mbox_msgs;
 }
 
-?>
\ No newline at end of file
+?>