From 2655f9315464236f2e41d2b6467d9cf97cb2b34b Mon Sep 17 00:00:00 2001
From: ftimme <ft@falkotimme.com>
Date: Fri, 30 Mar 2012 11:15:14 -0400
Subject: [PATCH] - nginx: added function to merge duplicate locations in the nginx vhost configuration.

---
 server/plugins-available/nginx_plugin.inc.php |  190 +++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 182 insertions(+), 8 deletions(-)

diff --git a/server/plugins-available/nginx_plugin.inc.php b/server/plugins-available/nginx_plugin.inc.php
index 149a4d1..69cd032 100644
--- a/server/plugins-available/nginx_plugin.inc.php
+++ b/server/plugins-available/nginx_plugin.inc.php
@@ -712,7 +712,20 @@
 		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($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']);
@@ -898,7 +911,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);
 
@@ -1386,18 +1399,33 @@
 		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;
+		}
+		
+		$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($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']);
+				}
+			}
 			//if($reload == true) $app->services->restartService('php-fpm','reload');
 			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');
@@ -1474,6 +1502,38 @@
 		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);
+		
+		// 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']);
+		}
+		
 		//$reload = true;
 
 		//if($reload == true) $app->services->restartService('php-fpm','reload');
@@ -1481,17 +1541,131 @@
 	
 	//* 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']) != ''){
+			$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]['open_tag'])) $locations[$location]['open_tag'] = '        location '.$location.' {';
+					if(!isset($locations[$location]['location'])) $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