* Mon May 12 2025 Brian Read <brianr@koozali.org> 11.0.0-1.sme
- Adding SM2 panel [SME: 13004] - Upgrade to phpsysinfo 3.4.4 - Add code to delete inline styles and add css to make it look better. - version saved / built uses the static version, which means no drops downs and choices.
This commit is contained in:
@@ -8,7 +8,7 @@
|
||||
* @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
|
||||
* @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
|
||||
*/
|
||||
@@ -19,12 +19,19 @@
|
||||
* @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
|
||||
* @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)=="+"))) {
|
||||
@@ -56,24 +63,32 @@ class CommonFunctions
|
||||
*
|
||||
* @param string $strProgram name of the program
|
||||
*
|
||||
* @return string complete path and name of the program
|
||||
* @return string|null complete path and name of the program
|
||||
*/
|
||||
private static function _findProgram($strProgram)
|
||||
public static function _findProgram($strProgram)
|
||||
{
|
||||
$path_parts = pathinfo($strProgram);
|
||||
if (empty($path_parts['basename'])) {
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
$arrPath = array();
|
||||
if ((PSI_OS == 'WINNT') && empty($path_parts['extension'])) {
|
||||
$strProgram .= '.exe';
|
||||
$path_parts = pathinfo($strProgram);
|
||||
}
|
||||
|
||||
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') {
|
||||
$arrPath = preg_split('/;/', getenv("Path"), -1, PREG_SPLIT_NO_EMPTY);
|
||||
if (self::readenv('Path', $serverpath)) {
|
||||
$arrPath = preg_split('/;/', $serverpath, -1, PREG_SPLIT_NO_EMPTY);
|
||||
}
|
||||
} else {
|
||||
$arrPath = preg_split('/:/', getenv("PATH"), -1, PREG_SPLIT_NO_EMPTY);
|
||||
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)) {
|
||||
@@ -82,13 +97,13 @@ class CommonFunctions
|
||||
$arrPath = array_merge(array(PSI_ADD_PATHS), $arrPath); // In this order so $addpaths is before $arrPath when looking for a program
|
||||
}
|
||||
}
|
||||
} else {
|
||||
} 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 (empty($arrPath) && (PSI_OS != 'WINNT')) {
|
||||
if (PSI_OS == 'Android') {
|
||||
array_push($arrPath, '/system/bin');
|
||||
} else {
|
||||
@@ -97,97 +112,74 @@ class CommonFunctions
|
||||
}
|
||||
|
||||
$exceptPath = "";
|
||||
if ((PSI_OS == 'WINNT') && (($windir = getenv("WinDir")) !== false)) {
|
||||
$windir = strtolower($windir);
|
||||
if ((PSI_OS == 'WINNT') && self::readenv('WinDir', $windir)) {
|
||||
foreach ($arrPath as $strPath) {
|
||||
if ((strtolower($strPath) == $windir."\\system32") && is_dir($windir."\\SysWOW64")) {
|
||||
$exceptPath = $windir."\\sysnative";
|
||||
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;
|
||||
}
|
||||
}
|
||||
} else if (PSI_OS == 'Android') {
|
||||
$exceptPath = '/system/bin';
|
||||
} elseif (PSI_OS == 'Android') {
|
||||
$exceptPath = '/system/bin';
|
||||
}
|
||||
|
||||
// If open_basedir defined, fill the $open_basedir array with authorized paths,. (Not tested when no open_basedir restriction)
|
||||
if ((bool) ini_get('open_basedir')) {
|
||||
if (PSI_OS == 'WINNT') {
|
||||
$open_basedir = preg_split('/;/', ini_get('open_basedir'), -1, PREG_SPLIT_NO_EMPTY);
|
||||
} else {
|
||||
$open_basedir = preg_split('/:/', ini_get('open_basedir'), -1, PREG_SPLIT_NO_EMPTY);
|
||||
}
|
||||
}
|
||||
foreach ($arrPath as $strPath) {
|
||||
// Path with trailing slash
|
||||
// Path with and without trailing slash
|
||||
if (PSI_OS == 'WINNT') {
|
||||
$strPathS = rtrim($strPath, "\\")."\\";
|
||||
$strPath = rtrim($strPath, "\\");
|
||||
$strPathS = $strPath."\\";
|
||||
} else {
|
||||
$strPathS = rtrim($strPath, "/")."/";
|
||||
}
|
||||
// To avoid "open_basedir restriction in effect" error when testing paths if restriction is enabled
|
||||
if (isset($open_basedir)) {
|
||||
$inBaseDir = false;
|
||||
if (PSI_OS == 'WINNT') {
|
||||
foreach ($open_basedir as $openbasedir) {
|
||||
if (substr($openbasedir, -1)=="\\") {
|
||||
$str_Path = $strPathS;
|
||||
} else {
|
||||
$str_Path = $strPath;
|
||||
}
|
||||
if (stripos($str_Path, $openbasedir) === 0) {
|
||||
$inBaseDir = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
foreach ($open_basedir as $openbasedir) {
|
||||
if (substr($openbasedir, -1)=="/") {
|
||||
$str_Path = $strPathS;
|
||||
} else {
|
||||
$str_Path = $strPath;
|
||||
}
|
||||
if (strpos($str_Path, $openbasedir) === 0) {
|
||||
$inBaseDir = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if ($inBaseDir == false) {
|
||||
continue;
|
||||
}
|
||||
$strPath = rtrim($strPath, "/");
|
||||
$strPathS = $strPath."/";
|
||||
}
|
||||
if (($strPath !== $exceptPath) && !is_dir($strPath)) {
|
||||
continue;
|
||||
}
|
||||
if (PSI_OS == 'WINNT') {
|
||||
$strProgrammpath = rtrim($strPath, "\\")."\\".$strProgram;
|
||||
} else {
|
||||
$strProgrammpath = rtrim($strPath, "/")."/".$strProgram;
|
||||
}
|
||||
if (is_executable($strProgrammpath)) {
|
||||
$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 checking. you need ' | ' for it to work
|
||||
* 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 $strArgs arguments to 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, $strArgs, &$strBuffer, $booErrorRep = true)
|
||||
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: ".trim($strProgramname.' '.$strArgs));
|
||||
$out = self::_parse_log_file("Executing: ".$strAll);
|
||||
if ($out == false) {
|
||||
if (substr(PSI_LOG, 0, 1)=="-") {
|
||||
$strBuffer = '';
|
||||
@@ -201,42 +193,164 @@ class CommonFunctions
|
||||
}
|
||||
}
|
||||
|
||||
$strBuffer = '';
|
||||
$strError = '';
|
||||
$pipes = array();
|
||||
$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 = Error::singleton();
|
||||
$error = PSI_Error::singleton();
|
||||
if (!$strProgram) {
|
||||
if ($booErrorRep) {
|
||||
$error->addError('find_program('.$strProgramname.')', 'program not found on the machine');
|
||||
if ($booErrorRep || $externally) {
|
||||
$error->addError('find_program("'.$strProgramname.'")', 'program not found on the machine');
|
||||
}
|
||||
|
||||
return false;
|
||||
} else {
|
||||
if (preg_match('/\s/', $strProgram)) {
|
||||
$strProgram = '"'.$strProgram.'"';
|
||||
}
|
||||
}
|
||||
// see if we've gotten a |, 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] == '|') {
|
||||
$strCmd = $arrArgs[$i + 1];
|
||||
$strNewcmd = self::_findProgram($strCmd);
|
||||
$strArgs = preg_replace("/\| ".$strCmd.'/', '| "'.$strNewcmd.'"', $strArgs);
|
||||
|
||||
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 (defined("PSI_MODE_POPEN") && PSI_MODE_POPEN === true) {
|
||||
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('"'.$strProgram.'" '.$strArgs." 2>nul", "r");
|
||||
$process = $pipes[1] = popen($strSet.$strProgram.$strArgs." 2>nul", "r");
|
||||
} else {
|
||||
$process = $pipes[1] = popen('"'.$strProgram.'" '.$strArgs." 2>/dev/null", "r");
|
||||
$process = $pipes[1] = popen($strSet.$strProgram.$strArgs." 2>/dev/null", "r");
|
||||
}
|
||||
} else {
|
||||
$process = proc_open('"'.$strProgram.'" '.$strArgs, $descriptorspec, $pipes);
|
||||
$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)) {
|
||||
self::_timeoutfgets($pipes, $strBuffer, $strError);
|
||||
if (defined("PSI_MODE_POPEN") && PSI_MODE_POPEN === true) {
|
||||
$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]);
|
||||
@@ -244,7 +358,12 @@ class CommonFunctions
|
||||
fclose($pipes[2]);
|
||||
// It is important that you close any pipes before calling
|
||||
// proc_close in order to avoid a deadlock
|
||||
$return_value = proc_close($process);
|
||||
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) {
|
||||
@@ -256,7 +375,7 @@ class CommonFunctions
|
||||
$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: ".trim($strProgramname.' '.$strArgs)."\n".$strBuffer."\n", 3, PSI_LOG);
|
||||
error_log("---".gmdate('r T')."--- Executing: ".$strAll."\n".$strBuffer."\n", 3, PSI_LOG);
|
||||
}
|
||||
if (! empty($strError)) {
|
||||
if ($booErrorRep) {
|
||||
@@ -269,19 +388,74 @@ class CommonFunctions
|
||||
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 integer $intLines control how many lines should be read
|
||||
* @param integer $intBytes control how many bytes of each line should be read
|
||||
* @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) {
|
||||
@@ -297,12 +471,18 @@ class CommonFunctions
|
||||
}
|
||||
}
|
||||
|
||||
if (PSI_ROOT_FILESYSTEM !== '') {
|
||||
$rfsinfo = "[".PSI_ROOT_FILESYSTEM."]";
|
||||
} else {
|
||||
$rfsinfo = '';
|
||||
}
|
||||
|
||||
$strFile = "";
|
||||
$intCurLine = 1;
|
||||
$error = Error::singleton();
|
||||
if (file_exists($strFileName)) {
|
||||
if (is_readable($strFileName)) {
|
||||
if ($fd = fopen($strFileName, 'r')) {
|
||||
$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) {
|
||||
@@ -322,21 +502,21 @@ class CommonFunctions
|
||||
}
|
||||
} else {
|
||||
if ($booErrorRep) {
|
||||
$error->addError('fopen('.$strFileName.')', 'file can not read by phpsysinfo');
|
||||
$error->addError('fopen('.$rfsinfo.$strFileName.')', 'file can not read by phpsysinfo');
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
if ($booErrorRep) {
|
||||
$error->addError('fopen('.$strFileName.')', 'file permission error');
|
||||
}
|
||||
if ($booErrorRep) {
|
||||
$error->addError('fopen('.$rfsinfo.$strFileName.')', 'file permission error');
|
||||
}
|
||||
|
||||
return false;
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
if ($booErrorRep) {
|
||||
$error->addError('file_exists('.$strFileName.')', 'the file does not exist on your machine');
|
||||
$error->addError('file_exists('.$rfsinfo.$strFileName.')', 'the file does not exist on your machine');
|
||||
}
|
||||
|
||||
return false;
|
||||
@@ -345,6 +525,76 @@ class CommonFunctions
|
||||
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
|
||||
*
|
||||
@@ -354,6 +604,10 @@ class CommonFunctions
|
||||
*/
|
||||
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)
|
||||
@@ -367,7 +621,14 @@ class CommonFunctions
|
||||
}
|
||||
}
|
||||
|
||||
return file_exists($strFileName);
|
||||
$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;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -381,7 +642,7 @@ class CommonFunctions
|
||||
public static function gdc($strPath, $booErrorRep = true)
|
||||
{
|
||||
$arrDirectoryContent = array();
|
||||
$error = Error::singleton();
|
||||
$error = PSI_Error::singleton();
|
||||
if (is_dir($strPath)) {
|
||||
if ($handle = opendir($strPath)) {
|
||||
while (($strFile = readdir($handle)) !== false) {
|
||||
@@ -419,12 +680,12 @@ class CommonFunctions
|
||||
*/
|
||||
public static function checkForExtensions($arrExt = array())
|
||||
{
|
||||
if ((strcasecmp(PSI_SYSTEM_CODEPAGE, "UTF-8") == 0) || (strcasecmp(PSI_SYSTEM_CODEPAGE, "CP437") == 0))
|
||||
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', 'mbstring', 'dom', 'com_dotnet');
|
||||
elseif (PSI_OS == 'WINNT')
|
||||
$arrReq = array('simplexml', 'pcre', 'xml', 'dom', 'mbstring', 'com_dotnet');
|
||||
else
|
||||
$arrReq = array('simplexml', 'pcre', 'xml', 'mbstring', 'dom');
|
||||
$arrReq = array('simplexml', 'pcre', 'xml', 'dom', 'mbstring');
|
||||
$extensions = array_merge($arrExt, $arrReq);
|
||||
$text = "";
|
||||
$error = false;
|
||||
@@ -441,7 +702,7 @@ class CommonFunctions
|
||||
$text .= " </Error>\n";
|
||||
$text .= "</phpsysinfo>";
|
||||
if ($error) {
|
||||
header("Content-Type: text/xml\n\n");
|
||||
header('Content-Type: text/xml');
|
||||
echo $text;
|
||||
die();
|
||||
}
|
||||
@@ -450,19 +711,20 @@ class CommonFunctions
|
||||
/**
|
||||
* 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 integer $timeout timeout value in seconds (default value is 30)
|
||||
* @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 void
|
||||
* @return boolean timeout expired or not
|
||||
*/
|
||||
private static function _timeoutfgets($pipes, &$out, &$err, $timeout = 30)
|
||||
private static function _timeoutfgets($pipes, &$out, &$err, $timeout, $separator = '')
|
||||
{
|
||||
$w = null;
|
||||
$e = null;
|
||||
$te = false;
|
||||
|
||||
if (defined("PSI_MODE_POPEN") && PSI_MODE_POPEN === true) {
|
||||
if (defined("PSI_MODE_POPEN") && PSI_MODE_POPEN) {
|
||||
$pipe2 = false;
|
||||
} else {
|
||||
$pipe2 = true;
|
||||
@@ -481,66 +743,30 @@ class CommonFunctions
|
||||
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);
|
||||
} else if (feof($pipes[1]) && $pipe2 && ($r == $pipes[2])) {//read STDERR after STDOUT
|
||||
} elseif (feof($pipes[1]) && $pipe2 && ($r == $pipes[2])) {//read STDERR after STDOUT
|
||||
$err .= fread($r, 4096);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* function for getting a list of values in the specified context
|
||||
* optionally filter this list, based on the list from third parameter
|
||||
*
|
||||
* @param $wmi holds the COM object that we pull the WMI data from
|
||||
* @param string $strClass name of the class where the values are stored
|
||||
* @param array $strValue filter out only needed values, if not set all values of the class are returned
|
||||
*
|
||||
* @return array content of the class stored in an array
|
||||
*/
|
||||
public static function getWMI($wmi, $strClass, $strValue = array())
|
||||
{
|
||||
$arrData = array();
|
||||
if ($wmi) {
|
||||
$value = "";
|
||||
try {
|
||||
$objWEBM = $wmi->Get($strClass);
|
||||
$arrProp = $objWEBM->Properties_;
|
||||
$arrWEBMCol = $objWEBM->Instances_();
|
||||
foreach ($arrWEBMCol as $objItem) {
|
||||
if (is_array($arrProp)) {
|
||||
reset($arrProp);
|
||||
}
|
||||
$arrInstance = array();
|
||||
foreach ($arrProp as $propItem) {
|
||||
$value = $objItem->{$propItem->Name}; //instead exploitable eval("\$value = \$objItem->".$propItem->Name.";");
|
||||
if (empty($strValue)) {
|
||||
if (is_string($value)) $arrInstance[$propItem->Name] = trim($value);
|
||||
else $arrInstance[$propItem->Name] = $value;
|
||||
} else {
|
||||
if (in_array($propItem->Name, $strValue)) {
|
||||
if (is_string($value)) $arrInstance[$propItem->Name] = trim($value);
|
||||
else $arrInstance[$propItem->Name] = $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
$arrData[] = $arrInstance;
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
if (PSI_DEBUG) {
|
||||
$error = Error::singleton();
|
||||
$error->addError($e->getCode(), $e->getMessage());
|
||||
}
|
||||
// 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 $arrData;
|
||||
return $te;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -560,4 +786,111 @@ class CommonFunctions
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user