Commit 9a335174 authored by Jesse Norell's avatar Jesse Norell
Browse files

always keep spamfilter_users entries: #6201

parent 20c5d50d
......@@ -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'.
......
-- default spamfilter_users.policy_id to 0
ALTER TABLE `spamfilter_users` ALTER `policy_id` SET DEFAULT 0;
......@@ -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,
......
......@@ -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'.
......
......@@ -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'.
......
......@@ -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'.
......
......@@ -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) {
......
......@@ -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'))
......
......@@ -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
}
}
......@@ -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();
?>
......@@ -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',
......
......@@ -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 -';
......@@ -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 -';
......@@ -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 -';
......@@ -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 -';
......@@ -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 -';
......@@ -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 -';
......@@ -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 -';
......@@ -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 -';
......@@ -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 -';
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment