You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
248 lines
6.8 KiB
248 lines
6.8 KiB
<?php
|
|
/**
|
|
* Gets important OS data for the rest of the system.
|
|
*
|
|
* PHP version 8.3
|
|
*
|
|
* @category Util
|
|
* @package Neato
|
|
* @author Robert S. <tips@technowizardbob.com>
|
|
* @license https://mit-license.org/ MIT License
|
|
* @link https://git.mysnippetsofcode.com/tts/neatoDeploy
|
|
*/
|
|
|
|
/**
|
|
* Checks to make sure php is in CLI AKA the Command Line
|
|
*
|
|
* @return bool true if in CLI
|
|
*/
|
|
function isCLI()
|
|
{
|
|
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;
|
|
}
|
|
|
|
if (isCLI() === false) {
|
|
echo('Unable to Start');
|
|
exit(1);
|
|
}
|
|
|
|
function formatBytes(int $bytes, int $precision = 2): string {
|
|
$units = ['B', 'KB', 'MB', 'GB', 'TB'];
|
|
$bytes = max($bytes, 0);
|
|
$pow = floor(($bytes ? log($bytes) : 0) / log(1024));
|
|
$pow = min($pow, count($units) - 1);
|
|
$bytes /= (1 << (10 * $pow)); // Calculation: 1024^$pow
|
|
return round($bytes, $precision) . ' ' . $units[$pow];
|
|
}
|
|
|
|
/**
|
|
* Grabs the Kernel and System Architecture using PHP uname.
|
|
*
|
|
* @return array about version of Kernal and CPU Type.
|
|
*/
|
|
function OS_details(): array
|
|
{
|
|
$kernel_version = php_uname('r');
|
|
$system_architecture = php_uname('m');
|
|
return ['kernel'=>$kernel_version, 'architecture'=>$system_architecture];
|
|
}
|
|
|
|
/**
|
|
* Grabs OS version and build.
|
|
*
|
|
* @return array Windows version and build number.
|
|
*/
|
|
function getWindowsVersion(): array
|
|
{
|
|
$os_info = php_uname();
|
|
$pattern = '/Windows\sNT\s(\d+\.\d+).*build\s(\d+)/';
|
|
preg_match($pattern, $os_info, $matches);
|
|
if ($matches && count($matches) >= 3) {
|
|
$version = $matches[1];
|
|
$build_number = $matches[2];
|
|
return ['version' => $version, 'build' => $build_number];
|
|
} else {
|
|
$windows_info = explode(" ", $os_info);
|
|
$windows_version = end($windows_info);
|
|
return ['version' => $windows_version, 'build' => 'unknown'];
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Fetch base OS name
|
|
*
|
|
* @param string $distributionName OS name
|
|
*
|
|
* @return string what is it based off of...Debian as an example
|
|
*/
|
|
function getBasedOnDistribution(string $distributionName): string
|
|
{
|
|
$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';
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Grabs a bunch of OS details like codename, name, etc...
|
|
*
|
|
* @return null|array Linux OS details
|
|
*/
|
|
function getLinuxVersion(): ?array
|
|
{
|
|
$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'=>getBasedOnDistribution($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'=>getBasedOnDistribution($distribution),'id'=> strtolower($distribution),'linux'=>true];
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Grab as much OS details and return as an array.
|
|
*
|
|
* @return null|array OS Details
|
|
*/
|
|
function getOSInformation(): ?array
|
|
{
|
|
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
|
|
$windows_version = getWindowsVersion();
|
|
$build = $windows_version['build'] ?? false;
|
|
$version = $windows_version['version'] ?? false;
|
|
return ['name' => 'Windows', 'version_codename'=>$build, 'version_id'=>$version, 'id_like'=>"win"+$version,'id' => $build,'linux'=>false];
|
|
}
|
|
|
|
if (function_exists("shell_exec") === false|| is_readable("/etc/os-release") === false) {
|
|
return getLinuxVersion();
|
|
}
|
|
|
|
$os = shell_exec('cat /etc/os-release');
|
|
$list_ids = preg_match_all('/.*=/', $os, $match_list_ids);
|
|
$list_ids = $match_list_ids[0];
|
|
|
|
$list_val = preg_match_all('/=.*/', $os, $match_list_val);
|
|
$list_val = $match_list_val[0];
|
|
|
|
array_walk(
|
|
$list_ids, function (&$v, $k) {
|
|
$v = strtolower(str_replace('=', '', $v));
|
|
}
|
|
);
|
|
|
|
array_walk(
|
|
$list_val, function (&$v, $k) {
|
|
$v = preg_replace('/=|"/', '', $v);
|
|
}
|
|
);
|
|
|
|
$a = array_combine($list_ids, $list_val);
|
|
$a['linux'] = true;
|
|
return $a;
|
|
}
|
|
|
|
/**
|
|
* Like str_contains.
|
|
*
|
|
* @param string $data what to look at
|
|
* @param string $find Search for this
|
|
*
|
|
* @return bool was the string found
|
|
*/
|
|
function isStringFound(string $data, string $find): bool
|
|
{
|
|
return (stripos($data, $find) !== false);
|
|
}
|
|
|
|
/**
|
|
* Get left most part of a string...
|
|
*
|
|
* @param string $str input
|
|
* @param int $length How much to get
|
|
*
|
|
* @return string data from the left most to X length.
|
|
*/
|
|
function getLeft(string $str, int $length): string
|
|
{
|
|
return substr($str, 0, $length);
|
|
}
|
|
|
|
/**
|
|
* Get right most part of a string...
|
|
*
|
|
* @param string $str input
|
|
* @param int $length How much to get
|
|
*
|
|
* @return string data from the left most to X length.
|
|
*/
|
|
function getRight(string $str, int $length): string
|
|
{
|
|
return substr($str, -$length);
|
|
}
|
|
|
|
$os = getOSInformation();
|
|
$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;
|