From f2ded16c1b28b471a5864aa7cfb96f68c44118f8 Mon Sep 17 00:00:00 2001
From: ftimme <ft@falkotimme.com>
Date: Wed, 05 Sep 2012 11:13:10 -0400
Subject: [PATCH] - Fixed FS#2318 - Starting nginx nginx: [emerg] duplicate listen options for [ipv6]:80

---
 server/plugins-available/apache2_plugin.inc.php |  508 ++++++++++++++++++++++++++++++++++++++------------------
 1 files changed, 346 insertions(+), 162 deletions(-)

diff --git a/server/plugins-available/apache2_plugin.inc.php b/server/plugins-available/apache2_plugin.inc.php
index 2967273..5012095 100644
--- a/server/plugins-available/apache2_plugin.inc.php
+++ b/server/plugins-available/apache2_plugin.inc.php
@@ -103,7 +103,7 @@
 			$app->log("CA path error, file does not exist:".$web_config['CA_path'].'/openssl.conf',LOGLEVEL_ERROR);	
 		
 		//* Only vhosts can have a ssl cert
-		if($data["new"]["type"] != "vhost") return;
+		if($data["new"]["type"] != "vhost" && $data["new"]["type"] != "vhostsubdomain") return;
 
 		// if(!is_dir($data['new']['document_root'].'/ssl')) exec('mkdir -p '.$data['new']['document_root'].'/ssl');
 		if(!is_dir($data['new']['document_root'].'/ssl')) $app->system->mkdirpath($data['new']['document_root'].'/ssl');
@@ -195,11 +195,12 @@
 			@$app->system->unlink($rand_file);
 			$ssl_request = $app->db->quote($app->system->file_get_contents($csr_file));
 			$ssl_cert = $app->db->quote($app->system->file_get_contents($crt_file));
+			$ssl_key2 = $app->db->quote($app->system->file_get_contents($key_file2));
 			/* Update the DB of the (local) Server */
-			$app->db->query("UPDATE web_domain SET ssl_request = '$ssl_request', ssl_cert = '$ssl_cert' WHERE domain = '".$data['new']['domain']."'");
+			$app->db->query("UPDATE web_domain SET ssl_request = '$ssl_request', ssl_cert = '$ssl_cert', ssl_key = '$ssl_key2' WHERE domain = '".$data['new']['domain']."'");
 			$app->db->query("UPDATE web_domain SET ssl_action = '' WHERE domain = '".$data['new']['domain']."'");
 			/* Update also the master-DB of the Server-Farm */
-			$app->dbmaster->query("UPDATE web_domain SET ssl_request = '$ssl_request', ssl_cert = '$ssl_cert' WHERE domain = '".$data['new']['domain']."'");
+			$app->dbmaster->query("UPDATE web_domain SET ssl_request = '$ssl_request', ssl_cert = '$ssl_cert', ssl_key = '$ssl_key2' WHERE domain = '".$data['new']['domain']."'");
 			$app->dbmaster->query("UPDATE web_domain SET ssl_action = '' WHERE domain = '".$data['new']['domain']."'");
 		}
 
@@ -209,7 +210,7 @@
 			$ssl_dir = $data["new"]["document_root"]."/ssl";
 			$domain = ($data["new"]["ssl_domain"] != '')?$data["new"]["ssl_domain"]:$data["new"]["domain"];
 			$key_file = $ssl_dir.'/'.$domain.'.key.org';
-		$key_file2 = $ssl_dir.'/'.$domain.'.key';
+			$key_file2 = $ssl_dir.'/'.$domain.'.key';
 			$csr_file = $ssl_dir.'/'.$domain.".csr";
 			$crt_file = $ssl_dir.'/'.$domain.".crt";
 			$bundle_file = $ssl_dir.'/'.$domain.".bundle";
@@ -225,6 +226,7 @@
 			if(trim($data["new"]["ssl_request"]) != '') $app->system->file_put_contents($csr_file,$data["new"]["ssl_request"]);
 			if(trim($data["new"]["ssl_cert"]) != '') $app->system->file_put_contents($crt_file,$data["new"]["ssl_cert"]);
 			if(trim($data["new"]["ssl_bundle"]) != '') $app->system->file_put_contents($bundle_file,$data["new"]["ssl_bundle"]);
+			if(trim($data["new"]["ssl_key"]) != '') $app->system->file_put_contents($key_file2,$data["new"]["ssl_key"]);
 			
 			/* Update the DB of the (local) Server */
 			$app->db->query("UPDATE web_domain SET ssl_action = '' WHERE domain = '".$data['new']['domain']."'");
@@ -277,7 +279,7 @@
 
 		if($this->action != 'insert') $this->action = 'update';
 
