Dennis1993
2013-09-02 7dff4437c06369da69841e3a4fee2b931bac0ccc
commit | author | age
95c2c3 1 <?php
T 2 /*
3  +-----------------------------------------------------------------------+
4  | steps/mail/search.inc                                                 |
5  |                                                                       |
6  | Search functions for rc webmail                                       |
7fe381 7  |                                                                       |
T 8  | Licensed under the GNU General Public License version 3 or            |
9  | any later version with exceptions for skins & plugins.                |
10  | See the README file for a full license statement.                     |
95c2c3 11  |                                                                       |
T 12  +-----------------------------------------------------------------------+
13  | Author: Benjamin Smith <defitro@gmail.com>                            |
42000a 14  |         Thomas Bruederli <roundcube@gmail.com>                        |
95c2c3 15  +-----------------------------------------------------------------------+
T 16 */
17
18 $REMOTE_REQUEST = TRUE;
19
04c618 20 // reset list_page and old search results
c321a9 21 $RCMAIL->storage->set_page(1);
T 22 $RCMAIL->storage->set_search_set(NULL);
95c2c3 23 $_SESSION['page'] = 1;
T 24
4d4264 25 // using encodeURI with javascript "should" give us
ecb9fb 26 // a correctly encoded query string
A 27 $imap_charset = RCMAIL_CHARSET;
42000a 28
95c2c3 29 // get search string
516467 30 $str     = get_input_value('_q', RCUBE_INPUT_GET, true);
A 31 $mbox    = get_input_value('_mbox', RCUBE_INPUT_GET, true);
32 $filter  = get_input_value('_filter', RCUBE_INPUT_GET);
30b152 33 $headers = get_input_value('_headers', RCUBE_INPUT_GET);
3e5c70 34 $subject = array();
516467 35
e538b3 36 $search_request = md5($mbox.$filter.$str);
95c2c3 37
e538b3 38 // add list filter string
A 39 $search_str = $filter && $filter != 'ALL' ? $filter : '';
40
41 $_SESSION['search_filter'] = $filter;
95c2c3 42
T 43 // Check the search string for type of search
e538b3 44 if (preg_match("/^from:.*/i", $str))
04c618 45 {
95c2c3 46   list(,$srch) = explode(":", $str);
30b152 47   $subject['from'] = "HEADER FROM";
95c2c3 48 }
1af6fe 49 else if (preg_match("/^to:.*/i", $str))
04c618 50 {
95c2c3 51   list(,$srch) = explode(":", $str);
30b152 52   $subject['to'] = "HEADER TO";
95c2c3 53 }
e538b3 54 else if (preg_match("/^cc:.*/i", $str))
04c618 55 {
95c2c3 56   list(,$srch) = explode(":", $str);
30b152 57   $subject['cc'] = "HEADER CC";
A 58 }
59 else if (preg_match("/^bcc:.*/i", $str))
60 {
61   list(,$srch) = explode(":", $str);
62   $subject['bcc'] = "HEADER BCC";
95c2c3 63 }
e538b3 64 else if (preg_match("/^subject:.*/i", $str))
04c618 65 {
95c2c3 66   list(,$srch) = explode(":", $str);
30b152 67   $subject['subject'] = "HEADER SUBJECT";
95c2c3 68 }
e538b3 69 else if (preg_match("/^body:.*/i", $str))
04c618 70 {
95c2c3 71   list(,$srch) = explode(":", $str);
347ba3 72   $subject['body'] = "BODY";
95c2c3 73 }
3e5c70 74 else if (strlen(trim($str)))
04c618 75 {
30b152 76   if ($headers) {
3e5c70 77     foreach (explode(',', $headers) as $header) {
A 78       if ($header == 'text') {
79         // #1488208: get rid of other headers when searching by "TEXT"
80         $subject = array('text' => 'TEXT');
81         break;
30b152 82       }
3e5c70 83       else {
347ba3 84         $subject[$header] = ($header != 'body' ? 'HEADER ' : '') . strtoupper($header);
3e5c70 85       }
A 86     }
b1f084 87
7910c0 88     // save search modifiers for the current folder to user prefs
T 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));
347ba3 92   }
AM 93   else {
ae119b 94     // search in subject by default
30b152 95     $subject['subject'] = 'HEADER SUBJECT';
A 96   }
95c2c3 97 }
T 98
3e5c70 99 $search = isset($srch) ? trim($srch) : trim($str);
30b152 100
3e5c70 101 if (!empty($subject)) {
30b152 102   $search_str .= str_repeat(' OR', count($subject)-1);
A 103   foreach ($subject as $sub)
a04a74 104     $search_str .= ' ' . $sub . ' ' . rcube_imap_generic::escape($search);
e538b3 105 }
A 106
703be8 107 $search_str  = trim($search_str);
AM 108 $sort_column = rcmail_sort_column();
e538b3 109
04c618 110 // execute IMAP search
697cc5 111 if ($search_str)
e0efd8 112   $RCMAIL->storage->search($mbox, $search_str, $imap_charset, $sort_column);
0803fb 113
697cc5 114 // save search results in session
A 115 if (!is_array($_SESSION['search']))
116   $_SESSION['search'] = array();
117
2a466a 118 if ($search_str) {
c321a9 119   $_SESSION['search'] = $RCMAIL->storage->get_search_set();
2a466a 120   $_SESSION['last_text_search'] = $str;
A 121 }
f6aac3 122 $_SESSION['search_request'] = $search_request;
2a466a 123
40c45e 124
A 125 // Get the headers
703be8 126 $result_h = $RCMAIL->storage->list_messages($mbox, 1, $sort_column, rcmail_sort_order());
c321a9 127 $count = $RCMAIL->storage->count($mbox, $RCMAIL->storage->get_threading() ? 'THREADS' : 'ALL');
40c45e 128
697cc5 129 // Make sure we got the headers
f6aac3 130 if (!empty($result_h)) {
3414c5 131   rcmail_js_message_list($result_h);
2a466a 132   if ($search_str)
c321a9 133     $OUTPUT->show_message('searchsuccessful', 'confirmation', array('nr' => $RCMAIL->storage->count(NULL, 'ALL')));
f11541 134 }
2d1d68 135 // handle IMAP errors (e.g. #1486905)
c321a9 136 else  if ($err_code = $RCMAIL->storage->get_error_code()) {
90f81a 137   rcmail_display_server_error();
2d1d68 138 }
f6aac3 139 else {
b76275 140   $OUTPUT->show_message('searchnomatch', 'notice');
f11541 141 }
04c618 142
T 143 // update message count display
bdb13a 144 $OUTPUT->set_env('search_request', $search_str ? $search_request : '');
f11541 145 $OUTPUT->set_env('messagecount', $count);
c321a9 146 $OUTPUT->set_env('pagecount', ceil($count/$RCMAIL->storage->get_pagesize()));
04689f 147 $OUTPUT->set_env('exists', $RCMAIL->storage->count($mbox_name, 'EXISTS'));
bba252 148 $OUTPUT->command('set_rowcount', rcmail_get_messagecount_text($count, 1), $mbox);
f11541 149 $OUTPUT->send();