diff options
Diffstat (limited to 'include/Env.class.php')
-rw-r--r-- | include/Env.class.php | 113 |
1 files changed, 113 insertions, 0 deletions
diff --git a/include/Env.class.php b/include/Env.class.php new file mode 100644 index 0000000..2f7ed44 --- /dev/null +++ b/include/Env.class.php @@ -0,0 +1,113 @@ +<?php + +class Env { + + private static $_variables = []; + private static $_lang; + + private static function _fetch() { + + if (empty(self::$_variables)) { + + $configdir = opendir(BASEPATH.'/config'); + + while ($file = readdir($configdir)) { + if (preg_match('/.*\.json$/', $file)) { + self::$_variables[preg_replace('/\.json$/', '', $file)] = json_decode(file_get_contents(BASEPATH.'/config/'.$file), TRUE); + } + } + + } + + } + + static function get() { + + self::_fetch(); + + $default = NULL; + $params = func_get_args(); + if (!is_string(end($params))) { + $default = array_pop($params); + } + elseif (strpos(end($params), 'default:') === 0) { + $default = str_replace('default:', '', array_pop($params)); + } + + $ret = self::$_variables; + foreach ($params as $param) { + if (isset($ret[$param])) { + $ret = $ret[$param]; + } + else { + if ($default!==null) { + return $default; + } + throw new EnvNoSuchEntryException('Entry '.implode(':',$params).' not found'); + } + } + return $ret; + + } + + static function remoteIP() { + $ip = $_SERVER['REMOTE_ADDR']; + $proxy_ip = $_SERVER['HTTP_X_FORWARDED_FOR']; + if ($proxy_ip != ""){ + $proxy_ip = split(", ", $proxy_ip); + $ip = array_shift($proxy_ip); + array_push($proxy_ip, $_SERVER['REMOTE_ADDR']); + $proxy_ip = join(", ", $proxy_ip); + } + return $ip; + } + + static public function getHostName() { + try { + $hostname = MySession::get('hostname'); + } + catch (SessionVariableException $e) { + $hostname = MySession::set('hostname', exec('hostname')); + } + return strtolower($hostname); + } + + static public function getDomain($default = '') { + return strtolower(isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $default); + } + + static public function lang() { + if (self::$_lang === null) { + try { + $lang = MySession::get('lang'); + } + catch (SessionVariableException $e) { + if (class_exists('MyLocale')) { + $lang = MyLocale::getPreferredLang(); + } + else { + $lang = Env::get('locale','default-language'); + } + } + self::setLang($lang); + } + return self::$_lang; + } + + static public function setLang($lang) { + if ($lang != self::$_lang) { + MySession::set('lang', $lang); + self::$_lang = $lang; + if (class_exists('MyLocale')) { + MyLocale::setLocale($lang); + } + } + } + + static public function AJAX_setLang($params) { + self::setLang($params['lang']); + return Env::lang(); + } + + +} |