gui refactoring to enable more complex search queries

This commit is contained in:
SJ 2013-10-05 11:34:06 +02:00
parent 29f4b60a9c
commit f08269603f
4 changed files with 121 additions and 214 deletions

View File

@ -4,10 +4,6 @@
class ControllerSearchHelper extends Controller { class ControllerSearchHelper extends Controller {
private $error = array(); private $error = array();
private $a = array( private $a = array(
'from' => '',
'to' => '',
'subject' => '',
'body' => '',
'date1' => '', 'date1' => '',
'date2' => '', 'date2' => '',
'direction' => '', 'direction' => '',
@ -19,7 +15,7 @@ class ControllerSearchHelper extends Controller {
'folders' => '', 'folders' => '',
'extra_folders' => '', 'extra_folders' => '',
'id' => '', 'id' => '',
'any' => '' 'match' => array()
); );
@ -49,7 +45,7 @@ class ControllerSearchHelper extends Controller {
if($this->request->post['searchtype'] == 'expert'){ if($this->request->post['searchtype'] == 'expert'){
if(isset($this->request->post['search']) && preg_match("/(from|to|subject|body|direction|size|date1|date2|attachment|tag|note|id)\:/", $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|id)\:/", $this->request->post['search'])) {
$this->preprocess_post_expert_request($this->request->post); $this->preprocess_post_expert_request($this->request->post);
} }
else { else {
@ -63,7 +59,7 @@ class ControllerSearchHelper extends Controller {
else { else {
$this->fixup_post_simple_request(); $this->fixup_post_simple_request();
list ($this->data['n'], $this->data['all_ids'], $this->data['messages']) = $this->model_search_search->search_messages($this->request->post, $this->data['page']); list ($this->data['n'], $this->data['all_ids'], $this->data['messages']) = $this->model_search_search->search_messages($this->a, $this->data['page']);
} }
@ -88,17 +84,24 @@ class ControllerSearchHelper extends Controller {
private function fixup_post_simple_request() { private function fixup_post_simple_request() {
if(!isset($this->request->post['from'])) { $this->request->post['from'] = ''; } $match = '';
if(!isset($this->request->post['to'])) { $this->request->post['to'] = ''; }
if(!isset($this->request->post['subject'])) { $this->request->post['subject'] = ''; } if(isset($this->request->post['from']) && $this->request->post['from']) { $match .= "@from " . $this->request->post['from'] . ' '; }
if(!isset($this->request->post['body'])) { $this->request->post['body'] = ''; } if(isset($this->request->post['to']) && $this->request->post['to']) { $match .= "@to " . $this->request->post['to'] . ' '; }
if(!isset($this->request->post['tag'])) { $this->request->post['tag'] = ''; } if(isset($this->request->post['subject']) && $this->request->post['subject']) { $match .= "@subject " . $this->request->post['subject'] . ' '; }
if(!isset($this->request->post['note'])) { $this->request->post['note'] = ''; } if(isset($this->request->post['body']) && $this->request->post['body']) { $match .= "@body " . $this->request->post['body'] . ' '; }
if(!isset($this->request->post['any'])) { $this->request->post['any'] = ''; }
if(!isset($this->request->post['id'])) { $this->request->post['id'] = ''; } if(isset($this->request->post['tag'])) { $this->a['tag'] = $this->request->post['tag']; }
if(!isset($this->request->post['attachment_type'])) { $this->request->post['attachment_type'] = ''; } if(isset($this->request->post['note'])) { $this->a['note'] = $this->request->post['note']; }
if(!isset($this->request->post['date1'])) { $this->request->post['date1'] = ''; } if(isset($this->request->post['attachment_type'])) { $this->a['attachment_type'] = $this->request->post['attachment_type']; }
if(!isset($this->request->post['date2'])) { $this->request->post['date2'] = ''; }
if(isset($this->request->post['date1'])) { $this->a['date1'] = $this->request->post['date1']; }
if(isset($this->request->post['date2'])) { $this->a['date2'] = $this->request->post['date2']; }
if($this->a['attachment_type'] && $this->a['attachment_type'] != "any") { $match .= " @attachment_types " . preg_replace("/,/", " OR ", $this->a['attachment_type']); }
$match = preg_replace("/OR/", "|", $match);
$this->a['match'] = preg_split("/ /", $match);
} }
@ -114,10 +117,13 @@ class ControllerSearchHelper extends Controller {
private function naive_preprocess_post_expert_request($data = array()) { private function naive_preprocess_post_expert_request($data = array()) {
$ndate = 0; $ndate = 0;
$from = $match = '';
$prev_token_is_email = 0;
if(!isset($data['search'])) { return; } if(!isset($data['search'])) { return; }
$b = preg_split("/\s/", $data['search']); $s = preg_replace("/OR/", "|", $data['search']);
$b = preg_split("/\s/", $s);
while(list($k, $v) = each($b)) { while(list($k, $v) = each($b)) {
if($v == '') { continue; } if($v == '') { continue; }
@ -127,40 +133,63 @@ class ControllerSearchHelper extends Controller {
$this->a["date$ndate"] = $v; $this->a["date$ndate"] = $v;
} }
else if(strchr($v, '@')) { else if(strchr($v, '@')) {
$this->a['from'] .= " $v"; $prev_token_is_email = 1;
if($from == '') { $from = "@from"; }
$from .= " $v";
} }
else { $this->a['any'] .= ' ' . $v; } else {
if($prev_token_is_email == 1) {
$prev_token_is_email = 0;
$from .= " $v";
} }
else {
$match .= ' ' . $v;
}
}
}
if($match && $match != ' ' . $this->data['text_enter_search_terms']) {
$match = "@(subject,body) $match";
}
if($from) { $match = $from . ' ' . $match; }
$this->a['match'] = preg_split("/ /", $match);
if($this->a['date1'] && $this->a['date2'] == '') { $this->a['date2'] = $this->a['date1']; } if($this->a['date1'] && $this->a['date2'] == '') { $this->a['date2'] = $this->a['date1']; }
if($this->a['any'] == ' ' . $this->data['text_enter_search_terms']) { $this->a['any'] = ''; }
} }
private function preprocess_post_expert_request($data = array()) { private function preprocess_post_expert_request($data = array()) {
$token = ''; $token = 'match';
$ndate = 0; $ndate = 0;
$match = array();
if(!isset($data['search'])) { return; } if(!isset($data['search'])) { return; }
$s = preg_replace("/:/", ": ", $data['search']); $s = preg_replace("/:/", ": ", $data['search']);
$s = preg_replace("/,/", " ", $s); $s = preg_replace("/,/", " ", $s);
$s = preg_replace("/\(/", "( ", $s);
$s = preg_replace("/\)/", ") ", $s);
$s = preg_replace("/OR/", "|", $s);
$s = preg_replace("/AND/", "", $s);
$s = preg_replace("/\s{1,}/", " ", $s); $s = preg_replace("/\s{1,}/", " ", $s);
$b = explode(" ", $s); $b = explode(" ", $s);
while(list($k, $v) = each($b)) { while(list($k, $v) = each($b)) {
if($v == '') { continue; } if($v == '') { continue; }
if($v == 'from:') { $token = 'from'; continue; } if($v == 'from:') { $token = 'match'; $this->a['match'][] = '@from'; continue; }
else if($v == 'to:') { $token = 'to'; continue; } else if($v == 'to:') { $token = 'match'; $this->a['match'][] = '@to'; continue; }
else if($v == 'subject:') { $token = 'subject'; continue; } else if($v == 'subject:') { $token = 'match'; $this->a['match'][] = '@subject'; continue; }
else if($v == 'body:') { $token = 'body'; continue; } else if($v == 'body:') { $token = 'match'; $this->a['match'][] = '@body'; continue; }
else if($v == 'direction:' || $v == 'd:') { $token = 'direction'; continue; } else if($v == 'direction:' || $v == 'd:') { $token = 'direction'; continue; }
else if($v == 'size:') { $token = 'size'; continue; } else if($v == 'size:') { $token = 'size'; continue; }
else if($v == 'date1:') { $token = 'date1'; continue; } else if($v == 'date1:') { $token = 'date1'; continue; }
else if($v == 'date2:') { $token = 'date2'; continue; } else if($v == 'date2:') { $token = 'date2'; continue; }
else if($v == 'attachment:' || $v == 'a:') { $token = 'attachment_type'; continue; } else if($v == 'attachment:' || $v == 'a:') { $token = 'match'; $this->a['match'][] = '@attachment_types'; continue; }
else if($v == 'size') { $token = 'size'; continue; } else if($v == 'size') { $token = 'size'; continue; }
else if($v == 'tag:') { $token = 'tag'; continue; } else if($v == 'tag:') { $token = 'tag'; continue; }
else if($v == 'note:') { $token = 'note'; continue; } else if($v == 'note:') { $token = 'note'; continue; }
@ -174,21 +203,9 @@ class ControllerSearchHelper extends Controller {
} }
if($token == 'from') { if($token == 'match') { $this->a['match'][] = $v; }
if($v == 'OR') { continue; }
$this->a['from'] .= " $v";
}
else if($token == 'to') {
if($v == 'OR') { continue; }
$this->a['to'] .= " $v";
}
else if($token == 'subject') { $this->a['subject'] .= ' ' . $v; }
else if($token == 'body') { $this->a['body'] .= ' ' . $v; }
else if($token == 'date1') { $this->a['date1'] = ' ' . $v; } else if($token == 'date1') { $this->a['date1'] = ' ' . $v; }
else if($token == 'date2') { $this->a['date2'] = ' ' . $v; } else if($token == 'date2') { $this->a['date2'] = ' ' . $v; }
else if($token == 'attachment_type') { $this->a['attachment_type'] .= '|' . $v; }
else if($token == 'tag') { $this->a['tag'] .= ' ' . $v; } else if($token == 'tag') { $this->a['tag'] .= ' ' . $v; }
else if($token == 'note') { $this->a['note'] .= ' ' . $v; } else if($token == 'note') { $this->a['note'] .= ' ' . $v; }
else if($token == 'ref') { $this->a['ref'] = ' ' . $v; } else if($token == 'ref') { $this->a['ref'] = ' ' . $v; }
@ -223,9 +240,6 @@ class ControllerSearchHelper extends Controller {
} }
if($this->a['any'] == ' ' . $this->data['text_enter_search_terms']) { $this->a['any'] = ''; }
$this->a['attachment_type'] = substr($this->a['attachment_type'], 1, strlen($this->a['attachment_type']));
} }

View File

@ -89,6 +89,9 @@ class ModelAuditAudit extends Model {
if(isset($query->rows)) { if(isset($query->rows)) {
foreach($query->rows as $a) { foreach($query->rows as $a) {
$a['description'] = preg_replace("/\"/", "'", $a['description']);
$results[] = array( $results[] = array(
'id' => $a['meta_id'], 'id' => $a['meta_id'],
'piler_id' => isset($m[$a['meta_id']]) ? $m[$a['meta_id']] : '', 'piler_id' => isset($m[$a['meta_id']]) ? $m[$a['meta_id']] : '',

View File

@ -428,6 +428,8 @@ class ModelSearchMessage extends Model {
private function highlight_search_terms($s = '', $terms = '') { private function highlight_search_terms($s = '', $terms = '') {
$fields = array("from:", "to:", "subject:", "body:");
$terms = preg_replace("/(\'|\")/", "", $terms); $terms = preg_replace("/(\'|\")/", "", $terms);
$terms = explode(" ", $terms); $terms = explode(" ", $terms);
@ -435,6 +437,10 @@ class ModelSearchMessage extends Model {
if(count($terms) <= 0) { return $s; } if(count($terms) <= 0) { return $s; }
while(list($k, $v) = each($terms)) { while(list($k, $v) = each($terms)) {
if(in_array($v, $fields)) { continue; }
$v = preg_replace("/\W/", "", $v);
if(strlen($v) < 3) { continue; } if(strlen($v) < 3) { continue; }
$s = preg_replace("/$v/i", "<span class=\"message_highlight\">$v</span>", $s); $s = preg_replace("/$v/i", "<span class=\"message_highlight\">$v</span>", $s);

View File

@ -16,7 +16,7 @@ class ModelSearchSearch extends Model {
$session = Registry::get('session'); $session = Registry::get('session');
while(list($k,$v) = each($data)) { while(list($k,$v) = each($data)) {
if($v) { $s .= '&' . $k . '=' . $v; } if($v) { if(is_array($v)) { $v = implode(" ", $v); } $s .= '&' . $k . '=' . $v; }
} }
if($s) { $s = substr($s, 1, strlen($s)); } if($s) { $s = substr($s, 1, strlen($s)); }
@ -90,159 +90,30 @@ class ModelSearchSearch extends Model {
} }
private function assemble_email_address_condition($from = '', $to = '') { private function assemble_email_address_filter() {
$s = ''; $session = Registry::get('session');
$f_from = $f_fromdomain = $f_to = $f_todomain = '';
$o_from = $o_fromdomain = $o_to = $o_todomain = '';
$f_f = $o_f = $f_t = $o_t = '';
$f = $t = $fdomain = $tdomain = '';
$session_emails = $this->fix_email_address_for_sphinx($_SESSION['emails']); if(Registry::get('auditor_user') == 1) {
$session_domains = $this->fix_email_address_for_sphinx($_SESSION['auditdomains']);
$all_your_addresses = $this->get_all_your_address(); if(RESTRICTED_AUDITOR == 1) {
$session_domains = $this->fix_email_address_for_sphinx($session->get('auditdomains'));
$from = preg_replace("/OR/", "", $from); $sd = $this->fix_email_address_for_sphinx($session->get('domain'));
$to = preg_replace("/OR/", "", $to);
if($from) { foreach ($session_domains as $d) { $sd .= '|' . $d; }
$e = preg_split("/\s/", $from);
foreach ($e as $email) {
if($email == '') { continue; }
$email = $this->fix_email_address_for_sphinx($email);
$field = 'from';
if($email[0] == 'X') {
$email = substr($email, 1, strlen($email));
$field = 'fromdomain';
$fdomain .= "|$email";
}
else {
$f .= "|$email";
}
if(in_array($email, $session_emails)) {
$a = "o_$field";
}
else {
$a = "f_$field";
}
if($$a) { $$a .= "|"; }
$$a .= "$email";
}
}
if($to) {
$e = preg_split("/\s/", $to);
foreach ($e as $email) {
if($email == '') { continue; }
$email = $this->fix_email_address_for_sphinx($email);
$field = 'to';
if($email[0] == 'X') {
$email = substr($email, 1, strlen($email));
$field = 'todomain';
$tdomain .= "|$email";
}
else {
$t .= "|$email";
}
if(in_array($email, $session_emails)) {
$a = "o_$field";
}
else {
$a = "f_$field";
}
if($$a) { $$a .= "|"; }
$$a .= "$email";
}
}
if($f) { $f = preg_replace("/^\|/", "@from ", $f); }
if($fdomain) {
$fdomain = preg_replace("/^\|/", "@fromdomain ", $fdomain);
if($f) { $f = "(($f)|($fdomain))"; }
else { $f = "($fdomain)"; }
}
if($t) { $t = preg_replace("/^\|/", "@to ", $t); }
if($tdomain) {
$tdomain = preg_replace("/^\|/", "@todomain ", $tdomain);
if($t) { $t = "(($t)|($tdomain))"; }
else { $t = "($tdomain)"; }
}
if(Registry::get('auditor_user') == 1 || ENABLE_FOLDER_RESTRICTIONS == 1) {
$domain_restrictions = '';
$sd = $this->fix_email_address_for_sphinx($_SESSION['domain']);
foreach ($session_domains as $d) {
$sd .= '|'.$d;
}
$sd = preg_replace("/^\|/", "", $sd); $sd = preg_replace("/^\|/", "", $sd);
return " (@todomain $sd . | @fromdomain $sd ) ";
if(RESTRICTED_AUDITOR == 1) {
$domain_restrictions = ' (@todomain ' . $sd . ' | @fromdomain ' . $sd . ')';
} }
if($from == '' && $to == '') { return $domain_restrictions; } else { return ""; }
if(RESTRICTED_AUDITOR == 1) {
$domain_restrictions = " & $domain_restrictions";
} }
if($f && $t) { return "($f & $t) $domain_restrictions"; } if(ENABLE_FOLDER_RESTRICTIONS == 1) { return ""; }
else if($f) { return "($f) $domain_restrictions"; }
else if($t) { return "($t) $domain_restrictions"; }
}
if($f_from) { $f_f = "@from $f_from"; }
if($f_fromdomain) { if($f_f) { $f_f = "($f_f | @fromdomain $f_fromdomain)"; } else { $f_f = "@fromdomain $f_fromdomain"; } }
if($o_from) { $o_f = "@from $o_from"; }
if($f_to) { $f_t = "@to $f_to"; }
if($f_todomain) { if($f_t) { $f_t = "($f_t | @todomain $f_todomain)"; } else { $f_t = "@todomain $f_todomain"; } }
if($o_to) { $o_t = "@to $o_to"; }
if($f_f == '' && $o_f == '' && $f_t == '' && $o_t == '') { return "(@from $all_your_addresses | @to $all_your_addresses)"; }
if($f_f == '' && $o_f == '' && $f_t == '' && $o_t ) { return "$o_t"; }
if($f_f == '' && $o_f == '' && $f_t && $o_t == '') { return "(@from $all_your_addresses & $f_t)"; }
if($f_f == '' && $o_f == '' && $f_t && $o_t ) { return "($o_t | (@from $all_your_addresses & $f_t))"; }
if($f_f == '' && $o_f && $f_t == '' && $o_t == '') { return "$o_f"; }
if($f_f == '' && $o_f && $f_t == '' && $o_t ) { return "($o_f & $o_t)"; }
if($f_f == '' && $o_f && $f_t && $o_t == '') { return "($o_f & $f_t)"; }
if($f_f == '' && $o_f && $f_t && $o_t ) { return "(($o_f & $f_t) | ($o_f & $o_t))"; }
if($f_f && $o_f == '' && $f_t == '' && $o_t == '') { return "($f_f & @to $all_your_addresses)"; }
if($f_f && $o_f == '' && $f_t == '' && $o_t ) { return "($f_f & $o_t)"; }
if($f_f && $o_f == '' && $f_t && $o_t == '') { return "@from INVALID"; }
if($f_f && $o_f == '' && $f_t && $o_t ) { return "($f_f & $o_t)"; }
if($f_f && $o_f && $f_t == '' && $o_t == '') { return "(($f_f & @to $all_your_addresses)|$o_f)"; }
if($f_f && $o_f && $f_t == '' && $o_t ) { return "(($f_f & $o_t)|($o_f & $o_t))"; }
if($f_f && $o_f && $f_t && $o_t == '') { return "($o_f & $f_t)"; }
if($f_f && $o_f && $f_t && $o_t ) { return "(($f_f & $o_t)|($o_f & $f_t))"; }
return "(@from $all_your_addresses | @to $all_your_addresses)";
$all_your_addresses = $this->get_all_your_address();
return " (@from $all_your_addresses | @to $all_your_addresses) ";
} }
@ -254,31 +125,44 @@ class ModelSearchSearch extends Model {
$tag_id_list = ''; $tag_id_list = '';
$a = ""; $a = "";
$id = ""; $id = "";
$fields = array("@(subject,body)", "@from", "@to", "@subject", "@body", "@attachment_types");
$emailfilter = $this->assemble_email_address_filter();
$match = $this->assemble_email_address_condition($data['from'], $data['to']);
if($data['body']) { $i = 0;
$data['body'] = $this->fixup_meta_characters($data['body']); while(list($k, $v) = each($data['match'])) {
$data['body'] = $this->fixup_sphinx_operators($data['body']); if($v == "@attachment_types") {
if($match) { $match .= " & "; } $match .= "(@body " . $data['body'] . ") "; list($k, $v) = each($data['match']);
$i++;
if($v == "any") {
$data['match'][$i-1] = "";
$data['match'][$i] = "";
$a = "attachments > 0 AND ";
}
} }
if($data['subject']) { if(substr($v, 0, 7) == "http://") { $v = preg_replace("/\./", "X", $v); $data['match'][$i] = preg_replace("/http\:\/\//", "__URL__", $v); }
$data['subject'] = $this->fixup_meta_characters($data['subject']);
$data['subject'] = $this->fixup_sphinx_operators($data['subject']); if(!in_array($v, $fields) && $i > 0 && strchr($v, "@")) {
if($match) { $match .= " & "; } $match .= "(@subject " . $data['subject'] . ") ";
if(substr($v, 0, 1) == "@") {
$v = substr($v, 1, strlen($v)-1);
if($data['match'][$i-1] == "@from") { $data['match'][$i-1] = "@fromdomain"; }
if($data['match'][$i-1] == "@to") { $data['match'][$i-1] = "@todomain"; }
} }
if($data['attachment_type'] && !strstr($data['attachment_type'], "any")) { if($match) { $match .= " & "; } $match .= "(@attachment_types " . preg_replace("/\,/", "|", $data['attachment_type']) . ") "; } $data['match'][$i] = $this->fix_email_address_for_sphinx($v);
}
$i++;
}
$match = implode(" ", $data['match']);
if($data['any']) { if($emailfilter) {
$data['any'] = $this->fixup_meta_characters($data['any']); if(strlen($match) > 2) { $match = "( $match ) & $emailfilter"; }
$data['any'] = $this->fixup_sphinx_operators($data['any']); else { $match = $emailfilter; }
$data['any'] = $this->fix_email_address_for_sphinx($data['any']);
$fields = '';
if($match) { $match = "($match) & "; } $match .= "(@(subject,body) " . $data['any'] . ") ";
} }