diff --git a/application/core/ManiaControl.php b/application/core/ManiaControl.php index 77fdcd71..a1c2c2e9 100644 --- a/application/core/ManiaControl.php +++ b/application/core/ManiaControl.php @@ -28,6 +28,7 @@ require_once __DIR__ . '/Configurators/Configurator.php'; require_once __DIR__ . '/Database.php'; require_once __DIR__ . '/FileUtil.php'; require_once __DIR__ . '/Formatter.php'; +require_once __DIR__ . '/GbxDataFetcher/gbxdatafetcher.inc.php'; require_once __DIR__ . '/ManiaExchange/mxinfofetcher.inc.php'; require_once __DIR__ . '/ManiaExchange/mxinfosearcher.inc.php'; require_once __DIR__ . '/Manialinks/ManialinkManager.php'; @@ -38,7 +39,7 @@ require_once __DIR__ . '/Players/PlayerManager.php'; require_once __DIR__ . '/Plugins/PluginManager.php'; require_once __DIR__ . '/Server/Server.php'; require_once __DIR__ . '/Settings/SettingManager.php'; -require_once __DIR__ . '/GbxDataFetcher/gbxdatafetcher.inc.php'; +require_once __DIR__ . '/UpdateManager.php'; list($endiantest) = array_values(unpack('L1L', pack('V', 1))); if ($endiantest == 1) { require_once __DIR__ . '/PhpRemote/GbxRemote.inc.php'; @@ -77,6 +78,7 @@ class ManiaControl implements CommandListener { public $pluginManager = null; public $server = null; public $settingManager = null; + public $updateManager = null; /** * Private properties @@ -89,6 +91,7 @@ class ManiaControl implements CommandListener { public function __construct() { logMessage('Loading ManiaControl v' . self::VERSION . '...'); + // Load ManiaControl Modules $this->database = new Database($this); $this->callbackManager = new CallbackManager($this); $this->settingManager = new SettingManager($this); @@ -102,7 +105,9 @@ class ManiaControl implements CommandListener { $this->mapManager = new MapManager($this); $this->configurator = new Configurator($this); $this->pluginManager = new PluginManager($this); + $this->updateManager = new UpdateManager($this); + // Register for commands $this->commandManager->registerCommandListener('version', $this, 'command_Version'); } diff --git a/application/core/UpdateManager.php b/application/core/UpdateManager.php new file mode 100644 index 00000000..4ff2fd19 --- /dev/null +++ b/application/core/UpdateManager.php @@ -0,0 +1,164 @@ +maniaControl = $maniaControl; + + // Init settings + $this->maniaControl->settingManager->initSetting($this, self::SETTING_ENABLEUPDATECHECK, true); + $this->maniaControl->settingManager->initSetting($this, self::SETTING_UPDATECHECK_INTERVAL, 24.); + $this->maniaControl->settingManager->initSetting($this, self::SETTING_UPDATECHECK_CHANNEL, 'alpha'); + + // Register for callbacks + $this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MC_1_MINUTE, $this, 'handle1Minute'); + $this->maniaControl->callbackManager->registerCallbackListener(PlayerManager::CB_PLAYERJOINED, $this, 'handlePlayerJoined'); + + // Register for chat commands + $this->maniaControl->commandManager->registerCommandListener('checkupdate', $this, 'handle_CheckUpdate', true); + } + + /** + * Handle ManiaControl 1Minute callback + * + * @param array $callback + */ + public function handle1Minute(array $callback) { + $updateCheckEnabled = $this->maniaControl->settingManager->getSetting($this, self::SETTING_ENABLEUPDATECHECK); + if (!$updateCheckEnabled) { + if ($this->coreUpdateData) { + $this->coreUpdateData = null; + } + return; + } + $updateInterval = $this->maniaControl->settingManager->getSetting($this, self::SETTING_UPDATECHECK_INTERVAL); + if ($this->lastUpdateCheck > time() - $updateInterval * 3600.) { + return; + } + $this->lastUpdateCheck = time(); + $updateData = $this->checkCoreUpdate(); + if (!$updateData) { + return; + } + $this->maniaControl->log('New ManiaControl Version ' . $updateData->version . ' available!'); + $this->coreUpdateData = $updateData; + } + + /** + * Handle ManiaControl PlayerJoined callback + * + * @param array $callback + */ + public function handlePlayerJoined(array $callback) { + if (!$this->coreUpdateData) { + return; + } + $player = $callback[1]; + if (!AuthenticationManager::checkRight($player, AuthenticationManager::AUTH_LEVEL_SUPERADMIN)) { + return; + } + $this->maniaControl->chat->sendInformation('New ManiaControl Version ' . $this->coreUpdateData->version . ' available!', + $player->login); + } + + /** + * Handle //checkupdate command + * + * @param array $chatCallback + * @param Player $player + */ + public function handle_CheckUpdate(array $chatCallback, Player $player) { + if (!AuthenticationManager::checkRight($player, AuthenticationManager::AUTH_LEVEL_SUPERADMIN)) { + $this->maniaControl->authenticationManager->sendNotAllowed($player); + return; + } + $updateData = $this->checkCoreUpdate(); + if (!$updateData) { + $this->maniaControl->chat->sendInformation('No Update available!', $player->login); + return; + } + $this->maniaControl->chat->sendSuccess('Update for Version ' . $updateData->version . ' available!', $player->login); + } + + /** + * Check given Plugin Class for Update + * + * @param string $pluginClass + * @return mixed + */ + public function checkPluginUpdate($pluginClass) { + if (is_object($pluginClass)) { + $pluginClass = get_class($pluginClass); + } + $pluginId = $pluginClass::getId(); + $url = self::URL_WEBSERVICE . 'plugins?id=' . $pluginId; + $dataJson = file_get_contents($url); + $pluginVersions = json_decode($dataJson); + if (!$pluginVersions || !isset($pluginVersions[0])) { + return false; + } + $pluginData = $pluginVersions[0]; + $pluginVersion = $pluginClass::getVersion(); + if ($pluginData->version <= $pluginVersion) { + return false; + } + return $pluginData; + } + + /** + * Check for Update of ManiaControl + * + * @return mixed + */ + private function checkCoreUpdate() { + $updateChannel = $this->maniaControl->settingManager->getSetting($this, self::SETTING_UPDATECHECK_CHANNEL); + $updateChannel = strtolower($updateChannel); + if (!in_array($updateChannel, array('release', 'beta', 'alpha'))) { + $updateChannel = 'release'; + } + $url = self::URL_WEBSERVICE . 'versions?current=1&channel=' . $updateChannel; + $dataJson = file_get_contents($url); + $versions = json_decode($dataJson); + if (!$versions || !isset($versions[0])) { + return false; + } + $updateData = $versions[0]; + if ($updateData->version <= ManiaControl::VERSION) { + return false; + } + return $updateData; + } +}