Subversion Repositories ALCASAR

Rev

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

Rev 3037 Rev 3100
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 dmi memory data
29
     * holds dmi memory data
30
     *
30
     *
31
     * @var array
31
     * @var array
32
     */
32
     */
33
    private static $_dmimd = null;
33
    private static $_dmimd = null;
34
 
34
 
35
    private static function _parse_log_file($string)
35
    private static function _parse_log_file($string)
36
    {
36
    {
37
        if (defined('PSI_LOG') && is_string(PSI_LOG) && (strlen(PSI_LOG)>0) && ((substr(PSI_LOG, 0, 1)=="-") || (substr(PSI_LOG, 0, 1)=="+"))) {
37
        if (defined('PSI_LOG') && is_string(PSI_LOG) && (strlen(PSI_LOG)>0) && ((substr(PSI_LOG, 0, 1)=="-") || (substr(PSI_LOG, 0, 1)=="+"))) {
38
            $log_file = substr(PSI_LOG, 1);
38
            $log_file = substr(PSI_LOG, 1);
39
            if (file_exists($log_file)) {
39
            if (file_exists($log_file)) {
40
                $contents = @file_get_contents($log_file);
40
                $contents = @file_get_contents($log_file);
41
                if ($contents && preg_match("/^\-\-\-[^-\r\n]+\-\-\- ".preg_quote($string, '/')."\r?\n/m", $contents, $matches, PREG_OFFSET_CAPTURE)) {
41
                if ($contents && preg_match("/^\-\-\-[^-\r\n]+\-\-\- ".preg_quote($string, '/')."\r?\n/m", $contents, $matches, PREG_OFFSET_CAPTURE)) {
42
                    $findIndex = $matches[0][1];
42
                    $findIndex = $matches[0][1];
43
                    if (preg_match("/\r?\n/m", $contents, $matches, PREG_OFFSET_CAPTURE, $findIndex)) {
43
                    if (preg_match("/\r?\n/m", $contents, $matches, PREG_OFFSET_CAPTURE, $findIndex)) {
44
                        $startIndex = $matches[0][1]+1;
44
                        $startIndex = $matches[0][1]+1;
45
                        if (preg_match("/^\-\-\-[^-\r\n]+\-\-\- /m", $contents, $matches, PREG_OFFSET_CAPTURE, $startIndex)) {
45
                        if (preg_match("/^\-\-\-[^-\r\n]+\-\-\- /m", $contents, $matches, PREG_OFFSET_CAPTURE, $startIndex)) {
46
                            $stopIndex = $matches[0][1];
46
                            $stopIndex = $matches[0][1];
47
 
47
 
48
                            return substr($contents, $startIndex, $stopIndex-$startIndex);
48
                            return substr($contents, $startIndex, $stopIndex-$startIndex);
49
                        } else {
49
                        } else {
50
                            return substr($contents, $startIndex);
50
                            return substr($contents, $startIndex);
51
                        }
51
                        }
52
                    }
52
                    }
53
                }
53
                }
54
            }
54
            }
55
        }
55
        }
56
 
56
 
57
        return false;
57
        return false;
58
    }
58
    }
59
 
59
 
60
    /**
60
    /**
61
     * Find a system program, do also path checking when not running on WINNT
61
     * Find a system program, do also path checking when not running on WINNT
62
     * on WINNT we simply return the name with the exe extension to the program name
62
     * on WINNT we simply return the name with the exe extension to the program name
63
     *
63
     *
64
     * @param string $strProgram name of the program
64
     * @param string $strProgram name of the program
65
     *
65
     *
66
     * @return string|null complete path and name of the program
66
     * @return string|null complete path and name of the program
67
     */
67
     */
68
    public static function _findProgram($strProgram)
68
    public static function _findProgram($strProgram)
69
    {
69
    {
70
        $path_parts = pathinfo($strProgram);
70
        $path_parts = pathinfo($strProgram);
71
        if (empty($path_parts['basename'])) {
71
        if (empty($path_parts['basename'])) {
72
            return null;
72
            return null;
73
        }
73
        }
74
        $arrPath = array();
74
        $arrPath = array();
75
 
75
 
76
        if (empty($path_parts['dirname']) || ($path_parts['dirname'] == '.')) {
76
        if (empty($path_parts['dirname']) || ($path_parts['dirname'] == '.')) {
77
            if ((PSI_OS == 'WINNT') && empty($path_parts['extension'])) {
77
            if ((PSI_OS == 'WINNT') && empty($path_parts['extension'])) {
78
                $strProgram .= '.exe';
78
                $strProgram .= '.exe';
79
                $path_parts = pathinfo($strProgram);
79
                $path_parts = pathinfo($strProgram);
80
            }
80
            }
81
            if (PSI_OS == 'WINNT') {
81
            if (PSI_OS == 'WINNT') {
82
                if (self::readenv('Path', $serverpath)) {
82
                if (self::readenv('Path', $serverpath)) {
83
                    $arrPath = preg_split('/;/', $serverpath, -1, PREG_SPLIT_NO_EMPTY);
83
                    $arrPath = preg_split('/;/', $serverpath, -1, PREG_SPLIT_NO_EMPTY);
84
                }
84
                }
85
            } else {
85
            } else {
86
                if (self::readenv('PATH', $serverpath)) {
86
                if (self::readenv('PATH', $serverpath)) {
87
                    $arrPath = preg_split('/:/', $serverpath, -1, PREG_SPLIT_NO_EMPTY);
87
                    $arrPath = preg_split('/:/', $serverpath, -1, PREG_SPLIT_NO_EMPTY);
88
                }
88
                }
89
            }
89
            }
90
            if (defined('PSI_UNAMEO') && (PSI_UNAMEO === 'Android') && !empty($arrPath)) {
90
            if (defined('PSI_UNAMEO') && (PSI_UNAMEO === 'Android') && !empty($arrPath)) {
91
                array_push($arrPath, '/system/bin'); // Termux patch
91
                array_push($arrPath, '/system/bin'); // Termux patch
92
            }
92
            }
93
            if (defined('PSI_ADD_PATHS') && is_string(PSI_ADD_PATHS)) {
93
            if (defined('PSI_ADD_PATHS') && is_string(PSI_ADD_PATHS)) {
94
                if (preg_match(ARRAY_EXP, PSI_ADD_PATHS)) {
94
                if (preg_match(ARRAY_EXP, PSI_ADD_PATHS)) {
95
                    $arrPath = array_merge(eval(PSI_ADD_PATHS), $arrPath); // In this order so $addpaths is before $arrPath when looking for a program
95
                    $arrPath = array_merge(eval(PSI_ADD_PATHS), $arrPath); // In this order so $addpaths is before $arrPath when looking for a program
96
                } else {
96
                } else {
97
                    $arrPath = array_merge(array(PSI_ADD_PATHS), $arrPath); // In this order so $addpaths is before $arrPath when looking for a program
97
                    $arrPath = array_merge(array(PSI_ADD_PATHS), $arrPath); // In this order so $addpaths is before $arrPath when looking for a program
98
                }
98
                }
99
            }
99
            }
100
        } else { //directory defined
100
        } else { //directory defined
101
            array_push($arrPath, $path_parts['dirname']);
101
            array_push($arrPath, $path_parts['dirname']);
102
            $strProgram = $path_parts['basename'];
102
            $strProgram = $path_parts['basename'];
103
        }
103
        }
104
 
104
 
105
        //add some default paths if we still have no paths here
105
        //add some default paths if we still have no paths here
106
        if (empty($arrPath) && (PSI_OS != 'WINNT')) {
106
        if (empty($arrPath) && (PSI_OS != 'WINNT')) {
107
            if (PSI_OS == 'Android') {
107
            if (PSI_OS == 'Android') {
108
                array_push($arrPath, '/system/bin');
108
                array_push($arrPath, '/system/bin');
109
            } else {
109
            } else {
110
                array_push($arrPath, '/bin', '/sbin', '/usr/bin', '/usr/sbin', '/usr/local/bin', '/usr/local/sbin');
110
                array_push($arrPath, '/bin', '/sbin', '/usr/bin', '/usr/sbin', '/usr/local/bin', '/usr/local/sbin');
111
            }
111
            }
112
        }
112
        }
113
 
113
 
114
        $exceptPath = "";
114
        $exceptPath = "";
115
        if ((PSI_OS == 'WINNT') && self::readenv('WinDir', $windir)) {
115
        if ((PSI_OS == 'WINNT') && self::readenv('WinDir', $windir)) {
116
            foreach ($arrPath as $strPath) {
116
            foreach ($arrPath as $strPath) {
117
                if ((strtolower($strPath) == strtolower($windir)."\\system32") && is_dir($windir."\\SysWOW64")) {
117
                if ((strtolower($strPath) == strtolower($windir)."\\system32") && is_dir($windir."\\SysWOW64")) {
118
                    if (is_dir($windir."\\sysnative\\drivers")) { // or strlen(decbin(~0)) == 32; is_dir($windir."\\sysnative") sometimes does not work
118
                    if (is_dir($windir."\\sysnative\\drivers")) { // or strlen(decbin(~0)) == 32; is_dir($windir."\\sysnative") sometimes does not work
119
                        $exceptPath = $windir."\\sysnative"; //32-bit PHP on 64-bit Windows
119
                        $exceptPath = $windir."\\sysnative"; //32-bit PHP on 64-bit Windows
120
                    } else {
120
                    } else {
121
                        $exceptPath = $windir."\\SysWOW64"; //64-bit PHP on 64-bit Windows
121
                        $exceptPath = $windir."\\SysWOW64"; //64-bit PHP on 64-bit Windows
122
                    }
122
                    }
123
                    array_push($arrPath, $exceptPath);
123
                    array_push($arrPath, $exceptPath);
124
                    break;
124
                    break;
125
                }
125
                }
126
            }
126
            }
127
        } elseif (PSI_OS == 'Android') {
127
        } elseif (PSI_OS == 'Android') {
128
            $exceptPath = '/system/bin';
128
            $exceptPath = '/system/bin';
129
        }
129
        }
130
 
130
 
131
        foreach ($arrPath as $strPath) {
131
        foreach ($arrPath as $strPath) {
132
            // Path with and without trailing slash
132
            // Path with and without trailing slash
133
            if (PSI_OS == 'WINNT') {
133
            if (PSI_OS == 'WINNT') {
134
                $strPath = rtrim($strPath, "\\");
134
                $strPath = rtrim($strPath, "\\");
135
                $strPathS = $strPath."\\";
135
                $strPathS = $strPath."\\";
136
            } else {
136
            } else {
137
                $strPath = rtrim($strPath, "/");
137
                $strPath = rtrim($strPath, "/");
138
                $strPathS = $strPath."/";
138
                $strPathS = $strPath."/";
139
            }
139
            }
140
            if (($strPath !== $exceptPath) && !is_dir($strPath)) {
140
            if (($strPath !== $exceptPath) && !is_dir($strPath)) {
141
                continue;
141
                continue;
142
            }
142
            }
143
            $strProgrammpath = $strPathS.$strProgram;
143
            $strProgrammpath = $strPathS.$strProgram;
144
            if (is_executable($strProgrammpath) || ((PSI_OS == 'WINNT') && (strtolower($path_parts['extension']) == 'py') && is_file($strProgrammpath))) {
144
            if (is_executable($strProgrammpath) || ((PSI_OS == 'WINNT') && (strtolower($path_parts['extension']) == 'py') && is_file($strProgrammpath))) {
145
                return $strProgrammpath;
145
                return $strProgrammpath;
146
            }
146
            }
147
        }
147
        }
148
 
148
 
149
        return null;
149
        return null;
150
    }
150
    }