-		if($data['new']['type'] != 'vhost' && $data['new']['parent_domain_id'] > 0) {
+		if($data['new']['type'] != 'vhost' && $data['new']['type'] != 'vhostsubdomain' && $data['new']['parent_domain_id'] > 0) {
 
 			$old_parent_domain_id = intval($data['old']['parent_domain_id']);
 			$new_parent_domain_id = intval($data['new']['parent_domain_id']);
@@ -323,6 +325,17 @@
 			return 0;
 		}
 		
+        $web_folder = 'web';
+        $log_folder = 'log';
+        if($data['new']['type'] == 'vhostsubdomain') {
+            $tmp = $app->db->queryOneRecord('SELECT `domain` FROM web_domain WHERE domain_id = '.intval($data['new']['parent_domain_id']));
+            $subdomain_host = preg_replace('/^(.*)\.' . preg_quote($tmp['domain'], '/') . '$/', '$1', $data['new']['domain']);
+            if($subdomain_host == '') $subdomain_host = 'web'.$data['new']['domain_id'];
+            $web_folder = $data['new']['web_folder'];
+            $log_folder .= '/' . $subdomain_host;
+            unset($tmp);
+        }
+
 		// Create group and user, if not exist
 		$app->uses('system');
 		
@@ -435,9 +448,9 @@
 		// Check if the directories are there and create them if necessary.
 		$app->system->web_folder_protection($data['new']['document_root'],false);
 		
-		if(!is_dir($data['new']['document_root'].'/web')) $app->system->mkdirpath($data['new']['document_root'].'/web');
-		if(!is_dir($data['new']['document_root'].'/web/error') and $data['new']['errordocs']) $app->system->mkdirpath($data['new']['document_root'].'/web/error');
-		//if(!is_dir($data['new']['document_root'].'/log')) exec('mkdir -p '.$data['new']['document_root'].'/log');
+		if(!is_dir($data['new']['document_root'].'/' . $web_folder)) $app->system->mkdirpath($data['new']['document_root'].'/' . $web_folder);
+		if(!is_dir($data['new']['document_root'].'/' . $web_folder . '/error') and $data['new']['errordocs']) $app->system->mkdirpath($data['new']['document_root'].'/' . $web_folder . '/error');
+		//if(!is_dir($data['new']['document_root'].'/'.$log_folder)) exec('mkdir -p '.$data['new']['document_root'].'/'.$log_folder);
 		if(!is_dir($data['new']['document_root'].'/ssl')) $app->system->mkdirpath($data['new']['document_root'].'/ssl');
 		if(!is_dir($data['new']['document_root'].'/cgi-bin')) $app->system->mkdirpath($data['new']['document_root'].'/cgi-bin');
 		if(!is_dir($data['new']['document_root'].'/tmp')) $app->system->mkdirpath($data['new']['document_root'].'/tmp');
@@ -455,19 +468,20 @@
 		// Remove the symlink for the site, if site is renamed
 		if($this->action == 'update' && $data['old']['domain'] != '' && $data['new']['domain'] != $data['old']['domain']) {
 			if(is_dir('/var/log/ispconfig/httpd/'.$data['old']['domain'])) exec('rm -rf /var/log/ispconfig/httpd/'.$data['old']['domain']);
-			if(is_link($data['old']['document_root'].'/log')) $app->system->unlink($data['old']['document_root'].'/log');
+			if(is_link($data['old']['document_root'].'/'.$log_folder)) $app->system->unlink($data['old']['document_root'].'/'.$log_folder);
 		}
 		
 		//* Create the log dir if nescessary and mount it
-		if(!is_dir($data['new']['document_root'].'/log') || is_link($data['new']['document_root'].'/log')) {
-			if(is_link($data['new']['document_root'].'/log')) unlink($data['new']['document_root'].'/log');
-			$app->system->mkdir($data['new']['document_root'].'/log');
-			$app->system->chown($data['new']['document_root'].'/log','root');
-			$app->system->chgrp($data['new']['document_root'].'/log','root');
-			$app->system->chmod($data['new']['document_root'].'/log',0755);
-			exec('mount --bind '.escapeshellarg('/var/log/ispconfig/httpd/'.$data['new']['domain']).' '.escapeshellarg($data['new']['document_root'].'/log'));
+        if(!is_dir('/var/log/ispconfig/httpd/'.$data['new']['domain'])) exec('mkdir -p /var/log/ispconfig/httpd/'.$data['new']['domain']);
+		if(!is_dir($data['new']['document_root'].'/'.$log_folder) || is_link($data['new']['document_root'].'/'.$log_folder)) {
+			if(is_link($data['new']['document_root'].'/'.$log_folder)) unlink($data['new']['document_root'].'/'.$log_folder);
+			$app->system->mkdirpath($data['new']['document_root'].'/'.$log_folder);
+			$app->system->chown($data['new']['document_root'].'/'.$log_folder,'root');
+			$app->system->chgrp($data['new']['document_root'].'/'.$log_folder,'root');
+			$app->system->chmod($data['new']['document_root'].'/'.$log_folder,0755);
+			exec('mount --bind '.escapeshellarg('/var/log/ispconfig/httpd/'.$data['new']['domain']).' '.escapeshellarg($data['new']['document_root'].'/'.$log_folder));
 			//* add mountpoint to fstab
-			$fstab_line = '/var/log/ispconfig/httpd/'.$data['new']['domain'].' '.$data['new']['document_root'].'/log    none    bind    0 0';
+			$fstab_line = '/var/log/ispconfig/httpd/'.$data['new']['domain'].' '.$data['new']['document_root'].'/'.$log_folder.'    none    bind    0 0';
 			$app->system->replaceLine('/etc/fstab',$fstab_line,$fstab_line,1,1);
 		}
 		
@@ -530,11 +544,11 @@
         // setting a local var here
            
         // normally $conf['templates'] = "/usr/local/ispconfig/server/conf";
-
-		if($this->action == 'insert' && $data['new']['type'] == 'vhost') {
-			// Copy the error pages
+		if($this->action == 'insert' && ($data['new']['type'] == 'vhost' || $data['new']['type'] == 'vhostsubdomain')) {
+            
+            // Copy the error pages
 			if($data['new']['errordocs']) {
-				$error_page_path = escapeshellcmd($data['new']['document_root']).'/web/error/';
+				$error_page_path = escapeshellcmd($data['new']['document_root']).'/' . $web_folder . '/error/';
 				if (file_exists($conf['rootpath'] . '/conf-custom/error/'.substr(escapeshellcmd($conf['language']),0,2))) {
 					exec('cp ' . $conf['rootpath'] . '/conf-custom/error/'.substr(escapeshellcmd($conf['language']),0,2).'/* '.$error_page_path);
 				}
@@ -550,35 +564,35 @@
 			}
 
 			if (file_exists($conf['rootpath'] . '/conf-custom/index/standard_index.html_'.substr(escapeshellcmd($conf['language']),0,2))) {
-				exec('cp ' . $conf['rootpath'] . '/conf-custom/index/standard_index.html_'.substr(escapeshellcmd($conf['language']),0,2).' '.escapeshellcmd($data['new']['document_root']).'/web/index.html');
+				exec('cp ' . $conf['rootpath'] . '/conf-custom/index/standard_index.html_'.substr(escapeshellcmd($conf['language']),0,2).' '.escapeshellcmd($data['new']['document_root']).'/' . $web_folder . '/index.html');
             
 			if(is_file($conf['rootpath'] . '/conf-custom/index/favicon.ico')) {
-                exec('cp ' . $conf['rootpath'] . '/conf-custom/index/favicon.ico '.escapeshellcmd($data['new']['document_root']).'/web/');
+                exec('cp ' . $conf['rootpath'] . '/conf-custom/index/favicon.ico '.escapeshellcmd($data['new']['document_root']).'/' . $web_folder . '/');
             }
 			if(is_file($conf['rootpath'] . '/conf-custom/index/robots.txt')) {
-                exec('cp ' . $conf['rootpath'] . '/conf-custom/index/robots.txt '.escapeshellcmd($data['new']['document_root']).'/web/');
+                exec('cp ' . $conf['rootpath'] . '/conf-custom/index/robots.txt '.escapeshellcmd($data['new']['document_root']).'/' . $web_folder . '/');
                 }
                 if(is_file($conf['rootpath'] . '/conf-custom/index/.htaccess')) {
-                    exec('cp ' . $conf['rootpath'] . '/conf-custom/index/.htaccess '.escapeshellcmd($data['new']['document_root']).'/web/');
+                    exec('cp ' . $conf['rootpath'] . '/conf-custom/index/.htaccess '.escapeshellcmd($data['new']['document_root']).'/' . $web_folder . '/');
                 }
             }
 			else {
 				if (file_exists($conf['rootpath'] . '/conf-custom/index/standard_index.html')) {
-					exec('cp ' . $conf['rootpath'] . '/conf-custom/index/standard_index.html '.escapeshellcmd($data['new']['document_root']).'/web/index.html');
+					exec('cp ' . $conf['rootpath'] . '/conf-custom/index/standard_index.html '.escapeshellcmd($data['new']['document_root']).'/' . $web_folder . '/index.html');
 				}
 				else {
-					exec('cp ' . $conf['rootpath'] . '/conf/index/standard_index.html_'.substr(escapeshellcmd($conf['language']),0,2).' '.escapeshellcmd($data['new']['document_root']).'/web/index.html');
-					if(is_file($conf['rootpath'] . '/conf/index/favicon.ico')) exec('cp ' . $conf['rootpath'] . '/conf/index/favicon.ico '.escapeshellcmd($data['new']['document_root']).'/web/');
-					if(is_file($conf['rootpath'] . '/conf/index/robots.txt')) exec('cp ' . $conf['rootpath'] . '/conf/index/robots.txt '.escapeshellcmd($data['new']['document_root']).'/web/');
-					if(is_file($conf['rootpath'] . '/conf/index/.htaccess')) exec('cp ' . $conf['rootpath'] . '/conf/index/.htaccess '.escapeshellcmd($data['new']['document_root']).'/web/');
+					exec('cp ' . $conf['rootpath'] . '/conf/index/standard_index.html_'.substr(escapeshellcmd($conf['language']),0,2).' '.escapeshellcmd($data['new']['document_root']).'/' . $web_folder . '/index.html');
+					if(is_file($conf['rootpath'] . '/conf/index/favicon.ico')) exec('cp ' . $conf['rootpath'] . '/conf/index/favicon.ico '.escapeshellcmd($data['new']['document_root']).'/' . $web_folder . '/');
+					if(is_file($conf['rootpath'] . '/conf/index/robots.txt')) exec('cp ' . $conf['rootpath'] . '/conf/index/robots.txt '.escapeshellcmd($data['new']['document_root']).'/' . $web_folder . '/');
+					if(is_file($conf['rootpath'] . '/conf/index/.htaccess')) exec('cp ' . $conf['rootpath'] . '/conf/index/.htaccess '.escapeshellcmd($data['new']['document_root']).'/' . $web_folder . '/');
 				}
 			}
-			exec('chmod -R a+r '.escapeshellcmd($data['new']['document_root']).'/web/');
+			exec('chmod -R a+r '.escapeshellcmd($data['new']['document_root']).'/' . $web_folder . '/');
 
 			//** Copy the error documents on update when the error document checkbox has been activated and was deactivated before
-		} elseif ($this->action == 'update' && $data['new']['type'] == 'vhost' && $data['old']['errordocs'] == 0 && $data['new']['errordocs'] == 1) {
-
-			$error_page_path = escapeshellcmd($data['new']['document_root']).'/web/error/';
+		} elseif ($this->action == 'update' && ($data['new']['type'] == 'vhost' || $data['new']['type'] == 'vhostsubdomain') && $data['old']['errordocs'] == 0 && $data['new']['errordocs'] == 1) {
+            
+			$error_page_path = escapeshellcmd($data['new']['document_root']).'/' . $web_folder . '/error/';
 			if (file_exists($conf['rootpath'] . '/conf-custom/error/'.substr(escapeshellcmd($conf['language']),0,2))) {
 				exec('cp ' . $conf['rootpath'] . '/conf-custom/error/'.substr(escapeshellcmd($conf['language']),0,2).'/* '.$error_page_path);
 			}
@@ -608,12 +622,12 @@
 
 		if($this->action == 'insert' || $data["new"]["system_user"] != $data["old"]["system_user"]) {
 			// Chown and chmod the directories below the document root
-			$this->_exec('chown -R '.$username.':'.$groupname.' '.escapeshellcmd($data['new']['document_root']).'/web');
+			$this->_exec('chown -R '.$username.':'.$groupname.' '.escapeshellcmd($data['new']['document_root']).'/' . $web_folder);
 			// The document root itself has to be owned by root in normal level and by the web owner in security level 20
 			if($web_config['security_level'] == 20) {
-				$this->_exec('chown '.$username.':'.$groupname.' '.escapeshellcmd($data['new']['document_root']).'/web');
+				$this->_exec('chown '.$username.':'.$groupname.' '.escapeshellcmd($data['new']['document_root']).'/' . $web_folder);
 			} else {
-				$this->_exec('chown root:root '.escapeshellcmd($data['new']['document_root']).'/web');
+				$this->_exec('chown root:root '.escapeshellcmd($data['new']['document_root']).'/' . $web_folder);
 			}
 		}
 
@@ -637,8 +651,8 @@
 				$app->system->chmod($data['new']['document_root'].'/tmp',0777);
 			
 				// Set Log directory to 755 to make the logs accessible by the FTP user
-				if(realpath($data['new']['document_root'].'/log') == '/var/log/ispconfig/httpd/'.$data['new']['domain'].'/error.log') {
-					$app->system->chmod($data['new']['document_root'].'/log',0755);
+				if(realpath($data['new']['document_root'].'/'.$log_folder . '/error.log') == '/var/log/ispconfig/httpd/'.$data['new']['domain'].'/error.log') {
+					$app->system->chmod($data['new']['document_root'].'/'.$log_folder,0755);
 				}
 				
 				if($web_config['add_web_users_to_sshusers_group'] == 'y') {
@@ -668,9 +682,9 @@
 				$app->system->chgrp($data['new']['document_root'],'root');
 				$app->system->chown($data['new']['document_root'].'/cgi-bin',$username);
 				$app->system->chgrp($data['new']['document_root'].'/cgi-bin',$groupname);
-				if(realpath($data['new']['document_root'].'/log') == '/var/log/ispconfig/httpd/'.$data['new']['domain'].'/error.log') {
-					$app->system->chown($data['new']['document_root'].'/log','root',false);
-					$app->system->chgrp($data['new']['document_root'].'/log',$groupname,false);
+				if(realpath($data['new']['document_root'].'/'.$log_folder . '/error.log') == '/var/log/ispconfig/httpd/'.$data['new']['domain'].'/error.log') {
+					$app->system->chown($data['new']['document_root'].'/'.$log_folder,'root',false);
+					$app->system->chgrp($data['new']['document_root'].'/'.$log_folder,$groupname,false);
 				}
 				$app->system->chown($data['new']['document_root'].'/ssl','root');
 				$app->system->chgrp($data['new']['document_root'].'/ssl','root');
@@ -695,22 +709,25 @@
 				$app->system->chmod($data['new']['document_root'].'/webdav',0755);
 				$app->system->chmod($data['new']['document_root'].'/ssl',0755);
 				$app->system->chmod($data['new']['document_root'].'/cgi-bin',0755);
-				if(realpath($data['new']['document_root'].'/log') == '/var/log/ispconfig/httpd/'.$data['new']['domain'].'/error.log') {
-					$app->system->chmod($data['new']['document_root'].'/log',0755);
-				}
 				
 				// make temp directory writable for Apache and the website users
 				$app->system->chmod($data['new']['document_root'].'/tmp',0777);
 				
+				// Set Log directory to 755 to make the logs accessible by the FTP user
+				if(realpath($data['new']['document_root'].'/'.$log_folder . '/error.log') == '/var/log/ispconfig/httpd/'.$data['new']['domain'].'/error.log') {
+					$app->system->chmod($data['new']['document_root'].'/'.$log_folder,0755);
+				}
+                
 				$app->system->chown($data['new']['document_root'],'root');
 				$app->system->chgrp($data['new']['document_root'],'root');
 				$app->system->chown($data['new']['document_root'].'/cgi-bin',$username);
 				$app->system->chgrp($data['new']['document_root'].'/cgi-bin',$groupname);
-				if(realpath($data['new']['document_root'].'/log') == '/var/log/ispconfig/httpd/'.$data['new']['domain'].'/error.log') {
-					$app->system->chown($data['new']['document_root'].'/log','root',false);
-					$app->system->chgrp($data['new']['document_root'].'/log','root',false);
+				if(realpath($data['new']['document_root'].'/'.$log_folder . '/error.log') == '/var/log/ispconfig/httpd/'.$data['new']['domain'].'/error.log') {
+					$app->system->chown($data['new']['document_root'].'/'.$log_folder,'root',false);
+					$app->system->chgrp($data['new']['document_root'].'/'.$log_folder,$groupname,false);
 				}
-				$app->system->chown($data['new']['document_root'].'/ssl','root');
+				
+                $app->system->chown($data['new']['document_root'].'/ssl','root');
 				$app->system->chgrp($data['new']['document_root'].'/ssl','root');
 				$app->system->chown($data['new']['document_root'].'/tmp',$username);
 				$app->system->chgrp($data['new']['document_root'].'/tmp',$groupname);
@@ -723,19 +740,39 @@
 				$app->system->chown($data['new']['document_root'].'/webdav',$username);
 				$app->system->chgrp($data['new']['document_root'].'/webdav',$groupname);
 			}
-		}
+		} elseif(($this->action == 'insert' && $data['new']['type'] == 'vhostsubdomain') or ($web_config['set_folder_permissions_on_update'] == 'y' && $data['new']['type'] == 'vhostsubdomain')) {
+			if($web_config['security_level'] == 20) {
+				$app->system->chmod($data['new']['document_root'].'/' . $web_folder,0710);
+				$app->system->chown($data['new']['document_root'].'/' . $web_folder,$username);
+				$app->system->chgrp($data['new']['document_root'].'/' . $web_folder,$groupname);
+				$app->system->chown($data['new']['document_root'].'/' . $web_folder . '/error',$username);
+				$app->system->chgrp($data['new']['document_root'].'/' . $web_folder . '/error',$groupname);
+				$app->system->chown($data['new']['document_root'].'/' . $web_folder . '/stats',$username);
+				$app->system->chgrp($data['new']['document_root'].'/' . $web_folder . '/stats',$groupname);
+            } else {
+				$app->system->chmod($data['new']['document_root'].'/' . $web_folder,0755);
+				$app->system->chown($data['new']['document_root'].'/' . $web_folder,$username);
+				$app->system->chgrp($data['new']['document_root'].'/' . $web_folder,$groupname);
+				$app->system->chown($data['new']['document_root'].'/' . $web_folder . '/error',$username);
+				$app->system->chgrp($data['new']['document_root'].'/' . $web_folder . '/error',$groupname);
+				$app->system->chown($data['new']['document_root'].'/' . $web_folder . '/stats',$username);
+				$app->system->chgrp($data['new']['document_root'].'/' . $web_folder . '/stats',$groupname);
+            }
+        }
 		
 		//* Protect web folders
 		$app->system->web_folder_protection($data['new']['document_root'],true);
 
-		// Change the ownership of the error log to the root user
-		if(!@is_file('/var/log/ispconfig/httpd/'.$data['new']['domain'].'/error.log')) exec('touch '.escapeshellcmd('/var/log/ispconfig/httpd/'.$data['new']['domain'].'/error.log'));
-		$app->system->chown('/var/log/ispconfig/httpd/'.$data['new']['domain'].'/error.log','root');
-		$app->system->chgrp('/var/log/ispconfig/httpd/'.$data['new']['domain'].'/error.log','root');
-
+		if($data['new']['type'] == 'vhost') {
+            // Change the ownership of the error log to the root user
+            if(!@is_file('/var/log/ispconfig/httpd/'.$data['new']['domain'].'/error.log')) exec('touch '.escapeshellcmd('/var/log/ispconfig/httpd/'.$data['new']['domain'].'/error.log'));
+            $app->system->chown('/var/log/ispconfig/httpd/'.$data['new']['domain'].'/error.log','root');
+            $app->system->chgrp('/var/log/ispconfig/httpd/'.$data['new']['domain'].'/error.log','root');
+        }
 
 		//* Write the custom php.ini file, if custom_php_ini fieled is not empty
 		$custom_php_ini_dir = $web_config['website_basedir'].'/conf/'.$data['new']['system_user'];
+        if($data['new']['type'] == 'vhostsubdomain') $custom_php_ini_dir .= '_' . $web_folder;
 		if(!is_dir($web_config['website_basedir'].'/conf')) $app->system->mkdir($web_config['website_basedir'].'/conf');
 		
 		//* add open_basedir restriction to custom php.ini content, required for suphp only
@@ -775,8 +812,8 @@
 
 		$vhost_data = $data['new'];
 		//unset($vhost_data['ip_address']);
-		$vhost_data['web_document_root'] = $data['new']['document_root'].'/web';
-		$vhost_data['web_document_root_www'] = $web_config['website_basedir'].'/'.$data['new']['domain'].'/web';
+		$vhost_data['web_document_root'] = $data['new']['document_root'].'/' . $web_folder;
+		$vhost_data['web_document_root_www'] = $web_config['website_basedir'].'/'.$data['new']['domain'].'/' . $web_folder;
 		$vhost_data['web_basedir'] = $web_config['website_basedir'];
 		$vhost_data['security_level'] = $web_config['security_level'];
 		$vhost_data['allow_override'] = ($data['new']['allow_override'] == '')?'All':$data['new']['allow_override'];
@@ -809,7 +846,7 @@
 
 		if(@is_file($bundle_file)) $vhost_data['has_bundle_cert'] = 1;
 
-		//$vhost_data['document_root'] = $data['new']['document_root'].'/web';
+		//$vhost_data['document_root'] = $data['new']['document_root'].'/' . $web_folder;
 		
 		// Set SEO Redirect
 		if($data['new']['seo_redirect'] != '' && ($data['new']['subdomain'] == 'www' || $data['new']['subdomain'] == '*')){
@@ -869,10 +906,25 @@
 						'rewrite_target_ssl' => $rewrite_target_ssl);
 			}
 		}
-
-		// get alias domains (co-domains and subdomains)
-		$aliases = $app->db->queryAllRecords('SELECT * FROM web_domain WHERE parent_domain_id = '.$data['new']['domain_id']." AND active = 'y'");
+		
 		$server_alias = array();
+		
+		// get autoalias
+		$auto_alias = $web_config['website_autoalias'];
+		if($auto_alias != '') {
+			// get the client username
+			$client = $app->db->queryOneRecord("SELECT `username` FROM `client` WHERE `client_id` = '" . intval($client_id) . "'");
+			$aa_search = array('[client_id]', '[website_id]', '[client_username]', '[website_domain]');
+			$aa_replace = array($client_id, $data['new']['domain_id'], $client['username'], $data['new']['domain']);
+			$auto_alias = str_replace($aa_search, $aa_replace, $auto_alias);
+			unset($client);
+			unset($aa_search);
+			unset($aa_replace);
+			$server_alias[] .= $auto_alias;
+		}
+		
+		// get alias domains (co-domains and subdomains)
+		$aliases = $app->db->queryAllRecords('SELECT * FROM web_domain WHERE parent_domain_id = '.$data['new']['domain_id']." AND active = 'y' AND type != 'vhostsubdomain'");
 		switch($data['new']['subdomain']) {
 			case 'www':
 				$server_alias[] .= 'www.'.$data['new']['domain'].' ';
@@ -1022,7 +1074,7 @@
 			$php_open_basedir = ($data['new']['php_open_basedir'] == '')?$data['new']['document_root']:$data['new']['php_open_basedir'];
 			$fcgi_tpl->setVar('open_basedir', escapeshellcmd($php_open_basedir));
 
-			$fcgi_starter_script = escapeshellcmd($fastcgi_starter_path.$fastcgi_config['fastcgi_starter_script']);
+			$fcgi_starter_script = escapeshellcmd($fastcgi_starter_path.$fastcgi_config['fastcgi_starter_script'].($data['new']['type'] == 'vhostsubdomain' ? '_web' . $data['new']['domain_id'] : ''));
 			$app->system->file_put_contents($fcgi_starter_script,$fcgi_tpl->grab());
 			unset($fcgi_tpl);
 
@@ -1034,18 +1086,25 @@
 						
 			$tpl->setVar('fastcgi_alias',$fastcgi_config['fastcgi_alias']);
 			$tpl->setVar('fastcgi_starter_path',$fastcgi_starter_path);
-			$tpl->setVar('fastcgi_starter_script',$fastcgi_config['fastcgi_starter_script']);
+			$tpl->setVar('fastcgi_starter_script',$fastcgi_config['fastcgi_starter_script'].($data['new']['type'] == 'vhostsubdomain' ? '_web' . $data['new']['domain_id'] : ''));
 			$tpl->setVar('fastcgi_config_syntax',$fastcgi_config['fastcgi_config_syntax']);
 
 		} else {
 			//remove the php fastgi starter script if available
 			if ($data['old']['php'] == 'fast-cgi') {
-				$fastcgi_config = $app->getconf->get_server_config($conf['server_id'], 'fastcgi');
-				$fastcgi_starter_path = str_replace('[system_user]',$data['old']['system_user'],$fastcgi_config['fastcgi_starter_path']);
-				$fastcgi_starter_path = str_replace('[client_id]',$client_id,$fastcgi_starter_path);
-				if (is_dir($fastcgi_starter_path)) {
-					exec('rm -rf '.$fastcgi_starter_path);
-				}
+                $fastcgi_config = $app->getconf->get_server_config($conf['server_id'], 'fastcgi');
+                $fastcgi_starter_path = str_replace('[system_user]',$data['old']['system_user'],$fastcgi_config['fastcgi_starter_path']);
+                $fastcgi_starter_path = str_replace('[client_id]',$client_id,$fastcgi_starter_path);
+                if($data['old']['type'] == 'vhost') {
+                    if (is_dir($fastcgi_starter_path)) {
+                        exec('rm -rf '.$fastcgi_starter_path);
+                    }
+                } else {
+                    $fcgi_starter_script = $fastcgi_starter_path.$fastcgi_config['fastcgi_starter_script'].'_web' . $data['old']['domain_id'];
+                    if (file_exists($fcgi_starter_script)) {
+                        exec('rm -f '.$fcgi_starter_script);
+                    }
+                }
 			}
 		}
 		
@@ -1105,7 +1164,7 @@
 			//$cgi_config = $app->getconf->get_server_config($conf['server_id'], 'cgi');
 
 			$cgi_config['cgi_starter_path'] = $web_config['website_basedir'].'/php-cgi-scripts/[system_user]/';
-			$cgi_config['cgi_starter_script'] = 'php-cgi-starter';
+			$cgi_config['cgi_starter_script'] = 'php-cgi-starter'.($data['new']['type'] == 'vhostsubdomain' ? '_web' . $data['new']['domain_id'] : '');
 			$cgi_config['cgi_bin'] = '/usr/bin/php-cgi';
 
 			$cgi_starter_path = str_replace('[system_user]',$data['new']['system_user'],$cgi_config['cgi_starter_path']);
@@ -1140,7 +1199,7 @@
 				$cgi_tpl->setVar('php_ini_path',escapeshellcmd($fastcgi_config['fastcgi_phpini_path']));
 			}
 
-			$cgi_starter_script = escapeshellcmd($cgi_starter_path.$cgi_config['cgi_starter_script']);
+			$cgi_starter_script = escapeshellcmd($cgi_starter_path.$cgi_config['cgi_starter_script'].($data['new']['type'] == 'vhostsubdomain' ? '_web' . $data['new']['domain_id'] : ''));
 			$app->system->file_put_contents($cgi_starter_script,$cgi_tpl->grab());
 			unset($cgi_tpl);
 
@@ -1152,7 +1211,7 @@
 			$app->system->chgrp($cgi_starter_script,$data['new']['system_group']);
 
 			$tpl->setVar('cgi_starter_path',$cgi_starter_path);
-			$tpl->setVar('cgi_starter_script',$cgi_config['cgi_starter_script']);
+			$tpl->setVar('cgi_starter_script',$cgi_config['cgi_starter_script'].($data['new']['type'] == 'vhostsubdomain' ? '_web' . $data['new']['domain_id'] : ''));
 
 		}
 
@@ -1271,11 +1330,11 @@
 		}
 
 		//* Create .htaccess and .htpasswd file for website statistics
