Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions webui/assets/js/piler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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()),
Expand All @@ -704,6 +707,7 @@ let Piler =
}

$('input#_search').val('');
$('#message_id').val('');

Piler.modal('advancedSearchModal');

Expand Down Expand Up @@ -732,13 +736,15 @@ let Piler =
Piler.log("[cancel]")//, a );

$('#_search').val('');
$('#message_id').val('');
$('#ref').val( '' );

Piler.Searches.Expert = {};

$('input#xfrom').val('');
$('input#xto').val('');
$('input#xsubject').val('');
$('input#xmessage_id').val('');
$('input#xbody').val('');
$('input#xtag').val('');
$('input#xnote').val('');
Expand Down
5 changes: 4 additions & 1 deletion webui/controller/search/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class ControllerSearchHelper extends Controller {
'attachment_type' => '',
'tag' => '',
'note' => '',
'message_id' => '',
'ref' => '',
'folders' => '',
'extra_folders' => '',
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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']; }
Expand All @@ -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']; }
Expand Down
1 change: 1 addition & 0 deletions webui/controller/search/search.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']; }

Expand Down
73 changes: 71 additions & 2 deletions webui/model/search/search.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand All @@ -331,6 +397,7 @@ public function preprocess_post_expert_request($data = array()) {
'attachment_type' => '',
'tag' => '',
'note' => '',
'message_id' => '',
'ref' => '',
'folders' => '',
'extra_folders' => '',
Expand Down Expand Up @@ -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;
}
Expand All @@ -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"; }
Expand Down
1 change: 1 addition & 0 deletions webui/templates/common/layout-search.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
<input type="hidden" name="sort" id="sort" value="date" />
<input type="hidden" name="order" id="order" value="0" />
<input type="hidden" name="ref" id="ref" value="" />
<input type="hidden" name="message_id" id="message_id" value="<?php if(isset($message_id)) { H($message_id); } ?>" />
<input type="hidden" name="prefix" id="prefix" value="" />
<input type="text" class="form-control" id="_search" name="_search" placeholder="<?php print $text_enter_search_terms; ?>" />
</div>
Expand Down
5 changes: 5 additions & 0 deletions webui/templates/search/modals.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@
<td><input type="text" class="form-control" name="xsubject" id="xsubject" value="<?php if(isset($subject)) { print $subject; } ?>" v-model="complex_search.subject" /></td>
</tr>

<tr>
<td>Message-ID</td>
<td><input type="text" class="form-control" name="xmessage_id" id="xmessage_id" value="<?php if(isset($message_id)) { H($message_id); } ?>" v-model="complex_search.message_id" /></td>
</tr>

<tr>
<td><?php print $text_body; ?></td>
<td><input type="text" class="form-control" name="xbody" id="xbody" value="<?php if(isset($body)) { print $body; } ?>" v-model="complex_search.body" /></td>
Expand Down
51 changes: 44 additions & 7 deletions webui/tests/EmailTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@

final class EmailTest extends TestCase {

private function getSearchModel() {
$loader = new Loader();
Registry::set('load', $loader);
$language = new Language();
Registry::set('language', $language);

$loader->load->model('search/search');
return new ModelSearchSearch();
}

public static function providerTestFixEmailAddressForSphinx() {
return [
['aaa@aaa.fu', 'aaaXaaaXfu'],
Expand All @@ -24,17 +34,44 @@ public static function providerTestFixEmailAddressForSphinx() {

#[DataProvider('providerTestFixEmailAddressForSphinx')]
public function test_get_boundary($input, $expected_result) {
$loader = new Loader();
Registry::set('load', $loader);
$language = new Language();
Registry::set('language', $language);

$loader->load->model('search/search');
$m = new ModelSearchSearch();
$m = $this->getSearchModel();

$result = $m->fix_email_address_for_sphinx($input);
$this->assertEquals($result, $expected_result);
}


public static function providerTestMessageIdSearchVariants() {
return [
['', []],
['<abc@example.com>', ['<abc@example.com>', 'abc@example.com']],
['abc@example.com', ['abc@example.com', '<abc@example.com>']]
];
}


#[DataProvider('providerTestMessageIdSearchVariants')]
public function test_message_id_search_variants($input, $expected_result) {
$m = $this->getSearchModel();

$result = $m->get_message_id_search_variants($input);
$this->assertEquals($expected_result, $result);
}


public function test_preprocess_message_id_expert_request() {
$m = $this->getSearchModel();

$result = $m->preprocess_post_expert_request([
'search' => 'message-id:<abc@example.com> from:sender@example.com',
'sort' => 'date',
'order' => 0
]);

$this->assertEquals('<abc@example.com>', trim($result['message_id']));
$this->assertContains(FROM_TOKEN, $result['match']);
$this->assertContains('sender@example.com', $result['match']);
}


}