151
 
151
 
152
    /**
152
    /**
153
     * Execute a system program. return a trim()'d result.
153
     * Execute a system program. return a trim()'d result.
154
     * does very crude pipe and multiple commands (on WinNT) checking.  you need ' | ' or ' & ' for it to work
154
     * does very crude pipe and multiple commands (on WinNT) checking.  you need ' | ' or ' & ' for it to work
155
     * ie $program = CommonFunctions::executeProgram('netstat', '-anp | grep LIST');
155
     * ie $program = CommonFunctions::executeProgram('netstat', '-anp | grep LIST');
156
     * NOT $program = CommonFunctions::executeProgram('netstat', '-anp|grep LIST');
156
     * NOT $program = CommonFunctions::executeProgram('netstat', '-anp|grep LIST');
157
     *
157
     *
158
     * @param string  $strProgramname name of the program
158
     * @param string  $strProgramname name of the program
159
     * @param string  $strArgs        arguments to the program
159
     * @param string  $strArguments   arguments to the program
160
     * @param string  &$strBuffer     output of the command
160
     * @param string  &$strBuffer     output of the command
161
     * @param boolean $booErrorRep    en- or disables the reporting of errors which should be logged
161
     * @param boolean $booErrorRep    en- or disables the reporting of errors which should be logged
162
     * @param int     $timeout        timeout value in seconds (default value is PSI_EXEC_TIMEOUT_INT)
162
     * @param int     $timeout        timeout value in seconds (default value is PSI_EXEC_TIMEOUT_INT)
163
     *
163
     *
164
     * @return boolean command successfull or not
164
     * @return boolean command successfull or not
165
     */
165
     */
166
    public static function executeProgram($strProgramname, $strArgs, &$strBuffer, $booErrorRep = true, $timeout = PSI_EXEC_TIMEOUT_INT)
166
    public static function executeProgram($strProgramname, $strArguments, &$strBuffer, $booErrorRep = true, $timeout = PSI_EXEC_TIMEOUT_INT, $separator = '')