-		if(!is_file($data['new']['document_root'].'/web/stats/.htaccess') or $data['old']['document_root'] != $data['new']['document_root']) {
-			if(!is_dir($data['new']['document_root'].'/web/stats')) $app->system->mkdir($data['new']['document_root'].'/web/stats');
+		if(!is_file($data['new']['document_root'].'/' . $web_folder . '/stats/.htaccess') or $data['old']['document_root'] != $data['new']['document_root']) {
+			if(!is_dir($data['new']['document_root'].'/' . $web_folder . '/stats')) $app->system->mkdir($data['new']['document_root'].'/' . $web_folder . '/stats');
 			$ht_file = "AuthType Basic\nAuthName \"Members Only\"\nAuthUserFile ".$data['new']['document_root']."/.htpasswd_stats\nrequire valid-user";
-			$app->system->file_put_contents($data['new']['document_root'].'/web/stats/.htaccess',$ht_file);
-			$app->system->chmod($data['new']['document_root'].'/web/stats/.htaccess',0755);
+			$app->system->file_put_contents($data['new']['document_root'].'/' . $web_folder . '/stats/.htaccess',$ht_file);
+			$app->system->chmod($data['new']['document_root'].'/' . $web_folder . '/stats/.htaccess',0755);
 			unset($ht_file);
 		}
 
@@ -1291,7 +1350,7 @@
 		}
 		
 		//* Create awstats configuration
