Marius Burkard
2016-04-20 4569cae57f127afd093794310ccd290d2d9fdf36
commit | author | age
6dc300 1 <?php
T 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 {
b1a6a5 32
6dc300 33     var $plugin_name = 'mail_user_filter_plugin';
T 34     var $class_name = 'mail_user_filter_plugin';
b1a6a5 35
6dc300 36     /*
T 37          This function is called when the plugin is loaded
38     */
b1a6a5 39
6dc300 40     function onLoad() {
T 41         global $app;
b1a6a5 42
6dc300 43         /*
T 44         Register for the events
45         */
b1a6a5 46
MC 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         $app->plugin->registerEvent('mail:mail_user_filter:on_after_delete', 'mail_user_filter_plugin', 'mail_user_filter_del');
50         $app->plugin->registerEvent('mailuser:mail_user_filter:on_after_insert', 'mail_user_filter_plugin', 'mail_user_filter_edit');
51         $app->plugin->registerEvent('mailuser:mail_user_filter:on_after_update', 'mail_user_filter_plugin', 'mail_user_filter_edit');
52         $app->plugin->registerEvent('mailuser:mail_user_filter:on_after_delete', 'mail_user_filter_plugin', 'mail_user_filter_del');
53
6dc300 54     }
b1a6a5 55
MC 56
6dc300 57     /*
b1a6a5 58         function to create the mail filter rule and insert it into the custom rules
6dc300 59         field when a new mail filter is added or modified.
T 60     */
b1a6a5 61     function mail_user_filter_edit($event_name, $page_form) {
6dc300 62         global $app, $conf;
b1a6a5 63
cc7a82 64         $mailuser = $app->db->queryOneRecord("SELECT custom_mailfilter FROM mail_user WHERE mailuser_id = ?", $page_form->dataRecord["mailuser_id"]);
6dc300 65         $skip = false;
b1a6a5 66         $lines = explode("\n", $mailuser['custom_mailfilter']);
6dc300 67         $out = '';
T 68         $found = false;
b1a6a5 69
6dc300 70         foreach($lines as $line) {
T 71             $line = rtrim($line);
72             if($line == '### BEGIN FILTER_ID:'.$page_form->id) {
73                 $skip = true;
74                 $found = true;
75             }
76             if($skip == false && $line != '') $out .= $line ."\n";
77             if($line == '### END FILTER_ID:'.$page_form->id) {
cab924 78                 if($page_form->dataRecord["active"] == 'y') $out .= $this->mail_user_filter_get_rule($page_form);
6dc300 79                 $skip = false;
T 80             }
81         }
b1a6a5 82
6dc300 83         // We did not found our rule, so we add it now as first rule.
cab924 84         if($found == false && $page_form->dataRecord["active"] == 'y') {
6dc300 85             $new_rule = $this->mail_user_filter_get_rule($page_form);
T 86             $out = $new_rule . $out;
87         }
b1a6a5 88
2af58c 89         $app->db->datalogUpdate('mail_user', array("custom_mailfilter" => $out), 'mailuser_id', $page_form->dataRecord["mailuser_id"]);
b1a6a5 90
MC 91
6dc300 92     }
b1a6a5 93
MC 94     function mail_user_filter_del($event_name, $page_form) {
8cf78b 95         global $app, $conf;
b1a6a5 96
cc7a82 97         $mailuser = $app->db->queryOneRecord("SELECT custom_mailfilter FROM mail_user WHERE mailuser_id = ?", $page_form->dataRecord["mailuser_id"]);
8cf78b 98         $skip = false;
b1a6a5 99         $lines = explode("\n", $mailuser['custom_mailfilter']);
8cf78b 100         $out = '';
b1a6a5 101
8cf78b 102         foreach($lines as $line) {
T 103             $line = trim($line);
104             if($line == '### BEGIN FILTER_ID:'.$page_form->id) {
105                 $skip = true;
106             }
107             if($skip == false && $line != '') $out .= $line ."\n";
108             if($line == '### END FILTER_ID:'.$page_form->id) {
109                 $skip = false;
110             }
111         }
b1a6a5 112
2af58c 113         $app->db->datalogUpdate('mail_user', array("custom_mailfilter" => $out), 'mailuser_id', $page_form->dataRecord["mailuser_id"]);
8cf78b 114     }
b1a6a5 115
MC 116
6dc300 117     /*
T 118         private function to create the mail filter rules in maildrop or sieve format.
119     */
120     private function mail_user_filter_get_rule($page_form) {
b1a6a5 121
MC 122         global $app, $conf;
123
6dc300 124         $app->uses("getconf");
cc7a82 125         $mailuser_rec = $app->db->queryOneRecord("SELECT server_id FROM mail_user WHERE mailuser_id = ?", $page_form->dataRecord["mailuser_id"]);
b1a6a5 126         $mail_config = $app->getconf->get_server_config($app->functions->intval($mailuser_rec["server_id"]), 'mail');
MC 127
6dc300 128         if($mail_config['mail_filter_syntax'] == 'sieve') {
b1a6a5 129
6dc300 130             // #######################################################
T 131             // Filter in Sieve Syntax
132             // #######################################################
b1a6a5 133
6dc300 134             $content = '';
T 135             $content .= '### BEGIN FILTER_ID:'.$page_form->id."\n";
b1a6a5 136
6dc300 137             //$content .= 'require ["fileinto", "regex", "vacation"];'."\n";
d907c0 138             
TB 139             if($page_form->dataRecord["op"] == 'domain') {
140                 $content .= 'if address :domain :is "'.strtolower($page_form->dataRecord["source"]).'" "'.$page_form->dataRecord["searchterm"].'" {'."\n";
141             } elseif ($page_form->dataRecord["op"] == 'localpart') {
142                 $content .= 'if address :localpart :is "'.strtolower($page_form->dataRecord["source"]).'" "'.$page_form->dataRecord["searchterm"].'" {'."\n";
143             } elseif ($page_form->dataRecord["source"] == 'Size') {
144                 if(substr(trim($page_form->dataRecord["searchterm"]),-1) == 'k' || substr(trim($page_form->dataRecord["searchterm"]),-1) == 'K') {
145                     $unit = 'k';
146                 } else {
147                     $unit = 'm';
148                 }
149                 $content .= 'if size :over '.intval($page_form->dataRecord["searchterm"]).$unit.' {'."\n";
150             } else {
151             
152                 if($page_form->dataRecord["source"] == 'Header') {
153                     $parts = explode(':',trim($page_form->dataRecord["searchterm"]));
154                     $page_form->dataRecord["source"] = trim($parts[0]);
155                     unset($parts[0]);
156                     $page_form->dataRecord["searchterm"] = trim(implode(':',$parts));
157                     unset($parts);
158                 }
b1a6a5 159
d907c0 160                 $content .= 'if header :regex    ["'.strtolower($page_form->dataRecord["source"]).'"] ["';
b1a6a5 161
d907c0 162                 $searchterm = preg_quote($page_form->dataRecord["searchterm"]);
TB 163                 $searchterm = str_replace(
164                     array(
165                         '"',
166                         '\\[',
167                         '\\]'
168                     ),
169                     array(
170                         '\\"',
171                         '\\\\[',
172                         '\\\\]'
173                     ), $searchterm);
b1a6a5 174
d907c0 175                 if($page_form->dataRecord["op"] == 'contains') {
TB 176                     $content .= ".*".$searchterm;
177                 } elseif ($page_form->dataRecord["op"] == 'is') {
178                     $content .= "^".$searchterm."$";
179                 } elseif ($page_form->dataRecord["op"] == 'begins') {
9dfbec 180                     $content .= "^".$searchterm."";
d907c0 181                 } elseif ($page_form->dataRecord["op"] == 'ends') {
TB 182                     $content .= ".*".$searchterm."$";
183                 }
184
185                 $content .= '"] {'."\n";
6dc300 186             }
b1a6a5 187
6dc300 188             if($page_form->dataRecord["action"] == 'move') {
d907c0 189                 $content .= '    fileinto "'.$page_form->dataRecord["target"].'";' . "\n    stop;\n";
TB 190             } elseif ($page_form->dataRecord["action"] == 'keep') {
191                 $content .= "    keep;\n";
192             } elseif ($page_form->dataRecord["action"] == 'stop') {
193                 $content .= "    stop;\n";
194             } elseif ($page_form->dataRecord["action"] == 'reject') {
195                 $content .= '    reject "'.$page_form->dataRecord["target"].'";    stop;\n\n';
6dc300 196             } else {
d907c0 197                 $content .= "    discard;\n    stop;\n";
6dc300 198             }
b1a6a5 199
d907c0 200             $content .= "}\n";
b1a6a5 201
6dc300 202             $content .= '### END FILTER_ID:'.$page_form->id."\n";
b1a6a5 203
6dc300 204         } else {
b1a6a5 205
6dc300 206             // #######################################################
T 207             // Filter in Maildrop Syntax
208             // #######################################################
209             $content = '';
210             $content .= '### BEGIN FILTER_ID:'.$page_form->id."\n";
211
212             $TargetNoQuotes = $page_form->dataRecord["target"];
213             $TargetQuotes = "\"$TargetNoQuotes\"";
214
215             $TestChDirNoQuotes = '$DEFAULT/.'.$TargetNoQuotes;
216             $TestChDirQuotes = "\"$TestChDirNoQuotes\"";
217
218             $MailDirMakeNoQuotes = $TargetQuotes.' $DEFAULT';
219
220             $EchoTargetFinal = $TargetNoQuotes;
221
222
223             if($page_form->dataRecord["action"] == 'move') {
224
b1a6a5 225                 $content .= "
6dc300 226 `test -e ".$TestChDirQuotes." && exit 1 || exit 0`
T 227 if ( ".'$RETURNCODE'." != 1 )
228 {
229     `maildirmake -f $MailDirMakeNoQuotes`
230     `chmod -R 0700 ".$TestChDirQuotes."`
231     `echo \"INBOX.$EchoTargetFinal\" >> ".'$DEFAULT'."/courierimapsubscribed`
232 }
233 ";
234             }
235
1fd3c0 236             $content .= "if (/^".$page_form->dataRecord["source"].": ";
6dc300 237
T 238             $searchterm = preg_quote($page_form->dataRecord["searchterm"]);
239
240             if($page_form->dataRecord["op"] == 'contains') {
241                 $content .= ".*".$searchterm."/:h)\n";
242             } elseif ($page_form->dataRecord["op"] == 'is') {
243                 $content .= $searchterm."$/:h)\n";
244             } elseif ($page_form->dataRecord["op"] == 'begins') {
1fd3c0 245                 $content .= $searchterm."/:h)\n";
6dc300 246             } elseif ($page_form->dataRecord["op"] == 'ends') {
T 247                 $content .= ".*".$searchterm."$/:h)\n";
248             }
249
250             $content .= "{\n";
251             $content .= "exception {\n";
252
253             if($page_form->dataRecord["action"] == 'move') {
254                 $content .= 'ID' . "$page_form->id" . 'EndFolder = "$DEFAULT/.' . $page_form->dataRecord['target'] . '/"' . "\n";
dee589 255                 $content .= "xfilter \"/usr/bin/formail -A \\\"X-User-Mail-Filter-ID"."$page_form->id".": Yes\\\"\"" . "\n";
6dc300 256                 $content .= "to ". '$ID' . "$page_form->id" . 'EndFolder' . "\n";
T 257             } else {
258                 $content .= "to /dev/null\n";
259             }
260
261             $content .= "}\n";
262             $content .= "}\n";
b1a6a5 263
6dc300 264             //}
b1a6a5 265
6dc300 266             $content .= '### END FILTER_ID:'.$page_form->id."\n";
b1a6a5 267
6dc300 268         }
b1a6a5 269
6dc300 270         return $content;
T 271     }
b1a6a5 272
6dc300 273
T 274 } // end class
275
276
277
b1a6a5 278 ?>