summaryrefslogtreecommitdiff
path: root/include/Env.class.php
blob: 2f7ed441bf7441993b87269f0cc78181b5431d79 (plain)
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
<?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();
    }


}