Skip to content

Commit 9587053

Browse files
committed
Added a function to update all email addresses when a mail domain is changed.
1 parent baf2ef3 commit 9587053

File tree

7 files changed

+103
-25
lines changed

7 files changed

+103
-25
lines changed

install/sql/ispconfig3.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ CREATE TABLE `mail_user` (
407407
`uid` int(10) unsigned NOT NULL default '5000',
408408
`gid` int(10) unsigned NOT NULL default '5000',
409409
`maildir` varchar(255) NOT NULL default '',
410-
`quota` int(11) NOT NULL,
410+
`quota` int(11) NOT NULL default '0',
411411
`homedir` varchar(255) NOT NULL,
412412
`autoresponder` enum('n','y') NOT NULL default 'n',
413413
`autoresponder_text` tinytext NOT NULL,

interface/lib/classes/db_mysql.inc.php

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,77 @@ public function update($tablename, $form, $bedingung, $debug = 0)
214214
if($debug == 1){ echo 'mySQL Error Message: '.$this->errorMessage; }
215215
}
216216
}
217+
218+
//** Function to fill the datalog with a full differential record.
219+
public function datalogSave($db_table, $action, $primary_field, $primary_id, $record_old, $record_new) {
220+
global $app,$conf;
221+
222+
// Insert backticks only for incomplete table names.
223+
if(stristr($db_table,'.')) {
224+
$escape = '';
225+
} else {
226+
$escape = '`';
227+
}
228+
229+
$diffrec_full = array();
230+
$diff_num = 0;
231+
232+
if(is_array($record_old) && count($record_old) > 0) {
233+
foreach($record_old as $key => $val) {
234+
if(isset($record_new[$key]) && $record_new[$key] != $val) {
235+
// Record has changed
236+
$diffrec_full['old'][$key] = $val;
237+
$diffrec_full['new'][$key] = $record_new[$key];
238+
$diff_num++;
239+
} else {
240+
$diffrec_full['old'][$key] = $val;
241+
$diffrec_full['new'][$key] = $val;
242+
}
243+
}
244+
} elseif(is_array($record_new)) {
245+
foreach($record_new as $key => $val) {
246+
if(isset($record_new[$key]) && $record_old[$key] != $val) {
247+
// Record has changed
248+
$diffrec_full['new'][$key] = $val;
249+
$diffrec_full['old'][$key] = $record_old[$key];
250+
$diff_num++;
251+
} else {
252+
$diffrec_full['new'][$key] = $val;
253+
$diffrec_full['old'][$key] = $val;
254+
}
255+
}
256+
}
257+
258+
// Insert the server_id, if the record has a server_id
259+
$server_id = (isset($record_old["server_id"]) && $record_old["server_id"] > 0)?$record_old["server_id"]:0;
260+
if(isset($record_new["server_id"])) $server_id = $record_new["server_id"];
261+
262+
if($diff_num > 0) {
263+
$diffstr = $app->db->quote(serialize($diffrec_full));
264+
$username = $app->db->quote($_SESSION["s"]["user"]["username"]);
265+
$dbidx = $primary_field.":".$primary_id;
266+
267+
if($action == 'INSERT') $action = 'i';
268+
if($action == 'UPDATE') $action = 'u';
269+
if($action == 'DELETE') $action = 'd';
270+
$sql = "INSERT INTO sys_datalog (dbtable,dbidx,server_id,action,tstamp,user,data) VALUES ('".$db_table."','$dbidx','$server_id','$action','".time()."','$username','$diffstr')";
271+
$app->db->query($sql);
272+
}
273+
274+
return true;
275+
}
276+
277+
//** Updates a record and saves the cahnges into the datalog
278+
public function datalogUpdate($tablename, $update_data, $index_field, $index_value) {
279+
global $app;
280+
281+
$old_rec = $this->queryOneRecord("SELECT * FROM $tablename WHERE $index_field = '$index_value'");
282+
$this->query("UPDATE $tablename SET $update_data WHERE $index_field = '$index_value'");
283+
$new_rec = $this->queryOneRecord("SELECT * FROM $tablename WHERE $index_field = '$index_value'");
284+
$this->datalogSave($tablename, 'UPDATE', $index_field, $index_value, $old_rec, $new_rec);
285+
286+
return true;
287+
}
217288

