diff --git a/webui/controller/search/helper.php b/webui/controller/search/helper.php index 367a82b3..f94f74d5 100644 --- a/webui/controller/search/helper.php +++ b/webui/controller/search/helper.php @@ -4,10 +4,6 @@ class ControllerSearchHelper extends Controller { private $error = array(); private $a = array( - 'from' => '', - 'to' => '', - 'subject' => '', - 'body' => '', 'date1' => '', 'date2' => '', 'direction' => '', @@ -19,7 +15,7 @@ class ControllerSearchHelper extends Controller { 'folders' => '', 'extra_folders' => '', 'id' => '', - 'any' => '' + 'match' => array() ); @@ -49,7 +45,7 @@ class ControllerSearchHelper extends Controller { 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); } else { @@ -63,7 +59,7 @@ class ControllerSearchHelper extends Controller { else { $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() { - if(!isset($this->request->post['from'])) { $this->request->post['from'] = ''; } - 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['body'])) { $this->request->post['body'] = ''; } - if(!isset($this->request->post['tag'])) { $this->request->post['tag'] = ''; } - if(!isset($this->request->post['note'])) { $this->request->post['note'] = ''; } - 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['attachment_type'])) { $this->request->post['attachment_type'] = ''; } - if(!isset($this->request->post['date1'])) { $this->request->post['date1'] = ''; } - if(!isset($this->request->post['date2'])) { $this->request->post['date2'] = ''; } + $match = ''; + + if(isset($this->request->post['from']) && $this->request->post['from']) { $match .= "@from " . $this->request->post['from'] . ' '; } + if(isset($this->request->post['to']) && $this->request->post['to']) { $match .= "@to " . $this->request->post['to'] . ' '; } + if(isset($this->request->post['subject']) && $this->request->post['subject']) { $match .= "@subject " . $this->request->post['subject'] . ' '; } + if(isset($this->request->post['body']) && $this->request->post['body']) { $match .= "@body " . $this->request->post['body'] . ' '; } + + 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['attachment_type'])) { $this->a['attachment_type'] = $this->request->post['attachment_type']; } + + 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()) { $ndate = 0; + $from = $match = ''; + $prev_token_is_email = 0; 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)) { if($v == '') { continue; } @@ -127,40 +133,63 @@ class ControllerSearchHelper extends Controller { $this->a["date$ndate"] = $v; } else if(strchr($v, '@')) { - $this->a['from'] .= " $v"; + $prev_token_is_email = 1; + if($from == '') { $from = "@from"; } + $from .= " $v"; + } + else { + if($prev_token_is_email == 1) { + $prev_token_is_email = 0; + $from .= " $v"; + } + else { + $match .= ' ' . $v; + } } - else { $this->a['any'] .= ' ' . $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['any'] == ' ' . $this->data['text_enter_search_terms']) { $this->a['any'] = ''; } } private function preprocess_post_expert_request($data = array()) { - $token = ''; + $token = 'match'; $ndate = 0; + $match = array(); if(!isset($data['search'])) { return; } $s = preg_replace("/:/", ": ", $data['search']); $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); $b = explode(" ", $s); while(list($k, $v) = each($b)) { if($v == '') { continue; } - if($v == 'from:') { $token = 'from'; continue; } - else if($v == 'to:') { $token = 'to'; continue; } - else if($v == 'subject:') { $token = 'subject'; continue; } - else if($v == 'body:') { $token = 'body'; continue; } + if($v == 'from:') { $token = 'match'; $this->a['match'][] = '@from'; continue; } + else if($v == 'to:') { $token = 'match'; $this->a['match'][] = '@to'; continue; } + else if($v == 'subject:') { $token = 'match'; $this->a['match'][] = '@subject'; continue; } + else if($v == 'body:') { $token = 'match'; $this->a['match'][] = '@body'; continue; } else if($v == 'direction:' || $v == 'd:') { $token = 'direction'; continue; } else if($v == 'size:') { $token = 'size'; continue; } else if($v == 'date1:') { $token = 'date1'; 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 == 'tag:') { $token = 'tag'; continue; } else if($v == 'note:') { $token = 'note'; continue; } @@ -174,21 +203,9 @@ class ControllerSearchHelper extends Controller { } - if($token == 'from') { - 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; } + if($token == 'match') { $this->a['match'][] = $v; } else if($token == 'date1') { $this->a['date1'] = ' ' . $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 == 'note') { $this->a['note'] .= ' ' . $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'])); } diff --git a/webui/model/audit/audit.php b/webui/model/audit/audit.php index 32ce7073..9c190b1a 100644 --- a/webui/model/audit/audit.php +++ b/webui/model/audit/audit.php @@ -89,6 +89,9 @@ class ModelAuditAudit extends Model { if(isset($query->rows)) { foreach($query->rows as $a) { + + $a['description'] = preg_replace("/\"/", "'", $a['description']); + $results[] = array( 'id' => $a['meta_id'], 'piler_id' => isset($m[$a['meta_id']]) ? $m[$a['meta_id']] : '', diff --git a/webui/model/search/message.php b/webui/model/search/message.php index 31309a29..c232cdc7 100644 --- a/webui/model/search/message.php +++ b/webui/model/search/message.php @@ -428,6 +428,8 @@ class ModelSearchMessage extends Model { private function highlight_search_terms($s = '', $terms = '') { + $fields = array("from:", "to:", "subject:", "body:"); + $terms = preg_replace("/(\'|\")/", "", $terms); $terms = explode(" ", $terms); @@ -435,6 +437,10 @@ class ModelSearchMessage extends Model { if(count($terms) <= 0) { return $s; } while(list($k, $v) = each($terms)) { + if(in_array($v, $fields)) { continue; } + + $v = preg_replace("/\W/", "", $v); + if(strlen($v) < 3) { continue; } $s = preg_replace("/$v/i", "$v", $s); diff --git a/webui/model/search/search.php b/webui/model/search/search.php index ec01ff44..02425f1f 100644 --- a/webui/model/search/search.php +++ b/webui/model/search/search.php @@ -16,7 +16,7 @@ class ModelSearchSearch extends Model { $session = Registry::get('session'); 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)); } @@ -90,159 +90,30 @@ class ModelSearchSearch extends Model { } - private function assemble_email_address_condition($from = '', $to = '') { - $s = ''; - $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 = ''; + private function assemble_email_address_filter() { + $session = Registry::get('session'); - $session_emails = $this->fix_email_address_for_sphinx($_SESSION['emails']); - $session_domains = $this->fix_email_address_for_sphinx($_SESSION['auditdomains']); + if(Registry::get('auditor_user') == 1) { + + if(RESTRICTED_AUDITOR == 1) { + $session_domains = $this->fix_email_address_for_sphinx($session->get('auditdomains')); + + $sd = $this->fix_email_address_for_sphinx($session->get('domain')); + + foreach ($session_domains as $d) { $sd .= '|' . $d; } + + $sd = preg_replace("/^\|/", "", $sd); + + return " (@todomain $sd . | @fromdomain $sd ) "; + } + + else { return ""; } + } + + if(ENABLE_FOLDER_RESTRICTIONS == 1) { return ""; } $all_your_addresses = $this->get_all_your_address(); - - $from = preg_replace("/OR/", "", $from); - $to = preg_replace("/OR/", "", $to); - - if($from) { - $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); - - - if(RESTRICTED_AUDITOR == 1) { - $domain_restrictions = ' (@todomain ' . $sd . ' | @fromdomain ' . $sd . ')'; - } - - if($from == '' && $to == '') { return $domain_restrictions; } - - if(RESTRICTED_AUDITOR == 1) { - $domain_restrictions = " & $domain_restrictions"; - } - - if($f && $t) { return "($f & $t) $domain_restrictions"; } - 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)"; - + return " (@from $all_your_addresses | @to $all_your_addresses) "; } @@ -254,31 +125,44 @@ class ModelSearchSearch extends Model { $tag_id_list = ''; $a = ""; $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']) { - $data['body'] = $this->fixup_meta_characters($data['body']); - $data['body'] = $this->fixup_sphinx_operators($data['body']); - if($match) { $match .= " & "; } $match .= "(@body " . $data['body'] . ") "; + $i = 0; + while(list($k, $v) = each($data['match'])) { + if($v == "@attachment_types") { + list($k, $v) = each($data['match']); + $i++; + if($v == "any") { + $data['match'][$i-1] = ""; + $data['match'][$i] = ""; + $a = "attachments > 0 AND "; + } + } + + if(substr($v, 0, 7) == "http://") { $v = preg_replace("/\./", "X", $v); $data['match'][$i] = preg_replace("/http\:\/\//", "__URL__", $v); } + + if(!in_array($v, $fields) && $i > 0 && strchr($v, "@")) { + + 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"; } + } + + $data['match'][$i] = $this->fix_email_address_for_sphinx($v); + } + $i++; } - if($data['subject']) { - $data['subject'] = $this->fixup_meta_characters($data['subject']); - $data['subject'] = $this->fixup_sphinx_operators($data['subject']); - if($match) { $match .= " & "; } $match .= "(@subject " . $data['subject'] . ") "; - } + $match = implode(" ", $data['match']); - if($data['attachment_type'] && !strstr($data['attachment_type'], "any")) { if($match) { $match .= " & "; } $match .= "(@attachment_types " . preg_replace("/\,/", "|", $data['attachment_type']) . ") "; } - - - if($data['any']) { - $data['any'] = $this->fixup_meta_characters($data['any']); - $data['any'] = $this->fixup_sphinx_operators($data['any']); - $data['any'] = $this->fix_email_address_for_sphinx($data['any']); - $fields = ''; - if($match) { $match = "($match) & "; } $match .= "(@(subject,body) " . $data['any'] . ") "; + if($emailfilter) { + if(strlen($match) > 2) { $match = "( $match ) & $emailfilter"; } + else { $match = $emailfilter; } }