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