improved search expression highlighting

This commit is contained in:
SJ 2014-06-28 11:19:04 +02:00
parent a6ff3bf098
commit f0665296fe

View File

@ -340,10 +340,12 @@ class ModelSearchMessage extends Model {
if($is_header == 1 && preg_match("/^Content-Transfer-Encoding:/i", $l)) $state = "CONTENT_TRANSFER_ENCODING"; if($is_header == 1 && preg_match("/^Content-Transfer-Encoding:/i", $l)) $state = "CONTENT_TRANSFER_ENCODING";
if($state == "CONTENT_TYPE"){ if($state == "CONTENT_TYPE"){
$x = stristr($l, "boundary"); $x = stristr($l, "boundary");
if($x){ if($x){
$x = preg_replace("/boundary\s{0,}=/i", "boundary=", $x);
$x = preg_replace("/boundary= /i", "boundary=", $x); $x = preg_replace("/boundary\s{0,}=\s{0,}/i", "boundary=", $x);
//$x = preg_replace("/boundary= /i", "boundary=", $x);
$x = preg_replace("/\"\;{0,1}/", "", $x); $x = preg_replace("/\"\;{0,1}/", "", $x);
$x = preg_replace("/\'/", "", $x); $x = preg_replace("/\'/", "", $x);
@ -449,7 +451,7 @@ class ModelSearchMessage extends Model {
if(strlen($html_message) > 20) { if(strlen($html_message) > 20) {
$message = $this->highlight_search_terms($html_message, $terms); $message = $this->highlight_search_terms($html_message, $terms, 1);
} else { } else {
$message = $this->highlight_search_terms($text_message, $terms); $message = $this->highlight_search_terms($text_message, $terms);
} }
@ -465,22 +467,26 @@ class ModelSearchMessage extends Model {
} }
private function highlight_search_terms($s = '', $terms = '') { private function highlight_search_terms($s = '', $terms = '', $html = 0) {
$fields = array("from:", "to:", "subject:", "body:"); $fields = array("from:", "to:", "subject:", "body:");
$terms = preg_replace("/(\'|\")/", "", $terms); $terms = preg_replace("/(\'|\")/", "", $terms);
$terms = explode(" ", $terms); $a = explode(" ", $terms);
$terms = array();
while(list($k, $v) = each($a)) {
if(strlen($v) >= 3 && !in_array($v, $fields)) {
$v = preg_replace("/\W/", "", $v);
array_push($terms, $v);
}
}
if(count($terms) <= 0) { return $s; } if(count($terms) <= 0) { return $s; }
if($html == 0) {
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; }
$s = preg_replace("/$v/i", "<span class=\"message_highlight\">$v</span>", $s); $s = preg_replace("/$v/i", "<span class=\"message_highlight\">$v</span>", $s);
} }
@ -488,6 +494,35 @@ class ModelSearchMessage extends Model {
} }
$tokens = preg_split("/\</", $s);
$s = '';
while(list($k, $token) = each($tokens)) {
$pos = strpos($token, ">");
if($pos > 0) {
$len = strlen($token);
$s .= '<' . substr($token, 0, $pos) . '>';
if($len > $pos+1) {
$str = substr($token, $pos+1, $len);
reset($terms);
while(list($k, $v) = each($terms)) {
$str = preg_replace("/$v/i", "<span class=\"message_highlight\">$v</span>", $str);
}
$s .= $str;
}
}
}
return $s;
}
private function check_boundary($boundary, $line) { private function check_boundary($boundary, $line) {
for($i=0; $i<count($boundary); $i++){ for($i=0; $i<count($boundary); $i++){
@ -524,11 +559,11 @@ class ModelSearchMessage extends Model {
} }
if($text_html == 1){ if($text_html == 1){
$chunk = preg_replace("/\<style([^\>]+)\>([\w\W]+)\<\/style\>/i", "", $chunk); $chunk = preg_replace("/\<style\>([\w\W]+)\<\/style\>/i", "", $chunk);
if(ENABLE_REMOTE_IMAGES == 0) { if(ENABLE_REMOTE_IMAGES == 0) {
$chunk = preg_replace("/style([\s]{0,}=[\s]{0,})\"([^\"]+)/", "style=\"xxxx", $chunk); $chunk = preg_replace("/style([\s]{0,}=[\s]{0,})\"([^\"]+)/", "style=\"xxxx", $chunk);
$chunk = preg_replace("/style([\s]{0,}=[\s]{0,})\'([^\']+)/", "style=\'xxxx", $chunk); $chunk = preg_replace("/style([\s]{0,}=[\s]{0,})\'([^\']+)/", "style='xxxx", $chunk);
} }
$chunk = preg_replace("/\<body ([\w\s\;\"\'\#\d\:\-\=]+)\>/i", "<body>", $chunk); $chunk = preg_replace("/\<body ([\w\s\;\"\'\#\d\:\-\=]+)\>/i", "<body>", $chunk);