167
    {
167
    {
-
 
168
        if (PSI_ROOT_FILESYSTEM !== '') { // disabled if ROOTFS defined
-
 
169
 
-
 
170
            return false;
-
 
171
        }
-
 
172
 
-
 
173
        if ((PSI_OS != 'WINNT') && preg_match('/^([^=]+=[^ \t]+)[ \t]+(.*)$/', $strProgramname, $strmatch)) {
-
 
174
            $strSet = $strmatch[1].' ';
-
 
175
            $strProgramname = $strmatch[2];
-
 
176
        } else {
-
 
177
            $strSet = '';
-
 
178
        }
-
 
179
        $strAll = trim($strSet.$strProgramname.' '.$strArguments);
-
 
180
 
168
        if (defined('PSI_LOG') && is_string(PSI_LOG) && (strlen(PSI_LOG)>0) && ((substr(PSI_LOG, 0, 1)=="-") || (substr(PSI_LOG, 0, 1)=="+"))) {
181
        if (defined('PSI_LOG') && is_string(PSI_LOG) && (strlen(PSI_LOG)>0) && ((substr(PSI_LOG, 0, 1)=="-") || (substr(PSI_LOG, 0, 1)=="+"))) {
169
            $out = self::_parse_log_file("Executing: ".trim($strProgramname.' '.$strArgs));
182
            $out = self::_parse_log_file("Executing: ".$strAll);
170
            if ($out == false) {
183
            if ($out == false) {
171
                if (substr(PSI_LOG, 0, 1)=="-") {
184
                if (substr(PSI_LOG, 0, 1)=="-") {
172
                    $strBuffer = '';
185
                    $strBuffer = '';
173
 
186
 
174
                    return false;
187
                    return false;
175
                }
188
                }
176
            } else {
189
            } else {
177
                $strBuffer = $out;
190
                $strBuffer = $out;
178
 
191
 
179
                return true;
192
                return true;
180
            }
193
            }
181
        }
194
        }
182
 
195
 
-
 
196
        $PathStr = '';
-
 
197
        if (defined('PSI_EMU_PORT') && !in_array($strProgramname, array('ping', 'snmpwalk'))) {
183
        if (PSI_ROOT_FILESYSTEM !== '') { // disabled if ROOTFS defined
198
            if (defined('PSI_SUDO_COMMANDS') && is_string(PSI_SUDO_COMMANDS)) {
184
 
-
 
-
 
199
                if (preg_match(ARRAY_EXP, PSI_SUDO_COMMANDS)) {
-
 
200
                    $sudocommands = eval(PSI_SUDO_COMMANDS);
185
            return false;
201
                } else {
-
 
202
                    $sudocommands = array(PSI_SUDO_COMMANDS);
186
        }
203
                }
187
 
-
 
188
        if ((PSI_OS != 'WINNT') && preg_match('/^([^=]+=[^ \t]+)[ \t]+(.*)$/', $strProgramname, $strmatch)) {
204
                if (in_array($strProgramname, $sudocommands)) {
189
            $strSet = $strmatch[1].' ';
205
                    $strAll = 'sudo '.$strAll;
190
            $strProgramname = $strmatch[2];
206
                }
191
        } else {
207
            }
192
            $strSet = '';
208
            $strSet = '';
-
 
209
            $strProgramname = 'sshpass';
-
 
210
            $strOptions = '';
-
 
211
            if (defined('PSI_EMU_ADD_OPTIONS') && is_string(PSI_EMU_ADD_OPTIONS)) {
-
 
212
                if (preg_match(ARRAY_EXP, PSI_EMU_ADD_OPTIONS)) {
-
 
213
                    $arrParams = eval(PSI_EMU_ADD_OPTIONS);
-
 
214
                } else {
-
 
215
                    $arrParams = array(PSI_EMU_ADD_OPTIONS);
-
 
216
                }
-
 
217
                foreach ($arrParams as $Params) if (preg_match('/(\S+)\s*\=\s*(\S+)/', $Params, $obuf)) {
-
 
218
                    $strOptions = $strOptions.'-o '.$obuf[1].'='.$obuf[2].' ';
-
 
219
                }
-
 
220
            }
-
 
221
            if (defined('PSI_EMU_ADD_PATHS') && is_string(PSI_EMU_ADD_PATHS)) {
-
 
222
                if (preg_match(ARRAY_EXP, PSI_EMU_ADD_PATHS)) {
-
 
223
                    $arrPath = eval(PSI_EMU_ADD_PATHS);
-
 
224
                } else {
-
 
225
                    $arrPath = array(PSI_EMU_ADD_PATHS);
-
 
226
                }
-
 
227
                foreach ($arrPath as $Path) {
-
 
228
                    if ($PathStr === '') {
-
 
229
                        $PathStr = $Path;
-
 
230
                    } else {
-
 
231
                        $PathStr = $PathStr.':'.$Path;
-
 
232
                    }
-
 
233
                }
-
 
234
                if ($separator === '') {
-
 
235
                    $strArguments = '-e ssh -Tq '.$strOptions.'-o ConnectTimeout='.$timeout.' -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null '.PSI_EMU_USER.'@'.PSI_EMU_HOSTNAME.' -p '.PSI_EMU_PORT.' "PATH=\''.$PathStr.':$PATH\' '.$strAll.'"' ;
-
 
236
                } else {
-
 
237
                    $strArguments = '-e ssh -Tq '.$strOptions.'-o ConnectTimeout='.$timeout.' -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null '.PSI_EMU_USER.'@'.PSI_EMU_HOSTNAME.' -p '.PSI_EMU_PORT;
-
 
238
                }
-
 
239
            } else {
-
 
240
                if ($separator === '') {
-
 
241
                    $strArguments = '-e ssh -Tq '.$strOptions.'-o ConnectTimeout='.$timeout.' -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null '.PSI_EMU_USER.'@'.PSI_EMU_HOSTNAME.' -p '.PSI_EMU_PORT.' "'.$strAll.'"' ;
-
 
242
                } else {
-
 
243
                    $strArguments = '-e ssh -Tq '.$strOptions.'-o ConnectTimeout='.$timeout.' -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null '.PSI_EMU_USER.'@'.PSI_EMU_HOSTNAME.' -p '.PSI_EMU_PORT;
-
 
244
                }
-
 
245
            }
-
 
246
            $externally = true;
-
 
247
        } else {
-
 
248
            $externally = false;
193
        }
249
        }
-
 
250
 
194
        $strProgram = self::_findProgram($strProgramname);
251
        $strProgram = self::_findProgram($strProgramname);
195
        $error = PSI_Error::singleton();
252
        $error = PSI_Error::singleton();
196
        if (!$strProgram) {
253
        if (!$strProgram) {
197
            if ($booErrorRep) {
254
            if ($booErrorRep || $externally) {
198
                $error->addError('find_program("'.$strProgramname.'")', 'program not found on the machine');
255
                $error->addError('find_program("'.$strProgramname.'")', 'program not found on the machine');
199
            }
256
            }
200
 
257
 
201
            return false;
258
            return false;
202
        } else {
259
        } else {
203
            if (preg_match('/\s/', $strProgram)) {
260
            if (preg_match('/\s/', $strProgram)) {
204
                $strProgram = '"'.$strProgram.'"';
261
                $strProgram = '"'.$strProgram.'"';
205
            }
262
            }
206
        }
263
        }
207
 
264
 
208
        if ((PSI_OS != 'WINNT') && defined('PSI_SUDO_COMMANDS') && is_string(PSI_SUDO_COMMANDS)) {
265
        if ((PSI_OS != 'WINNT') && !defined('PSI_EMU_HOSTNAME') && defined('PSI_SUDO_COMMANDS') && is_string(PSI_SUDO_COMMANDS)) {
209
            if (preg_match(ARRAY_EXP, PSI_SUDO_COMMANDS)) {
266
            if (preg_match(ARRAY_EXP, PSI_SUDO_COMMANDS)) {
210
                $sudocommands = eval(PSI_SUDO_COMMANDS);
267
                $sudocommands = eval(PSI_SUDO_COMMANDS);
211
            } else {
268
            } else {
212
                $sudocommands = array(PSI_SUDO_COMMANDS);
269
                $sudocommands = array(PSI_SUDO_COMMANDS);
213
            }
270
            }
214
            if (in_array($strProgramname, $sudocommands)) {
271
            if (in_array($strProgramname, $sudocommands)) {
215
                $sudoProgram = self::_findProgram("sudo");
272
                $sudoProgram = self::_findProgram("sudo");
216
                if (!$sudoProgram) {
273
                if (!$sudoProgram) {
217
                    if ($booErrorRep) {
-
 
218
                        $error->addError('find_program("sudo")', 'program not found on the machine');
274
                    $error->addError('find_program("sudo")', 'program not found on the machine');
219
                    }
-
 
220
 
275
 
221
                    return false;
276
                    return false;
222
                } else {
277
                } else {
223
                    if (preg_match('/\s/', $sudoProgram)) {
278
                    if (preg_match('/\s/', $sudoProgram)) {
224
                        $strProgram = '"'.$sudoProgram.'" '.$strProgram;
279
                        $strProgram = '"'.$sudoProgram.'" '.$strProgram;
225
                    } else {
280
                    } else {
226
                        $strProgram = $sudoProgram.' '.$strProgram;
281
                        $strProgram = $sudoProgram.' '.$strProgram;
227
                    }
282
                    }
228
                }
283
                }
229
            }
284
            }
230
        }
285
        }
231
 
286
 
-
 
287
        $strArgs = $strArguments;
232
        // see if we've gotten a | or &, if we have we need to do path checking on the cmd
288
        // see if we've gotten a | or &, if we have we need to do path checking on the cmd
233
        if ($strArgs) {
289
        if ($strArgs) {
234
            $arrArgs = preg_split('/ /', $strArgs, -1, PREG_SPLIT_NO_EMPTY);
290
            $arrArgs = preg_split('/ /', $strArgs, -1, PREG_SPLIT_NO_EMPTY);
235
            for ($i = 0, $cnt_args = count($arrArgs); $i < $cnt_args; $i++) {
291
            for ($i = 0, $cnt_args = count($arrArgs); $i < $cnt_args; $i++) {
236
                if (($arrArgs[$i] == '|') || ($arrArgs[$i] == '&')) {
292
                if (($arrArgs[$i] == '|') || ($arrArgs[$i] == '&')) {
237
                    $strCmd = $arrArgs[$i + 1];
293
                    $strCmd = $arrArgs[$i + 1];
238
                    $strNewcmd = self::_findProgram($strCmd);
294
                    $strNewcmd = self::_findProgram($strCmd);
-
 
295
                    if (!$strNewcmd) {
-
 
296
                        if ($booErrorRep || $externally) {
-
 
297
                            $error->addError('find_program("'.$strCmd.'")', 'program not found on the machine');
-
 
298
                        }
-
 
299
 
-
 
300
                        return false;
-
 
301
                    }
-
 
302
                    if (preg_match('/\s/', $strNewcmd)) {
239
                    if ($arrArgs[$i] == '|') {
303
                        if ($arrArgs[$i] == '|') {
240
                        $strArgs = preg_replace('/\| '.$strCmd.'/', '| "'.$strNewcmd.'"', $strArgs);
304
                            $strArgs = preg_replace('/\| '.$strCmd.'/', '| "'.$strNewcmd.'"', $strArgs);
-
 
305
                        } else {
-
 
306
                            $strArgs = preg_replace('/& '.$strCmd.'/', '& "'.$strNewcmd.'"', $strArgs);
-
 
307
                        }
241
                    } else {
308
                    } else {
-
 
309
                        if ($arrArgs[$i] == '|') {
-
 
310
                            $strArgs = preg_replace('/\| '.$strCmd.'/', '| '.$strNewcmd, $strArgs);
-
 
311
                        } else {
242
                        $strArgs = preg_replace('/& '.$strCmd.'/', '& "'.$strNewcmd.'"', $strArgs);
312
                            $strArgs = preg_replace('/& '.$strCmd.'/', '& '.$strNewcmd, $strArgs);
-
 
313
                        }
243
                    }
314
                    }
244
                }
315
                }
245
            }
316
            }
246
            $strArgs = ' '.$strArgs;
317
            $strArgs = ' '.$strArgs;
247
        }
318
        }
248
 
319
 
249
        $strBuffer = '';
320
        $strBuffer = '';
250
        $strError = '';
321
        $strError = '';
251
        $pipes = array();
322
        $pipes = array();
252
        $descriptorspec = array(0=>array("pipe", "r"), 1=>array("pipe", "w"), 2=>array("pipe", "w"));
323
        $descriptorspec = array(0=>array("pipe", "r"), 1=>array("pipe", "w"), 2=>array("pipe", "w"));
-
 
324
        if ($externally) {
-
 
325
            putenv('SSHPASS='.PSI_EMU_PASSWORD);
-
 
326
        }
253
        if (defined("PSI_MODE_POPEN") && PSI_MODE_POPEN) {
327
        if (defined("PSI_MODE_POPEN") && PSI_MODE_POPEN) {
-
 
328
            if ($separator !== '') {
-
 
329
                $error->addError('executeProgram', 'wrong execution mode');
-
 
330
 
-
 
331
                return false;
-
 
332
            }
254
            if (PSI_OS == 'WINNT') {
333
            if (PSI_OS == 'WINNT') {
255
                $process = $pipes[1] = popen($strSet.$strProgram.$strArgs." 2>nul", "r");
334
                $process = $pipes[1] = popen($strSet.$strProgram.$strArgs." 2>nul", "r");
256
            } else {
335
            } else {
257
                $process = $pipes[1] = popen($strSet.$strProgram.$strArgs." 2>/dev/null", "r");
336
                $process = $pipes[1] = popen($strSet.$strProgram.$strArgs." 2>/dev/null", "r");
258
            }
337
            }
259
        } else {
338
        } else {
260
            $process = proc_open($strSet.$strProgram.$strArgs, $descriptorspec, $pipes);
339
            $process = proc_open($strSet.$strProgram.$strArgs, $descriptorspec, $pipes);
-
 
340
            if ($separator !== '') {
-
 
341
                if ($PathStr === '') {
-
 
342
                    fwrite($pipes[0], $strAll."\n  "); // spaces at end for handling 'more'
-
 
343
                } else {
-
 
344
                    fwrite($pipes[0], 'PATH=\''.$PathStr.':$PATH\' '.$strAll."\n");
-
 
345
                }
-
 
346
            }
-
 
347
        }
-
 
348
        if ($externally) {
-
 
349
            putenv('SSHPASS');
261
        }
350
        }
262
        if (is_resource($process)) {
351
        if (is_resource($process)) {
263
            $te = self::_timeoutfgets($pipes, $strBuffer, $strError, $timeout);
352
            $te = self::_timeoutfgets($pipes, $strBuffer, $strError, $timeout, $separator);
264
            if (defined("PSI_MODE_POPEN") && PSI_MODE_POPEN) {
353
            if (defined("PSI_MODE_POPEN") && PSI_MODE_POPEN) {
265
                $return_value = pclose($pipes[1]);
354
                $return_value = pclose($pipes[1]);
266
            } else {
355
            } else {
267
                fclose($pipes[0]);
356
                fclose($pipes[0]);
268
                fclose($pipes[1]);
357
                fclose($pipes[1]);
269
                fclose($pipes[2]);
358
                fclose($pipes[2]);
270
                // It is important that you close any pipes before calling
359
                // It is important that you close any pipes before calling
271
                // proc_close in order to avoid a deadlock
360
                // proc_close in order to avoid a deadlock
272
                if ($te) {
361
                if ($te) {
273
                    proc_terminate($process); // proc_close tends to hang if the process is timing out
362
                    proc_terminate($process); // proc_close tends to hang if the process is timing out
274
                    $return_value = 0;
363
                    $return_value = 0;
275
                } else {
364
                } else {
276
                    $return_value = proc_close($process);
365
                    $return_value = proc_close($process);
277
                }
366
                }
278
            }
367
            }
279
        } else {
368
        } else {
280
            if ($booErrorRep) {
369
            if ($booErrorRep) {
281
                $error->addError($strProgram, "\nOpen process error");
370
                $error->addError($strProgram, "\nOpen process error");
282
            }
371
            }
283
 
372
 
284
            return false;
373
            return false;
285
        }
374
        }
286
        $strError = trim($strError);
375
        $strError = trim($strError);
287
        $strBuffer = trim($strBuffer);
376
        $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)!="+")) {
377
        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);
