From e439d1c66e7cf115fd628309d575f9721e60f1f4 Mon Sep 17 00:00:00 2001
From: mcramer <m.cramer@pixcept.de>
Date: Mon, 10 Dec 2012 12:57:34 -0500
Subject: [PATCH] Fixed:  FS#2066 - Check if problems can occur ins ervices when the default prefix is manually changed  - Notes:    once the entry was created or modified after applying this update the current prefix is stored with the entry and kept even if the global prefix changes

---
 interface/lib/classes/tools_sites.inc.php        |   24 +++++++
 interface/web/sites/form/ftp_user.tform.php      |    8 ++
 interface/lib/classes/aps_guicontroller.inc.php  |   10 +-
 interface/web/sites/form/database_user.tform.php |    8 ++
 interface/web/sites/database_edit.php            |   14 ++--
 interface/web/sites/form/webdav_user.tform.php   |    8 ++
 interface/web/sites/ftp_user_edit.php            |   16 +++--
 install/sql/ispconfig3.sql                       |    5 +
 interface/web/sites/webdav_user_edit.php         |   14 ++--
 interface/web/sites/shell_user_edit.php          |   14 ++--
 interface/web/sites/database_user_edit.php       |   17 +++--
 interface/web/sites/form/database.tform.php      |    8 ++
 interface/web/sites/form/shell_user.tform.php    |    8 ++
 server/lib/classes/aps_installer.inc.php         |    3 
 14 files changed, 116 insertions(+), 41 deletions(-)

diff --git a/install/sql/ispconfig3.sql b/install/sql/ispconfig3.sql
index 64cbb39..ce304b3 100644
--- a/install/sql/ispconfig3.sql
+++ b/install/sql/ispconfig3.sql
@@ -526,6 +526,7 @@
   `server_id` int(11) unsigned NOT NULL default '0',
   `parent_domain_id` int(11) unsigned NOT NULL default '0',
   `username` varchar(64) default NULL,
+  `username_prefix` varchar(50) NOT NULL default '',
   `password` varchar(64) default NULL,
   `quota_size` bigint(20) NOT NULL default '-1',
   `active` enum('n','y') NOT NULL default 'y',
@@ -1179,6 +1180,7 @@
   `server_id` int(11) unsigned NOT NULL default '0',
   `parent_domain_id` int(11) unsigned NOT NULL default '0',
   `username` varchar(64) default NULL,
+  `username_prefix` varchar(50) NOT NULL default '',
   `password` varchar(64) default NULL,
   `quota_size` bigint(20) NOT NULL default '-1',
   `active` enum('n','y') NOT NULL default 'y',
@@ -1608,6 +1610,7 @@
   `server_id` int(11) unsigned NOT NULL DEFAULT '0',
   `parent_domain_id` int(11) unsigned NOT NULL DEFAULT '0',
   `username` varchar(64) DEFAULT NULL,
+  `username_prefix` varchar(50) NOT NULL default '',
   `password` varchar(64) DEFAULT NULL,
   `active` enum('n','y') NOT NULL DEFAULT 'y',
   `dir` varchar(255) DEFAULT NULL,
@@ -1648,6 +1651,7 @@
   `parent_domain_id` int(11) unsigned NOT NULL DEFAULT  '0',
   `type` varchar(16) NOT NULL DEFAULT 'y',
   `database_name` varchar(64) DEFAULT NULL,
+  `database_name_prefix` varchar(50) NOT NULL default '',
   `database_user_id` int(11) unsigned DEFAULT NULL,
   `database_ro_user_id` int(11) unsigned DEFAULT NULL,
   `database_charset` varchar(64) DEFAULT NULL,
@@ -1676,6 +1680,7 @@
   `sys_perm_other` varchar(5) DEFAULT NULL,
   `server_id` int(11) UNSIGNED NOT NULL DEFAULT '0',
   `database_user` varchar(64) DEFAULT NULL,
