From e94a9fb5e4b6a2bc07e0bb4cf8ea35fc70c4bbf0 Mon Sep 17 00:00:00 2001
From: tbrehm <t.brehm@ispconfig.org>
Date: Thu, 10 May 2012 12:31:27 -0400
Subject: [PATCH] Merged revisions 3049-3051,3053-3054,3058,3070,3074-3077,3082,3086-3088,3091-3092,3094-3095 from stable branch.

---
 server/plugins-available/nginx_plugin.inc.php |  441 ++++++++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 406 insertions(+), 35 deletions(-)

diff --git a/server/plugins-available/nginx_plugin.inc.php b/server/plugins-available/nginx_plugin.inc.php
index 79fa68d..af5972e 100644
--- a/server/plugins-available/nginx_plugin.inc.php
+++ b/server/plugins-available/nginx_plugin.inc.php
@@ -41,7 +41,7 @@
 	function onInstall() {
 		global $conf;
 
-		if($conf['services']['web'] == true) {
+		if($conf['services']['web'] == true && !@is_link('/usr/local/ispconfig/server/plugins-enabled/apache2_plugin.inc.php')) {
 			return true;
 		} else {
 			return false;
@@ -256,7 +256,13 @@
 
 	function update($event_name,$data) {
 		global $app, $conf;
-
+		
+		//* Check if the apache plugin is enabled
+		if(@is_link('/usr/local/ispconfig/server/plugins-enabled/apache2_plugin.inc.php')) {
+			$app->log('The nginx plugin can not be used together with the apache2 plugin..',LOGLEVEL_WARN);
+			return 0;
+		}
+		
 		if($this->action != 'insert') $this->action = 'update';
 
 		if($data['new']['type'] != 'vhost' && $data['new']['parent_domain_id'] > 0) {
@@ -333,8 +339,14 @@
 			$tmp_docroot = explode('/',$data['old']['document_root']);
 			unset($tmp_docroot[count($tmp_docroot)-1]);
 			$old_dir = implode('/',$tmp_docroot);
-
-			exec('rm -rf '.$data['new']['document_root']);
+			
+			//* Check if there is already some data in the new docroot and rename it as we need a clean path to move the existing site to the new path
+			if(@is_dir($data['new']['document_root'])) {
+				rename($data['new']['document_root'],$data['new']['document_root'].'_bak_'.date('Y_m_d'));
+				$app->log('Renaming existing directory in new docroot location. mv '.$data['new']['document_root'].' '.$data['new']['document_root'].'_bak_'.date('Y_m_d'),LOGLEVEL_DEBUG);
+			}
+			
+			//* Create new base directory, if it does not exist yet
 			if(!is_dir($new_dir)) exec('mkdir -p '.$new_dir);
 			exec('mv '.$data['old']['document_root'].' '.$new_dir);
 			$app->log('Moving site to new document root: mv '.$data['old']['document_root'].' '.$new_dir,LOGLEVEL_DEBUG);
@@ -700,7 +712,39 @@
 		if($data['new']['ipv6_address'] != '') $tpl->setVar('ipv6_enabled', 1);
 		
 		// PHP-FPM
-		$pool_dir = escapeshellcmd($web_config['php_fpm_pool_dir']);
+		// Support for multiple PHP versions
+		/*
+		if(trim($data['new']['fastcgi_php_version']) != ''){
+			$default_php_fpm = false;
+			list($custom_php_fpm_name, $custom_php_fpm_init_script, $custom_php_fpm_ini_dir, $custom_php_fpm_pool_dir) = explode(':', trim($data['new']['fastcgi_php_version']));
+			if(substr($custom_php_fpm_ini_dir,-1) != '/') $custom_php_fpm_ini_dir .= '/';
+		} else {
+			$default_php_fpm = true;
+		}
+		*/
+		if($data['new']['php'] != 'no'){
+			if(trim($data['new']['fastcgi_php_version']) != ''){
+				$default_php_fpm = false;
+				list($custom_php_fpm_name, $custom_php_fpm_init_script, $custom_php_fpm_ini_dir, $custom_php_fpm_pool_dir) = explode(':', trim($data['new']['fastcgi_php_version']));
+				if(substr($custom_php_fpm_ini_dir,-1) != '/') $custom_php_fpm_ini_dir .= '/';
+			} else {
+				$default_php_fpm = true;
+			}
+		} else {
+			if(trim($data['old']['fastcgi_php_version']) != '' && $data['old']['php'] != 'no'){
+				$default_php_fpm = false;
+				list($custom_php_fpm_name, $custom_php_fpm_init_script, $custom_php_fpm_ini_dir, $custom_php_fpm_pool_dir) = explode(':', trim($data['old']['fastcgi_php_version']));
+				if(substr($custom_php_fpm_ini_dir,-1) != '/') $custom_php_fpm_ini_dir .= '/';
+			} else {
+				$default_php_fpm = true;
+			}
+		}
+		
+		if($default_php_fpm){
+			$pool_dir = escapeshellcmd($web_config['php_fpm_pool_dir']);
+		} else {
+			$pool_dir = $custom_php_fpm_pool_dir;
+		}
 		if(substr($pool_dir,-1) != '/') $pool_dir .= '/';
 		$pool_name = 'web'.$data['new']['domain_id'];
 		$socket_dir = escapeshellcmd($web_config['php_fpm_socket_dir']);
@@ -718,6 +762,9 @@
 		$fpm_socket = $socket_dir.$pool_name.'.sock';
 		$tpl->setVar('fpm_socket', $fpm_socket);
 		$vhost_data['fpm_port'] = $web_config['php_fpm_start_port'] + $data['new']['domain_id'] - 1;
+		
+		// backwards compatibility; since ISPConfig 3.0.5, the PHP mode for nginx is called 'php-fpm' instead of 'fast-cgi'. The following line makes sure that old web sites that have 'fast-cgi' in the database still get PHP-FPM support.
+		if($vhost_data['php'] == 'fast-cgi') $vhost_data['php'] = 'php-fpm';
 		
 		// Custom nginx directives
 		$final_nginx_directives = array();
@@ -766,9 +813,10 @@
 
 		// Rewrite rules
 		$rewrite_rules = array();
-		if($data['new']['redirect_type'] != '') {
+		if($data['new']['redirect_type'] != '' && $data['new']['redirect_path'] != '') {
 			if(substr($data['new']['redirect_path'],-1) != '/') $data['new']['redirect_path'] .= '/';
 			if(substr($data['new']['redirect_path'],0,8) == '[scheme]') $data['new']['redirect_path'] = '$scheme'.substr($data['new']['redirect_path'],8);
+			
 			/* Disabled path extension
 			if($data['new']['redirect_type'] == 'no' && substr($data['new']['redirect_path'],0,4) != 'http') {
 				$data['new']['redirect_path'] = $data['new']['document_root'].'/web'.realpath($data['new']['redirect_path']).'/';
@@ -777,22 +825,91 @@
 
 			switch($data['new']['subdomain']) {
 				case 'www':
+					if(substr($data['new']['redirect_path'],0,1) == '/'){ // relative path
+						$rewrite_exclude = '(?!'.substr($data['new']['redirect_path'],0,-1).')';
+					} else { // URL - check if URL is local
+						$tmp_redirect_path = $data['new']['redirect_path'];
+						if(substr($tmp_redirect_path,0,7) == '$scheme') $tmp_redirect_path = 'http'.substr($tmp_redirect_path,7);
+						$tmp_redirect_path_parts = parse_url($tmp_redirect_path);
+						if($tmp_redirect_path_parts['host'] == $data['new']['domain'] && ($tmp_redirect_path_parts['port'] == '80' || $tmp_redirect_path_parts['port'] == '443' || !isset($tmp_redirect_path_parts['port']))){
+							if(substr($tmp_redirect_path_parts['path'],-1) == '/') $tmp_redirect_path_parts['path'] = substr($tmp_redirect_path_parts['path'],0,-1);
+							if(substr($tmp_redirect_path_parts['path'],0,1) != '/') $tmp_redirect_path_parts['path'] = '/'.$tmp_redirect_path_parts['path'];
+							$rewrite_exclude = '(?!'.$tmp_redirect_path_parts['path'].')';
+						} else {
+							$rewrite_exclude = '(.?)';
+						}
+						unset($tmp_redirect_path);
+						unset($tmp_redirect_path_parts);
+					}
 					$rewrite_rules[] = array(	'rewrite_domain' 	=> '^'.$data['new']['domain'],
 					'rewrite_type' 		=> ($data['new']['redirect_type'] == 'no')?'':$data['new']['redirect_type'],
-					'rewrite_target' 	=> $data['new']['redirect_path']);
+					'rewrite_target' 	=> $data['new']['redirect_path'],
+					'rewrite_exclude'	=> $rewrite_exclude);
+					
+					if(substr($data['new']['redirect_path'],0,1) == '/'){ // relative path
+						$rewrite_exclude = '(?!'.substr($data['new']['redirect_path'],0,-1).')';
+					} else { // URL - check if URL is local
+						$tmp_redirect_path = $data['new']['redirect_path'];
+						if(substr($tmp_redirect_path,0,7) == '$scheme') $tmp_redirect_path = 'http'.substr($tmp_redirect_path,7);
+						$tmp_redirect_path_parts = parse_url($tmp_redirect_path);
+						if($tmp_redirect_path_parts['host'] == 'www.'.$data['new']['domain'] && ($tmp_redirect_path_parts['port'] == '80' || $tmp_redirect_path_parts['port'] == '443' || !isset($tmp_redirect_path_parts['port']))){
+							if(substr($tmp_redirect_path_parts['path'],-1) == '/') $tmp_redirect_path_parts['path'] = substr($tmp_redirect_path_parts['path'],0,-1);
+							if(substr($tmp_redirect_path_parts['path'],0,1) != '/') $tmp_redirect_path_parts['path'] = '/'.$tmp_redirect_path_parts['path'];
+							$rewrite_exclude = '(?!'.$tmp_redirect_path_parts['path'].')';
+						} else {
+							$rewrite_exclude = '(.?)';
+						}
+						unset($tmp_redirect_path);
+						unset($tmp_redirect_path_parts);
+					}
 					$rewrite_rules[] = array(	'rewrite_domain' 	=> '^www.'.$data['new']['domain'],
 							'rewrite_type' 		=> ($data['new']['redirect_type'] == 'no')?'':$data['new']['redirect_type'],
-							'rewrite_target' 	=> $data['new']['redirect_path']);
+							'rewrite_target' 	=> $data['new']['redirect_path'],
+							'rewrite_exclude'	=> $rewrite_exclude);
 					break;
 				case '*':
+					if(substr($data['new']['redirect_path'],0,1) == '/'){ // relative path
+						$rewrite_exclude = '(?!'.substr($data['new']['redirect_path'],0,-1).')';
+					} else { // URL - check if URL is local
+						$tmp_redirect_path = $data['new']['redirect_path'];
+						if(substr($tmp_redirect_path,0,7) == '$scheme') $tmp_redirect_path = 'http'.substr($tmp_redirect_path,7);
+						$tmp_redirect_path_parts = parse_url($tmp_redirect_path);
+						if(substr($tmp_redirect_path_parts['host'],-strlen($data['new']['domain'])) == $data['new']['domain'] && ($tmp_redirect_path_parts['port'] == '80' || $tmp_redirect_path_parts['port'] == '443' || !isset($tmp_redirect_path_parts['port']))){
+							if(substr($tmp_redirect_path_parts['path'],-1) == '/') $tmp_redirect_path_parts['path'] = substr($tmp_redirect_path_parts['path'],0,-1);
+							if(substr($tmp_redirect_path_parts['path'],0,1) != '/') $tmp_redirect_path_parts['path'] = '/'.$tmp_redirect_path_parts['path'];
+							$rewrite_exclude = '(?!'.$tmp_redirect_path_parts['path'].')';
+						} else {
+							$rewrite_exclude = '(.?)';
+						}
+						unset($tmp_redirect_path);
+						unset($tmp_redirect_path_parts);
+					}
 					$rewrite_rules[] = array(	'rewrite_domain' 	=> $data['new']['domain'],
 						'rewrite_type' 		=> ($data['new']['redirect_type'] == 'no')?'':$data['new']['redirect_type'],
-						'rewrite_target' 	=> $data['new']['redirect_path']);
+						'rewrite_target' 	=> $data['new']['redirect_path'],
+						'rewrite_exclude'	=> $rewrite_exclude);
 					break;
 				default:
+					if(substr($data['new']['redirect_path'],0,1) == '/'){ // relative path
+						$rewrite_exclude = '(?!'.substr($data['new']['redirect_path'],0,-1).')';
+					} else { // URL - check if URL is local
+						$tmp_redirect_path = $data['new']['redirect_path'];
+						if(substr($tmp_redirect_path,0,7) == '$scheme') $tmp_redirect_path = 'http'.substr($tmp_redirect_path,7);
+						$tmp_redirect_path_parts = parse_url($tmp_redirect_path);
+						if($tmp_redirect_path_parts['host'] == $data['new']['domain'] && ($tmp_redirect_path_parts['port'] == '80' || $tmp_redirect_path_parts['port'] == '443' || !isset($tmp_redirect_path_parts['port']))){
+							if(substr($tmp_redirect_path_parts['path'],-1) == '/') $tmp_redirect_path_parts['path'] = substr($tmp_redirect_path_parts['path'],0,-1);
+							if(substr($tmp_redirect_path_parts['path'],0,1) != '/') $tmp_redirect_path_parts['path'] = '/'.$tmp_redirect_path_parts['path'];
+							$rewrite_exclude = '(?!'.$tmp_redirect_path_parts['path'].')';
+						} else {
+							$rewrite_exclude = '(.?)';
+						}
+						unset($tmp_redirect_path);
+						unset($tmp_redirect_path_parts);
+					}
 					$rewrite_rules[] = array(	'rewrite_domain' 	=> '^'.$data['new']['domain'],
 					'rewrite_type' 		=> ($data['new']['redirect_type'] == 'no')?'':$data['new']['redirect_type'],
-					'rewrite_target' 	=> $data['new']['redirect_path']);
+					'rewrite_target' 	=> $data['new']['redirect_path'],
+					'rewrite_exclude'	=> $rewrite_exclude);
 			}
 		}
 
@@ -801,30 +918,30 @@
 		$server_alias = array();
 		switch($data['new']['subdomain']) {
 			case 'www':
-				$server_alias[] .= 'www.'.$data['new']['domain'].' ';
+				$server_alias[] = 'www.'.$data['new']['domain'].' ';
 				break;
 			case '*':
-				$server_alias[] .= '*.'.$data['new']['domain'].' ';
+				$server_alias[] = '*.'.$data['new']['domain'].' ';
 				break;
 		}
 		if(is_array($aliases)) {
 			foreach($aliases as $alias) {
 				switch($alias['subdomain']) {
 					case 'www':
-						$server_alias[] .= 'www.'.$alias['domain'].' '.$alias['domain'].' ';
+						$server_alias[] = 'www.'.$alias['domain'].' '.$alias['domain'].' ';
 						break;
 					case '*':
-						$server_alias[] .= '*.'.$alias['domain'].' '.$alias['domain'].' ';
+						$server_alias[] = '*.'.$alias['domain'].' '.$alias['domain'].' ';
 						break;
 					default:
-						$server_alias[] .= $alias['domain'].' ';
+						$server_alias[] = $alias['domain'].' ';
 						break;
 				}
 				$app->log('Add server alias: '.$alias['domain'],LOGLEVEL_DEBUG);
 				// Rewriting
-				if($alias['redirect_type'] != '') {
+				if($alias['redirect_type'] != '' && $alias['redirect_path'] != '') {
 					if(substr($alias['redirect_path'],-1) != '/') $alias['redirect_path'] .= '/';
-					if(substr($alias['redirect_path'],0,8) == '[scheme]') $alias['redirect_path'] = '$scheme'.substr($alias['redirect_path'],8);
+					if(substr($alias['redirect_path'],0,8) == '[scheme]') $alias['redirect_path'] = '$scheme'.substr($alias['redirect_path'],8);	
 					
 					/* Disabled the path extension
 					if($data['new']['redirect_type'] == 'no' && substr($data['new']['redirect_path'],0,4) != 'http') {
@@ -834,22 +951,91 @@
 					
 					switch($alias['subdomain']) {
 						case 'www':
+							if(substr($alias['redirect_path'],0,1) == '/'){ // relative path
+								$rewrite_exclude = '(?!'.substr($alias['redirect_path'],0,-1).')';
+							} else { // URL - check if URL is local
+								$tmp_redirect_path = $alias['redirect_path'];
+								if(substr($tmp_redirect_path,0,7) == '$scheme') $tmp_redirect_path = 'http'.substr($tmp_redirect_path,7);
+								$tmp_redirect_path_parts = parse_url($tmp_redirect_path);
+								if($tmp_redirect_path_parts['host'] == $alias['domain'] && ($tmp_redirect_path_parts['port'] == '80' || $tmp_redirect_path_parts['port'] == '443' || !isset($tmp_redirect_path_parts['port']))){
+									if(substr($tmp_redirect_path_parts['path'],-1) == '/') $tmp_redirect_path_parts['path'] = substr($tmp_redirect_path_parts['path'],0,-1);
+									if(substr($tmp_redirect_path_parts['path'],0,1) != '/') $tmp_redirect_path_parts['path'] = '/'.$tmp_redirect_path_parts['path'];
+									$rewrite_exclude = '(?!'.$tmp_redirect_path_parts['path'].')';
+								} else {
+									$rewrite_exclude = '(.?)';
+								}
+								unset($tmp_redirect_path);
+								unset($tmp_redirect_path_parts);
+							}
 							$rewrite_rules[] = array(	'rewrite_domain' 	=> '^'.$alias['domain'],
 								'rewrite_type' 		=> ($alias['redirect_type'] == 'no')?'':$alias['redirect_type'],
-								'rewrite_target' 	=> $alias['redirect_path']);
+								'rewrite_target' 	=> $alias['redirect_path'],
+								'rewrite_exclude'	=> $rewrite_exclude);
+								
+							if(substr($alias['redirect_path'],0,1) == '/'){ // relative path
+								$rewrite_exclude = '(?!'.substr($alias['redirect_path'],0,-1).')';
+							} else { // URL - check if URL is local
+								$tmp_redirect_path = $alias['redirect_path'];
+								if(substr($tmp_redirect_path,0,7) == '$scheme') $tmp_redirect_path = 'http'.substr($tmp_redirect_path,7);
+								$tmp_redirect_path_parts = parse_url($tmp_redirect_path);
+								if($tmp_redirect_path_parts['host'] == 'www.'.$alias['domain'] && ($tmp_redirect_path_parts['port'] == '80' || $tmp_redirect_path_parts['port'] == '443' || !isset($tmp_redirect_path_parts['port']))){
+									if(substr($tmp_redirect_path_parts['path'],-1) == '/') $tmp_redirect_path_parts['path'] = substr($tmp_redirect_path_parts['path'],0,-1);
+									if(substr($tmp_redirect_path_parts['path'],0,1) != '/') $tmp_redirect_path_parts['path'] = '/'.$tmp_redirect_path_parts['path'];
+									$rewrite_exclude = '(?!'.$tmp_redirect_path_parts['path'].')';
+								} else {
+									$rewrite_exclude = '(.?)';
+								}
+								unset($tmp_redirect_path);
+								unset($tmp_redirect_path_parts);
+							}
 							$rewrite_rules[] = array(	'rewrite_domain' 	=> '^www.'.$alias['domain'],
 									'rewrite_type' 		=> ($alias['redirect_type'] == 'no')?'':$alias['redirect_type'],
-									'rewrite_target' 	=> $alias['redirect_path']);
+									'rewrite_target' 	=> $alias['redirect_path'],
+									'rewrite_exclude'	=> $rewrite_exclude);
 							break;
 						case '*':
+							if(substr($alias['redirect_path'],0,1) == '/'){ // relative path
+								$rewrite_exclude = '(?!'.substr($alias['redirect_path'],0,-1).')';
+							} else { // URL - check if URL is local
+								$tmp_redirect_path = $alias['redirect_path'];
+								if(substr($tmp_redirect_path,0,7) == '$scheme') $tmp_redirect_path = 'http'.substr($tmp_redirect_path,7);
+								$tmp_redirect_path_parts = parse_url($tmp_redirect_path);
+								if(substr($tmp_redirect_path_parts['host'],-strlen($alias['domain'])) == $alias['domain'] && ($tmp_redirect_path_parts['port'] == '80' || $tmp_redirect_path_parts['port'] == '443' || !isset($tmp_redirect_path_parts['port']))){
+									if(substr($tmp_redirect_path_parts['path'],-1) == '/') $tmp_redirect_path_parts['path'] = substr($tmp_redirect_path_parts['path'],0,-1);
+									if(substr($tmp_redirect_path_parts['path'],0,1) != '/') $tmp_redirect_path_parts['path'] = '/'.$tmp_redirect_path_parts['path'];
+									$rewrite_exclude = '(?!'.$tmp_redirect_path_parts['path'].')';
+								} else {
+									$rewrite_exclude = '(.?)';
+								}
+								unset($tmp_redirect_path);
+								unset($tmp_redirect_path_parts);
+							}
 							$rewrite_rules[] = array(	'rewrite_domain' 	=> $alias['domain'],
 								'rewrite_type' 		=> ($alias['redirect_type'] == 'no')?'':$alias['redirect_type'],
-								'rewrite_target' 	=> $alias['redirect_path']);
+								'rewrite_target' 	=> $alias['redirect_path'],
+								'rewrite_exclude'	=> $rewrite_exclude);
 							break;
 						default:
+							if(substr($alias['redirect_path'],0,1) == '/'){ // relative path
+								$rewrite_exclude = '(?!'.substr($alias['redirect_path'],0,-1).')';
+							} else { // URL - check if URL is local
+								$tmp_redirect_path = $alias['redirect_path'];
+								if(substr($tmp_redirect_path,0,7) == '$scheme') $tmp_redirect_path = 'http'.substr($tmp_redirect_path,7);
+								$tmp_redirect_path_parts = parse_url($tmp_redirect_path);
+								if($tmp_redirect_path_parts['host'] == $alias['domain'] && ($tmp_redirect_path_parts['port'] == '80' || $tmp_redirect_path_parts['port'] == '443' || !isset($tmp_redirect_path_parts['port']))){
+									if(substr($tmp_redirect_path_parts['path'],-1) == '/') $tmp_redirect_path_parts['path'] = substr($tmp_redirect_path_parts['path'],0,-1);
+									if(substr($tmp_redirect_path_parts['path'],0,1) != '/') $tmp_redirect_path_parts['path'] = '/'.$tmp_redirect_path_parts['path'];
+									$rewrite_exclude = '(?!'.$tmp_redirect_path_parts['path'].')';
+								} else {
+									$rewrite_exclude = '(.?)';
+								}
+								unset($tmp_redirect_path);
+								unset($tmp_redirect_path_parts);
+							}
 							$rewrite_rules[] = array(	'rewrite_domain' 	=> '^'.$alias['domain'],
 							'rewrite_type' 		=> ($alias['redirect_type'] == 'no')?'':$alias['redirect_type'],
-							'rewrite_target' 	=> $alias['redirect_path']);
+							'rewrite_target' 	=> $alias['redirect_path'],
+							'rewrite_exclude'	=> $rewrite_exclude);
 					}
 				}
 			}
@@ -886,7 +1072,7 @@
 		if(file_exists($vhost_file)) copy($vhost_file,$vhost_file.'~');
 		
 		//* Write vhost file
-		file_put_contents($vhost_file,$tpl->grab());
+		file_put_contents($vhost_file,$this->nginx_merge_locations($tpl->grab()));
 		$app->log('Writing the vhost file: '.$vhost_file,LOGLEVEL_DEBUG);
 		unset($tpl);
 
@@ -1061,6 +1247,11 @@
 				if (is_dir($fastcgi_starter_path)) {
 					exec('rm -rf '.$fastcgi_starter_path);
 				}
+			}
+			
+			// 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
@@ -1338,6 +1529,7 @@
 		
 		$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_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') ) {
 				unlink($awstats_conf_dir.'/awstats.'.$data['old']['domain'].'.conf');
@@ -1372,20 +1564,50 @@
 	//* Update the PHP-FPM pool configuration file
 	private function php_fpm_pool_update ($data,$web_config,$pool_dir,$pool_name,$socket_dir) {
 		global $app, $conf;
-		//$reload = false;
+		/*
+		if(trim($data['new']['fastcgi_php_version']) != ''){
+			$default_php_fpm = false;
+			list($custom_php_fpm_name, $custom_php_fpm_init_script, $custom_php_fpm_ini_dir, $custom_php_fpm_pool_dir) = explode(':', trim($data['new']['fastcgi_php_version']));
+			if(substr($custom_php_fpm_ini_dir,-1) != '/') $custom_php_fpm_ini_dir .= '/';
+		} else {
+			$default_php_fpm = true;
+		}
+		*/
+		if($data['new']['php'] != 'no'){
+			if(trim($data['new']['fastcgi_php_version']) != ''){
+				$default_php_fpm = false;
+				list($custom_php_fpm_name, $custom_php_fpm_init_script, $custom_php_fpm_ini_dir, $custom_php_fpm_pool_dir) = explode(':', trim($data['new']['fastcgi_php_version']));
+				if(substr($custom_php_fpm_ini_dir,-1) != '/') $custom_php_fpm_ini_dir .= '/';
+			} else {
+				$default_php_fpm = true;
+			}
+		} else {
+			if(trim($data['old']['fastcgi_php_version']) != '' && $data['old']['php'] != 'no'){
+				$default_php_fpm = false;
+				list($custom_php_fpm_name, $custom_php_fpm_init_script, $custom_php_fpm_ini_dir, $custom_php_fpm_pool_dir) = explode(':', trim($data['old']['fastcgi_php_version']));
+				if(substr($custom_php_fpm_ini_dir,-1) != '/') $custom_php_fpm_ini_dir .= '/';
+			} else {
+				$default_php_fpm = true;
+			}
+		}
+		
+		$app->uses("getconf");
+		$web_config = $app->getconf->get_server_config($conf["server_id"], 'web');
 		
 		if($data['new']['php'] == 'no'){
 			if(@is_file($pool_dir.$pool_name.'.conf')){
 				unlink($pool_dir.$pool_name.'.conf');
-				//$reload = true;
 			}
-			//if($reload == true) $app->services->restartService('php-fpm','reload');
+			if($data['old']['php'] != 'no'){
+				if(!$default_php_fpm){
+					$app->services->restartService('php-fpm','reload:'.$custom_php_fpm_init_script);
+				} else {
+					$app->services->restartService('php-fpm','reload:'.$conf['init_scripts'].'/'.$web_config['php_fpm_init_script']);
+				}
+			}
 			return;
 		}
-			
-		$app->uses("getconf");
-		$web_config = $app->getconf->get_server_config($conf["server_id"], 'web');
-			
+				
 		$app->load('tpl');
 		$tpl = new tpl();
 		$tpl->newTemplate('php_fpm_pool.conf.master');
@@ -1408,10 +1630,13 @@
 		$tpl->setVar('fpm_port', $web_config['php_fpm_start_port'] + $data['new']['domain_id'] - 1);
 		$tpl->setVar('fpm_user', $data['new']['system_user']);
 		$tpl->setVar('fpm_group', $data['new']['system_group']);
+		$tpl->setVar('pm', $data['new']['pm']);
 		$tpl->setVar('pm_max_children', $data['new']['pm_max_children']);
 		$tpl->setVar('pm_start_servers', $data['new']['pm_start_servers']);
 		$tpl->setVar('pm_min_spare_servers', $data['new']['pm_min_spare_servers']);
 		$tpl->setVar('pm_max_spare_servers', $data['new']['pm_max_spare_servers']);
+		$tpl->setVar('pm_process_idle_timeout', $data['new']['pm_process_idle_timeout']);
+		$tpl->setVar('pm_max_requests', $data['new']['pm_max_requests']);
 		$tpl->setVar('document_root', $data['new']['document_root']);
 		$tpl->setVar('security_level',$web_config['security_level']);
 		$php_open_basedir = ($data['new']['php_open_basedir'] == '')?escapeshellcmd($data['new']['document_root']):escapeshellcmd($data['new']['php_open_basedir']);
@@ -1462,24 +1687,170 @@
 		file_put_contents($pool_dir.$pool_name.'.conf',$tpl->grab());
 		$app->log('Writing the PHP-FPM config file: '.$pool_dir.$pool_name.'.conf',LOGLEVEL_DEBUG);
 		unset($tpl);
-		//$reload = true;
-
-		//if($reload == true) $app->services->restartService('php-fpm','reload');
+		
+		// delete pool in all other PHP versions
+		$default_pool_dir = escapeshellcmd($web_config['php_fpm_pool_dir']);
+		if(substr($default_pool_dir,-1) != '/') $default_pool_dir .= '/';
+		if($default_pool_dir != $pool_dir){
+			if ( @is_file($default_pool_dir.$pool_name.'.conf') ) {
+					unlink($default_pool_dir.$pool_name.'.conf');
+					$app->log('Removed PHP-FPM config file: '.$default_pool_dir.$pool_name.'.conf',LOGLEVEL_DEBUG);
+					$app->services->restartService('php-fpm','reload:'.$conf['init_scripts'].'/'.$web_config['php_fpm_init_script']);
+			}
+		}
+		$php_versions = $app->db->queryAllRecords("SELECT * FROM server_php WHERE php_fpm_init_script != '' AND php_fpm_ini_dir != '' AND php_fpm_pool_dir != '' AND server_id = ".$conf["server_id"]);
+		if(is_array($php_versions) && !empty($php_versions)){
+			foreach($php_versions as $php_version){
+				if(substr($php_version['php_fpm_pool_dir'],-1) != '/') $php_version['php_fpm_pool_dir'] .= '/';
+				if($php_version['php_fpm_pool_dir'] != $pool_dir){
+					if ( @is_file($php_version['php_fpm_pool_dir'].$pool_name.'.conf') ) {
+						unlink($php_version['php_fpm_pool_dir'].$pool_name.'.conf');
+						$app->log('Removed PHP-FPM config file: '.$php_version['php_fpm_pool_dir'].$pool_name.'.conf',LOGLEVEL_DEBUG);
+						$app->services->restartService('php-fpm','reload:'.$php_version['php_fpm_init_script']);
+					}
+				}
+			}
+		}
+		// Reload current PHP-FPM after all others
+		sleep(1);
+		if(!$default_php_fpm){
+			$app->services->restartService('php-fpm','reload:'.$custom_php_fpm_init_script);
+		} else {
+			$app->services->restartService('php-fpm','reload:'.$conf['init_scripts'].'/'.$web_config['php_fpm_init_script']);
+		}
 	}
 	
 	//* Delete the PHP-FPM pool configuration file
 	private function php_fpm_pool_delete ($data,$web_config) {
-		global $app;
+		global $app, $conf;
 		
-		$pool_dir = escapeshellcmd($web_config['php_fpm_pool_dir']);
+		if(trim($data['old']['fastcgi_php_version']) != '' && $data['old']['php'] != 'no'){
+			$default_php_fpm = false;
+			list($custom_php_fpm_name, $custom_php_fpm_init_script, $custom_php_fpm_ini_dir, $custom_php_fpm_pool_dir) = explode(':', trim($data['old']['fastcgi_php_version']));
+			if(substr($custom_php_fpm_ini_dir,-1) != '/') $custom_php_fpm_ini_dir .= '/';
+		} else {
+			$default_php_fpm = true;
+		}
+		
+		if($default_php_fpm){
+			$pool_dir = escapeshellcmd($web_config['php_fpm_pool_dir']);
+		} else {
+			$pool_dir = $custom_php_fpm_pool_dir;
+		}
+		
 		if(substr($pool_dir,-1) != '/') $pool_dir .= '/';
 		$pool_name = 'web'.$data['old']['domain_id'];
 		
 		if ( @is_file($pool_dir.$pool_name.'.conf') ) {
 			unlink($pool_dir.$pool_name.'.conf');
 			$app->log('Removed PHP-FPM config file: '.$pool_dir.$pool_name.'.conf',LOGLEVEL_DEBUG);
-			//$app->services->restartService('php-fpm','reload');
 		}
+		
+		// delete pool in all other PHP versions
+		$default_pool_dir = escapeshellcmd($web_config['php_fpm_pool_dir']);
+		if(substr($default_pool_dir,-1) != '/') $default_pool_dir .= '/';
+		if($default_pool_dir != $pool_dir){
+			if ( @is_file($default_pool_dir.$pool_name.'.conf') ) {
+					unlink($default_pool_dir.$pool_name.'.conf');
+					$app->log('Removed PHP-FPM config file: '.$default_pool_dir.$pool_name.'.conf',LOGLEVEL_DEBUG);
+					$app->services->restartService('php-fpm','reload:'.$conf['init_scripts'].'/'.$web_config['php_fpm_init_script']);
+			}
+		}	
+		$php_versions = $app->db->queryAllRecords("SELECT * FROM server_php WHERE php_fpm_init_script != '' AND php_fpm_ini_dir != '' AND php_fpm_pool_dir != '' AND server_id = ".$data['old']['server_id']);
+		if(is_array($php_versions) && !empty($php_versions)){
+			foreach($php_versions as $php_version){
+				if(substr($php_version['php_fpm_pool_dir'],-1) != '/') $php_version['php_fpm_pool_dir'] .= '/';
+				if($php_version['php_fpm_pool_dir'] != $pool_dir){
+					if ( @is_file($php_version['php_fpm_pool_dir'].$pool_name.'.conf') ) {
+						unlink($php_version['php_fpm_pool_dir'].$pool_name.'.conf');
+						$app->log('Removed PHP-FPM config file: '.$php_version['php_fpm_pool_dir'].$pool_name.'.conf',LOGLEVEL_DEBUG);
+						$app->services->restartService('php-fpm','reload:'.$php_version['php_fpm_init_script']);
+					}
+				}
+			}
+		}
+		
+		// Reload current PHP-FPM after all others
+		sleep(1);
+		if(!$default_php_fpm){
+			$app->services->restartService('php-fpm','reload:'.$custom_php_fpm_init_script);
+		} else {
+			$app->services->restartService('php-fpm','reload:'.$conf['init_scripts'].'/'.$web_config['php_fpm_init_script']);
+		}
+	}
+	
+	private function nginx_merge_locations($vhost_conf){
+
+		$lines = explode("\n", $vhost_conf);
+		
+		if(is_array($lines) && !empty($lines)){
+		
+			$locations = array();
+			$islocation = false;
+			$linecount = sizeof($lines);
+
+			for($i=0;$i<$linecount;$i++){
+				$l = trim($lines[$i]);
+				if(substr($l, 0, 8) == 'location' && !$islocation){
+				
+					$islocation = true;
+					$level = 0;
+					
+					// Remove unnecessary whitespace
+					$l = preg_replace('/\s\s+/', ' ', $l);
+					
+					$loc_parts = explode(' ', $l);
+					// see http://wiki.nginx.org/HttpCoreModule#location
+					if($loc_parts[1] == '=' || $loc_parts[1] == '~' || $loc_parts[1] == '~*' || $loc_parts[1] == '^~'){
+						$location = $loc_parts[1].' '.$loc_parts[2];
+					} else {
+						$location = $loc_parts[1];
+					}
+					unset($loc_parts);
+					
+					if(!isset($locations[$location]['action'])) $locations[$location]['action'] = 'replace';
+					if(substr($l, -9) == '##merge##'){
+						$locations[$location]['action'] = 'merge';
+					}
+					
+					if(!isset($locations[$location]['open_tag'])) $locations[$location]['open_tag'] = '        location '.$location.' {';
+					if(!isset($locations[$location]['location']) || $locations[$location]['action'] == 'replace') $locations[$location]['location'] = '';
+					if(!isset($locations[$location]['end_tag'])) $locations[$location]['end_tag'] = '        }';
+					if(!isset($locations[$location]['start_line'])) $locations[$location]['start_line'] = $i;
+
+					unset($lines[$i]);
+					
+				} else {
+				
+					if($islocation){
+						if(strpos($l, '{') !== false){
+							$level += 1;
+						}
+						if(strpos($l, '}') !== false && $level > 0){
+							$level -= 1;
+							$locations[$location]['location'] .= $lines[$i]."\n";
+						} elseif(strpos($l, '}') !== false && $level == 0){
+							$islocation = false;
+						} else {
+							$locations[$location]['location'] .= $lines[$i]."\n";
+						}
+						unset($lines[$i]);
+					}
+					
+				}
+			}
+			
+			if(is_array($locations) && !empty($locations)){
+				foreach($locations as $key => $val){
+					$new_location = $val['open_tag']."\n".$val['location'].$val['end_tag'];
+					$lines[$val['start_line']] = $new_location;
+				}
+			}
+			ksort($lines);
+			$vhost_conf = implode("\n", $lines);
+		}
+		
+		return $vhost_conf;
 	}
 	
 	function client_delete($event_name,$data) {

--
Gitblit v1.9.1