| 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 | */ |
|---|
| 15 | |
|---|
| 16 | $REMOTE_REQUEST = TRUE; |
|---|
| 17 | |
|---|
| 18 | // reset list_page and old search results |
|---|
| 19 | $IMAP->set_page(1); |
|---|
| 20 | $IMAP->set_search_set(NULL); |
|---|
| 21 | $_SESSION['page'] = 1; |
|---|
| 22 | |
|---|
| 23 | // using encodeURI with javascript "should" give us |
|---|
| 24 | // a correctly UTF-8 encoded query string |
|---|
| 25 | $imap_charset = 'UTF-8'; |
|---|
| 26 | |
|---|
| 27 | // get search string |
|---|
| 28 | $str = get_input_value('_search', RCUBE_INPUT_GET); |
|---|
| 29 | $mbox = get_input_value('_mbox', RCUBE_INPUT_GET); |
|---|
| 30 | $search_request = md5($mbox.$str); |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | // Check the search string for type of search |
|---|
| 34 | if (preg_match("/^from:/i", $str)) |
|---|
| 35 | { |
|---|
| 36 | list(,$srch) = explode(":", $str); |
|---|
| 37 | $subject = "HEADER FROM"; |
|---|
| 38 | $search = trim($srch); |
|---|
| 39 | } |
|---|
| 40 | else if (preg_match("/^to:/i", $str)) |
|---|
| 41 | { |
|---|
| 42 | list(,$srch) = explode(":", $str); |
|---|
| 43 | $subject = "HEADER TO"; |
|---|
| 44 | $search = trim($srch); |
|---|
| 45 | } |
|---|
| 46 | else if (preg_match("/^cc:/i", $str)) |
|---|
| 47 | { |
|---|
| 48 | list(,$srch) = explode(":", $str); |
|---|
| 49 | $subject = "HEADER CC"; |
|---|
| 50 | $search = trim($srch); |
|---|
| 51 | } |
|---|
| 52 | else if (preg_match("/^subject:/i", $str)) |
|---|
| 53 | { |
|---|
| 54 | list(,$srch) = explode(":", $str); |
|---|
| 55 | $subject = "HEADER SUBJECT"; |
|---|
| 56 | $search = trim($srch); |
|---|
| 57 | } |
|---|
| 58 | else if (preg_match("/^body:/i", $str)) |
|---|
| 59 | { |
|---|
| 60 | list(,$srch) = explode(":", $str); |
|---|
| 61 | $subject = "TEXT"; |
|---|
| 62 | $search = trim($srch); |
|---|
| 63 | } |
|---|
| 64 | // search in subject and sender by default |
|---|
| 65 | else |
|---|
| 66 | { |
|---|
| 67 | $subject = array("HEADER SUBJECT", "HEADER FROM"); |
|---|
| 68 | $search = trim($str); |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | |
|---|
| 72 | // execute IMAP search |
|---|
| 73 | $result = $IMAP->search($mbox, $subject, $search, $imap_charset); |
|---|
| 74 | |
|---|
| 75 | $commands = ''; |
|---|
| 76 | $count = 0; |
|---|
| 77 | |
|---|
| 78 | // Make sure our $result is legit.. |
|---|
| 79 | if (is_array($result) && $result[0] != '') |
|---|
| 80 | { |
|---|
| 81 | // Get the headers |
|---|
| 82 | $result_h = $IMAP->list_header_set($mbox, $result, 1, $_SESSION['sort_col'], $_SESSION['sort_order']); |
|---|
| 83 | $count = count($result); |
|---|
| 84 | |
|---|
| 85 | // save search results in session |
|---|
| 86 | if (!is_array($_SESSION['search'])) |
|---|
| 87 | $_SESSION['search'] = array(); |
|---|
| 88 | |
|---|
| 89 | // Make sure we got the headers |
|---|
| 90 | if ($result_h != NULL) |
|---|
| 91 | { |
|---|
| 92 | $_SESSION['search'][$search_request] = $IMAP->get_search_set(); |
|---|
| 93 | $commands = rcmail_js_message_list($result_h); |
|---|
| 94 | $commands .= show_message('searchsuccessful', 'confirmation', array('nr' => $count)); |
|---|
| 95 | } |
|---|
| 96 | } |
|---|
| 97 | else |
|---|
| 98 | { |
|---|
| 99 | $commands = show_message('searchnomatch', 'warning'); |
|---|
| 100 | $search_request = -1; |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | // update message count display |
|---|
| 104 | $pages = ceil($count/$IMAP->page_size); |
|---|
| 105 | $commands .= sprintf("\nthis.set_env('search_request', '%s')\n", $search_request); |
|---|
| 106 | $commands .= sprintf("this.set_env('messagecount', %d);\n", $count); |
|---|
| 107 | $commands .= sprintf("this.set_env('pagecount', %d);\n", $pages); |
|---|
| 108 | $commands .= sprintf("this.set_rowcount('%s');\n", rcmail_get_messagecount_text($count, 1)); |
|---|
| 109 | rcube_remote_response($commands); |
|---|
| 110 | |
|---|
| 111 | ?> |
|---|