+  `database_user_prefix` varchar(50) NOT NULL default '',
   `database_password` varchar(64) DEFAULT NULL,
   PRIMARY KEY (`database_user_id`)
 ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
diff --git a/interface/lib/classes/aps_guicontroller.inc.php b/interface/lib/classes/aps_guicontroller.inc.php
index c5feec4..f8a13b3 100644
--- a/interface/lib/classes/aps_guicontroller.inc.php
+++ b/interface/lib/classes/aps_guicontroller.inc.php
@@ -285,7 +285,7 @@
 			}
 			//* Find a free db username for the app
 			for($n = 1; $n <= 1000; $n++) {
-				$mysql_db_user = ($dbname_prefix != '' ? $dbname_prefix.'aps'.$n : uniqid('aps'));
+				$mysql_db_user = ($dbuser_prefix != '' ? $dbuser_prefix.'aps'.$n : uniqid('aps'));
 				$tmp = $app->db->queryOneRecord("SELECT count(database_user_id) as number FROM web_database_user WHERE database_user = '".$app->db->quote($mysql_db_user)."'");
 				if($tmp['number'] == 0) break;
 			}
@@ -293,13 +293,13 @@
 			$mysql_db_password = $settings['main_database_password'];
 			
 			//* Create the mysql database user
-			$insert_data = "(`sys_userid`, `sys_groupid`, `sys_perm_user`, `sys_perm_group`, `sys_perm_other`, `server_id`, `database_user`, `database_password`) 
-					  VALUES( ".$websrv['sys_userid'].", ".$websrv['sys_groupid'].", 'riud', '".$websrv['sys_perm_group']."', '', 0, '$mysql_db_user', PASSWORD('$mysql_db_password'))";
+			$insert_data = "(`sys_userid`, `sys_groupid`, `sys_perm_user`, `sys_perm_group`, `sys_perm_other`, `server_id`, `database_user`, `database_user_prefix`, `database_password`) 
+					  VALUES( ".$websrv['sys_userid'].", ".$websrv['sys_groupid'].", 'riud', '".$websrv['sys_perm_group']."', '', 0, '$mysql_db_user', '".$app->db->quote($dbuser_prefix) . "', PASSWORD('$mysql_db_password'))";
 			$mysql_db_user_id = $app->db->datalogInsert('web_database_user', $insert_data, 'database_user_id');
             
 			//* Create the mysql database
-			$insert_data = "(`sys_userid`, `sys_groupid`, `sys_perm_user`, `sys_perm_group`, `sys_perm_other`, `server_id`, `parent_domain_id`, `type`, `database_name`, `database_user_id`, `database_ro_user_id`, `database_charset`, `remote_access`, `remote_ips`, `backup_copies`, `active`, `backup_interval`) 
-					  VALUES( ".$websrv['sys_userid'].", ".$websrv['sys_groupid'].", 'riud', '".$websrv['sys_perm_group']."', '', $mysql_db_server_id, ".$websrv['domain_id'].", 'mysql', '$mysql_db_name', '$mysql_db_user_id', 0, '', '$mysql_db_remote_access', '$mysql_db_remote_ips', ".$websrv['backup_copies'].", 'y', '".$websrv['backup_interval']."')";
+			$insert_data = "(`sys_userid`, `sys_groupid`, `sys_perm_user`, `sys_perm_group`, `sys_perm_other`, `server_id`, `parent_domain_id`, `type`, `database_name`, `database_name_prefix`, `database_user_id`, `database_ro_user_id`, `database_charset`, `remote_access`, `remote_ips`, `backup_copies`, `active`, `backup_interval`) 
+					  VALUES( ".$websrv['sys_userid'].", ".$websrv['sys_groupid'].", 'riud', '".$websrv['sys_perm_group']."', '', $mysql_db_server_id, ".$websrv['domain_id'].", 'mysql', '$mysql_db_name', '" . $app->db->quote($dbname_prefix) . "', '$mysql_db_user_id', 0, '', '$mysql_db_remote_access', '$mysql_db_remote_ips', ".$websrv['backup_copies'].", 'y', '".$websrv['backup_interval']."')";
 			$app->db->datalogInsert('web_database', $insert_data, 'database_id');
 			
 			//* Add db details to package settings