218289
public function closeConn()
219290
{

interface/lib/classes/tform.inc.php

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -875,15 +875,6 @@ function datalogSave($action,$primary_id, $record_old, $record_new) {
875875
} else {
876876
$escape = '`';
877877
}
878-
879-
/*
880-
if($action == "UPDATE" or $action == "DELETE") {
881-
$sql = "SELECT * FROM ".$escape.$this->formDef['db_table'].$escape." WHERE ".$this->formDef['db_table_idx']." = ".$primary_id;
882-
$record_old = $app->db->queryOneRecord($sql);
883-
} else {
884-
$record_old = array();
885-
}
886-
*/
887878

888879
$diffrec = array();
889880

@@ -934,12 +925,6 @@ function datalogSave($action,$primary_id, $record_old, $record_new) {
934925
}
935926
}
936927

937-
/*
938-
echo "<pre>";
939-
print_r($diffrec_full);
940-
echo "</pre>";
941-
*/
942-
943928
// Insert the server_id, if the record has a server_id
944929
$server_id = (isset($record_old["server_id"]) && $record_old["server_id"] > 0)?$record_old["server_id"]:0;
945930
if(isset($record_new["server_id"])) $server_id = $record_new["server_id"];

interface/lib/classes/tform_actions.inc.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class tform_actions {
4141
var $activeTab;
4242
var $dataRecord;
4343
var $plugins = array();
44+
var $oldDataRecord; // This array is only filled during updates and when db_history is enabled.
4445

4546
function onLoad() {
4647
global $app, $conf, $tform_def_file;
@@ -104,7 +105,7 @@ function onUpdate() {
104105
if($app->tform->errorMessage == '') {
105106

106107
if($app->tform->formDef['db_history'] == 'yes') {
107-
$old_data_record = $app->tform->getDataRecord($this->id);
108+
$this->oldDataRecord = $app->tform->getDataRecord($this->id);
108109
}
109110

110111
// Save record in database
@@ -124,7 +125,7 @@ function onUpdate() {
124125
// Write data history (sys_datalog)
125126
if($app->tform->formDef['db_history'] == 'yes') {
126127
$new_data_record = $app->tform->getDataRecord($this->id);
127-
$app->tform->datalogSave('UPDATE',$this->id,$old_data_record,$new_data_record);
128+
$app->tform->datalogSave('UPDATE',$this->id,$this->oldDataRecord,$new_data_record);
128129
unset($new_data_record);
129130
unset($old_data_record);
130131
}

interface/web/mail/form/mail_user.tform.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@
9494
'validators' => array ( 0 => array ( 'type' => 'ISINT',
9595
'errmsg'=> 'quota_error_isint'),
9696
),
97-
'default' => '',
97+
'default' => '0',
9898
'value' => '',
9999
'width' => '30',
100100
'maxlength' => '255'

interface/web/mail/mail_domain_edit.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,26 @@ function onAfterUpdate() {
216216
$app->db->query($sql);
217217
}
218218
} // endif spamfilter policy
219+
220+
//** If the domain name has been changed, change the domain in all mailbox records
221+
if($this->oldDataRecord['domain'] != $this->dataRecord['domain']) {
222+
$app->uses('getconf');
223+
$mail_config = $app->getconf->get_server_config($this->dataRecord["server_id"],'mail');
224+
$mailusers = $app->db->queryAllRecords("SELECT * FROM mail_user WHERE email like '%@".addslashes($this->oldDataRecord['domain'])."'");
225+
if(is_array($mailusers)) {
226+
foreach($mailusers as $rec) {
227+
// setting Maildir, Homedir, UID and GID
228+
$mail_parts = explode("@",$rec['email']);
229+
$maildir = str_replace("[domain]",$this->dataRecord['domain'],$mail_config["maildir_path"]);
230+
$maildir = str_replace("[localpart]",$mail_parts[0],$maildir);
231+
$maildir = addslashes($maildir);
232+
//$app->db->query("UPDATE mail_user SET maildir = '$maildir' WHERE mailuser_id = ".$rec['mailuser_id']);
233+
//$rec_new = $app->db->queryOneRecord("SELECT * FROM mail_user WHERE mailuser_id = ".$rec['mailuser_id']);
234+
$app->db->datalogUpdate('mail_user', "maildir = '$maildir'", 'mailuser_id', $rec['mailuser_id']);
235+
}
236+
}
237+
} // end if domain name changed
238+
219239
}
220240

221241
}

server/plugins-available/mail_plugin.inc.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ function onLoad() {
4848
$app->plugins->registerEvent('mail_user_insert',$this->plugin_name,'user_insert');
4949
$app->plugins->registerEvent('mail_user_update',$this->plugin_name,'user_update');
5050
$app->plugins->registerEvent('mail_user_delete',$this->plugin_name,'user_delete');
51-
51+
5252

5353
}
5454

@@ -58,7 +58,7 @@ function user_insert($event_name,$data) {
5858

5959
// Create the maildir, if it does not exist
6060
if(!is_dir($data['new']['maildir'])) {
61-
mkdir($data['new']['maildir']);
61+
exec('mkdir -p '.escapeshellcmd($data['new']['maildir']));
6262
exec('chown '.$mail_config['mailuser_name'].':'.$mail_config['mailuser_group'].' '.escapeshellcmd($data['new']['maildir']));
6363
$app->log('Created Maildir: '.$data['new']['maildir'],LOGLEVEL_DEBUG);
6464
}
@@ -74,15 +74,16 @@ function user_update($event_name,$data) {
7474

7575
// Create the maildir, if it does not exist
7676
if(!is_dir($data['new']['maildir'])) {
77-
mkdir($data['new']['maildir']);
77+
exec('mkdir -p '.escapeshellcmd($data['new']['maildir']));
7878
exec('chown '.$mail_config['mailuser_name'].':'.$mail_config['mailuser_group'].' '.escapeshellcmd($data['new']['maildir']));
7979
$app->log('Created Maildir: '.$data['new']['maildir'],LOGLEVEL_DEBUG);
8080
}
8181

8282
// Move mailbox, if domain has changed and delete old mailbox
8383
if($data['new']['maildir'] != $data['old']['maildir'] && is_dir($data['old']['maildir'])) {
84-
exec('mv -f'.escapeshellcmd($data['old']['maildir']).'* '.escapeshellcmd($data['new']['maildir']));
85-
unlink($data['old']['maildir']);
84+
exec('mv -f '.escapeshellcmd($data['old']['maildir']).'* '.escapeshellcmd($data['new']['maildir']));
85+
if(is_file($data['old']['maildir'].'.ispconfig_mailsize'))exec('mv -f '.escapeshellcmd($data['old']['maildir']).'.ispconfig_mailsize '.escapeshellcmd($data['new']['maildir']));
86+
rmdir($data['old']['maildir']);
8687
$app->log('Moved Maildir from: '.$data['old']['maildir'].' to '.$data['new']['maildir'],LOGLEVEL_DEBUG);
8788
}
8889

@@ -93,7 +94,7 @@ function user_delete($event_name,$data) {
9394

9495
$old_maildir_path = escapeshellcmd($data['old']['maildir']);
9596
if(!stristr($old_maildir_path,'..') && !stristr($old_maildir_path,'*') && strlen($old_maildir_path) >= 10) {
96-
exec('rm -rf '.$old_maildir_path);
97+
exec('rm -rf '.escapeshellcmd($old_maildir_path));
9798
$app->log('Deleted the Maildir: '.$data['old']['maildir'],LOGLEVEL_DEBUG);
9899
} else {
99100
$app->log('Possible security violation when deleting the maildir: '.$data['old']['maildir'],LOGLEVEL_ERROR);

0 commit comments

Comments
 (0)