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