@@ -13,23 +13,24 @@ class mail_mail_domain_plugin {
1313
1414 /*
1515 This function is called when the plugin is loaded
16- */
16+ */
1717 function onLoad () {
1818 global $ app ;
19+
1920 //Register for the events
2021 $ app ->plugin ->registerEvent ('mail:mail_domain:on_after_insert ' , 'mail_mail_domain_plugin ' , 'mail_mail_domain_edit ' );
2122 $ app ->plugin ->registerEvent ('mail:mail_domain:on_after_update ' , 'mail_mail_domain_plugin ' , 'mail_mail_domain_edit ' );
2223 }
2324
2425 /*
2526 Function to create the sites_web_domain rule and insert it into the custom rules
26- */
27+ */
2728 function mail_mail_domain_edit ($ event_name , $ page_form ) {
2829 global $ app , $ conf ;
2930
3031 $ domain = $ app ->functions ->idn_encode ($ page_form ->dataRecord ['domain ' ]);
3132
32- // make sure that the record belongs to the client group and not the admin group when a dmin inserts it
33+ // make sure that the record belongs to the client group and not the admin group when admin inserts it
3334 // also make sure that the user can not delete entry created by an admin
3435 if ($ _SESSION ["s " ]["user " ]["typ " ] == 'admin ' && isset ($ page_form ->dataRecord ["client_group_id " ])) {
3536 $ client_group_id = $ app ->functions ->intval ($ page_form ->dataRecord ["client_group_id " ]);
@@ -64,6 +65,8 @@ function mail_mail_domain_edit($event_name, $page_form) {
6465 $ app ->uses ('getconf ' );
6566 $ mail_config = $ app ->getconf ->get_server_config ($ page_form ->dataRecord ["server_id " ], 'mail ' );
6667
68+ $ old_domain = $ app ->functions ->idn_encode ($ page_form ->oldDataRecord ['domain ' ]);
69+
6770 //* Update the mailboxes
6871 $ mailusers = $ app ->db ->queryAllRecords ("SELECT * FROM mail_user WHERE email like ? " , "%@ " . $ page_form ->oldDataRecord ['domain ' ]);
6972 $ sys_groupid = $ app ->functions ->intval ((isset ($ page_form ->dataRecord ['client_group_id ' ]))?$ page_form ->dataRecord ['client_group_id ' ]:$ page_form ->oldDataRecord ['sys_groupid ' ]);
@@ -76,25 +79,164 @@ function mail_mail_domain_edit($event_name, $page_form) {
7679 $ maildir = str_replace ("[domain] " , $ domain , $ mail_config ["maildir_path " ]);
7780 $ maildir = str_replace ("[localpart] " , $ mail_parts [0 ], $ maildir );
7881 $ email = $ mail_parts [0 ].'@ ' .$ domain ;
82+
83+ // update spamfilter_users and spamfilter_wblist if email change
84+ $ skip_spamfilter_users_update = false ;
85+ if ($ email != $ rec ['email ' ]) {
86+ $ tmp_olduser = $ app ->db ->queryOneRecord ("SELECT id,fullname FROM spamfilter_users WHERE email = ? " , $ rec ['email ' ]);
87+ if ($ tmp_olduser ['id ' ] > 0 ) {
88+ $ tmp_newuser = $ app ->db ->queryOneRecord ("SELECT id FROM spamfilter_users WHERE email = ? " , $ email );
89+ if ($ tmp_newuser ['id ' ] > 0 ) {
90+ // There is a spamfilter_users for both old and new email, we'll update old wblist entries
91+ $ tmp_wblist = $ app ->db ->queryAllRecords ("SELECT wblist_id FROM spamfilter_wblist WHERE rid = ? " , $ tmp_olduser ['id ' ]);
92+ foreach ($ tmp_wblist as $ tmp ) {
93+ $ update_data = array (
94+ 'rid ' => $ tmp_newuser ['id ' ],
95+ 'sys_userid ' => $ client_user_id ,
96+ 'sys_groupid ' => $ sys_groupid ,
97+ );
98+ $ app ->db ->datalogUpdate ('spamfilter_wblist ' , $ update_data , 'wblist_id ' , $ tmp ['wblist_id ' ]);
99+ }
100+
101+ // now delete old spamfilter_users entry
102+ $ app ->db ->datalogDelete ('spamfilter_users ' , 'id ' , $ tmp_olduser ['id ' ]);
103+ } else {
104+ $ update_data = array (
105+ 'email ' => $ email ,
106+ 'sys_userid ' => $ client_user_id ,
107+ 'sys_groupid ' => $ sys_groupid ,
108+ );
109+ if ($ tmp_olduser ['fullname ' ] == $ app ->functions ->idn_decode ($ rec ['email ' ])) {
110+ $ update_data ['fullname ' ] = $ app ->functions ->idn_decode ($ email );
111+ }
112+ $ app ->db ->datalogUpdate ('spamfilter_users ' , $ update_data , 'id ' , $ tmp_olduser ['id ' ]);
113+ $ skip_spamfilter_users_update = true ;
114+
115+ $ tmp_wblist = $ app ->db ->queryAllRecords ("SELECT wblist_id FROM spamfilter_wblist WHERE rid = ? " , $ tmp_olduser ['id ' ]);
116+ $ update_data = array (
117+ 'sys_userid ' => $ client_user_id ,
118+ 'sys_groupid ' => $ sys_groupid ,
119+ );
120+ foreach ($ tmp_wblist as $ tmp ) {
121+ $ app ->db ->datalogUpdate ('spamfilter_wblist ' , $ update_data , 'wblist_id ' , $ tmp ['wblist_id ' ]);
122+ }
123+ }
124+ }
125+
126+ $ tmp_user = $ app ->db ->queryOneRecord ("SELECT id FROM spamfilter_users WHERE email = ? " , $ email );
127+ if ($ tmp_user ["id " ] > 0 ) {
128+ // There is already a record that we will update
129+ if (!$ skip_spamfilter_users_update ) {
130+ $ update_data = array (
131+ 'sys_userid ' => $ client_user_id ,
132+ 'sys_groupid ' => $ sys_groupid ,
133+ );
134+ $ app ->db ->datalogUpdate ('spamfilter_users ' , $ update_data , 'id ' , $ tmp_user ['id ' ]);
135+ }
136+ } else {
137+ // We create a new record
138+ $ insert_data = array (
139+ "sys_userid " => $ client_user_id ,
140+ "sys_groupid " => $ sys_groupid ,
141+ "sys_perm_user " => 'riud ' ,
142+ "sys_perm_group " => 'riud ' ,
143+ "sys_perm_other " => '' ,
144+ "server_id " => $ page_form ->dataRecord ["server_id " ],
145+ "priority " => 5 ,
146+ "policy_id " => 0 ,
147+ "email " => $ email ,
148+ "fullname " => $ app ->functions ->idn_decode ($ email ),
149+ "local " => 'Y '
150+ );
151+ $ app ->db ->datalogInsert ('spamfilter_users ' , $ insert_data , 'id ' );
152+ }
153+ }
154+
79155 $ app ->db ->datalogUpdate ('mail_user ' , array ("maildir " => $ maildir , "email " => $ email , "sys_userid " => $ client_user_id , "sys_groupid " => $ sys_groupid ), 'mailuser_id ' , $ rec ['mailuser_id ' ]);
156+
80157 }
81158 }
82159
83160 //* Update the aliases
84- $ forwardings = $ app ->db ->queryAllRecords ("SELECT * FROM mail_forwarding WHERE source LIKE ? OR destination LIKE ? " , " %@ " . $ page_form -> oldDataRecord [ ' domain ' ], " %@ " . $ page_form -> oldDataRecord [ ' domain ' ] );
161+ $ forwardings = $ app ->db ->queryAllRecords ("SELECT * FROM mail_forwarding WHERE source like ? OR destination like ? " , ' %@ ' . $ old_domain , ' %@ ' . $ old_domain );
85162 if (is_array ($ forwardings )) {
86163 foreach ($ forwardings as $ rec ) {
87- $ destination = str_replace ($ page_form ->oldDataRecord ['domain ' ], $ domain , $ rec ['destination ' ]);
88- $ source = str_replace ($ page_form ->oldDataRecord ['domain ' ], $ domain , $ rec ['source ' ]);
164+ $ destination = str_replace ($ old_domain , $ domain , $ rec ['destination ' ]);
165+ $ source = str_replace ($ old_domain , $ domain , $ rec ['source ' ]);
166+
167+ // update spamfilter_users and spamfilter_wblist if source email changes
168+ $ skip_spamfilter_users_update = false ;
169+ if (strpos ($ rec ['source ' ],'@ ' .$ old_domain ) && $ source != $ rec ['source ' ]) {
170+ $ tmp_olduser = $ app ->db ->queryOneRecord ("SELECT id,fullname FROM spamfilter_users WHERE email = ? " , $ rec ['source ' ]);
171+ if ($ tmp_olduser ['id ' ] > 0 ) {
172+ $ tmp_newuser = $ app ->db ->queryOneRecord ("SELECT id FROM spamfilter_users WHERE email = ? " , $ source );
173+ if ($ tmp_newuser ['id ' ] > 0 ) {
174+ // There is a spamfilter_users for both old and new email, we'll update old wblist entries
175+ $ tmp_wblist = $ app ->db ->queryAllRecords ("SELECT wblist_id FROM spamfilter_wblist WHERE rid = ? " , $ tmp_olduser ['id ' ]);
176+ foreach ($ tmp_wblist as $ tmp ) {
177+ $ update_data = array (
178+ 'rid ' => $ tmp_newuser ['id ' ],
179+ 'sys_userid ' => $ client_user_id ,
180+ 'sys_groupid ' => $ sys_groupid ,
181+ );
182+ $ app ->db ->datalogUpdate ('spamfilter_wblist ' , $ update_data , 'wblist_id ' , $ tmp ['wblist_id ' ]);
183+ }
184+
185+ // now delete old spamfilter_users entry
186+ $ app ->db ->datalogDelete ('spamfilter_users ' , 'id ' , $ tmp_olduser ['id ' ]);
187+ } else {
188+ $ update_data = array (
189+ 'email ' => $ source ,
190+ 'sys_userid ' => $ client_user_id ,
191+ 'sys_groupid ' => $ sys_groupid ,
192+ );
193+ if ($ tmp_olduser ['fullname ' ] == $ app ->functions ->idn_decode ($ rec ['source ' ])) {
194+ $ update_data ['fullname ' ] = $ app ->functions ->idn_decode ($ source );
195+ }
196+ $ app ->db ->datalogUpdate ('spamfilter_users ' , $ update_data , 'id ' , $ tmp_olduser ['id ' ]);
197+ $ skip_spamfilter_users_update = true ;
198+
199+ $ tmp_wblist = $ app ->db ->queryAllRecords ("SELECT wblist_id FROM spamfilter_wblist WHERE rid = ? " , $ tmp_olduser ['id ' ]);
200+ $ update_data = array (
201+ 'sys_userid ' => $ client_user_id ,
202+ 'sys_groupid ' => $ sys_groupid ,
203+ );
204+ foreach ($ tmp_wblist as $ tmp ) {
205+ $ app ->db ->datalogUpdate ('spamfilter_wblist ' , $ update_data , 'wblist_id ' , $ tmp ['wblist_id ' ]);
206+ }
207+ }
208+ }
209+
210+
211+ $ tmp_user = $ app ->db ->queryOneRecord ("SELECT id FROM spamfilter_users WHERE email = ? " , $ source );
212+ if ($ tmp_user ["id " ] > 0 ) {
213+ // There is already a record that we will update
214+ if (!$ skip_spamfilter_users_update ) {
215+ $ update_data = array (
216+ 'sys_userid ' => $ client_user_id ,
217+ 'sys_groupid ' => $ sys_groupid ,
218+ );
219+ $ app ->db ->datalogUpdate ('spamfilter_users ' , $ update_data , 'id ' , $ tmp_user ['id ' ]);
220+ }
221+ }
222+
223+ }
224+
89225 $ app ->db ->datalogUpdate ('mail_forwarding ' , array ("source " => $ source , "destination " => $ destination , "sys_userid " => $ client_user_id , "sys_groupid " => $ sys_groupid ), 'forwarding_id ' , $ rec ['forwarding_id ' ]);
90226 }
91227 }
92228
93229 //* Update the mailinglist
94- $ mailing_lists = $ app ->db ->queryAllRecords ("SELECT mailinglist_id FROM mail_mailinglist WHERE domain = ? " , $ page_form ->oldDataRecord ['domain ' ]);
95- if (is_array ($ mailing_lists )) {
96- foreach ($ mailing_lists as $ rec ) {
97- $ app ->db ->datalogUpdate ('mail_mailinglist ' , array ("sys_userid " => $ client_user_id , "sys_groupid " => $ sys_groupid ), 'mailinglist_id ' , $ rec ['mailinglist_id ' ]);
230+ $ mailinglists = $ app ->db ->queryAllRecords ("SELECT * FROM mail_mailinglist WHERE domain = ? " , $ old_domain );
231+ if (is_array ($ mailinglists )) {
232+ foreach ($ mailinglists as $ rec ) {
233+ $ update_data = array (
234+ 'sys_userid ' => $ client_user_id ,
235+ 'sys_groupid ' => $ sys_groupid ,
236+ 'domain ' => $ domain ,
237+ 'email ' => str_replace ($ old_domain , $ domain , $ rec ['email ' ]),
238+ );
239+ $ app ->db ->datalogUpdate ('mail_mailinglist ' , $ update_data , 'mailinglist_id ' , $ rec ['mailinglist_id ' ]);
98240 }
99241 }
100242
@@ -107,25 +249,81 @@ function mail_mail_domain_edit($event_name, $page_form) {
107249 }
108250 }
109251
110- if ($ page_form ->oldDataRecord ["domain " ] != $ domain ) {
111- //* Delete the old spamfilter record
112- $ tmp = $ app ->db ->queryOneRecord ("SELECT id FROM spamfilter_users WHERE email = ? " , "@ " . $ page_form ->oldDataRecord ["domain " ]);
113- $ app ->db ->datalogDelete ('spamfilter_users ' , 'id ' , $ tmp ["id " ]);
114- unset($ tmp );
252+ // Spamfilter policy
253+ $ policy_id = $ app ->functions ->intval ($ page_form ->dataRecord ["policy " ]);
254+
255+ // If domain changes, update spamfilter_users
256+ // and fire spamfilter_wblist_update events so rspamd files are rewritten
257+ if ($ old_domain != $ domain ) {
258+ $ tmp_users = $ app ->db ->queryOneRecord ("SELECT id,fullname FROM spamfilter_users WHERE email LIKE ? " , '%@ ' . $ old_domain );
259+ if (is_array ($ tmp_users )) {
260+ foreach ($ tmp_users as $ tmp_old ) {
261+ $ tmp_new = $ app ->db ->queryOneRecord ("SELECT id,fullname FROM spamfilter_users WHERE email = ? " , '@ ' . $ domain );
262+ if ($ tmp_new ['id ' ] > 0 ) {
263+ // There is a spamfilter_users for both old and new domain, we'll update old wblist entries
264+ $ update_data = array (
265+ 'sys_userid ' => $ client_user_id ,
266+ 'sys_groupid ' => $ sys_groupid ,
267+ 'rid ' => $ tmp_new ['id ' ],
268+ );
269+ $ tmp_wblist = $ app ->db ->queryAllRecords ("SELECT wblist_id FROM spamfilter_wblist WHERE rid = ? " , $ tmp_old ['id ' ]);
270+ foreach ($ tmp_wblist as $ tmp ) {
271+ $ app ->db ->datalogUpdate ('spamfilter_wblist ' , $ update_data , 'wblist_id ' , $ tmp ['wblist_id ' ]);
272+ }
273+
274+ // now delete old spamfilter_users entry
275+ $ app ->db ->datalogDelete ('spamfilter_users ' , 'id ' , $ tmp_old ['id ' ]);
276+
277+ /// and update the new
278+ $ update_data = array (
279+ 'sys_userid ' => $ client_user_id ,
280+ 'sys_groupid ' => $ sys_groupid ,
281+ );
282+ $ app ->db ->datalogUpdate ('spamfilter_users ' , $ update_data , 'id ' , $ tmp_old ['id ' ]);
283+ } else {
284+ $ update_data = array (
285+ 'sys_userid ' => $ client_user_id ,
286+ 'sys_groupid ' => $ sys_groupid ,
287+ 'email ' => '@ ' . $ domain ,
288+ );
289+ if ($ tmp_old ['fullname ' ] == '@ ' . $ old_domain ) {
290+ $ update_data ['fullname ' ] = '@ ' . $ domain ;
291+ }
292+ $ app ->db ->datalogUpdate ('spamfilter_users ' , $ update_data , 'id ' , $ tmp_old ['id ' ]);
293+ }
294+ }
295+ }
115296 }
116- $ app ->db ->query ("UPDATE spamfilter_users SET email=REPLACE(email, ?, ?), sys_userid = ?, sys_groupid = ? WHERE email LIKE ? " , $ page_form ->oldDataRecord ['domain ' ], $ domain , $ client_user_id , $ sys_groupid , "%@ " . $ page_form ->oldDataRecord ['domain ' ]);
117297
118- } // end if domain name changed
119-
120- //* Force-update the aliases (required for spamfilter changes)
121- $ forwardings = $ app ->db ->queryAllRecords ("SELECT * FROM mail_forwarding WHERE source LIKE ? OR destination LIKE ? " , "%@ " . $ domain , "%@ " . $ domain );
122-
123- if (is_array ($ forwardings )) {
124- foreach ($ forwardings as $ rec ) {
125- $ app ->db ->datalogUpdate ('mail_forwarding ' , array ("source " => $ rec ['source ' ]), 'forwarding_id ' , $ rec ['forwarding_id ' ],true );
298+ $ tmp_user = $ app ->db ->queryOneRecord ("SELECT id FROM spamfilter_users WHERE email = ? " , '@ ' . $ domain );
299+ if (isset ($ tmp_user ['id ' ]) && $ tmp_user ['id ' ] > 0 ) {
300+ // There is already a record that we will update
301+ if ($ policy_id != $ tmp_user ['policy_id ' ]) {
302+ $ update_data = array (
303+ 'policy_id ' => $ policy_id ,
304+ );
305+ $ app ->db ->datalogUpdate ('spamfilter_users ' , $ update_data , 'id ' , $ tmp_user ["id " ]);
306+ }
307+ } else {
308+ // We create a new record
309+ $ insert_data = array (
310+ "sys_userid " => $ client_user_id ,
311+ "sys_groupid " => $ sys_groupid ,
312+ "sys_perm_user " => 'riud ' ,
313+ "sys_perm_group " => 'riud ' ,
314+ "sys_perm_other " => '' ,
315+ "server_id " => $ page_form ->dataRecord ["server_id " ],
316+ "priority " => 5 ,
317+ "policy_id " => $ policy_id ,
318+ "email " => '@ ' . $ domain ,
319+ "fullname " => '@ ' . $ domain ,
320+ "local " => 'Y '
321+ );
322+ $ app ->db ->datalogInsert ('spamfilter_users ' , $ insert_data , 'id ' );
126323 }
127- }
128-
324+
325+ } // end if domain name changed
326+
129327 }
130328
131329}
0 commit comments