Several new things !

- General : syntax checking of the configuration file *(thanks to Micht69)*
- General : check PHP version (5.3+) *(thanks to Gilles)*
- General : add *check_updates* setting to the config file to enable or not the checking for updates
- General : add *auto_refresh* setting to the config file to automatically reload page each x seconds *(thanks to sebastienserre and Aranud)*
This commit is contained in:
ShevAbam 2014-10-21 11:27:39 +02:00
parent 8bc4826552
commit c963916274
3 changed files with 52 additions and 4 deletions

View File

@ -1,7 +1,9 @@
{ {
"esm": { "esm": {
"version": "2.1", "version": "2.2",
"website": "http://www.ezservermonitor.com" "website": "http://www.ezservermonitor.com",
"check_updates": true,
"auto_refresh": 0
}, },
"disk": { "disk": {
"show_tmpfs": false "show_tmpfs": false

View File

@ -8,6 +8,9 @@ $update = $Config->checkUpdate();
<html lang="en"> <html lang="en">
<head> <head>
<meta charset="utf-8"> <meta charset="utf-8">
<?php if ($Config->get('esm:auto_refresh') > 0): ?>
<meta http-equiv="refresh" content="<?php echo $Config->get('esm:auto_refresh'); ?>">
<?php endif; ?>
<title>eZ Server Monitor - <?php echo Misc::getHostname(); ?></title> <title>eZ Server Monitor - <?php echo Misc::getHostname(); ?></title>
<link rel="stylesheet" href="web/css/utilities.css" type="text/css"> <link rel="stylesheet" href="web/css/utilities.css" type="text/css">
<link rel="stylesheet" href="web/css/frontend.css" type="text/css"> <link rel="stylesheet" href="web/css/frontend.css" type="text/css">

View File

@ -7,9 +7,13 @@ class Config
public function __construct() public function __construct()
{ {
$this->_checkPHPVersion(5.3);
$this->file = __DIR__.'/../../esm.config.json'; $this->file = __DIR__.'/../../esm.config.json';
if (file_exists($this->file)) if (!file_exists($this->file))
throw new \Exception('Config file '.basename($this->file).' not found');
$this->_readFile(); $this->_readFile();
} }
@ -17,6 +21,11 @@ class Config
{ {
$content = file_get_contents($this->file); $content = file_get_contents($this->file);
$this->config = json_decode(utf8_encode($content), true); $this->config = json_decode(utf8_encode($content), true);
if ($this->config == null && json_last_error() != JSON_ERROR_NONE)
{
throw new \LogicException(sprintf("Failed to parse config file '%s'. Error: '%s'", basename($this->file) , json_last_error_msg()));
}
} }
@ -51,11 +60,26 @@ class Config
} }
/**
* Checks the PHP version compared to the required version
*/
private function _checkPHPVersion($min)
{
if (!version_compare(phpversion(), $min, '>='))
throw new \Exception('Your PHP version is too old ! PHP '.$min.' is required.');
return true;
}
/** /**
* Checks if there is an eSM`Web update available * Checks if there is an eSM`Web update available
*/ */
public function checkUpdate() public function checkUpdate()
{ {
if ($this->get('esm:check_updates') === false)
return false;
$response = null; $response = null;
$this_version = $this->get('esm:version'); $this_version = $this->get('esm:version');
$update_url = $this->get('esm:website').'/esm-web/update/'.$this_version; $update_url = $this->get('esm:website').'/esm-web/update/'.$this_version;
@ -92,3 +116,22 @@ class Config
} }
} }
} }
// PHP 5.5.0
if (!function_exists('json_last_error_msg'))
{
function json_last_error_msg()
{
static $errors = array(
JSON_ERROR_NONE => null,
JSON_ERROR_DEPTH => 'Maximum stack depth exceeded',
JSON_ERROR_STATE_MISMATCH => 'Underflow or the modes mismatch',
JSON_ERROR_CTRL_CHAR => 'Unexpected control character found',
JSON_ERROR_SYNTAX => 'Syntax error, malformed JSON',
JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded'
);
$error = json_last_error();
return array_key_exists($error, $errors) ? $errors[$error] : "Unknown error ({$error})";
}
}