Skip to content

Commit 6dc3004

Browse files
committed
Fixed: FS#996 - SOAP: Problems with (custom) mailfilters.
1 parent 6338aa2 commit 6dc3004

File tree

3 files changed

+233
-10
lines changed

3 files changed

+233
-10
lines changed

interface/lib/classes/remoting.inc.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ class remoting {
4040
private $session_timeout = 600;
4141

4242
private $server;
43+
public $oldDataRecord;
44+
public $dataRecord;
45+
public $id;
4346

4447
/*
4548
These variables shall stay global.
@@ -232,33 +235,39 @@ public function mail_user_filter_get($session_id, $primary_id)
232235

233236
public function mail_user_filter_add($session_id, $client_id, $params)
234237
{
238+
global $app;
235239
if (!$this->checkPerm($session_id, 'mail_user_filter_add')){
236240
$this->server->fault('permission_denied','You do not have the permissions to access this function.');
237241
return false;
238242
}
239243
$affected_rows = $this->insertQuery('../mail/form/mail_user_filter.tform.php', $client_id, $params);
244+
$app->plugin->raiseEvent('mail:mail_user_filter:on_after_insert',$this);
240245
return $affected_rows;
241246
}
242247

243248
public function mail_user_filter_update($session_id, $client_id, $primary_id, $params)
244249
{
250+
global $app;
245251
if (!$this->checkPerm($session_id, 'mail_user_filter_update'))
246252
{
247253
$this->server->fault('permission_denied','You do not have the permissions to access this function.');
248254
return false;
249255
}
250256
$affected_rows = $this->updateQuery('../mail/form/mail_user_filter.tform.php', $client_id, $primary_id, $params);
257+
$app->plugin->raiseEvent('mail:mail_user_filter:on_after_update',$this);
251258
return $affected_rows;
252259
}
253260

254261
public function mail_user_filter_delete($session_id,$domain_id)
255262
{
263+
global $app;
256264
if (!$this->checkPerm($session_id, 'mail_user_filter_delete'))
257265
{
258266
$this->server->fault('permission_denied','You do not have the permissions to access this function.');
259267
return false;
260268
}
261269
$affected_rows = $this->deleteQuery('../mail/form/mail_user_filter.tform.php',$domain_id);
270+
$app->plugin->raiseEvent('mail:mail_user_filter:on_after_delete',$this);
262271
return $affected_rows;
263272
}
264273

@@ -1945,7 +1954,9 @@ private function insertQuery($formdef_file, $client_id, $params)
19451954

19461955
}
19471956

1948-
1957+
// set a few values for compatibility with tform actions, mostly used by plugins
1958+
$this->id = $insert_id;
1959+
$this->dataRecord = $params;
19491960

19501961

19511962
return $insert_id;
@@ -1973,6 +1984,12 @@ private function updateQuery($formdef_file, $client_id, $primary_id, $params)
19731984

19741985
$old_rec = $app->remoting_lib->getDataRecord($primary_id);
19751986

1987+
// set a few values for compatibility with tform actions, mostly used by plugins
1988+
$this->oldDataRecord = $old_rec;
1989+
$this->id = $primary_id;
1990+
$this->dataRecord = $params;
1991+
1992+
19761993
$app->db->query($sql);
19771994

19781995
if($app->db->errorMessage != '') {
Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
<?php
2+
3+
/*
4+
Copyright (c) 2010, Till Brehm, projektfarm Gmbh
5+
All rights reserved.
6+
7+
Redistribution and use in source and binary forms, with or without modification,
8+
are permitted provided that the following conditions are met:
9+
10+
* Redistributions of source code must retain the above copyright notice,
11+
this list of conditions and the following disclaimer.
12+
* Redistributions in binary form must reproduce the above copyright notice,
13+
this list of conditions and the following disclaimer in the documentation
14+
and/or other materials provided with the distribution.
15+
* Neither the name of ISPConfig nor the names of its contributors
16+
may be used to endorse or promote products derived from this software without
17+
specific prior written permission.
18+
19+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22+
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23+
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24+
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
26+
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27+
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28+
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29+
*/
30+
31+
class mail_user_filter_plugin {
32+
33+
var $plugin_name = 'mail_user_filter_plugin';
34+
var $class_name = 'mail_user_filter_plugin';
35+
36+
/*
37+
This function is called when the plugin is loaded
38+
*/
39+
40+
function onLoad() {
41+
global $app;
42+
43+
/*
44+
Register for the events
45+
*/
46+
47+
$app->plugin->registerEvent('mail:mail_user_filter:on_after_insert','mail_user_filter_plugin','mail_user_filter_edit');
48+
$app->plugin->registerEvent('mail:mail_user_filter:on_after_update','mail_user_filter_plugin','mail_user_filter_edit');
49+
50+
51+
}
52+
53+
54+
/*
55+
function to create the mail filter rule and insert it into the custom rules
56+
field when a new mail filter is added or modified.
57+
*/
58+
function mail_user_filter_edit($event_name,$page_form) {
59+
global $app, $conf;
60+
61+
$mailuser = $app->db->queryOneRecord("SELECT custom_mailfilter FROM mail_user WHERE mailuser_id = ".$page_form->dataRecord["mailuser_id"]);
62+
$skip = false;
63+
$lines = explode("\n",$mailuser['custom_mailfilter']);
64+
$out = '';
65+
$found = false;
66+
67+
foreach($lines as $line) {
68+
$line = rtrim($line);
69+
if($line == '### BEGIN FILTER_ID:'.$page_form->id) {
70+
$skip = true;
71+
$found = true;
72+
}
73+
if($skip == false && $line != '') $out .= $line ."\n";
74+
if($line == '### END FILTER_ID:'.$page_form->id) {
75+
$out .= $this->mail_user_filter_get_rule($page_form);
76+
$skip = false;
77+
}
78+
}
79+
80+
// We did not found our rule, so we add it now as first rule.
81+
if($found == false) {
82+
$new_rule = $this->mail_user_filter_get_rule($page_form);
83+
$out = $new_rule . $out;
84+
}
85+
86+
$out = $app->db->quote($out);
87+
$app->db->datalogUpdate('mail_user', "custom_mailfilter = '$out'", 'mailuser_id', $page_form->dataRecord["mailuser_id"]);
88+
89+
90+
}
91+
92+
/*
93+
private function to create the mail filter rules in maildrop or sieve format.
94+
*/
95+
private function mail_user_filter_get_rule($page_form) {
96+
97+
global $app,$conf;
98+
99+
$app->uses("getconf");
100+
$mailuser_rec = $app->db->queryOneRecord("SELECT server_id FROM mail_user WHERE mailuser_id = ".intval($page_form->dataRecord["mailuser_id"]));
101+
$mail_config = $app->getconf->get_server_config(intval($mailuser_rec["server_id"]),'mail');
102+
103+
if($mail_config['mail_filter_syntax'] == 'sieve') {
104+
105+
// #######################################################
106+
// Filter in Sieve Syntax
107+
// #######################################################
108+
109+
$content = '';
110+
$content .= '### BEGIN FILTER_ID:'.$page_form->id."\n";
111+
112+
//$content .= 'require ["fileinto", "regex", "vacation"];'."\n";
113+
114+
$content .= 'if header :regex ["'.strtolower($page_form->dataRecord["source"]).'"] ["';
115+
116+
$searchterm = preg_quote($page_form->dataRecord["searchterm"]);
117+
118+
if($page_form->dataRecord["op"] == 'contains') {
119+
$content .= ".*".$searchterm;
120+
} elseif ($page_form->dataRecord["op"] == 'is') {
121+
$content .= $searchterm."$";
122+
} elseif ($page_form->dataRecord["op"] == 'begins') {
123+
$content .= " ".$searchterm."";
124+
} elseif ($page_form->dataRecord["op"] == 'ends') {
125+
$content .= ".*".$searchterm."$";
126+
}
127+
128+
$content .= '"] {'."\n";
129+
130+
if($page_form->dataRecord["action"] == 'move') {
131+
$content .= ' fileinto "'.$page_form->dataRecord["target"].'";' . "\n";
132+
} else {
133+
$content .= " discard;\n";
134+
}
135+
136+
$content .= " stop;\n}\n";
137+
138+
$content .= '### END FILTER_ID:'.$page_form->id."\n";
139+
140+
} else {
141+
142+
// #######################################################
143+
// Filter in Maildrop Syntax
144+
// #######################################################
145+
$content = '';
146+
$content .= '### BEGIN FILTER_ID:'.$page_form->id."\n";
147+
148+
$TargetNoQuotes = $page_form->dataRecord["target"];
149+
$TargetQuotes = "\"$TargetNoQuotes\"";
150+
151+
$TestChDirNoQuotes = '$DEFAULT/.'.$TargetNoQuotes;
152+
$TestChDirQuotes = "\"$TestChDirNoQuotes\"";
153+
154+
$MailDirMakeNoQuotes = $TargetQuotes.' $DEFAULT';
155+
156+
$EchoTargetFinal = $TargetNoQuotes;
157+
158+
159+
if($page_form->dataRecord["action"] == 'move') {
160+
161+
$content .= "
162+
`test -e ".$TestChDirQuotes." && exit 1 || exit 0`
163+
if ( ".'$RETURNCODE'." != 1 )
164+
{
165+
`maildirmake -f $MailDirMakeNoQuotes`
166+
`chmod -R 0700 ".$TestChDirQuotes."`
167+
`echo \"INBOX.$EchoTargetFinal\" >> ".'$DEFAULT'."/courierimapsubscribed`
168+
}
169+
";
170+
}
171+
172+
$content .= "if (/^".$page_form->dataRecord["source"].":";
173+
174+
$searchterm = preg_quote($page_form->dataRecord["searchterm"]);
175+
176+
if($page_form->dataRecord["op"] == 'contains') {
177+
$content .= ".*".$searchterm."/:h)\n";
178+
} elseif ($page_form->dataRecord["op"] == 'is') {
179+
$content .= $searchterm."$/:h)\n";
180+
} elseif ($page_form->dataRecord["op"] == 'begins') {
181+
$content .= " ".$searchterm."/:h)\n";
182+
} elseif ($page_form->dataRecord["op"] == 'ends') {
183+
$content .= ".*".$searchterm."$/:h)\n";
184+
}
185+
186+
$content .= "{\n";
187+
$content .= "exception {\n";
188+
189+
if($page_form->dataRecord["action"] == 'move') {
190+
$content .= 'ID' . "$page_form->id" . 'EndFolder = "$DEFAULT/.' . $page_form->dataRecord['target'] . '/"' . "\n";
191+
$content .= "to ". '$ID' . "$page_form->id" . 'EndFolder' . "\n";
192+
} else {
193+
$content .= "to /dev/null\n";
194+
}
195+
196+
$content .= "}\n";
197+
$content .= "}\n";
198+
199+
//}
200+
201+
$content .= '### END FILTER_ID:'.$page_form->id."\n";
202+
203+
}
204+
205+
return $content;
206+
}
207+
208+
209+
} // end class
210+
211+
212+
213+
?>

interface/web/mail/mail_user_filter_edit.php

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,10 @@ function onSubmit() {
6868
parent::onSubmit();
6969
}
7070

71+
/*
7172
function onAfterInsert() {
7273
global $app, $conf;
7374
74-
/*
75-
$mailuser = $app->db->queryOneRecord("SELECT sys_groupid, custom_mailfilter FROM mail_user WHERE mailuser_id = ".$this->dataRecord["mailuser_id"]);
76-
$rule_content = $mailuser['custom_mailfilter']."\n".$app->db->quote($this->getRule());
77-
$rule_content = $app->db->quote($rule_content);
78-
$app->db->datalogUpdate('mail_user', "custom_mailfilter = '$rule_content'", 'mailuser_id', $this->dataRecord["mailuser_id"]);
79-
80-
// set permissions
81-
$app->db->query("UPDATE mail_user_filter SET sys_groupid = ".$mailuser['sys_groupid']." WHERE filter_id = ".$this->id);
82-
*/
8375
$this->onAfterUpdate();
8476
8577
$app->db->query("UPDATE mail_user_filter SET sys_groupid = ".$mailuser['sys_groupid']." WHERE filter_id = ".$this->id);
@@ -230,6 +222,7 @@ function getRule() {
230222
231223
return $content;
232224
}
225+
*/
233226

234227
}
235228

0 commit comments

Comments
 (0)