From 7fddfe840a1c8680e9103bc6b93465f3390de7bb Mon Sep 17 00:00:00 2001
From: tbrehm <t.brehm@ispconfig.org>
Date: Thu, 8 Jul 2010 13:56:05 +0000
Subject: [PATCH] - Implemented: FS#1079 - Custom php.ini files for fcgi and
 mod_php - Fixed a formatting problem with suphp.conf and newer suphp
 versions.

---
 install/lib/installer_base.lib.php            |  2 +-
 install/sql/ispconfig3.sql                    |  1 +
 install/tpl/server.ini.master                 |  2 +
 .../web/admin/form/server_config.tform.php    | 22 ++++++++++
 .../web/admin/lib/lang/en_server_config.lng   |  3 ++
 .../templates/server_config_web_edit.htm      |  8 ++++
 interface/web/sites/form/web_domain.tform.php |  8 ++++
 .../web/sites/lib/lang/en_web_domain.lng      |  3 +-
 .../sites/templates/web_domain_advanced.htm   | 10 +++--
 server/conf/php-cgi-starter.master            |  4 ++
 server/conf/vhost.conf.master                 |  8 ++++
 .../plugins-available/apache2_plugin.inc.php  | 42 +++++++++++++++++--
 12 files changed, 105 insertions(+), 8 deletions(-)

