fixed //checkupdate

This commit is contained in:
Steffen Schröder 2014-04-29 23:53:55 +02:00
parent 200f199c06
commit ab375e636e

View File

@ -17,42 +17,45 @@ use ManiaControl\Plugins\PluginMenu;
/** /**
* Manager checking for ManiaControl Core and Plugin Updates * Manager checking for ManiaControl Core and Plugin Updates
* *
* @author steeffeen & kremsy * @author steeffeen & kremsy
* @copyright ManiaControl Copyright © 2014 ManiaControl Team * @copyright ManiaControl Copyright © 2014 ManiaControl Team
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3 * @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
*/ */
class UpdateManager implements CallbackListener, CommandListener, TimerListener { class UpdateManager implements CallbackListener, CommandListener, TimerListener {
/* /*
* Constants * Constants
*/ */
const SETTING_ENABLEUPDATECHECK = 'Enable Automatic Core Update Check'; const SETTING_ENABLEUPDATECHECK = 'Enable Automatic Core Update Check';
const SETTING_UPDATECHECK_INTERVAL = 'Core Update Check Interval (Hours)'; const SETTING_UPDATECHECK_INTERVAL = 'Core Update Check Interval (Hours)';
const SETTING_UPDATECHECK_CHANNEL = 'Core Update Channel (release, beta, nightly)'; const SETTING_UPDATECHECK_CHANNEL = 'Core Update Channel (release, beta, nightly)';
const SETTING_PERFORM_BACKUPS = 'Perform Backup before Updating'; const SETTING_PERFORM_BACKUPS = 'Perform Backup before Updating';
const SETTING_AUTO_UPDATE = 'Perform update automatically'; const SETTING_AUTO_UPDATE = 'Perform update automatically';
const SETTING_PERMISSION_UPDATE = 'Update Core'; const SETTING_PERMISSION_UPDATE = 'Update Core';
const SETTING_PERMISSION_UPDATECHECK = 'Check Core Update'; const SETTING_PERMISSION_UPDATECHECK = 'Check Core Update';
const CHANNEL_RELEASE = 'release'; const CHANNEL_RELEASE = 'release';
const CHANNEL_BETA = 'beta'; const CHANNEL_BETA = 'beta';
const CHANNEL_NIGHTLY = 'nightly'; const CHANNEL_NIGHTLY = 'nightly';
/* /*
* Private Properties * Private Properties
*/ */
private $maniaControl = null; private $maniaControl = null;
/** @var UpdateData $coreUpdateData */ /**
*
* @var UpdateData $coreUpdateData
*/
private $coreUpdateData = null; private $coreUpdateData = null;
private $currentBuildDate = ""; private $currentBuildDate = "";
/** /**
* Create a new Update Manager * Create a new Update Manager
* *
* @param ManiaControl $maniaControl * @param ManiaControl $maniaControl
*/ */
public function __construct(ManiaControl $maniaControl) { public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl; $this->maniaControl = $maniaControl;
// Init settings // Init settings
$this->maniaControl->settingManager->initSetting($this, self::SETTING_ENABLEUPDATECHECK, true); $this->maniaControl->settingManager->initSetting($this, self::SETTING_ENABLEUPDATECHECK, true);
$this->maniaControl->settingManager->initSetting($this, self::SETTING_AUTO_UPDATE, true); $this->maniaControl->settingManager->initSetting($this, self::SETTING_AUTO_UPDATE, true);
@ -60,29 +63,29 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener
// TODO: 'nightly' only during dev // TODO: 'nightly' only during dev
$this->maniaControl->settingManager->initSetting($this, self::SETTING_UPDATECHECK_CHANNEL, self::CHANNEL_BETA); $this->maniaControl->settingManager->initSetting($this, self::SETTING_UPDATECHECK_CHANNEL, self::CHANNEL_BETA);
$this->maniaControl->settingManager->initSetting($this, self::SETTING_PERFORM_BACKUPS, true); $this->maniaControl->settingManager->initSetting($this, self::SETTING_PERFORM_BACKUPS, true);
// Register for callbacks // Register for callbacks
$updateInterval = $this->maniaControl->settingManager->getSetting($this, self::SETTING_UPDATECHECK_INTERVAL); $updateInterval = $this->maniaControl->settingManager->getSetting($this, self::SETTING_UPDATECHECK_INTERVAL);
$this->maniaControl->timerManager->registerTimerListening($this, 'hourlyUpdateCheck', 1000 * 60 * 60 * $updateInterval); $this->maniaControl->timerManager->registerTimerListening($this, 'hourlyUpdateCheck', 1000 * 60 * 60 * $updateInterval);
$this->maniaControl->callbackManager->registerCallbackListener(PlayerManager::CB_PLAYERCONNECT, $this, 'handlePlayerJoined'); $this->maniaControl->callbackManager->registerCallbackListener(PlayerManager::CB_PLAYERCONNECT, $this, 'handlePlayerJoined');
$this->maniaControl->callbackManager->registerCallbackListener(PlayerManager::CB_PLAYERDISCONNECT, $this, 'autoUpdate'); $this->maniaControl->callbackManager->registerCallbackListener(PlayerManager::CB_PLAYERDISCONNECT, $this, 'autoUpdate');
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_PLAYERMANIALINKPAGEANSWER, $this, 'handleManialinkPageAnswer'); $this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_PLAYERMANIALINKPAGEANSWER, $this, 'handleManialinkPageAnswer');
//define Permissions // define Permissions
$this->maniaControl->authenticationManager->definePermissionLevel(self::SETTING_PERMISSION_UPDATE, AuthenticationManager::AUTH_LEVEL_ADMIN); $this->maniaControl->authenticationManager->definePermissionLevel(self::SETTING_PERMISSION_UPDATE, AuthenticationManager::AUTH_LEVEL_ADMIN);
$this->maniaControl->authenticationManager->definePermissionLevel(self::SETTING_PERMISSION_UPDATECHECK, AuthenticationManager::AUTH_LEVEL_MODERATOR); $this->maniaControl->authenticationManager->definePermissionLevel(self::SETTING_PERMISSION_UPDATECHECK, AuthenticationManager::AUTH_LEVEL_MODERATOR);
// Register for chat commands // Register for chat commands
$this->maniaControl->commandManager->registerCommandListener('checkupdate', $this, 'handle_CheckUpdate', true); $this->maniaControl->commandManager->registerCommandListener('checkupdate', $this, 'handle_CheckUpdate', true);
$this->maniaControl->commandManager->registerCommandListener('coreupdate', $this, 'handle_CoreUpdate', true); $this->maniaControl->commandManager->registerCommandListener('coreupdate', $this, 'handle_CoreUpdate', true);
$this->maniaControl->commandManager->registerCommandListener('pluginupdate', $this, 'handle_PluginUpdate', true); $this->maniaControl->commandManager->registerCommandListener('pluginupdate', $this, 'handle_PluginUpdate', true);
$this->currentBuildDate = $this->getNightlyBuildDate(); $this->currentBuildDate = $this->getNightlyBuildDate();
} }
/** /**
* Perform Hourly Update Check * Perform Hourly Update Check
* *
* @param $time * @param $time
*/ */
public function hourlyUpdateCheck($time) { public function hourlyUpdateCheck($time) {
@ -94,18 +97,19 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener
} }
return; return;
} }
//Check if a new Core Update is Available // Check if a new Core Update is Available
$self = $this; $self = $this;
$maniaControl = $this->maniaControl; $maniaControl = $this->maniaControl;
$this->checkCoreUpdateAsync(function (UpdateData $updateData) use ($self, $maniaControl, $time) { $this->checkCoreUpdateAsync(function (UpdateData $updateData) use($self, $maniaControl, $time) {
$buildDate = strtotime($self->getCurrentBuildDate()); $buildDate = strtotime($self->getCurrentBuildDate());
$releaseTime = strtotime($updateData->releaseDate); $releaseTime = strtotime($updateData->releaseDate);
if ($buildDate < $releaseTime) { if ($buildDate < $releaseTime) {
$updateChannel = $maniaControl->settingManager->getSetting($self, UpdateManager::SETTING_UPDATECHECK_CHANNEL); $updateChannel = $maniaControl->settingManager->getSetting($self, UpdateManager::SETTING_UPDATECHECK_CHANNEL);
if ($updateChannel != UpdateManager::CHANNEL_NIGHTLY) { if ($updateChannel != UpdateManager::CHANNEL_NIGHTLY) {
$maniaControl->log('New ManiaControl Version ' . $updateData->version . ' available!'); $maniaControl->log('New ManiaControl Version ' . $updateData->version . ' available!');
} else { }
else {
$maniaControl->log('New Nightly Build (' . $updateData->releaseDate . ') available!'); $maniaControl->log('New Nightly Build (' . $updateData->releaseDate . ') available!');
} }
$self->setCoreUpdateData($updateData); $self->setCoreUpdateData($updateData);
@ -116,7 +120,7 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener
/** /**
* Handle ManiaControl PlayerJoined callback * Handle ManiaControl PlayerJoined callback
* *
* @param Player $player * @param Player $player
*/ */
public function handlePlayerJoined(Player $player) { public function handlePlayerJoined(Player $player) {
@ -127,14 +131,15 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener
if (!$this->maniaControl->authenticationManager->checkPermission($player, self::SETTING_PERMISSION_UPDATE)) { if (!$this->maniaControl->authenticationManager->checkPermission($player, self::SETTING_PERMISSION_UPDATE)) {
return; return;
} }
$buildDate = strtotime($this->currentBuildDate); $buildDate = strtotime($this->currentBuildDate);
$releaseTime = strtotime($this->coreUpdateData->releaseDate); $releaseTime = strtotime($this->coreUpdateData->releaseDate);
if ($buildDate < $releaseTime) { if ($buildDate < $releaseTime) {
$updateChannel = $this->maniaControl->settingManager->getSetting($this, self::SETTING_UPDATECHECK_CHANNEL); $updateChannel = $this->maniaControl->settingManager->getSetting($this, self::SETTING_UPDATECHECK_CHANNEL);
if ($updateChannel != self::CHANNEL_NIGHTLY) { if ($updateChannel != self::CHANNEL_NIGHTLY) {
$this->maniaControl->chat->sendInformation('New ManiaControl Version ' . $this->coreUpdateData->version . ' available!', $player->login); $this->maniaControl->chat->sendInformation('New ManiaControl Version ' . $this->coreUpdateData->version . ' available!', $player->login);
} else { }
else {
$this->maniaControl->chat->sendSuccess('New Nightly Build (' . $this->coreUpdateData->releaseDate . ') available!', $player->login); $this->maniaControl->chat->sendSuccess('New Nightly Build (' . $this->coreUpdateData->releaseDate . ') available!', $player->login);
} }
} }
@ -142,7 +147,7 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener
/** /**
* Perform automatic update as soon as a the Server is empty (also every hour got checked when its empty) * Perform automatic update as soon as a the Server is empty (also every hour got checked when its empty)
* *
* @param mixed $callback * @param mixed $callback
*/ */
public function autoUpdate($callback) { public function autoUpdate($callback) {
@ -150,26 +155,26 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener
if (!$autoUpdate) { if (!$autoUpdate) {
return; return;
} }
if (count($this->maniaControl->playerManager->getPlayers()) > 0) { if (count($this->maniaControl->playerManager->getPlayers()) > 0) {
return; return;
} }
if (!$this->coreUpdateData) { if (!$this->coreUpdateData) {
return; return;
} }
$version = $this->maniaControl->client->getVersion(); $version = $this->maniaControl->client->getVersion();
if ($this->coreUpdateData->minDedicatedBuild > $version->build) { if ($this->coreUpdateData->minDedicatedBuild > $version->build) {
return; return;
} }
$buildDate = strtotime($this->currentBuildDate); $buildDate = strtotime($this->currentBuildDate);
$releaseTime = strtotime($this->coreUpdateData->releaseDate); $releaseTime = strtotime($this->coreUpdateData->releaseDate);
if ($buildDate && $buildDate >= $releaseTime) { if ($buildDate && $buildDate >= $releaseTime) {
return; return;
} }
$this->maniaControl->log("Starting Update to Version v{$this->coreUpdateData->version}..."); $this->maniaControl->log("Starting Update to Version v{$this->coreUpdateData->version}...");
$performBackup = $this->maniaControl->settingManager->getSetting($this, self::SETTING_PERFORM_BACKUPS); $performBackup = $this->maniaControl->settingManager->getSetting($this, self::SETTING_PERFORM_BACKUPS);
if ($performBackup && !$this->performBackup()) { if ($performBackup && !$this->performBackup()) {
@ -178,11 +183,10 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener
$this->performCoreUpdate($this->coreUpdateData); $this->performCoreUpdate($this->coreUpdateData);
} }
/** /**
* Handle //checkupdate command * Handle //checkupdate command
* *
* @param array $chatCallback * @param array $chatCallback
* @param Player $player * @param Player $player
*/ */
public function handle_CheckUpdate(array $chatCallback, Player $player) { public function handle_CheckUpdate(array $chatCallback, Player $player) {
@ -191,39 +195,22 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener
return; return;
} }
$updateChannel = $this->maniaControl->settingManager->getSetting($this, self::SETTING_UPDATECHECK_CHANNEL); $updateChannel = $this->maniaControl->settingManager->getSetting($this, self::SETTING_UPDATECHECK_CHANNEL);
if ($updateChannel != self::CHANNEL_NIGHTLY) { if ($updateChannel === self::CHANNEL_NIGHTLY) {
// Check update and send result message
$self = $this;
$this->checkCoreUpdateAsync(function (UpdateData $updateData) use (&$self, &$player) {
if (!$updateData) {
$self->maniaControl->chat->sendInformation('No Update available!', $player->login);
return;
}
$version = $self->maniaControl->client->getVersion();
if ($updateData->minDedicatedBuild > $version->build) {
$self->maniaControl->chat->sendError("No new Build for this Server-version available!", $player->login);
return;
}
$self->maniaControl->chat->sendSuccess('Update for Version ' . $updateData->version . ' available!', $player->login);
});
} else {
// Special nightly channel updating // Special nightly channel updating
$self = $this; $self = $this;
$this->checkCoreUpdateAsync(function (UpdateData $updateData) use (&$self, &$player) { $this->checkCoreUpdateAsync(function ($updateData) use(&$self, &$player) {
if (!$updateData) { if (!$updateData) {
$self->maniaControl->chat->sendInformation('No Update available!', $player->login); $self->maniaControl->chat->sendInformation('No Update available!', $player->login);
return; return;
} }
$version = $self->maniaControl->client->getVersion(); $version = $self->maniaControl->client->getVersion();
if ($updateData->minDedicatedBuild > $version->build) { if ($updateData->minDedicatedBuild > $version->build) {
$self->maniaControl->chat->sendError("No new Build for this Server-version available!", $player->login); $self->maniaControl->chat->sendError("No new Build for this Server-version available!", $player->login);
return; return;
} }
$buildTime = strtotime($self->currentBuildDate); $buildTime = strtotime($self->currentBuildDate);
$releaseTime = strtotime($updateData->releaseDate); $releaseTime = strtotime($updateData->releaseDate);
if ($buildTime != '') { if ($buildTime != '') {
if ($buildTime >= $releaseTime) { if ($buildTime >= $releaseTime) {
@ -231,31 +218,53 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener
return; return;
} }
$self->maniaControl->chat->sendSuccess('New Nightly Build (' . $updateData->releaseDate . ') available, current build: ' . $self->currentBuildDate . '!', $player->login); $self->maniaControl->chat->sendSuccess('New Nightly Build (' . $updateData->releaseDate . ') available, current build: ' . $self->currentBuildDate . '!', $player->login);
} else { }
else {
$self->maniaControl->chat->sendSuccess('New Nightly Build (' . $updateData->releaseDate . ') available!', $player->login); $self->maniaControl->chat->sendSuccess('New Nightly Build (' . $updateData->releaseDate . ') available!', $player->login);
} }
}, true); }, true);
} }
else {
// Check update and send result message
$self = $this;
$this->checkCoreUpdateAsync(function ($updateData) use(&$self, &$player) {
if (!$updateData) {
$self->maniaControl->chat->sendInformation('No Update available!', $player->login);
return;
}
$version = $self->maniaControl->client->getVersion();
if ($updateData->minDedicatedBuild > $version->build) {
$self->maniaControl->chat->sendError("No new Build for this Server-version available!", $player->login);
return;
}
$self->maniaControl->chat->sendSuccess('Update for Version ' . $updateData->version . ' available!', $player->login);
});
}
} }
/** /**
* Handle PlayerManialinkPageAnswer callback * Handle PlayerManialinkPageAnswer callback
* *
* @param array $callback * @param array $callback
*/ */
public function handleManialinkPageAnswer(array $callback) { public function handleManialinkPageAnswer(array $callback) {
$actionId = $callback[1][2]; $actionId = $callback[1][2];
$update = (strpos($actionId, PluginMenu::ACTION_PREFIX_UPDATEPLUGIN) === 0); $update = (strpos($actionId, PluginMenu::ACTION_PREFIX_UPDATEPLUGIN) === 0);
$install = (strpos($actionId, PluginInstallMenu::ACTION_PREFIX_INSTALLPLUGIN) === 0); $install = (strpos($actionId, PluginInstallMenu::ACTION_PREFIX_INSTALLPLUGIN) === 0);
$login = $callback[1][1]; $login = $callback[1][1];
$player = $this->maniaControl->playerManager->getPlayer($login); $player = $this->maniaControl->playerManager->getPlayer($login);
if ($update) { if ($update) {
$pluginClass = substr($actionId, strlen(PluginMenu::ACTION_PREFIX_UPDATEPLUGIN)); $pluginClass = substr($actionId, strlen(PluginMenu::ACTION_PREFIX_UPDATEPLUGIN));
if ($pluginClass == 'All') { if ($pluginClass == 'All') {
$this->checkPluginsUpdate($player); $this->checkPluginsUpdate($player);
} else { }
else {
$newUpdate = $this->checkPluginUpdate($pluginClass); $newUpdate = $this->checkPluginUpdate($pluginClass);
if ($newUpdate != false) { if ($newUpdate != false) {
$newUpdate->pluginClass = $pluginClass; $newUpdate->pluginClass = $pluginClass;
@ -263,12 +272,12 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener
} }
} }
} }
if ($install) { if ($install) {
$pluginId = substr($actionId, strlen(PluginInstallMenu::ACTION_PREFIX_INSTALLPLUGIN)); $pluginId = substr($actionId, strlen(PluginInstallMenu::ACTION_PREFIX_INSTALLPLUGIN));
$url = ManiaControl::URL_WEBSERVICE . 'plugins?id=' . $pluginId; $url = ManiaControl::URL_WEBSERVICE . 'plugins?id=' . $pluginId;
$dataJson = FileUtil::loadFile($url); $dataJson = FileUtil::loadFile($url);
$pluginVersions = json_decode($dataJson); $pluginVersions = json_decode($dataJson);
if ($pluginVersions && isset($pluginVersions[0])) { if ($pluginVersions && isset($pluginVersions[0])) {
$pluginData = $pluginVersions[0]; $pluginData = $pluginVersions[0];
@ -279,7 +288,7 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener
/** /**
* Get the Build Date of the local Nightly Build Version * Get the Build Date of the local Nightly Build Version
* *
* @return String $buildTime * @return String $buildTime
*/ */
private function getNightlyBuildDate() { private function getNightlyBuildDate() {
@ -293,7 +302,7 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener
/** /**
* Get the CurrentBuildDate * Get the CurrentBuildDate
* *
* @return string * @return string
*/ */
public function getCurrentBuildDate() { public function getCurrentBuildDate() {
@ -302,21 +311,21 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener
/** /**
* Set the Build Date of the local Nightly Build Version * Set the Build Date of the local Nightly Build Version
* *
* @param $date * @param $date
* @return mixed * @return mixed
*/ */
private function setNightlyBuildDate($date) { private function setNightlyBuildDate($date) {
$nightlyBuildDateFile = ManiaControlDir . '/core/nightly_build.txt'; $nightlyBuildDateFile = ManiaControlDir . '/core/nightly_build.txt';
$success = file_put_contents($nightlyBuildDateFile, $date); $success = file_put_contents($nightlyBuildDateFile, $date);
$this->currentBuildDate = $date; $this->currentBuildDate = $date;
return $success; return $success;
} }
/** /**
* Handle //coreupdate command * Handle //coreupdate command
* *
* @param array $chatCallback * @param array $chatCallback
* @param Player $player * @param Player $player
*/ */
public function handle_CoreUpdate(array $chatCallback, Player $player) { public function handle_CoreUpdate(array $chatCallback, Player $player) {
@ -324,9 +333,9 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener
$this->maniaControl->authenticationManager->sendNotAllowed($player); $this->maniaControl->authenticationManager->sendNotAllowed($player);
return; return;
} }
$self = $this; $self = $this;
$this->checkCoreUpdateAsync(function (UpdateData $updateData) use (&$self, &$player) { $this->checkCoreUpdateAsync(function (UpdateData $updateData) use(&$self, &$player) {
if (!$updateData) { if (!$updateData) {
$self->maniaControl->chat->sendError('Update is currently not possible!', $player->login); $self->maniaControl->chat->sendError('Update is currently not possible!', $player->login);
return; return;
@ -336,7 +345,7 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener
$self->maniaControl->chat->sendError("ManiaControl update version requires a newer Dedicated Server version!", $player->login); $self->maniaControl->chat->sendError("ManiaControl update version requires a newer Dedicated Server version!", $player->login);
return; return;
} }
$self->maniaControl->chat->sendInformation("Starting Update to Version v{$updateData->version}...", $player->login); $self->maniaControl->chat->sendInformation("Starting Update to Version v{$updateData->version}...", $player->login);
$self->maniaControl->log("Starting Update to Version v{$updateData->version}..."); $self->maniaControl->log("Starting Update to Version v{$updateData->version}...");
$performBackup = $self->maniaControl->settingManager->getSetting($self, UpdateManager::SETTING_PERFORM_BACKUPS); $performBackup = $self->maniaControl->settingManager->getSetting($self, UpdateManager::SETTING_PERFORM_BACKUPS);
@ -344,15 +353,15 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener
$self->maniaControl->chat->sendError('Creating backup failed.', $player->login); $self->maniaControl->chat->sendError('Creating backup failed.', $player->login);
$self->maniaControl->log("Creating backup failed."); $self->maniaControl->log("Creating backup failed.");
} }
$self->performCoreUpdate($updateData, $player); $self->performCoreUpdate($updateData, $player);
}, true); }, true);
} }
/** /**
* Handle //pluginupdate command * Handle //pluginupdate command
* *
* @param array $chatCallback * @param array $chatCallback
* @param Player $player * @param Player $player
*/ */
public function handle_PluginUpdate(array $chatCallback, Player $player) { public function handle_PluginUpdate(array $chatCallback, Player $player) {
@ -360,31 +369,31 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener
$this->maniaControl->authenticationManager->sendNotAllowed($player); $this->maniaControl->authenticationManager->sendNotAllowed($player);
return; return;
} }
$this->checkPluginsUpdate($player); $this->checkPluginsUpdate($player);
} }
/** /**
* Checks if there are outdated plugins active. * Checks if there are outdated plugins active.
* *
* @param Player $player * @param Player $player
*/ */
public function checkPluginsUpdate(Player $player = null) { public function checkPluginsUpdate(Player $player = null) {
$this->maniaControl->log('[UPDATE] Checking plugins for newer versions ...'); $this->maniaControl->log('[UPDATE] Checking plugins for newer versions ...');
$self = $this; $self = $this;
$this->maniaControl->pluginManager->fetchPluginList(function ($data, $error) use (&$self, &$player) { $this->maniaControl->pluginManager->fetchPluginList(function ($data, $error) use(&$self, &$player) {
$outdatedPlugins = array(); $outdatedPlugins = array();
if (!$data || $error) { if (!$data || $error) {
$self->maniaControl->log('[UPDATE] Error while checking plugins for newer version'); $self->maniaControl->log('[UPDATE] Error while checking plugins for newer version');
return; return;
} }
$pluginClasses = $self->maniaControl->pluginManager->getPluginClasses(); $pluginClasses = $self->maniaControl->pluginManager->getPluginClasses();
foreach($data as $plugin) { foreach ($data as $plugin) {
foreach($pluginClasses as $pluginClass) { foreach ($pluginClasses as $pluginClass) {
$id = $pluginClass::getId(); $id = $pluginClass::getId();
if ($plugin->id == $id) { if ($plugin->id == $id) {
if ($plugin->currentVersion->version > $pluginClass::getVersion()) { if ($plugin->currentVersion->version > $pluginClass::getVersion()) {
@ -394,17 +403,18 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener
} }
} }
} }
if (count($outdatedPlugins) > 0) { if (count($outdatedPlugins) > 0) {
$self->maniaControl->log('[UPDATE] Checking plugins: COMPLETE, there are ' . count($outdatedPlugins) . ' outdated plugins, now updating ...'); $self->maniaControl->log('[UPDATE] Checking plugins: COMPLETE, there are ' . count($outdatedPlugins) . ' outdated plugins, now updating ...');
if ($player) { if ($player) {
$self->maniaControl->chat->sendInformation('Checking plugins: COMPLETE, there are ' . count($outdatedPlugins) . ' outdated plugins, now updating ...', $player->login); $self->maniaControl->chat->sendInformation('Checking plugins: COMPLETE, there are ' . count($outdatedPlugins) . ' outdated plugins, now updating ...', $player->login);
} }
$self->performPluginsBackup(); $self->performPluginsBackup();
foreach($outdatedPlugins as $plugin) { foreach ($outdatedPlugins as $plugin) {
$self->updatePlugin($plugin, $player); $self->updatePlugin($plugin, $player);
} }
} else { }
else {
$self->maniaControl->log('[UPDATE] Checking plugins: COMPLETE, all plugins are up-to-date!'); $self->maniaControl->log('[UPDATE] Checking plugins: COMPLETE, all plugins are up-to-date!');
if ($player) { if ($player) {
$self->maniaControl->chat->sendInformation('Checking plugins: COMPLETE, all plugins are up-to-date!', $player->login); $self->maniaControl->chat->sendInformation('Checking plugins: COMPLETE, all plugins are up-to-date!', $player->login);
@ -415,7 +425,7 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener
/** /**
* Check given Plugin Class for Update * Check given Plugin Class for Update
* *
* @param string $pluginClass * @param string $pluginClass
* @return mixed * @return mixed
*/ */
@ -423,15 +433,18 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener
if (is_object($pluginClass)) { if (is_object($pluginClass)) {
$pluginClass = get_class($pluginClass); $pluginClass = get_class($pluginClass);
} }
/** @var Plugin $pluginClass */ /**
$pluginId = $pluginClass::getId(); *
$url = ManiaControl::URL_WEBSERVICE . 'plugins?id=' . $pluginId; * @var Plugin $pluginClass
$dataJson = FileUtil::loadFile($url); */
$pluginId = $pluginClass::getId();
$url = ManiaControl::URL_WEBSERVICE . 'plugins?id=' . $pluginId;
$dataJson = FileUtil::loadFile($url);
$pluginVersions = json_decode($dataJson); $pluginVersions = json_decode($dataJson);
if (!$pluginVersions || !isset($pluginVersions[0])) { if (!$pluginVersions || !isset($pluginVersions[0])) {
return false; return false;
} }
$pluginData = $pluginVersions[0]; $pluginData = $pluginVersions[0];
$pluginVersion = $pluginClass::getVersion(); $pluginVersion = $pluginClass::getVersion();
if ($pluginData->currentVersion->version <= $pluginVersion) { if ($pluginData->currentVersion->version <= $pluginVersion) {
return false; return false;
@ -441,26 +454,29 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener
/** /**
* Check for updates * Check for updates
* *
* @return mixed * @return mixed
*/ */
public function getPluginsUpdates() { public function getPluginsUpdates() {
$pluginUpdates = array(); $pluginUpdates = array();
$pluginsWS = array(); $pluginsWS = array();
$url = ManiaControl::URL_WEBSERVICE . 'plugins'; $url = ManiaControl::URL_WEBSERVICE . 'plugins';
$dataJson = FileUtil::loadFile($url); $dataJson = FileUtil::loadFile($url);
$pluginVersions = json_decode($dataJson); $pluginVersions = json_decode($dataJson);
if (!$pluginVersions || !isset($pluginVersions[0])) { if (!$pluginVersions || !isset($pluginVersions[0])) {
return false; return false;
} }
foreach($pluginVersions as $plugin) { foreach ($pluginVersions as $plugin) {
$pluginsWS[$plugin->id] = $plugin; $pluginsWS[$plugin->id] = $plugin;
} }
/** @var Plugin $pluginClass */ /**
foreach($this->maniaControl->pluginManager->getPluginClasses() as $pluginClass) { *
* @var Plugin $pluginClass
*/
foreach ($this->maniaControl->pluginManager->getPluginClasses() as $pluginClass) {
$pluginId = $pluginClass::getId(); $pluginId = $pluginClass::getId();
if (array_key_exists($pluginId, $pluginsWS)) { if (array_key_exists($pluginId, $pluginsWS)) {
if ($pluginsWS[$pluginId]->currentVersion->version > $pluginClass::getVersion()) { if ($pluginsWS[$pluginId]->currentVersion->version > $pluginClass::getVersion()) {
@ -468,7 +484,7 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener
} }
} }
} }
if (empty($pluginUpdates)) { if (empty($pluginUpdates)) {
return false; return false;
} }
@ -477,14 +493,14 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener
/** /**
* Update pluginfile * Update pluginfile
* *
* @param $pluginData * @param $pluginData
* @param Player $player * @param Player $player
* @param bool $reopen * @param bool $reopen
*/ */
private function updatePlugin($pluginData, Player $player = null, $reopen = false) { private function updatePlugin($pluginData, Player $player = null, $reopen = false) {
$self = $this; $self = $this;
$this->maniaControl->fileReader->loadFile($pluginData->currentVersion->url, function ($updateFileContent, $error) use (&$self, &$updateData, &$player, &$pluginData, &$reopen) { $this->maniaControl->fileReader->loadFile($pluginData->currentVersion->url, function ($updateFileContent, $error) use(&$self, &$updateData, &$player, &$pluginData, &$reopen) {
$self->maniaControl->log('[UPDATE] Now updating ' . $pluginData->name . ' ...'); $self->maniaControl->log('[UPDATE] Now updating ' . $pluginData->name . ' ...');
if ($player) { if ($player) {
$self->maniaControl->chat->sendInformation('Now updating ' . $pluginData->name . ' ...', $player->login); $self->maniaControl->chat->sendInformation('Now updating ' . $pluginData->name . ' ...', $player->login);
@ -494,7 +510,7 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener
mkdir($tempDir); mkdir($tempDir);
} }
$updateFileName = $tempDir . $pluginData->currentVersion->zipfile; $updateFileName = $tempDir . $pluginData->currentVersion->zipfile;
$bytes = file_put_contents($updateFileName, $updateFileContent); $bytes = file_put_contents($updateFileName, $updateFileContent);
if (!$bytes || $bytes <= 0) { if (!$bytes || $bytes <= 0) {
trigger_error("Couldn't save plugin Zip."); trigger_error("Couldn't save plugin Zip.");
@ -503,7 +519,7 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener
} }
return; return;
} }
$zip = new \ZipArchive(); $zip = new \ZipArchive();
$result = $zip->open($updateFileName); $result = $zip->open($updateFileName);
if ($result !== true) { if ($result !== true) {
trigger_error("Couldn't open plugin Zip. ({$result})"); trigger_error("Couldn't open plugin Zip. ({$result})");
@ -512,18 +528,18 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener
} }
return; return;
} }
$zip->extractTo(ManiaControlDir . '/plugins'); $zip->extractTo(ManiaControlDir . '/plugins');
$zip->close(); $zip->close();
unlink($updateFileName); unlink($updateFileName);
@rmdir($tempDir); @rmdir($tempDir);
$self->maniaControl->log('[UPDATE] Successfully updated ' . $pluginData->name . '!'); $self->maniaControl->log('[UPDATE] Successfully updated ' . $pluginData->name . '!');
if ($player) { if ($player) {
$self->maniaControl->chat->sendSuccess('Successfully updated ' . $pluginData->name . '!', $player->login); $self->maniaControl->chat->sendSuccess('Successfully updated ' . $pluginData->name . '!', $player->login);
$self->maniaControl->pluginManager->deactivatePlugin($pluginData->pluginClass); $self->maniaControl->pluginManager->deactivatePlugin($pluginData->pluginClass);
$self->maniaControl->pluginManager->activatePlugin($pluginData->pluginClass); $self->maniaControl->pluginManager->activatePlugin($pluginData->pluginClass);
if ($reopen) { if ($reopen) {
$menuId = $self->maniaControl->configurator->getMenuId('Plugins'); $menuId = $self->maniaControl->configurator->getMenuId('Plugins');
$self->maniaControl->configurator->reopenMenu($player, $menuId); $self->maniaControl->configurator->reopenMenu($player, $menuId);
@ -534,52 +550,52 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener
/** /**
* Install pluginfile * Install pluginfile
* *
* @param $pluginData * @param $pluginData
* @param Player $player * @param Player $player
* @param bool $reopen * @param bool $reopen
*/ */
private function installPlugin($pluginData, Player $player, $reopen = false) { private function installPlugin($pluginData, Player $player, $reopen = false) {
$self = $this; $self = $this;
$this->maniaControl->fileReader->loadFile($pluginData->currentVersion->url, function ($installFileContent, $error) use (&$self, &$updateData, &$player, &$pluginData, &$reopen) { $this->maniaControl->fileReader->loadFile($pluginData->currentVersion->url, function ($installFileContent, $error) use(&$self, &$updateData, &$player, &$pluginData, &$reopen) {
$pluginsDirectory = ManiaControlDir . '/plugins/'; $pluginsDirectory = ManiaControlDir . '/plugins/';
$pluginFiles = scandir($pluginsDirectory); $pluginFiles = scandir($pluginsDirectory);
$self->maniaControl->log('[UPDATE] Now installing ' . $pluginData->name . ' ...'); $self->maniaControl->log('[UPDATE] Now installing ' . $pluginData->name . ' ...');
$self->maniaControl->chat->sendInformation('Now installing ' . $pluginData->name . ' ...', $player->login); $self->maniaControl->chat->sendInformation('Now installing ' . $pluginData->name . ' ...', $player->login);
$tempDir = ManiaControlDir . '/temp/'; $tempDir = ManiaControlDir . '/temp/';
if (!is_dir($tempDir)) { if (!is_dir($tempDir)) {
mkdir($tempDir); mkdir($tempDir);
} }
$installFileName = $tempDir . $pluginData->currentVersion->zipfile; $installFileName = $tempDir . $pluginData->currentVersion->zipfile;
$bytes = file_put_contents($installFileName, $installFileContent); $bytes = file_put_contents($installFileName, $installFileContent);
if (!$bytes || $bytes <= 0) { if (!$bytes || $bytes <= 0) {
trigger_error("Couldn't save plugin Zip."); trigger_error("Couldn't save plugin Zip.");
$self->maniaControl->chat->sendError("Install failed: Couldn't save plugin zip!", $player->login); $self->maniaControl->chat->sendError("Install failed: Couldn't save plugin zip!", $player->login);
return; return;
} }
$zip = new \ZipArchive(); $zip = new \ZipArchive();
$result = $zip->open($installFileName); $result = $zip->open($installFileName);
if ($result !== true) { if ($result !== true) {
trigger_error("Couldn't open plugin Zip. ({$result})"); trigger_error("Couldn't open plugin Zip. ({$result})");
$self->maniaControl->chat->sendError("Install failed: Couldn't open plugin zip!", $player->login); $self->maniaControl->chat->sendError("Install failed: Couldn't open plugin zip!", $player->login);
return; return;
} }
$zip->extractTo(ManiaControlDir . '/plugins'); $zip->extractTo(ManiaControlDir . '/plugins');
$zip->close(); $zip->close();
unlink($installFileName); unlink($installFileName);
@rmdir($tempDir); @rmdir($tempDir);
$pluginFilesAfter = scandir($pluginsDirectory); $pluginFilesAfter = scandir($pluginsDirectory);
$newFiles = array_diff($pluginFilesAfter, $pluginFiles); $newFiles = array_diff($pluginFilesAfter, $pluginFiles);
$classesBefore = get_declared_classes(); $classesBefore = get_declared_classes();
foreach($newFiles as $newFile) { foreach ($newFiles as $newFile) {
$filePath = $pluginsDirectory . $newFile; $filePath = $pluginsDirectory . $newFile;
if (is_file($filePath)) { if (is_file($filePath)) {
$success = include_once $filePath; $success = include_once $filePath;
if (!$success) { if (!$success) {
@ -587,7 +603,7 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener
} }
continue; continue;
} }
$dirPath = $pluginsDirectory . $newFile; $dirPath = $pluginsDirectory . $newFile;
if (is_dir($dirPath)) { if (is_dir($dirPath)) {
$self->maniaControl->pluginManager->loadPluginFiles($dirPath); $self->maniaControl->pluginManager->loadPluginFiles($dirPath);
@ -595,17 +611,20 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener
} }
} }
$classesAfter = get_declared_classes(); $classesAfter = get_declared_classes();
$newClasses = array_diff($classesAfter, $classesBefore); $newClasses = array_diff($classesAfter, $classesBefore);
foreach($newClasses as $className) { foreach ($newClasses as $className) {
if (!$self->maniaControl->pluginManager->isPluginClass($className)) { if (!$self->maniaControl->pluginManager->isPluginClass($className)) {
continue; continue;
} }
/** @var Plugin $className */ /**
*
* @var Plugin $className
*/
$self->maniaControl->pluginManager->addPluginClass($className); $self->maniaControl->pluginManager->addPluginClass($className);
$className::prepare($self->maniaControl); $className::prepare($self->maniaControl);
if ($self->maniaControl->pluginManager->isPluginActive($className)) { if ($self->maniaControl->pluginManager->isPluginActive($className)) {
continue; continue;
} }
@ -613,10 +632,10 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener
continue; continue;
} }
} }
$self->maniaControl->log('[UPDATE] Successfully installed ' . $pluginData->name . '!'); $self->maniaControl->log('[UPDATE] Successfully installed ' . $pluginData->name . '!');
$self->maniaControl->chat->sendSuccess('Successfully installed ' . $pluginData->name . '!', $player->login); $self->maniaControl->chat->sendSuccess('Successfully installed ' . $pluginData->name . '!', $player->login);
if ($reopen) { if ($reopen) {
$menuId = $self->maniaControl->configurator->getMenuId('Install Plugins'); $menuId = $self->maniaControl->configurator->getMenuId('Install Plugins');
$self->maniaControl->configurator->reopenMenu($player, $menuId); $self->maniaControl->configurator->reopenMenu($player, $menuId);
@ -626,7 +645,7 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener
/** /**
* Set Core Update Data * Set Core Update Data
* *
* @param UpdateData $coreUpdateData * @param UpdateData $coreUpdateData
*/ */
public function setCoreUpdateData(UpdateData $coreUpdateData = null) { public function setCoreUpdateData(UpdateData $coreUpdateData = null) {
@ -635,31 +654,32 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener
/** /**
* Checks a core update Asynchronously * Checks a core update Asynchronously
* *
* @param $function * @param $function
* @param bool $ignoreVersion * @param bool $ignoreVersion
*/ */
private function checkCoreUpdateAsync($function, $ignoreVersion = false) { private function checkCoreUpdateAsync($function, $ignoreVersion = false) {
$updateChannel = $this->getCurrentUpdateChannelSetting(); $updateChannel = $this->getCurrentUpdateChannelSetting();
$url = ManiaControl::URL_WEBSERVICE . 'versions?update=1&current=1&channel=' . $updateChannel; $url = ManiaControl::URL_WEBSERVICE . 'versions?current=1&channel=' . $updateChannel;
$this->maniaControl->fileReader->loadFile($url, function ($dataJson, $error) use (&$function, $ignoreVersion) { $this->maniaControl->fileReader->loadFile($url, function ($dataJson, $error) use(&$function, $ignoreVersion) {
$versions = json_decode($dataJson); $versions = json_decode($dataJson);
if (!$versions || !isset($versions[0])) { if (!$versions || !isset($versions[0])) {
return; return;
} }
$updateData = new UpdateData($versions[0]); $updateData = new UpdateData($versions[0]);
if (!$ignoreVersion && $updateData->version <= ManiaControl::VERSION) { if (!$ignoreVersion && $updateData->version <= ManiaControl::VERSION) {
return; call_user_func($function, null);
}
else {
call_user_func($function, $updateData);
} }
call_user_func($function, $updateData);
}); });
} }
/** /**
* Perform a Backup of ManiaControl * Perform a Backup of ManiaControl
* *
* @return bool * @return bool
*/ */
private function performBackup() { private function performBackup() {
@ -668,15 +688,15 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener
mkdir($backupFolder); mkdir($backupFolder);
} }
$backupFileName = $backupFolder . 'backup_' . ManiaControl::VERSION . '_' . date('y-m-d') . '_' . time() . '.zip'; $backupFileName = $backupFolder . 'backup_' . ManiaControl::VERSION . '_' . date('y-m-d') . '_' . time() . '.zip';
$backupZip = new \ZipArchive(); $backupZip = new \ZipArchive();
if ($backupZip->open($backupFileName, \ZipArchive::CREATE) !== TRUE) { if ($backupZip->open($backupFileName, \ZipArchive::CREATE) !== TRUE) {
trigger_error("Couldn't create Backup Zip!"); trigger_error("Couldn't create Backup Zip!");
return false; return false;
} }
$excludes = array('.', '..', 'backup', 'logs', 'ManiaControl.log'); $excludes = array('.', '..', 'backup', 'logs', 'ManiaControl.log');
$pathInfo = pathInfo(ManiaControlDir); $pathInfo = pathInfo(ManiaControlDir);
$parentPath = $pathInfo['dirname'] . '/'; $parentPath = $pathInfo['dirname'] . '/';
$dirName = $pathInfo['basename']; $dirName = $pathInfo['basename'];
$backupZip->addEmptyDir($dirName); $backupZip->addEmptyDir($dirName);
$this->zipDirectory($backupZip, ManiaControlDir, strlen($parentPath), $excludes); $this->zipDirectory($backupZip, ManiaControlDir, strlen($parentPath), $excludes);
$backupZip->close(); $backupZip->close();
@ -685,7 +705,7 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener
/** /**
* Perform a Backup of the plugins * Perform a Backup of the plugins
* *
* @return bool * @return bool
*/ */
private function performPluginsBackup() { private function performPluginsBackup() {
@ -694,15 +714,15 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener
mkdir($backupFolder); mkdir($backupFolder);
} }
$backupFileName = $backupFolder . 'backup_plugins_' . date('y-m-d') . '_' . time() . '.zip'; $backupFileName = $backupFolder . 'backup_plugins_' . date('y-m-d') . '_' . time() . '.zip';
$backupZip = new \ZipArchive(); $backupZip = new \ZipArchive();
if ($backupZip->open($backupFileName, \ZipArchive::CREATE) !== TRUE) { if ($backupZip->open($backupFileName, \ZipArchive::CREATE) !== TRUE) {
trigger_error("Couldn't create Backup Zip!"); trigger_error("Couldn't create Backup Zip!");
return false; return false;
} }
$excludes = array('.', '..'); $excludes = array('.', '..');
$pathInfo = pathInfo(ManiaControlDir . '/plugins'); $pathInfo = pathInfo(ManiaControlDir . '/plugins');
$parentPath = $pathInfo['dirname'] . '/'; $parentPath = $pathInfo['dirname'] . '/';
$dirName = $pathInfo['basename']; $dirName = $pathInfo['basename'];
$backupZip->addEmptyDir($dirName); $backupZip->addEmptyDir($dirName);
$this->zipDirectory($backupZip, ManiaControlDir . '/plugins', strlen($parentPath), $excludes); $this->zipDirectory($backupZip, ManiaControlDir . '/plugins', strlen($parentPath), $excludes);
$backupZip->close(); $backupZip->close();
@ -711,11 +731,11 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener
/** /**
* Add a complete Directory to the ZipArchive * Add a complete Directory to the ZipArchive
* *
* @param \ZipArchive $zipArchive * @param \ZipArchive $zipArchive
* @param string $folderName * @param string $folderName
* @param int $prefixLength * @param int $prefixLength
* @param array $excludes * @param array $excludes
* @return bool * @return bool
*/ */
private function zipDirectory(\ZipArchive &$zipArchive, $folderName, $prefixLength, array $excludes = array()) { private function zipDirectory(\ZipArchive &$zipArchive, $folderName, $prefixLength, array $excludes = array()) {
@ -724,11 +744,11 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener
trigger_error("Couldn't open Folder '{$folderName}' for Backup!"); trigger_error("Couldn't open Folder '{$folderName}' for Backup!");
return false; return false;
} }
while(false !== ($file = readdir($folderHandle))) { while (false !== ($file = readdir($folderHandle))) {
if (in_array($file, $excludes)) { if (in_array($file, $excludes)) {
continue; continue;
} }
$filePath = $folderName . '/' . $file; $filePath = $folderName . '/' . $file;
$localPath = substr($filePath, $prefixLength); $localPath = substr($filePath, $prefixLength);
if (is_file($filePath)) { if (is_file($filePath)) {
$zipArchive->addFile($filePath, $localPath); $zipArchive->addFile($filePath, $localPath);
@ -746,9 +766,9 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener
/** /**
* Perform a Core Update * Perform a Core Update
* *
* @param object $updateData * @param object $updateData
* @param $player * @param $player
* @return bool * @return bool
*/ */
private function performCoreUpdate(UpdateData $updateData, Player $player = null) { private function performCoreUpdate(UpdateData $updateData, Player $player = null) {
@ -759,7 +779,7 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener
$this->maniaControl->log('Update failed!'); $this->maniaControl->log('Update failed!');
return false; return false;
} }
if (!isset($updateData->url) && !isset($updateData->releaseDate)) { if (!isset($updateData->url) && !isset($updateData->releaseDate)) {
if ($player) { if ($player) {
$this->maniaControl->chat->sendError('Update failed: No update Data available!', $player->login); $this->maniaControl->chat->sendError('Update failed: No update Data available!', $player->login);
@ -767,15 +787,15 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener
$this->maniaControl->log('Update failed: No update Data available!'); $this->maniaControl->log('Update failed: No update Data available!');
return false; return false;
} }
$self = $this; $self = $this;
$this->maniaControl->fileReader->loadFile($updateData->url, function ($updateFileContent, $error) use (&$self, &$updateData, &$player) { $this->maniaControl->fileReader->loadFile($updateData->url, function ($updateFileContent, $error) use(&$self, &$updateData, &$player) {
$tempDir = ManiaControlDir . '/temp/'; $tempDir = ManiaControlDir . '/temp/';
if (!is_dir($tempDir)) { if (!is_dir($tempDir)) {
mkdir($tempDir); mkdir($tempDir);
} }
$updateFileName = $tempDir . basename($updateData->url); $updateFileName = $tempDir . basename($updateData->url);
$bytes = file_put_contents($updateFileName, $updateFileContent); $bytes = file_put_contents($updateFileName, $updateFileContent);
if (!$bytes || $bytes <= 0) { if (!$bytes || $bytes <= 0) {
trigger_error("Couldn't save Update Zip."); trigger_error("Couldn't save Update Zip.");
@ -784,7 +804,7 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener
} }
return false; return false;
} }
$zip = new \ZipArchive(); $zip = new \ZipArchive();
$result = $zip->open($updateFileName); $result = $zip->open($updateFileName);
if ($result !== true) { if ($result !== true) {
trigger_error("Couldn't open Update Zip. ({$result})"); trigger_error("Couldn't open Update Zip. ({$result})");
@ -793,40 +813,40 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener
} }
return false; return false;
} }
$zip->extractTo(ManiaControlDir); $zip->extractTo(ManiaControlDir);
$zip->close(); $zip->close();
unlink($updateFileName); unlink($updateFileName);
@rmdir($tempDir); @rmdir($tempDir);
//Set the Nightly Build Date // Set the Nightly Build Date
$self->setNightlyBuildDate($updateData->releaseDate); $self->setNightlyBuildDate($updateData->releaseDate);
if ($player) { if ($player) {
$self->maniaControl->chat->sendSuccess('Update finished!', $player->login); $self->maniaControl->chat->sendSuccess('Update finished!', $player->login);
} }
$self->maniaControl->log("Update finished!"); $self->maniaControl->log("Update finished!");
$self->maniaControl->restart(); $self->maniaControl->restart();
return true; return true;
}); });
return true; return true;
} }
/** /**
* Function checks if ManiaControl has sufficient access to files to update them. * Function checks if ManiaControl has sufficient access to files to update them.
* *
* @return bool * @return bool
*/ */
private function checkPermissions() { private function checkPermissions() {
$writableDirectories = array('/core/', '/plugins/'); $writableDirectories = array('/core/', '/plugins/');
$path = ManiaControlDir; $path = ManiaControlDir;
foreach($writableDirectories as $writableDirecotry) { foreach ($writableDirectories as $writableDirecotry) {
$dir = new \RecursiveDirectoryIterator($path . $writableDirecotry); $dir = new \RecursiveDirectoryIterator($path . $writableDirecotry);
foreach(new \RecursiveIteratorIterator($dir) as $filename => $file) { foreach (new \RecursiveIteratorIterator($dir) as $filename => $file) {
if (substr($filename, -1) != '.' && substr($filename, -2) != '..') { if (substr($filename, -1) != '.' && substr($filename, -2) != '..') {
if (!is_writable($filename)) { if (!is_writable($filename)) {
$this->maniaControl->log('Cannot update: the file/directory "' . $filename . '" is not writable!'); $this->maniaControl->log('Cannot update: the file/directory "' . $filename . '" is not writable!');
@ -840,7 +860,7 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener
/** /**
* Retrieve the Update Channel Setting * Retrieve the Update Channel Setting
* *
* @return string * @return string
*/ */
private function getCurrentUpdateChannelSetting() { private function getCurrentUpdateChannelSetting() {