From 573306ed55cb2648db6bf7f68f89b1d22e1a1f13 Mon Sep 17 00:00:00 2001 From: "Km.Van" Date: Fri, 10 Nov 2017 15:19:44 +0800 Subject: [PATCH] Fixed memory and CPU name errors in PHP5.3 --- CHANGELOG.md | 6 +++++- dist/prober.php | 4 ++-- src/Config/Api.php | 2 +- src/Helper/Api.php | 18 ++++++++++++------ 4 files changed, 20 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4261d0ff..2e55c295 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,11 @@ -# Changelog +# Changelog All Notable changes to `x-prober` will be documented in this file +## 1.1.1 - 2017-11-10 +### Fixed +- Fixed errors in PHP5.3 + ## 1.1.0 - 2017-11-09 ### Added - Added most PHP database checkers diff --git a/dist/prober.php b/dist/prober.php index 6f370168..377f59bc 100644 --- a/dist/prober.php +++ b/dist/prober.php @@ -3,7 +3,7 @@ \define('DEBUG', false); \define('LANG', 'eyJodHRwczpcL1wvZ2l0aHViLmNvbVwva212YW5cL3gtcHJvYmVyIjp7InpoLUNOIjoiaHR0cDpcL1wvZ2l0aHViLmNvbVwva212YW5cL3gtcHJvYmVyIn0sIklOTiBTVFVESU8iOnsiemgtQ04iOiJJTk4gU1RVRElPIn0sImh0dHBzOlwvXC9pbm4tc3R1ZGlvLmNvbVwvcHJvYmVyIjp7InpoLUNOIjoiaHR0cHM6XC9cL2lubi1zdHVkaW8uY29tXC9wcm9iZXIifSwiWCBQcm9iZXIiOnsiemgtQ04iOiJYIFx1NjNhMlx1OTQ4OCJ9LCJGb3VuZCB1cGRhdGUhIHtBUFBfTkFNRX0gaGFzIG5ldyB2ZXJzaW9uIHZ7QVBQX05FV19WRVJTSU9OfSI6eyJ6aC1DTiI6Ilx1NTNkMVx1NzNiMFx1NjZmNFx1NjViMFx1ZmYwMXtBUFBfTkFNRX0gXHU2NzA5XHU2NWIwXHU3MjQ4XHU2NzJjXHVmZjFhdntBUFBfTkVXX1ZFUl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J6aC1DTiI6Ilx1NGY1Y1x1ODAwNVx1NGUzYSAlcyJ9fQ=='); namespace InnStudio\Prober\Awesome; use InnStudio\Prober\Events\Api as Events; use InnStudio\Prober\I18n\Api as I18n; class Awesome { private $ID = 'awesome'; private $ZH_CN_URL = 'https://cdn.bootcss.com/font-awesome/4.7.0/css/font-awesome.min.css'; private $DEFAULT_URL = 'https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css'; public function __construct() { } public function filter() { ?> ZH_CN_URL; } return $this->DEFAULT_URL; } } -namespace InnStudio\Prober\Config; class Api { public static $APP_VERSION = '1.1.0'; public static $APP_NAME = 'X Prober'; public static $APP_URL = 'https://github.com/kmvan/x-prober'; public static $AUTHOR_URL = 'https://inn-studio.com/prober'; public static $AUTHOR_NAME = 'INN STUDIO'; public static $CHANGELOG_URL = 'https://raw.githubusercontent.com/kmvan/x-prober/master/CHANGELOG.md'; } +namespace InnStudio\Prober\Config; class Api { public static $APP_VERSION = '1.1.1'; public static $APP_NAME = 'X Prober'; public static $APP_URL = 'https://github.com/kmvan/x-prober'; public static $AUTHOR_URL = 'https://inn-studio.com/prober'; public static $AUTHOR_NAME = 'INN STUDIO'; public static $CHANGELOG_URL = 'https://raw.githubusercontent.com/kmvan/x-prober/master/CHANGELOG.md'; } namespace InnStudio\Prober\Database; use InnStudio\Prober\Events\Api as Events; use InnStudio\Prober\Helper\Api as Helper; use InnStudio\Prober\I18n\Api as I18n; class Database { private $ID = 'database'; public function __construct() { Events::patch('mods', array($this, 'filter'), 500); } public function filter($mods) { $mods[$this->ID] = array( 'title' => I18n::_('Database'), 'tinyTitle' => I18n::_('DB'), 'display' => array($this, 'display'), ); return $mods; } public function display() { ?>
getContent(); ?>
I18n::_('SQLite3'), 'content' => $sqlite3Version, ), array( 'title' => 'sqlite_libversion', 'label' => I18n::_('SQLite'), 'content' => \function_exists('\sqlite_libversion') ? Helper::getIni(0, true) . ' ' . \sqlite_libversion() : Helper::getIni(0, false), ), array( 'title' => 'mysqli_get_client_version', 'label' => I18n::_('MySQLi client'), 'content' => Helper::getIni(0, true) . ' ' . \mysqli_get_client_version(), ), array( 'label' => I18n::_('Mongo'), 'content' => \class_exists('\Mongo') ? \MongoClient::VERSION : Helper::getIni(0, false), ), array( 'label' => I18n::_('MongoDB'), 'content' => \class_exists('\MongoDB') ? Helper::getIni(0, true) : Helper::getIni(0, false), ), array( 'label' => I18n::_('PostgreSQL'), 'content' => \function_exists('\pg_connect') ? Helper::getIni(0, true) : Helper::getIni(0, false), ), array( 'label' => I18n::_('Paradox'), 'content' => \function_exists('\px_new') ? Helper::getIni(0, true) : Helper::getIni(0, false), ), array( 'title' => I18n::_('Microsoft SQL Server Driver for PHP'), 'label' => I18n::_('MS SQL'), 'content' => \function_exists('\sqlsrv_server_info') ? Helper::getIni(0, true) : Helper::getIni(0, false), ), array( 'label' => I18n::_('File Pro'), 'content' => \function_exists('\filepro') ? Helper::getIni(0, true) : Helper::getIni(0, false), ), array( 'label' => I18n::_('MaxDB client'), 'content' => \function_exists('\maxdb_get_client_version') ? \maxdb_get_client_version() : Helper::getIni(0, false), ), array( 'label' => I18n::_('MaxDB server'), 'content' => \function_exists('\maxdb_get_server_version') ? Helper::getIni(0, true) : Helper::getIni(0, false), ), ); $content = ''; foreach ($items as $item) { $title = isset($item['title']) ? "title=\"{$item['title']}\"" : ''; $col = isset($item['col']) ? $item['col'] : '1-3'; $id = isset($item['id']) ? "id=\"{$item['id']}\"" : ''; echo <<
@@ -55,7 +55,7 @@ text-decoration: underline; } (int) $numberArr[0], 'tx' => (int) $numberArr[8], ); } return $eths; } public static function getBtn($tx, $url) { return '' . $tx . ''; } public static function getDiskTotalSpace($human = false) { static $space = null; if (null === $space) { $space = \disk_total_space('/'); } if ( ! $space) { return 0; } if (true === $human) { return self::formatBytes($space); } return $space; } public static function getDiskFreeSpace($human = false) { static $space = null; if (null === $space) { $space = \disk_free_space('/'); } if ( ! $space) { return 0; } if (true === $human) { return self::formatBytes($space); } return $space; } public static function getCpuModel() { $filePath = '/proc/cpuinfo'; if ( ! \is_readable($filePath)) { return I18n::_('Unavailable'); } $content = \file_get_contents($filePath); $cores = \mb_substr_count($content, 'cache size'); $lines = \explode("\n", $content); $modelName = \trim(\explode(':', $lines[4])[1]); $cacheSize = \trim(\explode(':', $lines[8])[1]); return "{$cores} x {$modelName} / " . \sprintf(I18n::_('%s cache'), $cacheSize); } public static function getServerTime() { return \date('Y-m-d H:i:s'); } public static function getServerUpTime() { $filePath = '/proc/uptime'; if ( ! \is_readable($filePath)) { return I18n::_('Unavailable'); } $str = \file_get_contents($filePath); $num = (float) $str; $secs = \fmod($num, 60); $num = (int) ($num / 60); $mins = $num % 60; $num = (int) ($num / 60); $hours = $num % 24; $num = (int) ($num / 24); $days = $num; return \sprintf( I18n::_('%1$dd %2$dh %3$dm %4$ds'), $days, $hours, $mins, $secs ); } public static function getErrNameByCode($code) { switch ($code) { case E_ERROR: return 'E_ERROR'; case E_WARNING: return 'E_WARNING'; case E_PARSE: return 'E_PARSE'; case E_NOTICE: return 'E_NOTICE'; case E_CORE_ERROR: return 'E_CORE_ERROR'; case E_CORE_WARNING: return 'E_CORE_WARNING'; case E_COMPILE_ERROR: return 'E_COMPILE_ERROR'; case E_COMPILE_WARNING: return 'E_COMPILE_WARNING'; case E_USER_ERROR: return 'E_USER_ERROR'; case E_USER_WARNING: return 'E_USER_WARNING'; case E_USER_NOTICE: return 'E_USER_NOTICE'; case E_STRICT: return 'E_STRICT'; case E_RECOVERABLE_ERROR: return 'E_RECOVERABLE_ERROR'; case E_DEPRECATED: return 'E_DEPRECATED'; case E_USER_DEPRECATED: return 'E_USER_DEPRECATED'; case E_ALL: return 'E_ALL'; } return $code; } public static function getIni($id, $forceSet = null) { if (true === $forceSet) { $ini = 1; } elseif (false === $forceSet) { $ini = 0; } else { $ini = \ini_get($id); } if ( ! \is_numeric($ini) && '' !== (string) $ini) { return $ini; } if (1 === (int) $ini) { return ''; } elseif (0 === (int) $ini) { return '×'; } return $ini; } public static function isWin() { return PHP_OS === 'WINNT'; } public static function htmlMinify($buffer) { \preg_match_all('#\.*\<\/textarea\>#Uis', $buffer, $foundTxt); \preg_match_all('#\.*\<\/pre\>#Uis', $buffer, $foundPre); $textareas = array(); foreach (\array_keys($foundTxt[0]) as $item) { $textareas[] = ''; } $pres = array(); foreach (\array_keys($foundPre[0]) as $item) { $pres[] = '
' . $item . '
'; } $buffer = \str_replace($foundTxt[0], $textareas, $buffer); $buffer = \str_replace($foundPre[0], $pres, $buffer); $search = array( '/\>[^\S ]+/s', '/[^\S ]+\', '<', '\\1', ); $buffer = \preg_replace($search, $replace, $buffer); $textareas = array(); foreach (\array_keys($foundTxt[0]) as $item) { $textareas[] = ''; } $pres = array(); foreach (\array_keys($foundPre[0]) as $item) { $pres[] = '
' . $item . '
'; } $buffer = \str_replace($textareas, $foundTxt[0], $buffer); $buffer = \str_replace($pres, $foundPre[0], $buffer); return $buffer; } public static function getClientIp() { $keys = array('HTTP_X_FORWARDED_FOR', 'HTTP_CLIENT_IP', 'REMOTE_ADDR'); foreach ($keys as $key) { if ( ! isset($_SERVER[$key])) { continue; } $ip = \array_filter(\explode(',', $_SERVER[$key])); $ip = \filter_var(\end($ip), FILTER_VALIDATE_IP); if ($ip) { return $ip; } } return ''; } public static function getCpuUsage() { static $cpu = null; if (null !== $cpu) { return $cpu; } $filePath = ('/proc/stat'); if ( ! \is_readable($filePath)) { $cpu = array(); return $cpu; } $stat1 = \file($filePath); \sleep(1); $stat2 = \file($filePath); $info1 = \explode(' ', \preg_replace('!cpu +!', '', $stat1[0])); $info2 = \explode(' ', \preg_replace('!cpu +!', '', $stat2[0])); $dif = array(); $dif['user'] = $info2[0] - $info1[0]; $dif['nice'] = $info2[1] - $info1[1]; $dif['sys'] = $info2[2] - $info1[2]; $dif['idle'] = $info2[3] - $info1[3]; $total = \array_sum($dif); $cpu = array(); foreach ($dif as $x => $y) { $cpu[$x] = \round($y / $total * 100, 1); } return $cpu; } public static function getHumanCpuUsageDetail() { $cpu = self::getCpuUsage(); if ( ! $cpu) { return ''; } $html = ''; foreach ($cpu as $k => $v) { $html .= '' . $k . ' ' . $v . ''; } return $html; } public static function getHumanCpuUsage() { $cpu = self::getCpuUsage(); return $cpu ?: array(); } public static function getSysLoadAvg() { if (self::isWin()) { return I18n::_('Not support on Windows'); } $avg = \sys_getloadavg(); $avg[0] = '' . I18n::_('1 min:') . " {$avg[0]}"; $avg[1] = '' . I18n::_('5 min:') . " {$avg[1]}"; $avg[2] = '' . I18n::_('15 min:') . " {$avg[2]}"; return \implode('', $avg); } public static function getMemoryUsage($key) { $key = \ucfirst($key); if (self::isWin()) { return array(); } static $memInfo = null; if (null === $memInfo) { $memInfoFile = '/proc/meminfo'; if ( ! \is_readable($memInfoFile)) { $memInfo = array(); return array(); } $memInfo = \file_get_contents($memInfoFile); $memInfo = \str_replace(array( ' kB', ' ', ), '', $memInfo); $lines = array(); foreach (\explode("\n", $memInfo) as $line) { if ( ! $line) { continue; } $line = \explode(':', $line); $lines[$line[0]] = (int) $line[1]; } $memInfo = $lines; } switch ($key) { case 'MemRealUsage': if ( ! isset($memInfo['MemAvailable']) || ! isset($memInfo['MemTotal'])) { return 0; } return $memInfo['MemTotal'] - $memInfo['MemAvailable']; case 'SwapRealUsage': if ( ! isset($memInfo['SwapTotal']) || ! isset($memInfo['SwapFree']) || ! isset($memInfo['SwapCached'])) { return 0; } return $memInfo['SwapTotal'] - $memInfo['SwapFree'] - $memInfo['SwapCached']; } return isset($memInfo[$key]) ? (int) $memInfo[$key] : 0; } public static function formatBytes($bytes, $precision = 2) { if ( ! $bytes) { return 0; } $base = \log($bytes, 1024); $suffixes = array('', ' K', ' M', ' G', ' T'); return \round(\pow(1024, $base - \floor($base)), $precision) . $suffixes[\floor($base)]; } public static function getHumamMemUsage($key) { return self::formatBytes(self::getMemoryUsage($key) * 1024); } } +namespace InnStudio\Prober\Helper; use InnStudio\Prober\I18n\Api as I18n; class Api { public static function dieJson($data) { \header('Content-Type: application/json'); die(\json_encode($data)); } public static function isAction($action) { return \filter_input(INPUT_GET, 'action', FILTER_SANITIZE_STRING) === $action; } public static function getNetworkStats() { $filePath = '/proc/net/dev'; if ( ! \is_readable($filePath)) { return I18n::_('Unavailable'); } static $eths = null; if (null !== $eths) { return $eths; } $lines = \file($filePath); unset($lines[0], $lines[1]); $eths = array(); foreach ($lines as $line) { $line = \preg_replace('/\s+/', ' ', \trim($line)); $lineArr = \explode(':', $line); $numberArr = \explode(' ', \trim($lineArr[1])); $eths[$lineArr[0]] = array( 'rx' => (int) $numberArr[0], 'tx' => (int) $numberArr[8], ); } return $eths; } public static function getBtn($tx, $url) { return '' . $tx . ''; } public static function getDiskTotalSpace($human = false) { static $space = null; if (null === $space) { $space = \disk_total_space('/'); } if ( ! $space) { return 0; } if (true === $human) { return self::formatBytes($space); } return $space; } public static function getDiskFreeSpace($human = false) { static $space = null; if (null === $space) { $space = \disk_free_space('/'); } if ( ! $space) { return 0; } if (true === $human) { return self::formatBytes($space); } return $space; } public static function getCpuModel() { $filePath = '/proc/cpuinfo'; if ( ! \is_readable($filePath)) { return I18n::_('Unavailable'); } $content = \file_get_contents($filePath); $cores = \substr_count($content, 'cache size'); $lines = \explode("\n", $content); $modelName = \explode(':', $lines[4]); $modelName = \trim($modelName[1]); $cacheSize = \explode(':', $lines[8]); $cacheSize = \trim($cacheSize[1]); return "{$cores} x {$modelName} / " . \sprintf(I18n::_('%s cache'), $cacheSize); } public static function getServerTime() { return \date('Y-m-d H:i:s'); } public static function getServerUpTime() { $filePath = '/proc/uptime'; if ( ! \is_readable($filePath)) { return I18n::_('Unavailable'); } $str = \file_get_contents($filePath); $num = (float) $str; $secs = \fmod($num, 60); $num = (int) ($num / 60); $mins = $num % 60; $num = (int) ($num / 60); $hours = $num % 24; $num = (int) ($num / 24); $days = $num; return \sprintf( I18n::_('%1$dd %2$dh %3$dm %4$ds'), $days, $hours, $mins, $secs ); } public static function getErrNameByCode($code) { switch ($code) { case E_ERROR: return 'E_ERROR'; case E_WARNING: return 'E_WARNING'; case E_PARSE: return 'E_PARSE'; case E_NOTICE: return 'E_NOTICE'; case E_CORE_ERROR: return 'E_CORE_ERROR'; case E_CORE_WARNING: return 'E_CORE_WARNING'; case E_COMPILE_ERROR: return 'E_COMPILE_ERROR'; case E_COMPILE_WARNING: return 'E_COMPILE_WARNING'; case E_USER_ERROR: return 'E_USER_ERROR'; case E_USER_WARNING: return 'E_USER_WARNING'; case E_USER_NOTICE: return 'E_USER_NOTICE'; case E_STRICT: return 'E_STRICT'; case E_RECOVERABLE_ERROR: return 'E_RECOVERABLE_ERROR'; case E_DEPRECATED: return 'E_DEPRECATED'; case E_USER_DEPRECATED: return 'E_USER_DEPRECATED'; case E_ALL: return 'E_ALL'; } return $code; } public static function getIni($id, $forceSet = null) { if (true === $forceSet) { $ini = 1; } elseif (false === $forceSet) { $ini = 0; } else { $ini = \ini_get($id); } if ( ! \is_numeric($ini) && '' !== (string) $ini) { return $ini; } if (1 === (int) $ini) { return ''; } elseif (0 === (int) $ini) { return '×'; } return $ini; } public static function isWin() { return PHP_OS === 'WINNT'; } public static function htmlMinify($buffer) { \preg_match_all('#\.*\<\/textarea\>#Uis', $buffer, $foundTxt); \preg_match_all('#\.*\<\/pre\>#Uis', $buffer, $foundPre); $textareas = array(); foreach (\array_keys($foundTxt[0]) as $item) { $textareas[] = ''; } $pres = array(); foreach (\array_keys($foundPre[0]) as $item) { $pres[] = '
' . $item . '
'; } $buffer = \str_replace($foundTxt[0], $textareas, $buffer); $buffer = \str_replace($foundPre[0], $pres, $buffer); $search = array( '/\>[^\S ]+/s', '/[^\S ]+\', '<', '\\1', ); $buffer = \preg_replace($search, $replace, $buffer); $textareas = array(); foreach (\array_keys($foundTxt[0]) as $item) { $textareas[] = ''; } $pres = array(); foreach (\array_keys($foundPre[0]) as $item) { $pres[] = '
' . $item . '
'; } $buffer = \str_replace($textareas, $foundTxt[0], $buffer); $buffer = \str_replace($pres, $foundPre[0], $buffer); return $buffer; } public static function getClientIp() { $keys = array('HTTP_X_FORWARDED_FOR', 'HTTP_CLIENT_IP', 'REMOTE_ADDR'); foreach ($keys as $key) { if ( ! isset($_SERVER[$key])) { continue; } $ip = \array_filter(\explode(',', $_SERVER[$key])); $ip = \filter_var(\end($ip), FILTER_VALIDATE_IP); if ($ip) { return $ip; } } return ''; } public static function getCpuUsage() { static $cpu = null; if (null !== $cpu) { return $cpu; } $filePath = ('/proc/stat'); if ( ! \is_readable($filePath)) { $cpu = array(); return $cpu; } $stat1 = \file($filePath); \sleep(1); $stat2 = \file($filePath); $info1 = \explode(' ', \preg_replace('!cpu +!', '', $stat1[0])); $info2 = \explode(' ', \preg_replace('!cpu +!', '', $stat2[0])); $dif = array(); $dif['user'] = $info2[0] - $info1[0]; $dif['nice'] = $info2[1] - $info1[1]; $dif['sys'] = $info2[2] - $info1[2]; $dif['idle'] = $info2[3] - $info1[3]; $total = \array_sum($dif); $cpu = array(); foreach ($dif as $x => $y) { $cpu[$x] = \round($y / $total * 100, 1); } return $cpu; } public static function getHumanCpuUsageDetail() { $cpu = self::getCpuUsage(); if ( ! $cpu) { return ''; } $html = ''; foreach ($cpu as $k => $v) { $html .= '' . $k . ' ' . $v . ''; } return $html; } public static function getHumanCpuUsage() { $cpu = self::getCpuUsage(); return $cpu ?: array(); } public static function getSysLoadAvg() { if (self::isWin()) { return I18n::_('Not support on Windows'); } $avg = \sys_getloadavg(); $avg[0] = '' . I18n::_('1 min:') . " {$avg[0]}"; $avg[1] = '' . I18n::_('5 min:') . " {$avg[1]}"; $avg[2] = '' . I18n::_('15 min:') . " {$avg[2]}"; return \implode('', $avg); } public static function getMemoryUsage($key) { $key = \ucfirst($key); if (self::isWin()) { return array(); } static $memInfo = null; if (null === $memInfo) { $memInfoFile = '/proc/meminfo'; if ( ! \is_readable($memInfoFile)) { $memInfo = array(); return array(); } $memInfo = \file_get_contents($memInfoFile); $memInfo = \str_replace(array( ' kB', ' ', ), '', $memInfo); $lines = array(); foreach (\explode("\n", $memInfo) as $line) { if ( ! $line) { continue; } $line = \explode(':', $line); $lines[$line[0]] = (int) $line[1]; } $memInfo = $lines; } switch ($key) { case 'MemRealUsage': $memAvailable = 0; if (isset($memInfo['MemAvailable'])) { $memAvailable = $memInfo['MemAvailable']; } elseif (isset($memInfo['MemFree'])) { $memAvailable = $memInfo['MemFree']; } return $memInfo['MemTotal'] - $memAvailable; case 'SwapRealUsage': if ( ! isset($memInfo['SwapTotal']) || ! isset($memInfo['SwapFree']) || ! isset($memInfo['SwapCached'])) { return 0; } return $memInfo['SwapTotal'] - $memInfo['SwapFree'] - $memInfo['SwapCached']; } return isset($memInfo[$key]) ? (int) $memInfo[$key] : 0; } public static function formatBytes($bytes, $precision = 2) { if ( ! $bytes) { return 0; } $base = \log($bytes, 1024); $suffixes = array('', ' K', ' M', ' G', ' T'); return \round(\pow(1024, $base - \floor($base)), $precision) . $suffixes[\floor($base)]; } public static function getHumamMemUsage($key) { return self::formatBytes(self::getMemoryUsage($key) * 1024); } } namespace InnStudio\Prober\I18n; class Api { public static function _($str) { static $preDefineLang = null; if (null === $preDefineLang) { $preDefineLang = \json_decode(\base64_decode(LANG), true); } if ( ! isset($preDefineLang[$str])) { return $str; } $lang = $preDefineLang[$str]; $clientLang = self::getClientLang(); return isset($lang[$clientLang]) ? $lang[$clientLang] : $str; } public static function getClientLang() { static $cache = null; if (null !== $cache) { return $cache; } if ( ! isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { $cache = ''; return $cache; } $client = \explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']); if (isset($client[0])) { $cache = $client[0]; } else { $cache = ''; } return $cache; } } namespace InnStudio\Prober\MyInfo; use InnStudio\Prober\Events\Api as Events; use InnStudio\Prober\Helper\Api as Helper; use InnStudio\Prober\I18n\Api as I18n; class MyInfo { private $ID = 'myInfo'; public function __construct() { Events::patch('mods', array($this, 'filter'), 900); } public function filter($mods) { $mods[$this->ID] = array( 'title' => I18n::_('My information'), 'tinyTitle' => I18n::_('Mine'), 'display' => array($this, 'display'), ); return $mods; } public function display() { echo $this->getContent(); } public function getContent() { $ua = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : ''; return << diff --git a/src/Config/Api.php b/src/Config/Api.php index e857621a..d231e15a 100644 --- a/src/Config/Api.php +++ b/src/Config/Api.php @@ -4,7 +4,7 @@ class Api { - public static $APP_VERSION = '1.1.0'; + public static $APP_VERSION = '1.1.1'; public static $APP_NAME = 'X Prober'; public static $APP_URL = 'https://github.com/kmvan/x-prober'; public static $AUTHOR_URL = 'https://inn-studio.com/prober'; diff --git a/src/Helper/Api.php b/src/Helper/Api.php index 0e2b9f25..71a27abd 100644 --- a/src/Helper/Api.php +++ b/src/Helper/Api.php @@ -101,11 +101,13 @@ public static function getCpuModel() } $content = \file_get_contents($filePath); - $cores = \mb_substr_count($content, 'cache size'); + $cores = \substr_count($content, 'cache size'); $lines = \explode("\n", $content); - $modelName = \trim(\explode(':', $lines[4])[1]); - $cacheSize = \trim(\explode(':', $lines[8])[1]); + $modelName = \explode(':', $lines[4]); + $modelName = \trim($modelName[1]); + $cacheSize = \explode(':', $lines[8]); + $cacheSize = \trim($cacheSize[1]); return "{$cores} x {$modelName} / " . \sprintf(I18n::_('%s cache'), $cacheSize); } @@ -386,11 +388,15 @@ public static function getMemoryUsage($key) switch ($key) { case 'MemRealUsage': - if ( ! isset($memInfo['MemAvailable']) || ! isset($memInfo['MemTotal'])) { - return 0; + $memAvailable = 0; + + if (isset($memInfo['MemAvailable'])) { + $memAvailable = $memInfo['MemAvailable']; + } elseif (isset($memInfo['MemFree'])) { + $memAvailable = $memInfo['MemFree']; } - return $memInfo['MemTotal'] - $memInfo['MemAvailable']; + return $memInfo['MemTotal'] - $memAvailable; case 'SwapRealUsage': if ( ! isset($memInfo['SwapTotal']) || ! isset($memInfo['SwapFree']) || ! isset($memInfo['SwapCached'])) { return 0;