Aleksander Machniak
2014-05-12 2b72ec46a761ded28c83ef2ac544c9406a874802
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
7d213a 91         $mkey               = $scope == 'all' ? '*' : $mbox;
f5d2ee 92         $search_mods        = rcmail_search_mods();
7d213a 93         $search_mods[$mkey] = array_fill_keys(array_keys($subject), 1);
f5d2ee 94
AM 95         $RCMAIL->user->save_prefs(array('search_mods' => $search_mods));
96     }
97     else {
98         // search in subject by default
99         $subject['subject'] = 'HEADER SUBJECT';
100     }
95c2c3 101 }
T 102
3e5c70 103 $search = isset($srch) ? trim($srch) : trim($str);
30b152 104
3e5c70 105 if (!empty($subject)) {
f5d2ee 106     $search_str .= str_repeat(' OR', count($subject)-1);
AM 107     foreach ($subject as $sub) {
108         $search_str .= ' ' . $sub . ' ' . rcube_imap_generic::escape($search);
109     }
26b520 110 }
19262e 111
26b520 112 $search_str  = trim($search_str);
TB 113 $sort_column = rcmail_sort_column();
114
31aa08 115 // set message set for already stored (but incomplete) search request
TB 116 if (!empty($continue) && isset($_SESSION['search']) && $_SESSION['search_request'] == $continue) {
117     $RCMAIL->storage->set_search_set($_SESSION['search']);
118     $search_str = $_SESSION['search'][0];
119 }
120
26b520 121 // execute IMAP search
TB 122 if ($search_str) {
19262e 123     // search all, current or subfolders folders
TB 124     if ($scope == 'all') {
188304 125         $mboxes = $RCMAIL->storage->list_folders_subscribed('', '*', 'mail', null, true);
TB 126         natcasesort($mboxes);  // we want natural alphabetic sorting of folders in the result set
19262e 127     }
TB 128     else if ($scope == 'sub') {
129         $mboxes = $RCMAIL->storage->list_folders_subscribed($mbox, '*', 'mail');
130         if ($mbox != 'INBOX' && $mboxes[0] == 'INBOX')
131             array_shift($mboxes);
132     }
e538b3 133
26b520 134     $result = $RCMAIL->storage->search($mboxes, $search_str, $imap_charset, $sort_column);
f5d2ee 135 }
0803fb 136
697cc5 137 // save search results in session
f5d2ee 138 if (!is_array($_SESSION['search'])) {
AM 139     $_SESSION['search'] = array();
140 }
697cc5 141
2a466a 142 if ($search_str) {
f5d2ee 143     $_SESSION['search'] = $RCMAIL->storage->get_search_set();
AM 144     $_SESSION['last_text_search'] = $str;
2a466a 145 }
f6aac3 146 $_SESSION['search_request'] = $search_request;
1bbf8c 147 $_SESSION['search_scope'] = $scope;
31aa08 148
TB 149
150 // Get the headers
151 if (!$result->incomplete) {
152     $result_h = $RCMAIL->storage->list_messages($mbox, 1, $sort_column, rcmail_sort_order());
153     $count    = $RCMAIL->storage->count($mbox, $RCMAIL->storage->get_threading() ? 'THREADS' : 'ALL');
154 }
40c45e 155
697cc5 156 // Make sure we got the headers
f6aac3 157 if (!empty($result_h)) {
19262e 158     rcmail_js_message_list($result_h, false);
f5d2ee 159     if ($search_str) {
AM 160         $OUTPUT->show_message('searchsuccessful', 'confirmation', array('nr' => $RCMAIL->storage->count(NULL, 'ALL')));
161     }
ac0fc3 162
f5d2ee 163     // remember last HIGHESTMODSEQ value (if supported)
AM 164     // we need it for flag updates in check-recent
165     $data = $RCMAIL->storage->folder_data($mbox_name);
166     if (!empty($data['HIGHESTMODSEQ'])) {
167         $_SESSION['list_mod_seq'] = $data['HIGHESTMODSEQ'];
168     }
f11541 169 }
2d1d68 170 // handle IMAP errors (e.g. #1486905)
31aa08 171 else if ($err_code = $RCMAIL->storage->get_error_code()) {
f5d2ee 172     $RCMAIL->display_server_error();
2d1d68 173 }
31aa08 174 // advice the client to re-send the (cross-folder) search request
TB 175 else if ($result->incomplete) {
176     $count = 0;  // keep UI locked
177     $OUTPUT->command('continue_search', $search_request);
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();