diff --git a/interface/lib/classes/tools_sites.inc.php b/interface/lib/classes/tools_sites.inc.php
index b440754..403f323 100644
--- a/interface/lib/classes/tools_sites.inc.php
+++ b/interface/lib/classes/tools_sites.inc.php
@@ -54,7 +54,29 @@
         }
         return $name;
     }
-
+    
+    function removePrefix($name, $currentPrefix, $globalPrefix) {
+        if($name == "") return "";
+        
+        if($currentPrefix == '#') return $name; // # = empty prefix, do not change name
+        if($currentPrefix === '') $currentPrefix = $globalPrefix; // entry has no prefix set, maybe it was created before this function was introduced
+        
+        if($currentPrefix === '') return $name; // no current prefix and global prefix is empty -> nothing to remove here.
+        
+        return preg_replace('/^' . preg_quote($currentPrefix, '/') . '/', '', $name); // return name without prefix
+    }
+    
+    function getPrefix($currentPrefix, $userPrefix, $adminPrefix = false) {
+        global $app;
+        
+        if($currentPrefix !== '') return ($currentPrefix == '#' ? '' : $currentPrefix); // return the currently set prefix for this entry (# = empty)
+        
+        if($adminPrefix === false) $adminPrefix = $userPrefix;
+        
+        if($_SESSION["s"]["user"]["typ"] == 'admin' || $app->auth->has_clients($_SESSION['s']['user']['userid'])) return $adminPrefix;
+        else return $userPrefix;
+    }
+    
     function getClientName($dataRecord) {
         global $app, $conf;
         if($_SESSION["s"]["user"]["typ"] != 'admin' && !$app->auth->has_clients($_SESSION['s']['user']['userid'])) {
diff --git a/interface/web/sites/database_edit.php b/interface/web/sites/database_edit.php
index 393ec38..ccd6725 100644
--- a/interface/web/sites/database_edit.php
+++ b/interface/web/sites/database_edit.php
@@ -116,14 +116,10 @@
 		
 		if ($this->dataRecord['database_name'] != ""){
 			/* REMOVE the restriction */
-			$app->tpl->setVar("database_name", str_replace($dbname_prefix , '', $this->dataRecord['database_name']));
+			$app->tpl->setVar("database_name", $app->tools_sites->removePrefix($this->dataRecord['database_name'], $this->dataRecord['database_name_prefix'], $dbname_prefix));
 		}
-		
-		if($_SESSION["s"]["user"]["typ"] == 'admin' || $app->auth->has_clients($_SESSION['s']['user']['userid'])) {
-			$app->tpl->setVar("database_name_prefix", $global_config['dbname_prefix']);
-		} else {
-			$app->tpl->setVar("database_name_prefix", $dbname_prefix);
-		}
+        
+        $app->tpl->setVar("database_name_prefix", $app->tools_sites->getPrefix($this->dataRecord['database_name_prefix'], $dbname_prefix, $global_config['dbname_prefix']));
 		
 		if($this->id > 0) {
 			//* we are editing a existing record
@@ -200,6 +196,9 @@
 		
 		//* Prevent that the database name and charset is changed
 		$old_record = $app->tform->getDataRecord($this->id);
+        $dbname_prefix = $app->tools_sites->getPrefix($old_record['database_name_prefix'], $dbname_prefix);
+        $this->dataRecord['database_name_prefix'] = ($dbname_prefix === '' ? '#' : $dbname_prefix);
+        
 		if($old_record["database_name"] != $dbname_prefix . $this->dataRecord["database_name"]) {
 			$app->tform->errorMessage .= $app->tform->wordbook["database_name_change_txt"].'<br />';
 		}
@@ -270,6 +269,7 @@
 		$app->uses('getconf,tools_sites');
 		$global_config = $app->getconf->get_global_config('sites');
 		$dbname_prefix = $app->tools_sites->replacePrefix($global_config['dbname_prefix'], $this->dataRecord);
+        $this->dataRecord['database_name_prefix'] = ($dbname_prefix === '' ? '#' : $dbname_prefix);
 		
 		if(strlen($dbname_prefix . $this->dataRecord['database_name']) > 64) $app->tform->errorMessage .= str_replace('{db}',$dbname_prefix . $this->dataRecord['database_name'],$app->tform->wordbook["database_name_error_len"]).'<br />';
 		
diff --git a/interface/web/sites/database_user_edit.php b/interface/web/sites/database_user_edit.php
index 86b1382..f54362c 100644
--- a/interface/web/sites/database_user_edit.php
+++ b/interface/web/sites/database_user_edit.php
@@ -100,14 +100,11 @@
         
 		if ($this->dataRecord['database_user'] != ""){
 			/* REMOVE the restriction */
-			$app->tpl->setVar("database_user", str_replace($dbuser_prefix , '', $this->dataRecord['database_user']));
+			$app->tpl->setVar("database_user", $app->tools_sites->removePrefix($this->dataRecord['database_user'], $this->dataRecord['database_user_prefix'], $dbuser_prefix));
 		}
 		
-		if($_SESSION["s"]["user"]["typ"] == 'admin' || $app->auth->has_clients($_SESSION['s']['user']['userid'])) {
-			$app->tpl->setVar("database_user_prefix", $global_config['dbuser_prefix']);
-		} else {
-			$app->tpl->setVar("database_user_prefix", $dbuser_prefix);
-		}
+    
+		$app->tpl->setVar("database_user_prefix", $app->tools_sites->getPrefix($this->dataRecord['database_user_prefix'], $dbuser_prefix, $global_config['dbuser_prefix']));
 		
 		parent::onShowEnd();
 	}
@@ -128,6 +125,11 @@
 		$global_config = $app->getconf->get_global_config('sites');
 		$dbuser_prefix = $app->tools_sites->replacePrefix($global_config['dbuser_prefix'], $this->dataRecord);
 
+        $this->oldDataRecord = $app->db->queryOneRecord("SELECT * FROM web_database_user WHERE database_user_id = '".$this->id."'");
+        
+        $dbuser_prefix = $app->tools_sites->getPrefix($this->oldDataRecord['database_user_prefix'], $dbuser_prefix);
+        $this->dataRecord['database_user_prefix'] = ($dbuser_prefix === '' ? '#' : $dbuser_prefix);
+        
 		//* Database username shall not be empty
 		if($this->dataRecord['database_user'] == '') $app->tform->errorMessage .= $app->tform->wordbook["database_user_error_empty"].'<br />';
 
@@ -146,7 +148,6 @@
 		}
 		
         $this->dataRecord['server_id'] = $conf['server_id'];
-        $this->oldDataRecord = $app->db->queryOneRecord("SELECT * FROM web_database_user WHERE database_user_id = '".$this->id."'");
         
 		parent::onBeforeUpdate();
 	}
