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