Skip to content

Commit 04cb4d7

Browse files
author
Till Brehm
committed
Merge branch 'custom_filter_match_regex' into 'stable-3.1'
Allow regex match in sieve filter Closes #5713 See merge request ispconfig/ispconfig3!1119
2 parents b74eaee + d12a706 commit 04cb4d7

28 files changed

+74
-26
lines changed

interface/lib/plugins/mail_user_filter_plugin.inc.php

Lines changed: 45 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,13 @@ private function mail_user_filter_get_rule($page_form) {
137137
$content = '';
138138
$content .= '### BEGIN FILTER_ID:'.$page_form->id."\n";
139139

140-
//$content .= 'require ["fileinto", "regex", "vacation"];'."\n";
141-
140+
if($page_form->dataRecord["source"] == 'Header') {
141+
$parts = explode(':',trim($page_form->dataRecord["searchterm"]));
142+
$page_form->dataRecord["source"] = trim(array_shift($parts));
143+
$page_form->dataRecord["searchterm"] = trim(implode(':',$parts));
144+
unset($parts);
145+
}
146+
142147
if($page_form->dataRecord["op"] == 'domain') {
143148
$content .= 'if address :domain :is "'.strtolower($page_form->dataRecord["source"]).'" "'.$page_form->dataRecord["searchterm"].'" {'."\n";
144149
} elseif ($page_form->dataRecord["op"] == 'localpart') {
@@ -152,33 +157,50 @@ private function mail_user_filter_get_rule($page_form) {
152157
$content .= 'if size :over '.intval($page_form->dataRecord["searchterm"]).$unit.' {'."\n";
153158
} else {
154159

155-
if($page_form->dataRecord["source"] == 'Header') {
156-
$parts = explode(':',trim($page_form->dataRecord["searchterm"]));
157-
$page_form->dataRecord["source"] = trim($parts[0]);
158-
unset($parts[0]);
159-
$page_form->dataRecord["searchterm"] = trim(implode(':',$parts));
160-
unset($parts);
161-
}
160+
$content .= 'if header :regex "'.strtolower($page_form->dataRecord["source"]).'" ["';
161+
162+
# special chars in sieve regex must be escaped with double-backslash
163+
if($page_form->dataRecord["op"] == 'regex') {
164+
# if providing a regex, special chars must already be quoted as intended;
165+
# we will simply try to check for an obviously unquoted double-quote and handle that.
166+
$patterns = array( '/([^\\\\]{2})"/', '/([^\\\\])\\\\"/' );
167+
$replace = array( '${1}\\\\\\\\"', '${1}\\\\\\\\"' );
168+
$searchterm = preg_replace( $patterns, $replace, $page_form->dataRecord["searchterm"] );
169+
} else {
170+
$sieve_regex_escape = array(
171+
'\\' => '\\\\\\',
172+
'+' => '\\\\+',
173+
'*' => '\\\\*',
174+
'?' => '\\\\?',
175+
'[' => '\\\\[',
176+
'^' => '\\\\^',
177+
']' => '\\\\]',
178+
'$' => '\\\\$',
179+
'(' => '\\\\(',
180+
')' => '\\\\)',
181+
'{' => '\\\\{',
182+
'}' => '\\\\}',
183+
'|' => '\\\\|',
184+
'.' => '\\\\.',
185+
# these (from preg_quote) should not be needed
186+
#'=' => '\\\\=',
187+
#'!' => '\\\\!',
188+
#'<' => '\\\\<',
189+
#'>' => '\\\\>',
190+
#':' => '\\\\:',
191+
#'-' => '\\\\-',
192+
#'#' => '\\\\#',
193+
);
194+
$searchterm = strtr( $page_form->dataRecord["searchterm"], $sieve_regex_escape );
162195

163-
$content .= 'if header :regex ["'.strtolower($page_form->dataRecord["source"]).'"] ["';
164-
165-
$searchterm = preg_quote($page_form->dataRecord["searchterm"]);
166-
$searchterm = str_replace(
167-
array(
168-
'"',
169-
'\\[',
170-
'\\]'
171-
),
172-
array(
173-
'\\"',
174-
'\\\\[',
175-
'\\\\]'
176-
), $searchterm);
196+
}
177197

178198
if($page_form->dataRecord["op"] == 'contains') {
179199
$content .= ".*".$searchterm;
180200
} elseif ($page_form->dataRecord["op"] == 'is') {
181201
$content .= "^".$searchterm."$";
202+
} elseif ($page_form->dataRecord["op"] == 'regex') {
203+
$content .= $searchterm;
182204
} elseif ($page_form->dataRecord["op"] == 'begins') {
183205
$content .= "^".$searchterm."";
184206
} elseif ($page_form->dataRecord["op"] == 'ends') {

interface/web/mail/form/mail_user_filter.tform.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,7 @@
9494
'datatype' => 'VARCHAR',
9595
'formtype' => 'SELECT',
9696
'default' => '',
97-
//'value' => array('contains'=>'contains_txt','is' => 'Is','begins'=>'Begins with','ends'=>'Ends with')
98-
'value' => array('contains'=>'contains_txt', 'is' => 'is_txt', 'begins'=>'begins_with_txt', 'ends'=>'ends_with_txt', 'localpart' => 'localpart_txt', 'domain' => 'domain_txt')
97+
'value' => array('contains'=>'contains_txt', 'is'=>'is_txt', 'begins'=>'begins_with_txt', 'ends'=>'ends_with_txt', 'regex'=>'regex_txt', 'localpart'=>'localpart_txt', 'domain'=>'domain_txt')
9998
),
10099
'searchterm' => array (
101100
'datatype' => 'VARCHAR',

interface/web/mail/lib/lang/ar_mail_user_filter.lng

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ $wb['contains_txt'] = 'Contains';
1616
$wb['is_txt'] = 'Is';
1717
$wb['begins_with_txt'] = 'Begins with';
1818
$wb['ends_with_txt'] = 'Ends with';
19+
$wb['regex_txt'] = 'Matches Regex';
1920
$wb['delete_txt'] = 'Delete';
2021
$wb['move_stop_txt'] = 'Move to';
2122
$wb['header_txt'] = 'Header';

interface/web/mail/lib/lang/bg_mail_user_filter.lng

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ $wb['contains_txt'] = 'Описание';
1616
$wb['is_txt'] = 'е';
1717
$wb['begins_with_txt'] = 'Начало с';
1818
$wb['ends_with_txt'] = 'Край с';
19+
$wb['regex_txt'] = 'Matches Regex';
1920
$wb['delete_txt'] = 'Изтрий';
2021
$wb['move_stop_txt'] = 'Move to';
2122
$wb['header_txt'] = 'Header';

interface/web/mail/lib/lang/br_mail_user_filter.lng

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ $wb['contains_txt'] = 'Contêm';
1616
$wb['is_txt'] = 'é';
1717
$wb['begins_with_txt'] = 'Iniciando com';
1818
$wb['ends_with_txt'] = 'Terminando com';
19+
$wb['regex_txt'] = 'Matches Regex';
1920
$wb['move_stop_txt'] = 'Mover para';
2021
$wb['delete_txt'] = 'Remover';
2122
$wb['header_txt'] = 'Cabeçalho';

interface/web/mail/lib/lang/ca_mail_user_filter.lng

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ $wb['contains_txt'] = 'Contient';
1616
$wb['is_txt'] = 'Est';
1717
$wb['begins_with_txt'] = 'Commence par';
1818
$wb['ends_with_txt'] = 'Fini par';
19+
$wb['regex_txt'] = 'Matches Regex';
1920
$wb['delete_txt'] = 'Supprimer';
2021
$wb['move_stop_txt'] = 'Move to';
2122
$wb['header_txt'] = 'Header';

interface/web/mail/lib/lang/cz_mail_user_filter.lng

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ $wb['contains_txt'] = 'Obsahuje';
1616
$wb['is_txt'] = 'Je';
1717
$wb['begins_with_txt'] = 'Začíná na';
1818
$wb['ends_with_txt'] = 'Končí na';
19+
$wb['regex_txt'] = 'Matches Regex';
1920
$wb['delete_txt'] = 'Smazat';
2021
$wb['move_stop_txt'] = 'Přesunout';
2122
$wb['header_txt'] = 'Hlavička';

interface/web/mail/lib/lang/de_mail_user_filter.lng

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ $wb['contains_txt'] = 'Enthält';
1616
$wb['is_txt'] = 'Ist';
1717
$wb['begins_with_txt'] = 'Beginnt mit';
1818
$wb['ends_with_txt'] = 'Endet mit';
19+
$wb['regex_txt'] = 'Matches Regex';
1920
$wb['move_stop_txt'] = 'Verschieben nach';
2021
$wb['delete_txt'] = 'Löschen';
2122
$wb['header_txt'] = 'Header';

interface/web/mail/lib/lang/dk_mail_user_filter.lng

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ $wb['contains_txt'] = 'Indeholder';
1616
$wb['is_txt'] = 'Er';
1717
$wb['begins_with_txt'] = 'Begynder med';
1818
$wb['ends_with_txt'] = 'Slutter med';
19+
$wb['regex_txt'] = 'Matches Regex';
1920
$wb['move_stop_txt'] = 'Flyt til';
2021
$wb['delete_txt'] = 'Slet';
2122
$wb['header_txt'] = 'Hoved';

interface/web/mail/lib/lang/el_mail_user_filter.lng

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ $wb['contains_txt'] = 'Περιλαμβάνει';
1616
$wb['is_txt'] = 'είναι';
1717
$wb['begins_with_txt'] = 'Ξεκινά με';
1818
$wb['ends_with_txt'] = 'Τελειώνει σε';
19+
$wb['regex_txt'] = 'Matches Regex';
1920
$wb['delete_txt'] = 'Διαγραφή';
2021
$wb['move_stop_txt'] = 'Move to';
2122
$wb['header_txt'] = 'Header';

0 commit comments

Comments
 (0)