378
            error_log("---".gmdate('r T')."--- Executing: ".$strAll."\n".$strBuffer."\n", 3, PSI_LOG);
290
        }
379
        }
291
        if (! empty($strError)) {
380
        if (! empty($strError)) {
292
            if ($booErrorRep) {
381
            if ($booErrorRep) {
293
                $error->addError($strProgram, $strError."\nReturn value: ".$return_value);
382
                $error->addError($strProgram, $strError."\nReturn value: ".$return_value);
294
            }
383
            }
295
 
384
 
296
            return $return_value == 0;
385
            return $return_value == 0;
297
        }
386
        }
298
 
387
 
299
        return true;
388
        return true;
300
    }
389
    }
301
 
390
 
302
    /**
391
    /**
303
     * read a one-line value from a file with a similar name
392
     * read a one-line value from a file with a similar name
304
     *
393
     *
305
     * @return value if successfull or null if not
394
     * @return value if successfull or null if not
306
     */
395
     */
307
    public static function rolv($similarFileName, $match = "//", $replace = "")
396
    public static function rolv($similarFileName, $match = "//", $replace = "")
308
    {
397
    {
-
 
398
        if (defined('PSI_EMU_PORT')) {
-
 
399
            return null;
-
 
400
        }
-
 
401
 
309
        $filename = preg_replace($match, $replace, $similarFileName);
402
        $filename = preg_replace($match, $replace, $similarFileName);
310
        if (self::fileexists($filename) && self::rfts($filename, $buf, 1, 4096, false) && (($buf=trim($buf)) != "")) {
403
        if (self::fileexists($filename) && self::rfts($filename, $buf, 1, 4096, false) && (($buf=trim($buf)) != "")) {
311
            return $buf;
404
            return $buf;
312
        } else {
405
        } else {
313
            return null;
406
            return null;
314
        }
407
        }
315
    }
408
    }
316
 
409
 
317
    /**
410
    /**
318
     * read data from array $_SERVER
411
     * read data from array $_SERVER
319
     *
412
     *
320
     * @param string $strElem    element of array
413
     * @param string $strElem    element of array
321
     * @param string &$strBuffer output of the command
414
     * @param string &$strBuffer output of the command
322
     *
415
     *
323
     * @return string
416
     * @return string
324
     */
417
     */
325
    public static function readenv($strElem, &$strBuffer)
418
    public static function readenv($strElem, &$strBuffer)
326
    {
419
    {
327
        $strBuffer = '';
420
        $strBuffer = '';
328
        if (PSI_OS == 'WINNT') { //case insensitive
421
        if (PSI_OS == 'WINNT') { //case insensitive
329
            if (isset($_SERVER)) {
422
            if (isset($_SERVER)) {
330
                foreach ($_SERVER as $index=>$value) {
423
                foreach ($_SERVER as $index=>$value) {
331
                    if (is_string($value) && (trim($value) !== '') && (strtolower($index) === strtolower($strElem))) {
424
                    if (is_string($value) && (trim($value) !== '') && (strtolower($index) === strtolower($strElem))) {
332
                        $strBuffer = $value;
425
                        $strBuffer = $value;
333
 
426
 
334
                        return true;
427
                        return true;
335
                    }
428
                    }
336
                }
429
                }
337
            }
430
            }
338
        } else {
431
        } else {
339
            if (isset($_SERVER[$strElem]) && is_string($value = $_SERVER[$strElem]) && (trim($value) !== '')) {
432
            if (isset($_SERVER[$strElem]) && is_string($value = $_SERVER[$strElem]) && (trim($value) !== '')) {
340
                $strBuffer = $value;
433
                $strBuffer = $value;
341
 
434
 
342
                return true;
435
                return true;
343
            }
436
            }
344
        }
437
        }
345
 
438
 
346
        return false;
439
        return false;
347
    }
440
    }
348
 
441
 
349
    /**
442
    /**
350
     * read a file and return the content as a string
443
     * read a file and return the content as a string
351
     *
444
     *
352
     * @param string  $strFileName name of the file which should be read
445
     * @param string  $strFileName name of the file which should be read
353
     * @param string  &$strRet     content of the file (reference)
446
     * @param string  &$strRet     content of the file (reference)
354
     * @param int     $intLines    control how many lines should be read
447
     * @param int     $intLines    control how many lines should be read
355
     * @param int     $intBytes    control how many bytes of each line should be read
448
     * @param int     $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
449
     * @param boolean $booErrorRep en- or disables the reporting of errors which should be logged
357
     *
450
     *
358
     * @return boolean command successfull or not
451
     * @return boolean command successfull or not
359
     */
452
     */
360
    public static function rfts($strFileName, &$strRet, $intLines = 0, $intBytes = 4096, $booErrorRep = true)
