3 +-----------------------------------------------------------------------+
4 | steps/mail/search.inc |
6 | Search functions for rc webmail |
7 | Licensed under the GNU GPL |
9 +-----------------------------------------------------------------------+
10 | Author: Benjamin Smith <defitro@gmail.com> |
11 | Thomas Bruederli <roundcube@gmail.com> |
12 +-----------------------------------------------------------------------+
16 $REMOTE_REQUEST = TRUE;
20 $_SESSION['page'] = 1;
22 // search query comes in with ISO encoding because javascript escape()
23 // uses ISO-8859-1. Better handling for that will follow.
24 $imap_charset = 'ISO-8859-1';
27 $str = get_input_value('_search', RCUBE_INPUT_GET);
28 $mbox = get_input_value('_mbox', RCUBE_INPUT_GET);
29 $search_request = md5($str);
32 // Check the search string for type of search
33 if (preg_match("/^from:/i", $str)) {
34 list(,$srch) = explode(":", $str);
35 $search = $IMAP->search($mbox, "HEADER FROM" ,trim($srch), $imap_charset);
36 finish_search($mbox, $search);
38 else if (preg_match("/^to:/i", $str)) {
39 list(,$srch) = explode(":", $str);
40 $search = $IMAP->search($mbox, "HEADER TO", trim($srch), $imap_charset);
41 finish_search($mbox, $search);
43 else if (preg_match("/^cc:/i", $str)) {
44 list(,$srch) = explode(":", $str);
45 $search = $IMAP->search($mbox, "HEADER CC", trim($srch), $imap_charset);
46 finish_search($mbox, $search);
48 else if (preg_match("/^subject:/i", $str)) {
49 list(,$srch) = explode(":", $str);
50 $search = $IMAP->search($mbox, "HEADER SUBJECT", trim($srch), $imap_charset);
51 finish_search($mbox, $search);
53 else if (preg_match("/^body:/i", $str)) {
54 list(,$srch) = explode(":", $str);
55 $search = $IMAP->search($mbox, "TEXT", trim($srch), $imap_charset);
56 finish_search($mbox, $search);
58 // search in subject and sender by default
60 $search = $IMAP->search($mbox, "HEADER SUBJECT", trim($str), $imap_charset);
61 $search2 = $IMAP->search($mbox, "HEADER FROM", trim($str), $imap_charset);
62 finish_search($mbox, array_unique(array_merge($search, $search2)));
66 // Complete the search display results or report error
67 function finish_search($mbox, $search)
69 global $IMAP, $JS_OBJECT_NAME, $OUTPUT, $search_request;
73 // Make sure our $search is legit..
74 if (is_array($search) && $search[0] != '')
77 $result_h = $IMAP->list_header_set($mbox, $search, 1, $_SESSION['sort_col'], $_SESSION['sort_order']);
78 $count = count($search);
80 // save search results in session
81 if (!is_array($_SESSION['search']))
82 $_SESSION['search'] = array();
84 // Make sure we got the headers
85 if ($result_h != NULL)
87 $_SESSION['search'][$search_request] = join(',', $search);
88 $commands = rcmail_js_message_list($result_h);
89 $commands .= show_message('searchsuccessful', 'confirmation', array('nr' => $count));
94 $commands = show_message('searchnomatch', 'warning');
98 // update message count display
99 $pages = ceil($count/$IMAP->page_size);
100 $commands .= sprintf("\nthis.set_env('search_request', '%s')\n", $search_request);
101 $commands .= sprintf("this.set_env('messagecount', %d);\n", $count);
102 $commands .= sprintf("this.set_env('pagecount', %d);\n", $pages);
103 $commands .= sprintf("this.set_rowcount('%s');\n", rcmail_get_messagecount_text($count, 1));
104 rcube_remote_response($commands);