From d7ba492d5798c99472a6869e95ed48f4f64446eb Mon Sep 17 00:00:00 2001 From: Chuck Scott Date: Wed, 22 Feb 2017 14:21:52 -0500 Subject: [PATCH] Added connection timeout at 5 seconds Improved error handling --- classes/serverBandwidthSupport.php | 13 +++++++++---- models/admin/dashboardWidget/serverStats.php | 16 ++++++++++++---- views/admin/dashboardWidget/serverStats.html | 5 +++-- 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/classes/serverBandwidthSupport.php b/classes/serverBandwidthSupport.php index 3b7c8fd..1f18a58 100644 --- a/classes/serverBandwidthSupport.php +++ b/classes/serverBandwidthSupport.php @@ -62,11 +62,17 @@ class GlmServerBandwidthSupport public function bandwidthDataConnect($host, $user, $pass, $database, $website) { + // Initialize MySQLi + $this->bwdb = mysqli_init(); + + // Set a short timeout + $this->bwdb->options(MYSQLI_OPT_CONNECT_TIMEOUT, 5); + // Connect to MySQL - $this->bwdb = mysqli_connect($host, $user, $pass, $database); + $this->bwdb->real_connect($host, $user, $pass, $database); - if (!$this->bwdb) { - $this->bwdb = false; + // If there was an error connecting + if ($this->bwdb->connect_errno) { return mysqli_connect_error(); } @@ -74,7 +80,6 @@ class GlmServerBandwidthSupport $this->bandwidthGetWebsiteID($website); if (!$this->siteId) { - $this->bwdb = false; return "Website Name not found: ".$website; } diff --git a/models/admin/dashboardWidget/serverStats.php b/models/admin/dashboardWidget/serverStats.php index d2580b9..daaf1aa 100644 --- a/models/admin/dashboardWidget/serverStats.php +++ b/models/admin/dashboardWidget/serverStats.php @@ -41,6 +41,13 @@ class GlmMembersAdmin_dashboardWidget_serverStats extends GlmServerBandwidthSupp * @access public */ public $dbError = ''; + /** + * Stats Results + * + * @var $haveStats + * @access public + */ + public $stats = false; /** * Server Stats Database Connection Enabled * @@ -100,7 +107,7 @@ class GlmMembersAdmin_dashboardWidget_serverStats extends GlmServerBandwidthSupp } // Connect to server stats database - $this->bandwidthDataConnect( + $this->dbError = $this->bandwidthDataConnect( $dbData['db_host'], $dbData['db_user'], $dbData['db_pass'], @@ -121,10 +128,10 @@ class GlmMembersAdmin_dashboardWidget_serverStats extends GlmServerBandwidthSupp $stats = false; // Check for good database connection - if ($this->bwdb != false) { + if ($this->dbError == '') { // Get current summary stats - $stats = $this->bandwidthGetStats(); + $this->stats = $this->bandwidthGetStats(); // Build summary info $thisDate = date('m/d/Y'); @@ -137,8 +144,9 @@ class GlmMembersAdmin_dashboardWidget_serverStats extends GlmServerBandwidthSupp // Compile template data $templateData = array( + 'dbError' => $this->dbError, 'serverStatsEnabled' => $this->enabled, - 'serverStats' => $stats, + 'serverStats' => $this->stats, 'thisDate' => $thisDate, 'thisDateTime' => $thisDateTime, 'thisMonth' => $thisMonth, diff --git a/views/admin/dashboardWidget/serverStats.html b/views/admin/dashboardWidget/serverStats.html index 6caaca3..0c28862 100644 --- a/views/admin/dashboardWidget/serverStats.html +++ b/views/admin/dashboardWidget/serverStats.html @@ -128,7 +128,8 @@ }); {else} -

NOTE: Unable to connect to server stats database!

-Check Server Stats Management setting
+

+ NOTE: Unable to connect to server Bandwidth Statistics database!
{$dbError} +

{/if} {/if} \ No newline at end of file -- 2.17.1