From a73793937ba81de1c861e2504db187727fe4ebce Mon Sep 17 00:00:00 2001 From: Robert Date: Thu, 14 Dec 2023 00:01:08 -0500 Subject: [PATCH] win/linux ver checking updates... --- app/neato_init.php | 108 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 83 insertions(+), 25 deletions(-) diff --git a/app/neato_init.php b/app/neato_init.php index a3a6b3d..3d022b8 100644 --- a/app/neato_init.php +++ b/app/neato_init.php @@ -4,23 +4,18 @@ function is_cli() { if (defined('STDIN')) { return true; } - if (php_sapi_name() === 'cli') { return true; } - if (array_key_exists('SHELL', $_ENV)) { return true; } - if (empty($_SERVER['REMOTE_ADDR']) and ! isset($_SERVER['HTTP_USER_AGENT']) and count($_SERVER['argv']) > 0) { return true; } - if (!array_key_exists('REQUEST_METHOD', $_SERVER)) { return true; } - return false; } @@ -29,40 +24,96 @@ if (is_cli() === false) { exit(1); } +function OS_details() { + $kernel_version = php_uname('r'); + $system_architecture = php_uname('m'); + return ['kernel'=>$kernel_version, 'architecture'=>$system_architecture]; +} + function get_Windows_version() { $os_info = php_uname(); - - // Regular expression pattern to extract version and build number $pattern = '/Windows\sNT\s(\d+\.\d+).*build\s(\d+)/'; - - // Perform the regular expression match preg_match($pattern, $os_info, $matches); - - // Check if the match was successful if ($matches && count($matches) >= 3) { - // Extracted version and build number $version = $matches[1]; $build_number = $matches[2]; - - // Return as an array return ['version' => $version, 'build' => $build_number]; } else { - // Return an empty array or handle the case when information is not found - return []; + $windows_info = explode(" ", $os_info); + $windows_version = end($windows_info); + return ['version' => $windows_version, 'build' => 'unknown']; } } +function get_based_on_distribution($distributionName) { + $distributions = [ + 'ubuntu' => 'debian', + 'debian' => 'unknown', + 'fedora' => 'red hat', + 'centos' => 'red hat', + 'arch' => 'unknown', + 'opensuse' => 'suse', + 'gentoo' => 'unknown', + 'slackware' => 'unknown', + 'mageia' => 'mandriva', + 'manjaro' => 'arch', + 'alpine' => 'unknown', + 'mint' => 'ubuntu', + 'elementary' => 'ubuntu', + 'kali' => 'debian', + 'red hat enterprise linux' => 'red hat', + 'oracle linux' => 'red hat', + 'scientific linux' => 'red hat', + 'clear linux' => 'unknown', + ]; + $distributionName = strtolower($distributionName); + if (isset($distributions[$distributionName])) { + return $distributions[$distributionName]; + } else { + return 'unknown'; + } +} + +function get_Linux_version() { + $os_info = php_uname(); + $pattern = '/^Linux (.+?)(?:\s+|\()([\d.]+)(?:\).*)?$/'; + if (preg_match($pattern, $os_info, $matches)) { + $distribution = $matches[1]; + $version = $matches[2]; + return ['name'=>$distribution,'version_codename'=>null,'version_id'=>$version, 'id_like'=>get_based_on_distribution($distribution),'id'=>strtolower($distribution),'linux'=>true]; + } + if (function_exists("exec")) { + $codename = "unknown"; + exec('lsb_release -sc', $output, $returnCode); + if ($returnCode === 0 && !empty($output)) { + $codename = trim($output[0]); + } + unset($output); + $version = "unknown"; + exec('lsb_release -rs', $output, $returnCode); + if ($returnCode === 0 && !empty($output)) { + $version = trim($output[0]); + } + unset($output); + exec('lsb_release -si', $output, $return_code); + if ($return_code === 0 && !empty($output)) { + $distribution = trim($output[0]); + return ['name'=>$distribution, 'version_codename'=>$codename, 'version_id'=>$version, 'id_like'=>get_based_on_distribution($distribution),'id'=> strtolower($distribution),'linux'=>true]; + } + } + return null; +} + function get_OS_information() { if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { - // Get Windows version information $windows_version = get_Windows_version(); $build = $windows_version['build'] ?? false; $version = $windows_version['version'] ?? false; - return ['name' => 'Windows', 'id' => 'win', 'build'=>$build, 'version_id'=>$version, 'id_like'=>'Windows']; + return ['name' => 'Windows', 'version_codename'=>$build, 'version_id'=>$version, 'id_like'=>"win"+$version,'id' => $build,'linux'=>false]; } - if (false == function_exists("shell_exec") || false == is_readable("/etc/os-release")) { - return null; + if (function_exists("shell_exec") === false|| is_readable("/etc/os-release") === false) { + return get_Linux_version(); } $os = shell_exec('cat /etc/os-release'); @@ -80,7 +131,9 @@ function get_OS_information() { $v = preg_replace('/=|"/', '', $v); }); - return array_combine($list_ids, $list_val); + $a = array_combine($list_ids, $list_val); + $a['linux'] = true; + return $a; } function is_string_found(string $data, string $find): bool { @@ -95,7 +148,12 @@ function get_right(string $str, int $length): string { return substr($str, -$length); } -$os = get_OS_information(); -$osVer = (isset($os['version_id'])) ? $os['version_id'] : false; -$codeName = (isset($os['version_codename'])) ? $os['version_codename'] : false; -$os_like = (isset($os['id_like'])) ? $os['id_like'] : false; \ No newline at end of file +$os = get_OS_information(); +$details = OS_details(); +$os['kernel'] = $details['kernel'] ?? false; +$os['architecture'] = $details['architecture'] ?? false; +$osVer = $os['version_id'] ?? false; +$codeName = $os['version_codename'] ?? false; +$os_like = $os['id_like'] ?? false; + +//var_dump($os); exit; \ No newline at end of file