Subversion Repositories ALCASAR

Rev

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