Subversion Repositories ALCASAR

Rev

Rev 2976 | Rev 3100 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log

Rev Author Line No. Line
2770 rexy 1
<?php
2
if (!defined('PSI_CONFIG_FILE')) {
3
    /**
4
     * phpSysInfo version
5
     */
3037 rexy 6
    define('PSI_VERSION', '3.4.1');
2770 rexy 7
    /**
8
     * phpSysInfo configuration
9
     */
10
    define('PSI_CONFIG_FILE', PSI_APP_ROOT.'/phpsysinfo.ini');
11
 
12
    define('ARRAY_EXP', '/^return array \([^;]*\);$/'); //array expression search
13
 
14
    if (!is_readable(PSI_CONFIG_FILE)) {
15
        echo "ERROR: phpsysinfo.ini does not exist or is not readable by the webserver in the phpsysinfo directory";
16
        die();
17
    } elseif (!($config = @parse_ini_file(PSI_CONFIG_FILE, true))) {
18
        echo "ERROR: phpsysinfo.ini file is not parsable";
19
        die();
20
    } else {
21
        foreach ($config as $name=>$group) {
22
            if (strtoupper($name)=="MAIN") {
23
                $name_prefix='PSI_';
24
            } elseif (strtoupper(substr($name, 0, 7))=="SENSOR_") {
25
                $name_prefix='PSI_'.strtoupper($name).'_';
26
            } else {
27
                $name_prefix='PSI_PLUGIN_'.strtoupper($name).'_';
28
            }
29
            foreach ($group as $param=>$value) {
30
                if ((trim($value)==="") || (trim($value)==="0")) {
31
                    define($name_prefix.strtoupper($param), false);
32
                } elseif (trim($value)==="1") {
33
                    define($name_prefix.strtoupper($param), true);
34
                } else {
35
                    if (strstr($value, ',')) {
36
                        define($name_prefix.strtoupper($param), 'return '.var_export(preg_split('/\s*,\s*/', trim($value), -1, PREG_SPLIT_NO_EMPTY), 1).';');
37
                    } else {
38
                        define($name_prefix.strtoupper($param), trim($value));
39
                    }
40
                }
41
            }
42
        }
43
    }
44
 
45
    if (defined('PSI_ALLOWED') && is_string(PSI_ALLOWED)) {
46
        if (preg_match(ARRAY_EXP, PSI_ALLOWED)) {
47
            $allowed = eval(strtolower(PSI_ALLOWED));
48
        } else {
49
            $allowed = array(strtolower(PSI_ALLOWED));
50
        }
51
 
52
        if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])) {
53
            $ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
54
        } else {
55
            if (isset($_SERVER["HTTP_CLIENT_IP"])) {
56
                $ip = $_SERVER["HTTP_CLIENT_IP"];
57
            } else {
58
                $ip = $_SERVER["REMOTE_ADDR"];
59
            }
60
        }
61
        $ip = preg_replace("/^::ffff:/", "", strtolower($ip));
62
 
63
        if (!in_array($ip, $allowed, true)) {
64
            echo "Client IP address not allowed";
65
            die();
66
        }
67
    }
68
 
69
    /* default error handler */
70
    if (function_exists('errorHandlerPsi')) {
71
        restore_error_handler();
72
    }
73
 
74
    /* fatal errors only */
75
    $old_err_rep = error_reporting();
76
    error_reporting(E_ERROR);
77
 
78
    /* get git revision */
79
    if (file_exists(PSI_APP_ROOT.'/.git/HEAD')) {
80
        $contents = @file_get_contents(PSI_APP_ROOT.'/.git/HEAD');
81
        if ($contents && preg_match("/^ref:\s+(.*)\/([^\/\s]*)/m", $contents, $matches)) {
82
            $contents = @file_get_contents(PSI_APP_ROOT.'/.git/'.$matches[1]."/".$matches[2]);
83
            if ($contents && preg_match("/^([^\s]*)/m", $contents, $revision)) {
84
                define('PSI_VERSION_STRING', PSI_VERSION ."-".$matches[2]."-".substr($revision[1], 0, 7));
85
            } else {
86
                define('PSI_VERSION_STRING', PSI_VERSION ."-".$matches[2]);
87
            }
88
        }
89
    }
90
    /* get svn revision */