diff --git a/install/lib/installer_base.lib.php b/install/lib/installer_base.lib.php
index 350ae05f9d..e242c6429f 100644
--- a/install/lib/installer_base.lib.php
+++ b/install/lib/installer_base.lib.php
@@ -944,7 +944,7 @@ class installer_base {
 		exec('mkdir -p /var/log/ispconfig/httpd');
 
 		if(is_file('/etc/suphp/suphp.conf')) {
-			replaceLine('/etc/suphp/suphp.conf','php=php:/usr/bin','x-httpd-suphp=php:/usr/bin/php-cgi',0);
+			replaceLine('/etc/suphp/suphp.conf','php=php:/usr/bin','x-httpd-suphp="php:/usr/bin/php-cgi"',0);
 			//replaceLine('/etc/suphp/suphp.conf','docroot=','docroot=/var/clients',0);
 			replaceLine('/etc/suphp/suphp.conf','umask=0077','umask=0022',0);
 		}
diff --git a/install/sql/ispconfig3.sql b/install/sql/ispconfig3.sql
index 2a8800363b..504886bed2 100644
--- a/install/sql/ispconfig3.sql
+++ b/install/sql/ispconfig3.sql
@@ -1084,6 +1084,7 @@ CREATE TABLE `web_domain` (
   `allow_override` varchar(255) NOT NULL default 'All',
   `apache_directives` text,
   `php_open_basedir` text,
+  `custom_php_ini` text,
   `backup_interval` VARCHAR( 255 ) NOT NULL DEFAULT 'none',
   `backup_copies` INT NOT NULL DEFAULT '1',
   `active` enum('n','y') NOT NULL default 'y',
diff --git a/install/tpl/server.ini.master b/install/tpl/server.ini.master
index d5584ba895..01fcd80ff9 100644
--- a/install/tpl/server.ini.master
+++ b/install/tpl/server.ini.master
@@ -51,6 +51,8 @@ awstats_conf_dir=/etc/awstats
 awstats_data_dir=/var/lib/awstats
 awstats_pl=/usr/lib/cgi-bin/awstats.pl
 awstats_buildstaticpages_pl=/usr/share/awstats/tools/awstats_buildstaticpages.pl
+php_ini_path_apache=/etc/php5/apache2/php.ini
+php_ini_path_cgi=/etc/php5/cgi/php.ini
 
 [dns]
 bind_user=root
diff --git a/interface/web/admin/form/server_config.tform.php b/interface/web/admin/form/server_config.tform.php
index cf43c9a351..585a120675 100644
--- a/interface/web/admin/form/server_config.tform.php
+++ b/interface/web/admin/form/server_config.tform.php
@@ -398,6 +398,28 @@ $form["tabs"]['web'] = array (
 			'width'		=> '40',
 			'maxlength'	=> '255'
 		),
+		'php_ini_path_apache' => array (
+			'datatype'	=> 'VARCHAR',
+			'formtype'	=> 'TEXT',
+			'default'	=> '',
+			'validators'	=> array ( 	0 => array (	'type'	=> 'NOTEMPTY',
+														'errmsg'=> 'php_ini_path_apache_error_empty'),
+									),
+			'value'		=> '',
+			'width'		=> '40',
+			'maxlength'	=> '255'
+		),
+		'php_ini_path_cgi' => array (
+			'datatype'	=> 'VARCHAR',
+			'formtype'	=> 'TEXT',
+			'default'	=> '',
+			'validators'	=> array ( 	0 => array (	'type'	=> 'NOTEMPTY',
+														'errmsg'=> 'php_ini_path_cgi_error_empty'),
+									),
+			'value'		=> '',
+			'width'		=> '40',
+			'maxlength'	=> '255'
+		),
 		'php_open_basedir' => array (
 			'datatype'	=> 'VARCHAR',
 			'formtype'	=> 'TEXT',
diff --git a/interface/web/admin/lib/lang/en_server_config.lng b/interface/web/admin/lib/lang/en_server_config.lng
index af703c4fde..bacf38ddc2 100644
--- a/interface/web/admin/lib/lang/en_server_config.lng
+++ b/interface/web/admin/lib/lang/en_server_config.lng
@@ -70,4 +70,7 @@ $wb["awstats_data_dir_txt"] = 'awstats data folder';
 $wb["awstats_pl_txt"] = 'awstats.pl script';
 $wb["awstats_buildstaticpages_pl_txt"] = 'awstats_buildstaticpages.pl script';
 $wb["backup_dir_txt"] = 'Backup directory';
+$wb["named_conf_local_path_txt"] = 'BIND named.conf.local path';
+$wb["php_ini_path_cgi_txt"] = 'CGI php.ini path';
+$wb["php_ini_path_apache_txt"] = 'Apache php.ini path';
 ?>
\ No newline at end of file
diff --git a/interface/web/admin/templates/server_config_web_edit.htm b/interface/web/admin/templates/server_config_web_edit.htm
index d08d032c46..68c57bd97c 100644
--- a/interface/web/admin/templates/server_config_web_edit.htm
+++ b/interface/web/admin/templates/server_config_web_edit.htm
@@ -42,6 +42,14 @@
         <input name="group" id="group" value="{tmpl_var name='group'}" size="40" maxlength="255" type="text" class="textInput" />
 	  </div>
 	  <div class="ctrlHolder">
+      	<label for="php_ini_path_apache">{tmpl_var name='php_ini_path_apache_txt'}</label>
+        <input name="php_ini_path_apache" id="php_ini_path_apache" value="{tmpl_var name='php_ini_path_apache'}" size="40" maxlength="255" type="text" class="textInput" />
+	  </div>
+	  <div class="ctrlHolder">
+      	<label for="php_ini_path_cgi">{tmpl_var name='php_ini_path_cgi_txt'}</label>
+        <input name="php_ini_path_cgi" id="php_ini_path_cgi" value="{tmpl_var name='php_ini_path_cgi'}" size="40" maxlength="255" type="text" class="textInput" />
+	  </div>
+	  <div class="ctrlHolder">
       	<label for="php_open_basedir">{tmpl_var name='php_open_basedir_txt'}</label>
         <input name="php_open_basedir" id="php_open_basedir" value="{tmpl_var name='php_open_basedir'}" size="40" maxlength="255" type="text" class="textInput" />
 	  </div>
diff --git a/interface/web/sites/form/web_domain.tform.php b/interface/web/sites/form/web_domain.tform.php
index ce76b690ad..a21a4850d8 100644
--- a/interface/web/sites/form/web_domain.tform.php
+++ b/interface/web/sites/form/web_domain.tform.php
@@ -487,6 +487,14 @@ $form["tabs"]['advanced'] = array (
 			'width'		=> '30',
 			'maxlength'	=> '255'
 		),
+		'custom_php_ini' => array (
+			'datatype'	=> 'TEXT',
+			'formtype'	=> 'TEXT',
+			'default'	=> '',
+			'value'		=> '',
+			'width'		=> '30',
+			'maxlength'	=> '255'
+		),
 		'apache_directives' => array (
 			'datatype'	=> 'TEXT',
 			'formtype'	=> 'TEXT',
diff --git a/interface/web/sites/lib/lang/en_web_domain.lng b/interface/web/sites/lib/lang/en_web_domain.lng
index d20f1b9efd..79438c8558 100644
--- a/interface/web/sites/lib/lang/en_web_domain.lng
+++ b/interface/web/sites/lib/lang/en_web_domain.lng
@@ -49,7 +49,7 @@ $wb["error_ssl_organisation_unit_empty"] = 'SSL Organisation Unit is empty.';
 $wb["error_ssl_country_empty"] = 'SSL Country is empty.';
 $wb["client_group_id_txt"] = 'Client';
 $wb["stats_password_txt"] = 'Webstatistics password';
-$wb["allow_override_txt"] = 'Allow Override';
+$wb["allow_override_txt"] = 'Apache AllowOverride';
 $wb["limit_web_quota_free_txt"] = 'Max. available Harddisk Quota';
 $wb["ssl_state_error_regex"] = 'Invalid SSL State. Valid characters are: a-z, 0-9 and .,-_';
 $wb["ssl_locality_error_regex"] = 'Invalid SSL Locality. Valid characters are: a-z, 0-9 and .,-_';
@@ -63,4 +63,5 @@ $wb["traffic_quota_exceeded_txt"] = 'Traffic quota exceeded';
 $wb["ruby_txt"] = 'Ruby';
 $wb["stats_user_txt"] = 'Webstatistics username';
 $wb["stats_type_txt"] = 'Webstatistics program';
+$wb["custom_php_ini_txt"] = 'Custom php.ini settings';
 ?>
\ No newline at end of file
diff --git a/interface/web/sites/templates/web_domain_advanced.htm b/interface/web/sites/templates/web_domain_advanced.htm
index a9b9d02649..f00b3dad97 100644
--- a/interface/web/sites/templates/web_domain_advanced.htm
+++ b/interface/web/sites/templates/web_domain_advanced.htm
@@ -22,12 +22,16 @@
 	  </div>
 	  <div class="ctrlHolder">
       	<label for="php_open_basedir">{tmpl_var name='php_open_basedir_txt'}</label>
-        <input name="php_open_basedir" id="php_open_basedir" value="{tmpl_var name='php_open_basedir'}" size="30" maxlength="255" type="text" class="textInput" />
+        <input name="php_open_basedir" id="php_open_basedir" value="{tmpl_var name='php_open_basedir'}" size="30" maxlength="255" type="text" class="textInput"  style="width:400px;" />
+	  </div>
+	  <div class="ctrlHolder">
+      	<label for="custom_php_ini">{tmpl_var name='custom_php_ini_txt'}</label>
+      	<textarea name="custom_php_ini" id="custom_php_ini" rows='10' cols='50' style="width:400px;">{tmpl_var name='custom_php_ini'}</textarea>
 	  </div>
       <div class="ctrlHolder">
       	<label for="apache_directives">{tmpl_var name='apache_directives_txt'}</label>
-      	<textarea name="apache_directives" id="apache_directives" rows='10' cols='30'>{tmpl_var name='apache_directives'}</textarea>
-			</div>
+      	<textarea name="apache_directives" id="apache_directives" rows='10' cols='50' style="width:400px;">{tmpl_var name='apache_directives'}</textarea>
+	  </div>
     </fieldset>
 
     <input type="hidden" name="id" value="{tmpl_var name='id'}">
diff --git a/server/conf/php-cgi-starter.master b/server/conf/php-cgi-starter.master
index 3c00f9dba8..be30993221 100644
--- a/server/conf/php-cgi-starter.master
+++ b/server/conf/php-cgi-starter.master
@@ -1,5 +1,9 @@
 #!/bin/sh
 
+<tmpl_if name='has_custom_php_ini'>
+export PHPRC="<tmpl_var name='php_ini_path'>"
+</tmpl_if>
+
 exec <tmpl_var name='php_cgi_bin'> \
 -d open_basedir=<tmpl_var name='open_basedir'> \
 -d upload_tmp_dir=<tmpl_var name='document_root'>/tmp \
diff --git a/server/conf/vhost.conf.master b/server/conf/vhost.conf.master
index 1a12fa8cd4..d6f33a97c1 100644
--- a/server/conf/vhost.conf.master
+++ b/server/conf/vhost.conf.master
@@ -118,6 +118,7 @@
     php_admin_value sendmail_path "/usr/sbin/sendmail -t -i -fwebmaster@<tmpl_var name='domain'>"	
     php_admin_value upload_tmp_dir <tmpl_var name='document_root'>/tmp
     php_admin_value session.save_path <tmpl_var name='document_root'>/tmp
+	PHPIniDir <tmpl_var name='custom_php_ini_dir'>
 <tmpl_if name='security_level' op='==' value='20'>
     php_admin_value open_basedir <tmpl_var name='php_open_basedir'>
 </tmpl_if>
@@ -127,6 +128,9 @@
     <Directory {tmpl_var name='web_document_root'}>
         suPHP_Engine on
         # suPHP_UserGroup <tmpl_var name='system_user'> <tmpl_var name='system_group'>
+<tmpl_if name='has_custom_php_ini'>
+	suPHP_ConfigPath <tmpl_var name='custom_php_ini_dir'>
+</tmpl_if>
         AddHandler x-httpd-suphp .php .php3 .php4 .php5
         suPHP_AddHandler x-httpd-suphp
     </Directory>
@@ -305,6 +309,7 @@
     php_admin_value sendmail_path "/usr/sbin/sendmail -t -i -fwebmaster@<tmpl_var name='domain'>"	
     php_admin_value upload_tmp_dir <tmpl_var name='document_root'>/tmp
     php_admin_value session.save_path <tmpl_var name='document_root'>/tmp
+	PHPIniDir <tmpl_var name='custom_php_ini_dir'>
 <tmpl_if name='security_level' op='==' value='20'>
     php_admin_value open_basedir <tmpl_var name='php_open_basedir'>
 </tmpl_if>
@@ -312,6 +317,9 @@
 <tmpl_if name='php' op='==' value='suphp'>
     suPHP_Engine on
     # suPHP_UserGroup <tmpl_var name='system_user'> <tmpl_var name='system_group'>
+<tmpl_if name='has_custom_php_ini'>
+	suPHP_ConfigPath <tmpl_var name='custom_php_ini_dir'>
+</tmpl_if>
     AddHandler x-httpd-suphp .php .php3 .php4 .php5
     suPHP_AddHandler x-httpd-suphp
 </tmpl_if>
diff --git a/server/plugins-available/apache2_plugin.inc.php b/server/plugins-available/apache2_plugin.inc.php
index c0fd8223e9..2f106ef5f3 100644
--- a/server/plugins-available/apache2_plugin.inc.php
+++ b/server/plugins-available/apache2_plugin.inc.php
@@ -543,7 +543,30 @@ class apache2_plugin {
 		$this->_exec("chown $username:$groupname ".escapeshellcmd($data["new"]["document_root"])."/log/error.log");
 
 
-		// Create the vhost config file
+		//* Write the custom php.ini file, if custom_php_ini filed is not empty
+		$custom_php_ini_dir = $web_config['website_basedir'].'/conf/'.$data["new"]["system_user"];
+		if(!is_dir($web_config['website_basedir'].'/conf')) mkdir($web_config['website_basedir'].'/conf');
+		if(trim($data["new"]['custom_php_ini']) != '') {
+			$has_custom_php_ini = true;
+			if(!is_dir($custom_php_ini_dir)) mkdir($custom_php_ini_dir);
+			$php_ini_content = '';
+			if($data["new"]['php'] == 'mod') {
+				$master_php_ini_path = $web_config['php_ini_path_apache'];
+			} else {
+				$master_php_ini_path = $web_config['php_ini_path_cgi'];
+			}
+			if($master_php_ini_path != '' && substr($master_php_ini_path,-7) == 'php.ini' && is_file($master_php_ini_path)) {
+				$php_ini_content .= file_get_contents($master_php_ini_path)."\n";
+			}
+			$php_ini_content .= trim($data["new"]['custom_php_ini']);
+			file_put_contents($custom_php_ini_dir.'/php.ini',$php_ini_content);
+		} else {
+			$has_custom_php_ini = false;
+			if(is_file($custom_php_ini_dir.'/php.ini')) unlink($custom_php_ini_dir.'/php.ini');
+		}
+
+
+		//* Create the vhost config file
 		$app->load('tpl');
 
 		$tpl = new tpl();
@@ -557,6 +580,8 @@ class apache2_plugin {
 		$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);
 
 		// Check if a SSL cert exists
 		$ssl_dir = $data["new"]["document_root"]."/ssl";
@@ -709,8 +734,12 @@ class apache2_plugin {
 
 			$fcgi_tpl = new tpl();
 			$fcgi_tpl->newTemplate("php-fcgi-starter.master");
-
-			$fcgi_tpl->setVar('php_ini_path',escapeshellcmd($fastcgi_config["fastcgi_phpini_path"]));
+			
+			if($has_custom_php_ini) {
+				$fcgi_tpl->setVar('php_ini_path',escapeshellcmd($custom_php_ini_dir));
+			} else {
+				$fcgi_tpl->setVar('php_ini_path',escapeshellcmd($fastcgi_config["fastcgi_phpini_path"]));
+			}
 			$fcgi_tpl->setVar('document_root',escapeshellcmd($data["new"]["document_root"]));
 			$fcgi_tpl->setVar('php_fcgi_children',escapeshellcmd($fastcgi_config["fastcgi_children"]));
 			$fcgi_tpl->setVar('php_fcgi_max_requests',escapeshellcmd($fastcgi_config["fastcgi_max_requests"]));
@@ -772,6 +801,13 @@ class apache2_plugin {
 			//$cgi_tpl->setVar('open_basedir', "/var/www/" . $data["new"]["domain"]);
 			$cgi_tpl->setVar('php_cgi_bin',$cgi_config["cgi_bin"]);
 			$cgi_tpl->setVar('security_level',$web_config["security_level"]);
+			
+			$cgi_tpl->setVar('has_custom_php_ini',$has_custom_php_ini);
+			if($has_custom_php_ini) {
+				$cgi_tpl->setVar('php_ini_path',escapeshellcmd($custom_php_ini_dir));
+			} else {
+				$cgi_tpl->setVar('php_ini_path',escapeshellcmd($fastcgi_config["fastcgi_phpini_path"]));
+			}
 
 			$cgi_starter_script = escapeshellcmd($cgi_starter_path.$cgi_config["cgi_starter_script"]);
 			file_put_contents($cgi_starter_script,$cgi_tpl->grab());
-- 
GitLab