TrackManiaControl/core/Utils/SystemUtil.php

199 lines
4.9 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>
2017-02-04 11:49:23 +01:00
* @copyright 2014-2017 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 {
Logger::log($message . self::MIN_PHP_VERSION . ' 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
}
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/de/zlib.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
}
}
exit;
}
/**
* Restart ManiaControl immediately
*/
public static function restart() {
if (SystemUtil::isUnix()) {
self::restartUnix();
} else {
self::restartWindows();
}
}
/**
* 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 restartUnix() {
if (!SystemUtil::checkFunctionAvailability('exec')) {
Logger::log("Can't restart ManiaControl because the function 'exec' is disabled!");
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 restart 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 restart on Windows
*/
private static function restartWindows() {
if (!SystemUtil::checkFunctionAvailability('system')) {
Logger::log("Can't restart ManiaControl because the function 'system' is disabled!");
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 restart 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
}