diff --git a/webui/assets/js/piler.js b/webui/assets/js/piler.js index eb8589b9..975bb588 100644 --- a/webui/assets/js/piler.js +++ b/webui/assets/js/piler.js @@ -651,11 +651,13 @@ let Piler = Piler.Searches.Expert = { search: $.trim($('input#_search').val()), searchtype: 'expert', + message_id: $.trim($('#message_id').val()), ref: $.trim($('#ref').val()), folders: Piler.folders, extra_folders: Piler.extra_folders } + $('#message_id').val(''); $('#ref').val(''); Piler.load_search_results(); @@ -692,6 +694,7 @@ let Piler = from : $.trim($('input#xfrom', z).val()), to : $.trim($('input#xto', z).val()), subject : $.trim($('input#xsubject', z).val()), + message_id : $.trim($('input#xmessage_id', z).val()), body : $.trim($('input#xbody', z).val()), tag : $.trim($('input#xtag', z).val()), note : $.trim($('input#xnote', z).val()), @@ -704,6 +707,7 @@ let Piler = } $('input#_search').val(''); + $('#message_id').val(''); Piler.modal('advancedSearchModal'); @@ -732,6 +736,7 @@ let Piler = Piler.log("[cancel]")//, a ); $('#_search').val(''); + $('#message_id').val(''); $('#ref').val( '' ); Piler.Searches.Expert = {}; @@ -739,6 +744,7 @@ let Piler = $('input#xfrom').val(''); $('input#xto').val(''); $('input#xsubject').val(''); + $('input#xmessage_id').val(''); $('input#xbody').val(''); $('input#xtag').val(''); $('input#xnote').val(''); diff --git a/webui/controller/search/helper.php b/webui/controller/search/helper.php index aa2bc2f4..3c80fb5c 100644 --- a/webui/controller/search/helper.php +++ b/webui/controller/search/helper.php @@ -11,6 +11,7 @@ class ControllerSearchHelper extends Controller { 'attachment_type' => '', 'tag' => '', 'note' => '', + 'message_id' => '', 'ref' => '', 'folders' => '', 'extra_folders' => '', @@ -46,7 +47,7 @@ public function index(){ if($this->request->post['searchtype'] == 'expert'){ - if(isset($this->request->post['search']) && preg_match("/(from|to|subject|body|direction|d|size|date1|date2|attachment|a|tag|note|id|raw)\:/", $this->request->post['search'])) { + if(isset($this->request->post['search']) && preg_match("/(from|to|subject|body|direction|d|size|date1|date2|attachment|a|tag|note|message-id|message_id|ref|id|raw)\:/", $this->request->post['search'])) { $this->a = $this->model_search_search->preprocess_post_expert_request($this->request->post); } else { @@ -101,6 +102,7 @@ private function fixup_post_simple_request() { if(isset($this->request->post['tag'])) { $this->a['tag'] = $this->request->post['tag']; } if(isset($this->request->post['note'])) { $this->a['note'] = $this->request->post['note']; } + if(isset($this->request->post['message_id']) && $this->request->post['message_id']) { $this->a['message_id'] = $this->request->post['message_id']; } if(isset($this->request->post['attachment_type'])) { $this->a['attachment_type'] = $this->request->post['attachment_type']; } if(isset($this->request->post['date1'])) { $this->a['date1'] = $this->request->post['date1']; } @@ -117,6 +119,7 @@ private function fixup_post_simple_request() { private function fixup_post_request() { + if(isset($this->request->post['message_id']) && $this->request->post['message_id']) { $this->a['message_id'] = $this->request->post['message_id']; } if(isset($this->request->post['ref'])) { $this->a['ref'] = $this->request->post['ref']; } if(isset($this->request->post['folders'])) { $this->a['folders'] = $this->request->post['folders']; } if(isset($this->request->post['extra_folders'])) { $this->a['extra_folders'] = $this->request->post['extra_folders']; } diff --git a/webui/controller/search/search.php b/webui/controller/search/search.php index 3b393d89..4b87162d 100644 --- a/webui/controller/search/search.php +++ b/webui/controller/search/search.php @@ -42,6 +42,7 @@ private function fixup_post_request() { if(isset($a['from'])) { $this->data['from'] = $a['from']; } if(isset($a['to'])) { $this->data['to'] = $a['to']; } if(isset($a['subject'])) { $this->data['subject'] = $a['subject']; } + if(isset($a['message_id'])) { $this->data['message_id'] = $a['message_id']; } if(isset($a['search'])) { $this->data['_search'] = $a['search']; } diff --git a/webui/model/search/search.php b/webui/model/search/search.php index c68f4663..feec1eca 100644 --- a/webui/model/search/search.php +++ b/webui/model/search/search.php @@ -37,7 +37,10 @@ public function search_messages($data = array(), $page = 0) { // TODO: check if data is cached - if(isset($data['ref']) && $data['ref']){ + if(isset($data['message_id']) && $data['message_id']){ + list ($total_hits, $ids) = $this->query_all_possible_IDs_by_message_id($data['message_id'], $page); + } + else if(isset($data['ref']) && $data['ref']){ list ($total_hits, $ids) = $this->query_all_possible_IDs_by_reference($data['ref'], $page); } else { @@ -318,6 +321,69 @@ private function query_all_possible_IDs_by_reference($reference = '', $page = 0) } + private function query_all_possible_IDs_by_message_id($message_id = '', $page = 0) { + $ids = array(); + $offset = 0; + + $message_ids = $this->get_message_id_search_variants($message_id); + + if(count($message_ids) == 0) { return array(0, $ids); } + + $session = Registry::get('session'); + + $pagelen = get_page_length(); + $offset = $page * $pagelen; + + $q = get_q_string($message_ids); + + $query = $this->db->query("SELECT id FROM " . TABLE_META . " WHERE message_id IN ($q) ORDER BY id DESC LIMIT $offset,$pagelen", $message_ids); + + foreach($query->rows as $q) { + if($this->check_your_permission_by_id($q['id'])) { + array_push($ids, $q['id']); + } + } + + if(ENABLE_FOLDER_RESTRICTIONS == 1 && count($ids) > 0) { + $query = $this->sphx->query("SELECT id, folder FROM " . SPHINX_MAIN_INDEX . " WHERE id IN (" . implode(",", $ids) . ")"); + $ids = array(); + foreach($query->rows as $q) { + if(isset($q['folder']) && in_array($q['folder'], $session->get("folders"))) { array_push($ids, $q['id']); } + } + } + + $total_found = count($ids); + + if($total_found >= $pagelen) { + $query = $this->db->query("SELECT count(*) AS num FROM " . TABLE_META . " WHERE message_id IN ($q)", $message_ids); + $total_found = $query->row['num']; + } + + return array($total_found, $ids); + } + + + public function get_message_id_search_variants($message_id = '') { + $variants = array(); + + $message_id = trim($message_id); + + if($message_id == '') { return $variants; } + + array_push($variants, $message_id); + + if($message_id[0] == '<' && substr($message_id, -1) == '>') { + $unwrapped = substr($message_id, 1, strlen($message_id)-2); + if($unwrapped) { array_push($variants, $unwrapped); } + } + else { + array_push($variants, '<' . $message_id . '>'); + } + + return array_values(array_unique($variants)); + } + + public function preprocess_post_expert_request($data = array()) { $token = 'match'; $ndate = 0; @@ -331,6 +397,7 @@ public function preprocess_post_expert_request($data = array()) { 'attachment_type' => '', 'tag' => '', 'note' => '', + 'message_id' => '', 'ref' => '', 'folders' => '', 'extra_folders' => '', @@ -362,6 +429,8 @@ public function preprocess_post_expert_request($data = array()) { else if($v == 'direction:' || $v == 'd:') { $token = 'direction'; continue; } else if($v == 'attachment:' || $v == 'a:') { $token = 'match'; $a['match'][] = '@attachment_types'; continue; } + else if($v == 'message-id:' || $v == 'message_id:') { $token = 'message_id'; continue; } + else if(in_array($v, ['size:', 'date1:', 'date2:', 'tag:', 'note:', 'ref:', 'id:', 'raw:'])) { $token = substr($v, 0, strlen($v)-1); continue; } @@ -374,7 +443,7 @@ public function preprocess_post_expert_request($data = array()) { } if($token == 'match') { $a['match'][] = $v; } - else if(in_array($token, ['date1', 'date2', 'ref', 'tag', 'note', 'id', 'raw'])) { $a[$token] .= ' ' . $v; } + else if(in_array($token, ['date1', 'date2', 'message_id', 'ref', 'tag', 'note', 'id', 'raw'])) { $a[$token] .= ' ' . $v; } else if($token == 'direction') { if($v == 'inbound') { $a['direction'] = "0"; } diff --git a/webui/templates/common/layout-search.tpl b/webui/templates/common/layout-search.tpl index e4c9fae9..480f8b69 100644 --- a/webui/templates/common/layout-search.tpl +++ b/webui/templates/common/layout-search.tpl @@ -39,6 +39,7 @@ + diff --git a/webui/templates/search/modals.tpl b/webui/templates/search/modals.tpl index 17c3c321..adeef1d9 100644 --- a/webui/templates/search/modals.tpl +++ b/webui/templates/search/modals.tpl @@ -75,6 +75,11 @@