@@ -162,6 +163,8 @@
 		$global_config = $app->getconf->get_global_config('sites');
 		$dbuser_prefix = $app->tools_sites->replacePrefix($global_config['dbuser_prefix'], $this->dataRecord);
 		
+        $this->dataRecord['database_user_prefix'] = ($dbuser_prefix === '' ? '#' : $dbuser_prefix);
+        
 		if(strlen($dbuser_prefix . $this->dataRecord['database_user']) > 16) $app->tform->errorMessage .= str_replace('{user}',$dbuser_prefix . $this->dataRecord['database_user'],$app->tform->wordbook["database_user_error_len"]).'<br />';
 		
 		//* Check database user against blacklist
diff --git a/interface/web/sites/form/database.tform.php b/interface/web/sites/form/database.tform.php
index c022060..e390f72 100644
--- a/interface/web/sites/form/database.tform.php
+++ b/interface/web/sites/form/database.tform.php
@@ -106,6 +106,14 @@
 			'maxlength'	=> '255',
 			'searchable' => 1
 		),
+		'database_name_prefix' => array (
+			'datatype'	=> 'VARCHAR',
+			'formtype'	=> 'TEXT',
+			'default'	=> '',
+			'value'		=> '',
+			'width'		=> '30',
+			'maxlength'	=> '25'
+		),
 		'database_user_id' => array (
 			'datatype'	=> 'INTEGER',
 			'formtype'	=> 'SELECT',
diff --git a/interface/web/sites/form/database_user.tform.php b/interface/web/sites/form/database_user.tform.php
index 7e84f1c..2bac933 100644
--- a/interface/web/sites/form/database_user.tform.php
+++ b/interface/web/sites/form/database_user.tform.php
@@ -91,6 +91,14 @@
 			'maxlength'	=> '255',
 			'searchable' => 1
 		),
