@@ -413,7 +413,7 @@ function onAfterUpdate() {
413413 // If domain changes, update spamfilter_users
414414 // and fire spamfilter_wblist_update events so rspamd files are rewritten
415415 $ skip_spamfilter_users_update = false ;
416- if (isset ( $ old_domain != $ domain ) {
416+ if ($ old_domain != $ domain ) {
417417 $ tmp_old = $ app ->db ->queryOneRecord ("SELECT id,fullname FROM spamfilter_users WHERE email = ? " , '@ ' . $ old_domain );
418418 if ($ tmp_old ['id ' ] > 0 ) {
419419 $ tmp_new = $ app ->db ->queryOneRecord ("SELECT id,fullname FROM spamfilter_users WHERE email = ? " , '@ ' . $ domain );
@@ -480,29 +480,152 @@ function onAfterUpdate() {
480480 $ mail_config = $ app ->getconf ->get_server_config ($ this ->dataRecord ["server_id " ], 'mail ' );
481481
482482 //* Update the mailboxes
483- $ mailusers = $ app ->db ->queryAllRecords ("SELECT * FROM mail_user WHERE email like ? " , '%@ ' . $ old_domain ;
483+ $ mailusers = $ app ->db ->queryAllRecords ("SELECT * FROM mail_user WHERE email like ? " , '%@ ' . $ old_domain) ;
484484 $ sys_groupid = $ app ->functions ->intval ((isset ($ this ->dataRecord ['client_group_id ' ]))?$ this ->dataRecord ['client_group_id ' ]:$ this ->oldDataRecord ['sys_groupid ' ]);
485485 $ tmp = $ app ->db ->queryOneRecord ("SELECT userid FROM sys_user WHERE default_group = ? " , $ sys_groupid );
486486 $ client_user_id = $ app ->functions ->intval (($ tmp ['userid ' ] > 0 )?$ tmp ['userid ' ]:1 );
487487 if (is_array ($ mailusers )) {
488- // fixme: change spamfilter_users and fire wblist events
489488 foreach ($ mailusers as $ rec ) {
490489 // setting Maildir, Homedir, UID and GID
491490 $ mail_parts = explode ("@ " , $ rec ['email ' ]);
492491 $ maildir = str_replace ("[domain] " , $ domain , $ mail_config ["maildir_path " ]);
493492 $ maildir = str_replace ("[localpart] " , $ mail_parts [0 ], $ maildir );
494493 $ email = $ mail_parts [0 ].'@ ' .$ this ->dataRecord ['domain ' ];
495- $ app ->db ->datalogUpdate ('mail_user ' , array ("maildir " => $ maildir , "email " => $ email , "sys_userid " => $ client_user_id , "sys_groupid " => $ sys_groupid ), 'mailuser_id ' , $ rec ['mailuser_id ' ]);
494+ // update spamfilter_users and spamfilter_wblist if email change
495+ $ skip_spamfilter_users_update = false ;
496+ if ($ email != $ mail_parts [0 ].'@ ' .$ this ->oldDataRecord ['domain ' ]) {
497+ $ tmp_olduser = $ app ->db ->queryOneRecord ("SELECT id,fullname FROM spamfilter_users WHERE email = ? " , $ mail_parts [0 ].'@ ' .$ this ->oldDataRecord ['domain ' ]);
498+ if ($ tmp_olduser ['id ' ] > 0 ) {
499+
500+ $ tmp_newuser = $ app ->db ->queryOneRecord ("SELECT id FROM spamfilter_users WHERE email = ? " , $ mail_parts [0 ].'@ ' .$ this ->dataRecord ['domain ' ]);
501+ if ($ tmp_newuser ['id ' ] > 0 ) {
502+ // There is a spamfilter_users for both old and new email, we'll update old wblist entries
503+ $ tmp_wblist = $ app ->db ->queryAllRecords ("SELECT wblist_id FROM spamfilter_users WHERE rid = ? " , $ tmp_olduser ['id ' ]);
504+ foreach ($ tmp_wblist as $ tmp ) {
505+ $ update_data = array (
506+ 'rid ' => $ tmp_newuser ['id ' ],
507+ 'sys_userid ' => $ client_user_id ,
508+ 'sys_groupid ' => $ sys_groupid ,
509+ );
510+ $ app ->db ->datalogUpdate ('spamfilter_wblist ' , $ update_data , 'wblist_id ' , $ tmp ['wblist_id ' ]);
511+ }
512+
513+ // now delete old spamfilter_users entry
514+ $ app ->db ->datalogDelete ('spamfilter_users ' , 'id ' , $ tmp_olduser ['id ' ]);
515+ } else {
516+ $ update_data = array (
517+ 'email ' => $ mail_parts [0 ].'@ ' .$ this ->dataRecord ['domain ' ],
518+ 'sys_userid ' => $ client_user_id ,
519+ 'sys_groupid ' => $ sys_groupid ,
520+ );
521+ if ($ tmp_olduser ['fullname ' ] == $ app ->functions ->idn_decode ($ mail_parts [0 ].'@ ' .$ this ->oldDataRecord ['domain ' ])) {
522+ $ update_data ['fullname ' ] = $ app ->functions ->idn_decode ($ mail_parts [0 ].'@ ' .$ this ->dataRecord ['domain ' ]);
523+ }
524+ $ app ->db ->datalogUpdate ('spamfilter_users ' , $ update_data , 'id ' , $ tmp_olduser ['id ' ]);
525+ $ skip_spamfilter_users_update = true ;
526+
527+ $ tmp_wblist = $ app ->db ->queryAllRecords ("SELECT wblist_id FROM spamfilter_users WHERE rid = ? " , $ tmp_olduser ['id ' ]);
528+ $ update_data = array (
529+ 'sys_userid ' => $ client_user_id ,
530+ 'sys_groupid ' => $ sys_groupid ,
531+ );
532+ foreach ($ tmp_wblist as $ tmp ) {
533+ $ app ->db ->datalogUpdate ('spamfilter_wblist ' , $ update_data , 'wblist_id ' , $ tmp ['wblist_id ' ]);
534+ }
535+ }
536+ }
537+
538+ $ tmp_user = $ app ->db ->queryOneRecord ("SELECT id FROM spamfilter_users WHERE email = ? " , $ mail_parts [0 ].'@ ' .$ this ->dataRecord ['domain ' ]);
539+ if ($ tmp_user ["id " ] > 0 ) {
540+ // There is already a record that we will update
541+ if (!$ skip_spamfilter_users_update ) {
542+ $ update_data = array (
543+ 'sys_userid ' => $ client_user_id ,
544+ 'sys_groupid ' => $ sys_groupid ,
545+ );
546+ $ app ->db ->datalogUpdate ('spamfilter_users ' , $ update_data , 'id ' , $ tmp_user ['id ' ]);
547+ }
548+ /*
549+ } else {
550+ # fixme: insert spamfilter_users with correct policy_id,
551+ # pending https://git.ispconfig.org/ispconfig/ispconfig3/-/issues/6201
552+ */
553+ }
554+
555+ $ app ->db ->datalogUpdate ('mail_user ' , array ("maildir " => $ maildir , "email " => $ email , "sys_userid " => $ client_user_id , "sys_groupid " => $ sys_groupid ), 'mailuser_id ' , $ rec ['mailuser_id ' ]);
556+ }
496557 }
497558 }
498559
499560 //* Update the aliases
500561 $ forwardings = $ app ->db ->queryAllRecords ("SELECT * FROM mail_forwarding WHERE source like ? OR destination like ? " , '%@ ' . $ old_domain , '%@ ' . $ old_domain );
501562 if (is_array ($ forwardings )) {
502- // fixme: change spamfilter_users and fire wblist events for aliases/forwards
503563 foreach ($ forwardings as $ rec ) {
504564 $ destination = str_replace ($ old_domain , $ domain , $ rec ['destination ' ]);
505565 $ source = str_replace ($ old_domain , $ domain , $ rec ['source ' ]);
566+
567+ // update spamfilter_users and spamfilter_wblist if email change
568+ $ skip_spamfilter_users_update = false ;
569+ if ($ source != $ rec ['source ' ]) {
570+ $ tmp_olduser = $ app ->db ->queryOneRecord ("SELECT id,fullname FROM spamfilter_users WHERE email = ? " , $ rec ['source ' ]);
571+ if ($ tmp_olduser ['id ' ] > 0 ) {
572+ $ tmp_newuser = $ app ->db ->queryOneRecord ("SELECT id FROM spamfilter_users WHERE email = ? " , $ source );
573+ if ($ tmp_newuser ['id ' ] > 0 ) {
574+ // There is a spamfilter_users for both old and new email, we'll update old wblist entries
575+ $ tmp_wblist = $ app ->db ->queryAllRecords ("SELECT wblist_id FROM spamfilter_users WHERE rid = ? " , $ tmp_olduser ['id ' ]);
576+ foreach ($ tmp_wblist as $ tmp ) {
577+ $ update_data = array (
578+ 'rid ' => $ tmp_newuser ['id ' ],
579+ 'sys_userid ' => $ client_user_id ,
580+ 'sys_groupid ' => $ sys_groupid ,
581+ );
582+ $ app ->db ->datalogUpdate ('spamfilter_wblist ' , $ update_data , 'wblist_id ' , $ tmp ['wblist_id ' ]);
583+ }
584+
585+ // now delete old spamfilter_users entry
586+ $ app ->db ->datalogDelete ('spamfilter_users ' , 'id ' , $ tmp_olduser ['id ' ]);
587+ } else {
588+ $ update_data = array (
589+ 'email ' => $ source ,
590+ 'sys_userid ' => $ client_user_id ,
591+ 'sys_groupid ' => $ sys_groupid ,
592+ );
593+ if ($ tmp_olduser ['fullname ' ] == $ app ->functions ->idn_decode ($ rec ['source ' ])) {
594+ $ update_data ['fullname ' ] = $ app ->functions ->idn_decode ($ source );
595+ }
596+ $ app ->db ->datalogUpdate ('spamfilter_users ' , $ update_data , 'id ' , $ tmp_olduser ['id ' ]);
597+ $ skip_spamfilter_users_update = true ;
598+
599+ $ tmp_wblist = $ app ->db ->queryAllRecords ("SELECT wblist_id FROM spamfilter_users WHERE rid = ? " , $ tmp_olduser ['id ' ]);
600+ $ update_data = array (
601+ 'sys_userid ' => $ client_user_id ,
602+ 'sys_groupid ' => $ sys_groupid ,
603+ );
604+ foreach ($ tmp_wblist as $ tmp ) {
605+ $ app ->db ->datalogUpdate ('spamfilter_wblist ' , $ update_data , 'wblist_id ' , $ tmp ['wblist_id ' ]);
606+ }
607+ }
608+ }
609+
610+ $ tmp_user = $ app ->db ->queryOneRecord ("SELECT id FROM spamfilter_users WHERE email = ? " , $ source );
611+ if ($ tmp_user ["id " ] > 0 ) {
612+ // There is already a record that we will update
613+ if (!$ skip_spamfilter_users_update ) {
614+ $ update_data = array (
615+ 'sys_userid ' => $ client_user_id ,
616+ 'sys_groupid ' => $ sys_groupid ,
617+ );
618+ $ app ->db ->datalogUpdate ('spamfilter_users ' , $ update_data , 'id ' , $ tmp_user ['id ' ]);
619+ }
620+ /*
621+ } else {
622+ # fixme: insert spamfilter_users with correct policy_id,
623+ # pending https://git.ispconfig.org/ispconfig/ispconfig3/-/issues/6201
624+ */
625+ }
626+
627+ }
628+
506629 $ app ->db ->datalogUpdate ('mail_forwarding ' , array ("source " => $ source , "destination " => $ destination , "sys_userid " => $ client_user_id , "sys_groupid " => $ sys_groupid ), 'forwarding_id ' , $ rec ['forwarding_id ' ]);
507630 }
508631 }
0 commit comments