summaryrefslogtreecommitdiff
path: root/libs/Utils
diff options
context:
space:
mode:
authorShevAbam <shevabam@gmail.com>2014-10-21 11:27:39 +0200
committerShevAbam <shevabam@gmail.com>2014-10-21 11:27:39 +0200
commitc963916274eed0ffe0ef430677c6d68ac6f6a9f1 (patch)
tree3bf3ddf5e92764347018790d4b7f318799b4bbb2 /libs/Utils
parent8bc4826552afb39c36abf09201461a119b81d4bf (diff)
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)*
Diffstat (limited to 'libs/Utils')
-rw-r--r--libs/Utils/Config.class.php47
1 files changed, 45 insertions, 2 deletions
diff --git a/libs/Utils/Config.class.php b/libs/Utils/Config.class.php
index a332b91..f218764 100644
--- a/libs/Utils/Config.class.php
+++ b/libs/Utils/Config.class.php
@@ -7,16 +7,25 @@ class Config
public function __construct()
{
+ $this->_checkPHPVersion(5.3);
+
$this->file = __DIR__.'/../../esm.config.json';
- if (file_exists($this->file))
- $this->_readFile();
+ if (!file_exists($this->file))
+ throw new \Exception('Config file '.basename($this->file).' not found');
+
+ $this->_readFile();
}
private function _readFile()
{
$content = file_get_contents($this->file);
$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()));
+ }
}
@@ -52,10 +61,25 @@ 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
*/
public function checkUpdate()
{
+ if ($this->get('esm:check_updates') === false)
+ return false;
+
$response = null;
$this_version = $this->get('esm:version');
$update_url = $this->get('esm:website').'/esm-web/update/'.$this_version;
@@ -91,4 +115,23 @@ 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})";
+ }
} \ No newline at end of file