From 7a58805fbcf9892e2c0888aa3853a02b641ee5ec Mon Sep 17 00:00:00 2001 From: Jesse Norell Date: Fri, 16 Jul 2021 15:39:48 -0600 Subject: [PATCH 1/7] send spamfilter_wblist_update events for change of spamfilter_users.email --- interface/web/mail/spamfilter_users_edit.php | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/interface/web/mail/spamfilter_users_edit.php b/interface/web/mail/spamfilter_users_edit.php index b8bc9316c5..98493173cc 100644 --- a/interface/web/mail/spamfilter_users_edit.php +++ b/interface/web/mail/spamfilter_users_edit.php @@ -90,7 +90,7 @@ class page_action extends tform_actions { $client_group_id = $app->functions->intval($_SESSION["s"]["user"]["default_group"]); $client = $app->db->queryOneRecord("SELECT limit_spamfilter_user FROM sys_group, client WHERE sys_group.client_id = client.client_id and sys_group.groupid = ?", $client_group_id); - // Check if the user may add another mailbox. + // Check if the user may add another spamfilter user. if($this->id == 0 && $client["limit_spamfilter_user"] >= 0) { $tmp = $app->db->queryOneRecord("SELECT count(id) as number FROM spamfilter_users WHERE sys_groupid = ?", $client_group_id); if($tmp["number"] >= $client["limit_spamfilter_user"]) { @@ -103,10 +103,21 @@ class page_action extends tform_actions { parent::onSubmit(); } + + function onAfterUpdate() { + global $app, $conf; + + // If email changes fire spamfilter_wblist_update events so rspamd files are rewritten + if(isset($this->dataRecord['email']) && $this->oldDataRecord['email'] != $this->dataRecord['email']) { + $tmp_wblist = $app->db->queryAllRecords("SELECT wblist_id FROM spamfilter_users WHERE rid = ?", $this->dataRecord['id']); + foreach ($tmp_wblist as $tmp) { + $app->db->datalogUpdate('spamfilter_wblist', array('rid' => $this->dataRecord['id']), 'wblist_id', $tmp['wblist_id']); + } + } + } + } $app->tform_actions = new page_action; $app->tform_actions->onLoad(); - -?> -- GitLab From d7f9f242e3b53c48a69a6c868bbba4ef6ebeeb24 Mon Sep 17 00:00:00 2001 From: Jesse Norell Date: Fri, 16 Jul 2021 15:40:37 -0600 Subject: [PATCH 2/7] send spamfilter_{users,wblist}_update events when mail_user email changes --- interface/web/mail/mail_domain_edit.php | 74 +++++++++++++++++++------ interface/web/mail/mail_user_edit.php | 40 +++++++++++-- 2 files changed, 93 insertions(+), 21 deletions(-) diff --git a/interface/web/mail/mail_domain_edit.php b/interface/web/mail/mail_domain_edit.php index 67a724dbaf..a980aeef50 100644 --- a/interface/web/mail/mail_domain_edit.php +++ b/interface/web/mail/mail_domain_edit.php @@ -405,14 +405,48 @@ class page_action extends tform_actions { global $app, $conf; $domain = $app->functions->idn_encode($this->dataRecord["domain"]); + $old_domain = $app->functions->idn_encode($this->oldDataRecord["domain"]); // Spamfilter policy $policy_id = $app->functions->intval($this->dataRecord["policy"]); + + // If domain changes, update spamfilter_users + // and fire spamfilter_wblist_update events so rspamd files are rewritten + $skip_spamfilter_users_update = false; + if(isset($old_domain != $domain) { + $tmp_old = $app->db->queryOneRecord("SELECT id,fullname FROM spamfilter_users WHERE email = ?", '@' . $old_domain); + if($tmp_old['id'] > 0) { + $tmp_new = $app->db->queryOneRecord("SELECT id,fullname FROM spamfilter_users WHERE email = ?", '@' . $domain); + if($tmp_new['id'] > 0) { + // There is a spamfilter_users for both old and new domain, we'll update old wblist entries + $tmp_wblist = $app->db->queryAllRecords("SELECT wblist_id FROM spamfilter_users WHERE rid = ?", $tmp_old['id']); + foreach ($tmp_wblist as $tmp) { + $app->db->datalogUpdate('spamfilter_wblist', array('rid' => $tmp_new['id']), 'wblist_id', $tmp['wblist_id']); + } + + // now delete old spamfilter_users entry + $app->db->datalogDelete('spamfilter_users', 'id', $tmp_old['id']); + } else { + $update_data = array( + 'email' => '@' . $domain, + 'policy_id' => $policy_id, + ); + if($tmp_old['fullname'] == '@' . $old_domain) { + $update_data['fullname'] = '@' . $domain; + } + $app->db->datalogUpdate('spamfilter_users', $update_data, 'id', $tmp_old['id']); + $skip_spamfilter_users_update = true; + } + } + } + $tmp_user = $app->db->queryOneRecord("SELECT id FROM spamfilter_users WHERE email = ?", '@' . $domain); if($policy_id > 0) { if($tmp_user["id"] > 0) { // There is already a record that we will update - $app->db->datalogUpdate('spamfilter_users', array("policy_id" => $policy_id), 'id', $tmp_user["id"]); + if(!$skip_spamfilter_users_update) { + $app->db->datalogUpdate('spamfilter_users', array("policy_id" => $policy_id), 'id', $tmp_user["id"]); + } } else { $tmp_domain = $app->db->queryOneRecord("SELECT sys_groupid FROM mail_domain WHERE domain_id = ?", $this->id); // We create a new record @@ -435,20 +469,23 @@ class page_action extends tform_actions { } else { if($tmp_user["id"] > 0) { // There is already a record but the user shall have no policy, so we delete it +// fixme: this abandons any spamfilter_wblist entries tied to this spamfilter_users id +// https://git.ispconfig.org/ispconfig/ispconfig3/-/issues/6201 $app->db->datalogDelete('spamfilter_users', 'id', $tmp_user["id"]); } } // endif spamfilter policy //** If the domain name or owner has been changed, change the domain and owner in all mailbox records - if($this->oldDataRecord['domain'] != $domain || (isset($this->dataRecord['client_group_id']) && $this->oldDataRecord['sys_groupid'] != $this->dataRecord['client_group_id'])) { + if($old_domain != $domain || (isset($this->dataRecord['client_group_id']) && $this->oldDataRecord['sys_groupid'] != $this->dataRecord['client_group_id'])) { $app->uses('getconf'); $mail_config = $app->getconf->get_server_config($this->dataRecord["server_id"], 'mail'); //* Update the mailboxes - $mailusers = $app->db->queryAllRecords("SELECT * FROM mail_user WHERE email like ?", '%@' . $this->oldDataRecord['domain']); + $mailusers = $app->db->queryAllRecords("SELECT * FROM mail_user WHERE email like ?", '%@' . $old_domain; $sys_groupid = $app->functions->intval((isset($this->dataRecord['client_group_id']))?$this->dataRecord['client_group_id']:$this->oldDataRecord['sys_groupid']); $tmp = $app->db->queryOneRecord("SELECT userid FROM sys_user WHERE default_group = ?", $sys_groupid); $client_user_id = $app->functions->intval(($tmp['userid'] > 0)?$tmp['userid']:1); if(is_array($mailusers)) { +// fixme: change spamfilter_users and fire wblist events foreach($mailusers as $rec) { // setting Maildir, Homedir, UID and GID $mail_parts = explode("@", $rec['email']); @@ -460,32 +497,38 @@ class page_action extends tform_actions { } //* Update the aliases - $forwardings = $app->db->queryAllRecords("SELECT * FROM mail_forwarding WHERE source like ? OR destination like ?", '%@' . $this->oldDataRecord['domain'], '%@' . $this->oldDataRecord['domain']); + $forwardings = $app->db->queryAllRecords("SELECT * FROM mail_forwarding WHERE source like ? OR destination like ?", '%@' . $old_domain, '%@' . $old_domain); if(is_array($forwardings)) { +// fixme: change spamfilter_users and fire wblist events for aliases/forwards foreach($forwardings as $rec) { - $destination = str_replace($this->oldDataRecord['domain'], $domain, $rec['destination']); - $source = str_replace($this->oldDataRecord['domain'], $domain, $rec['source']); + $destination = str_replace($old_domain, $domain, $rec['destination']); + $source = str_replace($old_domain, $domain, $rec['source']); $app->db->datalogUpdate('mail_forwarding', array("source" => $source, "destination" => $destination, "sys_userid" => $client_user_id, "sys_groupid" => $sys_groupid), 'forwarding_id', $rec['forwarding_id']); } } //* Update the mailinglist - $app->db->query("UPDATE mail_mailinglist SET sys_userid = ?, sys_groupid = ? WHERE domain = ?", $client_user_id, $sys_groupid, $this->oldDataRecord['domain']); + $mailinglists = $app->db->queryAllRecords("SELECT * FROM mail_mailinglist WHERE domain = ?", $old_domain); + if(is_array($mailinglists)) { + foreach($mailinglists as $rec) { + $update_data = array( + 'sys_userid' => $client_user_id, + 'sys_groupid' => $sys_groupid, + 'domain' => $domain, + 'email' => str_replace($old_domain, $domain, $rec['email']), + ); + $app->db->datalogUpdate('mail_mailinglist', $update_data, 'mailinglist_id', $rec['mailinglist_id']); + } + } //* Update fetchmail accounts - $fetchmail = $app->db->queryAllRecords("SELECT * FROM mail_get WHERE destination like ?", '%@' . $this->oldDataRecord['domain']); + $fetchmail = $app->db->queryAllRecords("SELECT * FROM mail_get WHERE destination like ?", '%@' . $old_domain); if(is_array($fetchmail)) { foreach($fetchmail as $rec) { - $destination = str_replace($this->oldDataRecord['domain'], $domain, $rec['destination']); + $destination = str_replace($old_domain, $domain, $rec['destination']); $app->db->datalogUpdate('mail_get', array("destination" => $destination, "sys_userid" => $client_user_id, "sys_groupid" => $sys_groupid), 'mailget_id', $rec['mailget_id']); } } - - //* Delete the old spamfilter record - $tmp = $app->db->queryOneRecord("SELECT id FROM spamfilter_users WHERE email = ?", '@' . $this->oldDataRecord["domain"]); - $app->db->datalogDelete('spamfilter_users', 'id', $tmp["id"]); - unset($tmp); - } // end if domain name changed //* update dns-record when the dkim record was changed @@ -559,4 +602,3 @@ class page_action extends tform_actions { $page = new page_action; $page->onLoad(); -?> diff --git a/interface/web/mail/mail_user_edit.php b/interface/web/mail/mail_user_edit.php index 1dca1db848..e24ca17c8f 100644 --- a/interface/web/mail/mail_user_edit.php +++ b/interface/web/mail/mail_user_edit.php @@ -329,11 +329,42 @@ class page_action extends tform_actions { // Spamfilter policy $policy_id = $app->functions->intval($this->dataRecord["policy"]); + // Handle email change + $skip_spamfilter_users_update = false; + if(isset($this->dataRecord['email']) && $this->oldDataRecord['email'] != $this->dataRecord['email']) { + $tmp_olduser = $app->db->queryOneRecord("SELECT id,fullname FROM spamfilter_users WHERE email = ?", $this->oldDataRecord['email']); + if($tmp_olduser["id"] > 0) { + $tmp_newuser = $app->db->queryOneRecord("SELECT id FROM spamfilter_users WHERE email = ?", $this->dataRecord['email']); + if($tmp_newuser['id'] > 0) { + // There is a spamfilter_users for both old and new email, we'll update old wblist entries + $tmp_wblist = $app->db->queryAllRecords("SELECT wblist_id FROM spamfilter_users WHERE rid = ?", $tmp_olduser['id']); + foreach ($tmp_wblist as $tmp) { + $app->db->datalogUpdate('spamfilter_wblist', array('rid' => $tmp_newuser['id']), 'wblist_id', $tmp['wblist_id']); + } + + // now delete old spamfilter_users entry + $app->db->datalogDelete('spamfilter_users', 'id', $tmp_olduser['id']); + } else { + $update_data = array( + 'email' => $this->dataRecord['email'], + 'policy_id' => $policy_id, + ); + if($tmp_olduser['fullname'] == $app->functions->idn_decode($this->oldDataRecord["email"])) { + $update_data['fullname'] = $app->functions->idn_decode($this->dataRecord["email"]); + } + $app->db->datalogUpdate('spamfilter_users', $update_data, 'id', $tmp_olduser['id']); + $skip_spamfilter_users_update = true; + } + } + } + $tmp_user = $app->db->queryOneRecord("SELECT id FROM spamfilter_users WHERE email = ?", $this->dataRecord["email"]); if($policy_id > 0) { if($tmp_user["id"] > 0) { // There is already a record that we will update - $app->db->datalogUpdate('spamfilter_users', array("policy_id" => $policy_id), 'id', $tmp_user["id"]); + if(!$skip_spamfilter_users_update) { + $app->db->datalogUpdate('spamfilter_users', array("policy_id" => $policy_id), 'id', $tmp_user["id"]); + } } else { // We create a new record $insert_data = array( @@ -351,9 +382,11 @@ class page_action extends tform_actions { ); $app->db->datalogInsert('spamfilter_users', $insert_data, 'id'); } - }else { + } else { if($tmp_user["id"] > 0) { // There is already a record but the user shall have no policy, so we delete it +// fixme: don't delete or we abandon users' wblist entries +// https://git.ispconfig.org/ispconfig/ispconfig3/-/issues/6201 $app->db->datalogDelete('spamfilter_users', 'id', $tmp_user["id"]); } } // endif spamfilter policy @@ -372,8 +405,6 @@ class page_action extends tform_actions { //** If the email address has been changed, change it in all aliases too if(isset($this->dataRecord['email']) && $this->oldDataRecord['email'] != $this->dataRecord['email']) { - //if($this->oldDataRecord['email'] != $this->dataRecord['email']) { - //* Update the aliases $forwardings = $app->db->queryAllRecords("SELECT * FROM mail_forwarding WHERE destination = ?", $this->oldDataRecord['email']); if(is_array($forwardings)) { @@ -401,4 +432,3 @@ class page_action extends tform_actions { $app->tform_actions = new page_action; $app->tform_actions->onLoad(); -?> -- GitLab From 7d3ec68f499d309d243b3a715f7e248adb543a0e Mon Sep 17 00:00:00 2001 From: Jesse Norell Date: Fri, 16 Jul 2021 16:36:52 -0600 Subject: [PATCH 3/7] update spamfilter_users and spamfilter_wblist when domain changes --- interface/web/mail/mail_domain_edit.php | 133 +++++++++++++++++++++++- 1 file changed, 128 insertions(+), 5 deletions(-) diff --git a/interface/web/mail/mail_domain_edit.php b/interface/web/mail/mail_domain_edit.php index a980aeef50..36185ead69 100644 --- a/interface/web/mail/mail_domain_edit.php +++ b/interface/web/mail/mail_domain_edit.php @@ -413,7 +413,7 @@ class page_action extends tform_actions { // If domain changes, update spamfilter_users // and fire spamfilter_wblist_update events so rspamd files are rewritten $skip_spamfilter_users_update = false; - if(isset($old_domain != $domain) { + if($old_domain != $domain) { $tmp_old = $app->db->queryOneRecord("SELECT id,fullname FROM spamfilter_users WHERE email = ?", '@' . $old_domain); if($tmp_old['id'] > 0) { $tmp_new = $app->db->queryOneRecord("SELECT id,fullname FROM spamfilter_users WHERE email = ?", '@' . $domain); @@ -480,29 +480,152 @@ class page_action extends tform_actions { $mail_config = $app->getconf->get_server_config($this->dataRecord["server_id"], 'mail'); //* Update the mailboxes - $mailusers = $app->db->queryAllRecords("SELECT * FROM mail_user WHERE email like ?", '%@' . $old_domain; + $mailusers = $app->db->queryAllRecords("SELECT * FROM mail_user WHERE email like ?", '%@' . $old_domain); $sys_groupid = $app->functions->intval((isset($this->dataRecord['client_group_id']))?$this->dataRecord['client_group_id']:$this->oldDataRecord['sys_groupid']); $tmp = $app->db->queryOneRecord("SELECT userid FROM sys_user WHERE default_group = ?", $sys_groupid); $client_user_id = $app->functions->intval(($tmp['userid'] > 0)?$tmp['userid']:1); if(is_array($mailusers)) { -// fixme: change spamfilter_users and fire wblist events foreach($mailusers as $rec) { // setting Maildir, Homedir, UID and GID $mail_parts = explode("@", $rec['email']); $maildir = str_replace("[domain]", $domain, $mail_config["maildir_path"]); $maildir = str_replace("[localpart]", $mail_parts[0], $maildir); $email = $mail_parts[0].'@'.$this->dataRecord['domain']; - $app->db->datalogUpdate('mail_user', array("maildir" => $maildir, "email" => $email, "sys_userid" => $client_user_id, "sys_groupid" => $sys_groupid), 'mailuser_id', $rec['mailuser_id']); + // update spamfilter_users and spamfilter_wblist if email change + $skip_spamfilter_users_update = false; + if($email != $mail_parts[0].'@'.$this->oldDataRecord['domain']) { + $tmp_olduser = $app->db->queryOneRecord("SELECT id,fullname FROM spamfilter_users WHERE email = ?", $mail_parts[0].'@'.$this->oldDataRecord['domain']); + if($tmp_olduser['id'] > 0) { + + $tmp_newuser = $app->db->queryOneRecord("SELECT id FROM spamfilter_users WHERE email = ?", $mail_parts[0].'@'.$this->dataRecord['domain']); + if($tmp_newuser['id'] > 0) { + // There is a spamfilter_users for both old and new email, we'll update old wblist entries + $tmp_wblist = $app->db->queryAllRecords("SELECT wblist_id FROM spamfilter_users WHERE rid = ?", $tmp_olduser['id']); + foreach ($tmp_wblist as $tmp) { + $update_data = array( + 'rid' => $tmp_newuser['id'], + 'sys_userid' => $client_user_id, + 'sys_groupid' => $sys_groupid, + ); + $app->db->datalogUpdate('spamfilter_wblist', $update_data, 'wblist_id', $tmp['wblist_id']); + } + + // now delete old spamfilter_users entry + $app->db->datalogDelete('spamfilter_users', 'id', $tmp_olduser['id']); + } else { + $update_data = array( + 'email' => $mail_parts[0].'@'.$this->dataRecord['domain'], + 'sys_userid' => $client_user_id, + 'sys_groupid' => $sys_groupid, + ); + if($tmp_olduser['fullname'] == $app->functions->idn_decode($mail_parts[0].'@'.$this->oldDataRecord['domain'])) { + $update_data['fullname'] = $app->functions->idn_decode($mail_parts[0].'@'.$this->dataRecord['domain']); + } + $app->db->datalogUpdate('spamfilter_users', $update_data, 'id', $tmp_olduser['id']); + $skip_spamfilter_users_update = true; + + $tmp_wblist = $app->db->queryAllRecords("SELECT wblist_id FROM spamfilter_users WHERE rid = ?", $tmp_olduser['id']); + $update_data = array( + 'sys_userid' => $client_user_id, + 'sys_groupid' => $sys_groupid, + ); + foreach ($tmp_wblist as $tmp) { + $app->db->datalogUpdate('spamfilter_wblist', $update_data, 'wblist_id', $tmp['wblist_id']); + } + } + } + + $tmp_user = $app->db->queryOneRecord("SELECT id FROM spamfilter_users WHERE email = ?", $mail_parts[0].'@'.$this->dataRecord['domain']); + if($tmp_user["id"] > 0) { + // There is already a record that we will update + if(!$skip_spamfilter_users_update) { + $update_data = array( + 'sys_userid' => $client_user_id, + 'sys_groupid' => $sys_groupid, + ); + $app->db->datalogUpdate('spamfilter_users', $update_data, 'id', $tmp_user['id']); + } + /* + } else { + # fixme: insert spamfilter_users with correct policy_id, + # pending https://git.ispconfig.org/ispconfig/ispconfig3/-/issues/6201 + */ + } + + $app->db->datalogUpdate('mail_user', array("maildir" => $maildir, "email" => $email, "sys_userid" => $client_user_id, "sys_groupid" => $sys_groupid), 'mailuser_id', $rec['mailuser_id']); + } } } //* Update the aliases $forwardings = $app->db->queryAllRecords("SELECT * FROM mail_forwarding WHERE source like ? OR destination like ?", '%@' . $old_domain, '%@' . $old_domain); if(is_array($forwardings)) { -// fixme: change spamfilter_users and fire wblist events for aliases/forwards foreach($forwardings as $rec) { $destination = str_replace($old_domain, $domain, $rec['destination']); $source = str_replace($old_domain, $domain, $rec['source']); + + // update spamfilter_users and spamfilter_wblist if email change + $skip_spamfilter_users_update = false; + if($source != $rec['source']) { + $tmp_olduser = $app->db->queryOneRecord("SELECT id,fullname FROM spamfilter_users WHERE email = ?", $rec['source']); + if($tmp_olduser['id'] > 0) { + $tmp_newuser = $app->db->queryOneRecord("SELECT id FROM spamfilter_users WHERE email = ?", $source); + if($tmp_newuser['id'] > 0) { + // There is a spamfilter_users for both old and new email, we'll update old wblist entries + $tmp_wblist = $app->db->queryAllRecords("SELECT wblist_id FROM spamfilter_users WHERE rid = ?", $tmp_olduser['id']); + foreach ($tmp_wblist as $tmp) { + $update_data = array( + 'rid' => $tmp_newuser['id'], + 'sys_userid' => $client_user_id, + 'sys_groupid' => $sys_groupid, + ); + $app->db->datalogUpdate('spamfilter_wblist', $update_data, 'wblist_id', $tmp['wblist_id']); + } + + // now delete old spamfilter_users entry + $app->db->datalogDelete('spamfilter_users', 'id', $tmp_olduser['id']); + } else { + $update_data = array( + 'email' => $source, + 'sys_userid' => $client_user_id, + 'sys_groupid' => $sys_groupid, + ); + if($tmp_olduser['fullname'] == $app->functions->idn_decode($rec['source'])) { + $update_data['fullname'] = $app->functions->idn_decode($source); + } + $app->db->datalogUpdate('spamfilter_users', $update_data, 'id', $tmp_olduser['id']); + $skip_spamfilter_users_update = true; + + $tmp_wblist = $app->db->queryAllRecords("SELECT wblist_id FROM spamfilter_users WHERE rid = ?", $tmp_olduser['id']); + $update_data = array( + 'sys_userid' => $client_user_id, + 'sys_groupid' => $sys_groupid, + ); + foreach ($tmp_wblist as $tmp) { + $app->db->datalogUpdate('spamfilter_wblist', $update_data, 'wblist_id', $tmp['wblist_id']); + } + } + } + + $tmp_user = $app->db->queryOneRecord("SELECT id FROM spamfilter_users WHERE email = ?", $source); + if($tmp_user["id"] > 0) { + // There is already a record that we will update + if(!$skip_spamfilter_users_update) { + $update_data = array( + 'sys_userid' => $client_user_id, + 'sys_groupid' => $sys_groupid, + ); + $app->db->datalogUpdate('spamfilter_users', $update_data, 'id', $tmp_user['id']); + } + /* + } else { + # fixme: insert spamfilter_users with correct policy_id, + # pending https://git.ispconfig.org/ispconfig/ispconfig3/-/issues/6201 + */ + } + + } + $app->db->datalogUpdate('mail_forwarding', array("source" => $source, "destination" => $destination, "sys_userid" => $client_user_id, "sys_groupid" => $sys_groupid), 'forwarding_id', $rec['forwarding_id']); } } -- GitLab From 20c5d50dbfa094575347544bd39eb0db6d36e313 Mon Sep 17 00:00:00 2001 From: Jesse Norell Date: Wed, 21 Jul 2021 12:34:36 -0600 Subject: [PATCH 4/7] update spamfilter_users and spamfilter_wblist when mail_user.email changes --- interface/web/mail/mail_user_edit.php | 99 ++++++++++++++++----------- 1 file changed, 59 insertions(+), 40 deletions(-) diff --git a/interface/web/mail/mail_user_edit.php b/interface/web/mail/mail_user_edit.php index e24ca17c8f..30c6d15000 100644 --- a/interface/web/mail/mail_user_edit.php +++ b/interface/web/mail/mail_user_edit.php @@ -327,43 +327,70 @@ class page_action extends tform_actions { $domain = $app->db->queryOneRecord("SELECT sys_groupid, server_id FROM mail_domain WHERE domain = ? AND ".$app->tform->getAuthSQL('r'), $app->functions->idn_encode($_POST["email_domain"])); $app->db->query("UPDATE mail_user SET sys_groupid = ? WHERE mailuser_id = ?", $domain["sys_groupid"], $this->id); + } + + // Set the fields for dovecot + if(isset($this->dataRecord["email"])) { + $disableimap = (isset($this->dataRecord["disableimap"]) && $this->dataRecord["disableimap"])?'y':'n'; + $disablepop3 = (isset($this->dataRecord["disablepop3"]) && $this->dataRecord["disablepop3"])?'y':'n'; + $disablesmtp = (isset($this->dataRecord["disablesmtp"]) && $this->dataRecord["disablesmtp"])?'y':'n'; + $disabledeliver = (isset($this->dataRecord["disabledeliver"]) && $this->dataRecord["disabledeliver"])?'y':'n'; + + $sql = "UPDATE mail_user SET disableimap = ?, disablesieve = ?, `disablesieve-filter` = ?, disablepop3 = ?, disablesmtp = ?, disabledeliver = ?, disablelda = ?, disablelmtp = ? WHERE mailuser_id = ?"; + $app->db->query($sql, $disableimap, $disableimap, $disableimap, $disablepop3, $disablesmtp, $disabledeliver, $disabledeliver, $disabledeliver, $this->id); + } + + //** Handle email address change + if(isset($this->dataRecord['email']) && $this->oldDataRecord['email'] != $this->dataRecord['email']) { // Spamfilter policy $policy_id = $app->functions->intval($this->dataRecord["policy"]); - // Handle email change + + //** Update spamfilter_users and spamfilter_wblist $skip_spamfilter_users_update = false; - if(isset($this->dataRecord['email']) && $this->oldDataRecord['email'] != $this->dataRecord['email']) { - $tmp_olduser = $app->db->queryOneRecord("SELECT id,fullname FROM spamfilter_users WHERE email = ?", $this->oldDataRecord['email']); - if($tmp_olduser["id"] > 0) { - $tmp_newuser = $app->db->queryOneRecord("SELECT id FROM spamfilter_users WHERE email = ?", $this->dataRecord['email']); - if($tmp_newuser['id'] > 0) { - // There is a spamfilter_users for both old and new email, we'll update old wblist entries - $tmp_wblist = $app->db->queryAllRecords("SELECT wblist_id FROM spamfilter_users WHERE rid = ?", $tmp_olduser['id']); - foreach ($tmp_wblist as $tmp) { - $app->db->datalogUpdate('spamfilter_wblist', array('rid' => $tmp_newuser['id']), 'wblist_id', $tmp['wblist_id']); - } + $tmp_olduser = $app->db->queryOneRecord("SELECT id,fullname FROM spamfilter_users WHERE email = ?", $this->oldDataRecord['email']); + if($tmp_olduser["id"] > 0) { + $tmp_newuser = $app->db->queryOneRecord("SELECT id FROM spamfilter_users WHERE email = ?", $this->dataRecord['email']); + if($tmp_newuser['id'] > 0) { + // There is a spamfilter_users for both old and new email, we'll update old wblist entries + $update_date = array('rid' => $tmp_newuser['id']); + if (isset($domain['sys_groupid'])) { + $update_data['sys_groupid'] = $domain['sys_groupid']; + } + $tmp_wblist = $app->db->queryAllRecords("SELECT wblist_id FROM spamfilter_users WHERE rid = ?", $tmp_olduser['id']); + foreach ($tmp_wblist as $tmp) { + $app->db->datalogUpdate('spamfilter_wblist', $update_data, 'wblist_id', $tmp['wblist_id']); + } - // now delete old spamfilter_users entry - $app->db->datalogDelete('spamfilter_users', 'id', $tmp_olduser['id']); - } else { - $update_data = array( - 'email' => $this->dataRecord['email'], - 'policy_id' => $policy_id, - ); - if($tmp_olduser['fullname'] == $app->functions->idn_decode($this->oldDataRecord["email"])) { - $update_data['fullname'] = $app->functions->idn_decode($this->dataRecord["email"]); - } - $app->db->datalogUpdate('spamfilter_users', $update_data, 'id', $tmp_olduser['id']); - $skip_spamfilter_users_update = true; + // now delete old spamfilter_users entry + $app->db->datalogDelete('spamfilter_users', 'id', $tmp_olduser['id']); + } else { + $update_data = array( + 'email' => $this->dataRecord['email'], + 'policy_id' => $policy_id, + ); + if (isset($domain['sys_groupid'])) { + $update_data['sys_groupid'] = $domain['sys_groupid']; + } + if($tmp_olduser['fullname'] == $app->functions->idn_decode($this->oldDataRecord['email'])) { + $update_data['fullname'] = $app->functions->idn_decode($this->dataRecord['email']); } + $app->db->datalogUpdate('spamfilter_users', $update_data, 'id', $tmp_olduser['id']); + $skip_spamfilter_users_update = true; } } - $tmp_user = $app->db->queryOneRecord("SELECT id FROM spamfilter_users WHERE email = ?", $this->dataRecord["email"]); + $tmp_user = $app->db->queryOneRecord("SELECT id FROM spamfilter_users WHERE email = ?", $this->dataRecord['email']); if($policy_id > 0) { if($tmp_user["id"] > 0) { // There is already a record that we will update if(!$skip_spamfilter_users_update) { - $app->db->datalogUpdate('spamfilter_users', array("policy_id" => $policy_id), 'id', $tmp_user["id"]); + $update_data = array( + 'policy_id' => $policy_id, + ); + if (isset($domain['sys_groupid'])) { + $update_data['sys_groupid'] = $domain['sys_groupid']; + } + $app->db->datalogUpdate('spamfilter_users', $update_data, 'id', $tmp_user['id']); } } else { // We create a new record @@ -390,27 +417,19 @@ class page_action extends tform_actions { $app->db->datalogDelete('spamfilter_users', 'id', $tmp_user["id"]); } } // endif spamfilter policy - } - - // Set the fields for dovecot - if(isset($this->dataRecord["email"])) { - $disableimap = (isset($this->dataRecord["disableimap"]) && $this->dataRecord["disableimap"])?'y':'n'; - $disablepop3 = (isset($this->dataRecord["disablepop3"]) && $this->dataRecord["disablepop3"])?'y':'n'; - $disablesmtp = (isset($this->dataRecord["disablesmtp"]) && $this->dataRecord["disablesmtp"])?'y':'n'; - $disabledeliver = (isset($this->dataRecord["disabledeliver"]) && $this->dataRecord["disabledeliver"])?'y':'n'; - $sql = "UPDATE mail_user SET disableimap = ?, disablesieve = ?, `disablesieve-filter` = ?, disablepop3 = ?, disablesmtp = ?, disabledeliver = ?, disablelda = ?, disablelmtp = ? WHERE mailuser_id = ?"; - $app->db->query($sql, $disableimap, $disableimap, $disableimap, $disablepop3, $disablesmtp, $disabledeliver, $disabledeliver, $disabledeliver, $this->id); - } - - //** If the email address has been changed, change it in all aliases too - if(isset($this->dataRecord['email']) && $this->oldDataRecord['email'] != $this->dataRecord['email']) { //* Update the aliases $forwardings = $app->db->queryAllRecords("SELECT * FROM mail_forwarding WHERE destination = ?", $this->oldDataRecord['email']); if(is_array($forwardings)) { foreach($forwardings as $rec) { $destination = $this->dataRecord['email']; - $app->db->datalogUpdate('mail_forwarding', array("destination" => $destination), 'forwarding_id', $rec['forwarding_id']); + $update_data = array( + 'destination' => $destination, + ); + if (isset($domain['sys_groupid'])) { + $update_data['sys_groupid'] = $domain['sys_groupid']; + } + $app->db->datalogUpdate('mail_forwarding', $update_data, 'forwarding_id', $rec['forwarding_id']); } } -- GitLab From 9a335174422f6baf6e051608fc979e35ded30405 Mon Sep 17 00:00:00 2001 From: Jesse Norell Date: Wed, 28 Jul 2021 17:40:25 -0600 Subject: [PATCH 5/7] always keep spamfilter_users entries: #6201 --- .../tpl/gentoo/amavisd-ispconfig.conf.master | 3 +- .../sql/incremental/upd_dev_collection.sql | 2 + install/sql/ispconfig3.sql | 2 +- install/tpl/amavisd_user_config.master | 3 +- install/tpl/fedora_amavisd_conf.master | 3 +- install/tpl/opensuse_amavisd_conf.master | 3 +- interface/lib/classes/remote.d/client.inc.php | 2 +- interface/lib/classes/remote.d/mail.inc.php | 4 +- .../plugins/mail_mail_domain_plugin.inc.php | 250 ++++++++++++++++-- interface/web/client/client_del.php | 6 +- .../web/mail/form/spamfilter_users.tform.php | 2 +- .../web/mail/lib/lang/ar_spamfilter_users.lng | 2 +- .../web/mail/lib/lang/bg_spamfilter_users.lng | 2 +- .../web/mail/lib/lang/br_spamfilter_users.lng | 1 + .../web/mail/lib/lang/ca_spamfilter_users.lng | 2 +- .../web/mail/lib/lang/cz_spamfilter_users.lng | 1 + .../web/mail/lib/lang/de_spamfilter_users.lng | 2 +- .../web/mail/lib/lang/dk_spamfilter_users.lng | 2 +- .../web/mail/lib/lang/el_spamfilter_users.lng | 2 +- .../web/mail/lib/lang/en_spamfilter_users.lng | 2 +- .../web/mail/lib/lang/es_spamfilter_users.lng | 2 +- .../web/mail/lib/lang/fi_spamfilter_users.lng | 2 +- .../web/mail/lib/lang/fr_spamfilter_users.lng | 2 +- .../web/mail/lib/lang/hr_spamfilter_users.lng | 2 +- .../web/mail/lib/lang/hu_spamfilter_users.lng | 2 +- .../web/mail/lib/lang/id_spamfilter_users.lng | 2 +- .../web/mail/lib/lang/it_spamfilter_users.lng | 2 +- .../web/mail/lib/lang/ja_spamfilter_users.lng | 2 +- .../web/mail/lib/lang/nl_spamfilter_users.lng | 2 +- .../web/mail/lib/lang/pl_spamfilter_users.lng | 2 +- .../web/mail/lib/lang/pt_spamfilter_users.lng | 2 +- .../web/mail/lib/lang/ro_spamfilter_users.lng | 2 +- .../web/mail/lib/lang/ru_spamfilter_users.lng | 2 +- .../web/mail/lib/lang/se_spamfilter_users.lng | 2 +- .../web/mail/lib/lang/sk_spamfilter_users.lng | 2 +- .../web/mail/lib/lang/tr_spamfilter_users.lng | 2 +- interface/web/mail/mail_domain_del.php | 4 + interface/web/mail/mail_domain_edit.php | 177 +++++++------ interface/web/mail/mail_user_del.php | 2 + interface/web/mail/mail_user_edit.php | 177 ++++++------- .../web/mail/spamfilter_blacklist_edit.php | 2 +- interface/web/mail/spamfilter_policy_edit.php | 11 +- interface/web/mail/spamfilter_users_edit.php | 25 +- .../web/mail/spamfilter_whitelist_edit.php | 2 +- .../mailuser/mail_user_spamfilter_edit.php | 58 ++-- interface/web/tools/resync.php | 86 +++--- server/plugins-available/mail_plugin.inc.php | 2 +- .../plugins-available/rspamd_plugin.inc.php | 24 +- 48 files changed, 561 insertions(+), 337 deletions(-) diff --git a/install/dist/tpl/gentoo/amavisd-ispconfig.conf.master b/install/dist/tpl/gentoo/amavisd-ispconfig.conf.master index 7e42c8a362..c60b50e2fa 100644 --- a/install/dist/tpl/gentoo/amavisd-ispconfig.conf.master +++ b/install/dist/tpl/gentoo/amavisd-ispconfig.conf.master @@ -51,7 +51,8 @@ use strict; $sql_select_policy = 'SELECT *,spamfilter_users.id'. ' FROM spamfilter_users LEFT JOIN spamfilter_policy ON spamfilter_users.policy_id=spamfilter_policy.id'. - ' WHERE spamfilter_users.email IN (%k) ORDER BY spamfilter_users.priority DESC'; + ' WHERE spamfilter_users.email IN (%k) AND spamfilter_users.policy_id != 0'. + ' ORDER BY spamfilter_users.priority DESC'; $sql_select_white_black_list = 'SELECT wb FROM spamfilter_wblist'. diff --git a/install/sql/incremental/upd_dev_collection.sql b/install/sql/incremental/upd_dev_collection.sql index e69de29bb2..2a9a9ffd09 100644 --- a/install/sql/incremental/upd_dev_collection.sql +++ b/install/sql/incremental/upd_dev_collection.sql @@ -0,0 +1,2 @@ +-- default spamfilter_users.policy_id to 0 +ALTER TABLE `spamfilter_users` ALTER `policy_id` SET DEFAULT 0; diff --git a/install/sql/ispconfig3.sql b/install/sql/ispconfig3.sql index 006beb6b53..9ebb69090f 100644 --- a/install/sql/ispconfig3.sql +++ b/install/sql/ispconfig3.sql @@ -1545,7 +1545,7 @@ CREATE TABLE `spamfilter_users` ( `sys_perm_other` varchar(5) NOT NULL DEFAULT '', `server_id` int(11) unsigned NOT NULL DEFAULT '0', `priority` tinyint(3) unsigned NOT NULL default '7', - `policy_id` int(11) unsigned NOT NULL default '1', + `policy_id` int(11) unsigned NOT NULL default '0', `email` varchar(255) NOT NULL DEFAULT '', `fullname` varchar(64) default NULL, `local` varchar(1) default NULL, diff --git a/install/tpl/amavisd_user_config.master b/install/tpl/amavisd_user_config.master index 344ea9a152..f764bb3acb 100644 --- a/install/tpl/amavisd_user_config.master +++ b/install/tpl/amavisd_user_config.master @@ -33,7 +33,8 @@ use strict; $sql_select_policy = 'SELECT *,spamfilter_users.id'. ' FROM spamfilter_users LEFT JOIN spamfilter_policy ON spamfilter_users.policy_id=spamfilter_policy.id'. - ' WHERE spamfilter_users.email IN (%k) ORDER BY spamfilter_users.priority DESC'; + ' WHERE spamfilter_users.email IN (%k) AND spamfilter_users.policy_id != 0'. + ' ORDER BY spamfilter_users.priority DESC'; $sql_select_white_black_list = 'SELECT wb FROM spamfilter_wblist'. diff --git a/install/tpl/fedora_amavisd_conf.master b/install/tpl/fedora_amavisd_conf.master index 9cf4b801bc..2435e6939e 100644 --- a/install/tpl/fedora_amavisd_conf.master +++ b/install/tpl/fedora_amavisd_conf.master @@ -751,7 +751,8 @@ $banned_filename_re = new_RE( $sql_select_policy = 'SELECT *,spamfilter_users.id'. ' FROM spamfilter_users LEFT JOIN spamfilter_policy ON spamfilter_users.policy_id=spamfilter_policy.id'. - ' WHERE spamfilter_users.email IN (%k) ORDER BY spamfilter_users.priority DESC'; + ' WHERE spamfilter_users.email IN (%k) AND spamfilter_users.policy_id != 0'. + ' ORDER BY spamfilter_users.priority DESC'; $sql_select_white_black_list = 'SELECT wb FROM spamfilter_wblist'. diff --git a/install/tpl/opensuse_amavisd_conf.master b/install/tpl/opensuse_amavisd_conf.master index 419eea237c..7310db9cbf 100644 --- a/install/tpl/opensuse_amavisd_conf.master +++ b/install/tpl/opensuse_amavisd_conf.master @@ -746,7 +746,8 @@ $banned_filename_re = new_RE( $sql_select_policy = 'SELECT *,spamfilter_users.id'. ' FROM spamfilter_users LEFT JOIN spamfilter_policy ON spamfilter_users.policy_id=spamfilter_policy.id'. - ' WHERE spamfilter_users.email IN (%k) ORDER BY spamfilter_users.priority DESC'; + ' WHERE spamfilter_users.email IN (%k) AND spamfilter_users.policy_id != 0'. + ' ORDER BY spamfilter_users.priority DESC'; $sql_select_white_black_list = 'SELECT wb FROM spamfilter_wblist'. diff --git a/interface/lib/classes/remote.d/client.inc.php b/interface/lib/classes/remote.d/client.inc.php index 58dcc3119f..5af1592db9 100644 --- a/interface/lib/classes/remote.d/client.inc.php +++ b/interface/lib/classes/remote.d/client.inc.php @@ -413,7 +413,7 @@ class remoting_client extends remoting { $app->db->query("DELETE FROM sys_user WHERE client_id = ?", $client_id); //* Delete all records (sub-clients, mail, web, etc....) of this client. - $tables = 'cron,dns_rr,dns_soa,dns_slave,ftp_user,mail_access,mail_content_filter,mail_domain,mail_forwarding,mail_get,mail_user,mail_user_filter,shell_user,spamfilter_users,support_message,web_database,web_database_user,web_domain,web_traffic,domain,mail_mailinglist,client'; + $tables = 'cron,dns_rr,dns_soa,dns_slave,ftp_user,mail_access,mail_content_filter,mail_domain,mail_forwarding,mail_get,mail_user,mail_user_filter,shell_user,spamfilter_users,support_message,web_database,web_database_user,web_domain,web_traffic,domain,mail_mailinglist,client,spamfilter_wblist'; $tables_array = explode(',', $tables); $client_group_id = $app->functions->intval($client_group['groupid']); if($client_group_id > 1) { diff --git a/interface/lib/classes/remote.d/mail.inc.php b/interface/lib/classes/remote.d/mail.inc.php index c4582c634e..286d6f6c57 100644 --- a/interface/lib/classes/remote.d/mail.inc.php +++ b/interface/lib/classes/remote.d/mail.inc.php @@ -663,7 +663,7 @@ class remoting_mail extends remoting { return $app->remoting_lib->getDataRecord($primary_id); } - //* biała lista e-mail + //* add spamfilter whitelist entry public function mail_spamfilter_whitelist_add($session_id, $client_id, $params) { if (!$this->checkPerm($session_id, 'mail_spamfilter_whitelist_add')) @@ -763,7 +763,7 @@ class remoting_mail extends remoting { return $app->remoting_lib->getDataRecord($primary_id); } - //* filtr spamu użytkowników e-mail + //* Add new spamfilter_users public function mail_spamfilter_user_add($session_id, $client_id, $params) { if (!$this->checkPerm($session_id, 'mail_spamfilter_user_add')) diff --git a/interface/lib/plugins/mail_mail_domain_plugin.inc.php b/interface/lib/plugins/mail_mail_domain_plugin.inc.php index 598fe74f09..4ff756f44f 100644 --- a/interface/lib/plugins/mail_mail_domain_plugin.inc.php +++ b/interface/lib/plugins/mail_mail_domain_plugin.inc.php @@ -13,9 +13,10 @@ class mail_mail_domain_plugin { /* This function is called when the plugin is loaded - */ + */ function onLoad() { global $app; + //Register for the events $app->plugin->registerEvent('mail:mail_domain:on_after_insert', 'mail_mail_domain_plugin', 'mail_mail_domain_edit'); $app->plugin->registerEvent('mail:mail_domain:on_after_update', 'mail_mail_domain_plugin', 'mail_mail_domain_edit'); @@ -23,13 +24,13 @@ class mail_mail_domain_plugin { /* Function to create the sites_web_domain rule and insert it into the custom rules - */ + */ function mail_mail_domain_edit($event_name, $page_form) { global $app, $conf; $domain = $app->functions->idn_encode($page_form->dataRecord['domain']); - // make sure that the record belongs to the client group and not the admin group when a dmin inserts it + // make sure that the record belongs to the client group and not the admin group when admin inserts it // also make sure that the user can not delete entry created by an admin if($_SESSION["s"]["user"]["typ"] == 'admin' && isset($page_form->dataRecord["client_group_id"])) { $client_group_id = $app->functions->intval($page_form->dataRecord["client_group_id"]); @@ -64,6 +65,8 @@ class mail_mail_domain_plugin { $app->uses('getconf'); $mail_config = $app->getconf->get_server_config($page_form->dataRecord["server_id"], 'mail'); + $old_domain = $app->functions->idn_encode($page_form->oldDataRecord['domain']); + //* Update the mailboxes $mailusers = $app->db->queryAllRecords("SELECT * FROM mail_user WHERE email like ?", "%@" . $page_form->oldDataRecord['domain']); $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 @@ class mail_mail_domain_plugin { $maildir = str_replace("[domain]", $domain, $mail_config["maildir_path"]); $maildir = str_replace("[localpart]", $mail_parts[0], $maildir); $email = $mail_parts[0].'@'.$domain; + + // update spamfilter_users and spamfilter_wblist if email change + $skip_spamfilter_users_update = false; + if($email != $rec['email']) { + $tmp_olduser = $app->db->queryOneRecord("SELECT id,fullname FROM spamfilter_users WHERE email = ?", $rec['email']); + if($tmp_olduser['id'] > 0) { + $tmp_newuser = $app->db->queryOneRecord("SELECT id FROM spamfilter_users WHERE email = ?", $email); + if($tmp_newuser['id'] > 0) { + // There is a spamfilter_users for both old and new email, we'll update old wblist entries + $tmp_wblist = $app->db->queryAllRecords("SELECT wblist_id FROM spamfilter_wblist WHERE rid = ?", $tmp_olduser['id']); + foreach ($tmp_wblist as $tmp) { + $update_data = array( + 'rid' => $tmp_newuser['id'], + 'sys_userid' => $client_user_id, + 'sys_groupid' => $sys_groupid, + ); + $app->db->datalogUpdate('spamfilter_wblist', $update_data, 'wblist_id', $tmp['wblist_id']); + } + + // now delete old spamfilter_users entry + $app->db->datalogDelete('spamfilter_users', 'id', $tmp_olduser['id']); + } else { + $update_data = array( + 'email' => $email, + 'sys_userid' => $client_user_id, + 'sys_groupid' => $sys_groupid, + ); + if($tmp_olduser['fullname'] == $app->functions->idn_decode($rec['email'])) { + $update_data['fullname'] = $app->functions->idn_decode($email); + } + $app->db->datalogUpdate('spamfilter_users', $update_data, 'id', $tmp_olduser['id']); + $skip_spamfilter_users_update = true; + + $tmp_wblist = $app->db->queryAllRecords("SELECT wblist_id FROM spamfilter_wblist WHERE rid = ?", $tmp_olduser['id']); + $update_data = array( + 'sys_userid' => $client_user_id, + 'sys_groupid' => $sys_groupid, + ); + foreach ($tmp_wblist as $tmp) { + $app->db->datalogUpdate('spamfilter_wblist', $update_data, 'wblist_id', $tmp['wblist_id']); + } + } + } + + $tmp_user = $app->db->queryOneRecord("SELECT id FROM spamfilter_users WHERE email = ?", $email); + if($tmp_user["id"] > 0) { + // There is already a record that we will update + if(!$skip_spamfilter_users_update) { + $update_data = array( + 'sys_userid' => $client_user_id, + 'sys_groupid' => $sys_groupid, + ); + $app->db->datalogUpdate('spamfilter_users', $update_data, 'id', $tmp_user['id']); + } + } else { + // We create a new record + $insert_data = array( + "sys_userid" => $client_user_id, + "sys_groupid" => $sys_groupid, + "sys_perm_user" => 'riud', + "sys_perm_group" => 'riud', + "sys_perm_other" => '', + "server_id" => $page_form->dataRecord["server_id"], + "priority" => 5, + "policy_id" => 0, + "email" => $email, + "fullname" => $app->functions->idn_decode($email), + "local" => 'Y' + ); + $app->db->datalogInsert('spamfilter_users', $insert_data, 'id'); + } + } + $app->db->datalogUpdate('mail_user', array("maildir" => $maildir, "email" => $email, "sys_userid" => $client_user_id, "sys_groupid" => $sys_groupid), 'mailuser_id', $rec['mailuser_id']); + } } //* Update the aliases - $forwardings = $app->db->queryAllRecords("SELECT * FROM mail_forwarding WHERE source LIKE ? OR destination LIKE ?", "%@" . $page_form->oldDataRecord['domain'], "%@" . $page_form->oldDataRecord['domain']); + $forwardings = $app->db->queryAllRecords("SELECT * FROM mail_forwarding WHERE source like ? OR destination like ?", '%@' . $old_domain, '%@' . $old_domain); if(is_array($forwardings)) { foreach($forwardings as $rec) { - $destination = str_replace($page_form->oldDataRecord['domain'], $domain, $rec['destination']); - $source = str_replace($page_form->oldDataRecord['domain'], $domain, $rec['source']); + $destination = str_replace($old_domain, $domain, $rec['destination']); + $source = str_replace($old_domain, $domain, $rec['source']); + + // update spamfilter_users and spamfilter_wblist if source email changes + $skip_spamfilter_users_update = false; + if(strpos($rec['source'],'@'.$old_domain) && $source != $rec['source']) { + $tmp_olduser = $app->db->queryOneRecord("SELECT id,fullname FROM spamfilter_users WHERE email = ?", $rec['source']); + if($tmp_olduser['id'] > 0) { + $tmp_newuser = $app->db->queryOneRecord("SELECT id FROM spamfilter_users WHERE email = ?", $source); + if($tmp_newuser['id'] > 0) { + // There is a spamfilter_users for both old and new email, we'll update old wblist entries + $tmp_wblist = $app->db->queryAllRecords("SELECT wblist_id FROM spamfilter_wblist WHERE rid = ?", $tmp_olduser['id']); + foreach ($tmp_wblist as $tmp) { + $update_data = array( + 'rid' => $tmp_newuser['id'], + 'sys_userid' => $client_user_id, + 'sys_groupid' => $sys_groupid, + ); + $app->db->datalogUpdate('spamfilter_wblist', $update_data, 'wblist_id', $tmp['wblist_id']); + } + + // now delete old spamfilter_users entry + $app->db->datalogDelete('spamfilter_users', 'id', $tmp_olduser['id']); + } else { + $update_data = array( + 'email' => $source, + 'sys_userid' => $client_user_id, + 'sys_groupid' => $sys_groupid, + ); + if($tmp_olduser['fullname'] == $app->functions->idn_decode($rec['source'])) { + $update_data['fullname'] = $app->functions->idn_decode($source); + } + $app->db->datalogUpdate('spamfilter_users', $update_data, 'id', $tmp_olduser['id']); + $skip_spamfilter_users_update = true; + + $tmp_wblist = $app->db->queryAllRecords("SELECT wblist_id FROM spamfilter_wblist WHERE rid = ?", $tmp_olduser['id']); + $update_data = array( + 'sys_userid' => $client_user_id, + 'sys_groupid' => $sys_groupid, + ); + foreach ($tmp_wblist as $tmp) { + $app->db->datalogUpdate('spamfilter_wblist', $update_data, 'wblist_id', $tmp['wblist_id']); + } + } + } + + + $tmp_user = $app->db->queryOneRecord("SELECT id FROM spamfilter_users WHERE email = ?", $source); + if($tmp_user["id"] > 0) { + // There is already a record that we will update + if(!$skip_spamfilter_users_update) { + $update_data = array( + 'sys_userid' => $client_user_id, + 'sys_groupid' => $sys_groupid, + ); + $app->db->datalogUpdate('spamfilter_users', $update_data, 'id', $tmp_user['id']); + } + } + + } + $app->db->datalogUpdate('mail_forwarding', array("source" => $source, "destination" => $destination, "sys_userid" => $client_user_id, "sys_groupid" => $sys_groupid), 'forwarding_id', $rec['forwarding_id']); } } //* Update the mailinglist - $mailing_lists = $app->db->queryAllRecords("SELECT mailinglist_id FROM mail_mailinglist WHERE domain = ?", $page_form->oldDataRecord['domain']); - if(is_array($mailing_lists)) { - foreach($mailing_lists as $rec) { - $app->db->datalogUpdate('mail_mailinglist', array("sys_userid" => $client_user_id, "sys_groupid" => $sys_groupid), 'mailinglist_id', $rec['mailinglist_id']); + $mailinglists = $app->db->queryAllRecords("SELECT * FROM mail_mailinglist WHERE domain = ?", $old_domain); + if(is_array($mailinglists)) { + foreach($mailinglists as $rec) { + $update_data = array( + 'sys_userid' => $client_user_id, + 'sys_groupid' => $sys_groupid, + 'domain' => $domain, + 'email' => str_replace($old_domain, $domain, $rec['email']), + ); + $app->db->datalogUpdate('mail_mailinglist', $update_data, 'mailinglist_id', $rec['mailinglist_id']); } } @@ -107,25 +249,81 @@ class mail_mail_domain_plugin { } } - if ($page_form->oldDataRecord["domain"] != $domain) { - //* Delete the old spamfilter record - $tmp = $app->db->queryOneRecord("SELECT id FROM spamfilter_users WHERE email = ?", "@" . $page_form->oldDataRecord["domain"]); - $app->db->datalogDelete('spamfilter_users', 'id', $tmp["id"]); - unset($tmp); + // Spamfilter policy + $policy_id = $app->functions->intval($page_form->dataRecord["policy"]); + + // If domain changes, update spamfilter_users + // and fire spamfilter_wblist_update events so rspamd files are rewritten + if ($old_domain != $domain) { + $tmp_users = $app->db->queryOneRecord("SELECT id,fullname FROM spamfilter_users WHERE email LIKE ?", '%@' . $old_domain); + if(is_array($tmp_users)) { + foreach ($tmp_users as $tmp_old) { + $tmp_new = $app->db->queryOneRecord("SELECT id,fullname FROM spamfilter_users WHERE email = ?", '@' . $domain); + if($tmp_new['id'] > 0) { + // There is a spamfilter_users for both old and new domain, we'll update old wblist entries + $update_data = array( + 'sys_userid' => $client_user_id, + 'sys_groupid' => $sys_groupid, + 'rid' => $tmp_new['id'], + ); + $tmp_wblist = $app->db->queryAllRecords("SELECT wblist_id FROM spamfilter_wblist WHERE rid = ?", $tmp_old['id']); + foreach ($tmp_wblist as $tmp) { + $app->db->datalogUpdate('spamfilter_wblist', $update_data, 'wblist_id', $tmp['wblist_id']); + } + + // now delete old spamfilter_users entry + $app->db->datalogDelete('spamfilter_users', 'id', $tmp_old['id']); + + /// and update the new + $update_data = array( + 'sys_userid' => $client_user_id, + 'sys_groupid' => $sys_groupid, + ); + $app->db->datalogUpdate('spamfilter_users', $update_data, 'id', $tmp_old['id']); + } else { + $update_data = array( + 'sys_userid' => $client_user_id, + 'sys_groupid' => $sys_groupid, + 'email' => '@' . $domain, + ); + if($tmp_old['fullname'] == '@' . $old_domain) { + $update_data['fullname'] = '@' . $domain; + } + $app->db->datalogUpdate('spamfilter_users', $update_data, 'id', $tmp_old['id']); + } + } + } } - $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']); - } // end if domain name changed - - //* Force-update the aliases (required for spamfilter changes) - $forwardings = $app->db->queryAllRecords("SELECT * FROM mail_forwarding WHERE source LIKE ? OR destination LIKE ?", "%@" . $domain, "%@" . $domain); - - if(is_array($forwardings)) { - foreach($forwardings as $rec) { - $app->db->datalogUpdate('mail_forwarding', array("source" => $rec['source']), 'forwarding_id', $rec['forwarding_id'],true); + $tmp_user = $app->db->queryOneRecord("SELECT id FROM spamfilter_users WHERE email = ?", '@' . $domain); + if(isset($tmp_user['id']) && $tmp_user['id'] > 0) { + // There is already a record that we will update + if($policy_id != $tmp_user['policy_id']) { + $update_data = array( + 'policy_id' => $policy_id, + ); + $app->db->datalogUpdate('spamfilter_users', $update_data, 'id', $tmp_user["id"]); + } + } else { + // We create a new record + $insert_data = array( + "sys_userid" => $client_user_id, + "sys_groupid" => $sys_groupid, + "sys_perm_user" => 'riud', + "sys_perm_group" => 'riud', + "sys_perm_other" => '', + "server_id" => $page_form->dataRecord["server_id"], + "priority" => 5, + "policy_id" => $policy_id, + "email" => '@' . $domain, + "fullname" => '@' . $domain, + "local" => 'Y' + ); + $app->db->datalogInsert('spamfilter_users', $insert_data, 'id'); } - } - + + } // end if domain name changed + } } diff --git a/interface/web/client/client_del.php b/interface/web/client/client_del.php index c018b7a27d..dadcae0bdf 100644 --- a/interface/web/client/client_del.php +++ b/interface/web/client/client_del.php @@ -69,7 +69,8 @@ class page_action extends tform_actions { 'mail_user' => 'email', 'mail_user_filter' => '', 'shell_user' => 'username', - 'spamfilter_users' => '', 'spamfilter_wblist' => '', + 'spamfilter_users' => '', + 'spamfilter_wblist' => '', 'support_message' => '', 'web_domain' => 'domain', 'web_folder' => 'path', @@ -208,6 +209,5 @@ class page_action extends tform_actions { } $page = new page_action; -$page->onDelete() +$page->onDelete(); -?> diff --git a/interface/web/mail/form/spamfilter_users.tform.php b/interface/web/mail/form/spamfilter_users.tform.php index 65f196b987..1388217b64 100644 --- a/interface/web/mail/form/spamfilter_users.tform.php +++ b/interface/web/mail/form/spamfilter_users.tform.php @@ -78,7 +78,7 @@ $form["tabs"]['users'] = array ( 'policy_id' => array ( 'datatype' => 'INTEGER', 'formtype' => 'SELECT', - 'default' => '5', + 'default' => '', 'datasource' => array ( 'type' => 'SQL', 'querystring' => 'SELECT id,policy_name FROM spamfilter_policy WHERE {AUTHSQL} ORDER BY policy_name', 'keyfield'=> 'id', diff --git a/interface/web/mail/lib/lang/ar_spamfilter_users.lng b/interface/web/mail/lib/lang/ar_spamfilter_users.lng index efe95b5c7f..05d0ca9bd6 100644 --- a/interface/web/mail/lib/lang/ar_spamfilter_users.lng +++ b/interface/web/mail/lib/lang/ar_spamfilter_users.lng @@ -10,4 +10,4 @@ $wb['fullname_error_notempty'] = 'The name must not be empty.'; $wb['10 - highest'] = '10 - highest'; $wb['5 - medium'] = '5 - medium'; $wb['1 - lowest'] = '1 - lowest'; -?> +$wb['inherit_policy'] = '- Inherit domain setting -'; diff --git a/interface/web/mail/lib/lang/bg_spamfilter_users.lng b/interface/web/mail/lib/lang/bg_spamfilter_users.lng index efe95b5c7f..05d0ca9bd6 100644 --- a/interface/web/mail/lib/lang/bg_spamfilter_users.lng +++ b/interface/web/mail/lib/lang/bg_spamfilter_users.lng @@ -10,4 +10,4 @@ $wb['fullname_error_notempty'] = 'The name must not be empty.'; $wb['10 - highest'] = '10 - highest'; $wb['5 - medium'] = '5 - medium'; $wb['1 - lowest'] = '1 - lowest'; -?> +$wb['inherit_policy'] = '- Inherit domain setting -'; diff --git a/interface/web/mail/lib/lang/br_spamfilter_users.lng b/interface/web/mail/lib/lang/br_spamfilter_users.lng index d18fbc13a9..bdc989833e 100644 --- a/interface/web/mail/lib/lang/br_spamfilter_users.lng +++ b/interface/web/mail/lib/lang/br_spamfilter_users.lng @@ -10,3 +10,4 @@ $wb['fullname_error_notempty'] = 'Nome está vazio.'; $wb['10 - highest'] = '10 - alta'; $wb['5 - medium'] = '5 - média'; $wb['1 - lowest'] = '1 - baixa'; +$wb['inherit_policy'] = '- Herdar configuração de domínio -'; diff --git a/interface/web/mail/lib/lang/ca_spamfilter_users.lng b/interface/web/mail/lib/lang/ca_spamfilter_users.lng index 9c7d5bbe7a..a54e84e608 100644 --- a/interface/web/mail/lib/lang/ca_spamfilter_users.lng +++ b/interface/web/mail/lib/lang/ca_spamfilter_users.lng @@ -10,4 +10,4 @@ $wb['fullname_error_notempty'] = 'The name must not be empty.'; $wb['10 - highest'] = '10 - highest'; $wb['5 - medium'] = '5 - medium'; $wb['1 - lowest'] = '1 - lowest'; -?> +$wb['inherit_policy'] = '- Inherit domain setting -'; diff --git a/interface/web/mail/lib/lang/cz_spamfilter_users.lng b/interface/web/mail/lib/lang/cz_spamfilter_users.lng index f2fff323f4..8c66a07144 100644 --- a/interface/web/mail/lib/lang/cz_spamfilter_users.lng +++ b/interface/web/mail/lib/lang/cz_spamfilter_users.lng @@ -10,3 +10,4 @@ $wb['fullname_error_notempty'] = 'Jméno nesmí být prázdné.'; $wb['10 - highest'] = '10 - nejvyšší'; $wb['5 - medium'] = '5 - střední'; $wb['1 - lowest'] = '1 - nejnižší'; +$wb['inherit_policy'] = '- Zdědit nastavení od domény -'; diff --git a/interface/web/mail/lib/lang/de_spamfilter_users.lng b/interface/web/mail/lib/lang/de_spamfilter_users.lng index 9322c80466..d6b148cbe3 100644 --- a/interface/web/mail/lib/lang/de_spamfilter_users.lng +++ b/interface/web/mail/lib/lang/de_spamfilter_users.lng @@ -10,4 +10,4 @@ $wb['fullname_error_notempty'] = 'Der Name darf nicht leer sein.'; $wb['10 - highest'] = '10 - höchste'; $wb['5 - medium'] = '5 - normal'; $wb['1 - lowest'] = '1 - niedrigste'; -?> +$wb['inherit_policy'] = '- Inherit domain setting -'; diff --git a/interface/web/mail/lib/lang/dk_spamfilter_users.lng b/interface/web/mail/lib/lang/dk_spamfilter_users.lng index 5b132bc774..b6d9a226d0 100644 --- a/interface/web/mail/lib/lang/dk_spamfilter_users.lng +++ b/interface/web/mail/lib/lang/dk_spamfilter_users.lng @@ -10,4 +10,4 @@ $wb['fullname_error_notempty'] = 'Navn må ikke være tomt.'; $wb['10 - highest'] = '10 - højeste'; $wb['5 - medium'] = '5 - medium'; $wb['1 - lowest'] = '1 - laveste'; -?> +$wb['inherit_policy'] = '- Inherit domain setting -'; diff --git a/interface/web/mail/lib/lang/el_spamfilter_users.lng b/interface/web/mail/lib/lang/el_spamfilter_users.lng index b6da2d1e15..fc61aa818d 100644 --- a/interface/web/mail/lib/lang/el_spamfilter_users.lng +++ b/interface/web/mail/lib/lang/el_spamfilter_users.lng @@ -10,4 +10,4 @@ $wb['fullname_error_notempty'] = 'The name must not be empty.'; $wb['10 - highest'] = '10 - highest'; $wb['5 - medium'] = '5 - medium'; $wb['1 - lowest'] = '1 - lowest'; -?> +$wb['inherit_policy'] = '- Inherit domain setting -'; diff --git a/interface/web/mail/lib/lang/en_spamfilter_users.lng b/interface/web/mail/lib/lang/en_spamfilter_users.lng index e43b315df6..05d0ca9bd6 100644 --- a/interface/web/mail/lib/lang/en_spamfilter_users.lng +++ b/interface/web/mail/lib/lang/en_spamfilter_users.lng @@ -10,4 +10,4 @@ $wb['fullname_error_notempty'] = 'The name must not be empty.'; $wb['10 - highest'] = '10 - highest'; $wb['5 - medium'] = '5 - medium'; $wb['1 - lowest'] = '1 - lowest'; -?> \ No newline at end of file +$wb['inherit_policy'] = '- Inherit domain setting -'; diff --git a/interface/web/mail/lib/lang/es_spamfilter_users.lng b/interface/web/mail/lib/lang/es_spamfilter_users.lng index c5a5a0f948..575c14a9f4 100644 --- a/interface/web/mail/lib/lang/es_spamfilter_users.lng +++ b/interface/web/mail/lib/lang/es_spamfilter_users.lng @@ -10,4 +10,4 @@ $wb['local_txt'] = 'Local'; $wb['policy_id_txt'] = 'Directiva'; $wb['priority_txt'] = 'Prioridad'; $wb['server_id_txt'] = 'Servidor'; -?> +$wb['inherit_policy'] = '- Inherit domain setting -'; diff --git a/interface/web/mail/lib/lang/fi_spamfilter_users.lng b/interface/web/mail/lib/lang/fi_spamfilter_users.lng index c4290efbf2..5c9570d6fb 100644 --- a/interface/web/mail/lib/lang/fi_spamfilter_users.lng +++ b/interface/web/mail/lib/lang/fi_spamfilter_users.lng @@ -10,4 +10,4 @@ $wb['fullname_error_notempty'] = 'The name must not be empty.'; $wb['10 - highest'] = '10 - highest'; $wb['5 - medium'] = '5 - medium'; $wb['1 - lowest'] = '1 - lowest'; -?> +$wb['inherit_policy'] = '- Inherit domain setting -'; diff --git a/interface/web/mail/lib/lang/fr_spamfilter_users.lng b/interface/web/mail/lib/lang/fr_spamfilter_users.lng index e8936b0183..b87fe87ccb 100644 --- a/interface/web/mail/lib/lang/fr_spamfilter_users.lng +++ b/interface/web/mail/lib/lang/fr_spamfilter_users.lng @@ -10,4 +10,4 @@ $wb['fullname_error_notempty'] = 'The name must not be empty.'; $wb['10 - highest'] = '10 - highest'; $wb['5 - medium'] = '5 - medium'; $wb['1 - lowest'] = '1 - lowest'; -?> +$wb['inherit_policy'] = '- Inherit domain setting -'; diff --git a/interface/web/mail/lib/lang/hr_spamfilter_users.lng b/interface/web/mail/lib/lang/hr_spamfilter_users.lng index 9969ef421d..9e78310d71 100644 --- a/interface/web/mail/lib/lang/hr_spamfilter_users.lng +++ b/interface/web/mail/lib/lang/hr_spamfilter_users.lng @@ -10,4 +10,4 @@ $wb['fullname_error_notempty'] = 'The name must not be empty.'; $wb['10 - highest'] = '10 - highest'; $wb['5 - medium'] = '5 - medium'; $wb['1 - lowest'] = '1 - lowest'; -?> +$wb['inherit_policy'] = '- Inherit domain setting -'; diff --git a/interface/web/mail/lib/lang/hu_spamfilter_users.lng b/interface/web/mail/lib/lang/hu_spamfilter_users.lng index 573d696182..a5097ddbfd 100644 --- a/interface/web/mail/lib/lang/hu_spamfilter_users.lng +++ b/interface/web/mail/lib/lang/hu_spamfilter_users.lng @@ -10,4 +10,4 @@ $wb['fullname_error_notempty'] = 'The name must not be empty.'; $wb['10 - highest'] = '10 - highest'; $wb['5 - medium'] = '5 - medium'; $wb['1 - lowest'] = '1 - lowest'; -?> +$wb['inherit_policy'] = '- Inherit domain setting -'; diff --git a/interface/web/mail/lib/lang/id_spamfilter_users.lng b/interface/web/mail/lib/lang/id_spamfilter_users.lng index f4549c5c46..4777d33581 100644 --- a/interface/web/mail/lib/lang/id_spamfilter_users.lng +++ b/interface/web/mail/lib/lang/id_spamfilter_users.lng @@ -10,4 +10,4 @@ $wb['fullname_error_notempty'] = 'The name must not be empty.'; $wb['10 - highest'] = '10 - highest'; $wb['5 - medium'] = '5 - medium'; $wb['1 - lowest'] = '1 - lowest'; -?> +$wb['inherit_policy'] = '- Inherit domain setting -'; diff --git a/interface/web/mail/lib/lang/it_spamfilter_users.lng b/interface/web/mail/lib/lang/it_spamfilter_users.lng index cebb20324e..2f49b05a41 100644 --- a/interface/web/mail/lib/lang/it_spamfilter_users.lng +++ b/interface/web/mail/lib/lang/it_spamfilter_users.lng @@ -10,4 +10,4 @@ $wb['fullname_error_notempty'] = 'The name must not be vuoto.'; $wb['10 - highest'] = '10 - elevata'; $wb['5 - medium'] = '5 - media'; $wb['1 - lowest'] = '1 - minima'; -?> +$wb['inherit_policy'] = '- Inherit domain setting -'; diff --git a/interface/web/mail/lib/lang/ja_spamfilter_users.lng b/interface/web/mail/lib/lang/ja_spamfilter_users.lng index cd212e9418..5272d3f3e1 100644 --- a/interface/web/mail/lib/lang/ja_spamfilter_users.lng +++ b/interface/web/mail/lib/lang/ja_spamfilter_users.lng @@ -10,4 +10,4 @@ $wb['fullname_error_notempty'] = 'The name must not be empty.'; $wb['10 - highest'] = '10 - highest'; $wb['5 - medium'] = '5 - medium'; $wb['1 - lowest'] = '1 - lowest'; -?> +$wb['inherit_policy'] = '- Inherit domain setting -'; diff --git a/interface/web/mail/lib/lang/nl_spamfilter_users.lng b/interface/web/mail/lib/lang/nl_spamfilter_users.lng index 697e130b35..b9da8e7e4a 100644 --- a/interface/web/mail/lib/lang/nl_spamfilter_users.lng +++ b/interface/web/mail/lib/lang/nl_spamfilter_users.lng @@ -10,4 +10,4 @@ $wb['fullname_error_notempty'] = 'The name must not be empty.'; $wb['10 - highest'] = '10 - highest'; $wb['5 - medium'] = '5 - medium'; $wb['1 - lowest'] = '1 - lowest'; -?> +$wb['inherit_policy'] = '- Inherit domain setting -'; diff --git a/interface/web/mail/lib/lang/pl_spamfilter_users.lng b/interface/web/mail/lib/lang/pl_spamfilter_users.lng index c46589b18d..5173099c27 100644 --- a/interface/web/mail/lib/lang/pl_spamfilter_users.lng +++ b/interface/web/mail/lib/lang/pl_spamfilter_users.lng @@ -10,4 +10,4 @@ $wb['fullname_error_notempty'] = 'The name must not be empty.'; $wb['10 - highest'] = '10 - highest'; $wb['5 - medium'] = '5 - medium'; $wb['1 - lowest'] = '1 - lowest'; -?> +$wb['inherit_policy'] = '- Inherit domain setting -'; diff --git a/interface/web/mail/lib/lang/pt_spamfilter_users.lng b/interface/web/mail/lib/lang/pt_spamfilter_users.lng index 0ee8d37d50..adec31f8a1 100644 --- a/interface/web/mail/lib/lang/pt_spamfilter_users.lng +++ b/interface/web/mail/lib/lang/pt_spamfilter_users.lng @@ -10,4 +10,4 @@ $wb['fullname_error_notempty'] = 'The name must not be empty.'; $wb['10 - highest'] = '10 - highest'; $wb['5 - medium'] = '5 - medium'; $wb['1 - lowest'] = '1 - lowest'; -?> +$wb['inherit_policy'] = '- Inherit domain setting -'; diff --git a/interface/web/mail/lib/lang/ro_spamfilter_users.lng b/interface/web/mail/lib/lang/ro_spamfilter_users.lng index c51b0b8182..bdc418c92c 100644 --- a/interface/web/mail/lib/lang/ro_spamfilter_users.lng +++ b/interface/web/mail/lib/lang/ro_spamfilter_users.lng @@ -10,4 +10,4 @@ $wb['fullname_error_notempty'] = 'The name must not be empty.'; $wb['10 - highest'] = '10 - highest'; $wb['5 - medium'] = '5 - medium'; $wb['1 - lowest'] = '1 - lowest'; -?> +$wb['inherit_policy'] = '- Inherit domain setting -'; diff --git a/interface/web/mail/lib/lang/ru_spamfilter_users.lng b/interface/web/mail/lib/lang/ru_spamfilter_users.lng index 8fcfff421b..238f3b7048 100644 --- a/interface/web/mail/lib/lang/ru_spamfilter_users.lng +++ b/interface/web/mail/lib/lang/ru_spamfilter_users.lng @@ -10,4 +10,4 @@ $wb['fullname_error_notempty'] = 'Имя не может быть пустым.' $wb['10 - highest'] = '10 - сильный'; $wb['5 - medium'] = '5 - средний'; $wb['1 - lowest'] = '1 - слабый'; -?> +$wb['inherit_policy'] = '- Inherit domain setting -'; diff --git a/interface/web/mail/lib/lang/se_spamfilter_users.lng b/interface/web/mail/lib/lang/se_spamfilter_users.lng index ba3b60e273..ca3fac06c2 100644 --- a/interface/web/mail/lib/lang/se_spamfilter_users.lng +++ b/interface/web/mail/lib/lang/se_spamfilter_users.lng @@ -10,4 +10,4 @@ $wb['fullname_error_notempty'] = 'Fältet för namn kan inte vara tomt.'; $wb['10 - highest'] = '10 - högsta'; $wb['5 - medium'] = '5 - medium'; $wb['1 - lowest'] = '1 - lägsta'; -?> +$wb['inherit_policy'] = '- Inherit domain setting -'; diff --git a/interface/web/mail/lib/lang/sk_spamfilter_users.lng b/interface/web/mail/lib/lang/sk_spamfilter_users.lng index 89bc415672..dfd74a3291 100644 --- a/interface/web/mail/lib/lang/sk_spamfilter_users.lng +++ b/interface/web/mail/lib/lang/sk_spamfilter_users.lng @@ -10,4 +10,4 @@ $wb['fullname_error_notempty'] = 'The name must not be empty.'; $wb['10 - highest'] = '10 - highest'; $wb['5 - medium'] = '5 - medium'; $wb['1 - lowest'] = '1 - lowest'; -?> +$wb['inherit_policy'] = '- Inherit domain setting -'; diff --git a/interface/web/mail/lib/lang/tr_spamfilter_users.lng b/interface/web/mail/lib/lang/tr_spamfilter_users.lng index 33ef04c610..10144b8232 100644 --- a/interface/web/mail/lib/lang/tr_spamfilter_users.lng +++ b/interface/web/mail/lib/lang/tr_spamfilter_users.lng @@ -10,4 +10,4 @@ $wb['fullname_error_notempty'] = 'Ad boş olamaz.'; $wb['10 - highest'] = '10 - en yüksek'; $wb['5 - medium'] = '5 - orta'; $wb['1 - lowest'] = '1 - en düşük'; -?> +$wb['inherit_policy'] = '- Inherit domain setting -'; diff --git a/interface/web/mail/mail_domain_del.php b/interface/web/mail/mail_domain_del.php index bce89695dc..e4c26399ef 100644 --- a/interface/web/mail/mail_domain_del.php +++ b/interface/web/mail/mail_domain_del.php @@ -80,6 +80,10 @@ class page_action extends tform_actions { // Delete all spamfilters that belong to this domain $records = $app->db->queryAllRecords("SELECT id FROM spamfilter_users WHERE email like ?", '%@' . $domain); foreach($records as $rec) { + $wblists = $app->db->queryAllRecords("SELECT wblist_id FROM spamfilter_wblist WHERE rid = ?", $rec['id']); + foreach($wblists as $wblist) { + $app->db->datalogDelete('spamfilter_wblist', 'wblist_id', $wblist['wblist_id']); + } $app->db->datalogDelete('spamfilter_users', 'id', $rec['id']); } diff --git a/interface/web/mail/mail_domain_edit.php b/interface/web/mail/mail_domain_edit.php index 36185ead69..48f6eb2e4d 100644 --- a/interface/web/mail/mail_domain_edit.php +++ b/interface/web/mail/mail_domain_edit.php @@ -190,19 +190,19 @@ class page_action extends tform_actions { $app->tpl->setVar("domain_module", 0); } - // Get the spamfilter policys for the user + // Get the spamfilter policies for the user $tmp_user = $app->db->queryOneRecord("SELECT policy_id FROM spamfilter_users WHERE email = ?", '@' . $this->dataRecord["domain"]); $sql = "SELECT id, policy_name FROM spamfilter_policy WHERE ".$app->tform->getAuthSQL('r')." ORDER BY policy_name"; - $policys = $app->db->queryAllRecords($sql); - $policy_select = ""; - if(is_array($policys)) { - foreach( $policys as $p) { + $policies = $app->db->queryAllRecords($sql); + $policy_select = ""; + if(is_array($policies)) { + foreach( $policies as $p) { $selected = ($p["id"] == $tmp_user["policy_id"])?'SELECTED':''; $policy_select .= "\r\n"; } } $app->tpl->setVar("policy", $policy_select); - unset($policys); + unset($policies); unset($policy_select); unset($tmp_user); @@ -257,7 +257,7 @@ class page_action extends tform_actions { if (!empty($rec['dkim_public'])) $app->tpl->setVar('dns_record', $dns_record, true); parent::onShowEnd(); - } + } function onSubmit() { global $app, $conf; @@ -334,30 +334,30 @@ class page_action extends tform_actions { // Spamfilter policy $policy_id = $app->functions->intval($this->dataRecord["policy"]); - if($policy_id > 0) { - $tmp_user = $app->db->queryOneRecord("SELECT id FROM spamfilter_users WHERE email = ?", '@' . $domain); - if($tmp_user["id"] > 0) { - // There is already a record that we will update + $tmp_user = $app->db->queryOneRecord("SELECT id, policy_id FROM spamfilter_users WHERE email = ?", '@' . $domain); + if($tmp_user["id"] > 0) { + // There is already a record that we will update + if($policy_id != $tmp_user['policy_id']) { $app->db->datalogUpdate('spamfilter_users', array("policy_id" => $policy_id), 'id', $tmp_user["id"]); - } else { - $tmp_domain = $app->db->queryOneRecord("SELECT sys_groupid FROM mail_domain WHERE domain_id = ?", $this->id); - // We create a new record - $insert_data = array( - "sys_userid" => $_SESSION["s"]["user"]["userid"], - "sys_groupid" => $tmp_domain["sys_groupid"], - "sys_perm_user" => 'riud', - "sys_perm_group" => 'riud', - "sys_perm_other" => '', - "server_id" => $this->dataRecord["server_id"], - "priority" => 5, - "policy_id" => $policy_id, - "email" => '@' . $domain, - "fullname" => '@' . $domain, - "local" => 'Y' - ); - $app->db->datalogInsert('spamfilter_users', $insert_data, 'id'); - unset($tmp_domain); } + } else { + $tmp_domain = $app->db->queryOneRecord("SELECT sys_groupid FROM mail_domain WHERE domain_id = ?", $this->id); + // We create a new record + $insert_data = array( + "sys_userid" => $_SESSION["s"]["user"]["userid"], + "sys_groupid" => $tmp_domain["sys_groupid"], + "sys_perm_user" => 'riud', + "sys_perm_group" => 'riud', + "sys_perm_other" => '', + "server_id" => $this->dataRecord["server_id"], + "priority" => 5, + "policy_id" => $policy_id, + "email" => '@' . $domain, + "fullname" => '@' . $domain, + "local" => 'Y' + ); + $app->db->datalogInsert('spamfilter_users', $insert_data, 'id'); + unset($tmp_domain); } // endif spamfilter policy //* create dns-record with dkim-values if the zone exists @@ -419,7 +419,7 @@ class page_action extends tform_actions { $tmp_new = $app->db->queryOneRecord("SELECT id,fullname FROM spamfilter_users WHERE email = ?", '@' . $domain); if($tmp_new['id'] > 0) { // There is a spamfilter_users for both old and new domain, we'll update old wblist entries - $tmp_wblist = $app->db->queryAllRecords("SELECT wblist_id FROM spamfilter_users WHERE rid = ?", $tmp_old['id']); + $tmp_wblist = $app->db->queryAllRecords("SELECT wblist_id FROM spamfilter_wblist WHERE rid = ?", $tmp_old['id']); foreach ($tmp_wblist as $tmp) { $app->db->datalogUpdate('spamfilter_wblist', array('rid' => $tmp_new['id']), 'wblist_id', $tmp['wblist_id']); } @@ -441,39 +441,31 @@ class page_action extends tform_actions { } $tmp_user = $app->db->queryOneRecord("SELECT id FROM spamfilter_users WHERE email = ?", '@' . $domain); - if($policy_id > 0) { - if($tmp_user["id"] > 0) { - // There is already a record that we will update - if(!$skip_spamfilter_users_update) { - $app->db->datalogUpdate('spamfilter_users', array("policy_id" => $policy_id), 'id', $tmp_user["id"]); - } - } else { - $tmp_domain = $app->db->queryOneRecord("SELECT sys_groupid FROM mail_domain WHERE domain_id = ?", $this->id); - // We create a new record - $insert_data = array( - "sys_userid" => $_SESSION["s"]["user"]["userid"], - "sys_groupid" => $tmp_domain["sys_groupid"], - "sys_perm_user" => 'riud', - "sys_perm_group" => 'riud', - "sys_perm_other" => '', - "server_id" => $this->dataRecord["server_id"], - "priority" => 5, - "policy_id" => $policy_id, - "email" => '@' . $domain, - "fullname" => '@' . $domain, - "local" => 'Y' - ); - $app->db->datalogInsert('spamfilter_users', $insert_data, 'id'); - unset($tmp_domain); + if($tmp_user["id"] > 0) { + // There is already a record that we will update + if((! $skip_spamfilter_users_update) && ($policy_id != $tmp_user['policy_id'])) { + $app->db->datalogUpdate('spamfilter_users', array("policy_id" => $policy_id), 'id', $tmp_user["id"]); } } else { - if($tmp_user["id"] > 0) { - // There is already a record but the user shall have no policy, so we delete it -// fixme: this abandons any spamfilter_wblist entries tied to this spamfilter_users id -// https://git.ispconfig.org/ispconfig/ispconfig3/-/issues/6201 - $app->db->datalogDelete('spamfilter_users', 'id', $tmp_user["id"]); - } + $tmp_domain = $app->db->queryOneRecord("SELECT sys_groupid FROM mail_domain WHERE domain_id = ?", $this->id); + // We create a new record + $insert_data = array( + "sys_userid" => $_SESSION["s"]["user"]["userid"], + "sys_groupid" => $tmp_domain["sys_groupid"], + "sys_perm_user" => 'riud', + "sys_perm_group" => 'riud', + "sys_perm_other" => '', + "server_id" => $this->dataRecord["server_id"], + "priority" => 5, + "policy_id" => $policy_id, + "email" => '@' . $domain, + "fullname" => '@' . $domain, + "local" => 'Y' + ); + $app->db->datalogInsert('spamfilter_users', $insert_data, 'id'); + unset($tmp_domain); } // endif spamfilter policy + //** If the domain name or owner has been changed, change the domain and owner in all mailbox records if($old_domain != $domain || (isset($this->dataRecord['client_group_id']) && $this->oldDataRecord['sys_groupid'] != $this->dataRecord['client_group_id'])) { $app->uses('getconf'); @@ -493,14 +485,13 @@ class page_action extends tform_actions { $email = $mail_parts[0].'@'.$this->dataRecord['domain']; // update spamfilter_users and spamfilter_wblist if email change $skip_spamfilter_users_update = false; - if($email != $mail_parts[0].'@'.$this->oldDataRecord['domain']) { - $tmp_olduser = $app->db->queryOneRecord("SELECT id,fullname FROM spamfilter_users WHERE email = ?", $mail_parts[0].'@'.$this->oldDataRecord['domain']); + if($email != $rec['email']) { + $tmp_olduser = $app->db->queryOneRecord("SELECT id,fullname FROM spamfilter_users WHERE email = ?", $rec['email']); if($tmp_olduser['id'] > 0) { - - $tmp_newuser = $app->db->queryOneRecord("SELECT id FROM spamfilter_users WHERE email = ?", $mail_parts[0].'@'.$this->dataRecord['domain']); + $tmp_newuser = $app->db->queryOneRecord("SELECT id FROM spamfilter_users WHERE email = ?", $email); if($tmp_newuser['id'] > 0) { // There is a spamfilter_users for both old and new email, we'll update old wblist entries - $tmp_wblist = $app->db->queryAllRecords("SELECT wblist_id FROM spamfilter_users WHERE rid = ?", $tmp_olduser['id']); + $tmp_wblist = $app->db->queryAllRecords("SELECT wblist_id FROM spamfilter_wblist WHERE rid = ?", $tmp_olduser['id']); foreach ($tmp_wblist as $tmp) { $update_data = array( 'rid' => $tmp_newuser['id'], @@ -518,13 +509,13 @@ class page_action extends tform_actions { 'sys_userid' => $client_user_id, 'sys_groupid' => $sys_groupid, ); - if($tmp_olduser['fullname'] == $app->functions->idn_decode($mail_parts[0].'@'.$this->oldDataRecord['domain'])) { - $update_data['fullname'] = $app->functions->idn_decode($mail_parts[0].'@'.$this->dataRecord['domain']); + if($tmp_olduser['fullname'] == $app->functions->idn_decode($rec['email'])) { + $update_data['fullname'] = $app->functions->idn_decode($email); } $app->db->datalogUpdate('spamfilter_users', $update_data, 'id', $tmp_olduser['id']); $skip_spamfilter_users_update = true; - $tmp_wblist = $app->db->queryAllRecords("SELECT wblist_id FROM spamfilter_users WHERE rid = ?", $tmp_olduser['id']); + $tmp_wblist = $app->db->queryAllRecords("SELECT wblist_id FROM spamfilter_wblist WHERE rid = ?", $tmp_olduser['id']); $update_data = array( 'sys_userid' => $client_user_id, 'sys_groupid' => $sys_groupid, @@ -535,7 +526,7 @@ class page_action extends tform_actions { } } - $tmp_user = $app->db->queryOneRecord("SELECT id FROM spamfilter_users WHERE email = ?", $mail_parts[0].'@'.$this->dataRecord['domain']); + $tmp_user = $app->db->queryOneRecord("SELECT id FROM spamfilter_users WHERE email = ?", $email); if($tmp_user["id"] > 0) { // There is already a record that we will update if(!$skip_spamfilter_users_update) { @@ -545,11 +536,22 @@ class page_action extends tform_actions { ); $app->db->datalogUpdate('spamfilter_users', $update_data, 'id', $tmp_user['id']); } - /* } else { - # fixme: insert spamfilter_users with correct policy_id, - # pending https://git.ispconfig.org/ispconfig/ispconfig3/-/issues/6201 - */ + // We create a new record + $insert_data = array( + "sys_userid" => $client_user_id, + "sys_groupid" => $sys_groupid, + "sys_perm_user" => 'riud', + "sys_perm_group" => 'riud', + "sys_perm_other" => '', + "server_id" => $this->dataRecord["server_id"], + "priority" => 5, + "policy_id" => 0, + "email" => $email, + "fullname" => $app->functions->idn_decode($email), + "local" => 'Y' + ); + $app->db->datalogInsert('spamfilter_users', $insert_data, 'id'); } $app->db->datalogUpdate('mail_user', array("maildir" => $maildir, "email" => $email, "sys_userid" => $client_user_id, "sys_groupid" => $sys_groupid), 'mailuser_id', $rec['mailuser_id']); @@ -564,15 +566,15 @@ class page_action extends tform_actions { $destination = str_replace($old_domain, $domain, $rec['destination']); $source = str_replace($old_domain, $domain, $rec['source']); - // update spamfilter_users and spamfilter_wblist if email change + // update spamfilter_users and spamfilter_wblist if source email changes $skip_spamfilter_users_update = false; - if($source != $rec['source']) { + if(strpos($rec['source'],'@'.$old_domain) && $source != $rec['source']) { $tmp_olduser = $app->db->queryOneRecord("SELECT id,fullname FROM spamfilter_users WHERE email = ?", $rec['source']); if($tmp_olduser['id'] > 0) { $tmp_newuser = $app->db->queryOneRecord("SELECT id FROM spamfilter_users WHERE email = ?", $source); if($tmp_newuser['id'] > 0) { // There is a spamfilter_users for both old and new email, we'll update old wblist entries - $tmp_wblist = $app->db->queryAllRecords("SELECT wblist_id FROM spamfilter_users WHERE rid = ?", $tmp_olduser['id']); + $tmp_wblist = $app->db->queryAllRecords("SELECT wblist_id FROM spamfilter_wblist WHERE rid = ?", $tmp_olduser['id']); foreach ($tmp_wblist as $tmp) { $update_data = array( 'rid' => $tmp_newuser['id'], @@ -596,7 +598,7 @@ class page_action extends tform_actions { $app->db->datalogUpdate('spamfilter_users', $update_data, 'id', $tmp_olduser['id']); $skip_spamfilter_users_update = true; - $tmp_wblist = $app->db->queryAllRecords("SELECT wblist_id FROM spamfilter_users WHERE rid = ?", $tmp_olduser['id']); + $tmp_wblist = $app->db->queryAllRecords("SELECT wblist_id FROM spamfilter_wblist WHERE rid = ?", $tmp_olduser['id']); $update_data = array( 'sys_userid' => $client_user_id, 'sys_groupid' => $sys_groupid, @@ -607,6 +609,7 @@ class page_action extends tform_actions { } } + $tmp_user = $app->db->queryOneRecord("SELECT id FROM spamfilter_users WHERE email = ?", $source); if($tmp_user["id"] > 0) { // There is already a record that we will update @@ -618,9 +621,25 @@ class page_action extends tform_actions { $app->db->datalogUpdate('spamfilter_users', $update_data, 'id', $tmp_user['id']); } /* + * should we insert spamfilter_users with policy_id = 0 for mail_forwardings? + * I think no (see https://git.ispconfig.org/ispconfig/ispconfig3/-/issues/6201) + * } else { - # fixme: insert spamfilter_users with correct policy_id, - # pending https://git.ispconfig.org/ispconfig/ispconfig3/-/issues/6201 + // We create a new record + $insert_data = array( + "sys_userid" => $client_user_id, + "sys_groupid" => $sys_groupid, + "sys_perm_user" => 'riud', + "sys_perm_group" => 'riud', + "sys_perm_other" => '', + "server_id" => $this->dataRecord["server_id"], + "priority" => 5, + "policy_id" => 0, + "email" => $source, + "fullname" => $app->functions->idn_decode($source), + "local" => 'Y' + ); + $app->db->datalogInsert('spamfilter_users', $insert_data, 'id'); */ } diff --git a/interface/web/mail/mail_user_del.php b/interface/web/mail/mail_user_del.php index 1f19166ef7..d301f50085 100644 --- a/interface/web/mail/mail_user_del.php +++ b/interface/web/mail/mail_user_del.php @@ -65,6 +65,8 @@ class page_action extends tform_actions { } $app->db->datalogDelete('spamfilter_users', 'id', $tmp_user["id"]); + // delete mail_forwardings with destination == email ? + $tmp_filters = $app->db->queryAllRecords("SELECT filter_id FROM mail_user_filter WHERE mailuser_id = ?", $this->id); if(is_array($tmp_filters)) { foreach($tmp_filters as $tmp) { diff --git a/interface/web/mail/mail_user_edit.php b/interface/web/mail/mail_user_edit.php index 30c6d15000..f13c5845a5 100644 --- a/interface/web/mail/mail_user_edit.php +++ b/interface/web/mail/mail_user_edit.php @@ -94,20 +94,20 @@ class page_action extends tform_actions { unset($domains); unset($domain_select); - // Get the spamfilter policys for the user + // Get the spamfilter policies for the user $tmp_user = $app->db->queryOneRecord("SELECT policy_id FROM spamfilter_users WHERE email = ?", $this->dataRecord["email"]); if (isset($_POST['policy'])) $tmp_user['policy_id'] = intval($_POST['policy']); $sql = "SELECT id, policy_name FROM spamfilter_policy WHERE ".$app->tform->getAuthSQL('r') . " ORDER BY policy_name"; - $policys = $app->db->queryAllRecords($sql); - $policy_select = ""; - if(is_array($policys)) { - foreach( $policys as $p) { + $policies = $app->db->queryAllRecords($sql); + $policy_select = ""; + if(is_array($policies)) { + foreach( $policies as $p) { $selected = ($p["id"] == $tmp_user["policy_id"])?'SELECTED':''; $policy_select .= "\r\n"; } } $app->tpl->setVar("policy", $policy_select); - unset($policys); + unset($policies); unset($policy_select); unset($tmp_user); @@ -281,42 +281,40 @@ class page_action extends tform_actions { // Spamfilter policy $policy_id = $app->functions->intval($this->dataRecord["policy"]); - if($policy_id > 0) { - $tmp_user = $app->db->queryOneRecord("SELECT id FROM spamfilter_users WHERE email = ?", $this->dataRecord["email"]); - if($tmp_user["id"] > 0) { - // There is already a record that we will update + $tmp_user = $app->db->queryOneRecord("SELECT id, policy_id FROM spamfilter_users WHERE email = ?", $this->dataRecord["email"]); + if($tmp_user["id"] > 0) { + // There is already a record that we will update + if($policy_id != $tmp_user['policy_id']) { $app->db->datalogUpdate('spamfilter_users', array("policy_id" => $policy_id), 'id', $tmp_user["id"]); - } else { - // We create a new record - $insert_data = array( - "sys_userid" => $_SESSION["s"]["user"]["userid"], - "sys_groupid" => $domain["sys_groupid"], - "sys_perm_user" => 'riud', - "sys_perm_group" => 'riud', - "sys_perm_other" => '', - "server_id" => $domain["server_id"], - "priority" => 10, - "policy_id" => $policy_id, - "email" => $this->dataRecord["email"], - "fullname" => $app->functions->idn_decode($this->dataRecord["email"]), - "local" => 'Y' - ); - $app->db->datalogInsert('spamfilter_users', $insert_data, 'id'); } - } // endif spamfilter policy + } else { + // We create a new record + $insert_data = array( + "sys_userid" => $_SESSION["s"]["user"]["userid"], + "sys_groupid" => $domain["sys_groupid"], + "sys_perm_user" => 'riud', + "sys_perm_group" => 'riud', + "sys_perm_other" => '', + "server_id" => $domain["server_id"], + "priority" => 5, + "policy_id" => $policy_id, + "email" => $this->dataRecord["email"], + "fullname" => $app->functions->idn_decode($this->dataRecord["email"]), + "local" => 'Y' + ); + $app->db->datalogInsert('spamfilter_users', $insert_data, 'id'); + } // Set the fields for dovecot - if(isset($this->dataRecord["email"])) { - $disableimap = ($this->dataRecord["disableimap"])?'y':'n'; - $disablepop3 = ($this->dataRecord["disablepop3"])?'y':'n'; - $disablesmtp = ($this->dataRecord["disablesmtp"])?'y':'n'; - $disabledeliver = ($this->dataRecord["disabledeliver"])?'y':'n'; - - $app->db->query($sql, $disableimap, $disableimap, $disablepop3, $disablesmtp, $disabledeliver, $disabledeliver, $this->id); - $sql = "UPDATE mail_user SET disableimap = ?, disablesieve = ?, disablepop3 = ?, disablesmtp = ?, disabledeliver = ?, disablelda = ?, disablelmtp = ? WHERE mailuser_id = ?"; - $app->db->query($sql, $disableimap, $disableimap, $disablepop3, $disablesmtp, $disabledeliver, $disabledeliver, $disabledeliver, $this->id); - } + $disableimap = ($this->dataRecord["disableimap"])?'y':'n'; + $disablepop3 = ($this->dataRecord["disablepop3"])?'y':'n'; + $disablesmtp = ($this->dataRecord["disablesmtp"])?'y':'n'; + $disabledeliver = ($this->dataRecord["disabledeliver"])?'y':'n'; + + $app->db->query($sql, $disableimap, $disableimap, $disablepop3, $disablesmtp, $disabledeliver, $disabledeliver, $this->id); + $sql = "UPDATE mail_user SET disableimap = ?, disablesieve = ?, disablepop3 = ?, disablesmtp = ?, disabledeliver = ?, disablelda = ?, disablelmtp = ? WHERE mailuser_id = ?"; + $app->db->query($sql, $disableimap, $disableimap, $disablepop3, $disablesmtp, $disabledeliver, $disabledeliver, $disabledeliver, $this->id); } function onAfterUpdate() { @@ -326,43 +324,44 @@ class page_action extends tform_actions { if(isset($_POST["email_domain"])) { $domain = $app->db->queryOneRecord("SELECT sys_groupid, server_id FROM mail_domain WHERE domain = ? AND ".$app->tform->getAuthSQL('r'), $app->functions->idn_encode($_POST["email_domain"])); $app->db->query("UPDATE mail_user SET sys_groupid = ? WHERE mailuser_id = ?", $domain["sys_groupid"], $this->id); - } // Set the fields for dovecot - if(isset($this->dataRecord["email"])) { - $disableimap = (isset($this->dataRecord["disableimap"]) && $this->dataRecord["disableimap"])?'y':'n'; - $disablepop3 = (isset($this->dataRecord["disablepop3"]) && $this->dataRecord["disablepop3"])?'y':'n'; - $disablesmtp = (isset($this->dataRecord["disablesmtp"]) && $this->dataRecord["disablesmtp"])?'y':'n'; - $disabledeliver = (isset($this->dataRecord["disabledeliver"]) && $this->dataRecord["disabledeliver"])?'y':'n'; - - $sql = "UPDATE mail_user SET disableimap = ?, disablesieve = ?, `disablesieve-filter` = ?, disablepop3 = ?, disablesmtp = ?, disabledeliver = ?, disablelda = ?, disablelmtp = ? WHERE mailuser_id = ?"; - $app->db->query($sql, $disableimap, $disableimap, $disableimap, $disablepop3, $disablesmtp, $disabledeliver, $disabledeliver, $disabledeliver, $this->id); - } + $disableimap = (isset($this->dataRecord["disableimap"]) && $this->dataRecord["disableimap"])?'y':'n'; + $disablepop3 = (isset($this->dataRecord["disablepop3"]) && $this->dataRecord["disablepop3"])?'y':'n'; + $disablesmtp = (isset($this->dataRecord["disablesmtp"]) && $this->dataRecord["disablesmtp"])?'y':'n'; + $disabledeliver = (isset($this->dataRecord["disabledeliver"]) && $this->dataRecord["disabledeliver"])?'y':'n'; + + $sql = "UPDATE mail_user SET disableimap = ?, disablesieve = ?, `disablesieve-filter` = ?, disablepop3 = ?, disablesmtp = ?, disabledeliver = ?, disablelda = ?, disablelmtp = ? WHERE mailuser_id = ?"; + $app->db->query($sql, $disableimap, $disableimap, $disableimap, $disablepop3, $disablesmtp, $disabledeliver, $disabledeliver, $disabledeliver, $this->id); + + // Spamfilter policy + $policy_id = $app->functions->intval($this->dataRecord["policy"]); + $skip_spamfilter_users_update = false; //** Handle email address change if(isset($this->dataRecord['email']) && $this->oldDataRecord['email'] != $this->dataRecord['email']) { - // Spamfilter policy - $policy_id = $app->functions->intval($this->dataRecord["policy"]); - //** Update spamfilter_users and spamfilter_wblist - $skip_spamfilter_users_update = false; $tmp_olduser = $app->db->queryOneRecord("SELECT id,fullname FROM spamfilter_users WHERE email = ?", $this->oldDataRecord['email']); if($tmp_olduser["id"] > 0) { $tmp_newuser = $app->db->queryOneRecord("SELECT id FROM spamfilter_users WHERE email = ?", $this->dataRecord['email']); if($tmp_newuser['id'] > 0) { // There is a spamfilter_users for both old and new email, we'll update old wblist entries - $update_date = array('rid' => $tmp_newuser['id']); + $update_data = array( + 'rid' => $tmp_newuser['id'], + ); if (isset($domain['sys_groupid'])) { $update_data['sys_groupid'] = $domain['sys_groupid']; } - $tmp_wblist = $app->db->queryAllRecords("SELECT wblist_id FROM spamfilter_users WHERE rid = ?", $tmp_olduser['id']); + $tmp_wblist = $app->db->queryAllRecords("SELECT wblist_id FROM spamfilter_wblist WHERE rid = ?", $tmp_olduser['id']); foreach ($tmp_wblist as $tmp) { $app->db->datalogUpdate('spamfilter_wblist', $update_data, 'wblist_id', $tmp['wblist_id']); } // now delete old spamfilter_users entry $app->db->datalogDelete('spamfilter_users', 'id', $tmp_olduser['id']); + + // we update spamfilter_users for new id below } else { $update_data = array( 'email' => $this->dataRecord['email'], @@ -379,45 +378,6 @@ class page_action extends tform_actions { } } - $tmp_user = $app->db->queryOneRecord("SELECT id FROM spamfilter_users WHERE email = ?", $this->dataRecord['email']); - if($policy_id > 0) { - if($tmp_user["id"] > 0) { - // There is already a record that we will update - if(!$skip_spamfilter_users_update) { - $update_data = array( - 'policy_id' => $policy_id, - ); - if (isset($domain['sys_groupid'])) { - $update_data['sys_groupid'] = $domain['sys_groupid']; - } - $app->db->datalogUpdate('spamfilter_users', $update_data, 'id', $tmp_user['id']); - } - } else { - // We create a new record - $insert_data = array( - "sys_userid" => $_SESSION["s"]["user"]["userid"], - "sys_groupid" => $domain["sys_groupid"], - "sys_perm_user" => 'riud', - "sys_perm_group" => 'riud', - "sys_perm_other" => '', - "server_id" => $domain["server_id"], - "priority" => 10, - "policy_id" => $policy_id, - "email" => $this->dataRecord["email"], - "fullname" => $app->functions->idn_decode($this->dataRecord["email"]), - "local" => 'Y' - ); - $app->db->datalogInsert('spamfilter_users', $insert_data, 'id'); - } - } else { - if($tmp_user["id"] > 0) { - // There is already a record but the user shall have no policy, so we delete it -// fixme: don't delete or we abandon users' wblist entries -// https://git.ispconfig.org/ispconfig/ispconfig3/-/issues/6201 - $app->db->datalogDelete('spamfilter_users', 'id', $tmp_user["id"]); - } - } // endif spamfilter policy - //* Update the aliases $forwardings = $app->db->queryAllRecords("SELECT * FROM mail_forwarding WHERE destination = ?", $this->oldDataRecord['email']); if(is_array($forwardings)) { @@ -426,15 +386,42 @@ class page_action extends tform_actions { $update_data = array( 'destination' => $destination, ); - if (isset($domain['sys_groupid'])) { - $update_data['sys_groupid'] = $domain['sys_groupid']; - } $app->db->datalogUpdate('mail_forwarding', $update_data, 'forwarding_id', $rec['forwarding_id']); } } } // end if email addess changed + $tmp_user = $app->db->queryOneRecord("SELECT id FROM spamfilter_users WHERE email = ?", $this->dataRecord['email']); + if($tmp_user["id"] > 0) { + // There is already a record that we will update + if(!$skip_spamfilter_users_update) { + $update_data = array( + 'policy_id' => $policy_id, + ); + if (isset($domain['sys_groupid'])) { + $update_data['sys_groupid'] = $domain['sys_groupid']; + } + $app->db->datalogUpdate('spamfilter_users', $update_data, 'id', $tmp_user['id']); + } + } else { + // We create a new record + $insert_data = array( + "sys_userid" => $_SESSION["s"]["user"]["userid"], + "sys_groupid" => $domain["sys_groupid"], + "sys_perm_user" => 'riud', + "sys_perm_group" => 'riud', + "sys_perm_other" => '', + "server_id" => $domain["server_id"], + "priority" => 7, + "policy_id" => $policy_id, + "email" => $this->dataRecord["email"], + "fullname" => $app->functions->idn_decode($this->dataRecord["email"]), + "local" => 'Y' + ); + $app->db->datalogInsert('spamfilter_users', $insert_data, 'id'); + } + //* Change backup options when user mail backup options have been changed if(isset($this->dataRecord['backup_interval']) && ($this->dataRecord['backup_interval'] != $this->oldDataRecord['backup_interval'] || $this->dataRecord['backup_copies'] != $this->oldDataRecord['backup_copies'])) { $backup_interval = $this->dataRecord['backup_interval']; diff --git a/interface/web/mail/spamfilter_blacklist_edit.php b/interface/web/mail/spamfilter_blacklist_edit.php index b76334b387..c304dafd2b 100644 --- a/interface/web/mail/spamfilter_blacklist_edit.php +++ b/interface/web/mail/spamfilter_blacklist_edit.php @@ -84,7 +84,7 @@ class page_action extends tform_actions { } } // end if user is not admin - // Select and set the server_id so it matches the server_id of the spa,filter_users record + // Select and set the server_id so it matches the server_id of the spamfilter_users record $tmp = $app->db->queryOneRecord("SELECT server_id FROM spamfilter_users WHERE id = ?", $this->dataRecord["rid"]); $this->dataRecord["server_id"] = $tmp["server_id"]; unset($tmp); diff --git a/interface/web/mail/spamfilter_policy_edit.php b/interface/web/mail/spamfilter_policy_edit.php index 8b15f2fac6..e8dd7f7473 100644 --- a/interface/web/mail/spamfilter_policy_edit.php +++ b/interface/web/mail/spamfilter_policy_edit.php @@ -113,16 +113,9 @@ class page_action extends tform_actions { // check if this is an email domain if(substr($spamfilter_user['email'],0,1) == '@') { $domain = substr($spamfilter_user['email'],1); - $forwardings = $app->db->queryAllRecords("SELECT * FROM mail_forwarding WHERE source LIKE ? OR destination LIKE ?", "%@" . $domain, "%@" . $domain); - - // Force-update aliases and forwards - if(is_array($forwardings)) { - foreach($forwardings as $rec) { - $app->db->datalogUpdate('mail_forwarding', array("source" => $rec['source']), 'forwarding_id', $rec['forwarding_id'],true); - } - } - } + // Nothing special to do for a domain + } } } } diff --git a/interface/web/mail/spamfilter_users_edit.php b/interface/web/mail/spamfilter_users_edit.php index 98493173cc..c06e31fc72 100644 --- a/interface/web/mail/spamfilter_users_edit.php +++ b/interface/web/mail/spamfilter_users_edit.php @@ -65,6 +65,29 @@ class page_action extends tform_actions { parent::onShowNew(); } + function onShowEnd() { + global $app, $conf; + + // Get the spamfilter policies for the user + $tmp_user = $app->db->queryOneRecord("SELECT policy_id FROM spamfilter_users WHERE email = ?", $this->dataRecord["email"]); + if (isset($_POST['policy_id'])) $tmp_user['policy_id'] = intval($_POST['policy_id']); + $sql = "SELECT id, policy_name FROM spamfilter_policy WHERE ".$app->tform->getAuthSQL('r') . " ORDER BY policy_name"; + $policies = $app->db->queryAllRecords($sql); + $policy_select = ""; + if(is_array($policies)) { + foreach( $policies as $p) { + $selected = ($p["id"] == $tmp_user["policy_id"])?'SELECTED':''; + $policy_select .= "\r\n"; + } + } + $app->tpl->setVar("policy_id", $policy_select); + unset($policies); + unset($policy_select); + unset($tmp_user); + + parent::onShowEnd(); + } + function onBeforeUpdate() { global $app, $conf; @@ -109,7 +132,7 @@ class page_action extends tform_actions { // If email changes fire spamfilter_wblist_update events so rspamd files are rewritten if(isset($this->dataRecord['email']) && $this->oldDataRecord['email'] != $this->dataRecord['email']) { - $tmp_wblist = $app->db->queryAllRecords("SELECT wblist_id FROM spamfilter_users WHERE rid = ?", $this->dataRecord['id']); + $tmp_wblist = $app->db->queryAllRecords("SELECT wblist_id FROM spamfilter_wblist WHERE rid = ?", $this->dataRecord['id']); foreach ($tmp_wblist as $tmp) { $app->db->datalogUpdate('spamfilter_wblist', array('rid' => $this->dataRecord['id']), 'wblist_id', $tmp['wblist_id']); } diff --git a/interface/web/mail/spamfilter_whitelist_edit.php b/interface/web/mail/spamfilter_whitelist_edit.php index a404db0ef1..bc161e6e55 100644 --- a/interface/web/mail/spamfilter_whitelist_edit.php +++ b/interface/web/mail/spamfilter_whitelist_edit.php @@ -84,7 +84,7 @@ class page_action extends tform_actions { } } // end if user is not admin - // Select and set the server_id so it matches the server_id of the spa,filter_users record + // Select and set the server_id so it matches the server_id of the spamfilter_users record $tmp = $app->db->queryOneRecord("SELECT server_id FROM spamfilter_users WHERE id = ?", $this->dataRecord["rid"]); $this->dataRecord["server_id"] = $tmp["server_id"]; unset($tmp); diff --git a/interface/web/mailuser/mail_user_spamfilter_edit.php b/interface/web/mailuser/mail_user_spamfilter_edit.php index 8d1e70ba20..2c8759b795 100644 --- a/interface/web/mailuser/mail_user_spamfilter_edit.php +++ b/interface/web/mailuser/mail_user_spamfilter_edit.php @@ -79,32 +79,25 @@ class page_action extends tform_actions { // Spamfilter policy $policy_id = $app->functions->intval($this->dataRecord["policy"]); $tmp_user = $app->db->queryOneRecord("SELECT id FROM spamfilter_users WHERE email = ?", $rec["email"]); - if($policy_id > 0) { - if($tmp_user["id"] > 0) { - // There is already a record that we will update - $app->db->datalogUpdate('spamfilter_users', array("policy_id" => $policy_id), 'id', $tmp_user["id"]); - } else { - // We create a new record - $insert_data = array( - "sys_userid" => $domain["sys_userid"], - "sys_groupid" => $domain["sys_groupid"], - "sys_perm_user" => 'riud', - "sys_perm_group" => 'riud', - "sys_perm_other" => '', - "server_id" => $domain["server_id"], - "priority" => 10, - "policy_id" => $policy_id, - "email" => $rec["email"], - "fullname" => $rec["email"], - "local" => 'Y' - ); - $app->db->datalogInsert('spamfilter_users', $insert_data, 'id'); - } - }else { - if($tmp_user["id"] > 0) { - // There is already a record but the user shall have no policy, so we delete it - $app->db->datalogDelete('spamfilter_users', 'id', $tmp_user["id"]); - } + if($tmp_user["id"] > 0) { + // There is already a record that we will update + $app->db->datalogUpdate('spamfilter_users', array("policy_id" => $policy_id), 'id', $tmp_user["id"]); + } else { + // We create a new record + $insert_data = array( + "sys_userid" => $domain["sys_userid"], + "sys_groupid" => $domain["sys_groupid"], + "sys_perm_user" => 'riud', + "sys_perm_group" => 'riud', + "sys_perm_other" => '', + "server_id" => $domain["server_id"], + "priority" => 7, + "policy_id" => $policy_id, + "email" => $rec["email"], + "fullname" => $rec["email"], + "local" => 'Y' + ); + $app->db->datalogInsert('spamfilter_users', $insert_data, 'id'); } // endif spamfilter policy } @@ -114,26 +107,25 @@ class page_action extends tform_actions { $rec = $app->tform->getDataRecord($this->id); $app->tpl->setVar("email", $app->functions->idn_decode($rec['email']), true); - // Get the spamfilter policys for the user + // Get the spamfilter policies for the user $tmp_user = $app->db->queryOneRecord("SELECT policy_id FROM spamfilter_users WHERE email = ?", $rec['email']); $sql = "SELECT id, policy_name FROM spamfilter_policy WHERE ".$app->tform->getAuthSQL('r'); - $policys = $app->db->queryAllRecords($sql); - $policy_select = ""; - if(is_array($policys)) { - foreach( $policys as $p) { + $policies = $app->db->queryAllRecords($sql); + $policy_select = ""; + if(is_array($policies)) { + foreach( $policies as $p) { $selected = ($p["id"] == $tmp_user["policy_id"])?'SELECTED':''; $policy_select .= "\r\n"; } } $app->tpl->setVar("policy", $policy_select); - unset($policys); + unset($policies); unset($policy_select); unset($tmp_user); parent::onShowEnd(); } - } $app->tform_actions = new page_action; diff --git a/interface/web/tools/resync.php b/interface/web/tools/resync.php index ec947421b7..0aa6677ac3 100644 --- a/interface/web/tools/resync.php +++ b/interface/web/tools/resync.php @@ -93,34 +93,34 @@ class page_action extends tform_actions { if($type == 'mail') { $server_data = array ( - 'mail_domain' => array ( - 'index_field' => 'domain_id', - 'server_type' => 'mail', + 'mail_domain' => array ( + 'index_field' => 'domain_id', + 'server_type' => 'mail', 'server_id' => $server_id, - ), + ), 'mail_get' => array ( 'index_field' => 'mailget_id', 'server_type' => 'mail', 'server_id' => $server_id, ), - 'mail_mailinglist' => array ( - 'index_field' => 'mailinglist_id', - 'server_type' => 'mail', + 'mail_mailinglist' => array ( + 'index_field' => 'mailinglist_id', + 'server_type' => 'mail', 'server_id' => $server_id, - ), - 'mail_user' => array ( - 'index_field' => 'mailuser_id', - 'server_type' => 'mail', + ), + 'mail_user' => array ( + 'index_field' => 'mailuser_id', + 'server_type' => 'mail', 'server_id' => $server_id, ), - 'mail_transport' => array ( - 'index_field' => 'transport_id', - 'server_type' => 'mail', + 'mail_transport' => array ( + 'index_field' => 'transport_id', + 'server_type' => 'mail', 'server_id' => $server_id, ), - 'mail_relay' => array ( - 'index_field' => 'relay_recipient_id', - 'server_type' => 'mail', + 'mail_relay' => array ( + 'index_field' => 'relay_recipient_id', + 'server_type' => 'mail', 'server_id' => $server_id, ), ); @@ -129,17 +129,17 @@ class page_action extends tform_actions { $server_data = array ( 'mail_access' => array ( 'index_field' => 'access_id', - 'server_type' => 'mail', + 'server_type' => 'mail', 'server_id' => $server_id, - ), + ), 'mail_content_filter' => array ( 'index_field' => 'content_filter_id', - 'server_type' => 'mail', - ), + 'server_type' => 'mail', + ), 'mail_user_filter' => array ( 'index_field' => 'filter_id', - 'server_type' => 'mail', - ), + 'server_type' => 'mail', + ), 'spamfilter_policy' => array ( 'index_field' => 'id', 'server_type' => 'mail', @@ -158,26 +158,26 @@ class page_action extends tform_actions { } if($type == 'web' ) { $server_data = array ( - 'web_domain' => array ( - 'index_field' => 'domain_id', - 'server_type' => 'web', + 'web_domain' => array ( + 'index_field' => 'domain_id', + 'server_type' => 'web', 'server_id' => $server_id, - ), - 'shell_user' => array ( - 'index_field' => 'shell_user_id', - 'server_type' => 'web', + ), + 'shell_user' => array ( + 'index_field' => 'shell_user_id', + 'server_type' => 'web', 'server_id' => $server_id, - ), - 'cron' => array ( - 'index_field' => 'id', - 'server_type' => 'cron', + ), + 'cron' => array ( + 'index_field' => 'id', + 'server_type' => 'cron', 'server_id' => $server_id, - ), - 'ftp_user' => array ( - 'index_field' => 'ftp_user_id', - 'server_type' => 'web', + ), + 'ftp_user' => array ( + 'index_field' => 'ftp_user_id', + 'server_type' => 'web', 'server_id' => $server_id, - ), + ), ); } if($type == 'dns' ) { @@ -191,11 +191,11 @@ class page_action extends tform_actions { } if($type == 'file' ) { $server_data = array ( - 'webdav_user' => array ( - 'index_field' => 'webdav_user_id', - 'server_type' => 'file', + 'webdav_user' => array ( + 'index_field' => 'webdav_user_id', + 'server_type' => 'file', 'server_id' => $server_id, - ), + ), ); } if($type == 'db' ) { diff --git a/server/plugins-available/mail_plugin.inc.php b/server/plugins-available/mail_plugin.inc.php index 0d2e52c4fc..2baf07ec7d 100644 --- a/server/plugins-available/mail_plugin.inc.php +++ b/server/plugins-available/mail_plugin.inc.php @@ -364,7 +364,7 @@ class mail_plugin { $app->log('Moved invalid maildir to corrupted Maildirs folder: '.$data['new']['maildir'], LOGLEVEL_WARN); } - //* Create the maildir, if it doesn not exist, set permissions, set quota. + //* Create the maildir, if it does not exist, set permissions, set quota. if(!empty($maildomain_path) && !is_dir($maildomain_path.'/new')) { $app->system->maildirmake($maildomain_path, $user, '', $group); diff --git a/server/plugins-available/rspamd_plugin.inc.php b/server/plugins-available/rspamd_plugin.inc.php index 9d34ac8a36..112020deb7 100644 --- a/server/plugins-available/rspamd_plugin.inc.php +++ b/server/plugins-available/rspamd_plugin.inc.php @@ -243,17 +243,7 @@ class rspamd_plugin { $settings_file = $this->users_config_dir . str_replace('@', '_', $settings_name) . '.conf'; //$app->log('Settings file for rspamd is ' . $settings_file, LOGLEVEL_WARN); if($mode === 'delete') { - $delete_file = true; - if($type === 'spamfilter_user') { - $search_for_policy[] = $email_address; - $search_for_policy[] = substr($email_address, strpos($email_address, '@')); - - $policy = $app->db->queryOneRecord("SELECT p.* FROM spamfilter_users as u INNER JOIN spamfilter_policy as p ON (p.id = u.policy_id) WHERE u.server_id = ? AND u.email IN ? ORDER BY u.priority DESC", $conf['server_id'], $search_for_policy); - if($policy) { - $delete_file = false; - } - } - if($delete_file === true && is_file($settings_file)) { + if(is_file($settings_file)) { unlink($settings_file); } } else { @@ -265,7 +255,12 @@ class rspamd_plugin { // get policy for entry if($type === 'spamfilter_user') { - $policy = $app->db->queryOneRecord("SELECT * FROM spamfilter_policy WHERE id = ?", intval($data['new']['policy_id'])); + if (intval($data['new']['policy_id']) > 0) { + $policy = $app->db->queryOneRecord("SELECT * FROM spamfilter_policy WHERE id = ?", intval($data['new']['policy_id'])); + } else { + $domain = substr($data['new']['email'], strpos($data['new']['email'], '@')); + $policy = $app->db->queryOneRecord("SELECT p.* FROM spamfilter_users as u INNER JOIN spamfilter_policy as p ON (p.id = u.policy_id) WHERE u.server_id = ? AND u.email = ?", $conf['server_id'], $domain); + } $check = $app->db->queryOneRecord('SELECT `greylisting` FROM `mail_user` WHERE `server_id` = ? AND `email` = ? UNION SELECT `greylisting` FROM `mail_forwarding` WHERE `server_id` = ? AND `source` = ? ORDER BY (`greylisting` = ?) DESC', $conf['server_id'], $email_address, $conf['server_id'], $email_address, 'y'); if($check) { @@ -286,7 +281,7 @@ class rspamd_plugin { $app->system->mkdirpath($this->users_config_dir); } - if(!$this->isValidEmail($app->functions->idn_encode($email_address))) { + if((!$this->isValidEmail($app->functions->idn_encode($email_address))) || intval($data['new']['policy_id']) == 0) { if(is_file($settings_file)) { unlink($settings_file); } @@ -310,11 +305,13 @@ class rspamd_plugin { } else { $tpl->setVar('from_email', $app->functions->idn_encode($email_address)); } + // unneded? $spamfilter appears unused $spamfilter = $data[$use_data]; } else { $tpl->setVar('to_email', $app->functions->idn_encode($email_address)); // need to get matching spamfilter user if any + // unneded? $spamfilter appears unused $spamfilter = $app->db->queryOneRecord('SELECT * FROM spamfilter_users WHERE `email` = ?', $email_address); } @@ -399,6 +396,7 @@ class rspamd_plugin { } else { $record_id = intval($data['new']['wblist_id']); $wblist_file = $this->users_config_dir.'spamfilter_wblist_'.$record_id.'.conf'; + $tmp = $app->db->queryOneRecord("SELECT email FROM spamfilter_users WHERE id = ?", intval($data['new']['rid'])); if($tmp && !empty($tmp)) { $filter = array( -- GitLab From 06d5ca4782f2afa68a3fe24a22e0ac5ad9d46fd2 Mon Sep 17 00:00:00 2001 From: Jesse Norell Date: Fri, 6 Aug 2021 16:42:41 -0600 Subject: [PATCH 6/7] add file_cleanup cronjob to catch abandoned rspamd config files --- .../classes/cron.d/600-file_cleanup.inc.php | 143 ++++++++++++++++++ server/lib/classes/cronjob.inc.php | 27 +++- 2 files changed, 169 insertions(+), 1 deletion(-) create mode 100644 server/lib/classes/cron.d/600-file_cleanup.inc.php diff --git a/server/lib/classes/cron.d/600-file_cleanup.inc.php b/server/lib/classes/cron.d/600-file_cleanup.inc.php new file mode 100644 index 0000000000..b487a529b8 --- /dev/null +++ b/server/lib/classes/cron.d/600-file_cleanup.inc.php @@ -0,0 +1,143 @@ +db->queryAllRecords($sql, $server_id); + if(is_array($records)) { + foreach($records as $rec){ + $mail_access[$rec['id']] = $rec['id']; + } + } + + $spamfilter_wblist = array(); + $sql = "SELECT wblist_id as id FROM spamfilter_wblist WHERE active = 'y' AND server_id = ?"; + $records = $app->db->queryAllRecords($sql, $server_id); + if(is_array($records)) { + foreach($records as $rec){ + $spamfilter_wblist[$rec['id']] = $rec['id']; + } + } + + $spamfilter_users = array(); + $sql = "SELECT id FROM spamfilter_users WHERE policy_id != 0 AND server_id = ?"; + $records = $app->db->queryAllRecords($sql, $server_id); + if(is_array($records)) { + foreach($records as $rec){ + $spamfilter_users[$rec['id']] = $rec['id']; + } + } + + $mail_user = array(); + $sql = "SELECT mailuser_id as id FROM mail_user WHERE postfix = 'y' AND server_id = ?"; + $records = $app->db->queryAllRecords($sql, $server_id); + if(is_array($records)) { + foreach($records as $rec){ + $mail_user[$rec['id']] = $rec['id']; + } + } + + $mail_forwarding = array(); + $sql = "SELECT forwarding_id as id FROM mail_forwarding WHERE active = 'y' AND server_id = ?"; + $records = $app->db->queryAllRecords($sql, $server_id); + if(is_array($records)) { + foreach($records as $rec){ + $mail_forwarding[$rec['id']] = $rec['id']; + } + } + + foreach (glob('/etc/rspamd/local.d/users/*.conf') as $file) { + if($handle = fopen($file, 'r')) { + if(($line = fgets($handle)) !== false) { + if(preg_match('/^((?:global|spamfilter)_wblist|ispc_(spamfilter_user|mail_user|mail_forwarding))[_-](\d+)\s/', $line, $matches)) { + switch($matches[1]) { + case 'global_wblist': + $remove = ! isset($mail_access[$matches[3]]); + break; + case 'spamfilter_wblist': + $remove = ! isset($spamfilter_wblist[$matches[3]]); + break; + case 'ispc_spamfilter_user': + $remove = ! isset($spamfilter_users[$matches[3]]); + break; + case 'ispc_mail_user': + $remove = ! isset($mail_user[$matches[3]]); + break; + case 'ispc_mail_forwarding': + $remove = ! isset($mail_forwarding[$matches[3]]); + break; + default: + $app->log("conf file has unhandled rule naming convention, ignoring: $file", LOGLEVEL_DEBUG); + $remove = false; + } + if($remove) { + $app->log("$matches[1] id $matches[3] not found, removing $file", LOGLEVEL_DEBUG); + unlink($file); + $this->restartServiceDelayed('rspamd', 'reload'); + } + } else { + $app->log("conf file has unknown rule naming convention, ignoring: $file", LOGLEVEL_DEBUG); + } + } + + fclose($handle); + } + } + } + + parent::onRunJob(); + } + +} + diff --git a/server/lib/classes/cronjob.inc.php b/server/lib/classes/cronjob.inc.php index 61d45749a8..6ea7559570 100644 --- a/server/lib/classes/cronjob.inc.php +++ b/server/lib/classes/cronjob.inc.php @@ -43,6 +43,9 @@ class cronjob { protected $_next_run = null; private $_running = false; + // services for delayed restart/reload + private $_delayed_restart_services = array(); + /** return schedule */ @@ -178,6 +181,12 @@ class cronjob { global $app, $conf; if($conf['log_priority'] <= LOGLEVEL_DEBUG) print "Called onAfterRun() for class " . get_class($this) . "\n"; + + if(is_array($this->_delayed_restart_services)) { + foreach ($this->_delayed_restart_services as $service => $mode) { + $this->restartService($service, $mode); + } + } } // child classes may NOT override this! @@ -188,6 +197,22 @@ class cronjob { $app->db->query("UPDATE `sys_cron` SET `running` = 0 WHERE `name` = ?", get_class($this)); } + // child classes may NOT override this! + protected function restartService($service, $mode='restart') { + global $app; + + $mode = ($mode == 'reload' ? 'reload' : 'restart'); + $app->system->exec_safe('service ? ?', $service, $mode); + } + + // child classes may NOT override this! + protected function restartServiceDelayed($service, $mode='restart') { + $mode = ($mode == 'reload' ? 'reload' : 'restart'); + + if (is_array($this->_delayed_restart_services)) { + $this->_delayed_restart_services[$service] = $mode; + } + } + } -?> -- GitLab From 0c4b8450db406dcae9bd7707e828a830524100d7 Mon Sep 17 00:00:00 2001 From: Jesse Norell Date: Mon, 16 Aug 2021 15:31:19 -0600 Subject: [PATCH 7/7] MR review suggestions/fixes --- .../web/mail/form/spamfilter_users.tform.php | 2 +- interface/web/mail/mail_user_edit.php | 2 +- .../classes/cron.d/600-file_cleanup.inc.php | 10 +++++----- server/lib/classes/cronjob.inc.php | 19 +++++++++++++------ 4 files changed, 20 insertions(+), 13 deletions(-) diff --git a/interface/web/mail/form/spamfilter_users.tform.php b/interface/web/mail/form/spamfilter_users.tform.php index 1388217b64..1bb7b5f682 100644 --- a/interface/web/mail/form/spamfilter_users.tform.php +++ b/interface/web/mail/form/spamfilter_users.tform.php @@ -78,7 +78,7 @@ $form["tabs"]['users'] = array ( 'policy_id' => array ( 'datatype' => 'INTEGER', 'formtype' => 'SELECT', - 'default' => '', + 'default' => '0', 'datasource' => array ( 'type' => 'SQL', 'querystring' => 'SELECT id,policy_name FROM spamfilter_policy WHERE {AUTHSQL} ORDER BY policy_name', 'keyfield'=> 'id', diff --git a/interface/web/mail/mail_user_edit.php b/interface/web/mail/mail_user_edit.php index f13c5845a5..d8730fa2eb 100644 --- a/interface/web/mail/mail_user_edit.php +++ b/interface/web/mail/mail_user_edit.php @@ -296,7 +296,7 @@ class page_action extends tform_actions { "sys_perm_group" => 'riud', "sys_perm_other" => '', "server_id" => $domain["server_id"], - "priority" => 5, + "priority" => 7, "policy_id" => $policy_id, "email" => $this->dataRecord["email"], "fullname" => $app->functions->idn_decode($this->dataRecord["email"]), diff --git a/server/lib/classes/cron.d/600-file_cleanup.inc.php b/server/lib/classes/cron.d/600-file_cleanup.inc.php index b487a529b8..765b091041 100644 --- a/server/lib/classes/cron.d/600-file_cleanup.inc.php +++ b/server/lib/classes/cron.d/600-file_cleanup.inc.php @@ -103,19 +103,19 @@ class cronjob_file_cleanup extends cronjob { if(preg_match('/^((?:global|spamfilter)_wblist|ispc_(spamfilter_user|mail_user|mail_forwarding))[_-](\d+)\s/', $line, $matches)) { switch($matches[1]) { case 'global_wblist': - $remove = ! isset($mail_access[$matches[3]]); + $remove = isset($mail_access[$matches[3]]) ? false : true; break; case 'spamfilter_wblist': - $remove = ! isset($spamfilter_wblist[$matches[3]]); + $remove = isset($spamfilter_wblist[$matches[3]]) ? false : true; break; case 'ispc_spamfilter_user': - $remove = ! isset($spamfilter_users[$matches[3]]); + $remove = isset($spamfilter_users[$matches[3]]) ? false : true; break; case 'ispc_mail_user': - $remove = ! isset($mail_user[$matches[3]]); + $remove = isset($mail_user[$matches[3]]) ? false : true; break; case 'ispc_mail_forwarding': - $remove = ! isset($mail_forwarding[$matches[3]]); + $remove = isset($mail_forwarding[$matches[3]]) ? false : true; break; default: $app->log("conf file has unhandled rule naming convention, ignoring: $file", LOGLEVEL_DEBUG); diff --git a/server/lib/classes/cronjob.inc.php b/server/lib/classes/cronjob.inc.php index 6ea7559570..df410042db 100644 --- a/server/lib/classes/cronjob.inc.php +++ b/server/lib/classes/cronjob.inc.php @@ -198,19 +198,26 @@ class cronjob { } // child classes may NOT override this! - protected function restartService($service, $mode='restart') { + protected function restartService($service, $action='restart') { global $app; - $mode = ($mode == 'reload' ? 'reload' : 'restart'); - $app->system->exec_safe('service ? ?', $service, $mode); + $app->uses('system'); + + $retval = array('output' => '', 'retval' => 0); + if($action == 'reload') { + exec($app->system->getinitcommand($service, 'reload').' 2>&1', $retval['output'], $retval['retval']); + } else { + exec($app->system->getinitcommand($service, 'restart').' 2>&1', $retval['output'], $retval['retval']); + } + return $retval; } // child classes may NOT override this! - protected function restartServiceDelayed($service, $mode='restart') { - $mode = ($mode == 'reload' ? 'reload' : 'restart'); + protected function restartServiceDelayed($service, $action='restart') { + $action = ($action == 'reload' ? 'reload' : 'restart'); if (is_array($this->_delayed_restart_services)) { - $this->_delayed_restart_services[$service] = $mode; + $this->_delayed_restart_services[$service] = $action; } } -- GitLab