91
    if (!defined('PSI_VERSION_STRING') && file_exists(PSI_APP_ROOT.'/.svn/entries')) {
92
        $contents = @file_get_contents(PSI_APP_ROOT.'/.svn/entries');
93
        if ($contents && preg_match("/dir\n(.+)/", $contents, $matches)) {
94
            define('PSI_VERSION_STRING', PSI_VERSION."-r".$matches[1]);
95
        } else {
96
            define('PSI_VERSION_STRING', PSI_VERSION);
97
        }
98
    }
99
    if (!defined('PSI_VERSION_STRING')) {
100
        define('PSI_VERSION_STRING', PSI_VERSION);
101
    }
102
 
3037 rexy 103
    if (defined('PSI_ROOTFS') && is_string(PSI_ROOTFS) && (PSI_ROOTFS !== '') && (PSI_ROOTFS !== '/')) {
104
        $rootfs = PSI_ROOTFS;
105
        if ($rootfs[0] === '/') {
106
            define('PSI_ROOT_FILESYSTEM', $rootfs);
107
        } else {
108
            define('PSI_ROOT_FILESYSTEM', '');
109
        }
110
    } else {
111
        define('PSI_ROOT_FILESYSTEM', '');
112
    }
113
 
2770 rexy 114
    if (!defined('PSI_OS')) { //if not overloaded in phpsysinfo.ini
115
        /* get Linux code page */
116
        if (PHP_OS == 'Linux') {
3037 rexy 117
            if (file_exists($fname = PSI_ROOT_FILESYSTEM.'/etc/sysconfig/i18n')
118
               || file_exists($fname = PSI_ROOT_FILESYSTEM.'/etc/default/locale')
119
               || file_exists($fname = PSI_ROOT_FILESYSTEM.'/etc/locale.conf')
120
               || file_exists($fname = PSI_ROOT_FILESYSTEM.'/etc/sysconfig/language')
121
               || file_exists($fname = PSI_ROOT_FILESYSTEM.'/etc/profile.d/lang.sh')
122
               || file_exists($fname = PSI_ROOT_FILESYSTEM.'/etc/profile.d/i18n.sh')
123
               || file_exists($fname = PSI_ROOT_FILESYSTEM.'/etc/profile')) {
2770 rexy 124
                $contents = @file_get_contents($fname);
125
            } else {
126
                $contents = false;
3037 rexy 127
                if (file_exists(PSI_ROOT_FILESYSTEM.'/system/build.prop')) { //Android
2770 rexy 128
                    define('PSI_OS', 'Android');
3037 rexy 129
                    if ((PSI_ROOT_FILESYSTEM === '') && function_exists('exec') && @exec('uname -o 2>/dev/null', $unameo) && (sizeof($unameo)>0) && (($unameo0 = trim($unameo[0])) != "")) {
2976 rexy 130
                        define('PSI_UNAMEO', $unameo0); // is Android on Termux
2770 rexy 131
                    }
3037 rexy 132
                    if ((PSI_ROOT_FILESYSTEM === '') && !defined('PSI_MODE_POPEN')) { //if not overloaded in phpsysinfo.ini
133
                        if (!function_exists("proc_open")) { //proc_open function test by executing 'pwd' bbbmand
2770 rexy 134
                            define('PSI_MODE_POPEN', true); //use popen() function - no stderr error handling (but with problems with timeout)
135
                        } else {
136
                            $out = '';
137
                            $err = '';
138
                            $pipes = array();
139
                            $descriptorspec = array(0=>array("pipe", "r"), 1=>array("pipe", "w"), 2=>array("pipe", "w"));
140
                            $process = proc_open("pwd 2>/dev/null ", $descriptorspec, $pipes);
141
                            if (!is_resource($process)) {
142
                                define('PSI_MODE_POPEN', true);
143
                            } else {
144
                                $w = null;
145
                                $e = null;
146
 
147
                                while (!(feof($pipes[1]) && feof($pipes[2]))) {
148
                                    $read = array($pipes[1], $pipes[2]);
149
 
150
                                    $n = stream_select($read, $w, $e, 5);
151
 
152
                                    if (($n === false) || ($n === 0)) {
153
                                        break;
154
                                    }
155
 
156
                                    foreach ($read as $r) {
157
                                        if ($r == $pipes[1]) {
158
                                            $out .= fread($r, 4096);
159
                                        } elseif (feof($pipes[1]) && ($r == $pipes[2])) {//read STDERR after STDOUT
160
                                            $err .= fread($r, 4096);
161
                                        }
162
                                    }
163
                                }
164
 
3037 rexy 165
                                if (($out === null) || (trim($out) == "") || (substr(trim($out), 0, 1) != "/")) {
2770 rexy 166
                                    define('PSI_MODE_POPEN', true);
167
                                }
168
                                fclose($pipes[0]);
169
                                fclose($pipes[1]);
170
                                fclose($pipes[2]);
171
                                // It is important that you close any pipes before calling
172
                                // proc_close in order to avoid a deadlock
173
                                proc_close($process);
174
                            }
175
                        }
176
                    }
177
                }
178
            }
179
            if (!(defined('PSI_SYSTEM_CODEPAGE') && defined('PSI_SYSTEM_LANG')) //also if both not overloaded in phpsysinfo.ini
180
               && $contents && (preg_match('/^(LANG="?[^"\n]*"?)/m', $contents, $matches)
181
               || preg_match('/^RC_(LANG="?[^"\n]*"?)/m', $contents, $matches)
182
               || preg_match('/^\s*export (LANG="?[^"\n]*"?)/m', $contents, $matches))) {
183
                if (!defined('PSI_SYSTEM_CODEPAGE')) {
3037 rexy 184
                    if (file_exists($vtfname = PSI_ROOT_FILESYSTEM.'/sys/module/vt/parameters/default_utf8')
2770 rexy 185
                       && (trim(@file_get_contents($vtfname)) === "1")) {
186
                            define('PSI_SYSTEM_CODEPAGE', 'UTF-8');
3037 rexy 187
                    } elseif ((PSI_ROOT_FILESYSTEM === '') && function_exists('exec') && @exec($matches[1].' locale -k LC_CTYPE 2>/dev/null', $lines)) { //if not overloaded in phpsysinfo.ini
2770 rexy 188
                        foreach ($lines as $line) {
189
                            if (preg_match('/^charmap="?([^"]*)/', $line, $matches2)) {
190
                                define('PSI_SYSTEM_CODEPAGE', $matches2[1]);
191
                                break;
192
                            }
193
                        }
194
                    }
195
                }
3037 rexy 196
                if ((PSI_ROOT_FILESYSTEM === '') && !defined('PSI_SYSTEM_LANG') && function_exists('exec') && @exec($matches[1].' locale 2>/dev/null', $lines2)) { //also if not overloaded in phpsysinfo.ini
2770 rexy 197
                    foreach ($lines2 as $line) {
198
                        if (preg_match('/^LC_MESSAGES="?([^\."@]*)/', $line, $matches2)) {
199
                            $lang = "";
200
                            if (is_readable(PSI_APP_ROOT.'/data/languages.ini') && ($langdata = @parse_ini_file(PSI_APP_ROOT.'/data/languages.ini', true))) {
201
                                if (isset($langdata['Linux']['_'.$matches2[1]])) {
202
                                    $lang = $langdata['Linux']['_'.$matches2[1]];
203
                                }
204
                            }
205
                            if ($lang == "") {
206
                                $lang = 'Unknown';
207
                            }
208
                            define('PSI_SYSTEM_LANG', $lang.' ('.$matches2[1].')');
209
                            break;
210
                        }
211
                    }
212
                }
213
            }
214
        } elseif (PHP_OS == 'Haiku') {
215
            if (!(defined('PSI_SYSTEM_CODEPAGE') && defined('PSI_SYSTEM_LANG')) //also if both not overloaded in phpsysinfo.ini
3037 rexy 216
                && (PSI_ROOT_FILESYSTEM === '') && function_exists('exec') && @exec('locale --message 2>/dev/null', $lines)) {
2770 rexy 217
                foreach ($lines as $line) {
218
                    if (preg_match('/^"?([^\."]*)\.?([^"]*)/', $line, $matches2)) {
219
 
3037 rexy 220
                        if (!defined('PSI_SYSTEM_CODEPAGE') && isset($matches2[2]) && ($matches2[2] !== null) && (trim($matches2[2]) != "")) { //also if not overloaded in phpsysinfo.ini
2770 rexy 221
                            define('PSI_SYSTEM_CODEPAGE', $matches2[2]);
222
                        }
223
 
224
                        if (!defined('PSI_SYSTEM_LANG')) { //if not overloaded in phpsysinfo.ini
225
                            $lang = "";
226
                            if (is_readable(PSI_APP_ROOT.'/data/languages.ini') && ($langdata = @parse_ini_file(PSI_APP_ROOT.'/data/languages.ini', true))) {
227
                                if (isset($langdata['Linux']['_'.$matches2[1]])) {
228
                                    $lang = $langdata['Linux']['_'.$matches2[1]];
229
                                }
230
                            }
231
                            if ($lang == "") {
232
                                $lang = 'Unknown';
233
                            }
234
                            define('PSI_SYSTEM_LANG', $lang.' ('.$matches2[1].')');
235
                        }
236
                        break;
237
                    }
238
                }
239
            }
240
        } elseif (PHP_OS == 'Darwin') {
241
            if (!defined('PSI_SYSTEM_LANG') //if not overloaded in phpsysinfo.ini
3037 rexy 242
                && (PSI_ROOT_FILESYSTEM === '') && function_exists('exec') && @exec('defaults read /Library/Preferences/.GlobalPreferences AppleLocale 2>/dev/null', $lines)) {
2770 rexy 243
                $lang = "";
244
                if (is_readable(PSI_APP_ROOT.'/data/languages.ini') && ($langdata = @parse_ini_file(PSI_APP_ROOT.'/data/languages.ini', true))) {
245
                    if (isset($langdata['Linux']['_'.$lines[0]])) {
246
                        $lang = $langdata['Linux']['_'.$lines[0]];
247
                    }
248
                }
249
                if ($lang == "") {
250
                    $lang = 'Unknown';
251
                }
252
                define('PSI_SYSTEM_LANG', $lang.' ('.$lines[0].')');
253
            }
254
        }
255
    }
