From b9b7bb83724c2cd21263280c6eb84c657973da9d Mon Sep 17 00:00:00 2001
From: prasad <prasad@vtiger.com>
Date: Mon, 5 Mar 2018 11:08:27 +0530
Subject: [PATCH] Fixes #841: Added install check for sql_mode to avoid runtime
 surprises

---
 languages/en_us/Install.php      |  1 +
 modules/Install/models/Utils.php | 25 ++++++++++++++++++++++++-
 2 files changed, 25 insertions(+), 1 deletion(-)

diff --git a/languages/en_us/Install.php b/languages/en_us/Install.php
index 7ddd32939..9f98636d6 100644
--- a/languages/en_us/Install.php
+++ b/languages/en_us/Install.php
@@ -15,6 +15,7 @@ $languageStrings = array(
 	'ERR_INVALID_MYSQL_PARAMETERS' => 'Invalid mySQL Connection Parameters specified',
 	'ERR_INVALID_MYSQL_VERSION' => 'MySQL version is not supported, kindly connect to MySQL 5.1.x or above',
 	'ERR_UNABLE_CREATE_DATABASE' => 'Unable to Create database',
+	'ERR_DB_SQLMODE_NOTFRIENDLY' => 'MySQL Server should be configured with:<br> sql_mode = ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION',
 	'LBL_ADMIN_INFORMATION'=>'Admin User Information',
 	'LBL_ADMIN_USER_INFORMATION' => 'Admin User Information',
 	'LBL_CHOOSE_LANGUAGE' => 'Choose the default language for this installation:',
diff --git a/modules/Install/models/Utils.php b/modules/Install/models/Utils.php
index f9c1995cc..11a00c02c 100644
--- a/modules/Install/models/Utils.php
+++ b/modules/Install/models/Utils.php
@@ -330,6 +330,25 @@ class Install_Utils_Model {
 		return $mysql_server_version;
 	}
 
+	/**
+	 * Function to check sql_mode configuration
+	 * @param DbConnection $conn 
+	 * @return boolean
+	 */
+	public static function isMySQLSqlModeFriendly($conn) {
+		$rs = $conn->Execute("SHOW VARIABLES LIKE 'sql_mode'");
+		if ($rs && ($row = $rs->fetchRow())) {
+			$values = explode(',', strtoupper($row['Value']));
+			$unsupported = array('ONLY_FULL_GROUP_BY', 'STRICT_TRANS_TABLES', 'NO_ZERO_IN_DATE', 'NO_ZERO_DATE');
+			foreach ($unsupported as $check) {
+				if (in_array($check, $values)) {
+					return false;
+				}
+			}
+		}
+		return true;
+	}
+
 	/**
 	 * Function checks the database connection
 	 * @param <String> $db_type
@@ -351,6 +370,7 @@ class Install_Utils_Model {
 		$db_creation_failed = false; // did we try to create a database and fail?
 		$db_exist_status = false; // does the database exist?
 		$db_utf8_support = false; // does the database support utf8?
+		$db_sqlmode_support = false; // does the database having friendly sql_mode?
 
 		//Checking for database connection parameters
 		if($db_type) {
@@ -362,7 +382,8 @@ class Install_Utils_Model {
 				if(self::isMySQL($db_type)) {
 					$mysql_server_version = self::getMySQLVersion($serverInfo);
 				}
-				if($create_db) {
+				$db_sqlmode_support = self::isMySQLSqlModeFriendly($conn);
+				if($create_db && $db_sqlmode_support) {
 					// drop the current database if it exists
 					$dropdb_conn = NewADOConnection($db_type);
 					if(@$dropdb_conn->Connect($db_hostname, $root_user, $root_password, $db_name)) {
@@ -409,6 +430,8 @@ class Install_Utils_Model {
 					-  '.getTranslatedString('MSG_DB_USER_NOT_AUTHORIZED', 'Install');
 		} elseif(self::isMySQL($db_type) && $mysql_server_version < 4.1) {
 			$error_msg = $mysql_server_version.' -> '.getTranslatedString('ERR_INVALID_MYSQL_VERSION', 'Install');
+		} elseif(!$db_sqlmode_support) {
+			$error_msg = getTranslatedString('ERR_DB_SQLMODE_NOTFRIENDLY', 'Install');
 		} elseif($db_creation_failed) {
 			$error_msg = getTranslatedString('ERR_UNABLE_CREATE_DATABASE', 'Install').' '.$db_name;
 			$error_msg_info = getTranslatedString('MSG_DB_ROOT_USER_NOT_AUTHORIZED', 'Install');
-- 
GitLab