453
    public static function rfts($strFileName, &$strRet, $intLines = 0, $intBytes = 4096, $booErrorRep = true)
361
    {
454
    {
-
 
455
        if (defined('PSI_EMU_PORT')) {
-
 
456
            return false;
-
 
457
        }
-
 
458
 
362
        if (defined('PSI_LOG') && is_string(PSI_LOG) && (strlen(PSI_LOG)>0) && ((substr(PSI_LOG, 0, 1)=="-") || (substr(PSI_LOG, 0, 1)=="+"))) {
459
        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);
460
            $out = self::_parse_log_file("Reading: ".$strFileName);
364
            if ($out == false) {
461
            if ($out == false) {
365
                if (substr(PSI_LOG, 0, 1)=="-") {
462
                if (substr(PSI_LOG, 0, 1)=="-") {
366
                    $strRet = '';
463
                    $strRet = '';
367
 
464
 
368
                    return false;
465
                    return false;
369
                }
466
                }
370
            } else {
467
            } else {
371
                $strRet = $out;
468
                $strRet = $out;
372
 
469
 
373
                return true;
470
                return true;
374
            }
471
            }
375
        }
472
        }
376
 
473
 
377
        if (PSI_ROOT_FILESYSTEM !== '') {
474
        if (PSI_ROOT_FILESYSTEM !== '') {
378
            $rfsinfo = "[".PSI_ROOT_FILESYSTEM."]";
475
            $rfsinfo = "[".PSI_ROOT_FILESYSTEM."]";
379
        } else {
476
        } else {
380
            $rfsinfo = '';
477
            $rfsinfo = '';
381
        }
478
        }
382
 
479
 
383
        $strFile = "";
480
        $strFile = "";
384
        $intCurLine = 1;
481
        $intCurLine = 1;
385
        $error = PSI_Error::singleton();
482
        $error = PSI_Error::singleton();
386
        if (file_exists(PSI_ROOT_FILESYSTEM.$strFileName)) {
483
        if (file_exists(PSI_ROOT_FILESYSTEM.$strFileName)) {
387
            if (is_readable(PSI_ROOT_FILESYSTEM.$strFileName)) {
484
            if (is_readable(PSI_ROOT_FILESYSTEM.$strFileName)) {
388
                if ($fd = fopen(PSI_ROOT_FILESYSTEM.$strFileName, 'r')) {
485
                if ($fd = fopen(PSI_ROOT_FILESYSTEM.$strFileName, 'r')) {
389
                    while (!feof($fd)) {
486
                    while (!feof($fd)) {
390
                        $strFile .= fgets($fd, $intBytes);
487
                        $strFile .= fgets($fd, $intBytes);
391
                        if ($intLines <= $intCurLine && $intLines != 0) {
488
                        if ($intLines <= $intCurLine && $intLines != 0) {
392
                            break;
489
                            break;
393
                        } else {
490
                        } else {
394
                            $intCurLine++;
491
                            $intCurLine++;
395
                        }
492
                        }
396
                    }
493
                    }
397
                    fclose($fd);
494
                    fclose($fd);
398
                    $strRet = $strFile;
495
                    $strRet = $strFile;
399
                    if (defined('PSI_LOG') && is_string(PSI_LOG) && (strlen(PSI_LOG)>0) && (substr(PSI_LOG, 0, 1)!="-") && (substr(PSI_LOG, 0, 1)!="+")) {
496
                    if (defined('PSI_LOG') && is_string(PSI_LOG) && (strlen(PSI_LOG)>0) && (substr(PSI_LOG, 0, 1)!="-") && (substr(PSI_LOG, 0, 1)!="+")) {
400
                        if ((strlen($strRet)>0)&&(substr($strRet, -1)!="\n")) {
497
                        if ((strlen($strRet)>0)&&(substr($strRet, -1)!="\n")) {
401
                            error_log("---".gmdate('r T')."--- Reading: ".$strFileName."\n".$strRet."\n", 3, PSI_LOG);
498
                            error_log("---".gmdate('r T')."--- Reading: ".$strFileName."\n".$strRet."\n", 3, PSI_LOG);
402
                        } else {
499
                        } else {
403
                            error_log("---".gmdate('r T')."--- Reading: ".$strFileName."\n".$strRet, 3, PSI_LOG);
500
                            error_log("---".gmdate('r T')."--- Reading: ".$strFileName."\n".$strRet, 3, PSI_LOG);
404
                        }
501
                        }
405
                    }
502
                    }
406
                } else {
503
                } else {
407
                    if ($booErrorRep) {
504
                    if ($booErrorRep) {
408
                        $error->addError('fopen('.$rfsinfo.$strFileName.')', 'file can not read by phpsysinfo');
505
                        $error->addError('fopen('.$rfsinfo.$strFileName.')', 'file can not read by phpsysinfo');
409
                    }
506
                    }
410
 
507
 
411
                    return false;
508
                    return false;
412
                }
509
                }
413
            } else {
510
            } else {
414
                if ($booErrorRep) {
511
                if ($booErrorRep) {
415
                    $error->addError('fopen('.$rfsinfo.$strFileName.')', 'file permission error');
512
                    $error->addError('fopen('.$rfsinfo.$strFileName.')', 'file permission error');
416
                }
513
                }
417
 
514
 
418
                return false;
515
                return false;
419
            }
516
            }
420
        } else {
517
        } else {
421
            if ($booErrorRep) {
518
            if ($booErrorRep) {
422
                $error->addError('file_exists('.$rfsinfo.$strFileName.')', 'the file does not exist on your machine');
519
                $error->addError('file_exists('.$rfsinfo.$strFileName.')', 'the file does not exist on your machine');
423
            }
520
            }
424
 
521
 
425
            return false;
522
            return false;
426
        }
523
        }
427
 
524
 
428
        return true;
525
        return true;
429
    }
526
    }
430
 
527
 
431
    /**
528
    /**
432
     * read a data file and return the content as a string
529
     * read a data file and return the content as a string
433
     *
530
     *
434
     * @param string $strDataFileName name of the data file which should be read
531
     * @param string $strDataFileName name of the data file which should be read
435
     * @param string &$strRet         content of the data file (reference)
532
     * @param string &$strRet         content of the data file (reference)
436
     *
533
     *
437
     * @return boolean command successfull or not
534
     * @return boolean command successfull or not
438
     */
535
     */
439
    public static function rftsdata($strDataFileName, &$strRet)
536
    public static function rftsdata($strDataFileName, &$strRet)
440
    {
537
    {
441
        $strFile = "";
538
        $strFile = "";
442
        $strFileName = PSI_APP_ROOT."/data/".$strDataFileName;
539
        $strFileName = PSI_APP_ROOT."/data/".$strDataFileName;
443
        $error = PSI_Error::singleton();
540
        $error = PSI_Error::singleton();
444
        if (file_exists($strFileName)) {
541
        if (file_exists($strFileName)) {
445
            if (is_readable($strFileName)) {
542
            if (is_readable($strFileName)) {
446
                if ($fd = fopen($strFileName, 'r')) {
543
                if ($fd = fopen($strFileName, 'r')) {
447
                    while (!feof($fd)) {
544
                    while (!feof($fd)) {
448
                        $strFile .= fgets($fd, 4096);
545
                        $strFile .= fgets($fd, 4096);
449
                    }
546
                    }
450
                    fclose($fd);
547
                    fclose($fd);
451
                    $strRet = $strFile;
548
                    $strRet = $strFile;
452
                } else {
549
                } else {
453
                    $error->addError('fopen('.$strFileName.')', 'file can not read by phpsysinfo');
550
                    $error->addError('fopen('.$strFileName.')', 'file can not read by phpsysinfo');
454
 
551
 
455
                    return false;
552
                    return false;
456
                }
553
                }
457
            } else {
554
            } else {
458
                $error->addError('fopen('.$strFileName.')', 'file permission error');
555
                $error->addError('fopen('.$strFileName.')', 'file permission error');
459
 
556
 
460
                return false;
557
                return false;
461
            }
558
            }
462
        } else {
559
        } else {
463
            $error->addError('file_exists('.$strFileName.')', 'the file does not exist on your machine');
560
            $error->addError('file_exists('.$strFileName.')', 'the file does not exist on your machine');
464
 
561
 
465
            return false;
562
            return false;
466
        }
563
        }
467
 
564
 
468
        return true;
565
        return true;
469
    }
566
    }
470
 
567
 
471
    /**
568
    /**
472
     * Find pathnames matching a pattern
569
     * Find pathnames matching a pattern
473
     *
570
     *
474
     * @param string $pattern the pattern. No tilde expansion or parameter substitution is done.
571
     * @param string $pattern the pattern. No tilde expansion or parameter substitution is done.
475
     * @param int    $flags
572
     * @param int    $flags
476
     *
573
     *
477
     * @return an array containing the matched files/directories, an empty array if no file matched or false on error
574
     * @return an array containing the matched files/directories, an empty array if no file matched or false on error
478
     */
