From 95870536d03191d8c95bc989076109ac0ad24bc3 Mon Sep 17 00:00:00 2001
From: tbrehm <t.brehm@ispconfig.org>
Date: Tue, 08 Jan 2008 10:00:40 -0500
Subject: [PATCH] Added a function to update all email addresses when a mail domain is changed.

---
 install/sql/ispconfig3.sql                   |    2 
 interface/lib/classes/tform_actions.inc.php  |    5 +
 interface/web/mail/form/mail_user.tform.php  |    2 
 interface/lib/classes/tform.inc.php          |   15 -----
 interface/web/mail/mail_domain_edit.php      |   20 ++++++
 server/plugins-available/mail_plugin.inc.php |   13 ++--
 interface/lib/classes/db_mysql.inc.php       |   71 +++++++++++++++++++++++
 7 files changed, 103 insertions(+), 25 deletions(-)

diff --git a/install/sql/ispconfig3.sql b/install/sql/ispconfig3.sql
index c8188f0..ebd094f 100644
--- a/install/sql/ispconfig3.sql
+++ b/install/sql/ispconfig3.sql
@@ -407,7 +407,7 @@
   `uid` int(10) unsigned NOT NULL default '5000',
   `gid` int(10) unsigned NOT NULL default '5000',
   `maildir` varchar(255) NOT NULL default '',
-  `quota` int(11) NOT NULL,
+  `quota` int(11) NOT NULL default '0',
   `homedir` varchar(255) NOT NULL,
   `autoresponder` enum('n','y') NOT NULL default 'n',
   `autoresponder_text` tinytext NOT NULL,
diff --git a/interface/lib/classes/db_mysql.inc.php b/interface/lib/classes/db_mysql.inc.php
index eeb7c59..54b210c 100644
--- a/interface/lib/classes/db_mysql.inc.php
+++ b/interface/lib/classes/db_mysql.inc.php
@@ -214,6 +214,77 @@
             if($debug == 1){ echo 'mySQL Error Message: '.$this->errorMessage; }
         }
     }
+	
+	//** 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()
     {
diff --git a/interface/lib/classes/tform.inc.php b/interface/lib/classes/tform.inc.php
index e5644c3..61ddefd 100644
--- a/interface/lib/classes/tform.inc.php
+++ b/interface/lib/classes/tform.inc.php
@@ -875,15 +875,6 @@
                 } else {
                         $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();
 				
@@ -933,12 +924,6 @@
 								}
                         }
                 }
-				
-				/*
-				echo "<pre>";
-				print_r($diffrec_full);
-				echo "</pre>";
-				*/
 				
 				// 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;
diff --git a/interface/lib/classes/tform_actions.inc.php b/interface/lib/classes/tform_actions.inc.php
index 7fb553f..c193081 100644
--- a/interface/lib/classes/tform_actions.inc.php
+++ b/interface/lib/classes/tform_actions.inc.php
@@ -41,6 +41,7 @@
         var $activeTab;
         var $dataRecord;
         var $plugins = array();
+		var $oldDataRecord; // This array is only filled during updates and when db_history is enabled.
 
         function onLoad() {
                 global $app, $conf, $tform_def_file;
@@ -104,7 +105,7 @@
                 if($app->tform->errorMessage == '') {
 						
 						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
@@ -124,7 +125,7 @@
 						// Write data history (sys_datalog)
 						if($app->tform->formDef['db_history'] == 'yes') {
 							$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($old_data_record);
 						}
diff --git a/interface/web/mail/form/mail_user.tform.php b/interface/web/mail/form/mail_user.tform.php
index eba1e1f..0015d23 100644
--- a/interface/web/mail/form/mail_user.tform.php
+++ b/interface/web/mail/form/mail_user.tform.php
@@ -94,7 +94,7 @@
 			'validators'	=> array ( 	0 => array (	'type'	=> 'ISINT',
 														'errmsg'=> 'quota_error_isint'),
 									),
-			'default'	=> '',
+			'default'	=> '0',
 			'value'		=> '',
 			'width'		=> '30',
 			'maxlength'	=> '255'
diff --git a/interface/web/mail/mail_domain_edit.php b/interface/web/mail/mail_domain_edit.php
index a32190d..91847d7 100644
--- a/interface/web/mail/mail_domain_edit.php
+++ b/interface/web/mail/mail_domain_edit.php
@@ -216,6 +216,26 @@
 				$app->db->query($sql);
 			}
 		} // 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
+		
 	}
 	
 }
diff --git a/server/plugins-available/mail_plugin.inc.php b/server/plugins-available/mail_plugin.inc.php
index 95ef769..9140708 100644
--- a/server/plugins-available/mail_plugin.inc.php
+++ b/server/plugins-available/mail_plugin.inc.php
@@ -48,7 +48,7 @@
 		$app->plugins->registerEvent('mail_user_insert',$this->plugin_name,'user_insert');
 		$app->plugins->registerEvent('mail_user_update',$this->plugin_name,'user_update');
 		$app->plugins->registerEvent('mail_user_delete',$this->plugin_name,'user_delete');
-
+		
 		
 	}
 	
@@ -58,7 +58,7 @@
 		
 		// Create the maildir, if it does not exist
 		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']));
 			$app->log('Created Maildir: '.$data['new']['maildir'],LOGLEVEL_DEBUG);
 		}
@@ -74,15 +74,16 @@
 		
 		// Create the maildir, if it does not exist
 		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']));
 			$app->log('Created Maildir: '.$data['new']['maildir'],LOGLEVEL_DEBUG);
 		}
 		
 		// Move mailbox, if domain has changed and delete old mailbox
 		if($data['new']['maildir'] != $data['old']['maildir'] && is_dir($data['old']['maildir'])) {
-			exec('mv -f'.escapeshellcmd($data['old']['maildir']).'* '.escapeshellcmd($data['new']['maildir']));
-			unlink($data['old']['maildir']);
+			exec('mv -f '.escapeshellcmd($data['old']['maildir']).'* '.escapeshellcmd($data['new']['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);
 		}
 		
@@ -93,7 +94,7 @@
 		
 		$old_maildir_path = escapeshellcmd($data['old']['maildir']);
 		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);
 		} else {
 			$app->log('Possible security violation when deleting the maildir: '.$data['old']['maildir'],LOGLEVEL_ERROR);

--
Gitblit v1.9.1