adding sanity checks to bootstrap to detect upgrade/missing config/etc;

This commit is contained in:
Pepijn Over 2014-04-05 15:18:36 +02:00
parent bc9e9c589f
commit bd623e1ebd
4 changed files with 64 additions and 19 deletions

View File

@ -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. <a href="install.php">Please click here to install PHP Server Monitor</a>.');
}
// 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, <a href="install.php">please click here</a> to update your database to the latest version.');
}
}
$lang = psm_get_conf('language', 'en_US');
psm_load_lang($lang);
psm_load_lang($lang);

View File

@ -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;
}
}

View File

@ -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');
}
/**

View File

@ -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