256
 
2976 rexy 257
    /* maximum time in seconds a script is allowed to run before it is terminated by the parser */
258
    if (defined('PSI_MAX_TIMEOUT')) {
259
        ini_set('max_execution_time', max(intval(PSI_MAX_TIMEOUT), 0));
260
    } else {
261
        ini_set('max_execution_time', 30);
262
    }
263
 
2770 rexy 264
    /* executeProgram() timeout value in seconds */
265
    if (defined('PSI_EXEC_TIMEOUT')) {
266
        define('PSI_EXEC_TIMEOUT_INT', max(intval(PSI_EXEC_TIMEOUT), 1));
267
    } else {
268
        define('PSI_EXEC_TIMEOUT_INT', 30);
269
    }
270
 
271
    /* snmprealwalk() and executeProgram("snmpwalk") number of seconds until the first timeout */
272
    if (defined('PSI_SNMP_TIMEOUT')) {
273
        define('PSI_SNMP_TIMEOUT_INT', max(intval(PSI_SNMP_TIMEOUT), 1));
274
    } else {
275
        define('PSI_SNMP_TIMEOUT_INT', 3);
276
    }
277
 
278
    /* snmprealwalk() and executeProgram("snmpwalk") number of times to retry if timeouts occur */
279
    if (defined('PSI_SNMP_RETRY')) {
280
        define('PSI_SNMP_RETRY_INT', max(intval(PSI_SNMP_RETRY), 0));
281
    } else {
282
        define('PSI_SNMP_RETRY_INT', 0);
283
    }
