You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

search.inc 8.3KB

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