+		'database_user_prefix' => array (
+			'datatype'	=> 'VARCHAR',
+			'formtype'	=> 'TEXT',
+			'default'	=> '',
+			'value'		=> '',
+			'width'		=> '30',
+			'maxlength'	=> '25'
+		),
 		'database_password' => array (
 			'datatype'	=> 'VARCHAR',
 			'formtype'	=> 'PASSWORD',
diff --git a/interface/web/sites/form/ftp_user.tform.php b/interface/web/sites/form/ftp_user.tform.php
index 658d951..543d863 100644
--- a/interface/web/sites/form/ftp_user.tform.php
+++ b/interface/web/sites/form/ftp_user.tform.php
@@ -100,6 +100,14 @@
 			'maxlength'	=> '255',
 			'searchable' => 1
 		),
+		'username_prefix' => array (
+			'datatype'	=> 'VARCHAR',
+			'formtype'	=> 'TEXT',
+			'default'	=> '',
+			'value'		=> '',
+			'width'		=> '30',
+			'maxlength'	=> '25'
+		),
 		'password' => array (
 			'datatype'	=> 'VARCHAR',
 			'formtype'	=> 'PASSWORD',
diff --git a/interface/web/sites/form/shell_user.tform.php b/interface/web/sites/form/shell_user.tform.php
index 061dd29..d698a7a 100644
--- a/interface/web/sites/form/shell_user.tform.php
+++ b/interface/web/sites/form/shell_user.tform.php
@@ -100,6 +100,14 @@
 			'maxlength'	=> '255',
 			'searchable' => 1
 		),
+		'username_prefix' => array (
+			'datatype'	=> 'VARCHAR',
+			'formtype'	=> 'TEXT',
+			'default'	=> '',
+			'value'		=> '',
+			'width'		=> '30',
+			'maxlength'	=> '25'
+		),
 		'password' => array (
 			'datatype'	=> 'VARCHAR',
 			'formtype'	=> 'PASSWORD',
diff --git a/interface/web/sites/form/webdav_user.tform.php b/interface/web/sites/form/webdav_user.tform.php
index 5d423b6..29a52dc 100644
--- a/interface/web/sites/form/webdav_user.tform.php
+++ b/interface/web/sites/form/webdav_user.tform.php
@@ -93,6 +93,14 @@
 			'width'		=> '30',
 			'maxlength'	=> '255'
 		),