-		if($data['new']['stats_type'] == 'awstats' && $data['new']['type'] == 'vhost') {
+		if($data['new']['stats_type'] == 'awstats' && ($data['new']['type'] == 'vhost' || $data['new']['type'] == 'vhostsubdomain')) {
 			$this->awstats_update($data,$web_config);
 		}
 		
@@ -1393,7 +1452,7 @@
 		$app->uses('system');
 		$web_config = $app->getconf->get_server_config($conf['server_id'], 'web');
 		
-		$app->system->web_folder_protection($data['new']['document_root'],false);
+		$app->system->web_folder_protection($data['old']['document_root'],false);
 
 		//* Check if this is a chrooted setup
 		if($web_config['website_basedir'] != '' && @is_file($web_config['website_basedir'].'/etc/passwd')) {
@@ -1401,8 +1460,26 @@
 		} else {
 			$apache_chrooted = false;
 		}
+		
+		//* Remove the mounts
+		$log_folder = 'log';
+        $web_folder = '';
+        if($data['old']['type'] == 'vhostsubdomain') {
+            $tmp = $app->db->queryOneRecord('SELECT `domain`,`document_root` FROM web_domain WHERE domain_id = '.intval($data['old']['parent_domain_id']));
+            $subdomain_host = preg_replace('/^(.*)\.' . preg_quote($tmp['domain'], '/') . '$/', '$1', $data['old']['domain']);
+            if($subdomain_host == '') $subdomain_host = 'web'.$data['old']['domain_id'];
+            $web_folder = $data['old']['web_folder'];
+            $log_folder .= '/' . $subdomain_host;
+            unset($tmp);
+		}
+        
+		exec('umount '.escapeshellarg($data['old']['document_root'].'/'.$log_folder));
+		
+		//* remove mountpoint from fstab
+		$fstab_line = '/var/log/ispconfig/httpd/'.$data['old']['domain'].' '.$data['old']['document_root'].'/'.$log_folder.'    none    bind    0 0';
+		$app->system->removeLine('/etc/fstab',$fstab_line);
 
-		if($data['old']['type'] != 'vhost' && $data['old']['parent_domain_id'] > 0) {
+		if($data['old']['type'] != 'vhost' && $data['old']['type'] != 'vhostsubdomain' && $data['old']['parent_domain_id'] > 0) {
 			//* This is a alias domain or subdomain, so we have to update the website instead
 			$parent_domain_id = intval($data['old']['parent_domain_id']);
 			$tmp = $app->db->queryOneRecord('SELECT * FROM web_domain WHERE domain_id = '.$parent_domain_id." AND active = 'y'");
@@ -1435,71 +1512,96 @@
 			
 			$app->system->unlink($vhost_file);
 			$app->log('Removing vhost file: '.$vhost_file,LOGLEVEL_DEBUG);
-
-			$docroot = escapeshellcmd($data['old']['document_root']);
-			if($docroot != '' && !stristr($docroot,'..')) exec('rm -rf '.$docroot);
-
-
-			//remove the php fastgi starter script if available
-			if ($data['old']['php'] == 'fast-cgi') {
-				$fastcgi_starter_path = str_replace('[system_user]',$data['old']['system_user'],$web_config['fastcgi_starter_path']);
-				if (is_dir($fastcgi_starter_path)) {
-					exec('rm -rf '.$fastcgi_starter_path);
-				}
-			}
+            
+            if($data['old']['type'] == 'vhost' || $data['old']['type'] == 'vhostsubdomain') {
+                $docroot = escapeshellcmd($data['old']['document_root']);
+                if($docroot != '' && !stristr($docroot,'..')) {
+                    if($data['old']['type'] == 'vhost') exec('rm -rf '.$docroot);
+                    elseif(!stristr($data['old']['web_folder'], '..')) exec('rm -rf '.$docroot.'/'.$web_folder);
+                }
 			
-			// remove PHP-FPM pool
-			if ($data['old']['php'] == 'php-fpm') {
-				$this->php_fpm_pool_delete($data,$web_config);
-			}
+                //remove the php fastgi starter script if available
+                if ($data['old']['php'] == 'fast-cgi') {
+                    $fastcgi_starter_path = str_replace('[system_user]',$data['old']['system_user'],$web_config['fastcgi_starter_path']);
+                    if($data['old']['type'] == 'vhost') {
+                        if (is_dir($fastcgi_starter_path)) {
+                            exec('rm -rf '.$fastcgi_starter_path);
+                        }
+                    } else {
+                        $fcgi_starter_script = $fastcgi_starter_path.$web_config['fastcgi_starter_script'].'_web'.$data['old']['domain_id'];
+                        if (file_exists($fcgi_starter_script)) {
+                            exec('rm -f '.$fcgi_starter_script);
+                        }
+                    }
+                }
+                
+                // remove PHP-FPM pool
+                if ($data['old']['php'] == 'php-fpm') {
+                    $this->php_fpm_pool_delete($data,$web_config);
+                }
 
-			//remove the php cgi starter script if available
-			if ($data['old']['php'] == 'cgi') {
-				// TODO: fetch the date from the server-settings
-				$web_config['cgi_starter_path'] = $web_config['website_basedir'].'/php-cgi-scripts/[system_user]/';
+                //remove the php cgi starter script if available
+                if ($data['old']['php'] == 'cgi') {
+                    // TODO: fetch the date from the server-settings
+                    $web_config['cgi_starter_path'] = $web_config['website_basedir'].'/php-cgi-scripts/[system_user]/';
 
-				$cgi_starter_path = str_replace('[system_user]',$data['old']['system_user'],$web_config['cgi_starter_path']);
-				if (is_dir($cgi_starter_path)) {
-					exec('rm -rf '.$cgi_starter_path);
-				}
-			}
+                    $cgi_starter_path = str_replace('[system_user]',$data['old']['system_user'],$web_config['cgi_starter_path']);
+                    if($data['old']['type'] == 'vhost') {
+                        if (is_dir($cgi_starter_path)) {
+                            exec('rm -rf '.$cgi_starter_path);
+                        }
+                    } else {
+                        $cgi_starter_script = $cgi_starter_path.'php-cgi-starter_web'.$data['old']['domain_id'];
+                        if (file_exists($cgi_starter_script)) {
+                            exec('rm -f '.$cgi_starter_script);
+                        }
+                    }
+                }
+            
+                $app->log('Removing website: '.$docroot,LOGLEVEL_DEBUG);
 
-			$app->log('Removing website: '.$docroot,LOGLEVEL_DEBUG);
+                // Delete the symlinks for the sites
+                $client = $app->db->queryOneRecord('SELECT client_id FROM sys_group WHERE sys_group.groupid = '.intval($data['old']['sys_groupid']));
+                $client_id = intval($client['client_id']);
+                unset($client);
+                $tmp_symlinks_array = explode(':',$web_config['website_symlinks']);
+                if(is_array($tmp_symlinks_array)) {
+                    foreach($tmp_symlinks_array as $tmp_symlink) {
+                        $tmp_symlink = str_replace('[client_id]',$client_id,$tmp_symlink);
+                        $tmp_symlink = str_replace('[website_domain]',$data['old']['domain'],$tmp_symlink);
+                        // Remove trailing slash
+                        if(substr($tmp_symlink, -1, 1) == '/') $tmp_symlink = substr($tmp_symlink, 0, -1);
+                        // create the symlinks, if not exist
+                        if(is_link($tmp_symlink)) {
+                            $app->system->unlink($tmp_symlink);
+                            $app->log('Removing symlink: '.$tmp_symlink,LOGLEVEL_DEBUG);
+                        }
+                    }
+                }
+                // end removing symlinks
+            }
 
-			// Delete the symlinks for the sites
-			$client = $app->db->queryOneRecord('SELECT client_id FROM sys_group WHERE sys_group.groupid = '.intval($data['old']['sys_groupid']));
-			$client_id = intval($client['client_id']);
-			unset($client);
-			$tmp_symlinks_array = explode(':',$web_config['website_symlinks']);
-			if(is_array($tmp_symlinks_array)) {
-				foreach($tmp_symlinks_array as $tmp_symlink) {
-					$tmp_symlink = str_replace('[client_id]',$client_id,$tmp_symlink);
-					$tmp_symlink = str_replace('[website_domain]',$data['old']['domain'],$tmp_symlink);
-					// Remove trailing slash
-					if(substr($tmp_symlink, -1, 1) == '/') $tmp_symlink = substr($tmp_symlink, 0, -1);
-					// create the symlinks, if not exist
-					if(is_link($tmp_symlink)) {
-						$app->system->unlink($tmp_symlink);
-						$app->log('Removing symlink: '.$tmp_symlink,LOGLEVEL_DEBUG);
-					}
-				}
-			}
-			// end removing symlinks
-
-			// Delete the log file directory
-			$vhost_logfile_dir = escapeshellcmd('/var/log/ispconfig/httpd/'.$data['old']['domain']);
-			if($data['old']['domain'] != '' && !stristr($vhost_logfile_dir,'..')) exec('rm -rf '.$vhost_logfile_dir);
-			$app->log('Removing website logfile directory: '.$vhost_logfile_dir,LOGLEVEL_DEBUG);
-
-			//delete the web user
-			$command = 'userdel';
-			$command .= ' '.$data['old']['system_user'];
-			exec($command);
-			if($apache_chrooted) $this->_exec('chroot '.escapeshellcmd($web_config['website_basedir']).' '.$command);
+            // Delete the log file directory
+            $vhost_logfile_dir = escapeshellcmd('/var/log/ispconfig/httpd/'.$data['old']['domain']);
+            if($data['old']['domain'] != '' && !stristr($vhost_logfile_dir,'..')) exec('rm -rf '.$vhost_logfile_dir);
+            $app->log('Removing website logfile directory: '.$vhost_logfile_dir,LOGLEVEL_DEBUG);
+            
+            if($data['old']['type'] == 'vhost') {
+                //delete the web user
+                $command = 'userdel';
+                $command .= ' '.$data['old']['system_user'];
+                exec($command);
+                if($apache_chrooted) $this->_exec('chroot '.escapeshellcmd($web_config['website_basedir']).' '.$command);
+                
+            }
+            
+            //* Remove the awstats configuration file
+            if($data['old']['stats_type'] == 'awstats') {
+                $this->awstats_delete($data,$web_config);
+            }
 			
-			//* Remove the awstats configuration file
-			if($data['old']['stats_type'] == 'awstats') {
-				$this->awstats_delete($data,$web_config);
+			if($data['old']['type'] == 'vhostsubdomain') {
+				$app->system->web_folder_protection($parent_web_document_root,true);
 			}
 			
 			if($apache_chrooted) {
@@ -1510,6 +1612,7 @@
 			}
 
 		}
+        if($data['old']['type'] != 'vhost') $app->system->web_folder_protection($data['old']['document_root'],true);
 	}
 
 	//* This function is called when a IP on the server is inserted, updated or deleted
@@ -1578,10 +1681,13 @@
 			return false;
 		}
 		
+        $web_folder = 'web';
+        if($website['type'] == 'vhostsubdomain') $web_folder = $website['web_folder'];
+        
 		//* Get the folder path.
 		if(substr($folder['path'],0,1) == '/') $folder['path'] = substr($folder['path'],1);
 		if(substr($folder['path'],-1) == '/') $folder['path'] = substr($folder['path'],0,-1);
-		$folder_path = escapeshellcmd($website['document_root'].'/web/'.$folder['path']);
+		$folder_path = escapeshellcmd($website['document_root'].'/' . $web_folder . '/'.$folder['path']);
 		if(substr($folder_path,-1) != '/') $folder_path .= '/';
 		
 		//* Check if the resulting path is inside the docroot
@@ -1638,12 +1744,26 @@
 		
 		//* Create the .htaccess file
 		//if(!is_file($folder_path.'.htaccess')) {
-			$ht_file = "AuthType Basic\nAuthName \"Members Only\"\nAuthUserFile ".$folder_path.".htpasswd\nrequire valid-user";
-			$app->system->file_put_contents($folder_path.'.htaccess',$ht_file);
+			$begin_marker = '### ISPConfig folder protection begin ###';
+            $end_marker = "### ISPConfig folder protection end ###\n\n";
+            $ht_file = $begin_marker."\nAuthType Basic\nAuthName \"Members Only\"\nAuthUserFile ".$folder_path.".htpasswd\nrequire valid-user\n".$end_marker;
+			
+            if(file_exists($folder_path.'.htaccess')) {
+                $old_content = $app->system->file_get_contents($folder_path.'.htaccess');
+                
+                if(preg_match('/' . preg_quote($begin_marker, '/') . '(.*?)' . preg_quote($end_marker, '/') . '/s', $old_content, $matches)) {
+                    $ht_file = str_replace($matches[0], $ht_file, $old_content);
+                } else {
+                    $ht_file .= $old_content;
+                }
+            }
+            unset($old_content);
+            
+            $app->system->file_put_contents($folder_path.'.htaccess',$ht_file);
 			$app->system->chmod($folder_path.'.htaccess',0755);
 			$app->system->chown($folder_path.'.htaccess',$website['system_user']);
 			$app->system->chgrp($folder_path.'.htaccess',$website['system_group']);
-			$app->log('Created file '.$folder_path.'.htaccess',LOGLEVEL_DEBUG);
+			$app->log('Created/modified file '.$folder_path.'.htaccess',LOGLEVEL_DEBUG);
 		//}
 		
 	}
