Subversion Repositories ALCASAR

Rev

Rev 2770 | Rev 3037 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log

Rev 2770 Rev 2976
1
<?php
1
<?php
2
/**
2
/**
3
 * common Functions class
3
 * common Functions class
4
 *
4
 *
5
 * PHP version 5
5
 * PHP version 5
6
 *
6
 *
7
 * @category  PHP
7
 * @category  PHP
8
 * @package   PSI
8
 * @package   PSI
9
 * @author    Michael Cramer <BigMichi1@users.sourceforge.net>
9
 * @author    Michael Cramer <BigMichi1@users.sourceforge.net>
10
 * @copyright 2009 phpSysInfo
10
 * @copyright 2009 phpSysInfo
11
 * @license   http://opensource.org/licenses/gpl-2.0.php GNU General Public License version 2, or (at your option) any later version
11
 * @license   http://opensource.org/licenses/gpl-2.0.php GNU General Public License version 2, or (at your option) any later version
12
 * @version   SVN: $Id: class.CommonFunctions.inc.php 699 2012-09-15 11:57:13Z namiltd $
12
 * @version   SVN: $Id: class.CommonFunctions.inc.php 699 2012-09-15 11:57:13Z namiltd $
13
 * @link      http://phpsysinfo.sourceforge.net
13
 * @link      http://phpsysinfo.sourceforge.net
14
 */
14
 */
15
 /**
15
 /**
16
 * class with common functions used in all places
16
 * class with common functions used in all places
17
 *
17
 *
18
 * @category  PHP
18
 * @category  PHP
19
 * @package   PSI
19
 * @package   PSI
20
 * @author    Michael Cramer <BigMichi1@users.sourceforge.net>
20
 * @author    Michael Cramer <BigMichi1@users.sourceforge.net>
21
 * @copyright 2009 phpSysInfo
21
 * @copyright 2009 phpSysInfo
22
 * @license   http://opensource.org/licenses/gpl-2.0.php GNU General Public License version 2, or (at your option) any later version
22
 * @license   http://opensource.org/licenses/gpl-2.0.php GNU General Public License version 2, or (at your option) any later version
23
 * @version   Release: 3.0
23
 * @version   Release: 3.0
24
 * @link      http://phpsysinfo.sourceforge.net
24
 * @link      http://phpsysinfo.sourceforge.net
25
 */
25
 */
