@@ -71,11 +71,14 @@ function onLoad() {
7171 $ app ->plugins ->registerEvent ('server_ip_insert ' , $ this ->plugin_name , 'server_ip ' );
7272 $ app ->plugins ->registerEvent ('server_ip_update ' , $ this ->plugin_name , 'server_ip ' );
7373 $ app ->plugins ->registerEvent ('server_ip_delete ' , $ this ->plugin_name , 'server_ip ' );
74+
75+ //* global mail access filters
76+ $ app ->plugins ->registerEvent ('mail_access_insert ' , $ this ->plugin_name , 'spamfilter_wblist_insert ' );
77+ $ app ->plugins ->registerEvent ('mail_access_update ' , $ this ->plugin_name , 'spamfilter_wblist_update ' );
78+ $ app ->plugins ->registerEvent ('mail_access_delete ' , $ this ->plugin_name , 'spamfilter_wblist_delete ' );
7479 }
7580
7681 function spamfilter_users_insert ($ event_name , $ data ) {
77- global $ app , $ conf ;
78-
7982 $ this ->action = 'insert ' ;
8083 // just run the spamfilter_users_update function
8184 $ this ->spamfilter_users_update ($ event_name , $ data );
@@ -161,8 +164,6 @@ function spamfilter_users_delete($event_name, $data) {
161164 }
162165
163166 function spamfilter_wblist_insert ($ event_name , $ data ) {
164- global $ app , $ conf ;
165-
166167 $ this ->action = 'insert ' ;
167168 // just run the spamfilter_wblist_update function
168169 $ this ->spamfilter_wblist_update ($ event_name , $ data );
@@ -175,34 +176,78 @@ function spamfilter_wblist_update($event_name, $data) {
175176 $ mail_config = $ app ->getconf ->get_server_config ($ conf ['server_id ' ], 'mail ' );
176177
177178 if (is_dir ('/etc/rspamd ' )) {
178- $ recipient = $ app -> db -> queryOneRecord ( " SELECT email FROM spamfilter_users WHERE id = ? " , intval ( $ data [ ' new ' ][ ' rid ' ])) ;
179+ $ global_filter = false ;
179180 //* Create the config file
180- $ wblist_file = $ this ->users_config_dir .'spamfilter_wblist_ ' .intval ($ data ['new ' ]['wblist_id ' ]).'.conf ' ;
181+ $ filter = null ;
182+ if ($ event_name === 'mail_access_insert ' || $ event_name === 'mail_access_update ' ) {
183+ $ global_filter = true ;
184+ $ record_id = intval ($ data ['new ' ]['access_id ' ]);
185+ $ wblist_file = $ this ->users_config_dir .'global_wblist_ ' .$ record_id .'.conf ' ;
186+ $ filter = array (
187+ 'wb ' => ($ data ['new ' ]['access ' ] === 'OK ' ? 'W ' : 'B ' ),
188+ 'from ' => ($ data ['new ' ]['type ' ] === 'sender ' ? $ app ->functions ->idn_encode ($ data ['new ' ]['source ' ]) : '' ),
189+ 'rcpt ' => ($ data ['new ' ]['type ' ] === 'recipient ' ? $ app ->functions ->idn_encode ($ data ['new ' ]['source ' ]) : '' ),
190+ 'ip ' => ($ data ['new ' ]['type ' ] === 'client ' && $ this ->_is_valid_ip_address ($ data ['new ' ]['source ' ]) ? $ data ['new ' ]['source ' ] : '' ),
191+ 'hostname ' => ($ data ['new ' ]['type ' ] === 'client ' && !$ this ->_is_valid_ip_address ($ data ['new ' ]['source ' ]) ? $ data ['new ' ]['source ' ] : '' )
192+ );
193+ } else {
194+ $ record_id = intval ($ data ['new ' ]['wblist_id ' ]);
195+ $ wblist_file = $ this ->users_config_dir .'spamfilter_wblist_ ' .$ record_id .'.conf ' ;
196+ $ tmp = $ app ->db ->queryOneRecord ("SELECT email FROM spamfilter_users WHERE id = ? " , intval ($ data ['new ' ]['rid ' ]));
197+ if ($ tmp && !empty ($ tmp )) {
198+ $ filter = array (
199+ 'wb ' => $ data ['new ' ]['wb ' ],
200+ 'from ' => $ app ->functions ->idn_encode ($ data ['new ' ]['email ' ]),
201+ 'rcpt ' => $ app ->functions ->idn_encode ($ tmp ['email ' ]),
202+ 'ip ' => '' ,
203+ 'hostname ' => ''
204+ );
205+ }
206+ }
181207
182- if ($ data ['new ' ]['active ' ] == 'y ' && is_array ($ recipient ) && !empty ($ recipient )){
208+ if ($ data ['new ' ]['active ' ] == 'y ' && is_array ($ filter ) && !empty ($ filter )){
183209 if (!is_dir ($ this ->users_config_dir )){
184210 $ app ->system ->mkdirpath ($ this ->users_config_dir );
185211 }
186212
187213 $ app ->load ('tpl ' );
188214
215+ $ filter_from = $ filter ['from ' ];
216+ if ($ filter_from != '' ) {
217+ if (strpos ($ filter_from , '@ ' ) === false ) {
218+ $ filter_from = '@ ' . $ filter_from ;
219+ } elseif (substr ($ filter_from , 0 , 2 ) === '*@ ' ) {
220+ $ filter_from = substr ($ filter_from , 1 );
221+ }
222+ }
223+ $ filter_rcpt = $ filter ['rcpt ' ];
224+ if ($ filter_rcpt != '' ) {
225+ if (strpos ($ filter_rcpt , '@ ' ) === false ) {
226+ $ filter_rcpt = '@ ' . $ filter_rcpt ;
227+ } elseif (substr ($ filter_rcpt , 0 , 2 ) === '*@ ' ) {
228+ $ filter_rcpt = substr ($ filter_rcpt , 1 );
229+ }
230+ }
231+
189232 $ tpl = new tpl ();
190233 $ tpl ->newTemplate ('rspamd_wblist.inc.conf.master ' );
191- $ tpl ->setVar ('record_id ' , intval ($ data ['new ' ]['wblist_id ' ]));
234+ $ tpl ->setVar ('list_scope ' , ($ global_filter ? 'global ' : 'spamfilter ' ));
235+ $ tpl ->setVar ('record_id ' , $ record_id );
192236 // we need to add 10 to priority to avoid mailbox/domain spamfilter settings overriding white/blacklists
193- $ tpl ->setVar ('priority ' , intval ($ data ['new ' ]['priority ' ]) + 10 );
194- $ tpl ->setVar ('from ' , $ app ->functions ->idn_encode ($ data ['new ' ]['email ' ]));
195- $ tpl ->setVar ('recipient ' , $ app ->functions ->idn_encode ($ recipient ['email ' ]));
196- //$tpl->setVar('action', ($data['new']['wb'] == 'W'? 'want_spam = yes;' : 'action = "reject";'));
197- $ tpl ->setVar ('wblist ' , $ data ['new ' ]['wb ' ]);
237+ $ tpl ->setVar ('priority ' , intval ($ data ['new ' ]['priority ' ]) + ($ global_filter ? 20 : 10 ));
238+ $ tpl ->setVar ('from ' , $ filter_from );
239+ $ tpl ->setVar ('recipient ' , $ filter_rcpt );
240+ $ tpl ->setVar ('hostname ' , $ filter ['hostname ' ]);
241+ $ tpl ->setVar ('ip ' , $ filter ['ip ' ]);
242+ $ tpl ->setVar ('wblist ' , $ filter ['wb ' ]);
198243
199244 $ app ->system ->file_put_contents ($ wblist_file , $ tpl ->grab ());
200- } else {
201- if ( is_file ( $ wblist_file )) unlink ($ wblist_file );
245+ } elseif ( is_file ( $ wblist_file )) {
246+ unlink ($ wblist_file );
202247 }
203248
204- if ($ mail_config ['content_filter ' ] == 'rspamd ' ) {
205- if ( is_file ( ' /etc/init.d/rspamd ' )) $ app ->services ->restartServiceDelayed ('rspamd ' , 'reload ' );
249+ if ($ mail_config ['content_filter ' ] == 'rspamd ' && is_file ( ' /etc/init.d/rspamd ' )) {
250+ $ app ->services ->restartServiceDelayed ('rspamd ' , 'reload ' );
206251 }
207252 }
208253 }
@@ -215,9 +260,15 @@ function spamfilter_wblist_delete($event_name, $data) {
215260
216261 if (is_dir ('/etc/rspamd ' )) {
217262 //* delete the config file
218- $ wblist_file = $ this ->users_config_dir .'spamfilter_wblist_ ' .intval ($ data ['old ' ]['wblist_id ' ]).'.conf ' ;
219- if (is_file ($ wblist_file )) unlink ($ wblist_file );
220-
263+ if ($ event_name === 'mail_access_delete ' ) {
264+ $ wblist_file = $ this ->users_config_dir .'global_wblist_ ' .intval ($ data ['old ' ]['access_id ' ]).'.conf ' ;
265+ } else {
266+ $ wblist_file = $ this ->users_config_dir .'spamfilter_wblist_ ' .intval ($ data ['old ' ]['wblist_id ' ]).'.conf ' ;
267+ }
268+ if (is_file ($ wblist_file )) {
269+ unlink ($ wblist_file );
270+ }
271+
221272 if ($ mail_config ['content_filter ' ] == 'rspamd ' ){
222273 if (is_file ('/etc/init.d/rspamd ' )) $ app ->services ->restartServiceDelayed ('rspamd ' , 'reload ' );
223274 }
@@ -253,4 +304,16 @@ function server_ip($event_name, $data) {
253304 }
254305 }
255306
307+ private function _is_valid_ip_address ($ ip ) {
308+ if (function_exists ('filter_var ' )) {
309+ if (!filter_var ($ ip , FILTER_VALIDATE_IP , FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6 )) {
310+ return false ;
311+ } else {
312+ return true ;
313+ }
314+ } else {
315+ return false ;
316+ }
317+ }
318+
256319} // end class
0 commit comments