source: subversion/trunk/roundcubemail/program/steps/mail/search.inc @ 5557

Last change on this file since 5557 was 5557, checked in by alec, 18 months ago
  • Fixed issues with big memory allocation of IMAP results, improved a lot of rcube_imap class
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.4 KB
Line 
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 $Id$
15
16*/
17
18$REMOTE_REQUEST = TRUE;
19
20// reset list_page and old search results
21$IMAP->set_page(1);
22$IMAP->set_search_set(NULL);
23$_SESSION['page'] = 1;
24
25// using encodeURI with javascript "should" give us
26// a correctly encoded query string
27$imap_charset = RCMAIL_CHARSET;
28
29// get search string
30$str     = get_input_value('_q', RCUBE_INPUT_GET, true);
31$mbox    = get_input_value('_mbox', RCUBE_INPUT_GET, true);
32$filter  = get_input_value('_filter', RCUBE_INPUT_GET);
33$headers = get_input_value('_headers', RCUBE_INPUT_GET);
34$subject = array();
35
36$search_request = md5($mbox.$filter.$str);
37
38// add list filter string
39$search_str = $filter && $filter != 'ALL' ? $filter : '';
40
41$_SESSION['search_filter'] = $filter;
42
43// Check the search string for type of search
44if (preg_match("/^from:.*/i", $str))
45{
46  list(,$srch) = explode(":", $str);
47  $subject['from'] = "HEADER FROM";
48}
49else if (preg_match("/^to:.*/i", $str))
50{
51  list(,$srch) = explode(":", $str);
52  $subject['to'] = "HEADER TO";
53}
54else if (preg_match("/^cc:.*/i", $str))
55{
56  list(,$srch) = explode(":", $str);
57  $subject['cc'] = "HEADER CC";
58}
59else if (preg_match("/^bcc:.*/i", $str))
60{
61  list(,$srch) = explode(":", $str);
62  $subject['bcc'] = "HEADER BCC";
63}
64else if (preg_match("/^subject:.*/i", $str))
65{
66  list(,$srch) = explode(":", $str);
67  $subject['subject'] = "HEADER SUBJECT";
68}
69else if (preg_match("/^body:.*/i", $str))
70{
71  list(,$srch) = explode(":", $str);
72  $subject['text'] = "TEXT";
73}
74else if (strlen(trim($str)))
75{
76  if ($headers) {
77    foreach (explode(',', $headers) as $header) {
78      if ($header == 'text') {
79        // #1488208: get rid of other headers when searching by "TEXT"
80        $subject = array('text' => 'TEXT');
81        break;
82      }
83      else {
84        $subject[$header] = 'HEADER '.strtoupper($header);
85      }
86    }
87
88    // save search modifiers for the current folder to user prefs
89    $search_mods = $RCMAIL->config->get('search_mods', $SEARCH_MODS_DEFAULT);
90    $search_mods[$mbox] = array_fill_keys(array_keys($subject), 1);
91    $RCMAIL->user->save_prefs(array('search_mods' => $search_mods));
92  } else {
93    // search in subject by default
94    $subject['subject'] = 'HEADER SUBJECT';
95  }
96}
97
98$search = isset($srch) ? trim($srch) : trim($str);
99
100if (!empty($subject)) {
101  $search_str .= str_repeat(' OR', count($subject)-1);
102  foreach ($subject as $sub)
103    $search_str .= sprintf(" %s {%d}\r\n%s", $sub, strlen($search), $search);
104}
105
106$search_str = trim($search_str);
107
108// execute IMAP search
109if ($search_str)
110  $IMAP->search($mbox, $search_str, $imap_charset, $_SESSION['sort_col']);
111
112// save search results in session
113if (!is_array($_SESSION['search']))
114  $_SESSION['search'] = array();
115
116if ($search_str) {
117  $_SESSION['search'] = $IMAP->get_search_set();
118  $_SESSION['last_text_search'] = $str;
119}
120$_SESSION['search_request'] = $search_request;
121
122
123// Get the headers
124$result_h = $IMAP->list_headers($mbox, 1, $_SESSION['sort_col'], $_SESSION['sort_order']);
125$count = $IMAP->messagecount($mbox, $IMAP->threading ? 'THREADS' : 'ALL');
126
127
128// Make sure we got the headers
129if (!empty($result_h)) {
130  rcmail_js_message_list($result_h);
131  if ($search_str)
132    $OUTPUT->show_message('searchsuccessful', 'confirmation', array('nr' => $IMAP->messagecount(NULL, 'ALL')));
133}
134// handle IMAP errors (e.g. #1486905)
135else  if ($err_code = $IMAP->get_error_code()) {
136  rcmail_display_server_error();
137}
138else {
139  $OUTPUT->show_message('searchnomatch', 'notice');
140}
141
142// update message count display
143$OUTPUT->set_env('search_request', $search_str ? $search_request : '');
144$OUTPUT->set_env('messagecount', $count);
145$OUTPUT->set_env('pagecount', ceil($count/$IMAP->page_size));
146$OUTPUT->command('set_rowcount', rcmail_get_messagecount_text($count, 1), $mbox);
147$OUTPUT->send();
148
149
Note: See TracBrowser for help on using the repository browser.