+		'username_prefix' => array (
+			'datatype'	=> 'VARCHAR',
+			'formtype'	=> 'TEXT',
+			'default'	=> '',
+			'value'		=> '',
+			'width'		=> '30',
+			'maxlength'	=> '25'
+		),
 		'password' => array (
 			'datatype'	=> 'VARCHAR',
 			'encryption' => 'CLEARTEXT', 
diff --git a/interface/web/sites/ftp_user_edit.php b/interface/web/sites/ftp_user_edit.php
index cd52d17..674178d 100644
--- a/interface/web/sites/ftp_user_edit.php
+++ b/interface/web/sites/ftp_user_edit.php
@@ -79,14 +79,10 @@
 		
 		if ($this->dataRecord['username'] != ""){
 			/* REMOVE the restriction */
-			$app->tpl->setVar("username", preg_replace('/'.$ftpuser_prefix.'/' , '', $this->dataRecord['username'], 1));
+			$app->tpl->setVar("username", $app->tools_sites->removePrefix($this->dataRecord['username'], $this->dataRecord['username_prefix'], $ftpuser_prefix));
 		}
-		if($_SESSION["s"]["user"]["typ"] == 'admin' || $app->auth->has_clients($_SESSION['s']['user']['userid'])) {
-			$app->tpl->setVar("username_prefix", $global_config['ftpuser_prefix']);
-		}
-		else {
-			$app->tpl->setVar("username_prefix", $ftpuser_prefix);
-		}
+        
+        $app->tpl->setVar("username_prefix", $app->tools_sites->getPrefix($this->dataRecord['username_prefix'], $ftpuser_prefix, $global_config['ftpuser_prefix']));
 
 		parent::onShowEnd();
 	}
@@ -116,6 +112,8 @@
 		$app->uses('getconf,tools_sites');
 		$global_config = $app->getconf->get_global_config('sites');
 		$ftpuser_prefix = $app->tools_sites->replacePrefix($global_config['ftpuser_prefix'], $this->dataRecord);
