TrackManiaControl/application/core/ManiaControl.php

408 lines
12 KiB
PHP
Raw Normal View History

2013-11-10 17:29:21 +01:00
<?php
namespace ManiaControl;
2013-12-31 12:42:07 +01:00
use ManiaControl\Admin\ActionsMenu;
use ManiaControl\Admin\AuthenticationManager;
2014-02-25 18:34:35 +01:00
use ManiaControl\Bills\BillManager;
use ManiaControl\Callbacks\CallbackManager;
use ManiaControl\Callbacks\Callbacks;
2014-02-22 11:14:35 +01:00
use ManiaControl\Callbacks\TimerListener;
2014-01-30 22:35:32 +01:00
use ManiaControl\Callbacks\TimerManager;
use ManiaControl\Commands\CommandListener;
use ManiaControl\Commands\CommandManager;
use ManiaControl\Configurators\Configurator;
2014-05-02 17:40:47 +02:00
use ManiaControl\Database\Database;
2014-02-07 12:30:53 +01:00
use ManiaControl\Files\AsynchronousFileReader;
2014-02-07 00:27:10 +01:00
use ManiaControl\Files\FileUtil;
use ManiaControl\Manialinks\ManialinkManager;
use ManiaControl\Maps\MapManager;
use ManiaControl\Players\Player;
use ManiaControl\Players\PlayerManager;
use ManiaControl\Plugins\PluginManager;
use ManiaControl\Server\Server;
2014-01-27 17:29:48 +01:00
use ManiaControl\Settings\SettingManager;
use ManiaControl\Statistics\StatisticManager;
2014-02-10 22:54:49 +01:00
use ManiaControl\Update\UpdateManager;
use ManiaControl\Utils\CommandLineHelper;
use ManiaControl\Utils\Formatter;
2014-01-16 17:19:01 +01:00
use Maniaplanet\DedicatedServer\Connection;
use Maniaplanet\DedicatedServer\Xmlrpc\AuthenticationException;
use Maniaplanet\DedicatedServer\Xmlrpc\Exception;
2014-04-27 15:21:57 +02:00
use Maniaplanet\DedicatedServer\Xmlrpc\TransportException;
2013-12-31 12:42:07 +01:00
2014-02-18 15:54:36 +01:00
require_once __DIR__ . '/Libs/Maniaplanet/DedicatedServer/Connection.php';
require_once __DIR__ . '/Libs/GbxDataFetcher/gbxdatafetcher.inc.php';
require_once __DIR__ . '/Libs/FML/autoload.php';
2014-02-18 18:13:59 +01:00
require_once __DIR__ . '/Libs/Symfony/autoload.php';
require_once __DIR__ . '/Libs/curl-easy/autoload.php';
2013-11-10 17:29:21 +01:00
/**
* ManiaControl Server Controller for ManiaPlanet Server
2014-05-02 17:40:47 +02:00
*
* @author ManiaControl Team <mail@maniacontrol.com>
* @copyright 2014 ManiaControl Team
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
2013-11-10 17:29:21 +01:00
*/
2014-02-22 11:14:35 +01:00
class ManiaControl implements CommandListener, TimerListener {
/*
2013-11-10 17:29:21 +01:00
* Constants
*/
2014-05-25 10:11:50 +02:00
const VERSION = '0.151';
2014-05-02 17:40:47 +02:00
const API_VERSION = '2013-04-16';
const MIN_DEDIVERSION = '2014-04-02_18_00';
const OS_UNIX = 'Unix';
const OS_WIN = 'Windows';
2014-05-09 16:33:33 +02:00
const SCRIPT_TIMEOUT = 10;
2014-05-02 17:40:47 +02:00
const URL_WEBSERVICE = 'http://ws.maniacontrol.com/';
2014-01-31 16:55:01 +01:00
const SETTING_PERMISSION_SHUTDOWN = 'Shutdown ManiaControl';
2014-05-02 17:40:47 +02:00
const SETTING_PERMISSION_RESTART = 'Restart ManiaControl';
/*
* Public Properties
2013-11-10 17:29:21 +01:00
*/
2013-12-31 12:42:07 +01:00
public $actionsMenu = null;
public $authenticationManager = null;
public $callbackManager = null;
2013-11-10 17:29:21 +01:00
public $chat = null;
/** @var \SimpleXMLElement $config */
public $config = null;
public $configurator = null;
2014-05-10 10:12:48 +02:00
/** @var Connection $client */
2013-11-10 17:29:21 +01:00
public $client = null;
public $commandManager = null;
2013-11-10 17:29:21 +01:00
public $database = null;
public $manialinkManager = null;
public $mapManager = null;
public $playerManager = null;
public $pluginManager = null;
2013-11-10 17:29:21 +01:00
public $server = null;
public $settingManager = null;
2013-12-31 17:31:05 +01:00
public $statisticManager = null;
2014-05-02 15:35:52 +02:00
/** @var UpdateManager $updateManager */
2013-12-17 17:38:44 +01:00
public $updateManager = null;
2014-01-27 20:28:37 +01:00
public $errorHandler = null;
2014-01-30 22:35:32 +01:00
public $timerManager = null;
2014-02-07 12:30:53 +01:00
public $fileReader = null;
2014-02-25 18:34:35 +01:00
public $billManager = null;
2014-05-02 17:40:47 +02:00
/*
* Private Properties
2013-11-10 17:29:21 +01:00
*/
private $shutdownRequested = false;
/**
* Construct ManiaControl
*/
public function __construct() {
// Construct Error Handler
2014-01-27 20:28:37 +01:00
$this->errorHandler = new ErrorHandler($this);
2014-05-02 17:40:47 +02:00
2014-06-12 15:50:13 +02:00
$this->log('Loading ManiaControl v' . self::VERSION . ' ...');
2014-05-02 17:40:47 +02:00
$this->loadConfig();
2014-05-02 17:40:47 +02:00
2013-12-17 17:38:44 +01:00
// Load ManiaControl Modules
2014-05-02 17:40:47 +02:00
$this->callbackManager = new CallbackManager($this);
$this->timerManager = new TimerManager($this);
$this->database = new Database($this);
$this->fileReader = new AsynchronousFileReader($this);
$this->billManager = new BillManager($this);
$this->settingManager = new SettingManager($this);
$this->statisticManager = new StatisticManager($this);
$this->manialinkManager = new ManialinkManager($this);
$this->actionsMenu = new ActionsMenu($this);
$this->chat = new Chat($this);
$this->commandManager = new CommandManager($this);
$this->server = new Server($this);
$this->authenticationManager = new AuthenticationManager($this);
2014-05-02 17:40:47 +02:00
$this->playerManager = new PlayerManager($this);
$this->mapManager = new MapManager($this);
$this->configurator = new Configurator($this);
$this->pluginManager = new PluginManager($this);
$this->updateManager = new UpdateManager($this);
$this->errorHandler->init();
// Define Permission Levels
2014-01-31 16:55:01 +01:00
$this->authenticationManager->definePermissionLevel(self::SETTING_PERMISSION_SHUTDOWN, AuthenticationManager::AUTH_LEVEL_SUPERADMIN);
$this->authenticationManager->definePermissionLevel(self::SETTING_PERMISSION_RESTART, AuthenticationManager::AUTH_LEVEL_SUPERADMIN);
2014-05-02 17:40:47 +02:00
2013-12-17 17:38:44 +01:00
// Register for commands
$this->commandManager->registerCommandListener('version', $this, 'commandVersion', false, 'Shows ManiaControl version.');
$this->commandManager->registerCommandListener('restart', $this, 'commandRestart', true, 'Restarts ManiaControl.');
$this->commandManager->registerCommandListener('shutdown', $this, 'commandShutdown', true, 'Shuts ManiaControl down.');
2014-05-02 17:40:47 +02:00
// Check connection every 30 seconds
2014-04-20 15:13:31 +02:00
$this->timerManager->registerTimerListening($this, 'checkConnection', 1000 * 30);
}
2014-05-02 17:40:47 +02:00
/**
* Print a message to console and log
*
* @param string $message
* @param bool $stripCodes
*/
public function log($message, $stripCodes = false) {
if ($stripCodes) {
$message = Formatter::stripCodes($message);
}
logMessage($message);
}
/**
* Load the Config XML-File
*/
private function loadConfig() {
2014-05-02 17:40:47 +02:00
$configId = CommandLineHelper::getParameter('-config');
$configFileName = ($configId ? $configId : 'server.xml');
2014-05-02 17:40:47 +02:00
$this->config = FileUtil::loadConfig($configFileName);
if (!$this->config) {
2014-05-15 13:04:05 +02:00
$this->quit("Error loading Configuration XML-File! ('{$configFileName}')", true);
}
2014-05-27 08:57:26 +02:00
if ($this->config->count() < 3) {
$this->quit("Your Configuration File ('{$configFileName}') doesn't seem to be maintained properly. Please check it again!", true);
}
}
2014-05-15 13:04:05 +02:00
/**
* Quit ManiaControl and log the given message
*
* @param string $message
* @param bool $errorPrefix
*/
public function quit($message = null, $errorPrefix = false) {
if ($message) {
if ($errorPrefix) {
$message = '[ERROR] ' . $message;
}
$this->log($message);
}
2014-05-29 22:54:43 +02:00
$this->disconnect();
2014-05-15 13:04:05 +02:00
exit();
}
2014-05-29 22:54:43 +02:00
/**
* Close the Client Connection
*/
public function disconnect() {
Connection::delete($this->client);
$this->client = null;
}
/**
* Check Connection
*/
public function checkConnection() {
2014-04-19 22:59:11 +02:00
if ($this->client->getIdleTime() > 180) {
$this->client->getServerName();
}
2013-11-10 17:29:21 +01:00
}
2013-12-09 15:53:10 +01:00
/**
2014-05-02 17:40:47 +02:00
* Handle Version Command
*
* @param array $chatCallback
* @param Player $player
*/
public function commandVersion(array $chatCallback, Player $player) {
2014-05-02 17:40:47 +02:00
$message = 'This server is using ManiaControl v' . ManiaControl::VERSION . '!';
$this->chat->sendInformation($message, $player->login);
}
/**
* Handle Restart AdminCommand
*
* @param array $chatCallback
* @param Player $player
*/
public function commandRestart(array $chatCallback, Player $player) {
2014-05-02 17:40:47 +02:00
if (!$this->authenticationManager->checkPermission($player, self::SETTING_PERMISSION_RESTART)) {
$this->authenticationManager->sendNotAllowed($player);
return;
}
$this->restart("ManiaControl Restart requested by '{$player->login}'!");
}
/**
* Restart ManiaControl
*
2013-12-31 12:42:07 +01:00
* @param string $message
2013-12-09 15:53:10 +01:00
*/
2014-05-02 17:40:47 +02:00
public function restart($message = null) {
// Shutdown callback
$this->callbackManager->triggerCallback(Callbacks::ONSHUTDOWN);
2014-05-02 17:40:47 +02:00
// Announce restart
$this->chat->sendInformation('Restarting ManiaControl...');
if ($message) {
$this->log($message);
2014-01-05 14:13:18 +01:00
}
2014-05-02 17:40:47 +02:00
// Hide widgets
2014-06-13 16:48:14 +02:00
if ($this->client) {
$this->client->sendHideManialinkPage();
}
2014-05-02 17:40:47 +02:00
$this->log('Restarting ManiaControl!');
// Execute start script in background
// TODO: restart the .php script itself ($_SERVER['scriptname'] or something + $argv)
if ($this->getOS(self::OS_UNIX)) {
2014-05-03 21:37:28 +02:00
$command = 'sh ' . escapeshellarg(ManiaControlDir . 'ManiaControl.sh') . ' > /dev/null &';
2014-05-02 17:40:47 +02:00
exec($command);
} else {
2014-05-03 21:37:28 +02:00
$command = escapeshellarg(ManiaControlDir . "ManiaControl.bat");
2014-06-12 15:46:24 +02:00
system($command); // TODO: windows gets stuck here as long controller is running
2014-05-02 17:40:47 +02:00
}
// Quit the old instance
$this->quit('Quitting ManiaControl to restart.');
2013-12-09 15:53:10 +01:00
}
/**
* Get the Operating System on which ManiaControl is running
2014-05-02 17:40:47 +02:00
*
* @param string $compareOS
* @return string|bool
*/
public function getOS($compareOS = null) {
$windows = defined('PHP_WINDOWS_VERSION_MAJOR');
2014-01-27 20:28:37 +01:00
if ($compareOS) {
// Return bool whether OS equals $compareOS
2014-01-27 20:28:37 +01:00
if ($compareOS == self::OS_WIN) {
return $windows;
}
return !$windows;
}
// Return OS
2014-01-27 20:28:37 +01:00
if ($windows) {
return self::OS_WIN;
}
return self::OS_UNIX;
}
2014-01-01 19:25:51 +01:00
/**
* Handle Shutdown Command
2014-05-02 17:40:47 +02:00
*
* @param array $chat
2014-01-01 19:25:51 +01:00
* @param Player $player
*/
public function commandShutdown(array $chat, Player $player) {
2014-01-31 16:55:01 +01:00
if (!$this->authenticationManager->checkPermission($player, self::SETTING_PERMISSION_SHUTDOWN)) {
2014-01-01 19:25:51 +01:00
$this->authenticationManager->sendNotAllowed($player);
return;
}
$this->quit("ManiaControl Shutdown requested by '{$player->login}'!");
}
2013-11-10 17:29:21 +01:00
/**
* Run ManiaControl
*/
public function run() {
$this->log('Starting ManiaControl v' . self::VERSION . '!');
2014-05-02 17:40:47 +02:00
2014-02-15 21:48:23 +01:00
// Connect to server
$this->connect();
2014-05-02 17:40:47 +02:00
2014-04-14 12:18:44 +02:00
// Check if the version of the server is high enough
$version = $this->client->getVersion();
if ($version->build < self::MIN_DEDIVERSION) {
2014-05-29 22:54:43 +02:00
$this->quit("The Server has Version '{$version->build}', while at least '" . self::MIN_DEDIVERSION . "' is required!", true);
2014-04-30 00:19:18 +02:00
}
2014-05-02 17:40:47 +02:00
// OnInit callback
$this->callbackManager->triggerCallback(Callbacks::ONINIT);
2014-05-02 17:40:47 +02:00
2014-01-18 10:26:32 +01:00
// Load plugins
$this->pluginManager->loadPlugins();
$this->updateManager->pluginUpdateManager->checkPluginsUpdate();
2014-05-02 17:40:47 +02:00
// AfterInit callback
$this->callbackManager->triggerCallback(Callbacks::AFTERINIT);
2014-05-02 17:40:47 +02:00
// Loading finished
$this->log('Loading completed!');
2014-02-13 20:41:55 +01:00
$this->log('Link: maniaplanet://#join=' . $this->server->login . '@' . $this->server->titleId);
2014-05-29 22:54:43 +02:00
$this->chat->sendInformation('ManiaControl v' . self::VERSION . ' successfully started!');
2014-05-02 17:40:47 +02:00
2013-11-10 17:29:21 +01:00
// Main loop
while (!$this->shutdownRequested) {
2014-05-09 13:46:04 +02:00
$this->loop();
}
2014-05-02 17:40:47 +02:00
2014-05-09 13:46:04 +02:00
// Shutdown
$this->quit();
}
2014-05-02 17:40:47 +02:00
2013-11-10 17:29:21 +01:00
/**
* Connect to ManiaPlanet server
*/
private function connect() {
// Load remote client
$this->server->loadConfig();
2014-05-02 17:40:47 +02:00
2014-05-29 22:54:43 +02:00
$this->log("Connecting to Server at {$this->server->config->host}:{$this->server->config->port}...");
2014-05-02 17:40:47 +02:00
2014-01-17 16:32:53 +01:00
try {
$this->client = Connection::factory($this->server->config->host, $this->server->config->port, self::SCRIPT_TIMEOUT, $this->server->config->user, $this->server->config->pass, self::API_VERSION);
} catch (AuthenticationException $e) {
$message = "Couldn't authenticate on Server with User '{$this->server->config->user}' & Pass '{$this->server->config->pass}'! " . $e->getMessage();
2014-05-09 14:08:39 +02:00
$this->quit($message, true);
}
2014-05-02 17:40:47 +02:00
2013-11-10 17:29:21 +01:00
// Enable callback system
$this->client->enableCallbacks(true);
2014-05-02 17:40:47 +02:00
2013-11-10 17:29:21 +01:00
// Wait for server to be ready
2014-03-01 10:49:07 +01:00
try {
if (!$this->server->waitForStatus(4)) {
2014-05-06 10:34:57 +02:00
$this->quit("Server couldn't get ready!");
2014-03-01 10:49:07 +01:00
}
2014-05-02 17:40:47 +02:00
} catch (Exception $e) {
// TODO remove
$this->errorHandler->handleException($e, false);
2014-05-09 14:08:39 +02:00
$this->quit($e->getMessage(), true);
2013-11-10 17:29:21 +01:00
}
2014-05-02 17:40:47 +02:00
2013-11-10 17:29:21 +01:00
// Connect finished
2013-12-31 14:50:45 +01:00
$this->log("Server Connection successfully established!");
2014-05-02 17:40:47 +02:00
2013-12-31 14:50:45 +01:00
// Hide old widgets
2014-01-16 17:19:01 +01:00
$this->client->sendHideManialinkPage();
2014-05-02 17:40:47 +02:00
// Enable script callbacks
$this->server->scriptManager->enableScriptCallbacks();
2013-11-10 17:29:21 +01:00
}
2014-05-09 14:08:39 +02:00
/**
* Perform the Main Loop
*/
private function loop() {
$loopStart = microtime(true);
2014-05-09 16:33:33 +02:00
// Extend script timeout
2014-05-09 14:08:39 +02:00
set_time_limit(self::SCRIPT_TIMEOUT);
try {
$this->callbackManager->manageCallbacks();
} catch (TransportException $e) {
$this->log("Connection interrupted!");
// TODO remove
$this->errorHandler->handleException($e, false);
$this->quit($e->getMessage(), true);
}
// Manage FileReader
$this->fileReader->appendData();
// Yield for next tick
$loopEnd = microtime(true);
$loopDuration = $loopEnd - $loopStart;
$sleepTime = (int)(2500 - $loopDuration * 1000000);
if ($sleepTime > 0) {
usleep($sleepTime);
}
}
2013-11-10 17:29:21 +01:00
}