Skip to content

Commit d7f9f24

Browse files
committed
send spamfilter_{users,wblist}_update events when mail_user email changes
1 parent 7a58805 commit d7f9f24

File tree

2 files changed

+93
-21
lines changed

2 files changed

+93
-21
lines changed

interface/web/mail/mail_domain_edit.php

Lines changed: 58 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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-
?>

interface/web/mail/mail_user_edit.php

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -329,11 +329,42 @@ function onAfterUpdate() {
329329

330330
// Spamfilter policy
331331
$policy_id = $app->functions->intval($this->dataRecord["policy"]);
332+
// Handle email change
333+
$skip_spamfilter_users_update = false;
334+
if(isset($this->dataRecord['email']) && $this->oldDataRecord['email'] != $this->dataRecord['email']) {
335+
$tmp_olduser = $app->db->queryOneRecord("SELECT id,fullname FROM spamfilter_users WHERE email = ?", $this->oldDataRecord['email']);
336+
if($tmp_olduser["id"] > 0) {
337+
$tmp_newuser = $app->db->queryOneRecord("SELECT id FROM spamfilter_users WHERE email = ?", $this->dataRecord['email']);
338+
if($tmp_newuser['id'] > 0) {
339+
// There is a spamfilter_users for both old and new email, we'll update old wblist entries
340+
$tmp_wblist = $app->db->queryAllRecords("SELECT wblist_id FROM spamfilter_users WHERE rid = ?", $tmp_olduser['id']);
341+
foreach ($tmp_wblist as $tmp) {
342+
$app->db->datalogUpdate('spamfilter_wblist', array('rid' => $tmp_newuser['id']), 'wblist_id', $tmp['wblist_id']);
343+
}
344+
345+
// now delete old spamfilter_users entry
346+
$app->db->datalogDelete('spamfilter_users', 'id', $tmp_olduser['id']);
347+
} else {
348+
$update_data = array(
349+
'email' => $this->dataRecord['email'],
350+
'policy_id' => $policy_id,
351+
);
352+
if($tmp_olduser['fullname'] == $app->functions->idn_decode($this->oldDataRecord["email"])) {
353+
$update_data['fullname'] = $app->functions->idn_decode($this->dataRecord["email"]);
354+
}
355+
$app->db->datalogUpdate('spamfilter_users', $update_data, 'id', $tmp_olduser['id']);
356+
$skip_spamfilter_users_update = true;
357+
}
358+
}
359+
}
360+
332361
$tmp_user = $app->db->queryOneRecord("SELECT id FROM spamfilter_users WHERE email = ?", $this->dataRecord["email"]);
333362
if($policy_id > 0) {
334363
if($tmp_user["id"] > 0) {
335364
// There is already a record that we will update
336-
$app->db->datalogUpdate('spamfilter_users', array("policy_id" => $policy_id), 'id', $tmp_user["id"]);
365+
if(!$skip_spamfilter_users_update) {
366+
$app->db->datalogUpdate('spamfilter_users', array("policy_id" => $policy_id), 'id', $tmp_user["id"]);
367+
}
337368
} else {
338369
// We create a new record
339370
$insert_data = array(
@@ -351,9 +382,11 @@ function onAfterUpdate() {
351382
);
352383
$app->db->datalogInsert('spamfilter_users', $insert_data, 'id');
353384
}
354-
}else {
385+
} else {
355386
if($tmp_user["id"] > 0) {
356387
// There is already a record but the user shall have no policy, so we delete it
388+
// fixme: don't delete or we abandon users' wblist entries
389+
// https://git.ispconfig.org/ispconfig/ispconfig3/-/issues/6201
357390
$app->db->datalogDelete('spamfilter_users', 'id', $tmp_user["id"]);
358391
}
359392
} // endif spamfilter policy
@@ -372,8 +405,6 @@ function onAfterUpdate() {
372405

373406
//** If the email address has been changed, change it in all aliases too
374407
if(isset($this->dataRecord['email']) && $this->oldDataRecord['email'] != $this->dataRecord['email']) {
375-
//if($this->oldDataRecord['email'] != $this->dataRecord['email']) {
376-
377408
//* Update the aliases
378409
$forwardings = $app->db->queryAllRecords("SELECT * FROM mail_forwarding WHERE destination = ?", $this->oldDataRecord['email']);
379410
if(is_array($forwardings)) {
@@ -401,4 +432,3 @@ function onAfterUpdate() {
401432
$app->tform_actions = new page_action;
402433
$app->tform_actions->onLoad();
403434

404-
?>

0 commit comments

Comments
 (0)