From add36e9a703c1977aca1d835526b017d57828ba6 Mon Sep 17 00:00:00 2001
From: ftimme <ft@falkotimme.com>
Date: Mon, 19 Sep 2011 06:46:21 -0400
Subject: [PATCH] - Added open_basedir support to PHP-FPM.

---
 server/plugins-available/nginx_plugin.inc.php |  121 ++++++++++++++++++++++++++++++++--------
 1 files changed, 96 insertions(+), 25 deletions(-)

diff --git a/server/plugins-available/nginx_plugin.inc.php b/server/plugins-available/nginx_plugin.inc.php
index cd2b351..44cd26b 100644
--- a/server/plugins-available/nginx_plugin.inc.php
+++ b/server/plugins-available/nginx_plugin.inc.php
@@ -435,42 +435,42 @@
 			// Copy the error pages
 			if($data['new']['errordocs']) {
 				$error_page_path = escapeshellcmd($data['new']['document_root']).'/web/error/';
-				if (file_exists($conf['templates'] . '-custom/error/'.substr(escapeshellcmd($conf['language']),0,2))) {
-					exec('cp ' . $conf['templates'] . '-custom/error/'.substr(escapeshellcmd($conf['language']),0,2).'/* '.$error_page_path);
+				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);
 				}
 				else {
-					if (file_exists($conf['templates'] . '-custom/error/400.html')) {
-						exec('cp '. $conf['templates'] .'-custom/error/*.html '.$error_page_path);
+					if (file_exists($conf['rootpath'].'/conf-custom/error/400.html')) {
+						exec('cp '. $conf['rootpath'].'/conf-custom/error/*.html '.$error_page_path);
 					}
 					else {
-						exec('cp ' . $conf['templates'] . '/error/'.substr(escapeshellcmd($conf['language']),0,2).'/* '.$error_page_path);
+						exec('cp ' . $conf['rootpath'] . '/conf/error/'.substr(escapeshellcmd($conf['language']),0,2).'/* '.$error_page_path);
 					}
 				}
 				exec('chmod -R a+r '.$error_page_path);
 			}
 
-			if (file_exists($conf['templates'] . '-custom/index/standard_index.html_'.substr(escapeshellcmd($conf['language']),0,2))) {
-				exec('cp ' . $conf['templates'] . '-custom/index/standard_index.html_'.substr(escapeshellcmd($conf['language']),0,2).' '.escapeshellcmd($data['new']['document_root']).'/web/index.html');
+			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');
             
-			if(is_file($conf['templates'] . '-custom/index/favicon.ico')) {
-                exec('cp ' . $conf['templates'] . '-custom/index/favicon.ico '.escapeshellcmd($data['new']['document_root']).'/web/');
+			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/');
             }
-			if(is_file($conf['templates'] . '-custom/index/robots.txt')) {
-                exec('cp ' . $conf['templates'] . '-custom/index/robots.txt '.escapeshellcmd($data['new']['document_root']).'/web/');
+			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/');
                 }
-                if(is_file($conf['templates'] . '-custom/index/.htaccess')) {
-                    exec('cp ' . $conf['templates'] . '-custom/index/.htaccess '.escapeshellcmd($data['new']['document_root']).'/web/');
+                if(is_file($conf['rootpath'] . '/conf-custom/index/.htaccess')) {
+                    exec('cp ' . $conf['rootpath'] . '/conf-custom/index/.htaccess '.escapeshellcmd($data['new']['document_root']).'/web/');
                 }
             }
 			else {
-				if (file_exists($conf['templates'] . '-custom/index/standard_index.html')) {
-					exec('cp ' . $conf['templates'] . '-custom/index/standard_index.html '.escapeshellcmd($data['new']['document_root']).'/web/index.html');
+				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');
 				}
 				else {
-					exec('cp ' . $conf['templates'] . '/index/standard_index.html_'.substr(escapeshellcmd($conf['language']),0,2).' '.escapeshellcmd($data['new']['document_root']).'/web/index.html');
-					if(is_file($conf['templates'] . '/index/favicon.ico')) exec('cp ' . $conf['templates'] . '/index/favicon.ico '.escapeshellcmd($data['new']['document_root']).'/web/');
-					if(is_file($conf['templates'] . '/index/robots.txt')) exec('cp ' . $conf['templates'] . '/index/robots.txt '.escapeshellcmd($data['new']['document_root']).'/web/');
-					if(is_file($conf['templates'] . '/index/.htaccess')) exec('cp ' . $conf['templates'] . '/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/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('chmod -R a+r '.escapeshellcmd($data['new']['document_root']).'/web/');
@@ -479,15 +479,15 @@
 		} 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/';
-			if (file_exists($conf['templates'] . '-custom/error/'.substr(escapeshellcmd($conf['language']),0,2))) {
-				exec('cp ' . $conf['templates'] . '-custom/error/'.substr(escapeshellcmd($conf['language']),0,2).'/* '.$error_page_path);
+			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);
 			}
 			else {
-				if (file_exists($conf['templates'] . '-custom/error/400.html')) {
-					exec('cp ' . $conf['templates'] . '-custom/error/*.html '.$error_page_path);
+				if (file_exists($conf['rootpath'] . '/conf-custom/error/400.html')) {
+					exec('cp ' . $conf['rootpath'] . '/conf-custom/error/*.html '.$error_page_path);
 				}
 				else {
-					exec('cp ' . $conf['templates'] . '/error/'.substr(escapeshellcmd($conf['language']),0,2).'/* '.$error_page_path);
+					exec('cp ' . $conf['rootpath'] . '/conf/error/'.substr(escapeshellcmd($conf['language']),0,2).'/* '.$error_page_path);
 				}
 			}
 			exec('chmod -R a+r '.$error_page_path);
@@ -640,10 +640,10 @@
 		$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'];
-		//$vhost_data['php_open_basedir'] = ($data['new']['php_open_basedir'] == '')?$data['new']['document_root']:$data['new']['php_open_basedir'];
 		$vhost_data['ssl_domain'] = $data['new']['ssl_domain'];
 		//$vhost_data['has_custom_php_ini'] = $has_custom_php_ini;
 		//$vhost_data['custom_php_ini_dir'] = escapeshellcmd($custom_php_ini_dir);
+		$vhost_data['fpm_port'] = $web_config['php_fpm_start_port'] + $data['new']['domain_id'];
 
 		// Check if a SSL cert exists
 		$ssl_dir = $data['new']['document_root'].'/ssl';
@@ -843,6 +843,8 @@
 			$this->awstats_update($data,$web_config);
 		}
 		
+		$this->php_fpm_pool_update($data,$web_config);
+		
 		if($web_config['check_apache_config'] == 'y') {
 			//* Test if nginx starts with the new configuration file
 			$nginx_online_status_before_restart = $this->_checkTcp('localhost',80);
@@ -976,6 +978,10 @@
 			if($data['old']['stats_type'] == 'awstats') {
 				$this->awstats_delete($data,$web_config);
 			}
+			
+			if($data['old']['php'] == 'fast-cgi') {
+				$this->php_fpm_pool_delete($data,$web_config);
+			}
 
 		}
 	}
@@ -1039,6 +1045,71 @@
 		}
 	}
 	