575
     */
479
    public static function findglob($pattern, $flags = 0)
576
    public static function findglob($pattern, $flags = 0)
480
    {
577
    {
-
 
578
        if (defined('PSI_EMU_PORT')) {
-
 
579
            return false;
-
 
580
        }
-
 
581
 
481
        $outarr = glob(PSI_ROOT_FILESYSTEM.$pattern, $flags);
582
        $outarr = glob(PSI_ROOT_FILESYSTEM.$pattern, $flags);
482
        if (PSI_ROOT_FILESYSTEM == '') {
583
        if (PSI_ROOT_FILESYSTEM == '') {
483
            return $outarr;
584
            return $outarr;
484
        } elseif ($outarr === false) {
585
        } elseif ($outarr === false) {
485
            return false;
586
            return false;
486
        } else {
587
        } else {
487
            $len = strlen(PSI_ROOT_FILESYSTEM);
588
            $len = strlen(PSI_ROOT_FILESYSTEM);
488
            $newoutarr = array();
589
            $newoutarr = array();
489
            foreach ($outarr as $out) {
590
            foreach ($outarr as $out) {
490
                $newoutarr[] = substr($out, $len); // path without ROOTFS
591
                $newoutarr[] = substr($out, $len); // path without ROOTFS
491
            }
592
            }
492
 
593
 
493
            return $newoutarr;
594
            return $newoutarr;
494
        }
595
        }
495
    }
596
    }
496
 
597
 
497
    /**
598
    /**
498
     * file exists
599
     * file exists
499
     *
600
     *
500
     * @param string $strFileName name of the file which should be check
601
     * @param string $strFileName name of the file which should be check
501
     *
602
     *
502
     * @return boolean command successfull or not
603
     * @return boolean command successfull or not
503
     */
604
     */
504
    public static function fileexists($strFileName)
605
    public static function fileexists($strFileName)
505
    {
606
    {
-
 
607
        if (defined('PSI_EMU_PORT')) {
-
 
608
            return false;
-
 
609
        }
-
 
610
 
506
        if (defined('PSI_LOG') && is_string(PSI_LOG) && (strlen(PSI_LOG)>0) && ((substr(PSI_LOG, 0, 1)=="-") || (substr(PSI_LOG, 0, 1)=="+"))) {
611
        if (defined('PSI_LOG') && is_string(PSI_LOG) && (strlen(PSI_LOG)>0) && ((substr(PSI_LOG, 0, 1)=="-") || (substr(PSI_LOG, 0, 1)=="+"))) {
507
            $log_file = substr(PSI_LOG, 1);
612
            $log_file = substr(PSI_LOG, 1);
508
            if (file_exists($log_file)
613
            if (file_exists($log_file)
509
                && ($contents = @file_get_contents($log_file))
614
                && ($contents = @file_get_contents($log_file))
510
                && preg_match("/^\-\-\-[^-\n]+\-\-\- ".preg_quote("Reading: ".$strFileName, '/')."\n/m", $contents)) {
615
                && preg_match("/^\-\-\-[^-\n]+\-\-\- ".preg_quote("Reading: ".$strFileName, '/')."\n/m", $contents)) {
511
                return true;
616
                return true;
512
            } else {
617
            } else {
513
                if (substr(PSI_LOG, 0, 1)=="-") {
618
                if (substr(PSI_LOG, 0, 1)=="-") {
514
                    return false;
619
                    return false;
515
                }
620
                }
516
            }
621
            }
517
        }
622
        }
518
 
623
 
519
        $exists =  file_exists(PSI_ROOT_FILESYSTEM.$strFileName);
624
        $exists =  file_exists(PSI_ROOT_FILESYSTEM.$strFileName);
520
        if (defined('PSI_LOG') && is_string(PSI_LOG) && (strlen(PSI_LOG)>0) && (substr(PSI_LOG, 0, 1)!="-") && (substr(PSI_LOG, 0, 1)!="+")) {
625
        if (defined('PSI_LOG') && is_string(PSI_LOG) && (strlen(PSI_LOG)>0) && (substr(PSI_LOG, 0, 1)!="-") && (substr(PSI_LOG, 0, 1)!="+")) {
521
            if ((substr($strFileName, 0, 5) === "/dev/") && $exists) {
626
            if ((substr($strFileName, 0, 5) === "/dev/") && $exists) {
522
                error_log("---".gmdate('r T')."--- Reading: ".$strFileName."\ndevice exists\n", 3, PSI_LOG);
627
                error_log("---".gmdate('r T')."--- Reading: ".$strFileName."\ndevice exists\n", 3, PSI_LOG);
523
            }
628
            }
524
        }
629
        }
525
 
630
 
526
        return $exists;
631
        return $exists;
527
    }
632
    }
528
 
633
 
529
    /**
634
    /**
530
     * reads a directory and return the name of the files and directorys in it
635
     * reads a directory and return the name of the files and directorys in it
531
     *
636
     *
532
     * @param string  $strPath     path of the directory which should be read
637
     * @param string  $strPath     path of the directory which should be read
533
     * @param boolean $booErrorRep en- or disables the reporting of errors which should be logged
638
     * @param boolean $booErrorRep en- or disables the reporting of errors which should be logged
534
     *
639
     *
535
     * @return array content of the directory excluding . and ..
640
     * @return array content of the directory excluding . and ..
536
     */
641
     */
537
    public static function gdc($strPath, $booErrorRep = true)
642
    public static function gdc($strPath, $booErrorRep = true)
538
    {
643
    {
539
        $arrDirectoryContent = array();
644
        $arrDirectoryContent = array();
540
        $error = PSI_Error::singleton();
645
        $error = PSI_Error::singleton();
541
        if (is_dir($strPath)) {
646
        if (is_dir($strPath)) {
542
            if ($handle = opendir($strPath)) {
647
            if ($handle = opendir($strPath)) {
543
                while (($strFile = readdir($handle)) !== false) {
648
                while (($strFile = readdir($handle)) !== false) {
544
                    if ($strFile != "." && $strFile != "..") {
649
                    if ($strFile != "." && $strFile != "..") {
545
                        $arrDirectoryContent[] = $strFile;
650
                        $arrDirectoryContent[] = $strFile;
546
                    }
651
                    }
547
                }
652
                }
548
                closedir($handle);
653
                closedir($handle);
549
            } else {
654
            } else {
550
                if ($booErrorRep) {
655
                if ($booErrorRep) {
551
                    $error->addError('opendir('.$strPath.')', 'directory can not be read by phpsysinfo');
656
                    $error->addError('opendir('.$strPath.')', 'directory can not be read by phpsysinfo');
552
                }
657
                }
553
            }
658
            }
554
        } else {
659
        } else {
555
            if ($booErrorRep) {
660
            if ($booErrorRep) {
556
                $error->addError('is_dir('.$strPath.')', 'directory does not exist on your machine');
661
                $error->addError('is_dir('.$strPath.')', 'directory does not exist on your machine');
557
            }
662
            }
558
        }
663
        }
559
 
664
 
560
        return $arrDirectoryContent;
665
        return $arrDirectoryContent;
561
    }
666
    }
562
 
667
 
563
    /**
668
    /**
564
     * Check for needed php extensions
669
     * Check for needed php extensions
565
     *
670
     *
566
     * We need that extensions for almost everything
671
     * We need that extensions for almost everything
567
     * This function will return a hard coded
672
     * This function will return a hard coded
568
     * XML string (with headers) if the SimpleXML extension isn't loaded.
673
     * XML string (with headers) if the SimpleXML extension isn't loaded.
569
     * Then it will terminate the script.
674
     * Then it will terminate the script.
570
     * See bug #1787137
675
     * See bug #1787137
571
     *
676
     *
572
     * @param array $arrExt additional extensions for which a check should run
677
     * @param array $arrExt additional extensions for which a check should run
573
     *
678
     *
574
     * @return void
679
     * @return void
575
     */
680
     */
576
    public static function checkForExtensions($arrExt = array())
681
    public static function checkForExtensions($arrExt = array())