+
+        $this->dataRecord['username_prefix'] = ($ftpuser_prefix === '' ? '#' : $ftpuser_prefix);
 		
 		if ($app->tform->errorMessage == '') {
 			$this->dataRecord['username'] = $ftpuser_prefix . $this->dataRecord['username'];
@@ -151,6 +149,10 @@
 		$global_config = $app->getconf->get_global_config('sites');
 		$ftpuser_prefix = $app->tools_sites->replacePrefix($global_config['ftpuser_prefix'], $this->dataRecord);
 		
+        $old_record = $app->tform->getDataRecord($this->id);
+        $ftpuser_prefix = $app->tools_sites->getPrefix($old_record['username_prefix'], $ftpuser_prefix);
+        $this->dataRecord['username_prefix'] = ($ftpuser_prefix === '' ? '#' : $ftpuser_prefix);
+        
 		/* restrict the names */
 		if ($app->tform->errorMessage == '') {
 			$this->dataRecord['username'] = $ftpuser_prefix . $this->dataRecord['username'];
diff --git a/interface/web/sites/shell_user_edit.php b/interface/web/sites/shell_user_edit.php
index 8f2a93f..f6b145f 100644
--- a/interface/web/sites/shell_user_edit.php
+++ b/interface/web/sites/shell_user_edit.php
@@ -79,13 +79,10 @@
 		
 		if ($this->dataRecord['username'] != ""){
 			/* REMOVE the restriction */
-			$app->tpl->setVar("username", preg_replace('/'.$shelluser_prefix.'/', '', $this->dataRecord['username'], 1));
+			$app->tpl->setVar("username", $app->tools_sites->removePrefix($this->dataRecord['username'], $this->dataRecord['username_prefix'], $shelluser_prefix));
 		}
-		if($_SESSION["s"]["user"]["typ"] == 'admin' || $app->auth->has_clients($_SESSION['s']['user']['userid'])) {
-			$app->tpl->setVar("username_prefix", $global_config['shelluser_prefix']);
-		} else {
-			$app->tpl->setVar("username_prefix", $shelluser_prefix);
-		}
+        
+        $app->tpl->setVar("username_prefix", $app->tools_sites->getPrefix($this->dataRecord['username_prefix'], $shelluser_prefix, $global_config['shelluser_prefix']));
 		
 		if($this->id > 0) {
 			//* we are editing a existing record
@@ -138,6 +135,7 @@
 			$global_config = $app->getconf->get_global_config('sites');
 			$shelluser_prefix = $app->tools_sites->replacePrefix($global_config['shelluser_prefix'], $this->dataRecord);
 			
+            $this->dataRecord['username_prefix'] = ($shelluser_prefix === '' ? '#' : $shelluser_prefix);
 			/* restrict the names */
 			$this->dataRecord['username'] = $shelluser_prefix . $this->dataRecord['username'];
 		}
@@ -184,6 +182,10 @@
 			$global_config = $app->getconf->get_global_config('sites');
 			$shelluser_prefix = $app->tools_sites->replacePrefix($global_config['shelluser_prefix'], $this->dataRecord);
 			
+            $old_record = $app->tform->getDataRecord($this->id);
+            $shelluser_prefix = $app->tools_sites->getPrefix($old_record['username_prefix'], $shelluser_prefix);
+            $this->dataRecord['username_prefix'] = ($shelluser_prefix === '' ? '#' : $shelluser_prefix);
+            
 			/* restrict the names */
 			$this->dataRecord['username'] = $shelluser_prefix . $this->dataRecord['username'];
 		}
diff --git a/interface/web/sites/webdav_user_edit.php b/interface/web/sites/webdav_user_edit.php
index 8f11bb0..54261e2 100644
--- a/interface/web/sites/webdav_user_edit.php
+++ b/interface/web/sites/webdav_user_edit.php
@@ -78,14 +78,11 @@
 
 		if ($this->dataRecord['username'] != "") {
 			/* REMOVE the restriction */
-			$app->tpl->setVar("username", str_replace($webdavuser_prefix , '', $this->dataRecord['username']));
+			$app->tpl->setVar("username", $app->tools_sites->removePrefix($this->dataRecord['username'], $this->dataRecord['username_prefix'], $webdavuser_prefix));
 		}
-		if($_SESSION["s"]["user"]["typ"] == 'admin' || $app->auth->has_clients($_SESSION['s']['user']['userid'])) {
-			$app->tpl->setVar("username_prefix", $global_config['webdavuser_prefix']);
-		} else {
-			$app->tpl->setVar("username_prefix", $webdavuser_prefix);
-		}
-
+        
+        $app->tpl->setVar("username_prefix", $app->tools_sites->getPrefix($this->dataRecord['username_prefix'], $webdavuser_prefix, $global_config['webdavuser_prefix']));
+		
 		if($this->id > 0) {
 			//* we are editing a existing record
 			$app->tpl->setVar("edit_disabled", 1);
@@ -131,6 +128,8 @@
 			$global_config = $app->getconf->get_global_config('sites');
 			$webdavuser_prefix = $app->tools_sites->replacePrefix($global_config['webdavuser_prefix'], $this->dataRecord);
 
+            $this->dataRecord['username_prefix'] = ($webdavuser_prefix === '' ? '#' : $webdavuser_prefix);
+            
 			/* restrict the names */
 			$this->dataRecord['username'] = $webdavuser_prefix . $this->dataRecord['username'];
 
@@ -167,6 +166,7 @@
 		$data = $app->db->queryOneRecord("SELECT * FROM webdav_user WHERE webdav_user_id = ".$app->functions->intval($this->id));
 		$this->dataRecord["username"] = $data['username'];
 		$this->dataRecord["dir"]      = $data['dir'];
+		$this->dataRecord['username_prefix'] = $data['username_prefix'];
 		$passwordOld = $data['password'];
 
 		/*
diff --git a/server/lib/classes/aps_installer.inc.php b/server/lib/classes/aps_installer.inc.php
index f5b661c..9bb8db4 100644
--- a/server/lib/classes/aps_installer.inc.php
+++ b/server/lib/classes/aps_installer.inc.php
@@ -294,7 +294,8 @@
         $db_id = parent::getXPathValue($sxe, '//db:id');
         if(empty($db_id)) return; // No database needed
         
-		/*
+		/* WARNING: if this will ever be uncommented please check the updated prefix handling for user and db names!!!
+         * 
         // Set the database owner to the domain owner
         // ISPConfig identifies the owner by the sys_groupid (not sys_userid!)
         // so sys_userid can be set to any value

--
Gitblit v1.9.1