From b2fd28423b20920c35a00c242ef7b59afa3b809d Mon Sep 17 00:00:00 2001 From: Janos SUTO Date: Sat, 18 Jul 2020 15:21:50 +0200 Subject: [PATCH] Introduced sphinx strict mode variable Signed-off-by: Janos SUTO --- config.php.in | 14 ++++++++++++-- etc/sphinx.conf.in | 14 +++++++++++++- webui/controller/search/helper.php | 6 +++--- webui/model/search/search.php | 14 +++++++------- 4 files changed, 35 insertions(+), 13 deletions(-) diff --git a/config.php.in b/config.php.in index 8cae31b6..f7903b79 100644 --- a/config.php.in +++ b/config.php.in @@ -238,6 +238,7 @@ $config['SPHINX_MAIN_INDEX'] = 'main1,dailydelta1,delta1'; $config['SPHINX_ATTACHMENT_INDEX'] = 'att1'; $config['SPHINX_TAG_INDEX'] = 'tag1'; $config['SPHINX_NOTE_INDEX'] = 'note1'; +$config['SPHINX_STRICT_SCHEMA'] = 0; $config['RELOAD_COMMAND'] = 'sudo -n /etc/init.d/rc.piler reload'; $config['PILERIMPORT_IMAP_COMMAND'] = '/usr/local/bin/pilerimport -d /var/piler/imap -q -r'; @@ -517,5 +518,14 @@ if(!isset($health_smtp_servers)) { $health_smtp_servers = array( array(PILER_HOST, PILER_PORT, "piler"), array(SMARTHOST, SMARTHOST_PORT, "smarthost") ); } - -?> +if(SPHINX_STRICT_SCHEMA) { + define('FROM_TOKEN', '@sender'); + define('FROMDOMAIN_TOKEN', '@senderdomain'); + define('TO_TOKEN', '@rcpt'); + define('TODOMAIN_TOKEN', '@rcptdomain'); +} else { + define('FROM_TOKEN', '@from'); + define('FROMDOMAIN_TOKEN', '@fromdomain'); + define('TO_TOKEN', '@to'); + define('TODOMAIN_TOKEN', '@todomain'); +} diff --git a/etc/sphinx.conf.in b/etc/sphinx.conf.in index 5b088f1c..cb0a8a62 100644 --- a/etc/sphinx.conf.in +++ b/etc/sphinx.conf.in @@ -13,7 +13,19 @@ define('SPHINX_CHARSET_TABLE', "0..9, english, _, \ U+01FD->U+E6, U+1D01->U+E6, U+1D02->U+E6, U+1D2D->U+E6, U+1D46->U+E6, \ U+DF, U+E1, U+E4, U+E5, U+E6, U+E9, U+ED, U+00F3, U+F6, U+F8, U+FA, U+FC, U+0151, U+0153, U+0171\n"); -define('SELECT_FIELDS', 'id, `from`, `to`, `fromdomain`, `todomain`, `subject`, `arrived`, `sent`, `body`, `size`, `direction`, `folder`, `attachments`, `attachment_types`'); +// Sphinx 3.3.1 introduced some strict rules for fulltext search column names +// In order to comply with it you must enable SPHINX_STRICT_SCHEMA variable +// Be sure to check out http://www.mailpiler.org/wiki/current:sphinx3 for more +// NB: The SPHINX_STRICT_SCHEMA in sphinx.conf MUST BE THE SAME as in config.php (or in config-site.php) +// +define('SPHINX_STRICT_SCHEMA', 0); + + +if(SPHINX_STRICT_SCHEMA) { + define('SELECT_FIELDS', 'id, `from` as sender, `to` as rcpt, `fromdomain` as senderdomain, `todomain` as rcptdomain, `subject`, `arrived`, `sent`, `body`, `size`, `direction`, `folder`, `attachments`, `attachment_types`'); +} else { + define('SELECT_FIELDS', 'id, `from`, `to`, `fromdomain`, `todomain`, `subject`, `arrived`, `sent`, `body`, `size`, `direction`, `folder`, `attachments`, `attachment_types`'); +} ?> diff --git a/webui/controller/search/helper.php b/webui/controller/search/helper.php index 399f3233..18b78f9c 100644 --- a/webui/controller/search/helper.php +++ b/webui/controller/search/helper.php @@ -98,8 +98,8 @@ class ControllerSearchHelper extends Controller { private function fixup_post_simple_request() { $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['from']) && $this->request->post['from']) { $match .= sprintf('%s %s ', FROM_TOKEN, $this->request->post['from']); } + if(isset($this->request->post['to']) && $this->request->post['to']) { $match .= sprintf('%s %s ', TO_TOKEN, $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'] . ' '; } @@ -149,7 +149,7 @@ class ControllerSearchHelper extends Controller { } else if(strchr($v, '@')) { $prev_token_is_email = 1; - if($from == '') { $from = "@from"; } + if($from == '') { $from = FROM_TOKEN; } $from .= " $v"; } else { diff --git a/webui/model/search/search.php b/webui/model/search/search.php index d2fecc85..bca9a8d1 100644 --- a/webui/model/search/search.php +++ b/webui/model/search/search.php @@ -73,7 +73,7 @@ class ModelSearchSearch extends Model { $sd = preg_replace("/^\|/", "", $sd); - return " (@todomain $sd | @fromdomain $sd ) "; + return sprintf(" (%s %s | %s %s ) ", TODOMAIN_TOKEN, $sd, FROMDOMAIN_TOKEN, $sd); } else { return ""; } @@ -82,7 +82,7 @@ class ModelSearchSearch extends Model { if(ENABLE_FOLDER_RESTRICTIONS == 1) { return ""; } $all_your_addresses = $this->get_all_your_address(); - return " (@from $all_your_addresses | @to $all_your_addresses) "; + return sprintf(" (%s %s | %s %s) ", FROM_TOKEN, $all_your_addresses, TO_TOKEN, $all_your_addresses); } @@ -96,7 +96,7 @@ class ModelSearchSearch extends Model { $id = ""; $offset = 0; $total_sphx_hits = $num_rows = 0; - $fields = array("@(subject,body)", "@from", "@to", "@subject", "@body", "@attachment_types"); + $fields = ["@(subject,body)", FROM_TOKEN, TO_TOKEN, "@subject", "@body", "@attachment_types"]; $pagelen = get_page_length(); @@ -128,8 +128,8 @@ class ModelSearchSearch extends Model { 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['match'][$i-1] == FROM_TOKEN) { $data['match'][$i-1] = FROMDOMAIN_TOKEN; } + if($data['match'][$i-1] == TO_TOKEN) { $data['match'][$i-1] = TODOMAIN_TOKEN; } } $data['match'][$i] = $this->fix_email_address_for_sphinx($v); @@ -351,8 +351,8 @@ class ModelSearchSearch extends Model { while(list($k, $v) = each($b)) { if($v == '') { continue; } - if($v == 'from:') { $token = 'match'; $a['match'][] = '@from'; continue; } - else if($v == 'to:') { $token = 'match'; $a['match'][] = '@to'; continue; } + if($v == 'from:') { $token = 'match'; $a['match'][] = FROM_TOKEN; continue; } + else if($v == 'to:') { $token = 'match'; $a['match'][] = TO_TOKEN; continue; } else if($v == 'subject:') { $token = 'match'; $a['match'][] = '@subject'; continue; } else if($v == 'body:') { $token = 'match'; $a['match'][] = '@body'; continue; } else if($v == 'direction:' || $v == 'd:') { $token = 'direction'; continue; }