577
    {
682
    {
578
        if (defined('PSI_SYSTEM_CODEPAGE') && (PSI_SYSTEM_CODEPAGE !== null) && ((strcasecmp(PSI_SYSTEM_CODEPAGE, "UTF-8") == 0) || (strcasecmp(PSI_SYSTEM_CODEPAGE, "CP437") == 0)))
683
        if (defined('PSI_SYSTEM_CODEPAGE') && (PSI_SYSTEM_CODEPAGE !== null) && ((strcasecmp(PSI_SYSTEM_CODEPAGE, "UTF-8") == 0) || (strcasecmp(PSI_SYSTEM_CODEPAGE, "CP437") == 0)))
579
            $arrReq = array('simplexml', 'pcre', 'xml', 'dom');
684
            $arrReq = array('simplexml', 'pcre', 'xml', 'dom');
580
        elseif (PSI_OS == 'WINNT')
685
        elseif (PSI_OS == 'WINNT')
581
            $arrReq = array('simplexml', 'pcre', 'xml', 'dom', 'mbstring', 'com_dotnet');
686
            $arrReq = array('simplexml', 'pcre', 'xml', 'dom', 'mbstring', 'com_dotnet');
582
        else
687
        else
583
            $arrReq = array('simplexml', 'pcre', 'xml', 'dom', 'mbstring');
688
            $arrReq = array('simplexml', 'pcre', 'xml', 'dom', 'mbstring');
584
        $extensions = array_merge($arrExt, $arrReq);
689
        $extensions = array_merge($arrExt, $arrReq);
585
        $text = "";
690
        $text = "";
586
        $error = false;
691
        $error = false;
587
        $text .= "<?xml version='1.0'?>\n";
692
        $text .= "<?xml version='1.0'?>\n";
588
        $text .= "<phpsysinfo>\n";
693
        $text .= "<phpsysinfo>\n";
589
        $text .= "  <Error>\n";
694
        $text .= "  <Error>\n";
590
        foreach ($extensions as $extension) {
695
        foreach ($extensions as $extension) {
591
            if (!extension_loaded($extension)) {
696
            if (!extension_loaded($extension)) {
592
                $text .= "    <Function>checkForExtensions</Function>\n";
697
                $text .= "    <Function>checkForExtensions</Function>\n";
593
                $text .= "    <Message>phpSysInfo requires the ".$extension." extension to php in order to work properly.</Message>\n";
698
                $text .= "    <Message>phpSysInfo requires the ".$extension." extension to php in order to work properly.</Message>\n";
594
                $error = true;
699
                $error = true;
595
            }
700
            }
596
        }
701
        }
597
        $text .= "  </Error>\n";
702
        $text .= "  </Error>\n";
598
        $text .= "</phpsysinfo>";
703
        $text .= "</phpsysinfo>";
599
        if ($error) {
704
        if ($error) {
600
            header('Content-Type: text/xml');
705
            header('Content-Type: text/xml');
601
            echo $text;
706
            echo $text;
602
            die();
707
            die();
603
        }
708
        }
604
    }
709
    }
605
 
710
 
606
    /**
711
    /**
607
     * get the content of stdout/stderr with the option to set a timeout for reading
712
     * get the content of stdout/stderr with the option to set a timeout for reading
608
     *
713
     *
609
     * @param array  $pipes   array of file pointers for stdin, stdout, stderr (proc_open())
714
     * @param array  $pipes   array of file pointers for stdin, stdout, stderr (proc_open())
610
     * @param string &$out    target string for the output message (reference)
715
     * @param string &$out    target string for the output message (reference)
611
     * @param string &$err    target string for the error message (reference)
716
     * @param string &$err    target string for the error message (reference)
612
     * @param int    $timeout timeout value in seconds
717
     * @param int    $timeout timeout value in seconds
613
     *
718
     *
614
     * @return boolean timeout expired or not
719
     * @return boolean timeout expired or not
615
     */
720
     */
616
    private static function _timeoutfgets($pipes, &$out, &$err, $timeout)
721
    private static function _timeoutfgets($pipes, &$out, &$err, $timeout, $separator = '')
617
    {
722
    {
618
        $w = null;
723
        $w = null;
619
        $e = null;
724
        $e = null;
620
        $te = false;
725
        $te = false;
621
 
726
 
622
        if (defined("PSI_MODE_POPEN") && PSI_MODE_POPEN) {
727
        if (defined("PSI_MODE_POPEN") && PSI_MODE_POPEN) {
623
            $pipe2 = false;
728
            $pipe2 = false;
624
        } else {
729
        } else {
625
            $pipe2 = true;
730
            $pipe2 = true;
626
        }
731
        }
627
        while (!(feof($pipes[1]) && (!$pipe2 || feof($pipes[2])))) {
732
        while (!(feof($pipes[1]) && (!$pipe2 || feof($pipes[2])))) {
628
            if ($pipe2) {
733
            if ($pipe2) {
629
                $read = array($pipes[1], $pipes[2]);
734
                $read = array($pipes[1], $pipes[2]);
630
            } else {
735
            } else {
631
                $read = array($pipes[1]);
736
                $read = array($pipes[1]);
632
            }
737
            }
633
 
738
 
634
            $n = stream_select($read, $w, $e, $timeout);
739
            $n = stream_select($read, $w, $e, $timeout);
635
 
740
 
636
            if ($n === false) {
741
            if ($n === false) {
637
                error_log('stream_select: failed !');
742
                error_log('stream_select: failed !');
638
                break;
743
                break;
639
            } elseif ($n === 0) {
744
            } elseif ($n === 0) {
640
                error_log('stream_select: timeout expired !');
745
                error_log('stream_select: timeout expired !');
-
 
746
//                if ($separator !== '') {
-
 
747
//                    fwrite($pipes[0], "q");
-
 
748
//                }
641
                $te = true;
749
                $te = true;
642
                break;
750
                break;
643
            }
751
            }
644
 
752
 
645
            foreach ($read as $r) {
753
            foreach ($read as $r) {
646
                if ($r == $pipes[1]) {
754
                if ($r == $pipes[1]) {
647
                    $out .= fread($r, 4096);
755
                    $out .= fread($r, 4096);
648
                } elseif (feof($pipes[1]) && $pipe2 && ($r == $pipes[2])) {//read STDERR after STDOUT
756
                } elseif (feof($pipes[1]) && $pipe2 && ($r == $pipes[2])) {//read STDERR after STDOUT
649
                    $err .= fread($r, 4096);
757
                    $err .= fread($r, 4096);
650
                }
758
                }
651
            }
759
            }
-
 
760
//            if (($separator !== '') && preg_match('/'.$separator.'[^'.$separator.']+'.$separator.'/', $out)) {
-
 
761
            if (($separator !== '') && preg_match('/'.$separator.'[\s\S]+'.$separator.'/', $out)) {
-
 
762
                fwrite($pipes[0], "quit\n");
-
 
763
                $separator = ''; //only one time
-
 
764
              //  $te = true;
-
 
765
              //  break;
-
 
766
            }
652
        }
767
        }
653
 
768
 
654
        return $te;
769
        return $te;
655
    }
770
    }
656
 
771
 
657
    /**
772
    /**
658
     * get all configured plugins from phpsysinfo.ini (file must be included and processed before calling this function)
773
     * get all configured plugins from phpsysinfo.ini (file must be included and processed before calling this function)
659
     *
774
     *
660
     * @return array
775
     * @return array
661
     */
776
     */
662
    public static function getPlugins()
777
    public static function getPlugins()
663
    {
778
    {
664
        if (defined('PSI_PLUGINS') && is_string(PSI_PLUGINS)) {
779
        if (defined('PSI_PLUGINS') && is_string(PSI_PLUGINS)) {
665
            if (preg_match(ARRAY_EXP, PSI_PLUGINS)) {
780
            if (preg_match(ARRAY_EXP, PSI_PLUGINS)) {
666
                return eval(strtolower(PSI_PLUGINS));
781
                return eval(strtolower(PSI_PLUGINS));
667
            } else {
782
            } else {
668
                return array(strtolower(PSI_PLUGINS));
783
                return array(strtolower(PSI_PLUGINS));
669
            }
784
            }
670
        } else {
785
        } else {
671
            return array();
786
            return array();
672
        }
787
        }
673
    }
788
    }
674
 
789
 
675
    /**
790
    /**
676
     * name natural compare function
791
     * name natural compare function
677
     *
792
     *
678
     * @return comprasion result
793
     * @return comprasion result
679
     */
794
     */
680
    public static function name_natural_compare($a, $b)
795
    public static function name_natural_compare($a, $b)
681
    {
796
    {
682
        return strnatcmp($a->getName(), $b->getName());
797
        return strnatcmp($a->getName(), $b->getName());
683
    }
798
    }
684
 
799
 
685
    /**
800
    /**
686
     * get virtualizer from dmi data
801
     * get virtualizer from dmi data
687
     *
802
     *
688
     * @return string|null
803
     * @return string|null
689
     */
804
     */
690
    public static function decodevirtualizer($vendor_data)
805
    public static function decodevirtualizer($vendor_data)