@@ -1662,10 +1782,13 @@
 			return false;
 		}
 		
+        $web_folder = 'web';
+        if($website['type'] == 'vhostsubdomain') $web_folder = $website['web_folder'];
+        
 		//* Get the folder path.
 		if(substr($folder['path'],0,1) == '/') $folder['path'] = substr($folder['path'],1);
 		if(substr($folder['path'],-1) == '/') $folder['path'] = substr($folder['path'],0,-1);
-		$folder_path = realpath($website['document_root'].'/web/'.$folder['path']);
+		$folder_path = realpath($website['document_root'].'/' . $web_folder . '/'.$folder['path']);
 		if(substr($folder_path,-1) != '/') $folder_path .= '/';
 		
 		//* Check if the resulting path is inside the docroot
@@ -1682,8 +1805,24 @@
 		
 		//* Remove .htaccess file
 		if(is_file($folder_path.'.htaccess')) {
-			$app->system->unlink($folder_path.'.htaccess');
-			$app->log('Removed file '.$folder_path.'.htaccess',LOGLEVEL_DEBUG);
+            $begin_marker = '### ISPConfig folder protection begin ###';
+            $end_marker = "### ISPConfig folder protection end ###\n\n";
+            
+            $ht_file = $app->system->file_get_contents($folder_path.'.htaccess');
+            
+            if(preg_match('/' . preg_quote($begin_marker, '/') . '(.*?)' . preg_quote($end_marker, '/') . '/s', $ht_file, $matches)) {
+                $ht_file = str_replace($matches[0], '', $ht_file);
+            } else {
+                $ht_file = str_replace("AuthType Basic\nAuthName \"Members Only\"\nAuthUserFile ".$folder_path.".htpasswd\nrequire valid-user", '', $ht_file);
+            }
+            
+            if(trim($ht_file) == '') {
+                $app->system->unlink($folder_path.'.htaccess');
+                $app->log('Removed file '.$folder_path.'.htaccess',LOGLEVEL_DEBUG);
+            } else {
+                $app->system->file_put_contents($folder_path.'.htaccess', $ht_file);
+                $app->log('Removed protection content from file '.$folder_path.'.htaccess',LOGLEVEL_DEBUG);
+            }
 		}
 	}
 	
