Thomas Bruederli
2014-04-23 31aa080609f6ea8a561182eb5b3da46733bef313
commit | author | age
95c2c3 1 <?php
f5d2ee 2
95c2c3 3 /*
T 4  +-----------------------------------------------------------------------+
5  | steps/mail/search.inc                                                 |
6  |                                                                       |
f5d2ee 7  | This file is part of the Roundcube Webmail client                     |
AM 8  | Copyright (C) 2005-2013, The Roundcube Dev Team                       |
7fe381 9  |                                                                       |
T 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.                     |
95c2c3 13  |                                                                       |
f5d2ee 14  | PURPOSE:                                                              |
AM 15  |   Mail messages search action                                         |
95c2c3 16  +-----------------------------------------------------------------------+
T 17  | Author: Benjamin Smith <defitro@gmail.com>                            |
42000a 18  |         Thomas Bruederli <roundcube@gmail.com>                        |
95c2c3 19  +-----------------------------------------------------------------------+
T 20 */
21
22 $REMOTE_REQUEST = TRUE;
23
6884f3 24 @set_time_limit(170);  // extend default max_execution_time to ~3 minutes
TB 25
04c618 26 // reset list_page and old search results
c321a9 27 $RCMAIL->storage->set_page(1);
T 28 $RCMAIL->storage->set_search_set(NULL);
95c2c3 29 $_SESSION['page'] = 1;
T 30
4d4264 31 // using encodeURI with javascript "should" give us
ecb9fb 32 // a correctly encoded query string
6b2b2e 33 $imap_charset = RCUBE_CHARSET;
42000a 34
95c2c3 35 // get search string
6b2b2e 36 $str     = rcube_utils::get_input_value('_q', rcube_utils::INPUT_GET, true);
AM 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);
1bbf8c 40 $scope   = rcube_utils::get_input_value('_scope', rcube_utils::INPUT_GET);
31aa08 41 $continue = rcube_utils::get_input_value('_continue', rcube_utils::INPUT_GET);
3e5c70 42 $subject = array();
516467 43
a93081 44 $filter         = trim($filter);
26b520 45 $search_request = md5($mbox.$scope.$filter.$str);
95c2c3 46
e538b3 47 // add list filter string
A 48 $search_str = $filter && $filter != 'ALL' ? $filter : '';
49
50 $_SESSION['search_filter'] = $filter;
95c2c3 51
T 52 // Check the search string for type of search
f5d2ee 53 if (preg_match("/^from:.*/i", $str)) {
AM 54     list(,$srch) = explode(":", $str);
55     $subject['from'] = "HEADER FROM";
95c2c3 56 }
f5d2ee 57 else if (preg_match("/^to:.*/i", $str)) {
AM 58     list(,$srch) = explode(":", $str);
59     $subject['to'] = "HEADER TO";
95c2c3 60 }
f5d2ee 61 else if (preg_match("/^cc:.*/i", $str)) {
AM 62     list(,$srch) = explode(":", $str);
63     $subject['cc'] = "HEADER CC";
30b152 64 }
f5d2ee 65 else if (preg_match("/^bcc:.*/i", $str)) {
AM 66     list(,$srch) = explode(":", $str);
67     $subject['bcc'] = "HEADER BCC";
95c2c3 68 }
f5d2ee 69 else if (preg_match("/^subject:.*/i", $str)) {
AM 70     list(,$srch) = explode(":", $str);
71     $subject['subject'] = "HEADER SUBJECT";
95c2c3 72 }
f5d2ee 73 else if (preg_match("/^body:.*/i", $str)) {
AM 74     list(,$srch) = explode(":", $str);
75     $subject['body'] = "BODY";
95c2c3 76 }
f5d2ee 77 else if (strlen(trim($str))) {
AM 78     if ($headers) {
79         foreach (explode(',', $headers) as $header) {
80             if ($header == 'text') {
81                 // #1488208: get rid of other headers when searching by "TEXT"
82                 $subject = array('text' => 'TEXT');
83                 break;
84             }
85             else {
86                 $subject[$header] = ($header != 'body' ? 'HEADER ' : '') . strtoupper($header);
87             }
88         }
b1f084 89
f5d2ee 90         // save search modifiers for the current folder to user prefs
AM 91         $search_mods        = rcmail_search_mods();
92         $search_mods[$mbox] = 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     }
95c2c3 100 }
T 101
3e5c70 102 $search = isset($srch) ? trim($srch) : trim($str);
30b152 103
3e5c70 104 if (!empty($subject)) {
f5d2ee 105     $search_str .= str_repeat(' OR', count($subject)-1);
AM 106     foreach ($subject as $sub) {
107         $search_str .= ' ' . $sub . ' ' . rcube_imap_generic::escape($search);
108     }
26b520 109 }
19262e 110
26b520 111 $search_str  = trim($search_str);
TB 112 $sort_column = rcmail_sort_column();
113
31aa08 114 // set message set for already stored (but incomplete) search request
TB 115 if (!empty($continue) && isset($_SESSION['search']) && $_SESSION['search_request'] == $continue) {
116     $RCMAIL->storage->set_search_set($_SESSION['search']);
117     $search_str = $_SESSION['search'][0];
118 }
119
26b520 120 // execute IMAP search
TB 121 if ($search_str) {
19262e 122     // search all, current or subfolders folders
TB 123     if ($scope == 'all') {
188304 124         $mboxes = $RCMAIL->storage->list_folders_subscribed('', '*', 'mail', null, true);
TB 125         natcasesort($mboxes);  // we want natural alphabetic sorting of folders in the result set
19262e 126     }
TB 127     else if ($scope == 'sub') {
128         $mboxes = $RCMAIL->storage->list_folders_subscribed($mbox, '*', 'mail');
129         if ($mbox != 'INBOX' && $mboxes[0] == 'INBOX')
130             array_shift($mboxes);
131     }
e538b3 132
26b520 133     $result = $RCMAIL->storage->search($mboxes, $search_str, $imap_charset, $sort_column);
f5d2ee 134 }
0803fb 135
697cc5 136 // save search results in session
f5d2ee 137 if (!is_array($_SESSION['search'])) {
AM 138     $_SESSION['search'] = array();
139 }
697cc5 140
2a466a 141 if ($search_str) {
f5d2ee 142     $_SESSION['search'] = $RCMAIL->storage->get_search_set();
AM 143     $_SESSION['last_text_search'] = $str;
2a466a 144 }
f6aac3 145 $_SESSION['search_request'] = $search_request;
1bbf8c 146 $_SESSION['search_scope'] = $scope;
31aa08 147
TB 148
149 // Get the headers
150 if (!$result->incomplete) {
151     $result_h = $RCMAIL->storage->list_messages($mbox, 1, $sort_column, rcmail_sort_order());
152     $count    = $RCMAIL->storage->count($mbox, $RCMAIL->storage->get_threading() ? 'THREADS' : 'ALL');
153 }
40c45e 154
697cc5 155 // Make sure we got the headers
f6aac3 156 if (!empty($result_h)) {
19262e 157     rcmail_js_message_list($result_h, false);
f5d2ee 158     if ($search_str) {
AM 159         $OUTPUT->show_message('searchsuccessful', 'confirmation', array('nr' => $RCMAIL->storage->count(NULL, 'ALL')));
160     }
ac0fc3 161
f5d2ee 162     // remember last HIGHESTMODSEQ value (if supported)
AM 163     // we need it for flag updates in check-recent
164     $data = $RCMAIL->storage->folder_data($mbox_name);
165     if (!empty($data['HIGHESTMODSEQ'])) {
166         $_SESSION['list_mod_seq'] = $data['HIGHESTMODSEQ'];
167     }
f11541 168 }
2d1d68 169 // handle IMAP errors (e.g. #1486905)
31aa08 170 else if ($err_code = $RCMAIL->storage->get_error_code()) {
f5d2ee 171     $RCMAIL->display_server_error();
2d1d68 172 }
31aa08 173 // advice the client to re-send the (cross-folder) search request
TB 174 else if ($result->incomplete) {
175     $count = 0;  // keep UI locked
176     $OUTPUT->command('continue_search', $search_request);
177     console('search incomplete', strlen(serialize($result)));
178 }
f6aac3 179 else {
f5d2ee 180     $OUTPUT->show_message('searchnomatch', 'notice');
26b520 181     $OUTPUT->set_env('multifolder_listing', (bool)$result->multi);
TB 182     if ($result->multi && $scope == 'all')
183         $OUTPUT->command('select_folder', '');
f11541 184 }
04c618 185
T 186 // update message count display
bdb13a 187 $OUTPUT->set_env('search_request', $search_str ? $search_request : '');
26b520 188 $OUTPUT->set_env('search_filter', $_SESSION['search_filter']);
1d6082 189 $OUTPUT->set_env('threading', $RCMAIL->storage->get_threading());
f11541 190 $OUTPUT->set_env('messagecount', $count);
c321a9 191 $OUTPUT->set_env('pagecount', ceil($count/$RCMAIL->storage->get_pagesize()));
04689f 192 $OUTPUT->set_env('exists', $RCMAIL->storage->count($mbox_name, 'EXISTS'));
bba252 193 $OUTPUT->command('set_rowcount', rcmail_get_messagecount_text($count, 1), $mbox);
6dc1a6 194 $OUTPUT->set_pagetitle($RCMAIL->gettext(array('name' => 'searchfor', 'vars' => array('q' => $str))));
f11541 195 $OUTPUT->send();