691
    {
806
    {
692
        if (gettype($vendor_data) === "array") {
807
        if (gettype($vendor_data) === "array") {
693
            $vendarray = array(
808
            $vendarray = array(
694
                'KVM' => 'kvm', // KVM
809
                'KVM' => 'kvm', // KVM
-
 
810
                'OpenStack' => 'kvm', // Detect OpenStack instance as KVM in non x86 architecture
-
 
811
                'KubeVirt' => 'kvm', // Detect KubeVirt instance as KVM in non x86 architecture
695
                'Amazon EC2' => 'amazon', // Amazon EC2 Nitro using Linux KVM
812
                'Amazon EC2' => 'amazon', // Amazon EC2 Nitro using Linux KVM
696
                'QEMU' => 'qemu', // QEMU
813
                'QEMU' => 'qemu', // QEMU
697
                'VMware' => 'vmware', // VMware https://kb.vmware.com/s/article/1009458
814
                'VMware' => 'vmware', // VMware https://kb.vmware.com/s/article/1009458
698
                'VMW' => 'vmware',
815
                'VMW' => 'vmware',
699
                'innotek GmbH' => 'oracle', // Oracle VM VirtualBox
816
                'innotek GmbH' => 'oracle', // Oracle VM VirtualBox
700
                'VirtualBox' => 'oracle',
817
                'VirtualBox' => 'oracle',
701
                'Xen' => 'xen', // Xen hypervisor
818
                'Xen' => 'xen', // Xen hypervisor
702
                'Bochs' => 'bochs', // Bochs
819
                'Bochs' => 'bochs', // Bochs
703
                'Parallels' => 'parallels', // Parallels
820
                'Parallels' => 'parallels', // Parallels
704
                // https://wiki.freebsd.org/bhyve
821
                // https://wiki.freebsd.org/bhyve
705
                'BHYVE' => 'bhyve', // bhyve
822
                'BHYVE' => 'bhyve', // bhyve
706
                'Hyper-V' => 'microsoft', // Hyper-V
823
                'Hyper-V' => 'microsoft', // Hyper-V
-
 
824
                'Apple Virtualization' => 'apple', // Apple Virtualization.framework guests
707
                'Microsoft Corporation Virtual Machine' => 'microsoft' // Hyper-V
825
                'Microsoft Corporation Virtual Machine' => 'microsoft' // Hyper-V
708
            );
826
            );
709
            for ($i = 0; $i < count($vendor_data); $i++) {
827
            for ($i = 0; $i < count($vendor_data); $i++) {
710
                foreach ($vendarray as $vend=>$virt) {
828
                foreach ($vendarray as $vend=>$virt) {
711
                    if (preg_match('/^'.$vend.'/', $vendor_data[$i])) {
829
                    if (preg_match('/^'.$vend.'/', $vendor_data[$i])) {
712
                        return $virt;
830
                        return $virt;
713
                    }
831
                    }
714
                }
832
                }
715
            }
833
            }
716
        } elseif (gettype($vendor_data) === "string") {
834
        } elseif (gettype($vendor_data) === "string") {
717
            $vidarray = array(
835
            $vidarray = array(
718
                'bhyvebhyve' => 'bhyve', // bhyve
836
                'bhyvebhyve' => 'bhyve', // bhyve
719
                'KVMKVMKVM' => 'kvm', // KVM
837
                'KVMKVMKVM' => 'kvm', // KVM
-
 
838
                'LinuxKVMHv' => 'hv-kvm', // KVM (KVM + HyperV Enlightenments)
720
                'MicrosoftHv' => 'microsoft', // Hyper-V
839
                'MicrosoftHv' => 'microsoft', // Hyper-V
721
                'lrpepyhvr' => 'parallels', // Parallels
840
                'lrpepyhvr' => 'parallels', // Parallels
722
                'UnisysSpar64' => 'spar', // Unisys sPar
841
                'UnisysSpar64' => 'spar', // Unisys sPar
723
                'VMwareVMware' => 'vmware', // VMware
842
                'VMwareVMware' => 'vmware', // VMware
724
                'XenVMMXenVMM' => 'xen', // Xen hypervisor
843
                'XenVMMXenVMM' => 'xen', // Xen hypervisor
725
                'ACRNACRNACRN' => 'acrn', // ACRN hypervisor
844
                'ACRNACRNACRN' => 'acrn', // ACRN hypervisor
726
                'TCGTCGTCGTCG' => 'qemu', // QEMU
845
                'TCGTCGTCGTCG' => 'qemu', // QEMU
727
                'QNXQVMBSQG' => 'qnx', // QNX hypervisor
846
                'QNXQVMBSQG' => 'qnx', // QNX hypervisor
728
                'VBoxVBoxVBox' => 'oracle' // Oracle VM VirtualBox
847
                'VBoxVBoxVBox' => 'oracle' // Oracle VM VirtualBox
729
            );
848
            );
730
            $shortvendorid = trim(preg_replace('/[\s!\.]/', '', $vendor_data));
849
            $shortvendorid = trim(preg_replace('/[\s!\.]/', '', $vendor_data));
731
            if (($shortvendorid !== "") && isset($vidarray[$shortvendorid])) {
850
            if (($shortvendorid !== "") && isset($vidarray[$shortvendorid])) {
732
                return $vidarray[$shortvendorid];
851
                return $vidarray[$shortvendorid];
733
            }
852
            }
734
        }
853
        }
735
 
854
 
736
        return null;
855
        return null;
737
    }
856
    }
738
 
857
 
739
 
858
 
740
    /**
859
    /**
741
     * readdmimemdata function
860
     * readdmimemdata function
742
     *
861
     *
743
     * @return array
862
     * @return array
744
     */
863
     */
745
    public static function readdmimemdata()
864
    public static function readdmimemdata()
746
    {
865
    {
747
        if ((PSI_OS != 'WINNT') && !defined('PSI_EMU_HOSTNAME') && (self::$_dmimd === null)) {
866
        if ((PSI_OS != 'WINNT') && (!defined('PSI_EMU_HOSTNAME') || defined('PSI_EMU_PORT')) && (self::$_dmimd === null)) {
748
            self::$_dmimd = array();
867
            self::$_dmimd = array();
749
            $buffer = '';
868
            $buffer = '';
750
            if (defined('PSI_DMIDECODE_ACCESS') && (strtolower(PSI_DMIDECODE_ACCESS)==='data')) {
869
            if (defined('PSI_DMIDECODE_ACCESS') && (strtolower(PSI_DMIDECODE_ACCESS)==='data')) {
751
                self::rftsdata('dmidecode.tmp', $buffer);
870
                self::rftsdata('dmidecode.tmp', $buffer);
752
            } elseif (self::_findProgram('dmidecode')) {
871
            } elseif (self::_findProgram('dmidecode')) {
753
                self::executeProgram('dmidecode', '-t 17', $buffer, PSI_DEBUG);
872
                self::executeProgram('dmidecode', '-t 17', $buffer, PSI_DEBUG);
754
            }
873
            }
755
            if (!empty($buffer)) {
874
            if (!empty($buffer)) {
756
                $banks = preg_split('/^(?=Handle\s)/m', $buffer, -1, PREG_SPLIT_NO_EMPTY);
875
                $banks = preg_split('/^(?=Handle\s)/m', $buffer, -1, PREG_SPLIT_NO_EMPTY);
757
                foreach ($banks as $bank) if (preg_match('/^Handle\s/', $bank)) {
876
                foreach ($banks as $bank) if (preg_match('/^Handle\s/', $bank)) {
758
                    $lines = preg_split("/\n/", $bank, -1, PREG_SPLIT_NO_EMPTY);
877
                    $lines = preg_split("/\n/", $bank, -1, PREG_SPLIT_NO_EMPTY);
759
                    $mem = array();
878
                    $mem = array();
760
                    foreach ($lines as $line) if (preg_match('/^\s+([^:]+):(.+)/', $line, $params)) {
879
                    foreach ($lines as $line) if (preg_match('/^\s+([^:]+):(.+)/', $line, $params)) {
761
                        if (preg_match('/^0x([A-F\d]+)/', $params2 = trim($params[2]), $buff)) {
880
                        if (preg_match('/^0x([A-F\d]+)/', $params2 = trim($params[2]), $buff)) {
762
                            $mem[trim($params[1])] = trim($buff[1]);
881
                            $mem[trim($params[1])] = trim($buff[1]);
763
                        } elseif ($params2 != '') {
882
                        } elseif ($params2 != '') {
764
                            $mem[trim($params[1])] = $params2;
883
                            $mem[trim($params[1])] = $params2;
765
                        }
884
                        }
766
                    }
885
                    }
767
                    if (!empty($mem)) {
886
                    if (!empty($mem)) {
768
                        self::$_dmimd[] = $mem;
887
                        self::$_dmimd[] = $mem;
769
                    }
888
                    }
770
                }
889
                }
771
            }
890
            }
772
        }
891
        }
773
 
892
 
774
        return self::$_dmimd;
893
        return self::$_dmimd;
775
    }
894
    }
776
}
895
}
777
 
896