+	//* Update the PHP-FPM pool configuration file
+	private function php_fpm_pool_update ($data,$web_config) {
+		global $app, $conf;
+		
+		$pool_dir = $web_config['php_fpm_pool_dir'];
+		//$reload = false;
+		
+		if($data['new']['php'] == 'no'){
+			if(@is_file($pool_dir.'/'.$data['old']['domain'].'.conf')){
+				unlink($pool_dir.'/'.$data['old']['domain'].'.conf');
+				//$reload = true;
+			}
+			if(@is_file($pool_dir.'/'.$data['new']['domain'].'.conf')){
+				unlink($pool_dir.'/'.$data['new']['domain'].'.conf');
+				//$reload = true;
+			}
+			//if($reload == true) $app->services->restartService('php-fpm','reload');
+			return;
+		}
+		
+		if(!@is_file($pool_dir.'/'.$data['new']['domain'].'.conf') || ($data['old']['domain'] != '' && $data['new']['domain'] != $data['old']['domain'])) {
+			if ( @is_file($pool_dir.'/'.$data['old']['domain'].'.conf') ) {
+				unlink($pool_dir.'/'.$data['old']['domain'].'.conf');
+			}
+			
+			$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');
+
+			$tpl->setVar('fpm_pool', $data['new']['domain']);
+			$tpl->setVar('fpm_port', $web_config['php_fpm_start_port'] + $data['new']['domain_id']);
+			$tpl->setVar('fpm_user', $data['new']['system_user']);
+			$tpl->setVar('fpm_group', $data['new']['system_group']);
+			$php_open_basedir = ($data['new']['php_open_basedir'] == '')?$data['new']['document_root']:$data['new']['php_open_basedir'];
+			$tpl->setVar('php_open_basedir', $php_open_basedir);
+			if($php_open_basedir != ''){
+				$tpl->setVar('enable_php_open_basedir', '');
+			} else {
+				$tpl->setVar('enable_php_open_basedir', ';');
+			}
+			
+			file_put_contents($pool_dir.'/'.$data['new']['domain'].'.conf',$tpl->grab());
+			$app->log('Writing the PHP-FPM config file: '.$pool_dir.'/'.$data['new']['domain'].'.conf',LOGLEVEL_DEBUG);
+			unset($tpl);
+			//$reload = true;
+		}
+		//if($reload == true) $app->services->restartService('php-fpm','reload');
+	}
+	
+	//* Delete the PHP-FPM pool configuration file
+	private function php_fpm_pool_delete ($data,$web_config) {
+		global $app;
+		
+		$pool_dir = $web_config['php_fpm_pool_dir'];
+		
+		if ( @is_file($pool_dir.'/'.$data['old']['domain'].'.conf') ) {
+			unlink($pool_dir.'/'.$data['old']['domain'].'.conf');
+			$app->log('Removed PHP-FPM config file: '.$pool_dir.'/'.$data['old']['domain'].'.conf',LOGLEVEL_DEBUG);
+			$app->services->restartService('php-fpm','reload');
+		}
+	}
+	
 	function client_delete($event_name,$data) {
 		global $app, $conf;
 		

--
Gitblit v1.9.1