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 |
/**
|
|
|
29 |
* holds codepage for chcp
|
|
|
30 |
*
|
|
|
31 |
* @var integer
|
|
|
32 |
*/
|
|
|
33 |
private static $_cp = null;
|
|
|
34 |
|
|
|
35 |
public static function setcp($cp)
|
|
|
36 |
{
|
|
|
37 |
CommonFunctions::$_cp = $cp;
|
|
|
38 |
}
|
|
|
39 |
|
|
|
40 |
private static function _parse_log_file($string)
|
|
|
41 |
{
|
|
|
42 |
if (defined('PSI_LOG') && is_string(PSI_LOG) && (strlen(PSI_LOG)>0) && ((substr(PSI_LOG, 0, 1)=="-") || (substr(PSI_LOG, 0, 1)=="+"))) {
|
|
|
43 |
$log_file = substr(PSI_LOG, 1);
|
|
|
44 |
if (file_exists($log_file)) {
|
|
|
45 |
$contents = @file_get_contents($log_file);
|
|
|
46 |
if ($contents && preg_match("/^\-\-\-[^-\r\n]+\-\-\- ".preg_quote($string, '/')."\r?\n/m", $contents, $matches, PREG_OFFSET_CAPTURE)) {
|
|
|
47 |
$findIndex = $matches[0][1];
|
|
|
48 |
if (preg_match("/\r?\n/m", $contents, $matches, PREG_OFFSET_CAPTURE, $findIndex)) {
|
|
|
49 |
$startIndex = $matches[0][1]+1;
|
|
|
50 |
if (preg_match("/^\-\-\-[^-\r\n]+\-\-\- /m", $contents, $matches, PREG_OFFSET_CAPTURE, $startIndex)) {
|
|
|
51 |
$stopIndex = $matches[0][1];
|
|
|
52 |
|
|
|
53 |
return substr($contents, $startIndex, $stopIndex-$startIndex);
|
|
|
54 |
} else {
|
|
|
55 |
return substr($contents, $startIndex);
|
|
|
56 |
}
|
|
|
57 |
}
|
|
|
58 |
}
|
|
|
59 |
}
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
return false;
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
/**
|
|
|
66 |
* Find a system program, do also path checking when not running on WINNT
|
|
|
67 |
* on WINNT we simply return the name with the exe extension to the program name
|
|
|
68 |
*
|
|
|
69 |
* @param string $strProgram name of the program
|
|
|
70 |
*
|
|
|
71 |
* @return string|null complete path and name of the program
|
|
|
72 |
*/
|
|
|
73 |
private static function _findProgram($strProgram)
|
|
|
74 |
{
|
|
|
75 |
$path_parts = pathinfo($strProgram);
|
|
|
76 |
if (empty($path_parts['basename'])) {
|
|
|
77 |
return null;
|
|
|
78 |
}
|
|
|
79 |
$arrPath = array();
|
|
|
80 |
|
|
|
81 |
if (empty($path_parts['dirname']) || ($path_parts['dirname'] == '.')) {
|
|
|
82 |
if ((PSI_OS == 'WINNT') && empty($path_parts['extension'])) {
|
|
|
83 |
$strProgram .= '.exe';
|
|
|
84 |
$path_parts = pathinfo($strProgram);
|
|
|
85 |
}
|
|
|
86 |
if (PSI_OS == 'WINNT') {
|
|
|
87 |
if (CommonFunctions::readenv('Path', $serverpath)) {
|
|
|
88 |
$arrPath = preg_split('/;/', $serverpath, -1, PREG_SPLIT_NO_EMPTY);
|
|
|
89 |
}
|
|
|
90 |
} else {
|
|
|
91 |
if (CommonFunctions::readenv('PATH', $serverpath)) {
|
|
|
92 |
$arrPath = preg_split('/:/', $serverpath, -1, PREG_SPLIT_NO_EMPTY);
|
|
|
93 |
}
|
|
|
94 |
}
|
|
|
95 |
if (defined('PSI_UNAMEO') && (PSI_UNAMEO === 'Android') && !empty($arrPath)) {
|
|
|
96 |
array_push($arrPath, '/system/bin'); // Termux patch
|
|
|
97 |
}
|
|
|
98 |
if (defined('PSI_ADD_PATHS') && is_string(PSI_ADD_PATHS)) {
|
|
|
99 |
if (preg_match(ARRAY_EXP, PSI_ADD_PATHS)) {
|
|
|
100 |
$arrPath = array_merge(eval(PSI_ADD_PATHS), $arrPath); // In this order so $addpaths is before $arrPath when looking for a program
|
|
|
101 |
} else {
|
|
|
102 |
$arrPath = array_merge(array(PSI_ADD_PATHS), $arrPath); // In this order so $addpaths is before $arrPath when looking for a program
|
|
|
103 |
}
|
|
|
104 |
}
|
|
|
105 |
} else { //directory defined
|
|
|
106 |
array_push($arrPath, $path_parts['dirname']);
|
|
|
107 |
$strProgram = $path_parts['basename'];
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
//add some default paths if we still have no paths here
|
|
|
111 |
if (empty($arrPath) && PSI_OS != 'WINNT') {
|
|
|
112 |
if (PSI_OS == 'Android') {
|
|
|
113 |
array_push($arrPath, '/system/bin');
|
|
|
114 |
} else {
|
|
|
115 |
array_push($arrPath, '/bin', '/sbin', '/usr/bin', '/usr/sbin', '/usr/local/bin', '/usr/local/sbin');
|
|
|
116 |
}
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
$exceptPath = "";
|
|
|
120 |
if ((PSI_OS == 'WINNT') && CommonFunctions::readenv('WinDir', $windir)) {
|
|
|
121 |
foreach ($arrPath as $strPath) {
|
|
|
122 |
if ((strtolower($strPath) == $windir."\\system32") && is_dir($windir."\\SysWOW64")) {
|
|
|
123 |
if (is_dir($windir."\\sysnative")) {
|
|
|
124 |
$exceptPath = $windir."\\sysnative"; //32-bit PHP on 64-bit Windows
|
|
|
125 |
} else {
|
|
|
126 |
$exceptPath = $windir."\\SysWOW64"; //64-bit PHP on 64-bit Windows
|
|
|
127 |
}
|
|
|
128 |
array_push($arrPath, $exceptPath);
|
|
|
129 |
break;
|
|
|
130 |
}
|
|
|
131 |
}
|
|
|
132 |
} elseif (PSI_OS == 'Android') {
|
|
|
133 |
$exceptPath = '/system/bin';
|
|
|
134 |
}
|
|
|
135 |
|
|
|
136 |
foreach ($arrPath as $strPath) {
|
|
|
137 |
// Path with and without trailing slash
|
|
|
138 |
if (PSI_OS == 'WINNT') {
|
|
|
139 |
$strPath = rtrim($strPath, "\\");
|
|
|
140 |
$strPathS = $strPath."\\";
|
|
|
141 |
} else {
|
|
|
142 |
$strPath = rtrim($strPath, "/");
|
|
|
143 |
$strPathS = $strPath."/";
|
|
|
144 |
}
|
|
|
145 |
if (($strPath !== $exceptPath) && !is_dir($strPath)) {
|
|
|
146 |
continue;
|
|
|
147 |
}
|
|
|
148 |
if (PSI_OS == 'WINNT') {
|
|
|
149 |
$strProgrammpath = $strPathS.$strProgram;
|
|
|
150 |
} else {
|
|
|
151 |
$strProgrammpath = $strPathS.$strProgram;
|
|
|
152 |
}
|
|
|
153 |
if (is_executable($strProgrammpath)) {
|
|
|
154 |
return $strProgrammpath;
|
|
|
155 |
}
|
|
|
156 |
}
|
|
|
157 |
|
|
|
158 |
return null;
|
|
|
159 |
}
|
|
|
160 |
|
|
|
161 |
/**
|
|
|
162 |
* Execute a system program. return a trim()'d result.
|
|
|
163 |
* does very crude pipe checking. you need ' | ' for it to work
|
|
|
164 |
* ie $program = CommonFunctions::executeProgram('netstat', '-anp | grep LIST');
|
|
|
165 |
* NOT $program = CommonFunctions::executeProgram('netstat', '-anp|grep LIST');
|
|
|
166 |
*
|
|
|
167 |
* @param string $strProgramname name of the program
|
|
|
168 |
* @param string $strArgs arguments to the program
|
|
|
169 |
* @param string &$strBuffer output of the command
|
|
|
170 |
* @param boolean $booErrorRep en- or disables the reporting of errors which should be logged
|
|
|
171 |
* @param integer $timeout timeout value in seconds (default value is PSI_EXEC_TIMEOUT_INT)
|
|
|
172 |
*
|
|
|
173 |
* @return boolean command successfull or not
|
|
|
174 |
*/
|
|
|
175 |
public static function executeProgram($strProgramname, $strArgs, &$strBuffer, $booErrorRep = true, $timeout = PSI_EXEC_TIMEOUT_INT)
|
|
|
176 |
{
|
|
|
177 |
if (defined('PSI_LOG') && is_string(PSI_LOG) && (strlen(PSI_LOG)>0) && ((substr(PSI_LOG, 0, 1)=="-") || (substr(PSI_LOG, 0, 1)=="+"))) {
|
|
|
178 |
$out = self::_parse_log_file("Executing: ".trim($strProgramname.' '.$strArgs));
|
|
|
179 |
if ($out == false) {
|
|
|
180 |
if (substr(PSI_LOG, 0, 1)=="-") {
|
|
|
181 |
$strBuffer = '';
|
|
|
182 |
|
|
|
183 |
return false;
|
|
|
184 |
}
|
|
|
185 |
} else {
|
|
|
186 |
$strBuffer = $out;
|
|
|
187 |
|
|
|
188 |
return true;
|
|
|
189 |
}
|
|
|
190 |
}
|
|
|
191 |
|
|
|
192 |
if ((PSI_OS !== 'WINNT') && preg_match('/^([^=]+=[^ \t]+)[ \t]+(.*)$/', $strProgramname, $strmatch)) {
|
|
|
193 |
$strSet = $strmatch[1].' ';
|
|
|
194 |
$strProgramname = $strmatch[2];
|
|
|
195 |
} else {
|
|
|
196 |
$strSet = '';
|
|
|
197 |
}
|
|
|
198 |
$strProgram = self::_findProgram($strProgramname);
|
|
|
199 |
$error = PSI_Error::singleton();
|
|
|
200 |
if (!$strProgram) {
|
|
|
201 |
if ($booErrorRep) {
|
|
|
202 |
$error->addError('find_program("'.$strProgramname.'")', 'program not found on the machine');
|
|
|
203 |
}
|
|
|
204 |
|
|
|
205 |
return false;
|
|
|
206 |
} else {
|
|
|
207 |
if (preg_match('/\s/', $strProgram)) {
|
|
|
208 |
$strProgram = '"'.$strProgram.'"';
|
|
|
209 |
}
|
|
|
210 |
}
|
|
|
211 |
|
|
|
212 |
if ((PSI_OS !== 'WINNT') && defined('PSI_SUDO_COMMANDS') && is_string(PSI_SUDO_COMMANDS)) {
|
|
|
213 |
if (preg_match(ARRAY_EXP, PSI_SUDO_COMMANDS)) {
|
|
|
214 |
$sudocommands = eval(PSI_SUDO_COMMANDS);
|
|
|
215 |
} else {
|
|
|
216 |
$sudocommands = array(PSI_SUDO_COMMANDS);
|
|
|
217 |
}
|
|
|
218 |
if (in_array($strProgramname, $sudocommands)) {
|
|
|
219 |
$sudoProgram = self::_findProgram("sudo");
|
|
|
220 |
if (!$sudoProgram) {
|
|
|
221 |
if ($booErrorRep) {
|
|
|
222 |
$error->addError('find_program("sudo")', 'program not found on the machine');
|
|
|
223 |
}
|
|
|
224 |
|
|
|
225 |
return false;
|
|
|
226 |
} else {
|
|
|
227 |
if (preg_match('/\s/', $sudoProgram)) {
|
|
|
228 |
$strProgram = '"'.$sudoProgram.'" '.$strProgram;
|
|
|
229 |
} else {
|
|
|
230 |
$strProgram = $sudoProgram.' '.$strProgram;
|
|
|
231 |
}
|
|
|
232 |
}
|
|
|
233 |
}
|
|
|
234 |
}
|
|
|
235 |
|
|
|
236 |
// see if we've gotten a |, if we have we need to do path checking on the cmd
|
|
|
237 |
if ($strArgs) {
|
|
|
238 |
$arrArgs = preg_split('/ /', $strArgs, -1, PREG_SPLIT_NO_EMPTY);
|
|
|
239 |
for ($i = 0, $cnt_args = count($arrArgs); $i < $cnt_args; $i++) {
|
|
|
240 |
if ($arrArgs[$i] == '|') {
|
|
|
241 |
$strCmd = $arrArgs[$i + 1];
|
|
|
242 |
$strNewcmd = self::_findProgram($strCmd);
|
|
|
243 |
$strArgs = preg_replace("/\| ".$strCmd.'/', '| "'.$strNewcmd.'"', $strArgs);
|
|
|
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"));
|
|
|
253 |
if (defined("PSI_MODE_POPEN") && PSI_MODE_POPEN === true) {
|
|
|
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);
|
|
|
264 |
if (defined("PSI_MODE_POPEN") && PSI_MODE_POPEN === true) {
|
|
|
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);
|
|
|
310 |
if (CommonFunctions::fileexists($filename) && CommonFunctions::rfts($filename, $buf, 1, 4096, false) && (($buf=trim($buf)) != "")) {
|
|
|
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)
|
|
|
354 |
* @param integer $intLines control how many lines should be read
|
|
|
355 |
* @param integer $intBytes control how many bytes of each line should be read
|
|
|
356 |
* @param boolean $booErrorRep en- or disables the reporting of errors which should be logged
|
|
|
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 |
|
|
|
377 |
$strFile = "";
|
|
|
378 |
$intCurLine = 1;
|
|
|
379 |
$error = PSI_Error::singleton();
|
|
|
380 |
if (file_exists($strFileName)) {
|
|
|
381 |
if (is_readable($strFileName)) {
|
|
|
382 |
if ($fd = fopen($strFileName, 'r')) {
|
|
|
383 |
while (!feof($fd)) {
|
|
|
384 |
$strFile .= fgets($fd, $intBytes);
|
|
|
385 |
if ($intLines <= $intCurLine && $intLines != 0) {
|
|
|
386 |
break;
|
|
|
387 |
} else {
|
|
|
388 |
$intCurLine++;
|
|
|
389 |
}
|
|
|
390 |
}
|
|
|
391 |
fclose($fd);
|
|
|
392 |
$strRet = $strFile;
|
|
|
393 |
if (defined('PSI_LOG') && is_string(PSI_LOG) && (strlen(PSI_LOG)>0) && (substr(PSI_LOG, 0, 1)!="-") && (substr(PSI_LOG, 0, 1)!="+")) {
|
|
|
394 |
if ((strlen($strRet)>0)&&(substr($strRet, -1)!="\n")) {
|
|
|
395 |
error_log("---".gmdate('r T')."--- Reading: ".$strFileName."\n".$strRet."\n", 3, PSI_LOG);
|
|
|
396 |
} else {
|
|
|
397 |
error_log("---".gmdate('r T')."--- Reading: ".$strFileName."\n".$strRet, 3, PSI_LOG);
|
|
|
398 |
}
|
|
|
399 |
}
|
|
|
400 |
} else {
|
|
|
401 |
if ($booErrorRep) {
|
|
|
402 |
$error->addError('fopen('.$strFileName.')', 'file can not read by phpsysinfo');
|
|
|
403 |
}
|
|
|
404 |
|
|
|
405 |
return false;
|
|
|
406 |
}
|
|
|
407 |
} else {
|
|
|
408 |
if ($booErrorRep) {
|
|
|
409 |
$error->addError('fopen('.$strFileName.')', 'file permission error');
|
|
|
410 |
}
|
|
|
411 |
|
|
|
412 |
return false;
|
|
|
413 |
}
|
|
|
414 |
} else {
|
|
|
415 |
if ($booErrorRep) {
|
|
|
416 |
$error->addError('file_exists('.$strFileName.')', 'the file does not exist on your machine');
|
|
|
417 |
}
|
|
|
418 |
|
|
|
419 |
return false;
|
|
|
420 |
}
|
|
|
421 |
|
|
|
422 |
return true;
|
|
|
423 |
}
|
|
|
424 |
|
|
|
425 |
/**
|
|
|
426 |
* file exists
|
|
|
427 |
*
|
|
|
428 |
* @param string $strFileName name of the file which should be check
|
|
|
429 |
*
|
|
|
430 |
* @return boolean command successfull or not
|
|
|
431 |
*/
|
|
|
432 |
public static function fileexists($strFileName)
|
|
|
433 |
{
|
|
|
434 |
if (defined('PSI_LOG') && is_string(PSI_LOG) && (strlen(PSI_LOG)>0) && ((substr(PSI_LOG, 0, 1)=="-") || (substr(PSI_LOG, 0, 1)=="+"))) {
|
|
|
435 |
$log_file = substr(PSI_LOG, 1);
|
|
|
436 |
if (file_exists($log_file)
|
|
|
437 |
&& ($contents = @file_get_contents($log_file))
|
|
|
438 |
&& preg_match("/^\-\-\-[^-\n]+\-\-\- ".preg_quote("Reading: ".$strFileName, '/')."\n/m", $contents)) {
|
|
|
439 |
return true;
|
|
|
440 |
} else {
|
|
|
441 |
if (substr(PSI_LOG, 0, 1)=="-") {
|
|
|
442 |
return false;
|
|
|
443 |
}
|
|
|
444 |
}
|
|
|
445 |
}
|
|
|
446 |
|
|
|
447 |
$exists = file_exists($strFileName);
|
|
|
448 |
if (defined('PSI_LOG') && is_string(PSI_LOG) && (strlen(PSI_LOG)>0) && (substr(PSI_LOG, 0, 1)!="-") && (substr(PSI_LOG, 0, 1)!="+")) {
|
|
|
449 |
if ((substr($strFileName, 0, 5) === "/dev/") && $exists) {
|
|
|
450 |
error_log("---".gmdate('r T')."--- Reading: ".$strFileName."\ndevice exists\n", 3, PSI_LOG);
|
|
|
451 |
}
|
|
|
452 |
}
|
|
|
453 |
|
|
|
454 |
return $exists;
|
|
|
455 |
}
|
|
|
456 |
|
|
|
457 |
/**
|
|
|
458 |
* reads a directory and return the name of the files and directorys in it
|
|
|
459 |
*
|
|
|
460 |
* @param string $strPath path of the directory which should be read
|
|
|
461 |
* @param boolean $booErrorRep en- or disables the reporting of errors which should be logged
|
|
|
462 |
*
|
|
|
463 |
* @return array content of the directory excluding . and ..
|
|
|
464 |
*/
|
|
|
465 |
public static function gdc($strPath, $booErrorRep = true)
|
|
|
466 |
{
|
|
|
467 |
$arrDirectoryContent = array();
|
|
|
468 |
$error = PSI_Error::singleton();
|
|
|
469 |
if (is_dir($strPath)) {
|
|
|
470 |
if ($handle = opendir($strPath)) {
|
|
|
471 |
while (($strFile = readdir($handle)) !== false) {
|
|
|
472 |
if ($strFile != "." && $strFile != "..") {
|
|
|
473 |
$arrDirectoryContent[] = $strFile;
|
|
|
474 |
}
|
|
|
475 |
}
|
|
|
476 |
closedir($handle);
|
|
|
477 |
} else {
|
|
|
478 |
if ($booErrorRep) {
|
|
|
479 |
$error->addError('opendir('.$strPath.')', 'directory can not be read by phpsysinfo');
|
|
|
480 |
}
|
|
|
481 |
}
|
|
|
482 |
} else {
|
|
|
483 |
if ($booErrorRep) {
|
|
|
484 |
$error->addError('is_dir('.$strPath.')', 'directory does not exist on your machine');
|
|
|
485 |
}
|
|
|
486 |
}
|
|
|
487 |
|
|
|
488 |
return $arrDirectoryContent;
|
|
|
489 |
}
|
|
|
490 |
|
|
|
491 |
/**
|
|
|
492 |
* Check for needed php extensions
|
|
|
493 |
*
|
|
|
494 |
* We need that extensions for almost everything
|
|
|
495 |
* This function will return a hard coded
|
|
|
496 |
* XML string (with headers) if the SimpleXML extension isn't loaded.
|
|
|
497 |
* Then it will terminate the script.
|
|
|
498 |
* See bug #1787137
|
|
|
499 |
*
|
|
|
500 |
* @param array $arrExt additional extensions for which a check should run
|
|
|
501 |
*
|
|
|
502 |
* @return void
|
|
|
503 |
*/
|
|
|
504 |
public static function checkForExtensions($arrExt = array())
|
|
|
505 |
{
|
|
|
506 |
if ((strcasecmp(PSI_SYSTEM_CODEPAGE, "UTF-8") == 0) || (strcasecmp(PSI_SYSTEM_CODEPAGE, "CP437") == 0))
|
|
|
507 |
$arrReq = array('simplexml', 'pcre', 'xml', 'dom');
|
|
|
508 |
elseif (PSI_OS == "WINNT")
|
|
|
509 |
$arrReq = array('simplexml', 'pcre', 'xml', 'dom', 'mbstring', 'com_dotnet');
|
|
|
510 |
else
|
|
|
511 |
$arrReq = array('simplexml', 'pcre', 'xml', 'dom', 'mbstring');
|
|
|
512 |
$extensions = array_merge($arrExt, $arrReq);
|
|
|
513 |
$text = "";
|
|
|
514 |
$error = false;
|
|
|
515 |
$text .= "<?xml version='1.0'?>\n";
|
|
|
516 |
$text .= "<phpsysinfo>\n";
|
|
|
517 |
$text .= " <Error>\n";
|
|
|
518 |
foreach ($extensions as $extension) {
|
|
|
519 |
if (!extension_loaded($extension)) {
|
|
|
520 |
$text .= " <Function>checkForExtensions</Function>\n";
|
|
|
521 |
$text .= " <Message>phpSysInfo requires the ".$extension." extension to php in order to work properly.</Message>\n";
|
|
|
522 |
$error = true;
|
|
|
523 |
}
|
|
|
524 |
}
|
|
|
525 |
$text .= " </Error>\n";
|
|
|
526 |
$text .= "</phpsysinfo>";
|
|
|
527 |
if ($error) {
|
|
|
528 |
header("Content-Type: text/xml\n\n");
|
|
|
529 |
echo $text;
|
|
|
530 |
die();
|
|
|
531 |
}
|
|
|
532 |
}
|
|
|
533 |
|
|
|
534 |
/**
|
|
|
535 |
* get the content of stdout/stderr with the option to set a timeout for reading
|
|
|
536 |
*
|
|
|
537 |
* @param array $pipes array of file pointers for stdin, stdout, stderr (proc_open())
|
|
|
538 |
* @param string &$out target string for the output message (reference)
|
|
|
539 |
* @param string &$err target string for the error message (reference)
|
|
|
540 |
* @param integer $timeout timeout value in seconds
|
|
|
541 |
*
|
|
|
542 |
* @return boolean timeout expired or not
|
|
|
543 |
*/
|
|
|
544 |
private static function _timeoutfgets($pipes, &$out, &$err, $timeout)
|
|
|
545 |
{
|
|
|
546 |
$w = null;
|
|
|
547 |
$e = null;
|
|
|
548 |
$te = false;
|
|
|
549 |
|
|
|
550 |
if (defined("PSI_MODE_POPEN") && PSI_MODE_POPEN === true) {
|
|
|
551 |
$pipe2 = false;
|
|
|
552 |
} else {
|
|
|
553 |
$pipe2 = true;
|
|
|
554 |
}
|
|
|
555 |
while (!(feof($pipes[1]) && (!$pipe2 || feof($pipes[2])))) {
|
|
|
556 |
if ($pipe2) {
|
|
|
557 |
$read = array($pipes[1], $pipes[2]);
|
|
|
558 |
} else {
|
|
|
559 |
$read = array($pipes[1]);
|
|
|
560 |
}
|
|
|
561 |
|
|
|
562 |
$n = stream_select($read, $w, $e, $timeout);
|
|
|
563 |
|
|
|
564 |
if ($n === false) {
|
|
|
565 |
error_log('stream_select: failed !');
|
|
|
566 |
break;
|
|
|
567 |
} elseif ($n === 0) {
|
|
|
568 |
error_log('stream_select: timeout expired !');
|
|
|
569 |
$te = true;
|
|
|
570 |
break;
|
|
|
571 |
}
|
|
|
572 |
|
|
|
573 |
foreach ($read as $r) {
|
|
|
574 |
if ($r == $pipes[1]) {
|
|
|
575 |
$out .= fread($r, 4096);
|
|
|
576 |
} elseif (feof($pipes[1]) && $pipe2 && ($r == $pipes[2])) {//read STDERR after STDOUT
|
|
|
577 |
$err .= fread($r, 4096);
|
|
|
578 |
}
|
|
|
579 |
}
|
|
|
580 |
}
|
|
|
581 |
|
|
|
582 |
return $te;
|
|
|
583 |
}
|
|
|
584 |
|
|
|
585 |
/**
|
|
|
586 |
* function for getting a list of values in the specified context
|
|
|
587 |
* optionally filter this list, based on the list from third parameter
|
|
|
588 |
*
|
|
|
589 |
* @param $wmi object holds the COM object that we pull the WMI data from
|
|
|
590 |
* @param string $strClass name of the class where the values are stored
|
|
|
591 |
* @param array $strValue filter out only needed values, if not set all values of the class are returned
|
|
|
592 |
*
|
|
|
593 |
* @return array content of the class stored in an array
|
|
|
594 |
*/
|
|
|
595 |
public static function getWMI($wmi, $strClass, $strValue = array())
|
|
|
596 |
{
|
|
|
597 |
$arrData = array();
|
|
|
598 |
if (gettype($wmi) === "object") {
|
|
|
599 |
$value = "";
|
|
|
600 |
try {
|
|
|
601 |
$objWEBM = $wmi->Get($strClass);
|
|
|
602 |
$arrProp = $objWEBM->Properties_;
|
|
|
603 |
$arrWEBMCol = $objWEBM->Instances_();
|
|
|
604 |
foreach ($arrWEBMCol as $objItem) {
|
|
|
605 |
if (is_array($arrProp)) {
|
|
|
606 |
reset($arrProp);
|
|
|
607 |
}
|
|
|
608 |
$arrInstance = array();
|
|
|
609 |
foreach ($arrProp as $propItem) {
|
|
|
610 |
$value = $objItem->{$propItem->Name}; //instead exploitable eval("\$value = \$objItem->".$propItem->Name.";");
|
|
|
611 |
if (empty($strValue)) {
|
|
|
612 |
if (is_string($value)) $arrInstance[$propItem->Name] = trim($value);
|
|
|
613 |
else $arrInstance[$propItem->Name] = $value;
|
|
|
614 |
} else {
|
|
|
615 |
if (in_array($propItem->Name, $strValue)) {
|
|
|
616 |
if (is_string($value)) $arrInstance[$propItem->Name] = trim($value);
|
|
|
617 |
else $arrInstance[$propItem->Name] = $value;
|
|
|
618 |
}
|
|
|
619 |
}
|
|
|
620 |
}
|
|
|
621 |
$arrData[] = $arrInstance;
|
|
|
622 |
}
|
|
|
623 |
} catch (Exception $e) {
|
|
|
624 |
if (PSI_DEBUG) {
|
|
|
625 |
$error = PSI_Error::singleton();
|
|
|
626 |
$error->addError("getWMI()", preg_replace('/<br\/>/', "\n", preg_replace('/<b>|<\/b>/', '', $e->getMessage())));
|
|
|
627 |
}
|
|
|
628 |
}
|
|
|
629 |
}
|
|
|
630 |
|
|
|
631 |
return $arrData;
|
|
|
632 |
}
|
|
|
633 |
|
|
|
634 |
/**
|
|
|
635 |
* get all configured plugins from phpsysinfo.ini (file must be included and processed before calling this function)
|
|
|
636 |
*
|
|
|
637 |
* @return array
|
|
|
638 |
*/
|
|
|
639 |
public static function getPlugins()
|
|
|
640 |
{
|
|
|
641 |
if (defined('PSI_PLUGINS') && is_string(PSI_PLUGINS)) {
|
|
|
642 |
if (preg_match(ARRAY_EXP, PSI_PLUGINS)) {
|
|
|
643 |
return eval(strtolower(PSI_PLUGINS));
|
|
|
644 |
} else {
|
|
|
645 |
return array(strtolower(PSI_PLUGINS));
|
|
|
646 |
}
|
|
|
647 |
} else {
|
|
|
648 |
return array();
|
|
|
649 |
}
|
|
|
650 |
}
|
|
|
651 |
|
|
|
652 |
/**
|
|
|
653 |
* name natural compare function
|
|
|
654 |
*
|
|
|
655 |
* @return comprasion result
|
|
|
656 |
*/
|
|
|
657 |
public static function name_natural_compare($a, $b)
|
|
|
658 |
{
|
|
|
659 |
return strnatcmp($a->getName(), $b->getName());
|
|
|
660 |
}
|
|
|
661 |
|
|
|
662 |
/**
|
|
|
663 |
* readReg function
|
|
|
664 |
*
|
|
|
665 |
* @return boolean command successfull or not
|
|
|
666 |
*/
|
|
|
667 |
public static function readReg($reg, $strName, &$strBuffer, $booErrorRep = true)
|
|
|
668 |
{
|
|
|
669 |
$strBuffer = '';
|
|
|
670 |
if ($reg === false) {
|
|
|
671 |
$last = strrpos($strName, "\\");
|
|
|
672 |
$keyname = substr($strName, $last + 1);
|
|
|
673 |
if (CommonFunctions::$_cp) {
|
|
|
674 |
if (CommonFunctions::executeProgram('cmd', '/c chcp '.CommonFunctions::$_cp.' && reg query "'.substr($strName, 0, $last).'" /v '.$keyname.' 2>&1', $strBuf, $booErrorRep) && (strlen($strBuf) > 0) && preg_match("/^\s*".$keyname."\s+REG_\S+\s+(.+)\s*$/mi", $strBuf, $buffer2)) {
|
|
|
675 |
$strBuffer = $buffer2[1];
|
|
|
676 |
} else {
|
|
|
677 |
return false;
|
|
|
678 |
}
|
|
|
679 |
} else {
|
|
|
680 |
if (CommonFunctions::executeProgram('reg', 'query "'.substr($strName, 0, $last).'" /v '.$keyname.' 2>&1', $strBuf, $booErrorRep) && (strlen($strBuf) > 0) && preg_match("/^\s*".$keyname."\s+REG_\S+\s+(.+)\s*$/mi", $strBuf, $buffer2)) {
|
|
|
681 |
$strBuffer = $buffer2[1];
|
|
|
682 |
} else {
|
|
|
683 |
return false;
|
|
|
684 |
}
|
|
|
685 |
}
|
|
|
686 |
} elseif (gettype($reg) === "object") {
|
|
|
687 |
try {
|
|
|
688 |
$strBuffer = $reg->RegRead($strName);
|
|
|
689 |
} catch (Exception $e) {
|
|
|
690 |
if ($booErrorRep) {
|
|
|
691 |
$error = PSI_Error::singleton();
|
|
|
692 |
$error->addError("readReg()", preg_replace('/<br\/>/', "\n", preg_replace('/<b>|<\/b>/', '', $e->getMessage())));
|
|
|
693 |
}
|
|
|
694 |
|
|
|
695 |
return false;
|
|
|
696 |
}
|
|
|
697 |
}
|
|
|
698 |
|
|
|
699 |
return true;
|
|
|
700 |
}
|
|
|
701 |
|
|
|
702 |
|
|
|
703 |
/**
|
|
|
704 |
* enumKey function
|
|
|
705 |
*
|
|
|
706 |
* @return boolean command successfull or not
|
|
|
707 |
*/
|
|
|
708 |
public static function enumKey($key, $strName, &$arrBuffer, $booErrorRep = true)
|
|
|
709 |
{
|
|
|
710 |
$_hkey = array('HKEY_CLASSES_ROOT'=>0x80000000, 'HKEY_CURRENT_USER'=>0x80000001, 'HKEY_LOCAL_MACHINE'=>0x80000002, 'HKEY_USERS'=>0x80000003, 'HKEY_PERFORMANCE_DATA'=>0x80000004, 'HKEY_PERFORMANCE_TEXT'=>0x80000050, 'HKEY_PERFORMANCE_NLSTEXT'=>0x80000060, 'HKEY_CURRENT_CONFIG'=>0x80000005, 'HKEY_DYN_DATA'=>0x80000006);
|
|
|
711 |
|
|
|
712 |
$arrBuffer = array();
|
|
|
713 |
if ($key === false) {
|
|
|
714 |
if (CommonFunctions::$_cp) {
|
|
|
715 |
if (CommonFunctions::executeProgram('cmd', '/c chcp '.CommonFunctions::$_cp.' && reg query "'.$strName.'" 2>&1', $strBuf, $booErrorRep) && (strlen($strBuf) > 0) && preg_match_all("/^".preg_replace("/\\\\/", "\\\\\\\\", $strName)."\\\\(.*)/mi", $strBuf, $buffer2)) {
|
|
|
716 |
foreach ($buffer2[1] as $sub_key) {
|
|
|
717 |
$arrBuffer[] = trim($sub_key);
|
|
|
718 |
}
|
|
|
719 |
} else {
|
|
|
720 |
return false;
|
|
|
721 |
}
|
|
|
722 |
} else {
|
|
|
723 |
if (CommonFunctions::executeProgram('reg', 'query "'.$strName.'" 2>&1', $strBuf, $booErrorRep) && (strlen($strBuf) > 0) && preg_match_all("/^".preg_replace("/\\\\/", "\\\\\\\\", $strName)."\\\\(.*)/mi", $strBuf, $buffer2)) {
|
|
|
724 |
foreach ($buffer2[1] as $sub_key) {
|
|
|
725 |
$arrBuffer[] = trim($sub_key);
|
|
|
726 |
}
|
|
|
727 |
} else {
|
|
|
728 |
return false;
|
|
|
729 |
}
|
|
|
730 |
}
|
|
|
731 |
} elseif (gettype($key) === "object") {
|
|
|
732 |
$first = strpos($strName, "\\");
|
|
|
733 |
$hkey = substr($strName, 0, $first);
|
|
|
734 |
if (isset($_hkey[$hkey])) {
|
|
|
735 |
$sub_keys = new VARIANT();
|
|
|
736 |
try {
|
|
|
737 |
$key->EnumKey(strval($_hkey[$hkey]), substr($strName, $first+1), $sub_keys);
|
|
|
738 |
} catch (Exception $e) {
|
|
|
739 |
if ($booErrorRep) {
|
|
|
740 |
$error = PSI_Error::singleton();
|
|
|
741 |
$error->addError("enumKey()", preg_replace('/<br\/>/', "\n", preg_replace('/<b>|<\/b>/', '', $e->getMessage())));;
|
|
|
742 |
}
|
|
|
743 |
|
|
|
744 |
return false;
|
|
|
745 |
}
|
|
|
746 |
foreach ($sub_keys as $sub_key) {
|
|
|
747 |
$arrBuffer[] = $sub_key;
|
|
|
748 |
}
|
|
|
749 |
} else {
|
|
|
750 |
return false;
|
|
|
751 |
}
|
|
|
752 |
}
|
|
|
753 |
|
|
|
754 |
return true;
|
|
|
755 |
}
|
|
|
756 |
}
|