1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
<?php
class Config
{
public $file = null;
public $config = null;
public function __construct()
{
$this->_checkPHPVersion(5.3);
$this->file = __DIR__.'/../../esm.config.json';
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()));
}
}
/**
* Returns a specific config variable
* Ex : get('ping:hosts')
*/
public function get($var)
{
$tab = $this->config;
$explode = explode(':', $var);
foreach ($explode as $vartmp)
{
if (isset($tab[$vartmp]))
{
$tab = $tab[$vartmp];
}
}
return $tab == $this->config ? null : $tab;
}
/**
* Returns all config variables
*/
public function getAll()
{
return $this->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 null;
$response = null;
$this_version = $this->get('esm:version');
$update_url = $this->get('esm:website').'/esm-web/update/'.$this_version;
if (!function_exists('curl_version'))
{
$tmp = @file_get_contents($update_url);
$response = json_decode($tmp, true);
}
else
{
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_CONNECTTIMEOUT => 10,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_TIMEOUT => 60,
CURLOPT_USERAGENT => 'eZ Server Monitor `Web',
CURLOPT_URL => $update_url,
));
$response = json_decode(curl_exec($curl), true);
curl_close($curl);
}
if (!is_null($response) && !empty($response))
{
if (is_null($response['error']))
{
return $response['datas'];
}
}
}
}
// 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})";
}
}
|