summaryrefslogtreecommitdiff
path: root/libs/Utils
diff options
context:
space:
mode:
authorShevAbam <shevabam@gmail.com>2014-06-18 10:11:11 +0200
committerShevAbam <shevabam@gmail.com>2014-06-18 10:11:11 +0200
commit20705a550df5b13a544dc0865c6aef16e64c99d9 (patch)
treee396dface41e30d39a68d5fe66097b1f8f641a81 /libs/Utils
+ First commit (version 2.0)v2.0
Diffstat (limited to 'libs/Utils')
-rw-r--r--libs/Utils/Config.class.php94
-rw-r--r--libs/Utils/Misc.class.php57
2 files changed, 151 insertions, 0 deletions
diff --git a/libs/Utils/Config.class.php b/libs/Utils/Config.class.php
new file mode 100644
index 0000000..8111b32
--- /dev/null
+++ b/libs/Utils/Config.class.php
@@ -0,0 +1,94 @@
+<?php
+
+class Config
+{
+ public $file = null;
+ public $config = null;
+
+ public function __construct()
+ {
+ $this->file = $_SERVER['DOCUMENT_ROOT'].'/esm.config.json';
+
+ if (file_exists($this->file))
+ $this->_readFile();
+ }
+
+ private function _readFile()
+ {
+ $content = file_get_contents($this->file);
+ $this->config = json_decode($content, true);
+ }
+
+
+ /**
+ * 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 if there is an eSM`Web update available
+ */
+ public function checkUpdate()
+ {
+ $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'];
+ }
+ }
+ }
+} \ No newline at end of file
diff --git a/libs/Utils/Misc.class.php b/libs/Utils/Misc.class.php
new file mode 100644
index 0000000..3fcfedb
--- /dev/null
+++ b/libs/Utils/Misc.class.php
@@ -0,0 +1,57 @@
+<?php
+
+class Misc
+{
+ /**
+ * Returns human size
+ */
+ public static function getSize($filesize, $precision = 2)
+ {
+ $units = array('', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y');
+
+ foreach ($units as $idUnit => $unit)
+ {
+ if ($filesize > 1024)
+ $filesize /= 1024;
+ else
+ break;
+ }
+
+ return round($filesize, $precision).' '.$units[$idUnit].'o';
+ }
+
+
+ /**
+ * Returns hostname
+ */
+ public static function getHostname()
+ {
+ return php_uname('n');
+ }
+
+
+ /**
+ * Returns server IP
+ */
+ public static function getLanIp()
+ {
+ return $_SERVER['SERVER_ADDR'];
+ }
+
+ /**
+ * Allows to pluralize a word based on a number
+ * Ex : echo 'mot'.Misc::pluralize(5); ==> prints mots
+ * Ex : echo 'cheva'.Misc::pluralize(5, 'ux', 'l'); ==> prints chevaux
+ * Ex : echo 'cheva'.Misc::pluralize(1, 'ux', 'l'); ==> prints cheval
+ *
+ * @param int $nb
+ * @param string $plural
+ * @param string $singular
+ *
+ * @return string
+ */
+ public static function pluralize($nb, $plural = 's', $singular = '')
+ {
+ return $nb > 1 ? $plural : $singular;
+ }
+} \ No newline at end of file