TrackManiaControl/core/Utils/SystemUtil.php

222 lines
5.6 KiB
PHP
Raw Normal View History

2014-06-15 00:51:43 +02:00
<?php
namespace ManiaControl\Utils;
use ManiaControl\Logger;
2014-06-15 00:51:43 +02:00
/**
* System Utility Class
*
* @author ManiaControl Team <mail@maniacontrol.com>
2020-01-22 10:39:35 +01:00
* @copyright 2014-2020 ManiaControl Team
2014-06-15 00:51:43 +02:00
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
*/
class SystemUtil {
/*
* Constants
*/
const OS_UNIX = 'Unix';
const OS_WIN = 'Windows';
const MIN_PHP_VERSION = '5.4';
2014-06-15 00:51:43 +02:00
/**
* Get whether ManiaControl is running on Windows
*
* @return bool
*/
public static function isWindows() {
return (self::getOS() === self::OS_WIN);
}
/**
* Get the Operating System on which ManiaControl is running
*
* @return string
*/
public static function getOS() {
if (defined('PHP_WINDOWS_VERSION_MAJOR')) {
return self::OS_WIN;
}
return self::OS_UNIX;
}
/**
2014-08-02 20:51:48 +02:00
* Check for the requirements to run ManiaControl
2014-06-17 22:25:33 +02:00
*/
public static function checkRequirements() {
$success = true;
// Check for min PHP version
$phpVersion = phpversion();
$message = 'Checking for minimum required PHP-Version ' . self::MIN_PHP_VERSION . ' ... ';
if ($phpVersion < self::MIN_PHP_VERSION) {
Logger::log($message . $phpVersion . ' TOO OLD VERSION!');
Logger::log(' -- Make sure that you install at least PHP ' . self::MIN_PHP_VERSION . '!');
2014-06-17 22:25:33 +02:00
$success = false;
} else {
2017-02-04 11:57:36 +01:00
Logger::log($message . $phpVersion . ' OK!');
2014-06-17 22:25:33 +02:00
}
// Check for MySQLi
$message = 'Checking for installed MySQLi ... ';
if (!extension_loaded('mysqli')) {
Logger::log($message . 'NOT FOUND!');
Logger::log(" -- You don't have MySQLi installed! Check: http://www.php.net/manual/en/mysqli.installation.php");
2014-06-17 22:25:33 +02:00
$success = false;
} else {
Logger::log($message . 'FOUND!');
2014-06-17 22:25:33 +02:00
}
// Check for cURL
$message = 'Checking for installed cURL ... ';
if (!extension_loaded('curl')) {
Logger::log($message . 'NOT FOUND!');
Logger::log(" -- You don't have cURL installed! Check: http://www.php.net/manual/en/curl.installation.php");
2014-06-17 22:25:33 +02:00
$success = false;
} else {
Logger::log($message . 'FOUND!');
2014-06-17 22:25:33 +02:00
}
// Check for ZIP
$message = 'Checking for installed PHP ZIP ... ';
if (!extension_loaded('zip')) {
Logger::log($message . 'NOT FOUND!');
Logger::log(" -- You don't have php-zip installed! Check: http://at1.php.net/manual/en/zip.installation.php");
$success = false;
} else {
Logger::log($message . 'FOUND!');
}
2015-02-24 10:21:27 +01:00
// Check for Zlib
$message = 'Checking for installed Zlib ... ';
if (!extension_loaded('zlib')) {
Logger::log($message . 'NOT FOUND!');
Logger::log(" -- You don't have Zlib installed! Check: http://php.net/manual/en/zlib.setup.php");
2015-02-24 10:21:27 +01:00
$success = false;
} else {
Logger::log($message . 'FOUND!');
}
// Check for MBString
$message = 'Checking for installed mbstring ... ';
if (!extension_loaded('mbstring')) {
Logger::log($message . 'NOT FOUND!');
Logger::log(" -- You don't have mbstring installed! Check: http://php.net/manual/en/mbstring.setup.php");
$success = false;
} else {
Logger::log($message . 'FOUND!');
}
2014-06-17 22:25:33 +02:00
if (!$success) {
// Missing requirements
2014-08-02 20:51:48 +02:00
self::quit();
}
}
/**
* Stop ManiaControl immediately
*
* @param string $message
* @param bool $errorPrefix
*/
public static function quit($message = null, $errorPrefix = false) {
if ($message) {
if ($errorPrefix) {
Logger::logError($message);
2014-08-05 02:17:41 +02:00
} else {
Logger::log($message);
2014-08-02 20:51:48 +02:00
}
}
2017-04-15 13:21:49 +02:00
if (!defined('PHP_UNIT_TEST')) {
exit;
}
2014-08-02 20:51:48 +02:00
}
/**
* Reboot ManiaControl immediately
2014-08-02 20:51:48 +02:00
*/
public static function reboot() {
2014-08-02 20:51:48 +02:00
if (SystemUtil::isUnix()) {
self::rebootUnix();
2014-08-02 20:51:48 +02:00
} else {
self::rebootWindows();
2014-08-02 20:51:48 +02:00
}
}
/**
* Get whether ManiaControl is running on Unix
*
* @return bool
*/
public static function isUnix() {
return (self::getOS() === self::OS_UNIX);
}
/**
* Perform restart on Unix
*/
private static function rebootUnix() {
2014-08-02 20:51:48 +02:00
if (!SystemUtil::checkFunctionAvailability('exec')) {
Logger::log("Can't reboot ManiaControl because the function 'exec' is disabled!");
2014-08-02 20:51:48 +02:00
return;
}
$fileName = null;
if ($scriptName = CommandLineHelper::getParameter('-sh')) {
$fileName = $scriptName;
} else {
$fileName = 'ManiaControl.sh';
}
$filePath = MANIACONTROL_PATH . $fileName;
if (!is_readable($filePath)) {
Logger::log("Can't reboot ManiaControl because the file '{$fileName}' doesn't exist or isn't readable!");
2014-08-02 20:51:48 +02:00
return;
}
$command = 'sh ' . escapeshellarg($filePath) . ' > /dev/null &';
2014-08-02 20:51:48 +02:00
exec($command);
}
/**
* Check whether the given function is available
*
* @param string $functionName
* @return bool
*/
public static function checkFunctionAvailability($functionName) {
return (function_exists($functionName) && !in_array($functionName, self::getDisabledFunctions()));
}
/**
* Get the array of disabled functions
*
* @return array
*/
protected static function getDisabledFunctions() {
$disabledText = ini_get('disable_functions');
return explode(',', $disabledText);
}
/**
* Perform reboot on Windows
2014-08-02 20:51:48 +02:00
*/
private static function rebootWindows() {
2014-08-02 20:51:48 +02:00
if (!SystemUtil::checkFunctionAvailability('system')) {
Logger::log("Can't reboot ManiaControl because the function 'system' is disabled!");
2014-08-02 20:51:48 +02:00
return;
2014-06-17 22:25:33 +02:00
}
$fileName = null;
if ($scriptName = CommandLineHelper::getParameter('-bat')) {
$fileName = $scriptName;
} else {
$fileName = 'ManiaControl.bat';
}
$filePath = MANIACONTROL_PATH . $fileName;
if (!is_readable($filePath)) {
Logger::log("Can't reboot ManiaControl because the file '{$fileName}' doesn't exist or isn't readable!");
return;
}
$command = escapeshellarg($filePath);
system($command); // TODO: windows stops here as long as controller is running
2014-06-17 22:25:33 +02:00
}
2014-06-15 00:51:43 +02:00
}