yllar
2006-12-16 77c28206a14b5bee3f3091f10cffd531bce5649c
commit | author | age
95c2c3 1 <?php
T 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>                            |
42000a 11  |         Thomas Bruederli <roundcube@gmail.com>                        |
95c2c3 12  +-----------------------------------------------------------------------+
T 13
14 */
15
16 $REMOTE_REQUEST = TRUE;
17
18 // reset list_page
19 $IMAP->set_page(1);
20 $_SESSION['page'] = 1;
21
4d4264 22 // using encodeURI with javascript "should" give us
T 23 // a correctly UTF-8 encoded query string
24 $imap_charset = 'UTF-8';
42000a 25
95c2c3 26 // get search string
T 27 $str = get_input_value('_search', RCUBE_INPUT_GET);
28 $mbox = get_input_value('_mbox', RCUBE_INPUT_GET);
ac6b87 29 $search_request = md5($str);
95c2c3 30
T 31
32 // Check the search string for type of search
33 if (preg_match("/^from:/i", $str)) {
34   list(,$srch) = explode(":", $str);
42000a 35   $search = $IMAP->search($mbox, "HEADER FROM" ,trim($srch), $imap_charset);
95c2c3 36   finish_search($mbox, $search);
T 37 }
38 else if (preg_match("/^to:/i", $str)) {
39   list(,$srch) = explode(":", $str);
42000a 40   $search = $IMAP->search($mbox, "HEADER TO", trim($srch), $imap_charset);
95c2c3 41   finish_search($mbox, $search);
T 42 }
43 else if (preg_match("/^cc:/i", $str)) {
44   list(,$srch) = explode(":", $str);
42000a 45   $search = $IMAP->search($mbox, "HEADER CC", trim($srch), $imap_charset);
95c2c3 46   finish_search($mbox, $search);
T 47 }
48 else if (preg_match("/^subject:/i", $str)) {
49   list(,$srch) = explode(":", $str);
42000a 50   $search = $IMAP->search($mbox, "HEADER SUBJECT", trim($srch), $imap_charset);
95c2c3 51   finish_search($mbox, $search);
T 52 }
53 else if (preg_match("/^body:/i", $str)) {
54   list(,$srch) = explode(":", $str);
42000a 55   $search = $IMAP->search($mbox, "TEXT", trim($srch), $imap_charset);
95c2c3 56   finish_search($mbox, $search);
T 57 }
58 // search in subject and sender by default
59 else {
42000a 60   $search = $IMAP->search($mbox, "HEADER SUBJECT", trim($str), $imap_charset);
T 61   $search2 = $IMAP->search($mbox, "HEADER FROM", trim($str), $imap_charset);
95c2c3 62   finish_search($mbox, array_unique(array_merge($search, $search2)));
T 63 }
64
65
66 // Complete the search display results or report error
67 function finish_search($mbox, $search)
68   {
ac6b87 69   global $IMAP, $JS_OBJECT_NAME, $OUTPUT, $search_request;
95c2c3 70   $commands = '';
T 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);
ac6b87 79
95c2c3 80     // save search results in session
T 81     if (!is_array($_SESSION['search']))
82       $_SESSION['search'] = array();
83
84     // Make sure we got the headers
85     if ($result_h != NULL)
86       {
ac6b87 87       $_SESSION['search'][$search_request] = join(',', $search);
95c2c3 88       $commands = rcmail_js_message_list($result_h);
T 89       $commands .= show_message('searchsuccessful', 'confirmation', array('nr' => $count));
90       }
91     }
92   else
ac6b87 93     {
95c2c3 94     $commands = show_message('searchnomatch', 'warning');
ac6b87 95     $search_request = -1;
T 96     }
95c2c3 97   
T 98   // update message count display
99   $pages = ceil($count/$IMAP->page_size);
ac6b87 100   $commands .= sprintf("\nthis.set_env('search_request', '%s')\n", $search_request);
95c2c3 101   $commands .= sprintf("this.set_env('messagecount', %d);\n", $count);
T 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 ?>