@@ -1698,15 +1837,18 @@
 			return false;
 		}
 		
+        $web_folder = 'web';
+        if($website['type'] == 'vhostsubdomain') $web_folder = $website['web_folder'];
+        
 		//* Get the folder path.
 		if(substr($data['old']['path'],0,1) == '/') $data['old']['path'] = substr($data['old']['path'],1);
 		if(substr($data['old']['path'],-1) == '/') $data['old']['path'] = substr($data['old']['path'],0,-1);
-		$old_folder_path = realpath($website['document_root'].'/web/'.$data['old']['path']);
+		$old_folder_path = realpath($website['document_root'].'/' . $web_folder . '/'.$data['old']['path']);
 		if(substr($old_folder_path,-1) != '/') $old_folder_path .= '/';
 			
 		if(substr($data['new']['path'],0,1) == '/') $data['new']['path'] = substr($data['new']['path'],1);
 		if(substr($data['new']['path'],-1) == '/') $data['new']['path'] = substr($data['new']['path'],0,-1);
-		$new_folder_path = escapeshellcmd($website['document_root'].'/web/'.$data['new']['path']);
+		$new_folder_path = escapeshellcmd($website['document_root'].'/' . $web_folder . '/'.$data['new']['path']);
 		if(substr($new_folder_path,-1) != '/') $new_folder_path .= '/';
 		
 		//* Check if the resulting path is inside the docroot
