]> git.donarmstrong.com Git - roundcube.git/blob - program/steps/mail/search.inc
Imported Upstream version 0.1~beta2.2~dfsg
[roundcube.git] / program / steps / mail / search.inc
1 <?php
2 /*
3  +-----------------------------------------------------------------------+
4  | steps/mail/search.inc                                                 |
5  |                                                                       |
6  | Search functions for rc webmail                                       |
7  | Licensed under the GNU GPL                                            |
8  |                                                                       |
9  +-----------------------------------------------------------------------+
10  | Author: Benjamin Smith <defitro@gmail.com>                            |
11  |         Thomas Bruederli <roundcube@gmail.com>                        |
12  +-----------------------------------------------------------------------+
13
14 */
15
16 $REMOTE_REQUEST = TRUE;
17
18 // reset list_page
19 $IMAP->set_page(1);
20 $_SESSION['page'] = 1;
21
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';
25
26 // get search string
27 $str = get_input_value('_search', RCUBE_INPUT_GET);
28 $mbox = get_input_value('_mbox', RCUBE_INPUT_GET);
29 $search_request = md5($str);
30
31
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);
37 }
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);
42 }
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);
47 }
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);
52 }
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);
57 }
58 // search in subject and sender by default
59 else {
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)));
63 }
64
65
66 // Complete the search display results or report error
67 function finish_search($mbox, $search)
68   {
69   global $IMAP, $JS_OBJECT_NAME, $OUTPUT, $search_request;
70   $commands = '';
71   $count = 0;
72     
73   // Make sure our $search is legit..
74   if (is_array($search) && $search[0] != '')
75     {
76     // Get the headers
77     $result_h = $IMAP->list_header_set($mbox, $search, 1, $_SESSION['sort_col'], $_SESSION['sort_order']);
78     $count = count($search);
79
80     // save search results in session
81     if (!is_array($_SESSION['search']))
82       $_SESSION['search'] = array();
83
84     // Make sure we got the headers
85     if ($result_h != NULL)
86       {
87       $_SESSION['search'][$search_request] = join(',', $search);
88       $commands = rcmail_js_message_list($result_h);
89       $commands .= show_message('searchsuccessful', 'confirmation', array('nr' => $count));
90       }
91     }
92   else
93     {
94     $commands = show_message('searchnomatch', 'warning');
95     $search_request = -1;
96     }
97   
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);
105   }
106
107 ?>