284
 
285
    if (!defined('PSI_OS')) {
286
        define('PSI_OS', PHP_OS);
287
    }
288
 
289
    if (!defined('PSI_SYSTEM_LANG')) {
290
        define('PSI_SYSTEM_LANG', null);
291
    }
292
    if (!defined('PSI_SYSTEM_CODEPAGE')) { //if not overloaded in phpsysinfo.ini
293
        if ((PSI_OS=='Android') || (PSI_OS=='Darwin')) {
294
            define('PSI_SYSTEM_CODEPAGE', 'UTF-8');
295
        } elseif (PSI_OS=='Minix') {
296
            define('PSI_SYSTEM_CODEPAGE', 'CP437');
2976 rexy 297
        } elseif (PSI_OS!='WINNT') {
2770 rexy 298
            define('PSI_SYSTEM_CODEPAGE', null);
299
        }
300
    }
301
 
302
    if (!defined('PSI_JSON_ISSUE')) { //if not overloaded in phpsysinfo.ini
303
        if (!extension_loaded("xml")) {
304
            die("phpSysInfo requires the xml extension to php in order to work properly.");
305
        }
306
        if (simplexml_load_string("<A><B><C/></B>\n</A>") !== simplexml_load_string("<A><B><C/></B></A>")) { // json_encode issue test
307
            define('PSI_JSON_ISSUE', true); // Problem must be solved
308
        }
309
    }
310
 
311
    /* restore error level */
312
    error_reporting($old_err_rep);
313
 
314
    /* restore error handler */
315
    if (function_exists('errorHandlerPsi')) {
316
        set_error_handler('errorHandlerPsi');
317
    }
318
}