From 0e8a6d59611b719f85094a92b1f696ec93fda845 Mon Sep 17 00:00:00 2001 From: Jesse Norell Date: Wed, 15 Aug 2018 17:17:53 -0600 Subject: [PATCH] fix db connection test for server.php --- interface/lib/classes/db_mysql.inc.php | 32 ++++++++++++++++++-------- server/lib/classes/db_mysql.inc.php | 32 ++++++++++++++++++-------- server/server.php | 6 ++--- 3 files changed, 47 insertions(+), 23 deletions(-) diff --git a/interface/lib/classes/db_mysql.inc.php b/interface/lib/classes/db_mysql.inc.php index 6384882ec..b0ae61ef3 100644 --- a/interface/lib/classes/db_mysql.inc.php +++ b/interface/lib/classes/db_mysql.inc.php @@ -52,8 +52,7 @@ class db private $dbClientFlags = 0; // MySQL Client falgs /**#@-*/ - public $show_error_messages = false; // false in server, true in interface - + public $show_error_messages = false; // false in server, interface sets true when generating templates /* old things - unused now //// private $linkId = 0; // last result of mysqli_connect() @@ -80,7 +79,7 @@ class db $this->dbUser = $user ? $user : $conf['db_user']; $this->dbPass = $pass ? $pass : $conf['db_password']; $this->dbCharset = $conf['db_charset']; - $this->dbClientFlags = $flags ? $flags : $conf['db_client_flags']; + $this->dbClientFlags = ($flags !== NULL) ? $flags : $conf['db_client_flags']; $this->_iConnId = mysqli_init(); mysqli_real_connect($this->_iConnId, $this->dbHost, $this->dbUser, $this->dbPass, '', (int)$this->dbPort, NULL, $this->dbClientFlags); @@ -117,6 +116,18 @@ class db $this->_iConnId = null; } + /* + * Test mysql connection. + * + * @return boolean returns true if db connection is good. + */ + public function testConnection() { + if(mysqli_connect_errno()) { + return false; + } + return (boolean)(is_object($this->_iConnId) && mysqli_ping($this->_iConnId)); + } + /* This allows our private variables to be "read" out side of the class */ public function __get($var) { return isset($this->$var) ? $this->$var : NULL; @@ -435,13 +446,14 @@ class db * @return int id of last inserted row or 0 if none */ public function insert_id() { - $iRes = mysqli_query($this->_iConnId, 'SELECT LAST_INSERT_ID() as `newid`'); - if(!is_object($iRes)) return false; - - $aReturn = mysqli_fetch_assoc($iRes); - mysqli_free_result($iRes); - - return $aReturn['newid']; + $oResult = $this->query('SELECT LAST_INSERT_ID() as `newid`'); + if(!$oResult) { + $this->_sqlerror('Unable to select last_insert_id()'); + return false; + } + $aReturn = $oResult->get(); + $oResult->free(); + return isset($aReturn['newid']) ? $aReturn['newid'] : 0; } diff --git a/server/lib/classes/db_mysql.inc.php b/server/lib/classes/db_mysql.inc.php index 6384882ec..b0ae61ef3 100644 --- a/server/lib/classes/db_mysql.inc.php +++ b/server/lib/classes/db_mysql.inc.php @@ -52,8 +52,7 @@ class db private $dbClientFlags = 0; // MySQL Client falgs /**#@-*/ - public $show_error_messages = false; // false in server, true in interface - + public $show_error_messages = false; // false in server, interface sets true when generating templates /* old things - unused now //// private $linkId = 0; // last result of mysqli_connect() @@ -80,7 +79,7 @@ class db $this->dbUser = $user ? $user : $conf['db_user']; $this->dbPass = $pass ? $pass : $conf['db_password']; $this->dbCharset = $conf['db_charset']; - $this->dbClientFlags = $flags ? $flags : $conf['db_client_flags']; + $this->dbClientFlags = ($flags !== NULL) ? $flags : $conf['db_client_flags']; $this->_iConnId = mysqli_init(); mysqli_real_connect($this->_iConnId, $this->dbHost, $this->dbUser, $this->dbPass, '', (int)$this->dbPort, NULL, $this->dbClientFlags); @@ -117,6 +116,18 @@ class db $this->_iConnId = null; } + /* + * Test mysql connection. + * + * @return boolean returns true if db connection is good. + */ + public function testConnection() { + if(mysqli_connect_errno()) { + return false; + } + return (boolean)(is_object($this->_iConnId) && mysqli_ping($this->_iConnId)); + } + /* This allows our private variables to be "read" out side of the class */ public function __get($var) { return isset($this->$var) ? $this->$var : NULL; @@ -435,13 +446,14 @@ class db * @return int id of last inserted row or 0 if none */ public function insert_id() { - $iRes = mysqli_query($this->_iConnId, 'SELECT LAST_INSERT_ID() as `newid`'); - if(!is_object($iRes)) return false; - - $aReturn = mysqli_fetch_assoc($iRes); - mysqli_free_result($iRes); - - return $aReturn['newid']; + $oResult = $this->query('SELECT LAST_INSERT_ID() as `newid`'); + if(!$oResult) { + $this->_sqlerror('Unable to select last_insert_id()'); + return false; + } + $aReturn = $oResult->get(); + $oResult->free(); + return isset($aReturn['newid']) ? $aReturn['newid'] : 0; } diff --git a/server/server.php b/server/server.php index 689cb1749..106d3edc6 100644 --- a/server/server.php +++ b/server/server.php @@ -61,7 +61,7 @@ $conf['server_id'] = intval($conf['server_id']); /* * Try to Load the server configuration from the master-db */ -if ($app->dbmaster->connect_error == NULL) { +if ($app->dbmaster->testConnection()) { $server_db_record = $app->dbmaster->queryOneRecord("SELECT * FROM server WHERE server_id = ?", $conf['server_id']); if(!is_array($server_db_record)) die('Unable to load the server configuration from database.'); @@ -152,7 +152,7 @@ $needStartCore = true; /* * Next we try to process the datalog */ -if ($app->db->connect_error == NULL && $app->dbmaster->connect_error == NULL) { +if ($app->db->testConnection() && $app->dbmaster->testConnection()) { // Check if there is anything to update if ($conf['mirror_server_id'] > 0) { @@ -187,7 +187,7 @@ if ($app->db->connect_error == NULL && $app->dbmaster->connect_error == NULL) { $needStartCore = false; } else { - if ($app->db->connect->connect_error == NULL) { + if (!$app->db->connect->testConnection()) { $app->log('Unable to connect to local server.' . $app->db->errorMessage, LOGLEVEL_WARN); } else { $app->log('Unable to connect to master server.' . $app->dbmaster->errorMessage, LOGLEVEL_WARN); -- GitLab