@@ -46,6 +46,59 @@ function onInstall() {
4646 }
4747 }
4848
49+ private function isValidEmail ($ email ) {
50+ $ atIndex = strrpos ($ email , '@ ' );
51+ if ($ atIndex === false ) {
52+ return false ;
53+ }
54+
55+ $ domain = substr ($ email , $ atIndex + 1 );
56+ $ local = substr ($ email , 0 , $ atIndex );
57+ $ localLen = strlen ($ local );
58+ $ domainLen = strlen ($ domain );
59+ if ($ localLen > 64 ) {
60+ return false ;
61+ } elseif ($ domainLen < 1 || $ domainLen > 255 ) {
62+ return false ;
63+ } elseif (substr ($ local , 0 , 1 ) == '. ' || substr ($ local , -1 , 1 ) == '. ' ) {
64+ return false ; // first or last sign is dot
65+ } elseif (strpos ($ local , '.. ' ) !== false ) {
66+ return false ; // two dots not allowed
67+ } elseif (!preg_match ('/^[A-Za-z0-9 \\- \\.]+$/ ' , $ domain )) {
68+ return false ; // invalid character
69+ } elseif (strpos ($ domain , '.. ' ) !== false ) {
70+ return false ; // two dots not allowed
71+ } elseif ($ local && !preg_match ('/^( \\\\.|[A-Za-z0-9!#%&`_= \\/$ \'*+?^{}|~.-])+$/ ' , str_replace ("\\\\" , "" , $ local ))) {
72+ // character not valid in local part unless
73+ // local part is quoted
74+ if (!preg_match ('/^"( \\\\"|[^"])+"$/ ' , str_replace ("\\\\" , "" , $ local ))) {
75+ return false ;
76+ }
77+ }
78+
79+ $ domain_array = explode ('. ' , $ domain );
80+ for ($ i = 0 ; $ i < count ($ domain_array ); $ i ++) {
81+ if (!preg_match ("/^(([A-Za-z0-9!#$%&'*+\/=?^_`{|}~-][A-Za-z0-9!#$%&'*+\/=?^_`{|}~\.-]{0,63})|( \"[^( \\| \")]{0,62} \"))$/ " , $ domain_array [$ i ])) {
82+ return false ;
83+ }
84+ }
85+
86+ if (!preg_match ("/^\[?[0-9\.]+\]?$/ " , $ domain )) {
87+ $ domain_array = explode ('. ' , $ domain );
88+ if (count ($ domain_array ) < 2 ) {
89+ return false ; // Not enough parts to domain
90+ }
91+
92+ for ($ i = 0 ; $ i < count ($ domain_array ); $ i ++) {
93+ if (!preg_match ("/^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$/ " , $ domain_array [$ i ])) {
94+ return false ;
95+ }
96+ }
97+ }
98+
99+ return true ;
100+ }
101+
49102 /*
50103 This function is called when the plugin is loaded
51104 */
@@ -190,62 +243,69 @@ function user_settings_update($event_name, $data) {
190243 $ app ->system ->mkdirpath ($ this ->users_config_dir );
191244 }
192245
193- $ app ->load ('tpl ' );
194-
195- $ tpl = new tpl ();
196- $ tpl ->newTemplate ('rspamd_users.inc.conf.master ' );
197-
198- $ tpl ->setVar ('record_identifier ' , 'ispc_ ' . $ type . '_ ' . $ entry_id );
199- $ tpl ->setVar ('priority ' , $ settings_priority );
200-
201- if ($ type === 'spamfilter_user ' ) {
202- if ($ data [$ use_data ]['local ' ] === 'Y ' ) {
246+ if (!$ this ->isValidEmail ($ app ->functions ->idn_encode ($ email_address ))) {
247+ if (is_file ($ settings_file )) {
248+ unlink ($ settings_file );
249+ }
250+ } else {
251+
252+ $ app ->load ('tpl ' );
253+
254+ $ tpl = new tpl ();
255+ $ tpl ->newTemplate ('rspamd_users.inc.conf.master ' );
256+
257+ $ tpl ->setVar ('record_identifier ' , 'ispc_ ' . $ type . '_ ' . $ entry_id );
258+ $ tpl ->setVar ('priority ' , $ settings_priority );
259+
260+ if ($ type === 'spamfilter_user ' ) {
261+ if ($ data [$ use_data ]['local ' ] === 'Y ' ) {
262+ $ tpl ->setVar ('to_email ' , $ app ->functions ->idn_encode ($ email_address ));
263+ } else {
264+ $ tpl ->setVar ('from_email ' , $ app ->functions ->idn_encode ($ email_address ));
265+ }
266+ $ spamfilter = $ data [$ use_data ];
267+ } else {
203268 $ tpl ->setVar ('to_email ' , $ app ->functions ->idn_encode ($ email_address ));
269+
270+ // need to get matching spamfilter user if any
271+ $ spamfilter = $ app ->db ->queryOneRecord ('SELECT * FROM spamfilter_users WHERE `email` = ? ' , $ email_address );
272+ }
273+
274+ if (!isset ($ policy ['rspamd_spam_tag_level ' ])) {
275+ $ policy ['rspamd_spam_tag_level ' ] = 6.0 ;
276+ }
277+ if (!isset ($ policy ['rspamd_spam_tag_method ' ])) {
278+ $ policy ['rspamd_spam_tag_method ' ] = 'add_header ' ;
279+ }
280+ if (!isset ($ policy ['rspamd_spam_kill_level ' ])) {
281+ $ policy ['rspamd_spam_kill_level ' ] = 15.0 ;
282+ }
283+ if (!isset ($ policy ['rspamd_virus_kill_level ' ])) {
284+ $ policy ['rspamd_virus_kill_level ' ] = floatval ($ policy ['rspamd_spam_kill_level ' ]) + 1000 ;
285+ }
286+
287+ $ tpl ->setVar ('rspamd_spam_tag_level ' , floatval ($ policy ['rspamd_spam_tag_level ' ]));
288+ $ tpl ->setVar ('rspamd_spam_tag_method ' , floatval ($ policy ['rspamd_spam_tag_method ' ]));
289+ $ tpl ->setVar ('rspamd_spam_kill_level ' , floatval ($ policy ['rspamd_spam_kill_level ' ]));
290+ $ tpl ->setVar ('rspamd_virus_kill_level ' , floatval ($ policy ['rspamd_spam_kill_level ' ]) + 1000 );
291+
292+ if (isset ($ policy ['spam_lover ' ]) && $ policy ['spam_lover ' ] == 'Y ' ) {
293+ $ tpl ->setVar ('spam_lover ' , true );
294+ }
295+ if (isset ($ policy ['virus_lover ' ]) && $ policy ['virus_lover ' ] == 'Y ' ) {
296+ $ tpl ->setVar ('virus_lover ' , true );
297+ }
298+
299+ $ tpl ->setVar ('greylisting ' , $ greylisting );
300+
301+ if (isset ($ policy ['rspamd_spam_greylisting_level ' ])) {
302+ $ tpl ->setVar ('greylisting_level ' , floatval ($ policy ['rspamd_spam_greylisting_level ' ]));
204303 } else {
205- $ tpl ->setVar ('from_email ' , $ app -> functions -> idn_encode ( $ email_address ) );
304+ $ tpl ->setVar ('greylisting_level ' , 0.1 );
206305 }
207- $ spamfilter = $ data [$ use_data ];
208- } else {
209- $ tpl ->setVar ('to_email ' , $ app ->functions ->idn_encode ($ email_address ));
210-
211- // need to get matching spamfilter user if any
212- $ spamfilter = $ app ->db ->queryOneRecord ('SELECT * FROM spamfilter_users WHERE `email` = ? ' , $ email_address );
213- }
214-
215- if (!isset ($ policy ['rspamd_spam_tag_level ' ])) {
216- $ policy ['rspamd_spam_tag_level ' ] = 6.0 ;
217- }
218- if (!isset ($ policy ['rspamd_spam_tag_method ' ])) {
219- $ policy ['rspamd_spam_tag_method ' ] = 'add_header ' ;
220- }
221- if (!isset ($ policy ['rspamd_spam_kill_level ' ])) {
222- $ policy ['rspamd_spam_kill_level ' ] = 15.0 ;
223- }
224- if (!isset ($ policy ['rspamd_virus_kill_level ' ])) {
225- $ policy ['rspamd_virus_kill_level ' ] = floatval ($ policy ['rspamd_spam_kill_level ' ]) + 1000 ;
226- }
227-
228- $ tpl ->setVar ('rspamd_spam_tag_level ' , floatval ($ policy ['rspamd_spam_tag_level ' ]));
229- $ tpl ->setVar ('rspamd_spam_tag_method ' , floatval ($ policy ['rspamd_spam_tag_method ' ]));
230- $ tpl ->setVar ('rspamd_spam_kill_level ' , floatval ($ policy ['rspamd_spam_kill_level ' ]));
231- $ tpl ->setVar ('rspamd_virus_kill_level ' , floatval ($ policy ['rspamd_spam_kill_level ' ]) + 1000 );
232-
233- if (isset ($ policy ['spam_lover ' ]) && $ policy ['spam_lover ' ] == 'Y ' ) {
234- $ tpl ->setVar ('spam_lover ' , true );
235- }
236- if (isset ($ policy ['virus_lover ' ]) && $ policy ['virus_lover ' ] == 'Y ' ) {
237- $ tpl ->setVar ('virus_lover ' , true );
238- }
239-
240- $ tpl ->setVar ('greylisting ' , $ greylisting );
241306
242- if (isset ($ policy ['rspamd_spam_greylisting_level ' ])) {
243- $ tpl ->setVar ('greylisting_level ' , floatval ($ policy ['rspamd_spam_greylisting_level ' ]));
244- } else {
245- $ tpl ->setVar ('greylisting_level ' , 0.1 );
307+ $ app ->system ->file_put_contents ($ settings_file , $ tpl ->grab ());
246308 }
247-
248- $ app ->system ->file_put_contents ($ settings_file , $ tpl ->grab ());
249309 }
250310
251311 if ($ mail_config ['content_filter ' ] == 'rspamd ' ){
@@ -318,20 +378,32 @@ function spamfilter_wblist_update($event_name, $data) {
318378 $ filter_rcpt = substr ($ filter_rcpt , 1 );
319379 }
320380 }
321-
322- $ tpl = new tpl ();
323- $ tpl ->newTemplate ('rspamd_wblist.inc.conf.master ' );
324- $ tpl ->setVar ('list_scope ' , ($ global_filter ? 'global ' : 'spamfilter ' ));
325- $ tpl ->setVar ('record_id ' , $ record_id );
326- // we need to add 10 to priority to avoid mailbox/domain spamfilter settings overriding white/blacklists
327- $ tpl ->setVar ('priority ' , intval ($ data ['new ' ]['priority ' ]) + ($ global_filter ? 10 : 20 ));
328- $ tpl ->setVar ('from ' , $ filter_from );
329- $ tpl ->setVar ('recipient ' , $ filter_rcpt );
330- $ tpl ->setVar ('hostname ' , $ filter ['hostname ' ]);
331- $ tpl ->setVar ('ip ' , $ filter ['ip ' ]);
332- $ tpl ->setVar ('wblist ' , $ filter ['wb ' ]);
333-
334- $ app ->system ->file_put_contents ($ wblist_file , $ tpl ->grab ());
381+
382+ if (!$ this ->isValidEmail ($ filter_from )) {
383+ $ filter_from = '' ;
384+ }
385+ if (!$ this ->isValidEmail ($ filter_rcpt )) {
386+ $ filter_rcpt = '' ;
387+ }
388+ if (($ global_filter === true && !$ filter_from && !$ filter_rcpt ) || ($ global_filter === false && (!$ filter_from || !$ filter_rcpt ))) {
389+ if (is_file ($ wblist_file )) {
390+ unlink ($ wblist_file );
391+ }
392+ } else {
393+ $ tpl = new tpl ();
394+ $ tpl ->newTemplate ('rspamd_wblist.inc.conf.master ' );
395+ $ tpl ->setVar ('list_scope ' , ($ global_filter ? 'global ' : 'spamfilter ' ));
396+ $ tpl ->setVar ('record_id ' , $ record_id );
397+ // we need to add 10 to priority to avoid mailbox/domain spamfilter settings overriding white/blacklists
398+ $ tpl ->setVar ('priority ' , intval ($ data ['new ' ]['priority ' ]) + ($ global_filter ? 10 : 20 ));
399+ $ tpl ->setVar ('from ' , $ filter_from );
400+ $ tpl ->setVar ('recipient ' , $ filter_rcpt );
401+ $ tpl ->setVar ('hostname ' , $ filter ['hostname ' ]);
402+ $ tpl ->setVar ('ip ' , $ filter ['ip ' ]);
403+ $ tpl ->setVar ('wblist ' , $ filter ['wb ' ]);
404+
405+ $ app ->system ->file_put_contents ($ wblist_file , $ tpl ->grab ());
406+ }
335407 } elseif (is_file ($ wblist_file )) {
336408 unlink ($ wblist_file );
337409 }
0 commit comments