removed 'application' folder to have everything in the root directory
This commit is contained in:
55
core/Update/PluginUpdateData.php
Normal file
55
core/Update/PluginUpdateData.php
Normal file
@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
namespace ManiaControl\Update;
|
||||
|
||||
/**
|
||||
* Plugin Update Data Model Class
|
||||
*
|
||||
* @author ManiaControl Team <mail@maniacontrol.com>
|
||||
* @copyright 2014 ManiaControl Team
|
||||
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
|
||||
*/
|
||||
class PluginUpdateData {
|
||||
/*
|
||||
* Public properties
|
||||
*/
|
||||
public $pluginId = null;
|
||||
public $pluginName = null;
|
||||
public $pluginAuthor = null;
|
||||
public $pluginDescription = null;
|
||||
public $id = null;
|
||||
public $version = null;
|
||||
public $zipfile = null;
|
||||
public $url = null;
|
||||
|
||||
/**
|
||||
* Construct new plugin update data instance
|
||||
*
|
||||
* @param object $updateData
|
||||
*/
|
||||
public function __construct($updateData) {
|
||||
$this->pluginId = $updateData->id;
|
||||
$this->pluginName = $updateData->name;
|
||||
$this->pluginAuthor = $updateData->author;
|
||||
$this->pluginDescription = $updateData->description;
|
||||
if ($updateData->currentVersion) {
|
||||
$this->id = $updateData->currentVersion->id;
|
||||
$this->version = $updateData->currentVersion->version;
|
||||
$this->zipfile = $updateData->currentVersion->zipfile;
|
||||
$this->url = $updateData->currentVersion->url;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the plugin update data is newer than the given plugin version
|
||||
*
|
||||
* @param float $version
|
||||
* @return bool
|
||||
*/
|
||||
public function isNewerThan($version) {
|
||||
if (!$version) {
|
||||
return true;
|
||||
}
|
||||
return ($this->version > $version);
|
||||
}
|
||||
}
|
402
core/Update/PluginUpdateManager.php
Normal file
402
core/Update/PluginUpdateManager.php
Normal file
@ -0,0 +1,402 @@
|
||||
<?php
|
||||
|
||||
namespace ManiaControl\Update;
|
||||
|
||||
use ManiaControl\Callbacks\CallbackListener;
|
||||
use ManiaControl\Callbacks\CallbackManager;
|
||||
use ManiaControl\Callbacks\TimerListener;
|
||||
use ManiaControl\Commands\CommandListener;
|
||||
use ManiaControl\Files\BackupUtil;
|
||||
use ManiaControl\Files\FileUtil;
|
||||
use ManiaControl\Logger;
|
||||
use ManiaControl\ManiaControl;
|
||||
use ManiaControl\Players\Player;
|
||||
use ManiaControl\Plugins\InstallMenu;
|
||||
use ManiaControl\Plugins\Plugin;
|
||||
use ManiaControl\Plugins\PluginManager;
|
||||
use ManiaControl\Plugins\PluginMenu;
|
||||
use ManiaControl\Utils\WebReader;
|
||||
|
||||
/**
|
||||
* Manager checking for ManiaControl Plugin Updates
|
||||
*
|
||||
* @author ManiaControl Team <mail@maniacontrol.com>
|
||||
* @copyright 2014 ManiaControl Team
|
||||
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
|
||||
*/
|
||||
class PluginUpdateManager implements CallbackListener, CommandListener, TimerListener {
|
||||
/*
|
||||
* Private properties
|
||||
*/
|
||||
/** @var ManiaControl $maniaControl */
|
||||
private $maniaControl = null;
|
||||
|
||||
/**
|
||||
* Create a new plugin update manager instance
|
||||
*
|
||||
* @param ManiaControl $maniaControl
|
||||
*/
|
||||
public function __construct(ManiaControl $maniaControl) {
|
||||
$this->maniaControl = $maniaControl;
|
||||
|
||||
// Callbacks
|
||||
$this->maniaControl->getCallbackManager()->registerCallbackListener(CallbackManager::CB_MP_PLAYERMANIALINKPAGEANSWER, $this, 'handleManialinkPageAnswer');
|
||||
|
||||
// Chat commands
|
||||
$this->maniaControl->getCommandManager()->registerCommandListener('checkpluginsupdate', $this, 'handle_CheckPluginsUpdate', true, 'Check for Plugin Updates.');
|
||||
$this->maniaControl->getCommandManager()->registerCommandListener('pluginsupdate', $this, 'handle_PluginsUpdate', true, 'Perform the Plugin Updates.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle //checkpluginsupdate command
|
||||
*
|
||||
* @param array $chatCallback
|
||||
* @param Player $player
|
||||
*/
|
||||
public function handle_CheckPluginsUpdate(array $chatCallback, Player $player) {
|
||||
if (!$this->maniaControl->getAuthenticationManager()->checkPermission($player, UpdateManager::SETTING_PERMISSION_UPDATECHECK)
|
||||
) {
|
||||
$this->maniaControl->getAuthenticationManager()->sendNotAllowed($player);
|
||||
return;
|
||||
}
|
||||
|
||||
$this->checkPluginsUpdate($player);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if there are Outdated Plugins installed
|
||||
*
|
||||
* @param Player $player
|
||||
*/
|
||||
public function checkPluginsUpdate(Player $player = null) {
|
||||
$message = 'Checking Plugins for newer Versions...';
|
||||
if ($player) {
|
||||
$this->maniaControl->getChat()->sendInformation($message, $player);
|
||||
}
|
||||
Logger::log($message);
|
||||
|
||||
$this->maniaControl->getPluginManager()->fetchPluginList(function ($data, $error) use (&$player) {
|
||||
if (!$data || $error) {
|
||||
$message = 'Error while checking Plugins for newer Versions!';
|
||||
if ($player) {
|
||||
$this->maniaControl->getChat()->sendError($message, $player);
|
||||
}
|
||||
Logger::logError($message);
|
||||
return;
|
||||
}
|
||||
|
||||
$pluginsData = $this->parsePluginsData($data);
|
||||
$pluginClasses = $this->maniaControl->getPluginManager()->getPluginClasses();
|
||||
$pluginUpdates = array();
|
||||
|
||||
foreach ($pluginClasses as $pluginClass) {
|
||||
/** @var Plugin $pluginClass */
|
||||
$pluginId = $pluginClass::getId();
|
||||
if (!isset($pluginsData[$pluginId])) {
|
||||
continue;
|
||||
}
|
||||
/** @var PluginUpdateData $pluginData */
|
||||
$pluginData = $pluginsData[$pluginId];
|
||||
$pluginVersion = $pluginClass::getVersion();
|
||||
if ($pluginData->isNewerThan($pluginVersion)) {
|
||||
$pluginUpdates[$pluginId] = $pluginData;
|
||||
$message = "There is an Update of '{$pluginData->pluginName}' available! ('{$pluginClass}' - Version {$pluginData->version})";
|
||||
if ($player) {
|
||||
$this->maniaControl->getChat()->sendSuccess($message, $player);
|
||||
}
|
||||
Logger::log($message);
|
||||
}
|
||||
}
|
||||
|
||||
if (empty($pluginUpdates)) {
|
||||
$message = 'Plugins Update Check completed: All Plugins are up-to-date!';
|
||||
if ($player) {
|
||||
$this->maniaControl->getChat()->sendSuccess($message, $player);
|
||||
}
|
||||
Logger::log($message);
|
||||
} else {
|
||||
$updatesCount = count($pluginUpdates);
|
||||
$message = "Plugins Update Check completed: There are {$updatesCount} Updates available!";
|
||||
if ($player) {
|
||||
$this->maniaControl->getChat()->sendSuccess($message, $player);
|
||||
}
|
||||
Logger::log($message);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an Array of Plugin Update Data from the given Web Service Result
|
||||
*
|
||||
* @param mixed $webServiceResult
|
||||
* @return mixed
|
||||
*/
|
||||
public function parsePluginsData($webServiceResult) {
|
||||
if (!$webServiceResult || !is_array($webServiceResult)) {
|
||||
return false;
|
||||
}
|
||||
$pluginsData = array();
|
||||
foreach ($webServiceResult as $pluginResult) {
|
||||
$pluginData = new PluginUpdateData($pluginResult);
|
||||
$pluginsData[$pluginData->pluginId] = $pluginData;
|
||||
}
|
||||
return $pluginsData;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle //pluginsupdate command
|
||||
*
|
||||
* @param array $chatCallback
|
||||
* @param Player $player
|
||||
*/
|
||||
public function handle_PluginsUpdate(array $chatCallback, Player $player) {
|
||||
if (!$this->maniaControl->getAuthenticationManager()->checkPermission($player, UpdateManager::SETTING_PERMISSION_UPDATE)
|
||||
) {
|
||||
$this->maniaControl->getAuthenticationManager()->sendNotAllowed($player);
|
||||
return;
|
||||
}
|
||||
|
||||
$this->performPluginsUpdate($player);
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform an Update of all outdated Plugins
|
||||
*
|
||||
* @param Player $player
|
||||
*/
|
||||
public function performPluginsUpdate(Player $player = null) {
|
||||
$pluginsUpdates = $this->getPluginsUpdates();
|
||||
if (empty($pluginsUpdates)) {
|
||||
$message = 'There are no Plugin Updates available!';
|
||||
if ($player) {
|
||||
$this->maniaControl->getChat()->sendInformation($message, $player);
|
||||
}
|
||||
Logger::log($message);
|
||||
return;
|
||||
}
|
||||
|
||||
$message = "Starting Plugins Updating...";
|
||||
if ($player) {
|
||||
$this->maniaControl->getChat()->sendInformation($message, $player);
|
||||
}
|
||||
Logger::log($message);
|
||||
|
||||
$performBackup = $this->maniaControl->getSettingManager()->getSettingValue($this->maniaControl->getUpdateManager(), UpdateManager::SETTING_PERFORM_BACKUPS);
|
||||
if ($performBackup && !BackupUtil::performPluginsBackup()) {
|
||||
$message = 'Creating Backup before Plugins Update failed!';
|
||||
if ($player) {
|
||||
$this->maniaControl->getChat()->sendError($message, $player);
|
||||
}
|
||||
Logger::logError($message);
|
||||
}
|
||||
|
||||
foreach ($pluginsUpdates as $pluginUpdateData) {
|
||||
$this->installPlugin($pluginUpdateData, $player, true);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check for Plugin Updates
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getPluginsUpdates() {
|
||||
$url = ManiaControl::URL_WEBSERVICE . 'plugins';
|
||||
$response = WebReader::getUrl($url);
|
||||
$dataJson = $response->getContent();
|
||||
$pluginData = json_decode($dataJson);
|
||||
if (!$pluginData || empty($pluginData)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$pluginsUpdates = $this->parsePluginsData($pluginData);
|
||||
|
||||
$updates = array();
|
||||
$pluginClasses = $this->maniaControl->getPluginManager()->getPluginClasses();
|
||||
foreach ($pluginClasses as $pluginClass) {
|
||||
/** @var Plugin $pluginClass */
|
||||
$pluginId = $pluginClass::getId();
|
||||
if (isset($pluginsUpdates[$pluginId])) {
|
||||
/** @var PluginUpdateData $pluginUpdateData */
|
||||
$pluginUpdateData = $pluginsUpdates[$pluginId];
|
||||
$pluginVersion = $pluginClass::getVersion();
|
||||
if ($pluginUpdateData->isNewerThan($pluginVersion)) {
|
||||
$updates[$pluginId] = $pluginUpdateData;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (empty($updates)) {
|
||||
return false;
|
||||
}
|
||||
return $updates;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the given Plugin Update Data
|
||||
*
|
||||
* @param PluginUpdateData $pluginUpdateData
|
||||
* @param Player $player
|
||||
* @param bool $update
|
||||
*/
|
||||
private function installPlugin(PluginUpdateData $pluginUpdateData, Player $player = null, $update = false) {
|
||||
$this->maniaControl->getFileReader()->loadFile($pluginUpdateData->url, function ($updateFileContent, $error) use (
|
||||
&$pluginUpdateData, &$player, &$update
|
||||
) {
|
||||
if (!$updateFileContent || $error) {
|
||||
$message = "Error loading Update Data for '{$pluginUpdateData->pluginName}': {$error}!";
|
||||
if ($player) {
|
||||
$this->maniaControl->getChat()->sendInformation($message, $player);
|
||||
}
|
||||
Logger::logError($message);
|
||||
return;
|
||||
}
|
||||
|
||||
$actionNoun = ($update ? 'Update' : 'Install');
|
||||
$actionVerb = ($update ? 'Updating' : 'Installing');
|
||||
$actionVerbDone = ($update ? 'updated' : 'installed');
|
||||
|
||||
$message = "Now {$actionVerb} '{$pluginUpdateData->pluginName}'...";
|
||||
if ($player) {
|
||||
$this->maniaControl->getChat()->sendInformation($message, $player);
|
||||
}
|
||||
Logger::log($message);
|
||||
|
||||
$tempDir = FileUtil::getTempFolder();
|
||||
$updateFileName = $tempDir . $pluginUpdateData->zipfile;
|
||||
|
||||
$bytes = file_put_contents($updateFileName, $updateFileContent);
|
||||
if (!$bytes || $bytes <= 0) {
|
||||
$message = "Plugin {$actionNoun} failed: Couldn't save {$actionNoun} Zip!";
|
||||
if ($player) {
|
||||
$this->maniaControl->getChat()->sendError($message, $player);
|
||||
}
|
||||
Logger::logError($message);
|
||||
return;
|
||||
}
|
||||
|
||||
$zip = new \ZipArchive();
|
||||
$result = $zip->open($updateFileName);
|
||||
if ($result !== true) {
|
||||
$message = "Plugin {$actionNoun} failed: Couldn't open {$actionNoun} Zip! ({$result})";
|
||||
if ($player) {
|
||||
$this->maniaControl->getChat()->sendError($message, $player);
|
||||
}
|
||||
Logger::logError($message);
|
||||
return;
|
||||
}
|
||||
|
||||
$zip->extractTo(MANIACONTROL_PATH . 'plugins' . DIRECTORY_SEPARATOR);
|
||||
$zip->close();
|
||||
unlink($updateFileName);
|
||||
FileUtil::deleteTempFolder();
|
||||
|
||||
$messageExtra = '';
|
||||
if ($update) {
|
||||
$messageExtra = ' (Restart ManiaControl to load the new Version!)';
|
||||
}
|
||||
$message = "Successfully {$actionVerbDone} '{$pluginUpdateData->pluginName}'!{$messageExtra}";
|
||||
if ($player) {
|
||||
$this->maniaControl->getChat()->sendSuccess($message, $player);
|
||||
}
|
||||
Logger::log($message);
|
||||
|
||||
if (!$update) {
|
||||
$newPluginClasses = $this->maniaControl->getPluginManager()->loadPlugins();
|
||||
if (empty($newPluginClasses)) {
|
||||
$message = "Loading fresh installed Plugin '{$pluginUpdateData->pluginName}' failed!";
|
||||
if ($player) {
|
||||
$this->maniaControl->getChat()->sendError($message, $player);
|
||||
}
|
||||
Logger::log($message);
|
||||
} else {
|
||||
$message = "Successfully loaded fresh installed Plugin '{$pluginUpdateData->pluginName}'!";
|
||||
if ($player) {
|
||||
$this->maniaControl->getChat()->sendSuccess($message, $player);
|
||||
}
|
||||
Logger::log($message);
|
||||
|
||||
$this->maniaControl->getConfigurator()->showMenu($player, InstallMenu::getTitle());
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle PlayerManialinkPageAnswer callback
|
||||
*
|
||||
* @param array $callback
|
||||
*/
|
||||
public function handleManialinkPageAnswer(array $callback) {
|
||||
$actionId = $callback[1][2];
|
||||
$update = (strpos($actionId, PluginMenu::ACTION_PREFIX_UPDATEPLUGIN) === 0);
|
||||
$install = (strpos($actionId, InstallMenu::ACTION_PREFIX_INSTALL_PLUGIN) === 0);
|
||||
if (!$update && !$install) {
|
||||
return;
|
||||
}
|
||||
|
||||
$login = $callback[1][1];
|
||||
$player = $this->maniaControl->getPlayerManager()->getPlayer($login);
|
||||
|
||||
if ($update) {
|
||||
$pluginClass = substr($actionId, strlen(PluginMenu::ACTION_PREFIX_UPDATEPLUGIN));
|
||||
if ($pluginClass === 'All') {
|
||||
$this->performPluginsUpdate($player);
|
||||
} else {
|
||||
$pluginUpdateData = $this->getPluginUpdate($pluginClass);
|
||||
if ($pluginUpdateData) {
|
||||
$this->installPlugin($pluginUpdateData, $player, true);
|
||||
} else {
|
||||
$message = 'Error loading Plugin Update Data!';
|
||||
$this->maniaControl->getChat()->sendError($message, $player);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$pluginId = substr($actionId, strlen(InstallMenu::ACTION_PREFIX_INSTALL_PLUGIN));
|
||||
|
||||
$url = ManiaControl::URL_WEBSERVICE . 'plugins/' . $pluginId;
|
||||
$this->maniaControl->getFileReader()->loadFile($url, function ($data, $error) use (&$player) {
|
||||
if ($error || !$data) {
|
||||
$message = "Error loading Plugin Install Data! {$error}";
|
||||
$this->maniaControl->getChat()->sendError($message, $player);
|
||||
return;
|
||||
}
|
||||
|
||||
$data = json_decode($data);
|
||||
if (!$data) {
|
||||
$message = "Error loading Plugin Install Data! {$error}";
|
||||
$this->maniaControl->getChat()->sendError($message, $player);
|
||||
return;
|
||||
}
|
||||
|
||||
$pluginUpdateData = new PluginUpdateData($data);
|
||||
$this->installPlugin($pluginUpdateData, $player);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check given Plugin Class for Update
|
||||
*
|
||||
* @param string $pluginClass
|
||||
* @return mixed
|
||||
*/
|
||||
public function getPluginUpdate($pluginClass) {
|
||||
$pluginClass = PluginManager::getPluginClass($pluginClass);
|
||||
/** @var Plugin $pluginClass */
|
||||
$pluginId = $pluginClass::getId();
|
||||
$url = ManiaControl::URL_WEBSERVICE . 'plugins/' . $pluginId;
|
||||
$response = WebReader::getUrl($url);
|
||||
$dataJson = $response->getContent();
|
||||
$pluginVersion = json_decode($dataJson);
|
||||
if (!$pluginVersion) {
|
||||
return false;
|
||||
}
|
||||
$pluginUpdateData = new PluginUpdateData($pluginVersion);
|
||||
$version = $pluginClass::getVersion();
|
||||
if ($pluginUpdateData->isNewerThan($version)) {
|
||||
return $pluginUpdateData;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
49
core/Update/UpdateData.php
Normal file
49
core/Update/UpdateData.php
Normal file
@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace ManiaControl\Update;
|
||||
|
||||
/**
|
||||
* ManiaControl Update Data Model Class
|
||||
*
|
||||
* @author ManiaControl Team <mail@maniacontrol.com>
|
||||
* @copyright 2014 ManiaControl Team
|
||||
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
|
||||
*/
|
||||
class UpdateData {
|
||||
/*
|
||||
* Public properties
|
||||
*/
|
||||
public $version = null;
|
||||
public $channel = null;
|
||||
public $url = null;
|
||||
public $releaseDate = null;
|
||||
public $minDedicatedBuild = null;
|
||||
|
||||
/**
|
||||
* Construct new update data instance
|
||||
*
|
||||
* @param object $updateData
|
||||
*/
|
||||
public function __construct($updateData) {
|
||||
$this->version = $updateData->version;
|
||||
$this->channel = $updateData->channel;
|
||||
$this->url = $updateData->url;
|
||||
$this->releaseDate = $updateData->release_date;
|
||||
$this->minDedicatedBuild = $updateData->min_dedicated_build;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the update data is newer than the given date
|
||||
*
|
||||
* @param string $compareDate
|
||||
* @return bool
|
||||
*/
|
||||
public function isNewerThan($compareDate) {
|
||||
if (!$compareDate) {
|
||||
return true;
|
||||
}
|
||||
$compareTime = strtotime($compareDate);
|
||||
$releaseTime = strtotime($this->releaseDate);
|
||||
return ($releaseTime > $compareTime);
|
||||
}
|
||||
}
|
515
core/Update/UpdateManager.php
Normal file
515
core/Update/UpdateManager.php
Normal file
@ -0,0 +1,515 @@
|
||||
<?php
|
||||
|
||||
namespace ManiaControl\Update;
|
||||
|
||||
use ManiaControl\Admin\AuthenticationManager;
|
||||
use ManiaControl\Callbacks\CallbackListener;
|
||||
use ManiaControl\Callbacks\TimerListener;
|
||||
use ManiaControl\Commands\CommandListener;
|
||||
use ManiaControl\Files\BackupUtil;
|
||||
use ManiaControl\Files\FileUtil;
|
||||
use ManiaControl\Logger;
|
||||
use ManiaControl\ManiaControl;
|
||||
use ManiaControl\Players\Player;
|
||||
use ManiaControl\Players\PlayerManager;
|
||||
|
||||
/**
|
||||
* Manager checking for ManiaControl Core Updates
|
||||
*
|
||||
* @author ManiaControl Team <mail@maniacontrol.com>
|
||||
* @copyright 2014 ManiaControl Team
|
||||
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
|
||||
*/
|
||||
class UpdateManager implements CallbackListener, CommandListener, TimerListener {
|
||||
/*
|
||||
* Constants
|
||||
*/
|
||||
const CHANNEL_RELEASE = 'release';
|
||||
const CHANNEL_BETA = 'beta';
|
||||
const CHANNEL_NIGHTLY = 'nightly';
|
||||
const SETTING_ENABLE_UPDATECHECK = 'Enable Automatic Core Update Check';
|
||||
const SETTING_UPDATECHECK_INTERVAL = 'Core Update Check Interval (Hours)';
|
||||
const SETTING_UPDATECHECK_CHANNEL = 'Core Update Channel (release, beta, nightly)';
|
||||
const SETTING_PERFORM_BACKUPS = 'Perform Backup before Updating';
|
||||
const SETTING_AUTO_UPDATE = 'Perform update automatically';
|
||||
const SETTING_PERMISSION_UPDATE = 'Update Core';
|
||||
const SETTING_PERMISSION_UPDATECHECK = 'Check Core Update';
|
||||
const BUILD_DATE_FILE_NAME = 'build_date.txt';
|
||||
|
||||
/*
|
||||
* Public properties
|
||||
*/
|
||||
/** @var PluginUpdateManager $pluginUpdateManager
|
||||
* @deprecated see getPluginUpdateManager()
|
||||
*/
|
||||
public $pluginUpdateManager = null;
|
||||
|
||||
/*
|
||||
* Private properties
|
||||
*/
|
||||
/** @var ManiaControl $maniaControl */
|
||||
private $maniaControl = null;
|
||||
private $currentBuildDate = null;
|
||||
/** @var UpdateData $coreUpdateData */
|
||||
private $coreUpdateData = null;
|
||||
|
||||
/**
|
||||
* Construct a new update manager instance
|
||||
*
|
||||
* @param ManiaControl $maniaControl
|
||||
*/
|
||||
public function __construct(ManiaControl $maniaControl) {
|
||||
$this->maniaControl = $maniaControl;
|
||||
|
||||
// Settings
|
||||
$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_ENABLE_UPDATECHECK, true);
|
||||
$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_AUTO_UPDATE, true);
|
||||
$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_UPDATECHECK_INTERVAL, 1);
|
||||
$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_UPDATECHECK_CHANNEL, $this->getUpdateChannels());
|
||||
$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_PERFORM_BACKUPS, true);
|
||||
|
||||
// Callbacks
|
||||
$updateInterval = $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_UPDATECHECK_INTERVAL);
|
||||
$this->maniaControl->getTimerManager()->registerTimerListening($this, 'hourlyUpdateCheck', 1000 * 60 * 60 * $updateInterval);
|
||||
$this->maniaControl->getCallbackManager()->registerCallbackListener(PlayerManager::CB_PLAYERCONNECT, $this, 'handlePlayerJoined');
|
||||
$this->maniaControl->getCallbackManager()->registerCallbackListener(PlayerManager::CB_PLAYERDISCONNECT, $this, 'handlePlayerDisconnect');
|
||||
|
||||
// Permissions
|
||||
$this->maniaControl->getAuthenticationManager()->definePermissionLevel(self::SETTING_PERMISSION_UPDATE, AuthenticationManager::AUTH_LEVEL_ADMIN);
|
||||
$this->maniaControl->getAuthenticationManager()->definePermissionLevel(self::SETTING_PERMISSION_UPDATECHECK, AuthenticationManager::AUTH_LEVEL_MODERATOR);
|
||||
|
||||
// Chat commands
|
||||
$this->maniaControl->getCommandManager()->registerCommandListener('checkupdate', $this, 'handle_CheckUpdate', true, 'Checks if there is a core update.');
|
||||
$this->maniaControl->getCommandManager()->registerCommandListener('coreupdate', $this, 'handle_CoreUpdate', true, 'Performs the core update.');
|
||||
|
||||
// Children
|
||||
$this->pluginUpdateManager = new PluginUpdateManager($maniaControl);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the possible update channels
|
||||
*
|
||||
* @return string[]
|
||||
*/
|
||||
public function getUpdateChannels() {
|
||||
// TODO: change default channel on release
|
||||
return array(self::CHANNEL_BETA, self::CHANNEL_RELEASE, self::CHANNEL_NIGHTLY);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the plugin update manager
|
||||
*
|
||||
* @return PluginUpdateManager
|
||||
*/
|
||||
public function getPluginUpdateManager() {
|
||||
return $this->pluginUpdateManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform Hourly Update Check
|
||||
*/
|
||||
public function hourlyUpdateCheck() {
|
||||
$updateCheckEnabled = $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_ENABLE_UPDATECHECK);
|
||||
if (!$updateCheckEnabled) {
|
||||
$this->setCoreUpdateData();
|
||||
} else {
|
||||
$this->checkUpdate();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set Core Update Data
|
||||
*
|
||||
* @param UpdateData $coreUpdateData
|
||||
*/
|
||||
public function setCoreUpdateData(UpdateData $coreUpdateData = null) {
|
||||
$this->coreUpdateData = $coreUpdateData;
|
||||
}
|
||||
|
||||
/**
|
||||
* Start an Update Check
|
||||
*/
|
||||
public function checkUpdate() {
|
||||
$this->checkCoreUpdateAsync(array($this, 'handleUpdateCheck'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks a Core Update asynchronously
|
||||
*
|
||||
* @param callable $function
|
||||
*/
|
||||
public function checkCoreUpdateAsync($function) {
|
||||
$updateChannel = $this->getCurrentUpdateChannelSetting();
|
||||
$url = ManiaControl::URL_WEBSERVICE . 'versions?current=1&channel=' . $updateChannel;
|
||||
|
||||
$this->maniaControl->getFileReader()->loadFile($url, function ($dataJson, $error) use (&$function) {
|
||||
if ($error) {
|
||||
Logger::logError('Error on UpdateCheck: ' . $error);
|
||||
return;
|
||||
}
|
||||
$versions = json_decode($dataJson);
|
||||
if (!$versions || !isset($versions[0])) {
|
||||
call_user_func($function);
|
||||
} else {
|
||||
$updateData = new UpdateData($versions[0]);
|
||||
call_user_func($function, $updateData);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the Update Channel Setting
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getCurrentUpdateChannelSetting() {
|
||||
$updateChannel = $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_UPDATECHECK_CHANNEL);
|
||||
$updateChannel = strtolower($updateChannel);
|
||||
if (!in_array($updateChannel, $this->getUpdateChannels())) {
|
||||
$updateChannel = self::CHANNEL_RELEASE;
|
||||
}
|
||||
return $updateChannel;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the fetched Update Data of the hourly Check
|
||||
*
|
||||
* @param UpdateData $updateData
|
||||
*/
|
||||
public function handleUpdateCheck(UpdateData $updateData = null) {
|
||||
if (!$this->checkUpdateData($updateData)) {
|
||||
// No new update available
|
||||
return;
|
||||
}
|
||||
if (!$this->checkUpdateDataBuildVersion($updateData)) {
|
||||
// Server incompatible
|
||||
Logger::logError("Please update Your Server to '{$updateData->minDedicatedBuild}' in order to receive further Updates!");
|
||||
return;
|
||||
}
|
||||
|
||||
if ($this->coreUpdateData != $updateData) {
|
||||
if ($this->isNightlyUpdateChannel()) {
|
||||
Logger::log("New Nightly Build ({$updateData->releaseDate}) available!");
|
||||
} else {
|
||||
Logger::log("New ManiaControl Version {$updateData->version} available!");
|
||||
}
|
||||
$this->setCoreUpdateData($updateData);
|
||||
}
|
||||
|
||||
$this->checkAutoUpdate();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the given Update Data has a new Version and fits for the Server
|
||||
*
|
||||
* @param UpdateData $updateData
|
||||
* @return bool
|
||||
*/
|
||||
public function checkUpdateData(UpdateData $updateData = null) {
|
||||
if (!$updateData || !$updateData->url) {
|
||||
// Data corrupted
|
||||
return false;
|
||||
}
|
||||
|
||||
$isNightly = $this->isNightlyUpdateChannel();
|
||||
$buildDate = $this->getBuildDate();
|
||||
|
||||
if ($isNightly || $buildDate) {
|
||||
return $updateData->isNewerThan($buildDate);
|
||||
}
|
||||
|
||||
return ($updateData->version > ManiaControl::VERSION);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if ManiaControl is running the Nightly Update Channel
|
||||
*
|
||||
* @param string $updateChannel
|
||||
* @return bool
|
||||
*/
|
||||
public function isNightlyUpdateChannel($updateChannel = null) {
|
||||
if (!$updateChannel) {
|
||||
$updateChannel = $this->getCurrentUpdateChannelSetting();
|
||||
}
|
||||
return ($updateChannel === self::CHANNEL_NIGHTLY);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the build date of the local version
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getBuildDate() {
|
||||
if (!$this->currentBuildDate) {
|
||||
$nightlyBuildDateFile = MANIACONTROL_PATH . 'core' . DIRECTORY_SEPARATOR . self::BUILD_DATE_FILE_NAME;
|
||||
if (file_exists($nightlyBuildDateFile)) {
|
||||
$this->currentBuildDate = file_get_contents($nightlyBuildDateFile);
|
||||
}
|
||||
}
|
||||
return $this->currentBuildDate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the Update Data is compatible with the Server
|
||||
*
|
||||
* @param UpdateData $updateData
|
||||
* @return bool
|
||||
*/
|
||||
public function checkUpdateDataBuildVersion(UpdateData $updateData = null) {
|
||||
if (!$updateData) {
|
||||
// Data corrupted
|
||||
return false;
|
||||
}
|
||||
|
||||
$version = $this->maniaControl->getClient()->getVersion();
|
||||
if ($updateData->minDedicatedBuild > $version->build) {
|
||||
// Server not compatible
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if an automatic Update should be performed
|
||||
*/
|
||||
public function checkAutoUpdate() {
|
||||
$autoUpdate = $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_AUTO_UPDATE);
|
||||
if (!$autoUpdate) {
|
||||
// Auto update turned off
|
||||
return;
|
||||
}
|
||||
if (!$this->coreUpdateData) {
|
||||
// No update available
|
||||
return;
|
||||
}
|
||||
if ($this->maniaControl->getPlayerManager()->getPlayerCount(false) > 0
|
||||
) {
|
||||
// Server not empty
|
||||
return;
|
||||
}
|
||||
|
||||
$this->performCoreUpdate();
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform a Core Update
|
||||
*
|
||||
* @param Player $player
|
||||
* @return bool
|
||||
*/
|
||||
public function performCoreUpdate(Player $player = null) {
|
||||
if (!$this->coreUpdateData) {
|
||||
$message = 'Update failed: No update Data available!';
|
||||
if ($player) {
|
||||
$this->maniaControl->getChat()->sendError($message, $player);
|
||||
}
|
||||
Logger::logError($message);
|
||||
return false;
|
||||
}
|
||||
|
||||
Logger::log("Starting Update to Version v{$this->coreUpdateData->version}...");
|
||||
|
||||
$directories = array('core', 'plugins');
|
||||
if (!FileUtil::checkWritePermissions($directories)) {
|
||||
$message = 'Update not possible: Incorrect File System Permissions!';
|
||||
if ($player) {
|
||||
$this->maniaControl->getChat()->sendError($message, $player);
|
||||
}
|
||||
Logger::logError($message);
|
||||
return false;
|
||||
}
|
||||
|
||||
$performBackup = $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_PERFORM_BACKUPS);
|
||||
if ($performBackup && !BackupUtil::performFullBackup()) {
|
||||
$message = 'Creating Backup before Update failed!';
|
||||
if ($player) {
|
||||
$this->maniaControl->getChat()->sendError($message, $player);
|
||||
}
|
||||
Logger::logError($message);
|
||||
}
|
||||
|
||||
$updateData = $this->coreUpdateData;
|
||||
$this->maniaControl->getFileReader()->loadFile($updateData->url, function ($updateFileContent, $error) use (
|
||||
$updateData, &$player
|
||||
) {
|
||||
if (!$updateFileContent || $error) {
|
||||
$message = "Update failed: Couldn't load Update zip! {$error}";
|
||||
if ($player) {
|
||||
$this->maniaControl->getChat()->sendError($message, $player);
|
||||
}
|
||||
Logger::logError($message);
|
||||
return;
|
||||
}
|
||||
|
||||
$tempDir = FileUtil::getTempFolder();
|
||||
if (!$tempDir) {
|
||||
$message = "Update failed: Can't save Update zip!";
|
||||
if ($player) {
|
||||
$this->maniaControl->getChat()->sendError($message, $player);
|
||||
}
|
||||
Logger::logError($message);
|
||||
return;
|
||||
}
|
||||
$updateFileName = $tempDir . basename($updateData->url);
|
||||
|
||||
$bytes = file_put_contents($updateFileName, $updateFileContent);
|
||||
if (!$bytes || $bytes <= 0) {
|
||||
$message = "Update failed: Couldn't save Update zip!";
|
||||
if ($player) {
|
||||
$this->maniaControl->getChat()->sendError($message, $player);
|
||||
}
|
||||
Logger::logError($message);
|
||||
return;
|
||||
}
|
||||
|
||||
$zip = new \ZipArchive();
|
||||
$result = $zip->open($updateFileName);
|
||||
if ($result !== true) {
|
||||
$message = "Update failed: Couldn't open Update Zip. ({$result})";
|
||||
if ($player) {
|
||||
$this->maniaControl->getChat()->sendError($message, $player);
|
||||
}
|
||||
Logger::logError($message);
|
||||
unlink($updateFileName);
|
||||
return;
|
||||
}
|
||||
|
||||
$zip->extractTo(MANIACONTROL_PATH);
|
||||
$zip->close();
|
||||
unlink($updateFileName);
|
||||
FileUtil::deleteTempFolder();
|
||||
|
||||
// Set the build date
|
||||
$this->setBuildDate($updateData->releaseDate);
|
||||
|
||||
$message = 'Update finished!';
|
||||
if ($player) {
|
||||
$this->maniaControl->getChat()->sendSuccess($message, $player);
|
||||
}
|
||||
Logger::log($message);
|
||||
|
||||
$this->maniaControl->restart();
|
||||
});
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the build date version
|
||||
*
|
||||
* @param string $date
|
||||
* @return bool
|
||||
*/
|
||||
public function setBuildDate($date) {
|
||||
$nightlyBuildDateFile = MANIACONTROL_PATH . 'core' . DIRECTORY_SEPARATOR . self::BUILD_DATE_FILE_NAME;
|
||||
$success = (bool)file_put_contents($nightlyBuildDateFile, $date);
|
||||
$this->currentBuildDate = $date;
|
||||
return $success;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle ManiaControl PlayerJoined callback
|
||||
*
|
||||
* @param Player $player
|
||||
*/
|
||||
public function handlePlayerJoined(Player $player) {
|
||||
if (!$this->coreUpdateData) {
|
||||
return;
|
||||
}
|
||||
// Announce available update
|
||||
if (!$this->maniaControl->getAuthenticationManager()->checkPermission($player, self::SETTING_PERMISSION_UPDATE)
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($this->isNightlyUpdateChannel()) {
|
||||
$this->maniaControl->getChat()->sendSuccess('New Nightly Build (' . $this->coreUpdateData->releaseDate . ') available!', $player->login);
|
||||
} else {
|
||||
$this->maniaControl->getChat()->sendInformation('New ManiaControl Version ' . $this->coreUpdateData->version . ' available!', $player->login);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle Player Disconnect Callback
|
||||
*
|
||||
* @param Player $player
|
||||
*/
|
||||
public function handlePlayerDisconnect(Player $player) {
|
||||
$this->checkAutoUpdate();
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle //checkupdate command
|
||||
*
|
||||
* @param array $chatCallback
|
||||
* @param Player $player
|
||||
*/
|
||||
public function handle_CheckUpdate(array $chatCallback, Player $player) {
|
||||
if (!$this->maniaControl->getAuthenticationManager()->checkPermission($player, self::SETTING_PERMISSION_UPDATECHECK)
|
||||
) {
|
||||
$this->maniaControl->getAuthenticationManager()->sendNotAllowed($player);
|
||||
return;
|
||||
}
|
||||
|
||||
$this->checkCoreUpdateAsync(function (UpdateData $updateData = null) use (&$player) {
|
||||
if (!$this->checkUpdateData($updateData)) {
|
||||
$this->maniaControl->getChat()->sendInformation('No Update available!', $player->login);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!$this->checkUpdateDataBuildVersion($updateData)) {
|
||||
$this->maniaControl->getChat()->sendError("Please update Your Server to '{$updateData->minDedicatedBuild}' in order to receive further Updates!", $player->login);
|
||||
return;
|
||||
}
|
||||
|
||||
$isNightly = $this->isNightlyUpdateChannel();
|
||||
if ($isNightly) {
|
||||
$buildDate = $this->getBuildDate();
|
||||
if ($buildDate) {
|
||||
if ($updateData->isNewerThan($buildDate)) {
|
||||
$this->maniaControl->getChat()->sendInformation("No new Build available! (Current Build: '{$buildDate}')", $player->login);
|
||||
return;
|
||||
} else {
|
||||
$this->maniaControl->getChat()->sendSuccess("New Nightly Build ({$updateData->releaseDate}) available! (Current Build: '{$buildDate}')", $player->login);
|
||||
}
|
||||
} else {
|
||||
$this->maniaControl->getChat()->sendSuccess("New Nightly Build ('{$updateData->releaseDate}') available!", $player->login);
|
||||
}
|
||||
} else {
|
||||
$this->maniaControl->getChat()->sendSuccess('Update for Version ' . $updateData->version . ' available!', $player->login);
|
||||
}
|
||||
|
||||
$this->coreUpdateData = $updateData;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle //coreupdate command
|
||||
*
|
||||
* @param array $chatCallback
|
||||
* @param Player $player
|
||||
*/
|
||||
public function handle_CoreUpdate(array $chatCallback, Player $player) {
|
||||
if (!$this->maniaControl->getAuthenticationManager()->checkPermission($player, self::SETTING_PERMISSION_UPDATE)
|
||||
) {
|
||||
$this->maniaControl->getAuthenticationManager()->sendNotAllowed($player);
|
||||
return;
|
||||
}
|
||||
|
||||
$this->checkCoreUpdateAsync(function (UpdateData $updateData = null) use (&$player) {
|
||||
if (!$updateData) {
|
||||
$this->maniaControl->getChat()->sendError('Update is currently not possible!', $player);
|
||||
return;
|
||||
}
|
||||
if (!$this->checkUpdateDataBuildVersion($updateData)) {
|
||||
$this->maniaControl->getChat()->sendError("The Next ManiaControl Update requires a newer Dedicated Server Version!", $player);
|
||||
return;
|
||||
}
|
||||
|
||||
$this->coreUpdateData = $updateData;
|
||||
|
||||
$this->performCoreUpdate($player);
|
||||
});
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user