From 02e7eaf2860f6f892eb32639940e97c76b65baad Mon Sep 17 00:00:00 2001
From: Marius Cramer <m.cramer@pixcept.de>
Date: Mon, 15 Jun 2015 19:24:18 +0200
Subject: [PATCH] - backported patches

---
 interface/lib/classes/db_mysql.inc.php        |  2 +-
 interface/web/dns/dns_soa_edit.php            |  8 +++----
 interface/web/dns/lib/lang/de_dns_soa.lng     |  1 +
 interface/web/dns/lib/lang/en_dns_soa.lng     |  1 +
 .../mail/form/mail_relay_recipient.tform.php  |  2 +-
 server/lib/classes/db_mysql.inc.php           |  2 +-
 .../plugins-available/apache2_plugin.inc.php  | 22 +++++++++++------
 server/plugins-available/nginx_plugin.inc.php | 24 ++++++++++++-------
 .../shelluser_base_plugin.inc.php             | 19 +++++++++++++++
 9 files changed, 58 insertions(+), 23 deletions(-)

diff --git a/interface/lib/classes/db_mysql.inc.php b/interface/lib/classes/db_mysql.inc.php
index 9fbb227863..edd1f7ae96 100644
--- a/interface/lib/classes/db_mysql.inc.php
+++ b/interface/lib/classes/db_mysql.inc.php
@@ -483,7 +483,7 @@ class db extends mysqli
 		$cur_encoding = mb_detect_encoding($sString);
 		if($cur_encoding != "UTF-8") {
 			if($cur_encoding != 'ASCII') {
-				$app->log('String ' . substr($sString, 0, 25) . '... is ' . $cur_encoding . '.', LOGLEVEL_INFO);
+				if(is_object($app) && method_exists($app, 'log')) $app->log('String ' . substr($sString, 0, 25) . '... is ' . $cur_encoding . '.', LOGLEVEL_INFO);
 				if($cur_encoding) $sString = mb_convert_encoding($sString, 'UTF-8', $cur_encoding);
 				else $sString = mb_convert_encoding($sString, 'UTF-8');
 			}
diff --git a/interface/web/dns/dns_soa_edit.php b/interface/web/dns/dns_soa_edit.php
index 634a91d188..d6a20df749 100644
--- a/interface/web/dns/dns_soa_edit.php
+++ b/interface/web/dns/dns_soa_edit.php
@@ -273,16 +273,14 @@ function onBeforeUpdate () {
 
 	//* Check if the server has been changed
 	// We do this only for the admin or reseller users, as normal clients can not change the server ID anyway
-	if($_SESSION["s"]["user"]["typ"] != 'admin' && !$app->auth->has_clients($_SESSION['s']['user']['userid'])) {
+	if($_SESSION["s"]["user"]["typ"] != 'admin' && !$app->auth->has_clients($_SESSION['s']['user']['userid']) && isset($this->dataRecord['origin'])) {
 		//* We do not allow users to change a domain which has been created by the admin
 		$rec = $app->db->queryOneRecord("SELECT origin from dns_soa WHERE id = ?", $this->id);
-		$drOrigin = (isset($this->dataRecord['origin']))
-			? $app->functions->idn_encode($this->dataRecord['origin'])
-			: false;
+		$drOrigin = $app->functions->idn_encode($this->dataRecord['origin']);
 
 		if($rec['origin'] !== $drOrigin && $app->tform->checkPerm($this->id, 'u')) {
 			//* Add a error message and switch back to old server
-			$app->tform->errorMessage .= $app->lng('The Zone (soa) can not be changed. Please ask your Administrator if you want to change the Zone name.');
+			$app->tform->errorMessage .= $app->tform->wordbook["soa_cannot_be_changed_txt"];
 			$this->dataRecord["origin"] = $rec['origin'];
 		}
 		unset($rec);
diff --git a/interface/web/dns/lib/lang/de_dns_soa.lng b/interface/web/dns/lib/lang/de_dns_soa.lng
index 4a017e6fe8..efd6e90551 100644
--- a/interface/web/dns/lib/lang/de_dns_soa.lng
+++ b/interface/web/dns/lib/lang/de_dns_soa.lng
@@ -35,4 +35,5 @@ $wb['expire_range_error'] = 'Min. Expire ist 60 Sekunden.';
 $wb['minimum_range_error'] = 'Min. Minimum ist 60 Sekunden.';
 $wb['ttl_range_error'] = 'Min. TTL ist 60 Sekunden.';
 $wb['xfer_error_regex'] = 'Zonentransfer: Verwenden Sie eine oder mehrere durch Komma getrennte IP Adressen oder das Wort: any.';
+$wb['soa_cannot_be_changed_txt'] = 'Die Zone (SOA) kann nicht verändert werden. Bitte kontaktieren Sie ihren Administrator, um die Zone zu ändern.';
 ?>
diff --git a/interface/web/dns/lib/lang/en_dns_soa.lng b/interface/web/dns/lib/lang/en_dns_soa.lng
index 50ee65b6fd..433530c02d 100644
--- a/interface/web/dns/lib/lang/en_dns_soa.lng
+++ b/interface/web/dns/lib/lang/en_dns_soa.lng
@@ -36,4 +36,5 @@ $wb['expire_range_error'] = 'Min. Expire time is 60 seconds.';
 $wb['minimum_range_error'] = 'Min. Minimum time is 60 seconds.';
 $wb['ttl_range_error'] = 'Min. TTL time is 60 seconds.';
 $wb['error_not_allowed_server_id'] = 'The selected server is not allowed for this account.';
+$wb['soa_cannot_be_changed_txt'] = 'Die Zone (SOA) kann nicht verändert werden. Bitte kontaktieren Sie ihren Administrator, um die Zone zu ändern.';
 ?>
diff --git a/interface/web/mail/form/mail_relay_recipient.tform.php b/interface/web/mail/form/mail_relay_recipient.tform.php
index 210f5194b2..4c5b2b1db1 100644
--- a/interface/web/mail/form/mail_relay_recipient.tform.php
+++ b/interface/web/mail/form/mail_relay_recipient.tform.php
@@ -63,7 +63,7 @@ $form["tabs"]['relay_recipient'] = array (
 			'formtype' => 'SELECT',
 			'default' => '',
 			'datasource' => array (  'type' => 'SQL',
-				'querystring' => 'SELECT server_id,server_name FROM server WHERE mail_server = 1 AND mirror_servr_id = 0 AND {AUTHSQL} ORDER BY server_name',
+				'querystring' => 'SELECT server_id,server_name FROM server WHERE mail_server = 1 AND mirror_server_id = 0 AND {AUTHSQL} ORDER BY server_name',
 				'keyfield'=> 'server_id',
 				'valuefield'=> 'server_name'
 			),
diff --git a/server/lib/classes/db_mysql.inc.php b/server/lib/classes/db_mysql.inc.php
index 2d728f1cf5..f11b8a2dbc 100644
--- a/server/lib/classes/db_mysql.inc.php
+++ b/server/lib/classes/db_mysql.inc.php
@@ -447,7 +447,7 @@ class db extends mysqli
 		$cur_encoding = mb_detect_encoding($sString);
 		if($cur_encoding != "UTF-8") {
 			if($cur_encoding != 'ASCII') {
-				$app->log('String ' . substr($sString, 0, 25) . '... is ' . $cur_encoding . '.', LOGLEVEL_INFO);
+				if(is_object($app) && method_exists($app, 'log')) $app->log('String ' . substr($sString, 0, 25) . '... is ' . $cur_encoding . '.', LOGLEVEL_INFO);
 				if($cur_encoding) $sString = mb_convert_encoding($sString, 'UTF-8', $cur_encoding);
 				else $sString = mb_convert_encoding($sString, 'UTF-8');
 			}
diff --git a/server/plugins-available/apache2_plugin.inc.php b/server/plugins-available/apache2_plugin.inc.php
index 4bacf07e76..6eabdd8112 100644
--- a/server/plugins-available/apache2_plugin.inc.php
+++ b/server/plugins-available/apache2_plugin.inc.php
@@ -1405,7 +1405,7 @@ class apache2_plugin {
 		 * PHP-FPM
 		 */
 		// Support for multiple PHP versions
-		if($data['new']['php'] == 'php-fpm' || $data['new']['php'] == 'hhvm'){
+		if($data['new']['php'] == 'php-fpm'){
 			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']));
@@ -2855,11 +2855,14 @@ class apache2_plugin {
 			file_put_contents('/etc/init.d/hhvm_' . $data['new']['system_user'], $content);
 			exec('chmod +x /etc/init.d/hhvm_' . $data['new']['system_user'] . ' >/dev/null 2>&1');
 			exec('/usr/sbin/update-rc.d hhvm_' . $data['new']['system_user'] . ' defaults >/dev/null 2>&1');
-			exec('/etc/init.d/hhvm_' . $data['new']['system_user'] . ' start >/dev/null 2>&1');
+			exec('/etc/init.d/hhvm_' . $data['new']['system_user'] . ' restart >/dev/null 2>&1');
 			
-			$monit_content = str_replace('{SYSTEM_USER}', $data['new']['system_user'], $monit_content);
-			file_put_contents('/etc/monit/conf.d/hhvm_' . $data['new']['system_user'], $monit_content);
-			exec('/etc/init.d/monit restart >/dev/null 2>&1');
+			if(is_dir('/etc/monit/conf.d')){
+				$monit_content = str_replace('{SYSTEM_USER}', $data['new']['system_user'], $monit_content);
+				file_put_contents('/etc/monit/conf.d/00-hhvm_' . $data['new']['system_user'], $monit_content);
+				if(is_file('/etc/monit/conf.d/hhvm_' . $data['new']['system_user'])) unlink('/etc/monit/conf.d/hhvm_' . $data['new']['system_user']);
+				exec('/etc/init.d/monit restart >/dev/null 2>&1');
+			}
 			
  		} elseif($data['new']['php'] != 'hhvm' && $data['old']['php'] == 'hhvm') {
 			exec('/etc/init.d/hhvm_' . $data['old']['system_user'] . ' stop >/dev/null 2>&1');
@@ -2867,8 +2870,13 @@ class apache2_plugin {
 			unlink('/etc/init.d/hhvm_' . $data['old']['system_user']);
 			if(is_file('/etc/hhvm/'.$data['old']['system_user'].'.ini')) unlink('/etc/hhvm/'.$data['old']['system_user'].'.ini');
 			
-			if(is_file('/etc/monit/conf.d/hhvm_' . $data['new']['system_user'])){
-				unlink('/etc/monit/conf.d/hhvm_' . $data['new']['system_user']);
+			if(is_file('/etc/monit/conf.d/hhvm_' . $data['new']['system_user']) || is_file('/etc/monit/conf.d/00-hhvm_' . $data['new']['system_user'])){
+				if(is_file('/etc/monit/conf.d/hhvm_' . $data['new']['system_user'])){
+					unlink('/etc/monit/conf.d/hhvm_' . $data['new']['system_user']);
+				}
+				if(is_file('/etc/monit/conf.d/00-hhvm_' . $data['new']['system_user'])){
+					unlink('/etc/monit/conf.d/00-hhvm_' . $data['new']['system_user']);
+				}
 				exec('/etc/init.d/monit restart >/dev/null 2>&1');
 			}
 		}
diff --git a/server/plugins-available/nginx_plugin.inc.php b/server/plugins-available/nginx_plugin.inc.php
index 9ce9de83f0..d3eb706408 100644
--- a/server/plugins-available/nginx_plugin.inc.php
+++ b/server/plugins-available/nginx_plugin.inc.php
@@ -950,7 +950,7 @@ class nginx_plugin {
 			$default_php_fpm = true;
 		}
 		*/
-		if($data['new']['php'] != 'no'){
+		if($data['new']['php'] == 'php-fpm'){
 			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']));
@@ -2424,9 +2424,12 @@ class nginx_plugin {
 			exec('/usr/sbin/update-rc.d hhvm_' . $data['new']['system_user'] . ' defaults >/dev/null 2>&1');
 			exec('/etc/init.d/hhvm_' . $data['new']['system_user'] . ' restart >/dev/null 2>&1');
 			
-			$monit_content = str_replace('{SYSTEM_USER}', $data['new']['system_user'], $monit_content);
-			file_put_contents('/etc/monit/conf.d/hhvm_' . $data['new']['system_user'], $monit_content);
-			exec('/etc/init.d/monit restart >/dev/null 2>&1');
+			if(is_dir('/etc/monit/conf.d')){
+				$monit_content = str_replace('{SYSTEM_USER}', $data['new']['system_user'], $monit_content);
+				file_put_contents('/etc/monit/conf.d/00-hhvm_' . $data['new']['system_user'], $monit_content);
+				if(is_file('/etc/monit/conf.d/hhvm_' . $data['new']['system_user'])) unlink('/etc/monit/conf.d/hhvm_' . $data['new']['system_user']);
+				exec('/etc/init.d/monit restart >/dev/null 2>&1');
+			}
 			
  		} elseif($data['new']['php'] != 'hhvm' && $data['old']['php'] == 'hhvm') {
 			exec('/etc/init.d/hhvm_' . $data['old']['system_user'] . ' stop >/dev/null 2>&1');
@@ -2434,8 +2437,13 @@ class nginx_plugin {
 			unlink('/etc/init.d/hhvm_' . $data['old']['system_user']);
 			if(is_file('/etc/hhvm/'.$data['old']['system_user'].'.ini')) unlink('/etc/hhvm/'.$data['old']['system_user'].'.ini');
 			
-			if(is_file('/etc/monit/conf.d/hhvm_' . $data['new']['system_user'])){
-				unlink('/etc/monit/conf.d/hhvm_' . $data['new']['system_user']);
+			if(is_file('/etc/monit/conf.d/hhvm_' . $data['new']['system_user']) || is_file('/etc/monit/conf.d/00-hhvm_' . $data['new']['system_user'])){
+				if(is_file('/etc/monit/conf.d/hhvm_' . $data['new']['system_user'])){
+					unlink('/etc/monit/conf.d/hhvm_' . $data['new']['system_user']);
+				}
+				if(is_file('/etc/monit/conf.d/00-hhvm_' . $data['new']['system_user'])){
+					unlink('/etc/monit/conf.d/00-hhvm_' . $data['new']['system_user']);
+				}
 				exec('/etc/init.d/monit restart >/dev/null 2>&1');
 			}
 		}
@@ -2454,7 +2462,7 @@ class nginx_plugin {
 			$default_php_fpm = true;
 		}
 		*/
-		if($data['new']['php'] != 'no'){
+		if($data['new']['php'] == 'php-fpm'){
 			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']));
@@ -2475,7 +2483,7 @@ class nginx_plugin {
 		$app->uses("getconf");
 		$web_config = $app->getconf->get_server_config($conf["server_id"], 'web');
 
-		if($data['new']['php'] == 'no'){
+		if($data['new']['php'] != 'php-fpm'){
 			if(@is_file($pool_dir.$pool_name.'.conf')){
 				$app->system->unlink($pool_dir.$pool_name.'.conf');
 				//$reload = true;
diff --git a/server/plugins-available/shelluser_base_plugin.inc.php b/server/plugins-available/shelluser_base_plugin.inc.php
index d66428b781..8a51b1071a 100755
--- a/server/plugins-available/shelluser_base_plugin.inc.php
+++ b/server/plugins-available/shelluser_base_plugin.inc.php
@@ -149,6 +149,12 @@ class shelluser_base_plugin {
 				$app->system->chown(escapeshellcmd($homedir).'/.bash_history', $data['new']['username']);
 				$app->system->chgrp(escapeshellcmd($homedir).'/.bash_history', $data['new']['pgroup']);
 
+				//* Create .profile file
+				$app->system->touch(escapeshellcmd($homedir).'/.profile');
+				$app->system->chmod(escapeshellcmd($homedir).'/.profile', 0644);
+				$app->system->chown(escapeshellcmd($homedir).'/.profile', $data['new']['username']);
+				$app->system->chgrp(escapeshellcmd($homedir).'/.profile', $data['new']['pgroup']);
+
 				//* Disable shell user temporarily if we use jailkit
 				if($data['new']['chroot'] == 'jailkit') {
 					$command = 'usermod -s /bin/false -L '.escapeshellcmd($data['new']['username']).' 2>/dev/null';
@@ -214,6 +220,9 @@ class shelluser_base_plugin {
 				
 				// Check if the user that we want to update exists, if not, we insert it
 				if($app->system->is_user($data['old']['username'])) {
+					//* Remove webfolder protection
+					$app->system->web_folder_protection($web['document_root'], false);
+					
 					/*
 					$command = 'usermod';
 					$command .= ' --home '.escapeshellcmd($data['new']['dir']);
@@ -270,7 +279,17 @@ class shelluser_base_plugin {
 						$app->system->chown(escapeshellcmd($homedir).'/.bash_history', escapeshellcmd($data['new']['username']));
 						$app->system->chgrp(escapeshellcmd($homedir).'/.bash_history', escapeshellcmd($data['new']['pgroup']));
 					}
+					
+					//* Create .profile file
+					if(!is_file($data['new']['dir']).'/.profile') {
+						$app->system->touch(escapeshellcmd($homedir).'/.profile');
+						$app->system->chmod(escapeshellcmd($homedir).'/.profile', 0644);
+						$app->system->chown(escapeshellcmd($homedir).'/.profile', escapeshellcmd($data['new']['username']));
+						$app->system->chgrp(escapeshellcmd($homedir).'/.profile', escapeshellcmd($data['new']['pgroup']));
+					}
 
+					//* Add webfolder protection again
+					$app->system->web_folder_protection($web['document_root'], true);
 				} else {
 					// The user does not exist, so we insert it now
 					$this->insert($event_name, $data);
-- 
GitLab