0
|
1 <?php
|
|
2
|
|
3 /**
|
|
4 +-----------------------------------------------------------------------+
|
|
5 | steps/mail/search.inc |
|
|
6 | |
|
|
7 | This file is part of the Roundcube Webmail client |
|
|
8 | Copyright (C) 2005-2013, The Roundcube Dev Team |
|
|
9 | |
|
|
10 | Licensed under the GNU General Public License version 3 or |
|
|
11 | any later version with exceptions for skins & plugins. |
|
|
12 | See the README file for a full license statement. |
|
|
13 | |
|
|
14 | PURPOSE: |
|
|
15 | Mail messages search action |
|
|
16 +-----------------------------------------------------------------------+
|
|
17 | Author: Benjamin Smith <defitro@gmail.com> |
|
|
18 | Thomas Bruederli <roundcube@gmail.com> |
|
|
19 +-----------------------------------------------------------------------+
|
|
20 */
|
|
21
|
|
22 $REMOTE_REQUEST = TRUE;
|
|
23
|
|
24 @set_time_limit(170); // extend default max_execution_time to ~3 minutes
|
|
25
|
|
26 // reset list_page and old search results
|
|
27 $RCMAIL->storage->set_page(1);
|
|
28 $RCMAIL->storage->set_search_set(NULL);
|
|
29 $_SESSION['page'] = 1;
|
|
30
|
|
31 // using encodeURI with javascript "should" give us
|
|
32 // a correctly encoded query string
|
|
33 $imap_charset = RCUBE_CHARSET;
|
|
34
|
|
35 // get search string
|
|
36 $str = rcube_utils::get_input_value('_q', rcube_utils::INPUT_GET, true);
|
|
37 $mbox = rcube_utils::get_input_value('_mbox', rcube_utils::INPUT_GET, true);
|
|
38 $filter = rcube_utils::get_input_value('_filter', rcube_utils::INPUT_GET);
|
|
39 $headers = rcube_utils::get_input_value('_headers', rcube_utils::INPUT_GET);
|
|
40 $scope = rcube_utils::get_input_value('_scope', rcube_utils::INPUT_GET);
|
|
41 $interval = rcube_utils::get_input_value('_interval', rcube_utils::INPUT_GET);
|
|
42 $continue = rcube_utils::get_input_value('_continue', rcube_utils::INPUT_GET);
|
|
43 $subject = array();
|
|
44
|
|
45 $filter = trim($filter);
|
|
46 $search_request = md5($mbox.$scope.$interval.$filter.$str);
|
|
47
|
|
48 // add list filter string
|
|
49 $search_str = $filter && $filter != 'ALL' ? $filter : '';
|
|
50
|
|
51 // Check the search string for type of search
|
|
52 if (preg_match("/^from:.*/i", $str)) {
|
|
53 list(,$srch) = explode(":", $str);
|
|
54 $subject['from'] = "HEADER FROM";
|
|
55 }
|
|
56 else if (preg_match("/^to:.*/i", $str)) {
|
|
57 list(,$srch) = explode(":", $str);
|
|
58 $subject['to'] = "HEADER TO";
|
|
59 }
|
|
60 else if (preg_match("/^cc:.*/i", $str)) {
|
|
61 list(,$srch) = explode(":", $str);
|
|
62 $subject['cc'] = "HEADER CC";
|
|
63 }
|
|
64 else if (preg_match("/^bcc:.*/i", $str)) {
|
|
65 list(,$srch) = explode(":", $str);
|
|
66 $subject['bcc'] = "HEADER BCC";
|
|
67 }
|
|
68 else if (preg_match("/^subject:.*/i", $str)) {
|
|
69 list(,$srch) = explode(":", $str);
|
|
70 $subject['subject'] = "HEADER SUBJECT";
|
|
71 }
|
|
72 else if (preg_match("/^body:.*/i", $str)) {
|
|
73 list(,$srch) = explode(":", $str);
|
|
74 $subject['body'] = "BODY";
|
|
75 }
|
|
76 else if (strlen(trim($str))) {
|
|
77 if ($headers) {
|
|
78 foreach (explode(',', $headers) as $header) {
|
|
79 if ($header == 'text') {
|
|
80 // #1488208: get rid of other headers when searching by "TEXT"
|
|
81 $subject = array('text' => 'TEXT');
|
|
82 break;
|
|
83 }
|
|
84 else {
|
|
85 $subject[$header] = ($header != 'body' ? 'HEADER ' : '') . strtoupper($header);
|
|
86 }
|
|
87 }
|
|
88
|
|
89 // save search modifiers for the current folder to user prefs
|
|
90 $mkey = $scope == 'all' ? '*' : $mbox;
|
|
91 $search_mods = rcmail_search_mods();
|
|
92 $search_mods[$mkey] = array_fill_keys(array_keys($subject), 1);
|
|
93
|
|
94 $RCMAIL->user->save_prefs(array('search_mods' => $search_mods));
|
|
95 }
|
|
96 else {
|
|
97 // search in subject by default
|
|
98 $subject['subject'] = 'HEADER SUBJECT';
|
|
99 }
|
|
100 }
|
|
101
|
|
102 $search = isset($srch) ? trim($srch) : trim($str);
|
|
103
|
|
104 if ($search_interval = rcmail_search_interval_criteria($interval)) {
|
|
105 $search_str .= ' ' . $search_interval;
|
|
106 }
|
|
107
|
|
108 if (!empty($subject)) {
|
|
109 $search_str .= str_repeat(' OR', count($subject)-1);
|
|
110 foreach ($subject as $sub) {
|
|
111 $search_str .= ' ' . $sub . ' ' . rcube_imap_generic::escape($search);
|
|
112 }
|
|
113 }
|
|
114
|
|
115 $search_str = trim($search_str);
|
|
116 $sort_column = rcmail_sort_column();
|
|
117
|
|
118 // set message set for already stored (but incomplete) search request
|
|
119 if (!empty($continue) && isset($_SESSION['search']) && $_SESSION['search_request'] == $continue) {
|
|
120 $RCMAIL->storage->set_search_set($_SESSION['search']);
|
|
121 $search_str = $_SESSION['search'][0];
|
|
122 }
|
|
123
|
|
124 // execute IMAP search
|
|
125 if ($search_str) {
|
|
126 // search all, current or subfolders folders
|
|
127 if ($scope == 'all') {
|
|
128 $mboxes = $RCMAIL->storage->list_folders_subscribed('', '*', 'mail', null, true);
|
|
129 natcasesort($mboxes); // we want natural alphabetic sorting of folders in the result set
|
|
130 }
|
|
131 else if ($scope == 'sub') {
|
|
132 $delim = $RCMAIL->storage->get_hierarchy_delimiter();
|
|
133 $mboxes = $RCMAIL->storage->list_folders_subscribed($mbox . $delim, '*', 'mail');
|
|
134 array_unshift($mboxes, $mbox);
|
|
135 }
|
|
136
|
|
137 if ($scope != 'all') {
|
|
138 // Remember current folder, it can change in meantime (plugins)
|
|
139 // but we need it to e.g. recognize Sent folder to handle From/To column later
|
|
140 $RCMAIL->output->set_env('mailbox', $mbox);
|
|
141 }
|
|
142
|
|
143 $result = $RCMAIL->storage->search($mboxes, $search_str, $imap_charset, $sort_column);
|
|
144 }
|
|
145
|
|
146 // save search results in session
|
|
147 if (!is_array($_SESSION['search'])) {
|
|
148 $_SESSION['search'] = array();
|
|
149 }
|
|
150
|
|
151 if ($search_str) {
|
|
152 $_SESSION['search'] = $RCMAIL->storage->get_search_set();
|
|
153 $_SESSION['last_text_search'] = $str;
|
|
154 }
|
|
155 $_SESSION['search_request'] = $search_request;
|
|
156 $_SESSION['search_scope'] = $scope;
|
|
157 $_SESSION['search_interval'] = $interval;
|
|
158 $_SESSION['search_filter'] = $filter;
|
|
159
|
|
160 // Get the headers
|
|
161 if (!$result->incomplete) {
|
|
162 $result_h = $RCMAIL->storage->list_messages($mbox, 1, $sort_column, rcmail_sort_order());
|
|
163 }
|
|
164
|
|
165 // Make sure we got the headers
|
|
166 if (!empty($result_h)) {
|
|
167 $count = $RCMAIL->storage->count($mbox, $RCMAIL->storage->get_threading() ? 'THREADS' : 'ALL');
|
|
168
|
|
169 rcmail_js_message_list($result_h, false);
|
|
170
|
|
171 if ($search_str) {
|
|
172 $OUTPUT->show_message('searchsuccessful', 'confirmation', array('nr' => $RCMAIL->storage->count(NULL, 'ALL')));
|
|
173 }
|
|
174
|
|
175 // remember last HIGHESTMODSEQ value (if supported)
|
|
176 // we need it for flag updates in check-recent
|
|
177 if ($mbox !== null) {
|
|
178 $data = $RCMAIL->storage->folder_data($mbox);
|
|
179 if (!empty($data['HIGHESTMODSEQ'])) {
|
|
180 $_SESSION['list_mod_seq'] = $data['HIGHESTMODSEQ'];
|
|
181 }
|
|
182 }
|
|
183 }
|
|
184 // handle IMAP errors (e.g. #1486905)
|
|
185 else if ($err_code = $RCMAIL->storage->get_error_code()) {
|
|
186 $count = 0;
|
|
187 $RCMAIL->display_server_error();
|
|
188 }
|
|
189 // advice the client to re-send the (cross-folder) search request
|
|
190 else if ($result->incomplete) {
|
|
191 $count = 0; // keep UI locked
|
|
192 $OUTPUT->command('continue_search', $search_request);
|
|
193 }
|
|
194 else {
|
|
195 $count = 0;
|
|
196 $OUTPUT->show_message('searchnomatch', 'notice');
|
|
197 $OUTPUT->set_env('multifolder_listing', (bool)$result->multi);
|
|
198 if ($result->multi && $scope == 'all') {
|
|
199 $OUTPUT->command('select_folder', '');
|
|
200 }
|
|
201 }
|
|
202
|
|
203 // update message count display
|
|
204 $OUTPUT->set_env('search_request', $search_str ? $search_request : '');
|
|
205 $OUTPUT->set_env('search_filter', $_SESSION['search_filter']);
|
|
206 $OUTPUT->set_env('messagecount', $count);
|
|
207 $OUTPUT->set_env('pagecount', ceil($count/$RCMAIL->storage->get_pagesize()));
|
|
208 $OUTPUT->set_env('exists', $mbox === null ? 0 : $RCMAIL->storage->count($mbox, 'EXISTS'));
|
|
209 $OUTPUT->command('set_rowcount', rcmail_get_messagecount_text($count, 1), $mbox);
|
|
210
|
|
211 rcmail_list_pagetitle();
|
|
212
|
|
213 // update unseen messages count
|
|
214 if (empty($search_str)) {
|
|
215 rcmail_send_unread_count($mbox, false, empty($result_h) ? 0 : null);
|
|
216 }
|
|
217
|
|
218 if (!$result->incomplete) {
|
|
219 $OUTPUT->command('set_quota', $RCMAIL->quota_content(null, $result->multi ? 'INBOX' : $mbox));
|
|
220 }
|
|
221
|
|
222 $OUTPUT->send();
|
|
223
|
|
224
|
|
225 // Creates BEFORE/SINCE search criteria from the specified interval
|
|
226 // Interval can be: 1W, 1M, 1Y, -1W, -1M, -1Y
|
|
227 function rcmail_search_interval_criteria($interval)
|
|
228 {
|
|
229 if (empty($interval)) {
|
|
230 return;
|
|
231 }
|
|
232
|
|
233 if ($interval[0] == '-') {
|
|
234 $search = 'BEFORE';
|
|
235 $interval = substr($interval, 1);
|
|
236 }
|
|
237 else {
|
|
238 $search = 'SINCE';
|
|
239 }
|
|
240
|
|
241 $date = new DateTime('now');
|
|
242 $interval = new DateInterval('P' . $interval);
|
|
243
|
|
244 $date->sub($interval);
|
|
245
|
|
246 return $search . ' ' . $date->format('j-M-Y');
|
|
247 }
|