Subversion Repositories ALCASAR

Rev

Rev 3037 | 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
     */
3100 rexy 6
    define('PSI_VERSION', '3.4.2');
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 {
3100 rexy 35
                    if ((($paramup = strtoupper($param)) !== 'WMI_PASSWORD') && ($paramup !== 'SSH_PASSWORD') && strstr($value, ',')) {
36
                        define($name_prefix.$paramup, 'return '.var_export(preg_split('/\s*,\s*/', trim($value), -1, PREG_SPLIT_NO_EMPTY), 1).';');
2770 rexy 37
                    } else {
3100 rexy 38
                        define($name_prefix.$paramup, trim($value));
2770 rexy 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
 
3100 rexy 63
        $ip_decimal = ip2long($ip);
64
        if ($ip_decimal === false) {
65
            echo "Client IP wrong address (".$ip."). Client not allowed.";
2770 rexy 66
            die();
67
        }
3100 rexy 68
 
69
        // code based on https://gist.github.com/tott/7684443
70
        $was = false;
71
        foreach ($allowed as $allow) {
72
            if (strpos($allow, '/') === false) {
73
                    $was = ($allow === $ip);
74
            } else {
75
                  list($allow, $netmask) = explode('/', $allow, 2);
76
                  $allow_decimal = ip2long($allow);
77
                  $wildcard_decimal = pow(2, (32 - $netmask)) - 1;
78
                  $netmask_decimal = ~$wildcard_decimal;
79
                $was = (($ip_decimal & $netmask_decimal) === ($allow_decimal & $netmask_decimal));
80
            }
81
            if ($was) {
82
               break;
83
            }
84
        }
85
 
86
        if (!$was) {
87
            echo "Client IP address (".$ip.") not allowed.";
88
            die();
89
        }
2770 rexy 90
    }
91
 
92
    /* default error handler */
93
    if (function_exists('errorHandlerPsi')) {
94
        restore_error_handler();
95
    }
96
 
97
    /* fatal errors only */
98
    $old_err_rep = error_reporting();
99
    error_reporting(E_ERROR);
100
 
101
    /* get git revision */
102
    if (file_exists(PSI_APP_ROOT.'/.git/HEAD')) {
103
        $contents = @file_get_contents(PSI_APP_ROOT.'/.git/HEAD');
104
        if ($contents && preg_match("/^ref:\s+(.*)\/([^\/\s]*)/m", $contents, $matches)) {
105
            $contents = @file_get_contents(PSI_APP_ROOT.'/.git/'.$matches[1]."/".$matches[2]);
106
            if ($contents && preg_match("/^([^\s]*)/m", $contents, $revision)) {
107
                define('PSI_VERSION_STRING', PSI_VERSION ."-".$matches[2]."-".substr($revision[1], 0, 7));
108
            } else {
109
                define('PSI_VERSION_STRING', PSI_VERSION ."-".$matches[2]);
110
            }
111
        }
112
    }
113
    /* get svn revision */
114
    if (!defined('PSI_VERSION_STRING') && file_exists(PSI_APP_ROOT.'/.svn/entries')) {
115
        $contents = @file_get_contents(PSI_APP_ROOT.'/.svn/entries');
116
        if ($contents && preg_match("/dir\n(.+)/", $contents, $matches)) {
117
            define('PSI_VERSION_STRING', PSI_VERSION."-r".$matches[1]);
118
        } else {
119
            define('PSI_VERSION_STRING', PSI_VERSION);
120
        }
121
    }
122
    if (!defined('PSI_VERSION_STRING')) {
123
        define('PSI_VERSION_STRING', PSI_VERSION);
124
    }
125
 
3037 rexy 126
    if (defined('PSI_ROOTFS') && is_string(PSI_ROOTFS) && (PSI_ROOTFS !== '') && (PSI_ROOTFS !== '/')) {
127
        $rootfs = PSI_ROOTFS;
128
        if ($rootfs[0] === '/') {
129
            define('PSI_ROOT_FILESYSTEM', $rootfs);
130
        } else {
131
            define('PSI_ROOT_FILESYSTEM', '');
132
        }
133
    } else {
134
        define('PSI_ROOT_FILESYSTEM', '');
135
    }
136
 
2770 rexy 137
    if (!defined('PSI_OS')) { //if not overloaded in phpsysinfo.ini
138
        /* get Linux code page */
3100 rexy 139
        if ((PHP_OS == 'Linux') || (PHP_OS == 'GNU')) {
3037 rexy 140
            if (file_exists($fname = PSI_ROOT_FILESYSTEM.'/etc/sysconfig/i18n')
141
               || file_exists($fname = PSI_ROOT_FILESYSTEM.'/etc/default/locale')
142
               || file_exists($fname = PSI_ROOT_FILESYSTEM.'/etc/locale.conf')
143
               || file_exists($fname = PSI_ROOT_FILESYSTEM.'/etc/sysconfig/language')
144
               || file_exists($fname = PSI_ROOT_FILESYSTEM.'/etc/profile.d/lang.sh')
145
               || file_exists($fname = PSI_ROOT_FILESYSTEM.'/etc/profile.d/i18n.sh')
146
               || file_exists($fname = PSI_ROOT_FILESYSTEM.'/etc/profile')) {
2770 rexy 147
                $contents = @file_get_contents($fname);
148
            } else {
149
                $contents = false;
3100 rexy 150
                if (PHP_OS == 'Linux') {
151
                    if (file_exists(PSI_ROOT_FILESYSTEM.'/system/build.prop')) { //Android
152
                        define('PSI_OS', 'Android');
153
                        if ((PSI_ROOT_FILESYSTEM === '') && function_exists('exec') && @exec('uname -o 2>/dev/null', $unameo) && (sizeof($unameo)>0) && (($unameo0 = trim($unameo[0])) != "")) {
154
                            define('PSI_UNAMEO', $unameo0); // is Android on Termux
155
                        }
156
                        if ((PSI_ROOT_FILESYSTEM === '') && !defined('PSI_MODE_POPEN')) { //if not overloaded in phpsysinfo.ini
157
                            if (!function_exists("proc_open")) { //proc_open function test by executing 'pwd' bbbmand
158
                                define('PSI_MODE_POPEN', true); //use popen() function - no stderr error handling (but with problems with timeout)
2770 rexy 159
                            } else {
3100 rexy 160
                                $out = '';
161
                                $err = '';
162
                                $pipes = array();
163
                                $descriptorspec = array(0=>array("pipe", "r"), 1=>array("pipe", "w"), 2=>array("pipe", "w"));
164
                                $process = proc_open("pwd 2>/dev/null ", $descriptorspec, $pipes);
165
                                if (!is_resource($process)) {
166
                                    define('PSI_MODE_POPEN', true);
167
                                } else {
168
                                    $w = null;
169
                                    $e = null;
2770 rexy 170
 
3100 rexy 171
                                    while (!(feof($pipes[1]) && feof($pipes[2]))) {
172
                                        $read = array($pipes[1], $pipes[2]);
2770 rexy 173
 
3100 rexy 174
                                        $n = stream_select($read, $w, $e, 5);
2770 rexy 175
 
3100 rexy 176
                                        if (($n === false) || ($n === 0)) {
177
                                            break;
178
                                        }
2770 rexy 179
 
3100 rexy 180
                                        foreach ($read as $r) {
181
                                            if ($r == $pipes[1]) {
182
                                                $out .= fread($r, 4096);
183
                                            } elseif (feof($pipes[1]) && ($r == $pipes[2])) {//read STDERR after STDOUT
184
                                                $err .= fread($r, 4096);
185
                                            }
2770 rexy 186
                                        }
187
                                    }
188
 
3100 rexy 189
                                    if (($out === null) || (trim($out) == "") || (substr(trim($out), 0, 1) != "/")) {
190
                                        define('PSI_MODE_POPEN', true);
191
                                    }
192
                                    fclose($pipes[0]);
193
                                    fclose($pipes[1]);
194
                                    fclose($pipes[2]);
195
                                    // It is important that you close any pipes before calling
196
                                    // proc_close in order to avoid a deadlock
197
                                    proc_close($process);
2770 rexy 198
                                }
199
                            }
200
                        }
3100 rexy 201
                    } elseif (file_exists(PSI_ROOT_FILESYSTEM.'/var/mobile/Library/Cydia/metadata.cb0')) { //jailbroken iOS with Cydia
202
                        define('PSI_OS', 'Darwin');
2770 rexy 203
                    }
204
                }
205
            }
206
            if (!(defined('PSI_SYSTEM_CODEPAGE') && defined('PSI_SYSTEM_LANG')) //also if both not overloaded in phpsysinfo.ini
207
               && $contents && (preg_match('/^(LANG="?[^"\n]*"?)/m', $contents, $matches)
208
               || preg_match('/^RC_(LANG="?[^"\n]*"?)/m', $contents, $matches)
209
               || preg_match('/^\s*export (LANG="?[^"\n]*"?)/m', $contents, $matches))) {
210
                if (!defined('PSI_SYSTEM_CODEPAGE')) {
3037 rexy 211
                    if (file_exists($vtfname = PSI_ROOT_FILESYSTEM.'/sys/module/vt/parameters/default_utf8')
2770 rexy 212
                       && (trim(@file_get_contents($vtfname)) === "1")) {
3100 rexy 213
                        define('PSI_SYSTEM_CODEPAGE', 'UTF-8');
3037 rexy 214
                    } 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 215
                        foreach ($lines as $line) {
216
                            if (preg_match('/^charmap="?([^"]*)/', $line, $matches2)) {
217
                                define('PSI_SYSTEM_CODEPAGE', $matches2[1]);
218
                                break;
219
                            }
220
                        }
221
                    }
222
                }
3037 rexy 223
                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 224
                    foreach ($lines2 as $line) {
225
                        if (preg_match('/^LC_MESSAGES="?([^\."@]*)/', $line, $matches2)) {
226
                            $lang = "";
227
                            if (is_readable(PSI_APP_ROOT.'/data/languages.ini') && ($langdata = @parse_ini_file(PSI_APP_ROOT.'/data/languages.ini', true))) {
228
                                if (isset($langdata['Linux']['_'.$matches2[1]])) {
229
                                    $lang = $langdata['Linux']['_'.$matches2[1]];
230
                                }
231
                            }
232
                            if ($lang == "") {
233
                                $lang = 'Unknown';
234
                            }
235
                            define('PSI_SYSTEM_LANG', $lang.' ('.$matches2[1].')');
236
                            break;
237
                        }
238
                    }
239
                }
240
            }
241
        } elseif (PHP_OS == 'Haiku') {
242
            if (!(defined('PSI_SYSTEM_CODEPAGE') && defined('PSI_SYSTEM_LANG')) //also if both not overloaded in phpsysinfo.ini
3037 rexy 243
                && (PSI_ROOT_FILESYSTEM === '') && function_exists('exec') && @exec('locale --message 2>/dev/null', $lines)) {
2770 rexy 244
                foreach ($lines as $line) {
245
                    if (preg_match('/^"?([^\."]*)\.?([^"]*)/', $line, $matches2)) {
246
 
3037 rexy 247
                        if (!defined('PSI_SYSTEM_CODEPAGE') && isset($matches2[2]) && ($matches2[2] !== null) && (trim($matches2[2]) != "")) { //also if not overloaded in phpsysinfo.ini
2770 rexy 248
                            define('PSI_SYSTEM_CODEPAGE', $matches2[2]);
249
                        }
250
 
251
                        if (!defined('PSI_SYSTEM_LANG')) { //if not overloaded in phpsysinfo.ini
252
                            $lang = "";
253
                            if (is_readable(PSI_APP_ROOT.'/data/languages.ini') && ($langdata = @parse_ini_file(PSI_APP_ROOT.'/data/languages.ini', true))) {
254
                                if (isset($langdata['Linux']['_'.$matches2[1]])) {
255
                                    $lang = $langdata['Linux']['_'.$matches2[1]];
256
                                }
257
                            }
258
                            if ($lang == "") {
259
                                $lang = 'Unknown';
260
                            }
261
                            define('PSI_SYSTEM_LANG', $lang.' ('.$matches2[1].')');
262
                        }
263
                        break;
264
                    }
265
                }
266
            }
3100 rexy 267
        } elseif ((PHP_OS == 'Darwin') || (defined('PSI_OS') && (PSI_OS == 'Darwin'))){
2770 rexy 268
            if (!defined('PSI_SYSTEM_LANG') //if not overloaded in phpsysinfo.ini
3037 rexy 269
                && (PSI_ROOT_FILESYSTEM === '') && function_exists('exec') && @exec('defaults read /Library/Preferences/.GlobalPreferences AppleLocale 2>/dev/null', $lines)) {
2770 rexy 270
                $lang = "";
271
                if (is_readable(PSI_APP_ROOT.'/data/languages.ini') && ($langdata = @parse_ini_file(PSI_APP_ROOT.'/data/languages.ini', true))) {
272
                    if (isset($langdata['Linux']['_'.$lines[0]])) {
273
                        $lang = $langdata['Linux']['_'.$lines[0]];
274
                    }
275
                }
276
                if ($lang == "") {
277
                    $lang = 'Unknown';
278
                }
279
                define('PSI_SYSTEM_LANG', $lang.' ('.$lines[0].')');
280
            }
281
        }
