New release with enhanced connection error handling.
authorChuck Scott <cscott@gaslightmedia.com>
Wed, 22 Feb 2017 19:37:47 +0000 (14:37 -0500)
committerChuck Scott <cscott@gaslightmedia.com>
Wed, 22 Feb 2017 19:37:47 +0000 (14:37 -0500)
classes/serverBandwidthSupport.php
index.php
models/adminServerStats.php
views/adminServerStats.html

index 560c55b..4bcfee4 100644 (file)
@@ -62,11 +62,17 @@ class glmServerStatsBandwidthSupport
     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 glmServerStatsBandwidthSupport
         $this->bandwidthGetWebsiteID($website);
 
         if (!$this->siteId) {
-            $this->bwdb = false;
             return "Website Name not found: ".$website;
         }
 
index 34363a9..3431ce7 100644 (file)
--- a/index.php
+++ b/index.php
@@ -3,7 +3,7 @@
  * Plugin Name: GLM ServerStats - Stand Alone
  * Plugin URI: http://www.gaslightmedia.com/
  * Description: Gaslight Media Server Stats Display.
- * Version: 1.0.10
+ * Version: 1.1.0
  * Author: Gaslight Media
  * Author URI: http://www.gaslightmedia.com/
  * License: GPL2
  * @package glmServerStats
  * @author Chuck Scott <cscott@gaslightmedia.com>
  * @license http://www.gaslightmedia.com Gaslightmedia
- * @version 1.0.10
+ * @version 1.1.0
  */
 
-define('GLM_SERVERSTATS_PLUGIN_VERSION', '1.0.10');
+define('GLM_SERVERSTATS_PLUGIN_VERSION', '1.1.0');
 
 // Check that we're being called by WordPress.
 if (!defined('ABSPATH')) {
index 4aa71e4..1c69557 100644 (file)
@@ -50,6 +50,7 @@ class adminServerStats extends glmServerStatsBandwidthSupport
     {
 
         $haveStats     = false;
+        $connectError  = '';
         $thisDate      = false;
         $thisDateTime  = false;
         $thisMonth     = false;
@@ -62,7 +63,7 @@ class adminServerStats extends glmServerStatsBandwidthSupport
         if ($config != false) {
 
             // Connect to the bandwidth database
-            $this->bandwidthDataConnect(
+            $err = $this->bandwidthDataConnect(
                 $config['db_host'],
                 $config['db_user'],
                 $config['db_pass'],
@@ -70,27 +71,34 @@ class adminServerStats extends glmServerStatsBandwidthSupport
                 $config['website']
             );
 
-            // Get the current bandwidth stats
-            $stats = $this->bandwidthGetStats();
+            // Check for connection error
+            if ($err != '') {
+                $connectError = $err;
+            } else {
 
-            if ($stats != false) {
+                // Get the current bandwidth stats
+                $stats = $this->bandwidthGetStats();
 
-                // Build summary info
-                $thisDate = date('m/d/Y');
-                $thisDateTime = strtotime($thisDate);
-                $thisMonth = date('m/Y');
-                $thisMonthTime = strtotime(date('m/01/Y'));
-                $websiteId = $this->siteId;
+                if ($stats != false) {
 
-                $haveStats = true;
+                    // Build summary info
+                    $thisDate = date('m/d/Y');
+                    $thisDateTime = strtotime($thisDate);
+                    $thisMonth = date('m/Y');
+                    $thisMonthTime = strtotime(date('m/01/Y'));
+                    $websiteId = $this->siteId;
 
-            }
+                    $haveStats = true;
+
+                }
 
+            }
         }
 
         // Compile template data
         $templateData = array(
             'haveStats' => $haveStats,
+            'connectError' => $connectError,
             'serverStats' => $stats,
             'thisDate' => $thisDate,
             'thisDateTime' => $thisDateTime,
index 5c31a0a..e1e3a01 100644 (file)
 </script>
 {else}
     <h3 class="glm-error">Server Bandwidth has not been configured. Please ask system administrator to configure this plugin.</h3>
+    <p><b>Error:</b> {$connectError}</p>
 {/if}
 
 {include file='footer.html'}