TrackManiaControl/application/core/Update/PluginUpdateManager.php

400 lines
13 KiB
PHP
Raw Normal View History

2014-05-02 04:03:02 +02:00
<?php
namespace ManiaControl\Update;
use ManiaControl\Callbacks\CallbackListener;
use ManiaControl\Callbacks\CallbackManager;
use ManiaControl\Callbacks\TimerListener;
use ManiaControl\Commands\CommandListener;
2014-05-02 17:50:30 +02:00
use ManiaControl\Files\BackupUtil;
2014-05-02 04:03:02 +02:00
use ManiaControl\Files\FileUtil;
use ManiaControl\ManiaControl;
use ManiaControl\Players\Player;
use ManiaControl\Plugins\Plugin;
use ManiaControl\Plugins\PluginInstallMenu;
use ManiaControl\Plugins\PluginManager;
2014-05-02 17:50:30 +02:00
use ManiaControl\Plugins\PluginMenu;
2014-05-02 04:03:02 +02:00
/**
* Manager checking for ManiaControl Plugin Updates
2014-05-02 17:50:30 +02:00
*
* @author ManiaControl Team <mail@maniacontrol.com>
* @copyright 2014 ManiaControl Team
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
2014-05-02 04:03:02 +02:00
*/
class PluginUpdateManager implements CallbackListener, CommandListener, TimerListener {
/*
* Private Properties
*/
private $maniaControl = null;
/**
* Create a new Plugin Update Manager
2014-05-02 17:50:30 +02:00
*
2014-05-02 04:03:02 +02:00
* @param ManiaControl $maniaControl
*/
public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl;
2014-05-02 17:50:30 +02:00
2014-05-02 04:03:02 +02:00
// Register for callbacks
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_PLAYERMANIALINKPAGEANSWER, $this, 'handleManialinkPageAnswer');
2014-05-02 17:50:30 +02:00
2014-05-02 04:03:02 +02:00
// Register for chat commands
$this->maniaControl->commandManager->registerCommandListener('checkpluginsupdate', $this, 'handle_CheckPluginsUpdate', true, 'Check for Plugin Updates.');
$this->maniaControl->commandManager->registerCommandListener('pluginsupdate', $this, 'handle_PluginsUpdate', true, 'Perform the Plugin Updates.');
}
/**
* Handle //checkpluginsupdate command
2014-05-02 17:50:30 +02:00
*
* @param array $chatCallback
2014-05-02 04:03:02 +02:00
* @param Player $player
*/
public function handle_CheckPluginsUpdate(array $chatCallback, Player $player) {
if (!$this->maniaControl->authenticationManager->checkPermission($player, UpdateManager::SETTING_PERMISSION_UPDATECHECK)) {
$this->maniaControl->authenticationManager->sendNotAllowed($player);
return;
}
2014-05-02 17:50:30 +02:00
$this->checkPluginsUpdate($player);
2014-05-02 04:03:02 +02:00
}
/**
* Check if there are Outdated Plugins installed
2014-05-02 17:50:30 +02:00
*
2014-05-02 04:03:02 +02:00
* @param Player $player
*/
public function checkPluginsUpdate(Player $player = null) {
$message = 'Checking Plugins for newer Versions...';
if ($player) {
$this->maniaControl->chat->sendInformation($message, $player);
}
$this->maniaControl->log($message);
2014-05-02 17:50:30 +02:00
$self = $this;
$maniaControl = $this->maniaControl;
$this->maniaControl->pluginManager->fetchPluginList(function ($data, $error) use (&$self, &$maniaControl, &$player) {
2014-05-02 04:03:02 +02:00
if (!$data || $error) {
$message = 'Error while checking Plugins for newer Versions!';
if ($player) {
$maniaControl->chat->sendError($message, $player);
2014-05-02 04:03:02 +02:00
}
$maniaControl->log($message);
2014-05-02 04:03:02 +02:00
return;
}
2014-05-02 17:50:30 +02:00
$pluginsData = $self->parsePluginsData($data);
$pluginClasses = $maniaControl->pluginManager->getPluginClasses();
2014-05-02 04:03:02 +02:00
$pluginUpdates = array();
2014-05-02 17:50:30 +02:00
2014-05-02 04:03:02 +02:00
foreach ($pluginClasses as $pluginClass) {
/** @var Plugin $pluginClass */
2014-05-02 04:03:02 +02:00
$pluginId = $pluginClass::getId();
if (!isset($pluginsData[$pluginId])) {
continue;
}
2014-05-02 15:35:52 +02:00
/** @var PluginUpdateData $pluginData */
2014-05-02 17:50:30 +02:00
$pluginData = $pluginsData[$pluginId];
2014-05-02 04:03:02 +02:00
$pluginVersion = $pluginClass::getVersion();
if ($pluginData->isNewerThan($pluginVersion)) {
$pluginUpdates[$pluginId] = $pluginData;
2014-05-02 17:50:30 +02:00
$message = "There is an Update of '{$pluginData->pluginName}' available! ('{$pluginClass}' - Version {$pluginData->version})";
2014-05-02 04:03:02 +02:00
if ($player) {
$maniaControl->chat->sendSuccess($message, $player);
2014-05-02 04:03:02 +02:00
}
$maniaControl->log($message);
2014-05-02 04:03:02 +02:00
}
}
2014-05-02 17:50:30 +02:00
2014-05-02 04:03:02 +02:00
if (empty($pluginUpdates)) {
$message = 'Plugins Update Check completed: All Plugins are up-to-date!';
if ($player) {
$maniaControl->chat->sendSuccess($message, $player);
2014-05-02 04:03:02 +02:00
}
$maniaControl->log($message);
2014-05-02 17:50:30 +02:00
} else {
2014-05-02 04:03:02 +02:00
$updatesCount = count($pluginUpdates);
2014-05-02 17:50:30 +02:00
$message = "Plugins Update Check completed: There are {$updatesCount} Updates available!";
2014-05-02 04:03:02 +02:00
if ($player) {
$maniaControl->chat->sendSuccess($message, $player);
2014-05-02 04:03:02 +02:00
}
$maniaControl->log($message);
2014-05-02 04:03:02 +02:00
}
});
}
2014-05-02 17:50:30 +02:00
/**
* 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->authenticationManager->checkPermission($player, UpdateManager::SETTING_PERMISSION_UPDATE)) {
$this->maniaControl->authenticationManager->sendNotAllowed($player);
return;
}
$this->performPluginsUpdate($player);
}
2014-05-02 04:03:02 +02:00
/**
* Perform an Update of all outdated Plugins
2014-05-02 17:50:30 +02:00
*
2014-05-02 04:03:02 +02:00
* @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->chat->sendInformation($message, $player);
}
$this->maniaControl->log($message);
return;
}
2014-05-02 17:50:30 +02:00
2014-05-02 05:04:55 +02:00
$message = "Starting Plugins Updating...";
2014-05-02 04:03:02 +02:00
if ($player) {
$this->maniaControl->chat->sendInformation($message, $player);
}
$this->maniaControl->log($message);
2014-05-02 17:50:30 +02:00
2014-05-13 16:03:26 +02:00
$performBackup = $this->maniaControl->settingManager->getSettingValue($this->maniaControl->updateManager, UpdateManager::SETTING_PERFORM_BACKUPS);
2014-05-02 04:03:02 +02:00
if ($performBackup && !BackupUtil::performPluginsBackup()) {
$message = 'Creating Backup before Plugins Update failed!';
if ($player) {
$this->maniaControl->chat->sendError($message, $player);
}
$this->maniaControl->log($message);
}
2014-05-02 17:50:30 +02:00
2014-05-02 04:03:02 +02:00
foreach ($pluginsUpdates as $pluginUpdateData) {
2014-05-02 05:04:55 +02:00
$this->installPlugin($pluginUpdateData, $player, true);
2014-05-02 04:03:02 +02:00
}
}
/**
* Check for Plugin Updates
2014-05-02 17:50:30 +02:00
*
2014-05-02 04:03:02 +02:00
* @return mixed
*/
public function getPluginsUpdates() {
2014-05-02 17:50:30 +02:00
$url = ManiaControl::URL_WEBSERVICE . 'plugins';
$dataJson = FileUtil::loadFile($url);
2014-05-02 04:03:02 +02:00
$pluginData = json_decode($dataJson);
if (!$pluginData || empty($pluginData)) {
return false;
}
2014-05-02 17:50:30 +02:00
2014-05-02 04:03:02 +02:00
$pluginsUpdates = $this->parsePluginsData($pluginData);
2014-05-02 17:50:30 +02:00
$updates = array();
2014-05-02 04:03:02 +02:00
$pluginClasses = $this->maniaControl->pluginManager->getPluginClasses();
foreach ($pluginClasses as $pluginClass) {
/**
* @var Plugin $pluginClass
*/
$pluginId = $pluginClass::getId();
if (isset($pluginsUpdates[$pluginId])) {
2014-05-02 15:35:52 +02:00
/** @var PluginUpdateData $pluginUpdateData */
2014-05-02 04:03:02 +02:00
$pluginUpdateData = $pluginsUpdates[$pluginId];
2014-05-02 17:50:30 +02:00
$pluginVersion = $pluginClass::getVersion();
2014-05-02 04:03:02 +02:00
if ($pluginUpdateData->isNewerThan($pluginVersion)) {
$updates[$pluginId] = $pluginUpdateData;
}
}
}
2014-05-02 17:50:30 +02:00
2014-05-02 04:03:02 +02:00
if (empty($updates)) {
return false;
}
return $updates;
}
/**
* Load the given Plugin Update Data
2014-05-02 17:50:30 +02:00
*
2014-05-02 04:03:02 +02:00
* @param PluginUpdateData $pluginUpdateData
2014-05-02 17:50:30 +02:00
* @param Player $player
* @param bool $update
2014-05-02 04:03:02 +02:00
*/
private function installPlugin(PluginUpdateData $pluginUpdateData, Player $player = null, $update = false) {
$self = $this;
2014-05-02 17:50:30 +02:00
$this->maniaControl->fileReader->loadFile($pluginUpdateData->url, function ($updateFileContent, $error) use (&$self, &$pluginUpdateData, &$player, &$update) {
2014-05-02 18:21:38 +02:00
if (!$updateFileContent || $error) {
$message = "Error loading Update Data for '{$pluginUpdateData->pluginName}': {$error}!";
if ($player) {
$self->maniaControl->chat->sendInformation($message, $player);
}
$self->maniaControl->log($message);
return;
}
2014-05-02 17:50:30 +02:00
$actionNoun = ($update ? 'Update' : 'Install');
$actionVerb = ($update ? 'Updating' : 'Installing');
2014-05-02 04:03:02 +02:00
$actionVerbDone = ($update ? 'updated' : 'installed');
2014-05-02 17:50:30 +02:00
2014-05-02 04:03:02 +02:00
$message = "Now {$actionVerb} '{$pluginUpdateData->pluginName}'...";
if ($player) {
$self->maniaControl->chat->sendInformation($message, $player);
}
$self->maniaControl->log($message);
2014-05-02 17:50:30 +02:00
$tempDir = FileUtil::getTempFolder();
2014-05-02 04:03:02 +02:00
$updateFileName = $tempDir . $pluginUpdateData->zipfile;
2014-05-02 17:50:30 +02:00
2014-05-02 04:03:02 +02:00
$bytes = file_put_contents($updateFileName, $updateFileContent);
if (!$bytes || $bytes <= 0) {
$message = "Plugin {$actionNoun} failed: Couldn't save {$actionNoun} Zip!";
if ($player) {
$self->maniaControl->chat->sendError($message, $player);
}
trigger_error($message);
return;
}
2014-05-02 17:50:30 +02:00
$zip = new \ZipArchive();
2014-05-02 04:03:02 +02:00
$result = $zip->open($updateFileName);
if ($result !== true) {
$message = "Plugin {$actionNoun} failed: Couldn't open {$actionNoun} Zip! ({$result})";
if ($player) {
$self->maniaControl->chat->sendError($message, $player);
}
trigger_error($message);
return;
}
2014-05-02 17:50:30 +02:00
2014-05-03 21:37:28 +02:00
$zip->extractTo(ManiaControlDir . 'plugins' . DIRECTORY_SEPARATOR);
2014-05-02 04:03:02 +02:00
$zip->close();
unlink($updateFileName);
FileUtil::removeTempFolder();
2014-05-02 17:50:30 +02:00
2014-05-02 04:58:41 +02:00
$messageExtra = '';
if ($update) {
$messageExtra = ' (Restart ManiaControl to load the new Version!)';
}
$message = "Successfully {$actionVerbDone} '{$pluginUpdateData->pluginName}'!{$messageExtra}";
2014-05-02 04:03:02 +02:00
if ($player) {
$self->maniaControl->chat->sendSuccess($message, $player);
}
$self->maniaControl->log($message);
2014-05-02 17:50:30 +02:00
2014-05-02 04:03:02 +02:00
if (!$update) {
$newPluginClasses = $self->maniaControl->pluginManager->loadPlugins();
if (empty($newPluginClasses)) {
$message = "Loading fresh installed Plugin '{$pluginUpdateData->pluginName}' failed!";
if ($player) {
$self->maniaControl->chat->sendError($message, $player);
}
$self->maniaControl->log($message);
2014-05-02 17:50:30 +02:00
} else {
2014-05-02 04:03:02 +02:00
$message = "Successfully loaded fresh installed Plugin '{$pluginUpdateData->pluginName}'!";
if ($player) {
$self->maniaControl->chat->sendSuccess($message, $player);
}
$self->maniaControl->log($message);
2014-05-02 17:50:30 +02:00
2014-05-02 04:03:02 +02:00
$menuId = $self->maniaControl->configurator->getMenuId('Install Plugins');
$self->maniaControl->configurator->reopenMenu($player, $menuId);
}
}
});
}
2014-05-02 17:50:30 +02:00
/**
* 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, PluginInstallMenu::ACTION_PREFIX_INSTALLPLUGIN) === 0);
if (!$update && !$install) {
return;
}
$login = $callback[1][1];
$player = $this->maniaControl->playerManager->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->chat->sendError($message, $player);
}
}
} else {
$pluginId = substr($actionId, strlen(PluginInstallMenu::ACTION_PREFIX_INSTALLPLUGIN));
$url = ManiaControl::URL_WEBSERVICE . 'plugins?id=' . $pluginId;
$self = $this;
$this->maniaControl->fileReader->loadFile($url, function ($data, $error) use (&$self, &$player) {
if ($error || !$data) {
$message = "Error loading Plugin Install Data! {$error}";
$self->maniaControl->chat->sendError($message, $player);
return;
}
$data = json_decode($data);
if (!isset($data[0])) {
$message = "Error loading Plugin Install Data! {$error}";
$self->maniaControl->chat->sendError($message, $player);
return;
}
$pluginUpdateData = new PluginUpdateData($data[0]);
$self->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?id=' . $pluginId;
$dataJson = FileUtil::loadFile($url);
$pluginVersions = json_decode($dataJson);
if (!$pluginVersions || !isset($pluginVersions[0])) {
return false;
}
$pluginUpdateData = new PluginUpdateData($pluginVersions[0]);
$pluginVersion = $pluginClass::getVersion();
if ($pluginUpdateData->isNewerThan($pluginVersion)) {
return $pluginUpdateData;
}
return false;
}
2014-05-02 04:03:02 +02:00
}