282
    }
283
 
2976 rexy 284
    /* maximum time in seconds a script is allowed to run before it is terminated by the parser */
285
    if (defined('PSI_MAX_TIMEOUT')) {
286
        ini_set('max_execution_time', max(intval(PSI_MAX_TIMEOUT), 0));
287
    } else {
288
        ini_set('max_execution_time', 30);
289
    }
290
 
2770 rexy 291
    /* executeProgram() timeout value in seconds */
292
    if (defined('PSI_EXEC_TIMEOUT')) {
293
        define('PSI_EXEC_TIMEOUT_INT', max(intval(PSI_EXEC_TIMEOUT), 1));
294
    } else {
295
        define('PSI_EXEC_TIMEOUT_INT', 30);
296
    }
297
 
298
    /* snmprealwalk() and executeProgram("snmpwalk") number of seconds until the first timeout */
299
    if (defined('PSI_SNMP_TIMEOUT')) {
300
        define('PSI_SNMP_TIMEOUT_INT', max(intval(PSI_SNMP_TIMEOUT), 1));
301
    } else {
302
        define('PSI_SNMP_TIMEOUT_INT', 3);
303
    }
304
 
305
    /* snmprealwalk() and executeProgram("snmpwalk") number of times to retry if timeouts occur */
306
    if (defined('PSI_SNMP_RETRY')) {
307
        define('PSI_SNMP_RETRY_INT', max(intval(PSI_SNMP_RETRY), 0));
308
    } else {
309
        define('PSI_SNMP_RETRY_INT', 0);
310
    }
