@@ -405,14 +405,48 @@ function onAfterUpdate() {
405405 global $ app , $ conf ;
406406
407407 $ domain = $ app ->functions ->idn_encode ($ this ->dataRecord ["domain " ]);
408+ $ old_domain = $ app ->functions ->idn_encode ($ this ->oldDataRecord ["domain " ]);
408409
409410 // Spamfilter policy
410411 $ policy_id = $ app ->functions ->intval ($ this ->dataRecord ["policy " ]);
412+
413+ // If domain changes, update spamfilter_users
414+ // and fire spamfilter_wblist_update events so rspamd files are rewritten
415+ $ skip_spamfilter_users_update = false ;
416+ if (isset ($ old_domain != $ domain ) {
417+ $ tmp_old = $ app ->db ->queryOneRecord ("SELECT id,fullname FROM spamfilter_users WHERE email = ? " , '@ ' . $ old_domain );
418+ if ($ tmp_old ['id ' ] > 0 ) {
419+ $ tmp_new = $ app ->db ->queryOneRecord ("SELECT id,fullname FROM spamfilter_users WHERE email = ? " , '@ ' . $ domain );
420+ if ($ tmp_new ['id ' ] > 0 ) {
421+ // There is a spamfilter_users for both old and new domain, we'll update old wblist entries
422+ $ tmp_wblist = $ app ->db ->queryAllRecords ("SELECT wblist_id FROM spamfilter_users WHERE rid = ? " , $ tmp_old ['id ' ]);
423+ foreach ($ tmp_wblist as $ tmp ) {
424+ $ app ->db ->datalogUpdate ('spamfilter_wblist ' , array ('rid ' => $ tmp_new ['id ' ]), 'wblist_id ' , $ tmp ['wblist_id ' ]);
425+ }
426+
427+ // now delete old spamfilter_users entry
428+ $ app ->db ->datalogDelete ('spamfilter_users ' , 'id ' , $ tmp_old ['id ' ]);
429+ } else {
430+ $ update_data = array (
431+ 'email ' => '@ ' . $ domain ,
432+ 'policy_id ' => $ policy_id ,
433+ );
434+ if ($ tmp_old ['fullname ' ] == '@ ' . $ old_domain ) {
435+ $ update_data ['fullname ' ] = '@ ' . $ domain ;
436+ }
437+ $ app ->db ->datalogUpdate ('spamfilter_users ' , $ update_data , 'id ' , $ tmp_old ['id ' ]);
438+ $ skip_spamfilter_users_update = true ;
439+ }
440+ }
441+ }
442+
411443 $ tmp_user = $ app ->db ->queryOneRecord ("SELECT id FROM spamfilter_users WHERE email = ? " , '@ ' . $ domain );
412444 if ($ policy_id > 0 ) {
413445 if ($ tmp_user ["id " ] > 0 ) {
414446 // There is already a record that we will update
415- $ app ->db ->datalogUpdate ('spamfilter_users ' , array ("policy_id " => $ policy_id ), 'id ' , $ tmp_user ["id " ]);
447+ if (!$ skip_spamfilter_users_update ) {
448+ $ app ->db ->datalogUpdate ('spamfilter_users ' , array ("policy_id " => $ policy_id ), 'id ' , $ tmp_user ["id " ]);
449+ }
416450 } else {
417451 $ tmp_domain = $ app ->db ->queryOneRecord ("SELECT sys_groupid FROM mail_domain WHERE domain_id = ? " , $ this ->id );
418452 // We create a new record
@@ -435,20 +469,23 @@ function onAfterUpdate() {
435469 } else {
436470 if ($ tmp_user ["id " ] > 0 ) {
437471 // There is already a record but the user shall have no policy, so we delete it
472+ // fixme: this abandons any spamfilter_wblist entries tied to this spamfilter_users id
473+ // https://git.ispconfig.org/ispconfig/ispconfig3/-/issues/6201
438474 $ app ->db ->datalogDelete ('spamfilter_users ' , 'id ' , $ tmp_user ["id " ]);
439475 }
440476 } // endif spamfilter policy
441477 //** If the domain name or owner has been changed, change the domain and owner in all mailbox records
442- if ($ this -> oldDataRecord [ ' domain ' ] != $ domain || (isset ($ this ->dataRecord ['client_group_id ' ]) && $ this ->oldDataRecord ['sys_groupid ' ] != $ this ->dataRecord ['client_group_id ' ])) {
478+ if ($ old_domain != $ domain || (isset ($ this ->dataRecord ['client_group_id ' ]) && $ this ->oldDataRecord ['sys_groupid ' ] != $ this ->dataRecord ['client_group_id ' ])) {
443479 $ app ->uses ('getconf ' );
444480 $ mail_config = $ app ->getconf ->get_server_config ($ this ->dataRecord ["server_id " ], 'mail ' );
445481
446482 //* Update the mailboxes
447- $ mailusers = $ app ->db ->queryAllRecords ("SELECT * FROM mail_user WHERE email like ? " , '%@ ' . $ this -> oldDataRecord [ ' domain ' ]) ;
483+ $ mailusers = $ app ->db ->queryAllRecords ("SELECT * FROM mail_user WHERE email like ? " , '%@ ' . $ old_domain ;
448484 $ sys_groupid = $ app ->functions ->intval ((isset ($ this ->dataRecord ['client_group_id ' ]))?$ this ->dataRecord ['client_group_id ' ]:$ this ->oldDataRecord ['sys_groupid ' ]);
449485 $ tmp = $ app ->db ->queryOneRecord ("SELECT userid FROM sys_user WHERE default_group = ? " , $ sys_groupid );
450486 $ client_user_id = $ app ->functions ->intval (($ tmp ['userid ' ] > 0 )?$ tmp ['userid ' ]:1 );
451487 if (is_array ($ mailusers )) {
488+ // fixme: change spamfilter_users and fire wblist events
452489 foreach ($ mailusers as $ rec ) {
453490 // setting Maildir, Homedir, UID and GID
454491 $ mail_parts = explode ("@ " , $ rec ['email ' ]);
@@ -460,32 +497,38 @@ function onAfterUpdate() {
460497 }
461498
462499 //* Update the aliases
463- $ forwardings = $ app ->db ->queryAllRecords ("SELECT * FROM mail_forwarding WHERE source like ? OR destination like ? " , '%@ ' . $ this -> oldDataRecord [ ' domain ' ] , '%@ ' . $ this -> oldDataRecord [ ' domain ' ] );
500+ $ forwardings = $ app ->db ->queryAllRecords ("SELECT * FROM mail_forwarding WHERE source like ? OR destination like ? " , '%@ ' . $ old_domain , '%@ ' . $ old_domain );
464501 if (is_array ($ forwardings )) {
502+ // fixme: change spamfilter_users and fire wblist events for aliases/forwards
465503 foreach ($ forwardings as $ rec ) {
466- $ destination = str_replace ($ this -> oldDataRecord [ ' domain ' ] , $ domain , $ rec ['destination ' ]);
467- $ source = str_replace ($ this -> oldDataRecord [ ' domain ' ] , $ domain , $ rec ['source ' ]);
504+ $ destination = str_replace ($ old_domain , $ domain , $ rec ['destination ' ]);
505+ $ source = str_replace ($ old_domain , $ domain , $ rec ['source ' ]);
468506 $ app ->db ->datalogUpdate ('mail_forwarding ' , array ("source " => $ source , "destination " => $ destination , "sys_userid " => $ client_user_id , "sys_groupid " => $ sys_groupid ), 'forwarding_id ' , $ rec ['forwarding_id ' ]);
469507 }
470508 }
471509
472510 //* Update the mailinglist
473- $ app ->db ->query ("UPDATE mail_mailinglist SET sys_userid = ?, sys_groupid = ? WHERE domain = ? " , $ client_user_id , $ sys_groupid , $ this ->oldDataRecord ['domain ' ]);
511+ $ mailinglists = $ app ->db ->queryAllRecords ("SELECT * FROM mail_mailinglist WHERE domain = ? " , $ old_domain );
512+ if (is_array ($ mailinglists )) {
513+ foreach ($ mailinglists as $ rec ) {
514+ $ update_data = array (
515+ 'sys_userid ' => $ client_user_id ,
516+ 'sys_groupid ' => $ sys_groupid ,
517+ 'domain ' => $ domain ,
518+ 'email ' => str_replace ($ old_domain , $ domain , $ rec ['email ' ]),
519+ );
520+ $ app ->db ->datalogUpdate ('mail_mailinglist ' , $ update_data , 'mailinglist_id ' , $ rec ['mailinglist_id ' ]);
521+ }
522+ }
474523
475524 //* Update fetchmail accounts
476- $ fetchmail = $ app ->db ->queryAllRecords ("SELECT * FROM mail_get WHERE destination like ? " , '%@ ' . $ this -> oldDataRecord [ ' domain ' ] );
525+ $ fetchmail = $ app ->db ->queryAllRecords ("SELECT * FROM mail_get WHERE destination like ? " , '%@ ' . $ old_domain );
477526 if (is_array ($ fetchmail )) {
478527 foreach ($ fetchmail as $ rec ) {
479- $ destination = str_replace ($ this -> oldDataRecord [ ' domain ' ] , $ domain , $ rec ['destination ' ]);
528+ $ destination = str_replace ($ old_domain , $ domain , $ rec ['destination ' ]);
480529 $ app ->db ->datalogUpdate ('mail_get ' , array ("destination " => $ destination , "sys_userid " => $ client_user_id , "sys_groupid " => $ sys_groupid ), 'mailget_id ' , $ rec ['mailget_id ' ]);
481530 }
482531 }
483-
484- //* Delete the old spamfilter record
485- $ tmp = $ app ->db ->queryOneRecord ("SELECT id FROM spamfilter_users WHERE email = ? " , '@ ' . $ this ->oldDataRecord ["domain " ]);
486- $ app ->db ->datalogDelete ('spamfilter_users ' , 'id ' , $ tmp ["id " ]);
487- unset($ tmp );
488-
489532 } // end if domain name changed
490533
491534 //* update dns-record when the dkim record was changed
@@ -559,4 +602,3 @@ private function update_dns($dataRecord, $new_rr) {
559602$ page = new page_action ;
560603$ page ->onLoad ();
561604
562- ?>
0 commit comments