@@ -1732,6 +1874,9 @@
 		//* Create the folder path, if it does not exist
 		if(!is_dir($new_folder_path)) $app->system->mkdirpath($new_folder_path);
 		
+        $begin_marker = '### ISPConfig folder protection begin ###';
+        $end_marker = "### ISPConfig folder protection end ###\n\n";
+        
 		if($data['old']['path'] != $data['new']['path']) {
 
 		
@@ -1743,26 +1888,63 @@
 			
 			//* delete old .htaccess file
 			if(is_file($old_folder_path.'.htaccess')) {
-				$app->system->unlink($old_folder_path.'.htaccess');
-				$app->log('Deleted file '.$old_folder_path.'.htaccess',LOGLEVEL_DEBUG);
+                $ht_file = $app->system->file_get_contents($old_folder_path.'.htaccess');
+                
+                if(preg_match('/' . preg_quote($begin_marker, '/') . '(.*?)' . preg_quote($end_marker, '/') . '/s', $ht_file, $matches)) {
+                    $ht_file = str_replace($matches[0], '', $ht_file);
+                } else {
+                    $ht_file = str_replace("AuthType Basic\nAuthName \"Members Only\"\nAuthUserFile ".$old_folder_path.".htpasswd\nrequire valid-user", '', $ht_file);
+                }
+                
+                if(trim($ht_file) == '') {
+                    $app->system->unlink($old_folder_path.'.htaccess');
+                    $app->log('Removed file '.$old_folder_path.'.htaccess',LOGLEVEL_DEBUG);
+                } else {
+                    $app->system->file_put_contents($old_folder_path.'.htaccess', $ht_file);
+                    $app->log('Removed protection content from file '.$old_folder_path.'.htaccess',LOGLEVEL_DEBUG);
+                }
 			}
 		
 		}
 		
 		//* Create the .htaccess file
 		if($data['new']['active'] == 'y') {
-			$ht_file = "AuthType Basic\nAuthName \"Members Only\"\nAuthUserFile ".$new_folder_path.".htpasswd\nrequire valid-user";
-			$app->system->file_put_contents($new_folder_path.'.htaccess',$ht_file);
-			$app->system->chmod($new_folder_path.'.htpasswd',0755);
-			$app->system->chown($folder_path.'.htpasswd',$website['system_user']);
-			$app->system->chgrp($folder_path.'.htpasswd',$website['system_group']);
-			$app->log('Created file '.$new_folder_path.'.htpasswd',LOGLEVEL_DEBUG);
+            $ht_file = $begin_marker."\nAuthType Basic\nAuthName \"Members Only\"\nAuthUserFile ".$new_folder_path.".htpasswd\nrequire valid-user\n".$end_marker;
+			
+            if(file_exists($new_folder_path.'.htaccess')) {
+                $old_content = $app->system->file_get_contents($new_folder_path.'.htaccess');
+                
+                if(preg_match('/' . preg_quote($begin_marker, '/') . '(.*?)' . preg_quote($end_marker, '/') . '/s', $old_content, $matches)) {
+                    $ht_file = str_replace($matches[0], $ht_file, $old_content);
+                } else {
+                    $ht_file .= $old_content;
+                }
+            }
+            
+            $app->system->file_put_contents($new_folder_path.'.htaccess',$ht_file);
+			$app->system->chmod($new_folder_path.'.htaccess',0755);
+			$app->system->chown($new_folder_path.'.htaccess',$website['system_user']);
+			$app->system->chgrp($new_folder_path.'.htaccess',$website['system_group']);
+			$app->log('Created/modified file '.$new_folder_path.'.htaccess',LOGLEVEL_DEBUG);
 		}
 		
 		//* Remove .htaccess file
 		if($data['new']['active'] == 'n' && is_file($new_folder_path.'.htaccess')) {
-			$app->system->unlink($new_folder_path.'.htaccess');
-			$app->log('Removed file '.$new_folder_path.'.htaccess',LOGLEVEL_DEBUG);
+            $ht_file = $app->system->file_get_contents($new_folder_path.'.htaccess');
+            
+            if(preg_match('/' . preg_quote($begin_marker, '/') . '(.*?)' . preg_quote($end_marker, '/') . '/s', $ht_file, $matches)) {
+                $ht_file = str_replace($matches[0], '', $ht_file);
+            } else {
+                $ht_file = str_replace("AuthType Basic\nAuthName \"Members Only\"\nAuthUserFile ".$new_folder_path.".htpasswd\nrequire valid-user", '', $ht_file);
+            }
+            
+            if(trim($ht_file) == '') {
+                $app->system->unlink($new_folder_path.'.htaccess');
+                $app->log('Removed file '.$new_folder_path.'.htaccess',LOGLEVEL_DEBUG);
+            } else {
+                $app->system->file_put_contents($new_folder_path.'.htaccess', $ht_file);
+                $app->log('Removed protection content from file '.$new_folder_path.'.htaccess',LOGLEVEL_DEBUG);
+            }
 		}
 		
 		
