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+ ?>
0 commit comments