From ebd0e986ed11f2a34fb58cdd33efbfab192083ad Mon Sep 17 00:00:00 2001
From: Till Brehm <tbrehm@ispconfig.org>
Date: Fri, 22 Apr 2016 05:26:17 -0400
Subject: [PATCH] Added PHP 7 check in installer and updater.

---
 interface/lib/classes/tform.inc.php |   93 ++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 85 insertions(+), 8 deletions(-)

diff --git a/interface/lib/classes/tform.inc.php b/interface/lib/classes/tform.inc.php
index 03a45f1..1722a77 100644
--- a/interface/lib/classes/tform.inc.php
+++ b/interface/lib/classes/tform.inc.php
@@ -146,7 +146,7 @@
 		}
 
 		if(is_array($wb_global)) {
-			$wb = array_merge($wb_global, $wb);
+			$wb = $app->functions->array_merge($wb_global, $wb);
 		}
 		if(isset($wb_global)) unset($wb_global);
 
@@ -156,7 +156,6 @@
 
 		return true;
 	}
-
 
 	/**
 	 * Converts the data in the array to human readable format
@@ -252,7 +251,9 @@
 			unset($tmp_recordid);
 
 			$querystring = str_replace("{AUTHSQL}", $this->getAuthSQL('r'), $querystring);
-			$querystring = preg_replace_callback('@{AUTHSQL::(.+?)}@', "self::table_auth_sql", $querystring);
+			//$querystring = preg_replace_callback('@{AUTHSQL::(.+?)}@', "self::table_auth_sql", $querystring);
+			//*Used the ld form to be compatible with php < 5.3
+			$querystring = preg_replace_callback('@{AUTHSQL::(.+?)}@', create_function('$matches','global $app; $tmp = $app->tform->getAuthSQL("r", $matches[1]); return $tmp;'), $querystring);
 
 			// Getting the records
 			$tmp_records = $app->db->queryAllRecords($querystring);
@@ -293,10 +294,12 @@
 		return $values;
 
 	}
-
+	
+	/*
 	function table_auth_sql($matches){
 		return $this->getAuthSQL('r', $matches[1]);
 	}
+	*/
 
 	//* If the parameter 'valuelimit' is set
 	function applyValueLimit($limit, $values) {
@@ -380,7 +383,36 @@
 
 		if(!is_array($this->formDef)) $app->error("No form definition found.");
 		if(!is_array($this->formDef['tabs'][$tab])) $app->error("The tab is empty or does not exist (TAB: $tab).");
-
+		
+		/* CSRF PROTECTION */
+		// generate csrf protection id and key
+		/*$_csrf_id = uniqid($this->formDef['name'] . '_');
+		$_csrf_value = sha1(uniqid(microtime(true), true));
+		if(!isset($_SESSION['_csrf'])) $_SESSION['_csrf'] = array();
+		if(!isset($_SESSION['_csrf_timeout'])) $_SESSION['_csrf_timeout'] = array();
+		$_SESSION['_csrf'][$_csrf_id] = $_csrf_value;
+		$_SESSION['_csrf_timeout'][$_csrf_id] = time() + 3600; // timeout hash in 1 hour
+		*/
+		$csrf_token = $app->auth->csrf_token_get($this->formDef['name']);
+		$_csrf_id = $csrf_token['csrf_id'];
+		$_csrf_value = $csrf_token['csrf_key'];
+		
+		$this->formDef['tabs'][$tab]['fields']['_csrf_id'] = array(
+			'datatype' => 'VARCHAR',
+			'formtype' => 'TEXT',
+			'default' => $_csrf_id,
+			'value' => $_csrf_id
+		);
+		$this->formDef['tabs'][$tab]['fields']['_csrf_key'] = array(
+			'datatype' => 'VARCHAR',
+			'formtype' => 'TEXT',
+			'default' => $_csrf_value,
+			'value' => $_csrf_value
+		);
+		$record['_csrf_id'] = $_csrf_id;
+		$record['_csrf_key'] = $_csrf_value;
+		/* CSRF PROTECTION */
+		
 		$new_record = array();
 		if($action == 'EDIT') {
 			$record = $this->decode($record, $tab);
@@ -640,7 +672,43 @@
 
 		if(!is_array($this->formDef['tabs'][$tab])) $app->error("Tab is empty or does not exist (TAB: $tab).");
 		//$this->errorMessage = '';
-
+		
+		/* CSRF PROTECTION */
+		
+		if(isset($_POST) && is_array($_POST)) {
+			$_csrf_valid = false;
+			if(isset($_POST['_csrf_id']) && isset($_POST['_csrf_key'])) {
+				$_csrf_id = trim($_POST['_csrf_id']);
+				$_csrf_key = trim($_POST['_csrf_key']);
+				if(isset($_SESSION['_csrf']) && isset($_SESSION['_csrf'][$_csrf_id]) && isset($_SESSION['_csrf_timeout']) && isset($_SESSION['_csrf_timeout'][$_csrf_id])) {
+					if($_SESSION['_csrf'][$_csrf_id] === $_csrf_key && $_SESSION['_csrf_timeout'] >= time()) $_csrf_valid = true;
+				}
+			}
+			if($_csrf_valid !== true) {
+				$app->log('CSRF attempt blocked. Referer: ' . (isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : 'unknown'), LOGLEVEL_WARN);
+				$errmsg = 'err_csrf_attempt_blocked';
+				$this->errorMessage .= ($api == true ? $errmsg : $this->wordbook[$errmsg]."<br />") . "\r\n";
+				unset($_POST);
+				unset($record);
+			}
+			
+			if(isset($_SESSION['_csrf_timeout']) && is_array($_SESSION['_csrf_timeout'])) {
+				$to_unset = array();
+				foreach($_SESSION['_csrf_timeout'] as $_csrf_id => $timeout) {
+					if($timeout < time()) $to_unset[] = $_csrf_id;
+				}
+				foreach($to_unset as $_csrf_id) {
+					$_SESSION['_csrf'][$_csrf_id] = null;
+					$_SESSION['_csrf_timeout'][$_csrf_id] = null;
+					unset($_SESSION['_csrf'][$_csrf_id]);
+					unset($_SESSION['_csrf_timeout'][$_csrf_id]);
+				}
+				unset($to_unset);
+			}
+		}
+		/* CSRF PROTECTION */
+		
+		$new_record = array();
 		if(is_array($record)) {
 			foreach($this->formDef['tabs'][$tab]['fields'] as $key => $field) {
 
@@ -845,6 +913,15 @@
 					}
 				}
 				break;
+			case 'ISASCII':
+				if(preg_match("/[^\x20-\x7F]/", $field_value)) {
+					$errmsg = $validator['errmsg'];
+					if(isset($this->wordbook[$errmsg])) {
+						$this->errorMessage .= $this->wordbook[$errmsg]."<br />\r\n";
+					} else {
+						$this->errorMessage .= $errmsg."<br />\r\n";
+					}
+				}
 			case 'ISEMAIL':
 				if(function_exists('filter_var')) {
 					if(filter_var($field_value, FILTER_VALIDATE_EMAIL) === false) {
@@ -1293,7 +1370,7 @@
 		$perm = $app->db->quote($perm);
 		$table = $app->db->quote($table);
 		
-		if($_SESSION["s"]["user"]["typ"] == 'admin') {
+		if($_SESSION["s"]["user"]["typ"] == 'admin' || $_SESSION['s']['user']['mailuser_id'] > 0) {
 			return '1';
 		} else {
 			if ($table != ''){
@@ -1500,7 +1577,7 @@
 	 */
 	function _getDateTimeHTML($form_element, $default_value, $display_seconds=false)
 	{
-		$_datetime = strtotime($default_value);
+		$_datetime = ($default_value && $default_value != '0000-00-00 00:00:00' ? strtotime($default_value) : false);
 		$_showdate = ($_datetime === false) ? false : true;
 
 		$dselect = array('day', 'month', 'year', 'hour', 'minute');

--
Gitblit v1.9.1