@@ -2058,9 +2240,11 @@
 	private function awstats_update ($data,$web_config) {
 		global $app;
 		
+        $web_folder = $data['new']['web_folder'];
+        if($data['new']['type'] == 'vhost') $web_folder = 'web';
 		$awstats_conf_dir = $web_config['awstats_conf_dir'];
 		
-		if(!is_dir($data['new']['document_root']."/web/stats/")) mkdir($data['new']['document_root']."/web/stats");
+		if(!is_dir($data['new']['document_root']."/" . $web_folder . "/stats/")) mkdir($data['new']['document_root']."/" . $web_folder . "/stats");
 		if(!@is_file($awstats_conf_dir.'/awstats.'.$data['new']['domain'].'.conf') || ($data['old']['domain'] != '' && $data['new']['domain'] != $data['old']['domain'])) {
 			if ( @is_file($awstats_conf_dir.'/awstats.'.$data['old']['domain'].'.conf') ) {
 				$app->system->unlink($awstats_conf_dir.'/awstats.'.$data['old']['domain'].'.conf');
@@ -2076,8 +2260,8 @@
 			$app->log('Created AWStats config file: '.$awstats_conf_dir.'/awstats.'.$data['new']['domain'].'.conf',LOGLEVEL_DEBUG);
 		}
 		
-		if(is_file($data['new']['document_root']."/web/stats/index.html")) $app->system->unlink($data['new']['document_root']."/web/stats/index.html");
-		$app->system->copy("/usr/local/ispconfig/server/conf/awstats_index.php.master",$data['new']['document_root']."/web/stats/index.php");
+		if(is_file($data['new']['document_root']."/" . $web_folder . "/stats/index.html")) $app->system->unlink($data['new']['document_root']."/" . $web_folder . "/stats/index.html");
+		$app->system->copy("/usr/local/ispconfig/server/conf/awstats_index.php.master",$data['new']['document_root']."/" . $web_folder . "/stats/index.php");
 	}
 	
 	//* Delete the awstats configuration file
@@ -2185,8 +2369,8 @@
 				foreach($ini_settings as $ini_setting){
 						list($key, $value) = explode('=', $ini_setting);
 						if($value){
-							$value = escapeshellcmd(trim($value));
-							$key = escapeshellcmd(trim($key));
+							$value = trim($value);
+							$key = trim($key);
 							switch (strtolower($value)) {
 								case '0':
 									// PHP-FPM might complain about invalid boolean value if you use 0

--
Gitblit v1.9.1