311
 
312
    if (!defined('PSI_OS')) {
313
        define('PSI_OS', PHP_OS);
314
    }
315
 
316
    if (!defined('PSI_SYSTEM_LANG')) {
317
        define('PSI_SYSTEM_LANG', null);
318
    }
319
    if (!defined('PSI_SYSTEM_CODEPAGE')) { //if not overloaded in phpsysinfo.ini
320
        if ((PSI_OS=='Android') || (PSI_OS=='Darwin')) {
321
            define('PSI_SYSTEM_CODEPAGE', 'UTF-8');
322
        } elseif (PSI_OS=='Minix') {
323
            define('PSI_SYSTEM_CODEPAGE', 'CP437');
2976 rexy 324
        } elseif (PSI_OS!='WINNT') {
2770 rexy 325
            define('PSI_SYSTEM_CODEPAGE', null);
326
        }
327
    }
328
 
329
    if (!defined('PSI_JSON_ISSUE')) { //if not overloaded in phpsysinfo.ini
330
        if (!extension_loaded("xml")) {
331
            die("phpSysInfo requires the xml extension to php in order to work properly.");
332
        }
333
        if (simplexml_load_string("<A><B><C/></B>\n</A>") !== simplexml_load_string("<A><B><C/></B></A>")) { // json_encode issue test
334
            define('PSI_JSON_ISSUE', true); // Problem must be solved
335
        }
336
    }
337
 
338
    /* restore error level */
339
    error_reporting($old_err_rep);
340
 
341
    /* restore error handler */
342
    if (function_exists('errorHandlerPsi')) {
343
        set_error_handler('errorHandlerPsi');
344
    }
345
}