From 7fa98b3e021bbb7f8df2aa44c4201183b3aaaeab Mon Sep 17 00:00:00 2001
From: Till Brehm <tbrehm@ispconfig.org>
Date: Thu, 26 Oct 2017 14:44:22 +0200
Subject: [PATCH] Fixed: #4829 Updater ignores MySQL Masterserver Port

---
 install/install.php       |  2 +-
 install/lib/mysql.lib.php | 13 ++++++++-----
 install/update.php        |  3 ++-
 3 files changed, 11 insertions(+), 7 deletions(-)

diff --git a/install/install.php b/install/install.php
index b9d2f0641e..1b55a3dae6 100644
--- a/install/install.php
+++ b/install/install.php
@@ -282,7 +282,7 @@ if($install_mode == 'expert' && strtolower($inst->simple_query('Shall this serve
 	// initialize the connection to the master database
 	$inst->dbmaster = new db();
 	if($inst->dbmaster->linkId) $inst->dbmaster->closeConn();
-	$inst->dbmaster->setDBData($conf['mysql']["master_host"], $conf['mysql']["master_admin_user"], $conf['mysql']["master_admin_password"]);
+	$inst->dbmaster->setDBData($conf['mysql']["master_host"], $conf['mysql']["master_admin_user"], $conf['mysql']["master_admin_password"], $conf['mysql']['master_port']);
 	$inst->dbmaster->setDBName($conf['mysql']["master_database"]);
 
 } else {
diff --git a/install/lib/mysql.lib.php b/install/lib/mysql.lib.php
index e3c12a6d3b..bca8ac0f75 100644
--- a/install/lib/mysql.lib.php
+++ b/install/lib/mysql.lib.php
@@ -36,6 +36,7 @@ class db
 	private $_iConnId;
 
 	private $dbHost = '';  // hostname of the MySQL server
+	private $dbPort = '';  // port of the MySQL server
 	private $dbName = '';  // logical database name on that server
 	private $dbUser = '';  // database authorized user
 	private $dbPass = '';  // user's password
@@ -68,7 +69,8 @@ class db
 		global $conf;
 		
 		if($this->_iConnId) return true;
-		$this->dbHost = $conf["mysql"]["host"];
+		$this->dbHost = $conf['mysql']['port'];
+		$this->dbPort = $conf[$prefix.'db_port'];
 		$this->dbName = false;//$conf["mysql"]["database"];
 		$this->dbUser = $conf["mysql"]["admin_user"];
 		$this->dbPass = $conf["mysql"]["admin_password"];
@@ -76,13 +78,13 @@ class db
 		$this->dbNewLink = false;
 		$this->dbClientFlags = null;
 		
-		$this->_iConnId = mysqli_connect($this->dbHost, $this->dbUser, $this->dbPass);
+		$this->_iConnId = mysqli_connect($this->dbHost, $this->dbUser, $this->dbPass, (int)$this->dbPort);
 		$try = 0;
 		while((!is_object($this->_iConnId) || mysqli_connect_error()) && $try < 5) {
 			if($try > 0) sleep(1);
 
 			$try++;
-			$this->_iConnId = mysqli_connect($this->dbHost, $this->dbUser, $this->dbPass);
+			$this->_iConnId = mysqli_connect($this->dbHost, $this->dbUser, $this->dbPass, (int)$this->dbPort);
 		}
 
 		if(!is_object($this->_iConnId) || mysqli_connect_error()) {
@@ -96,10 +98,11 @@ class db
 		$this->_setCharset();
 	}
 	
-	public function setDBData($host, $user, $password) {
+	public function setDBData($host, $user, $password, $port) {
 		$this->dbHost = $host;
 		$this->dbUser = $user;
 		$this->dbPass = $password;
+		$this->dbPort = $port;
 	}
 	
 	public function setDBName($name) {
@@ -201,7 +204,7 @@ class db
 			$try++;
 			$ok = mysqli_ping($this->_iConnId);
 			if(!$ok) {
-				if(!mysqli_connect($this->dbHost, $this->dbUser, $this->dbPass, $this->dbName)) {
+				if(!mysqli_connect($this->dbHost, $this->dbUser, $this->dbPass, $this->dbName, (int)$this->dbPort)) {
 					if($this->errorNumber == '111') {
 						// server is not available
 						if($try > 9) {
diff --git a/install/update.php b/install/update.php
index 230fa4db80..cae871128b 100644
--- a/install/update.php
+++ b/install/update.php
@@ -162,6 +162,7 @@ if($conf['timezone'] == '{timezone}' or trim($conf['timezone']) == '') $conf['ti
 $conf['language_file_import_enabled'] = (isset($conf_old['language_file_import_enabled']))?$conf_old['language_file_import_enabled']:true;
 
 if(isset($conf_old["dbmaster_host"])) $conf["mysql"]["master_host"] = $conf_old["dbmaster_host"];
+if(isset($conf_old["dbmaster_port"])) $conf["mysql"]["master_host"] = $conf_old["dbmaster_port"];
 if(isset($conf_old["dbmaster_database"])) $conf["mysql"]["master_database"] = $conf_old["dbmaster_database"];
 if(isset($conf_old["dbmaster_user"])) $conf["mysql"]["master_ispconfig_user"] = $conf_old["dbmaster_user"];
 if(isset($conf_old["dbmaster_password"])) $conf["mysql"]["master_ispconfig_password"] = $conf_old["dbmaster_password"];
@@ -275,7 +276,7 @@ if($conf['mysql']['master_slave_setup'] == 'y') {
 	// initialize the connection to the master database
 	$inst->dbmaster = new db();
 	if($inst->dbmaster->linkId) $inst->dbmaster->closeConn();
-	$inst->dbmaster->setDBData($conf['mysql']["master_host"], $conf['mysql']["master_admin_user"], $conf['mysql']["master_admin_password"]);
+	$inst->dbmaster->setDBData($conf['mysql']["master_host"], $conf['mysql']["master_admin_user"], $conf['mysql']["master_admin_password"], $conf['mysql']["master_port"]);
 	$inst->dbmaster->setDBName($conf['mysql']["master_database"]);
 } else {
 	$inst->dbmaster = $inst->db;
-- 
GitLab