26
class CommonFunctions
26
class CommonFunctions
27
{
27
{
28
    /**
28
    /**
29
     * holds codepage for chcp
29
     * holds codepage for chcp
30
     *
30
     *
31
     * @var integer
31
     * @var integer
32
     */
32
     */
33
    private static $_cp = null;
33
    private static $_cp = null;
34
 
34
 
-
 
35
    /**
-
 
36
     * value of checking run as administrator
-
 
37
     *
-
 
38
     * @var boolean
-
 
39
     */
-
 
40
    private static $_asadmin = null;
-
 
41
 
35
    public static function setcp($cp)
42
    public static function setcp($cp)
36
    {
43
    {
37
        CommonFunctions::$_cp = $cp;
44
        self::$_cp = $cp;
-
 
45
    }
-
 
46
 
-
 
47
    public static function getcp()
-
 
48
    {
-
 
49
        return self::$_cp;
-
 
50
    }
-
 
51
 
-
 
52
    public static function isAdmin()
-
 
53
    {
-
 
54
        if (self::$_asadmin == null) {
-
 
55
            if (PSI_OS == 'WINNT') {
-
 
56
                $strBuf = '';
-
 
57
                self::executeProgram('sfc', '2>&1', $strBuf, false); // 'net session' for detection does not work if "Server" (LanmanServer) service is stopped
-
 
58
                if (preg_match('/^\/SCANNOW\s/m', preg_replace('/(\x00)/', '', $strBuf))) { // SCANNOW checking - also if Unicode
-
 
59
                    self::$_asadmin = true;
-
 
60
                } else {
-
 
61
                    self::$_asadmin = false;
-
 
62
                }
-
 
63
            } else {
-
 
64
                self::$_asadmin = false;
-
 
65
            }
-
 
66
        }
-
 
67
 
-
 
68
        return self::$_asadmin;
38
    }
69
    }
39
 
70
 
40
    private static function _parse_log_file($string)
71
    private static function _parse_log_file($string)
41
    {
72
    {
42
        if (defined('PSI_LOG') && is_string(PSI_LOG) && (strlen(PSI_LOG)>0) && ((substr(PSI_LOG, 0, 1)=="-") || (substr(PSI_LOG, 0, 1)=="+"))) {
73
        if (defined('PSI_LOG') && is_string(PSI_LOG) && (strlen(PSI_LOG)>0) && ((substr(PSI_LOG, 0, 1)=="-") || (substr(PSI_LOG, 0, 1)=="+"))) {
43
            $log_file = substr(PSI_LOG, 1);
74
            $log_file = substr(PSI_LOG, 1);
44
            if (file_exists($log_file)) {
75
            if (file_exists($log_file)) {
45
                $contents = @file_get_contents($log_file);
76
                $contents = @file_get_contents($log_file);
46
                if ($contents && preg_match("/^\-\-\-[^-\r\n]+\-\-\- ".preg_quote($string, '/')."\r?\n/m", $contents, $matches, PREG_OFFSET_CAPTURE)) {
77
                if ($contents && preg_match("/^\-\-\-[^-\r\n]+\-\-\- ".preg_quote($string, '/')."\r?\n/m", $contents, $matches, PREG_OFFSET_CAPTURE)) {
47
                    $findIndex = $matches[0][1];
78
                    $findIndex = $matches[0][1];
48
                    if (preg_match("/\r?\n/m", $contents, $matches, PREG_OFFSET_CAPTURE, $findIndex)) {
79
                    if (preg_match("/\r?\n/m", $contents, $matches, PREG_OFFSET_CAPTURE, $findIndex)) {
49
                        $startIndex = $matches[0][1]+1;
80
                        $startIndex = $matches[0][1]+1;
50
                        if (preg_match("/^\-\-\-[^-\r\n]+\-\-\- /m", $contents, $matches, PREG_OFFSET_CAPTURE, $startIndex)) {
81
                        if (preg_match("/^\-\-\-[^-\r\n]+\-\-\- /m", $contents, $matches, PREG_OFFSET_CAPTURE, $startIndex)) {
51
                            $stopIndex = $matches[0][1];
82
                            $stopIndex = $matches[0][1];
52
 
83
 
53
                            return substr($contents, $startIndex, $stopIndex-$startIndex);
84
                            return substr($contents, $startIndex, $stopIndex-$startIndex);
54
                        } else {
85
                        } else {
55
                            return substr($contents, $startIndex);
86
                            return substr($contents, $startIndex);
56
                        }
87
                        }
57
                    }
88
                    }
58
                }
89
                }
59
            }
90
            }
60
        }
91
        }
61
 
92
 
62
        return false;
93
        return false;
63
    }
94
    }
64
 
95
 
65
    /**
96
    /**
66
     * Find a system program, do also path checking when not running on WINNT
97
     * Find a system program, do also path checking when not running on WINNT
67
     * on WINNT we simply return the name with the exe extension to the program name
98
     * on WINNT we simply return the name with the exe extension to the program name
68
     *
99
     *
69
     * @param string $strProgram name of the program
100
     * @param string $strProgram name of the program
70
     *
101
     *
71
     * @return string|null complete path and name of the program
102
     * @return string|null complete path and name of the program
72
     */
103
     */
73
    private static function _findProgram($strProgram)
104
    public static function _findProgram($strProgram)
74
    {
105
    {
75
        $path_parts = pathinfo($strProgram);
106
        $path_parts = pathinfo($strProgram);
76
        if (empty($path_parts['basename'])) {
107
        if (empty($path_parts['basename'])) {
77
            return null;
108
            return null;
78
        }
109
        }
79
        $arrPath = array();
110
        $arrPath = array();
80
 
111
 
81
        if (empty($path_parts['dirname']) || ($path_parts['dirname'] == '.')) {
112
        if (empty($path_parts['dirname']) || ($path_parts['dirname'] == '.')) {
82
            if ((PSI_OS == 'WINNT') && empty($path_parts['extension'])) {
113
            if ((PSI_OS == 'WINNT') && empty($path_parts['extension'])) {
83
                $strProgram .= '.exe';
114
                $strProgram .= '.exe';
84
                $path_parts = pathinfo($strProgram);
115
                $path_parts = pathinfo($strProgram);
85
            }
116
            }
86
            if (PSI_OS == 'WINNT') {
117
            if (PSI_OS == 'WINNT') {
87
                if (CommonFunctions::readenv('Path', $serverpath)) {
118
                if (self::readenv('Path', $serverpath)) {
88
                    $arrPath = preg_split('/;/', $serverpath, -1, PREG_SPLIT_NO_EMPTY);
119
                    $arrPath = preg_split('/;/', $serverpath, -1, PREG_SPLIT_NO_EMPTY);
89
                }
120
                }
90
            } else {
121
            } else {
91
                if (CommonFunctions::readenv('PATH', $serverpath)) {
122
                if (self::readenv('PATH', $serverpath)) {
92
                    $arrPath = preg_split('/:/', $serverpath, -1, PREG_SPLIT_NO_EMPTY);
123
                    $arrPath = preg_split('/:/', $serverpath, -1, PREG_SPLIT_NO_EMPTY);
93
                }
124
                }
94
            }
125
            }
95
            if (defined('PSI_UNAMEO') && (PSI_UNAMEO === 'Android') && !empty($arrPath)) {
126
            if (defined('PSI_UNAMEO') && (PSI_UNAMEO === 'Android') && !empty($arrPath)) {
96
                array_push($arrPath, '/system/bin'); // Termux patch
127
                array_push($arrPath, '/system/bin'); // Termux patch
97
            }
128
            }
98
            if (defined('PSI_ADD_PATHS') && is_string(PSI_ADD_PATHS)) {
129
            if (defined('PSI_ADD_PATHS') && is_string(PSI_ADD_PATHS)) {
99
                if (preg_match(ARRAY_EXP, PSI_ADD_PATHS)) {
130
                if (preg_match(ARRAY_EXP, PSI_ADD_PATHS)) {
100
                    $arrPath = array_merge(eval(PSI_ADD_PATHS), $arrPath); // In this order so $addpaths is before $arrPath when looking for a program
131
                    $arrPath = array_merge(eval(PSI_ADD_PATHS), $arrPath); // In this order so $addpaths is before $arrPath when looking for a program
101
                } else {
132
                } else {
102
                    $arrPath = array_merge(array(PSI_ADD_PATHS), $arrPath); // In this order so $addpaths is before $arrPath when looking for a program
133
                    $arrPath = array_merge(array(PSI_ADD_PATHS), $arrPath); // In this order so $addpaths is before $arrPath when looking for a program
103
                }
134
                }
104
            }
135
            }
105
        } else { //directory defined
136
        } else { //directory defined
106
            array_push($arrPath, $path_parts['dirname']);
137
            array_push($arrPath, $path_parts['dirname']);
107
            $strProgram = $path_parts['basename'];
138
            $strProgram = $path_parts['basename'];
108
        }
139
        }
109
 
140
 
110
        //add some default paths if we still have no paths here
141
        //add some default paths if we still have no paths here
111
        if (empty($arrPath) && PSI_OS != 'WINNT') {
142
        if (empty($arrPath) && (PSI_OS != 'WINNT')) {
112
            if (PSI_OS == 'Android') {
143
            if (PSI_OS == 'Android') {
113
                array_push($arrPath, '/system/bin');
144
                array_push($arrPath, '/system/bin');
114
            } else {
145
            } else {
115
                array_push($arrPath, '/bin', '/sbin', '/usr/bin', '/usr/sbin', '/usr/local/bin', '/usr/local/sbin');
146
                array_push($arrPath, '/bin', '/sbin', '/usr/bin', '/usr/sbin', '/usr/local/bin', '/usr/local/sbin');
116
            }
147
            }
117
        }
148
        }
118
 
149
 
119
        $exceptPath = "";
150
        $exceptPath = "";
120
        if ((PSI_OS == 'WINNT') && CommonFunctions::readenv('WinDir', $windir)) {
151
        if ((PSI_OS == 'WINNT') && self::readenv('WinDir', $windir)) {
121
            foreach ($arrPath as $strPath) {
152
            foreach ($arrPath as $strPath) {
122
                if ((strtolower($strPath) == $windir."\\system32") && is_dir($windir."\\SysWOW64")) {
153
                if ((strtolower($strPath) == strtolower($windir)."\\system32") && is_dir($windir."\\SysWOW64")) {
123
                    if (is_dir($windir."\\sysnative")) {
154
                    if (is_dir($windir."\\sysnative\\drivers")) { // or strlen(decbin(~0)) == 32; is_dir($windir."\\sysnative") sometimes does not work
124
                        $exceptPath = $windir."\\sysnative"; //32-bit PHP on 64-bit Windows
155
                        $exceptPath = $windir."\\sysnative"; //32-bit PHP on 64-bit Windows
125
                    } else {
156
                    } else {
126
                        $exceptPath = $windir."\\SysWOW64"; //64-bit PHP on 64-bit Windows
157
                        $exceptPath = $windir."\\SysWOW64"; //64-bit PHP on 64-bit Windows
127
                    }
158
                    }
128
                    array_push($arrPath, $exceptPath);
159
                    array_push($arrPath, $exceptPath);
129
                    break;
160
                    break;
130
                }
161
                }
131
            }
162
            }
132
        } elseif (PSI_OS == 'Android') {
163
        } elseif (PSI_OS == 'Android') {
133
            $exceptPath = '/system/bin';
164
            $exceptPath = '/system/bin';
134
        }
165
        }
135
 
166
 
136
        foreach ($arrPath as $strPath) {
167
        foreach ($arrPath as $strPath) {
137
            // Path with and without trailing slash
168
            // Path with and without trailing slash
138
            if (PSI_OS == 'WINNT') {
169
            if (PSI_OS == 'WINNT') {
139
                $strPath = rtrim($strPath, "\\");
170
                $strPath = rtrim($strPath, "\\");
140
                $strPathS = $strPath."\\";
171
                $strPathS = $strPath."\\";
141
            } else {
172
            } else {
142
                $strPath = rtrim($strPath, "/");
173
                $strPath = rtrim($strPath, "/");
143
                $strPathS = $strPath."/";
174
                $strPathS = $strPath."/";
144
            }
175
            }
145
            if (($strPath !== $exceptPath) && !is_dir($strPath)) {
176
            if (($strPath !== $exceptPath) && !is_dir($strPath)) {
146
                continue;
177
                continue;
147
            }
178
            }
148
            if (PSI_OS == 'WINNT') {
-
 
149
                $strProgrammpath = $strPathS.$strProgram;
-
 
150
            } else {
-
 
151
                $strProgrammpath = $strPathS.$strProgram;
179
            $strProgrammpath = $strPathS.$strProgram;
152
            }
-
 
153
            if (is_executable($strProgrammpath)) {
180
            if (is_executable($strProgrammpath) || ((PSI_OS == 'WINNT') && (strtolower($path_parts['extension']) == 'py'))) {
154
                return $strProgrammpath;
181
                return $strProgrammpath;
155
            }
182
            }
156
        }
183
        }
157
 
184
 
158
        return null;
185
        return null;
159
    }
186
    }
160
 
187
 
161
    /**
188
    /**
162
     * Execute a system program. return a trim()'d result.
189
     * Execute a system program. return a trim()'d result.
163
     * does very crude pipe checking.  you need ' | ' for it to work
190
     * does very crude pipe and multiple commands (on WinNT) checking.  you need ' | ' or ' & ' for it to work
164
     * ie $program = CommonFunctions::executeProgram('netstat', '-anp | grep LIST');
191
     * ie $program = CommonFunctions::executeProgram('netstat', '-anp | grep LIST');
165
     * NOT $program = CommonFunctions::executeProgram('netstat', '-anp|grep LIST');
192
     * NOT $program = CommonFunctions::executeProgram('netstat', '-anp|grep LIST');
166
     *
193
     *
167
     * @param string  $strProgramname name of the program
194
     * @param string  $strProgramname name of the program
168
     * @param string  $strArgs        arguments to the program
195
     * @param string  $strArgs        arguments to the program
169
     * @param string  &$strBuffer     output of the command
196
     * @param string  &$strBuffer     output of the command
170
     * @param boolean $booErrorRep    en- or disables the reporting of errors which should be logged
197
     * @param boolean $booErrorRep    en- or disables the reporting of errors which should be logged
171
     * @param integer $timeout        timeout value in seconds (default value is PSI_EXEC_TIMEOUT_INT)
198
     * @param integer $timeout        timeout value in seconds (default value is PSI_EXEC_TIMEOUT_INT)
172
     *
199
     *
173
     * @return boolean command successfull or not
200
     * @return boolean command successfull or not
174
     */
201
     */
175
    public static function executeProgram($strProgramname, $strArgs, &$strBuffer, $booErrorRep = true, $timeout = PSI_EXEC_TIMEOUT_INT)
202
    public static function executeProgram($strProgramname, $strArgs, &$strBuffer, $booErrorRep = true, $timeout = PSI_EXEC_TIMEOUT_INT)
176
    {
203
    {
177
        if (defined('PSI_LOG') && is_string(PSI_LOG) && (strlen(PSI_LOG)>0) && ((substr(PSI_LOG, 0, 1)=="-") || (substr(PSI_LOG, 0, 1)=="+"))) {
204
        if (defined('PSI_LOG') && is_string(PSI_LOG) && (strlen(PSI_LOG)>0) && ((substr(PSI_LOG, 0, 1)=="-") || (substr(PSI_LOG, 0, 1)=="+"))) {
178
            $out = self::_parse_log_file("Executing: ".trim($strProgramname.' '.$strArgs));
205
            $out = self::_parse_log_file("Executing: ".trim($strProgramname.' '.$strArgs));
179
            if ($out == false) {
206
            if ($out == false) {
180
                if (substr(PSI_LOG, 0, 1)=="-") {
207
                if (substr(PSI_LOG, 0, 1)=="-") {
181
                    $strBuffer = '';
208
                    $strBuffer = '';
182
 
209
 
183
                    return false;
210
                    return false;
184
                }
211
                }
185
            } else {
212
            } else {
186
                $strBuffer = $out;
213
                $strBuffer = $out;
187
 
214
 
188
                return true;
215
                return true;
189
            }
216
            }
190
        }
217
        }
191
 
218
 
192
        if ((PSI_OS !== 'WINNT') && preg_match('/^([^=]+=[^ \t]+)[ \t]+(.*)$/', $strProgramname, $strmatch)) {
219
        if ((PSI_OS != 'WINNT') && preg_match('/^([^=]+=[^ \t]+)[ \t]+(.*)$/', $strProgramname, $strmatch)) {
193
            $strSet = $strmatch[1].' ';
220
            $strSet = $strmatch[1].' ';
194
            $strProgramname = $strmatch[2];
221
            $strProgramname = $strmatch[2];
195
        } else {
222
        } else {
196
            $strSet = '';
223
            $strSet = '';
197
        }
224
        }
198
        $strProgram = self::_findProgram($strProgramname);
225
        $strProgram = self::_findProgram($strProgramname);
199
        $error = PSI_Error::singleton();
226
        $error = PSI_Error::singleton();
200
        if (!$strProgram) {
227
        if (!$strProgram) {
201
            if ($booErrorRep) {
228
            if ($booErrorRep) {
202
                $error->addError('find_program("'.$strProgramname.'")', 'program not found on the machine');
229
                $error->addError('find_program("'.$strProgramname.'")', 'program not found on the machine');
203
            }
230
            }
204
 
231
 
205
            return false;
232
            return false;
206
        } else {
233
        } else {
207
            if (preg_match('/\s/', $strProgram)) {
234
            if (preg_match('/\s/', $strProgram)) {
208
                $strProgram = '"'.$strProgram.'"';
235
                $strProgram = '"'.$strProgram.'"';
209
            }
236
            }
210
        }
237
        }
211
 
238
 
212
        if ((PSI_OS !== 'WINNT') && defined('PSI_SUDO_COMMANDS') && is_string(PSI_SUDO_COMMANDS)) {
239
        if ((PSI_OS != 'WINNT') && defined('PSI_SUDO_COMMANDS') && is_string(PSI_SUDO_COMMANDS)) {
213
            if (preg_match(ARRAY_EXP, PSI_SUDO_COMMANDS)) {
240
            if (preg_match(ARRAY_EXP, PSI_SUDO_COMMANDS)) {
214
                $sudocommands = eval(PSI_SUDO_COMMANDS);
241
                $sudocommands = eval(PSI_SUDO_COMMANDS);
215
            } else {
242
            } else {
216
                $sudocommands = array(PSI_SUDO_COMMANDS);
243
                $sudocommands = array(PSI_SUDO_COMMANDS);
217
            }
244
            }
218
            if (in_array($strProgramname, $sudocommands)) {
245
            if (in_array($strProgramname, $sudocommands)) {
219
                $sudoProgram = self::_findProgram("sudo");
246
                $sudoProgram = self::_findProgram("sudo");
220
                if (!$sudoProgram) {
247
                if (!$sudoProgram) {
221
                    if ($booErrorRep) {
248
                    if ($booErrorRep) {
222
                        $error->addError('find_program("sudo")', 'program not found on the machine');
249
                        $error->addError('find_program("sudo")', 'program not found on the machine');
223
                    }
250
                    }
224
 
251
 
225
                    return false;
252
                    return false;
226
                } else {
253
                } else {
227
                    if (preg_match('/\s/', $sudoProgram)) {
254
                    if (preg_match('/\s/', $sudoProgram)) {
228
                        $strProgram = '"'.$sudoProgram.'" '.$strProgram;
255
                        $strProgram = '"'.$sudoProgram.'" '.$strProgram;
229
                    } else {
256
                    } else {
230
                        $strProgram = $sudoProgram.' '.$strProgram;
257
                        $strProgram = $sudoProgram.' '.$strProgram;
231
                    }
258
                    }
232
                }
259
                }
233
            }
260
            }
234
        }
261
        }
235
 
262
 
236
        // see if we've gotten a |, if we have we need to do path checking on the cmd
263
        // see if we've gotten a | or &, if we have we need to do path checking on the cmd
237
        if ($strArgs) {
264
        if ($strArgs) {
238
            $arrArgs = preg_split('/ /', $strArgs, -1, PREG_SPLIT_NO_EMPTY);
265
            $arrArgs = preg_split('/ /', $strArgs, -1, PREG_SPLIT_NO_EMPTY);
239
            for ($i = 0, $cnt_args = count($arrArgs); $i < $cnt_args; $i++) {
266
            for ($i = 0, $cnt_args = count($arrArgs); $i < $cnt_args; $i++) {
240
                if ($arrArgs[$i] == '|') {
267
                if (($arrArgs[$i] == '|') || ($arrArgs[$i] == '&')) {
241
                    $strCmd = $arrArgs[$i + 1];
268
                    $strCmd = $arrArgs[$i + 1];
242
                    $strNewcmd = self::_findProgram($strCmd);
269
                    $strNewcmd = self::_findProgram($strCmd);
-
 
270
                    if ($arrArgs[$i] == '|') {
243
                    $strArgs = preg_replace("/\| ".$strCmd.'/', '| "'.$strNewcmd.'"', $strArgs);
271
                        $strArgs = preg_replace('/\| '.$strCmd.'/', '| "'.$strNewcmd.'"', $strArgs);
-
 
272
                    } else {
-
 
273
                        $strArgs = preg_replace('/& '.$strCmd.'/', '& "'.$strNewcmd.'"', $strArgs);
-
 
274
                    }
244
                }
275
                }
245
            }
276
            }
246
            $strArgs = ' '.$strArgs;
277
            $strArgs = ' '.$strArgs;
247
        }
278
        }
248
 
279
 
249
        $strBuffer = '';
280
        $strBuffer = '';
250
        $strError = '';
281
        $strError = '';
251
        $pipes = array();
282
        $pipes = array();
252
        $descriptorspec = array(0=>array("pipe", "r"), 1=>array("pipe", "w"), 2=>array("pipe", "w"));
283
        $descriptorspec = array(0=>array("pipe", "r"), 1=>array("pipe", "w"), 2=>array("pipe", "w"));
253
        if (defined("PSI_MODE_POPEN") && PSI_MODE_POPEN === true) {
284
        if (defined("PSI_MODE_POPEN") && PSI_MODE_POPEN === true) {
254
            if (PSI_OS == 'WINNT') {
285
            if (PSI_OS == 'WINNT') {
255
                $process = $pipes[1] = popen($strSet.$strProgram.$strArgs." 2>nul", "r");
286
                $process = $pipes[1] = popen($strSet.$strProgram.$strArgs." 2>nul", "r");
256
            } else {
287
            } else {
257
                $process = $pipes[1] = popen($strSet.$strProgram.$strArgs." 2>/dev/null", "r");
288
                $process = $pipes[1] = popen($strSet.$strProgram.$strArgs." 2>/dev/null", "r");
258
            }
289
            }
259
        } else {
290
        } else {
260
            $process = proc_open($strSet.$strProgram.$strArgs, $descriptorspec, $pipes);
291
            $process = proc_open($strSet.$strProgram.$strArgs, $descriptorspec, $pipes);
261
        }
292
        }
262
        if (is_resource($process)) {
293
        if (is_resource($process)) {
263
            $te = self::_timeoutfgets($pipes, $strBuffer, $strError, $timeout);
294
            $te = self::_timeoutfgets($pipes, $strBuffer, $strError, $timeout);
264
            if (defined("PSI_MODE_POPEN") && PSI_MODE_POPEN === true) {
295
            if (defined("PSI_MODE_POPEN") && PSI_MODE_POPEN === true) {
265
                $return_value = pclose($pipes[1]);
296
                $return_value = pclose($pipes[1]);
266
            } else {
297
            } else {
267
                fclose($pipes[0]);
298
                fclose($pipes[0]);
268
                fclose($pipes[1]);
299
                fclose($pipes[1]);
269
                fclose($pipes[2]);
300
                fclose($pipes[2]);
270
                // It is important that you close any pipes before calling
301
                // It is important that you close any pipes before calling
271
                // proc_close in order to avoid a deadlock
302
                // proc_close in order to avoid a deadlock
272
                if ($te) {
303
                if ($te) {
273
                    proc_terminate($process); // proc_close tends to hang if the process is timing out
304
                    proc_terminate($process); // proc_close tends to hang if the process is timing out
274
                    $return_value = 0;
305
                    $return_value = 0;
275
                } else {
306
                } else {
276
                    $return_value = proc_close($process);
307
                    $return_value = proc_close($process);
277
                }
308
                }
278
            }
309
            }
279
        } else {
310
        } else {
280
            if ($booErrorRep) {
311
            if ($booErrorRep) {
281
                $error->addError($strProgram, "\nOpen process error");
312
                $error->addError($strProgram, "\nOpen process error");
282
            }
313
            }
283
 
314
 
284
            return false;
315
            return false;
285
        }
316
        }
286
        $strError = trim($strError);
317
        $strError = trim($strError);
287
        $strBuffer = trim($strBuffer);
318
        $strBuffer = trim($strBuffer);
288
        if (defined('PSI_LOG') && is_string(PSI_LOG) && (strlen(PSI_LOG)>0) && (substr(PSI_LOG, 0, 1)!="-") && (substr(PSI_LOG, 0, 1)!="+")) {
319
        if (defined('PSI_LOG') && is_string(PSI_LOG) && (strlen(PSI_LOG)>0) && (substr(PSI_LOG, 0, 1)!="-") && (substr(PSI_LOG, 0, 1)!="+")) {
289
            error_log("---".gmdate('r T')."--- Executing: ".trim($strProgramname.$strArgs)."\n".$strBuffer."\n", 3, PSI_LOG);
320
            error_log("---".gmdate('r T')."--- Executing: ".trim($strProgramname.$strArgs)."\n".$strBuffer."\n", 3, PSI_LOG);
290
        }
321
        }
291
        if (! empty($strError)) {
322
        if (! empty($strError)) {
292
            if ($booErrorRep) {
323
            if ($booErrorRep) {
293
                $error->addError($strProgram, $strError."\nReturn value: ".$return_value);
324
                $error->addError($strProgram, $strError."\nReturn value: ".$return_value);
294
            }
325
            }
295
 
326
 
296
            return $return_value == 0;
327
            return $return_value == 0;
297
        }
328
        }
298
 
329
 
299
        return true;
330
        return true;
300
    }
331
    }
301
 
332
 
302
    /**
333
    /**
303
     * read a one-line value from a file with a similar name
334
     * read a one-line value from a file with a similar name
304
     *
335
     *
305
     * @return value if successfull or null if not
336
     * @return value if successfull or null if not
306
     */
337
     */
307
    public static function rolv($similarFileName, $match = "//", $replace = "")
338
    public static function rolv($similarFileName, $match = "//", $replace = "")
308
    {
339
    {
309
        $filename = preg_replace($match, $replace, $similarFileName);
340
        $filename = preg_replace($match, $replace, $similarFileName);
310
        if (CommonFunctions::fileexists($filename) && CommonFunctions::rfts($filename, $buf, 1, 4096, false) && (($buf=trim($buf)) != "")) {
341
        if (self::fileexists($filename) && self::rfts($filename, $buf, 1, 4096, false) && (($buf=trim($buf)) != "")) {
311
            return $buf;
342
            return $buf;
312
        } else {
343
        } else {
313
            return null;
344
            return null;
314
        }
345
        }
315
    }
346
    }
316
 
347
 
317
    /**
348
    /**
318
     * read data from array $_SERVER
349
     * read data from array $_SERVER
319
     *
350
     *
320
     * @param string $strElem    element of array
351
     * @param string $strElem    element of array
321
     * @param string &$strBuffer output of the command
352
     * @param string &$strBuffer output of the command
322
     *
353
     *
323
     * @return string
354
     * @return string
324
     */
355
     */
325
    public static function readenv($strElem, &$strBuffer)
356
    public static function readenv($strElem, &$strBuffer)
326
    {
357
    {
327
        $strBuffer = '';
358
        $strBuffer = '';
328
        if (PSI_OS == 'WINNT') { //case insensitive
359
        if (PSI_OS == 'WINNT') { //case insensitive
329
            if (isset($_SERVER)) {
360
            if (isset($_SERVER)) {
330
                foreach ($_SERVER as $index=>$value) {
361
                foreach ($_SERVER as $index=>$value) {
331
                    if (is_string($value) && (trim($value) !== '') && (strtolower($index) === strtolower($strElem))) {
362
                    if (is_string($value) && (trim($value) !== '') && (strtolower($index) === strtolower($strElem))) {
332
                        $strBuffer = $value;
363
                        $strBuffer = $value;
333
 
364
 
334
                        return true;
365
                        return true;
335
                    }
366
                    }
336
                }
367
                }
337
            }
368
            }
338
        } else {
369
        } else {
339
            if (isset($_SERVER[$strElem]) && is_string($value = $_SERVER[$strElem]) && (trim($value) !== '')) {
370
            if (isset($_SERVER[$strElem]) && is_string($value = $_SERVER[$strElem]) && (trim($value) !== '')) {
340
                $strBuffer = $value;
371
                $strBuffer = $value;
341
 
372
 
342
                return true;
373
                return true;
343
            }
374
            }
344
        }
375
        }
345
 
376
 
346
        return false;
377
        return false;
347
    }
378
    }
348
 
379
 
349
    /**
380
    /**
350
     * read a file and return the content as a string
381
     * read a file and return the content as a string
351
     *
382
     *
352
     * @param string  $strFileName name of the file which should be read
383
     * @param string  $strFileName name of the file which should be read
353
     * @param string  &$strRet     content of the file (reference)
384
     * @param string  &$strRet     content of the file (reference)
354
     * @param integer $intLines    control how many lines should be read
385
     * @param integer $intLines    control how many lines should be read
355
     * @param integer $intBytes    control how many bytes of each line should be read
386
     * @param integer $intBytes    control how many bytes of each line should be read
356
     * @param boolean $booErrorRep en- or disables the reporting of errors which should be logged
387
     * @param boolean $booErrorRep en- or disables the reporting of errors which should be logged
357
     *
388
     *
358
     * @return boolean command successfull or not
389
     * @return boolean command successfull or not
359
     */
390
     */
360
    public static function rfts($strFileName, &$strRet, $intLines = 0, $intBytes = 4096, $booErrorRep = true)
391
    public static function rfts($strFileName, &$strRet, $intLines = 0, $intBytes = 4096, $booErrorRep = true)
361
    {
392
    {
362
        if (defined('PSI_LOG') && is_string(PSI_LOG) && (strlen(PSI_LOG)>0) && ((substr(PSI_LOG, 0, 1)=="-") || (substr(PSI_LOG, 0, 1)=="+"))) {
393
        if (defined('PSI_LOG') && is_string(PSI_LOG) && (strlen(PSI_LOG)>0) && ((substr(PSI_LOG, 0, 1)=="-") || (substr(PSI_LOG, 0, 1)=="+"))) {
363
            $out = self::_parse_log_file("Reading: ".$strFileName);
394
            $out = self::_parse_log_file("Reading: ".$strFileName);
364
            if ($out == false) {
395
            if ($out == false) {
365
                if (substr(PSI_LOG, 0, 1)=="-") {
396
                if (substr(PSI_LOG, 0, 1)=="-") {
366
                    $strRet = '';
397
                    $strRet = '';
367
 
398
 
368
                    return false;
399
                    return false;
369
                }
400
                }
370
            } else {
401
            } else {
371
                $strRet = $out;
402
                $strRet = $out;
372
 
403
 
373
                return true;
404
                return true;
374
            }
405
            }
375
        }
406
        }
376
 
407
 
377
        $strFile = "";
408
        $strFile = "";
378
        $intCurLine = 1;
409
        $intCurLine = 1;
379
        $error = PSI_Error::singleton();
410
        $error = PSI_Error::singleton();
380
        if (file_exists($strFileName)) {
411
        if (file_exists($strFileName)) {
381
            if (is_readable($strFileName)) {
412
            if (is_readable($strFileName)) {
382
                if ($fd = fopen($strFileName, 'r')) {
413
                if ($fd = fopen($strFileName, 'r')) {
383
                    while (!feof($fd)) {
414
                    while (!feof($fd)) {
384
                        $strFile .= fgets($fd, $intBytes);
415
                        $strFile .= fgets($fd, $intBytes);
385
                        if ($intLines <= $intCurLine && $intLines != 0) {
416
                        if ($intLines <= $intCurLine && $intLines != 0) {
386
                            break;
417
                            break;
387
                        } else {
418
                        } else {
388
                            $intCurLine++;
419
                            $intCurLine++;
389
                        }
420
                        }
390
                    }
421
                    }
391
                    fclose($fd);
422
                    fclose($fd);
392
                    $strRet = $strFile;
423
                    $strRet = $strFile;
393
                    if (defined('PSI_LOG') && is_string(PSI_LOG) && (strlen(PSI_LOG)>0) && (substr(PSI_LOG, 0, 1)!="-") && (substr(PSI_LOG, 0, 1)!="+")) {
424
                    if (defined('PSI_LOG') && is_string(PSI_LOG) && (strlen(PSI_LOG)>0) && (substr(PSI_LOG, 0, 1)!="-") && (substr(PSI_LOG, 0, 1)!="+")) {
394
                        if ((strlen($strRet)>0)&&(substr($strRet, -1)!="\n")) {
425
                        if ((strlen($strRet)>0)&&(substr($strRet, -1)!="\n")) {
395
                            error_log("---".gmdate('r T')."--- Reading: ".$strFileName."\n".$strRet."\n", 3, PSI_LOG);
426
                            error_log("---".gmdate('r T')."--- Reading: ".$strFileName."\n".$strRet."\n", 3, PSI_LOG);
396
                        } else {
427
                        } else {
397
                            error_log("---".gmdate('r T')."--- Reading: ".$strFileName."\n".$strRet, 3, PSI_LOG);
428
                            error_log("---".gmdate('r T')."--- Reading: ".$strFileName."\n".$strRet, 3, PSI_LOG);
398
                        }
429
                        }
399
                    }
430
                    }
400
                } else {
431
                } else {
401
                    if ($booErrorRep) {
432
                    if ($booErrorRep) {
402
                        $error->addError('fopen('.$strFileName.')', 'file can not read by phpsysinfo');
433
                        $error->addError('fopen('.$strFileName.')', 'file can not read by phpsysinfo');
403
                    }
434
                    }
404
 
435
 
405
                    return false;
436
                    return false;
406
                }
437
                }
407
            } else {
438
            } else {
408
                if ($booErrorRep) {
439
                if ($booErrorRep) {
409
                    $error->addError('fopen('.$strFileName.')', 'file permission error');
440
                    $error->addError('fopen('.$strFileName.')', 'file permission error');
410
                }
441
                }
411
 
442
 
412
                return false;
443
                return false;
413
            }
444
            }
414
        } else {
445
        } else {
415
            if ($booErrorRep) {
446
            if ($booErrorRep) {
416
                $error->addError('file_exists('.$strFileName.')', 'the file does not exist on your machine');
447
                $error->addError('file_exists('.$strFileName.')', 'the file does not exist on your machine');
417
            }
448
            }
418
 
449
 
419
            return false;
450
            return false;
420
        }
451
        }
421
 
452
 
422
        return true;
453
        return true;
423
    }
454
    }
424
 
455
 
425
    /**
456
    /**
426
     * file exists
457
     * file exists
427
     *
458
     *
428
     * @param string $strFileName name of the file which should be check
459
     * @param string $strFileName name of the file which should be check
429
     *
460
     *
430
     * @return boolean command successfull or not
461
     * @return boolean command successfull or not
431
     */
462
     */
432
    public static function fileexists($strFileName)
463
    public static function fileexists($strFileName)
433
    {
464
    {
434
        if (defined('PSI_LOG') && is_string(PSI_LOG) && (strlen(PSI_LOG)>0) && ((substr(PSI_LOG, 0, 1)=="-") || (substr(PSI_LOG, 0, 1)=="+"))) {
465
        if (defined('PSI_LOG') && is_string(PSI_LOG) && (strlen(PSI_LOG)>0) && ((substr(PSI_LOG, 0, 1)=="-") || (substr(PSI_LOG, 0, 1)=="+"))) {
435
            $log_file = substr(PSI_LOG, 1);
466
            $log_file = substr(PSI_LOG, 1);
436
            if (file_exists($log_file)
467
            if (file_exists($log_file)
437
                && ($contents = @file_get_contents($log_file))
468
                && ($contents = @file_get_contents($log_file))
438
                && preg_match("/^\-\-\-[^-\n]+\-\-\- ".preg_quote("Reading: ".$strFileName, '/')."\n/m", $contents)) {
469
                && preg_match("/^\-\-\-[^-\n]+\-\-\- ".preg_quote("Reading: ".$strFileName, '/')."\n/m", $contents)) {
439
                return true;
470
                return true;
440
            } else {
471
            } else {
441
                if (substr(PSI_LOG, 0, 1)=="-") {
472
                if (substr(PSI_LOG, 0, 1)=="-") {
442
                    return false;
473
                    return false;
443
                }
474
                }
444
            }
475
            }
445
        }
476
        }
446
 
477
 
447
        $exists =  file_exists($strFileName);
478
        $exists =  file_exists($strFileName);
448
        if (defined('PSI_LOG') && is_string(PSI_LOG) && (strlen(PSI_LOG)>0) && (substr(PSI_LOG, 0, 1)!="-") && (substr(PSI_LOG, 0, 1)!="+")) {
479
        if (defined('PSI_LOG') && is_string(PSI_LOG) && (strlen(PSI_LOG)>0) && (substr(PSI_LOG, 0, 1)!="-") && (substr(PSI_LOG, 0, 1)!="+")) {
449
            if ((substr($strFileName, 0, 5) === "/dev/") && $exists) {
480
            if ((substr($strFileName, 0, 5) === "/dev/") && $exists) {
450
                error_log("---".gmdate('r T')."--- Reading: ".$strFileName."\ndevice exists\n", 3, PSI_LOG);
481
                error_log("---".gmdate('r T')."--- Reading: ".$strFileName."\ndevice exists\n", 3, PSI_LOG);
451
            }
482
            }
452
        }
483
        }
453
 
484
 
454
        return $exists;
485
        return $exists;
455
    }
486
    }
456
 
487
 
457
    /**
488
    /**
458
     * reads a directory and return the name of the files and directorys in it
489
     * reads a directory and return the name of the files and directorys in it
459
     *
490
     *
460
     * @param string  $strPath     path of the directory which should be read
491
     * @param string  $strPath     path of the directory which should be read
461
     * @param boolean $booErrorRep en- or disables the reporting of errors which should be logged
492
     * @param boolean $booErrorRep en- or disables the reporting of errors which should be logged
462
     *
493
     *
463
     * @return array content of the directory excluding . and ..
494
     * @return array content of the directory excluding . and ..
464
     */
495
     */
465
    public static function gdc($strPath, $booErrorRep = true)
496
    public static function gdc($strPath, $booErrorRep = true)
466
    {
497
    {
467
        $arrDirectoryContent = array();
498
        $arrDirectoryContent = array();
468
        $error = PSI_Error::singleton();
499
        $error = PSI_Error::singleton();
469
        if (is_dir($strPath)) {
500
        if (is_dir($strPath)) {
470
            if ($handle = opendir($strPath)) {
501
            if ($handle = opendir($strPath)) {
471
                while (($strFile = readdir($handle)) !== false) {
502
                while (($strFile = readdir($handle)) !== false) {
472
                    if ($strFile != "." && $strFile != "..") {
503
                    if ($strFile != "." && $strFile != "..") {
473
                        $arrDirectoryContent[] = $strFile;
504
                        $arrDirectoryContent[] = $strFile;
474
                    }
505
                    }
475
                }
506
                }
476
                closedir($handle);
507
                closedir($handle);
477
            } else {
508
            } else {
478
                if ($booErrorRep) {
509
                if ($booErrorRep) {
479
                    $error->addError('opendir('.$strPath.')', 'directory can not be read by phpsysinfo');
510
                    $error->addError('opendir('.$strPath.')', 'directory can not be read by phpsysinfo');
480
                }
511
                }
481
            }
512
            }
482
        } else {
513
        } else {
483
            if ($booErrorRep) {
514
            if ($booErrorRep) {
484
                $error->addError('is_dir('.$strPath.')', 'directory does not exist on your machine');
515
                $error->addError('is_dir('.$strPath.')', 'directory does not exist on your machine');
485
            }
516
            }
486
        }
517
        }
487
 
518
 
488
        return $arrDirectoryContent;
519
        return $arrDirectoryContent;
489
    }
520
    }
490
 
521
 
491
    /**
522
    /**
492
     * Check for needed php extensions
523
     * Check for needed php extensions
493
     *
524
     *
494
     * We need that extensions for almost everything
525
     * We need that extensions for almost everything
495
     * This function will return a hard coded
526
     * This function will return a hard coded
496
     * XML string (with headers) if the SimpleXML extension isn't loaded.
527
     * XML string (with headers) if the SimpleXML extension isn't loaded.
497
     * Then it will terminate the script.
528
     * Then it will terminate the script.
498
     * See bug #1787137
529
     * See bug #1787137
499
     *
530
     *
500
     * @param array $arrExt additional extensions for which a check should run
531
     * @param array $arrExt additional extensions for which a check should run
501
     *
532
     *
502
     * @return void
533
     * @return void
503
     */
534
     */
504
    public static function checkForExtensions($arrExt = array())
535
    public static function checkForExtensions($arrExt = array())
505
    {
536
    {
506
        if ((strcasecmp(PSI_SYSTEM_CODEPAGE, "UTF-8") == 0) || (strcasecmp(PSI_SYSTEM_CODEPAGE, "CP437") == 0))
537
        if (defined('PSI_SYSTEM_CODEPAGE') && ((strcasecmp(PSI_SYSTEM_CODEPAGE, "UTF-8") == 0) || (strcasecmp(PSI_SYSTEM_CODEPAGE, "CP437") == 0)))
507
            $arrReq = array('simplexml', 'pcre', 'xml', 'dom');
538
            $arrReq = array('simplexml', 'pcre', 'xml', 'dom');
508
        elseif (PSI_OS == "WINNT")
539
        elseif (PSI_OS == 'WINNT')
509
            $arrReq = array('simplexml', 'pcre', 'xml', 'dom', 'mbstring', 'com_dotnet');
540
            $arrReq = array('simplexml', 'pcre', 'xml', 'dom', 'mbstring', 'com_dotnet');
510
        else
541
        else
511
            $arrReq = array('simplexml', 'pcre', 'xml', 'dom', 'mbstring');
542
            $arrReq = array('simplexml', 'pcre', 'xml', 'dom', 'mbstring');
512
        $extensions = array_merge($arrExt, $arrReq);
543
        $extensions = array_merge($arrExt, $arrReq);
513
        $text = "";
544
        $text = "";
514
        $error = false;
545
        $error = false;
515
        $text .= "<?xml version='1.0'?>\n";
546
        $text .= "<?xml version='1.0'?>\n";
516
        $text .= "<phpsysinfo>\n";
547
        $text .= "<phpsysinfo>\n";
517
        $text .= "  <Error>\n";
548
        $text .= "  <Error>\n";
518
        foreach ($extensions as $extension) {
549
        foreach ($extensions as $extension) {
519
            if (!extension_loaded($extension)) {
550
            if (!extension_loaded($extension)) {
520
                $text .= "    <Function>checkForExtensions</Function>\n";
551
                $text .= "    <Function>checkForExtensions</Function>\n";
521
                $text .= "    <Message>phpSysInfo requires the ".$extension." extension to php in order to work properly.</Message>\n";
552
                $text .= "    <Message>phpSysInfo requires the ".$extension." extension to php in order to work properly.</Message>\n";
522
                $error = true;
553
                $error = true;
523
            }
554
            }
524
        }
555
        }
525
        $text .= "  </Error>\n";
556
        $text .= "  </Error>\n";
526
        $text .= "</phpsysinfo>";
557
        $text .= "</phpsysinfo>";
527
        if ($error) {
558
        if ($error) {
528
            header("Content-Type: text/xml\n\n");
559
            header("Content-Type: text/xml\n\n");
529
            echo $text;
560
            echo $text;
530
            die();
561
            die();
531
        }
562
        }
532
    }
563
    }
533
 
564
 
534
    /**
565
    /**
535
     * get the content of stdout/stderr with the option to set a timeout for reading
566
     * get the content of stdout/stderr with the option to set a timeout for reading
536
     *
567
     *
537
     * @param array   $pipes   array of file pointers for stdin, stdout, stderr (proc_open())
568
     * @param array   $pipes   array of file pointers for stdin, stdout, stderr (proc_open())
538
     * @param string  &$out    target string for the output message (reference)
569
     * @param string  &$out    target string for the output message (reference)
539
     * @param string  &$err    target string for the error message (reference)
570
     * @param string  &$err    target string for the error message (reference)
540
     * @param integer $timeout timeout value in seconds
571
     * @param integer $timeout timeout value in seconds
541
     *
572
     *
542
     * @return boolean timeout expired or not
573
     * @return boolean timeout expired or not
543
     */
574
     */
544
    private static function _timeoutfgets($pipes, &$out, &$err, $timeout)
575
    private static function _timeoutfgets($pipes, &$out, &$err, $timeout)
545
    {
576
    {
546
        $w = null;
577
        $w = null;
547
        $e = null;
578
        $e = null;
548
        $te = false;
579
        $te = false;
549
 
580
 
550
        if (defined("PSI_MODE_POPEN") && PSI_MODE_POPEN === true) {
581
        if (defined("PSI_MODE_POPEN") && PSI_MODE_POPEN === true) {
551
            $pipe2 = false;
582
            $pipe2 = false;
552
        } else {
583
        } else {
553
            $pipe2 = true;
584
            $pipe2 = true;
554
        }
585
        }
555
        while (!(feof($pipes[1]) && (!$pipe2 || feof($pipes[2])))) {
586
        while (!(feof($pipes[1]) && (!$pipe2 || feof($pipes[2])))) {
556
            if ($pipe2) {
587
            if ($pipe2) {
557
                $read = array($pipes[1], $pipes[2]);
588
                $read = array($pipes[1], $pipes[2]);
558
            } else {
589
            } else {
559
                $read = array($pipes[1]);
590
                $read = array($pipes[1]);
560
            }
591
            }
561
 
592
 
562
            $n = stream_select($read, $w, $e, $timeout);
593
            $n = stream_select($read, $w, $e, $timeout);
563
 
594
 
564
            if ($n === false) {
595
            if ($n === false) {
565
                error_log('stream_select: failed !');
596
                error_log('stream_select: failed !');
566
                break;
597
                break;
567
            } elseif ($n === 0) {
598
            } elseif ($n === 0) {
568
                error_log('stream_select: timeout expired !');
599
                error_log('stream_select: timeout expired !');
569
                $te = true;
600
                $te = true;
570
                break;
601
                break;
571
            }
602
            }
572
 
603
 
573
            foreach ($read as $r) {
604
            foreach ($read as $r) {
574
                if ($r == $pipes[1]) {
605
                if ($r == $pipes[1]) {
575
                    $out .= fread($r, 4096);
606
                    $out .= fread($r, 4096);
576
                } elseif (feof($pipes[1]) && $pipe2 && ($r == $pipes[2])) {//read STDERR after STDOUT
607
                } elseif (feof($pipes[1]) && $pipe2 && ($r == $pipes[2])) {//read STDERR after STDOUT
577
                    $err .= fread($r, 4096);
608
                    $err .= fread($r, 4096);
578
                }
609
                }
579
            }
610
            }
580
        }
611
        }
581
 
612
 
582
        return $te;
613
        return $te;
583
    }
614
    }
584
 
615
 
585
    /**
616
    /**
586
     * function for getting a list of values in the specified context
617
     * function for getting a list of values in the specified context
587
     * optionally filter this list, based on the list from third parameter
618
     * optionally filter this list, based on the list from third parameter
588
     *
619
     *
589
     * @param $wmi object holds the COM object that we pull the WMI data from
620
     * @param $wmi object holds the COM object that we pull the WMI data from
590
     * @param string $strClass name of the class where the values are stored
621
     * @param string $strClass name of the class where the values are stored
591
     * @param array  $strValue filter out only needed values, if not set all values of the class are returned
622
     * @param array  $strValue filter out only needed values, if not set all values of the class are returned
592
     *
623
     *
593
     * @return array content of the class stored in an array
624
     * @return array content of the class stored in an array
594
     */
625
     */
595
    public static function getWMI($wmi, $strClass, $strValue = array())
626
    public static function getWMI($wmi, $strClass, $strValue = array())
596
    {
627
    {
597
        $arrData = array();
628
        $arrData = array();
598
        if (gettype($wmi) === "object") {
629
        if (gettype($wmi) === "object") {
599
            $value = "";
630
            $value = "";
600
            try {
631
            try {
601
                $objWEBM = $wmi->Get($strClass);
632
                $objWEBM = $wmi->Get($strClass);
602
                $arrProp = $objWEBM->Properties_;
633
                $arrProp = $objWEBM->Properties_;
603
                $arrWEBMCol = $objWEBM->Instances_();
634
                $arrWEBMCol = $objWEBM->Instances_();
604
                foreach ($arrWEBMCol as $objItem) {
635
                foreach ($arrWEBMCol as $objItem) {
605
                    if (is_array($arrProp)) {
636
                    if (is_array($arrProp)) {
606
                        reset($arrProp);
637
                        reset($arrProp);
607
                    }
638
                    }
608
                    $arrInstance = array();
639
                    $arrInstance = array();
609
                    foreach ($arrProp as $propItem) {
640
                    foreach ($arrProp as $propItem) {
610
                        $value = $objItem->{$propItem->Name}; //instead exploitable eval("\$value = \$objItem->".$propItem->Name.";");
641
                        $value = $objItem->{$propItem->Name}; //instead exploitable eval("\$value = \$objItem->".$propItem->Name.";");
611
                        if (empty($strValue)) {
642
                        if (empty($strValue)) {
612
                            if (is_string($value)) $arrInstance[$propItem->Name] = trim($value);
643
                            if (is_string($value)) $arrInstance[$propItem->Name] = trim($value);
613
                            else $arrInstance[$propItem->Name] = $value;
644
                            else $arrInstance[$propItem->Name] = $value;
614
                        } else {
645
                        } else {
615
                            if (in_array($propItem->Name, $strValue)) {
646
                            if (in_array($propItem->Name, $strValue)) {
616
                                if (is_string($value)) $arrInstance[$propItem->Name] = trim($value);
647
                                if (is_string($value)) $arrInstance[$propItem->Name] = trim($value);
617
                                else $arrInstance[$propItem->Name] = $value;
648
                                else $arrInstance[$propItem->Name] = $value;
618
                            }
649
                            }
619
                        }
650
                        }
620
                    }
651
                    }
621
                    $arrData[] = $arrInstance;
652
                    $arrData[] = $arrInstance;
622
                }
653
                }
623
            } catch (Exception $e) {
654
            } catch (Exception $e) {
624
                if (PSI_DEBUG) {
655
                if (PSI_DEBUG) {
625
                    $error = PSI_Error::singleton();
656
                    $error = PSI_Error::singleton();
626
                    $error->addError("getWMI()", preg_replace('/<br\/>/', "\n", preg_replace('/<b>|<\/b>/', '', $e->getMessage())));
657
                    $error->addError("getWMI()", preg_replace('/<br\/>/', "\n", preg_replace('/<b>|<\/b>/', '', $e->getMessage())));
627
                }
658
                }
628
            }
659
            }
-
 
660
        } elseif ((gettype($wmi) === "string") && (PSI_OS == 'Linux')) {
-
 
661
            $delimeter = '@@@DELIM@@@';
-
 
662
            if (self::executeProgram('wmic', '--delimiter="'.$delimeter.'" '.$wmi.' '.$strClass.'" 2>/dev/null', $strBuf, true) && preg_match("/^CLASS:\s/", $strBuf)) {
-
 
663
                if (self::$_cp) {
-
 
664
                    if (self::$_cp == 932) {
-
 
665
                        $codename = ' (SJIS)';
-
 
666
                    } elseif (self::$_cp == 949) {
-
 
667
                        $codename = ' (EUC-KR)';
-
 
668
                    } elseif (self::$_cp == 950) {
-
 
669
                        $codename = ' (BIG-5)';
-
 
670
                    } else {
-
 
671
                        $codename = '';
-
 
672
                    }
-
 
673
                    self::convertCP($strBuf, 'windows-'.self::$_cp.$codename);
-
 
674
                }
-
 
675
                $lines = preg_split('/\n/', $strBuf, -1, PREG_SPLIT_NO_EMPTY);
-
 
676
                if (count($lines) >=3) {
-
 
677
                    unset($lines[0]);
-
 
678
                    $names = preg_split('/'.$delimeter.'/', $lines[1], -1, PREG_SPLIT_NO_EMPTY);
-
 
679
                    $namesc = count($names);
-
 
680
                    unset($lines[1]);
-
 
681
                    foreach ($lines as $line) {
-
 
682
                        $arrInstance = array();
-
 
683
                        $values = preg_split('/'.$delimeter.'/', $line, -1);
-
 
684
                        if (count($values) == $namesc) {
-
 
685
                            foreach ($values as $id=>$value) {
-
 
686
                                if (empty($strValue)) {
-
 
687
                                    if ($value !== "(null)") $arrInstance[$names[$id]] = trim($value);
-
 
688
                                    else $arrInstance[$names[$id]] = null;
-
 
689
                                } else {
-
 
690
                                    if (in_array($names[$id], $strValue)) {
-
 
691
                                        if ($value !== "(null)") $arrInstance[$names[$id]] = trim($value);
-
 
692
                                        else $arrInstance[$names[$id]] = null;
-
 
693
                                    }
-
 
694
                                }
-
 
695
                            }
-
 
696
                            $arrData[] = $arrInstance;
-
 
697
                        }
-
 
698
                    }
-
 
699
                }
-
 
700
            }
629
        }
701
        }
630
 
702
 
631
        return $arrData;
703
        return $arrData;
632
    }
704
    }
633
 
705
 
634
    /**
706
    /**
635
     * get all configured plugins from phpsysinfo.ini (file must be included and processed before calling this function)
707
     * get all configured plugins from phpsysinfo.ini (file must be included and processed before calling this function)
636
     *
708
     *
637
     * @return array
709
     * @return array
638
     */
710
     */
639
    public static function getPlugins()
711
    public static function getPlugins()
640
    {
712
    {
641
        if (defined('PSI_PLUGINS') && is_string(PSI_PLUGINS)) {
713
        if (defined('PSI_PLUGINS') && is_string(PSI_PLUGINS)) {
642
            if (preg_match(ARRAY_EXP, PSI_PLUGINS)) {
714
            if (preg_match(ARRAY_EXP, PSI_PLUGINS)) {
643
                return eval(strtolower(PSI_PLUGINS));
715
                return eval(strtolower(PSI_PLUGINS));
644
            } else {
716
            } else {
645
                return array(strtolower(PSI_PLUGINS));
717
                return array(strtolower(PSI_PLUGINS));
646
            }
718
            }
647
        } else {
719
        } else {
648
            return array();
720
            return array();
649
        }
721
        }
650
    }
722
    }
651
 
723
 
652
    /**
724
    /**
653
     * name natural compare function
725
     * name natural compare function
654
     *
726
     *
655
     * @return comprasion result
727
     * @return comprasion result
656
     */
728
     */
657
    public static function name_natural_compare($a, $b)
729
    public static function name_natural_compare($a, $b)
658
    {
730
    {
659
        return strnatcmp($a->getName(), $b->getName());
731
        return strnatcmp($a->getName(), $b->getName());
660
    }
732
    }
661
 
733
 
662
    /**
734
    /**
663
     * readReg function
735
     * readReg function
664
     *
736
     *
665
     * @return boolean command successfull or not
737
     * @return boolean command successfull or not
666
     */
738
     */
667
    public static function readReg($reg, $strName, &$strBuffer, $booErrorRep = true)
739
    public static function readReg($reg, $strName, &$strBuffer, $booErrorRep = true)
668
    {
740
    {
-
 
741
        $arrBuffer = array();
669
        $strBuffer = '';
742
        $_hkey = array('HKEY_CLASSES_ROOT'=>0x80000000, 'HKEY_CURRENT_USER'=>0x80000001, 'HKEY_LOCAL_MACHINE'=>0x80000002, 'HKEY_USERS'=>0x80000003, 'HKEY_PERFORMANCE_DATA'=>0x80000004, 'HKEY_PERFORMANCE_TEXT'=>0x80000050, 'HKEY_PERFORMANCE_NLSTEXT'=>0x80000060, 'HKEY_CURRENT_CONFIG'=>0x80000005, 'HKEY_DYN_DATA'=>0x80000006);
-
 
743
 
670
        if ($reg === false) {
744
        if ($reg === false) {
-
 
745
            if (defined('PSI_EMU_HOSTNAME')) {
-
 
746
                return false;
-
 
747
            }
671
            $last = strrpos($strName, "\\");
748
            $last = strrpos($strName, "\\");
672
            $keyname = substr($strName, $last + 1);
749
            $keyname = substr($strName, $last + 1);
673
            if (CommonFunctions::$_cp) {
750
            if (self::$_cp) {
674
                if (CommonFunctions::executeProgram('cmd', '/c chcp '.CommonFunctions::$_cp.' && reg query "'.substr($strName, 0, $last).'" /v '.$keyname.' 2>&1', $strBuf, $booErrorRep) && (strlen($strBuf) > 0) && preg_match("/^\s*".$keyname."\s+REG_\S+\s+(.+)\s*$/mi", $strBuf, $buffer2)) {
751
                if (self::executeProgram('cmd', '/c chcp '.self::$_cp.' >nul & reg query "'.substr($strName, 0, $last).'" /v '.$keyname.' 2>&1', $strBuf, $booErrorRep) && (strlen($strBuf) > 0) && preg_match("/^\s*".$keyname."\s+REG_\S+\s+(.+)\s*$/mi", $strBuf, $buffer2)) {
675
                    $strBuffer = $buffer2[1];
752
                    $strBuffer = $buffer2[1];
676
                } else {
753
                } else {
677
                    return false;
754
                    return false;
678
                }
755
                }
679
            } else {
756
            } else {
680
                if (CommonFunctions::executeProgram('reg', 'query "'.substr($strName, 0, $last).'" /v '.$keyname.' 2>&1', $strBuf, $booErrorRep) && (strlen($strBuf) > 0) && preg_match("/^\s*".$keyname."\s+REG_\S+\s+(.+)\s*$/mi", $strBuf, $buffer2)) {
757
                if (self::executeProgram('reg', 'query "'.substr($strName, 0, $last).'" /v '.$keyname.' 2>&1', $strBuf, $booErrorRep) && (strlen($strBuf) > 0) && preg_match("/^\s*".$keyname."\s+REG_\S+\s+(.+)\s*$/mi", $strBuf, $buffer2)) {
681
                    $strBuffer = $buffer2[1];
758
                    $strBuffer = $buffer2[1];
682
                } else {
759
                } else {
683
                    return false;
760
                    return false;
684
                }
761
                }
685
            }
762
            }
686
        } elseif (gettype($reg) === "object") {
763
        } elseif (gettype($reg) === "object") {
-
 
764
            $first = strpos($strName, "\\");
-
 
765
            $last = strrpos($strName, "\\");
-
 
766
            $hkey = substr($strName, 0, $first);
687
            try {
767
            if (isset($_hkey[$hkey])) {
688
                $strBuffer = $reg->RegRead($strName);
768
                $sub_keys = new VARIANT();
-
 
769
                try {
-
 
770
                    $reg->Get("StdRegProv")->GetStringValue(strval($_hkey[$hkey]), substr($strName, $first+1, $last-$first-1), substr($strName, $last+1), $sub_keys);
689
            } catch (Exception $e) {
771
                } catch (Exception $e) {
690
                if ($booErrorRep) {
772
                    if ($booErrorRep) {
691
                    $error = PSI_Error::singleton();
773
                        $error = PSI_Error::singleton();
692
                    $error->addError("readReg()", preg_replace('/<br\/>/', "\n", preg_replace('/<b>|<\/b>/', '', $e->getMessage())));
774
                        $error->addError("GetStringValue()", preg_replace('/<br\/>/', "\n", preg_replace('/<b>|<\/b>/', '', $e->getMessage())));
693
                }
775
                    }
694
 
776
 
-
 
777
                    return false;
-
 
778
                }
-
 
779
                if (variant_get_type($sub_keys) !== VT_NULL) {
-
 
780
                    $strBuffer = strval($sub_keys);
-
 
781
                } else {
-
 
782
                    return false;
-
 
783
                }
-
 
784
            } else {
695
                return false;
785
               return false;
696
            }
786
            }
697
        }
787
        }
698
 
788
 
699
        return true;
789
        return true;
700
    }
790
    }
701
 
791
 
702
 
-
 
703
    /**
792
    /**
704
     * enumKey function
793
     * enumKey function
705
     *
794
     *
706
     * @return boolean command successfull or not
795
     * @return boolean command successfull or not
707
     */
796
     */
708
    public static function enumKey($key, $strName, &$arrBuffer, $booErrorRep = true)
797
    public static function enumKey($reg, $strName, &$arrBuffer, $booErrorRep = true)
709
    {
798
    {
-
 
799
        $arrBuffer = array();
710
        $_hkey = array('HKEY_CLASSES_ROOT'=>0x80000000, 'HKEY_CURRENT_USER'=>0x80000001, 'HKEY_LOCAL_MACHINE'=>0x80000002, 'HKEY_USERS'=>0x80000003, 'HKEY_PERFORMANCE_DATA'=>0x80000004, 'HKEY_PERFORMANCE_TEXT'=>0x80000050, 'HKEY_PERFORMANCE_NLSTEXT'=>0x80000060, 'HKEY_CURRENT_CONFIG'=>0x80000005, 'HKEY_DYN_DATA'=>0x80000006);
800
        $_hkey = array('HKEY_CLASSES_ROOT'=>0x80000000, 'HKEY_CURRENT_USER'=>0x80000001, 'HKEY_LOCAL_MACHINE'=>0x80000002, 'HKEY_USERS'=>0x80000003, 'HKEY_PERFORMANCE_DATA'=>0x80000004, 'HKEY_PERFORMANCE_TEXT'=>0x80000050, 'HKEY_PERFORMANCE_NLSTEXT'=>0x80000060, 'HKEY_CURRENT_CONFIG'=>0x80000005, 'HKEY_DYN_DATA'=>0x80000006);
711
 
801
 
712
        $arrBuffer = array();
802
        if ($reg === false) {
-
 
803
            if (defined('PSI_EMU_HOSTNAME')) {
713
        if ($key === false) {
804
                return false;
-
 
805
            }
714
            if (CommonFunctions::$_cp) {
806
            if (self::$_cp) {
715
                if (CommonFunctions::executeProgram('cmd', '/c chcp '.CommonFunctions::$_cp.' && reg query "'.$strName.'" 2>&1', $strBuf, $booErrorRep) && (strlen($strBuf) > 0) && preg_match_all("/^".preg_replace("/\\\\/", "\\\\\\\\", $strName)."\\\\(.*)/mi", $strBuf, $buffer2)) {
807
                if (self::executeProgram('cmd', '/c chcp '.self::$_cp.' >nul & reg query "'.$strName.'" 2>&1', $strBuf, $booErrorRep) && (strlen($strBuf) > 0) && preg_match_all("/^".preg_replace("/\\\\/", "\\\\\\\\", $strName)."\\\\(.*)/mi", $strBuf, $buffer2)) {
716
                    foreach ($buffer2[1] as $sub_key) {
808
                    foreach ($buffer2[1] as $sub_key) {
717
                        $arrBuffer[] = trim($sub_key);
809
                        $arrBuffer[] = trim($sub_key);
718
                    }
810
                    }
719
                } else {
811
                } else {
720
                    return false;
812
                    return false;
721
                }
813
                }
722
            } else {
814
            } else {
723
                if (CommonFunctions::executeProgram('reg', 'query "'.$strName.'" 2>&1', $strBuf, $booErrorRep) && (strlen($strBuf) > 0) && preg_match_all("/^".preg_replace("/\\\\/", "\\\\\\\\", $strName)."\\\\(.*)/mi", $strBuf, $buffer2)) {
815
                if (self::executeProgram('reg', 'query "'.$strName.'" 2>&1', $strBuf, $booErrorRep) && (strlen($strBuf) > 0) && preg_match_all("/^".preg_replace("/\\\\/", "\\\\\\\\", $strName)."\\\\(.*)/mi", $strBuf, $buffer2)) {
724
                    foreach ($buffer2[1] as $sub_key) {
816
                    foreach ($buffer2[1] as $sub_key) {
725
                        $arrBuffer[] = trim($sub_key);
817
                        $arrBuffer[] = trim($sub_key);
726
                    }
818
                    }
727
                } else {
819
                } else {
728
                    return false;
820
                    return false;
729
                }
821
                }
730
            }
822
            }
731
        } elseif (gettype($key) === "object") {
823
        } elseif (gettype($reg) === "object") {
732
            $first = strpos($strName, "\\");
824
            $first = strpos($strName, "\\");
733
            $hkey = substr($strName, 0, $first);
825
            $hkey = substr($strName, 0, $first);
734
            if (isset($_hkey[$hkey])) {
826
            if (isset($_hkey[$hkey])) {
735
                $sub_keys = new VARIANT();
827
                $sub_keys = new VARIANT();
736
                try {
828
                try {
737
                   $key->EnumKey(strval($_hkey[$hkey]), substr($strName, $first+1), $sub_keys);
829
                   $reg->Get("StdRegProv")->EnumKey(strval($_hkey[$hkey]), substr($strName, $first+1), $sub_keys);
738
                } catch (Exception $e) {
830
                } catch (Exception $e) {
739
                    if ($booErrorRep) {
831
                    if ($booErrorRep) {
740
                        $error = PSI_Error::singleton();
832
                        $error = PSI_Error::singleton();
741
                        $error->addError("enumKey()", preg_replace('/<br\/>/', "\n", preg_replace('/<b>|<\/b>/', '', $e->getMessage())));;
833
                        $error->addError("enumKey()", preg_replace('/<br\/>/', "\n", preg_replace('/<b>|<\/b>/', '', $e->getMessage())));;
742
                    }
834
                    }
743
 
835
 
744
                    return false;
836
                    return false;
745
                }
837
                }
746
                foreach ($sub_keys as $sub_key) {
838
                if (variant_get_type($sub_keys) !== VT_NULL) foreach ($sub_keys as $sub_key) {
747
                    $arrBuffer[] = $sub_key;
839
                    $arrBuffer[] = $sub_key;
-
 
840
                } else {
-
 
841
                    return false;
748
                }
842
                }
749
            } else {
843
            } else {
750
               return false;
844
               return false;
751
            }
845
            }
752
        }
846
        }
753
 
847
 
754
        return true;
848
        return true;
755
    }
849
    }
-
 
850
 
-
 
851
 
-
 
852
    /**
-
 
853
     * initWMI function
-
 
854
     *
-
 
855
     * @return string, object or false
-
 
856
     */
-
 
857
    public static function initWMI($namespace, $booErrorRep = false)
-
 
858
    {
-
 
859
        $wmi = false;
-
 
860
        try {
-
 
861
            if (PSI_OS == 'Linux') {
-
 
862
                if (defined('PSI_EMU_HOSTNAME'))
-
 
863
                    $wmi = '--namespace="'.$namespace.'" -U '.PSI_EMU_USER.'%'.PSI_EMU_PASSWORD.' //'.PSI_EMU_HOSTNAME.' "select * from';
-
 
864
            } elseif (PSI_OS == 'WINNT') {
-
 
865
                $objLocator = new COM('WbemScripting.SWbemLocator');
-
 
866
                if (defined('PSI_EMU_HOSTNAME'))
-
 
867
                    $wmi = $objLocator->ConnectServer(PSI_EMU_HOSTNAME, $namespace, PSI_EMU_USER, PSI_EMU_PASSWORD);
-
 
868
                else
-
 
869
                    $wmi = $objLocator->ConnectServer('', $namespace);
-
 
870
            }
-
 
871
        } catch (Exception $e) {
-
 
872
            if ($booErrorRep) {
-
 
873
                $error = PSI_Error::singleton();
-
 
874
                $error->addError("WMI connect ".$namespace." error", "PhpSysInfo can not connect to the WMI interface for security reasons.\nCheck an authentication mechanism for the directory where phpSysInfo is installed or credentials.");
-
 
875
            }
-
 
876
        }
-
 
877
 
-
 
878
        return $wmi;
-
 
879
    }
-
 
880
 
-
 
881
    /**
-
 
882
     * convertCP function
-
 
883
     *
-
 
884
     * @return void
-
 
885
     */
-
 
886
    public static function convertCP(&$strBuf, $encoding)
-
 
887
    {
-
 
888
        if (defined('PSI_SYSTEM_CODEPAGE') && ($encoding != null) && ($encoding != PSI_SYSTEM_CODEPAGE)) {
-
 
889
            $systemcp = PSI_SYSTEM_CODEPAGE;
-
 
890
            if (preg_match("/^windows-\d+ \((.+)\)$/", $systemcp, $buf)) {
-
 
891
                $systemcp = $buf[1];
-
 
892
            }
-
 
893
            if (preg_match("/^windows-\d+ \((.+)\)$/", $encoding, $buf)) {
-
 
894
                $encoding = $buf[1];
-
 
895
            }
-
 
896
            $enclist = mb_list_encodings();
-
 
897
            if (in_array($encoding, $enclist) && in_array($systemcp, $enclist)) {
-
 
898
                $strBuf = mb_convert_encoding($strBuf, $encoding, $systemcp);
-
 
899
            } elseif (function_exists("iconv")) {
-
 
900
                if (($iconvout=iconv($systemcp, $encoding.'//IGNORE', $strBuf))!==false) {
-
 
901
                    $strBuf = $iconvout;
-
 
902
                }
-
 
903
            } elseif (function_exists("libiconv") && (($iconvout=libiconv($systemcp, $encoding, $strBuf))!==false)) {
-
 
904
                $strBuf = $iconvout;
-
 
905
            }
-
 
906
        }
-
 
907
    }
756
}
908
}
757
 
909