diff --git a/src/bootstrap.php b/src/bootstrap.php
index 952b695b..540c3526 100644
--- a/src/bootstrap.php
+++ b/src/bootstrap.php
@@ -88,14 +88,36 @@ foreach($includes as $file) {
// init db connection
$db = new psm\Service\Database();
-if($db->status() && (!defined('PSM_INSTALL') || !PSM_INSTALL)) {
- psm_load_conf();
+// sanity check!
+if(defined('PSM_INSTALL') && PSM_INSTALL) {
+ // install mode
+ if($db->status()) {
+ // connection established, attempt to load config.
+ // no biggie if it doesnt work because the user is still in the install module.
+ psm_load_conf();
+ }
} else {
- // no config yet! lets help them in the right direction
- if(!defined('PSM_INSTALL')) {
+ if($db->getDbHost() === null) {
+ // no config file has been loaded, redirect the user to the install
header('Location: install.php');
die();
}
+ // config file has been loaded, check if we have a connection
+ if(!$db->status()) {
+ die('Unable to establish database connection...');
+ }
+ // attempt to load configuration from database
+ if(!psm_load_conf()) {
+ // unable to load from config table
+ die('We were unable to find an existing installation. Please click here to install PHP Server Monitor.');
+ }
+ // config load OK, make sure database version is up to date
+ $version_db = psm_get_conf('version');
+
+ if(version_compare(PSM_VERSION, $version_db, '>')) {
+ die('Your database is for an older version, please click here to update your database to the latest version.');
+ }
}
+
$lang = psm_get_conf('language', 'en_US');
-psm_load_lang($lang);
+psm_load_lang($lang);
\ No newline at end of file
diff --git a/src/includes/functions.inc.php b/src/includes/functions.inc.php
index b9d42884..5d713ee0 100644
--- a/src/includes/functions.inc.php
+++ b/src/includes/functions.inc.php
@@ -128,6 +128,7 @@ function psm_get_conf($key, $alt = null) {
* Load config from the database to the $GLOBALS['sm_config'] variable
*
* @global object $db
+ * @return boolean
* @see psm_get_conf()
*/
function psm_load_conf() {
@@ -135,14 +136,19 @@ function psm_load_conf() {
// load config from database into global scope
$GLOBALS['sm_config'] = array();
- $config_db = $db->select(PSM_DB_PREFIX . 'config', null, array('key', 'value'));
- foreach($config_db as $setting) {
- $GLOBALS['sm_config'][$setting['key']] = $setting['value'];
- }
- if(empty($GLOBALS['sm_config']) && basename($_SERVER['SCRIPT_NAME']) != 'install.php') {
- // no config found, go to install page
- die('Failed to load config table. Please run the install.php file');
+ if(!$db->ifTableExists(PSM_DB_PREFIX.'config')) {
+ return false;
+ }
+ $config_db = $db->select(PSM_DB_PREFIX . 'config', null, array('key', 'value'));
+
+ if(is_array($config_db) && !empty($config_db)) {
+ foreach($config_db as $setting) {
+ $GLOBALS['sm_config'][$setting['key']] = $setting['value'];
+ }
+ return true;
+ } else {
+ return false;
}
}
diff --git a/src/psm/Module/Install/Controller/InstallController.class.php b/src/psm/Module/Install/Controller/InstallController.class.php
index fde992e3..f561df6a 100644
--- a/src/psm/Module/Install/Controller/InstallController.class.php
+++ b/src/psm/Module/Install/Controller/InstallController.class.php
@@ -335,13 +335,7 @@ class InstallController extends AbstractController {
if(!$this->db->status()) {
return false;
}
- $confExists = $this->db->query("SHOW TABLES LIKE '".PSM_DB_PREFIX."config';", false)->rowCount();
-
- if($confExists > 0) {
- return true;
- } else {
- return false;
- }
+ return $this->db->ifTableExists(PSM_DB_PREFIX.'config');
}
/**
diff --git a/src/psm/Service/Database.class.php b/src/psm/Service/Database.class.php
index d5933809..24649ebd 100644
--- a/src/psm/Service/Database.class.php
+++ b/src/psm/Service/Database.class.php
@@ -313,6 +313,29 @@ class Database {
return $this->last;
}
+ /**
+ * Check if a certain table exists.
+ * @param string $table
+ * @return boolean
+ */
+ public function ifTableExists($table) {
+ $table = $this->quote($table);
+ $db = $this->quote($this->getDbName());
+
+ $if_exists = "SELECT COUNT(*) AS `cnt`
+ FROM `information_schema`.`tables`
+ WHERE `table_schema` = {$db}
+ AND `table_name` = {$table};
+ ";
+ $if_exists = $this->query($if_exists);
+
+ if(isset($if_exists[0]['cnt']) && $if_exists[0]['cnt'] == 1) {
+ return true;
+ } else {
+ false;
+ }
+ }
+
/**
* Quote a string
* @param string $value