Commit 95870536 authored by tbrehm's avatar tbrehm
Browse files

Added a function to update all email addresses when a mail domain is changed.

parent baf2ef33
...@@ -407,7 +407,7 @@ CREATE TABLE `mail_user` ( ...@@ -407,7 +407,7 @@ CREATE TABLE `mail_user` (
`uid` int(10) unsigned NOT NULL default '5000', `uid` int(10) unsigned NOT NULL default '5000',
`gid` int(10) unsigned NOT NULL default '5000', `gid` int(10) unsigned NOT NULL default '5000',
`maildir` varchar(255) NOT NULL default '', `maildir` varchar(255) NOT NULL default '',
`quota` int(11) NOT NULL, `quota` int(11) NOT NULL default '0',
`homedir` varchar(255) NOT NULL, `homedir` varchar(255) NOT NULL,
`autoresponder` enum('n','y') NOT NULL default 'n', `autoresponder` enum('n','y') NOT NULL default 'n',
`autoresponder_text` tinytext NOT NULL, `autoresponder_text` tinytext NOT NULL,
......
...@@ -215,6 +215,77 @@ class db ...@@ -215,6 +215,77 @@ class db
} }
} }
//** Function to fill the datalog with a full differential record.
public function datalogSave($db_table, $action, $primary_field, $primary_id, $record_old, $record_new) {
global $app,$conf;
// Insert backticks only for incomplete table names.
if(stristr($db_table,'.')) {
$escape = '';
} else {
$escape = '`';
}
$diffrec_full = array();
$diff_num = 0;
if(is_array($record_old) && count($record_old) > 0) {
foreach($record_old as $key => $val) {
if(isset($record_new[$key]) && $record_new[$key] != $val) {
// Record has changed
$diffrec_full['old'][$key] = $val;
$diffrec_full['new'][$key] = $record_new[$key];
$diff_num++;
} else {
$diffrec_full['old'][$key] = $val;
$diffrec_full['new'][$key] = $val;
}
}
} elseif(is_array($record_new)) {
foreach($record_new as $key => $val) {
if(isset($record_new[$key]) && $record_old[$key] != $val) {
// Record has changed
$diffrec_full['new'][$key] = $val;
$diffrec_full['old'][$key] = $record_old[$key];
$diff_num++;
} else {
$diffrec_full['new'][$key] = $val;
$diffrec_full['old'][$key] = $val;
}
}
}
// Insert the server_id, if the record has a server_id
$server_id = (isset($record_old["server_id"]) && $record_old["server_id"] > 0)?$record_old["server_id"]:0;
if(isset($record_new["server_id"])) $server_id = $record_new["server_id"];
if($diff_num > 0) {
$diffstr = $app->db->quote(serialize($diffrec_full));
$username = $app->db->quote($_SESSION["s"]["user"]["username"]);
$dbidx = $primary_field.":".$primary_id;
if($action == 'INSERT') $action = 'i';
if($action == 'UPDATE') $action = 'u';
if($action == 'DELETE') $action = 'd';
$sql = "INSERT INTO sys_datalog (dbtable,dbidx,server_id,action,tstamp,user,data) VALUES ('".$db_table."','$dbidx','$server_id','$action','".time()."','$username','$diffstr')";
$app->db->query($sql);
}
return true;
}
//** Updates a record and saves the cahnges into the datalog
public function datalogUpdate($tablename, $update_data, $index_field, $index_value) {
global $app;
$old_rec = $this->queryOneRecord("SELECT * FROM $tablename WHERE $index_field = '$index_value'");
$this->query("UPDATE $tablename SET $update_data WHERE $index_field = '$index_value'");
$new_rec = $this->queryOneRecord("SELECT * FROM $tablename WHERE $index_field = '$index_value'");
$this->datalogSave($tablename, 'UPDATE', $index_field, $index_value, $old_rec, $new_rec);
return true;
}
public function closeConn() public function closeConn()
{ {
if($this->linkId) if($this->linkId)
......
...@@ -876,15 +876,6 @@ class tform { ...@@ -876,15 +876,6 @@ class tform {
$escape = '`'; $escape = '`';
} }
/*
if($action == "UPDATE" or $action == "DELETE") {
$sql = "SELECT * FROM ".$escape.$this->formDef['db_table'].$escape." WHERE ".$this->formDef['db_table_idx']." = ".$primary_id;
$record_old = $app->db->queryOneRecord($sql);
} else {
$record_old = array();
}
*/
$diffrec = array(); $diffrec = array();
if(is_array($record_new) && count($record_new) > 0) { if(is_array($record_new) && count($record_new) > 0) {
...@@ -934,12 +925,6 @@ class tform { ...@@ -934,12 +925,6 @@ class tform {
} }
} }
/*
echo "<pre>";
print_r($diffrec_full);
echo "</pre>";
*/
// Insert the server_id, if the record has a server_id // Insert the server_id, if the record has a server_id
$server_id = (isset($record_old["server_id"]) && $record_old["server_id"] > 0)?$record_old["server_id"]:0; $server_id = (isset($record_old["server_id"]) && $record_old["server_id"] > 0)?$record_old["server_id"]:0;
if(isset($record_new["server_id"])) $server_id = $record_new["server_id"]; if(isset($record_new["server_id"])) $server_id = $record_new["server_id"];
......
...@@ -41,6 +41,7 @@ class tform_actions { ...@@ -41,6 +41,7 @@ class tform_actions {
var $activeTab; var $activeTab;
var $dataRecord; var $dataRecord;
var $plugins = array(); var $plugins = array();
var $oldDataRecord; // This array is only filled during updates and when db_history is enabled.
function onLoad() { function onLoad() {
global $app, $conf, $tform_def_file; global $app, $conf, $tform_def_file;
...@@ -104,7 +105,7 @@ class tform_actions { ...@@ -104,7 +105,7 @@ class tform_actions {
if($app->tform->errorMessage == '') { if($app->tform->errorMessage == '') {
if($app->tform->formDef['db_history'] == 'yes') { if($app->tform->formDef['db_history'] == 'yes') {
$old_data_record = $app->tform->getDataRecord($this->id); $this->oldDataRecord = $app->tform->getDataRecord($this->id);
} }
// Save record in database // Save record in database
...@@ -124,7 +125,7 @@ class tform_actions { ...@@ -124,7 +125,7 @@ class tform_actions {
// Write data history (sys_datalog) // Write data history (sys_datalog)
if($app->tform->formDef['db_history'] == 'yes') { if($app->tform->formDef['db_history'] == 'yes') {
$new_data_record = $app->tform->getDataRecord($this->id); $new_data_record = $app->tform->getDataRecord($this->id);
$app->tform->datalogSave('UPDATE',$this->id,$old_data_record,$new_data_record); $app->tform->datalogSave('UPDATE',$this->id,$this->oldDataRecord,$new_data_record);
unset($new_data_record); unset($new_data_record);
unset($old_data_record); unset($old_data_record);
} }
......
...@@ -94,7 +94,7 @@ $form["tabs"]['mailuser'] = array ( ...@@ -94,7 +94,7 @@ $form["tabs"]['mailuser'] = array (
'validators' => array ( 0 => array ( 'type' => 'ISINT', 'validators' => array ( 0 => array ( 'type' => 'ISINT',
'errmsg'=> 'quota_error_isint'), 'errmsg'=> 'quota_error_isint'),
), ),
'default' => '', 'default' => '0',
'value' => '', 'value' => '',
'width' => '30', 'width' => '30',
'maxlength' => '255' 'maxlength' => '255'
......
...@@ -216,6 +216,26 @@ class page_action extends tform_actions { ...@@ -216,6 +216,26 @@ class page_action extends tform_actions {
$app->db->query($sql); $app->db->query($sql);
} }
} // endif spamfilter policy } // endif spamfilter policy
//** If the domain name has been changed, change the domain in all mailbox records
if($this->oldDataRecord['domain'] != $this->dataRecord['domain']) {
$app->uses('getconf');
$mail_config = $app->getconf->get_server_config($this->dataRecord["server_id"],'mail');
$mailusers = $app->db->queryAllRecords("SELECT * FROM mail_user WHERE email like '%@".addslashes($this->oldDataRecord['domain'])."'");
if(is_array($mailusers)) {
foreach($mailusers as $rec) {
// setting Maildir, Homedir, UID and GID
$mail_parts = explode("@",$rec['email']);
$maildir = str_replace("[domain]",$this->dataRecord['domain'],$mail_config["maildir_path"]);
$maildir = str_replace("[localpart]",$mail_parts[0],$maildir);
$maildir = addslashes($maildir);
//$app->db->query("UPDATE mail_user SET maildir = '$maildir' WHERE mailuser_id = ".$rec['mailuser_id']);
//$rec_new = $app->db->queryOneRecord("SELECT * FROM mail_user WHERE mailuser_id = ".$rec['mailuser_id']);
$app->db->datalogUpdate('mail_user', "maildir = '$maildir'", 'mailuser_id', $rec['mailuser_id']);
}
}
} // end if domain name changed
} }
} }
......
...@@ -58,7 +58,7 @@ class mail_plugin { ...@@ -58,7 +58,7 @@ class mail_plugin {
// Create the maildir, if it does not exist // Create the maildir, if it does not exist
if(!is_dir($data['new']['maildir'])) { if(!is_dir($data['new']['maildir'])) {
mkdir($data['new']['maildir']); exec('mkdir -p '.escapeshellcmd($data['new']['maildir']));
exec('chown '.$mail_config['mailuser_name'].':'.$mail_config['mailuser_group'].' '.escapeshellcmd($data['new']['maildir'])); exec('chown '.$mail_config['mailuser_name'].':'.$mail_config['mailuser_group'].' '.escapeshellcmd($data['new']['maildir']));
$app->log('Created Maildir: '.$data['new']['maildir'],LOGLEVEL_DEBUG); $app->log('Created Maildir: '.$data['new']['maildir'],LOGLEVEL_DEBUG);
} }
...@@ -74,15 +74,16 @@ class mail_plugin { ...@@ -74,15 +74,16 @@ class mail_plugin {
// Create the maildir, if it does not exist // Create the maildir, if it does not exist
if(!is_dir($data['new']['maildir'])) { if(!is_dir($data['new']['maildir'])) {
mkdir($data['new']['maildir']); exec('mkdir -p '.escapeshellcmd($data['new']['maildir']));
exec('chown '.$mail_config['mailuser_name'].':'.$mail_config['mailuser_group'].' '.escapeshellcmd($data['new']['maildir'])); exec('chown '.$mail_config['mailuser_name'].':'.$mail_config['mailuser_group'].' '.escapeshellcmd($data['new']['maildir']));
$app->log('Created Maildir: '.$data['new']['maildir'],LOGLEVEL_DEBUG); $app->log('Created Maildir: '.$data['new']['maildir'],LOGLEVEL_DEBUG);
} }
// Move mailbox, if domain has changed and delete old mailbox // Move mailbox, if domain has changed and delete old mailbox
if($data['new']['maildir'] != $data['old']['maildir'] && is_dir($data['old']['maildir'])) { if($data['new']['maildir'] != $data['old']['maildir'] && is_dir($data['old']['maildir'])) {
exec('mv -f'.escapeshellcmd($data['old']['maildir']).'* '.escapeshellcmd($data['new']['maildir'])); exec('mv -f '.escapeshellcmd($data['old']['maildir']).'* '.escapeshellcmd($data['new']['maildir']));
unlink($data['old']['maildir']); if(is_file($data['old']['maildir'].'.ispconfig_mailsize'))exec('mv -f '.escapeshellcmd($data['old']['maildir']).'.ispconfig_mailsize '.escapeshellcmd($data['new']['maildir']));
rmdir($data['old']['maildir']);
$app->log('Moved Maildir from: '.$data['old']['maildir'].' to '.$data['new']['maildir'],LOGLEVEL_DEBUG); $app->log('Moved Maildir from: '.$data['old']['maildir'].' to '.$data['new']['maildir'],LOGLEVEL_DEBUG);
} }
...@@ -93,7 +94,7 @@ class mail_plugin { ...@@ -93,7 +94,7 @@ class mail_plugin {
$old_maildir_path = escapeshellcmd($data['old']['maildir']); $old_maildir_path = escapeshellcmd($data['old']['maildir']);
if(!stristr($old_maildir_path,'..') && !stristr($old_maildir_path,'*') && strlen($old_maildir_path) >= 10) { if(!stristr($old_maildir_path,'..') && !stristr($old_maildir_path,'*') && strlen($old_maildir_path) >= 10) {
exec('rm -rf '.$old_maildir_path); exec('rm -rf '.escapeshellcmd($old_maildir_path));
$app->log('Deleted the Maildir: '.$data['old']['maildir'],LOGLEVEL_DEBUG); $app->log('Deleted the Maildir: '.$data['old']['maildir'],LOGLEVEL_DEBUG);
} else { } else {
$app->log('Possible security violation when deleting the maildir: '.$data['old']['maildir'],LOGLEVEL_ERROR); $app->log('Possible security violation when deleting the maildir: '.$data['old']['maildir'],LOGLEVEL_ERROR);
......
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