improved updatemanager

This commit is contained in:
kremsy 2014-02-10 22:54:49 +01:00 committed by Steffen Schröder
parent 8500d86d96
commit be2ce96cac
5 changed files with 71 additions and 72 deletions

View File

@ -3,8 +3,7 @@
namespace ManiaControl; namespace ManiaControl;
use ManiaControl\Files\FileUtil; use ManiaControl\Files\FileUtil;
use ManiaControl\ManiaControl; use ManiaControl\Update\UpdateManager;
use ManiaControl\UpdateManager;
/** /**
* Error and Exception Manager Class * Error and Exception Manager Class
@ -30,7 +29,7 @@ class ErrorHandler {
* ManiaControl ExceptionHandler * ManiaControl ExceptionHandler
* ManiaControl Shuts down after exception * ManiaControl Shuts down after exception
* *
* @param Exception $ex * @param \Exception $ex
*/ */
public function exceptionHandler(\Exception $ex) { public function exceptionHandler(\Exception $ex) {
$message = "[ManiaControl EXCEPTION]: {$ex->getMessage()} Trace: {$ex->getTraceAsString()}!"; $message = "[ManiaControl EXCEPTION]: {$ex->getMessage()} Trace: {$ex->getTraceAsString()}!";

View File

@ -9,7 +9,6 @@ use ManiaControl\Callbacks\TimerManager;
use ManiaControl\Commands\CommandListener; use ManiaControl\Commands\CommandListener;
use ManiaControl\Commands\CommandManager; use ManiaControl\Commands\CommandManager;
use ManiaControl\Configurators\Configurator; use ManiaControl\Configurators\Configurator;
use ManiaControl\ErrorHandler;
use ManiaControl\Files\AsynchronousFileReader; use ManiaControl\Files\AsynchronousFileReader;
use ManiaControl\Files\FileUtil; use ManiaControl\Files\FileUtil;
use ManiaControl\Manialinks\ManialinkManager; use ManiaControl\Manialinks\ManialinkManager;
@ -20,6 +19,7 @@ use ManiaControl\Plugins\PluginManager;
use ManiaControl\Server\Server; use ManiaControl\Server\Server;
use ManiaControl\Settings\SettingManager; use ManiaControl\Settings\SettingManager;
use ManiaControl\Statistics\StatisticManager; use ManiaControl\Statistics\StatisticManager;
use ManiaControl\Update\UpdateManager;
use Maniaplanet\DedicatedServer\Connection; use Maniaplanet\DedicatedServer\Connection;
require_once __DIR__ . '/Maniaplanet/DedicatedServer/Connection.php'; require_once __DIR__ . '/Maniaplanet/DedicatedServer/Connection.php';

View File

@ -5,7 +5,7 @@ namespace ManiaControl\Server;
use ManiaControl\Callbacks\TimerListener; use ManiaControl\Callbacks\TimerListener;
use ManiaControl\Formatter; use ManiaControl\Formatter;
use ManiaControl\ManiaControl; use ManiaControl\ManiaControl;
use ManiaControl\UpdateManager; use ManiaControl\Update\UpdateManager;
/** /**
* Class reports Usage * Class reports Usage

View File

@ -0,0 +1,22 @@
<?php
/**
* UpdateStructure
*
* @author steeffeen & kremsy
*/
namespace ManiaControl\Update;
class UpdateData {
public $version = 0;
public $channel = "";
public $url = "";
public $releaseDate = "";
public function __construct($updateData) {
$this->version = $updateData->version;
$this->channel = $updateData->channel;
$this->url = $updateData->url;
$this->releaseDate = $updateData->release_date;
}
}

View File

@ -1,12 +1,13 @@
<?php <?php
namespace ManiaControl; namespace ManiaControl\Update;
use ManiaControl\Admin\AuthenticationManager; use ManiaControl\Admin\AuthenticationManager;
use ManiaControl\Callbacks\CallbackListener; use ManiaControl\Callbacks\CallbackListener;
use ManiaControl\Callbacks\TimerListener; use ManiaControl\Callbacks\TimerListener;
use ManiaControl\Commands\CommandListener; use ManiaControl\Commands\CommandListener;
use ManiaControl\Files\FileUtil; use ManiaControl\Files\FileUtil;
use ManiaControl\ManiaControl;
use ManiaControl\Players\Player; use ManiaControl\Players\Player;
use ManiaControl\Players\PlayerManager; use ManiaControl\Players\PlayerManager;
use ManiaControl\Plugins\Plugin; use ManiaControl\Plugins\Plugin;
@ -36,6 +37,7 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener
* Private Properties * Private Properties
*/ */
private $maniaControl = null; private $maniaControl = null;
/** @var UpdateData $coreUpdateData */
private $coreUpdateData = null; private $coreUpdateData = null;
private $currentBuildDate = ""; private $currentBuildDate = "";
@ -87,15 +89,15 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener
} }
//Check if a new Core Update is Available //Check if a new Core Update is Available
$this->checkCoreUpdateAsync(function ($updateData) use ($time) { $this->checkCoreUpdateAsync(function (UpdateData $updateData) use ($time) {
$buildDate = strtotime($this->currentBuildDate); $buildDate = strtotime($this->currentBuildDate);
$releaseTime = strtotime($updateData->release_date); $releaseTime = strtotime($updateData->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->log('New ManiaControl Version ' . $updateData->version . ' available!'); $this->maniaControl->log('New ManiaControl Version ' . $updateData->version . ' available!');
} else { } else {
$this->maniaControl->log('New Nightly Build (' . $updateData->release_date . ') available!'); $this->maniaControl->log('New Nightly Build (' . $updateData->releaseDate . ') available!');
} }
$this->coreUpdateData = $updateData; $this->coreUpdateData = $updateData;
$this->autoUpdate($time); $this->autoUpdate($time);
@ -119,18 +121,17 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener
} }
$buildDate = strtotime($this->currentBuildDate); $buildDate = strtotime($this->currentBuildDate);
$releaseTime = strtotime($this->coreUpdateData->release_date); $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->release_date . ') available!', $player->login); $this->maniaControl->chat->sendSuccess('New Nightly Build (' . $this->coreUpdateData->releaseDate . ') available!', $player->login);
} }
} }
} }
/** /**
* 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)
* *
@ -151,7 +152,7 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener
} }
$buildDate = strtotime($this->currentBuildDate); $buildDate = strtotime($this->currentBuildDate);
$releaseTime = strtotime($this->coreUpdateData->release_date); $releaseTime = strtotime($this->coreUpdateData->releaseDate);
if ($buildDate && $buildDate >= $releaseTime) { if ($buildDate && $buildDate >= $releaseTime) {
return; return;
} }
@ -179,7 +180,7 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener
$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 // Check update and send result message
$this->checkCoreUpdateAsync(function ($updateData) use (&$player) { $this->checkCoreUpdateAsync(function (UpdateData $updateData) use (&$player) {
if (!$updateData) { if (!$updateData) {
$this->maniaControl->chat->sendInformation('No Update available!', $player->login); $this->maniaControl->chat->sendInformation('No Update available!', $player->login);
return; return;
@ -188,22 +189,22 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener
}); });
} else { } else {
// Special nightly channel updating // Special nightly channel updating
$this->checkCoreUpdateAsync(function ($updateData) use (&$player) { $this->checkCoreUpdateAsync(function (UpdateData $updateData) use (&$player) {
if (!$updateData) { if (!$updateData) {
$this->maniaControl->chat->sendInformation('No Update available!', $player->login); $this->maniaControl->chat->sendInformation('No Update available!', $player->login);
return; return;
} }
$buildTime = strtotime($this->currentBuildDate); $buildTime = strtotime($this->currentBuildDate);
$releaseTime = strtotime($updateData->release_date); $releaseTime = strtotime($updateData->releaseDate);
if ($buildTime != '') { if ($buildTime != '') {
if ($buildTime >= $releaseTime) { if ($buildTime >= $releaseTime) {
$this->maniaControl->chat->sendInformation('No new Build available, current build: ' . date("Y-m-d", $buildTime) . '!', $player->login); $this->maniaControl->chat->sendInformation('No new Build available, current build: ' . date("Y-m-d", $buildTime) . '!', $player->login);
return; return;
} }
$this->maniaControl->chat->sendSuccess('New Nightly Build (' . $updateData->release_date . ') available, current build: ' . $this->currentBuildDate . '!', $player->login); $this->maniaControl->chat->sendSuccess('New Nightly Build (' . $updateData->releaseDate . ') available, current build: ' . $this->currentBuildDate . '!', $player->login);
} else { } else {
$this->maniaControl->chat->sendSuccess('New Nightly Build (' . $updateData->release_date . ') available!', $player->login); $this->maniaControl->chat->sendSuccess('New Nightly Build (' . $updateData->releaseDate . ') available!', $player->login);
} }
}, true); }, true);
} }
@ -247,7 +248,8 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener
$this->maniaControl->authenticationManager->sendNotAllowed($player); $this->maniaControl->authenticationManager->sendNotAllowed($player);
return; return;
} }
$updateData = $this->checkCoreUpdate(true);
$this->checkCoreUpdateAsync(function ($updateData) use (&$player) {
if (!$updateData) { if (!$updateData) {
$this->maniaControl->chat->sendError('Update is currently not possible!', $player->login); $this->maniaControl->chat->sendError('Update is currently not possible!', $player->login);
return; return;
@ -262,6 +264,7 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener
} }
$this->performCoreUpdate($updateData, $player); $this->performCoreUpdate($updateData, $player);
}, true);
} }
/** /**
@ -292,7 +295,7 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener
/** /**
* Checks a core update Asynchrnously * Checks a core update Asynchronously
* *
* @param $function * @param $function
* @param bool $ignoreVersion * @param bool $ignoreVersion
@ -306,7 +309,7 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener
if (!$versions || !isset($versions[0])) { if (!$versions || !isset($versions[0])) {
return; return;
} }
$updateData = $versions[0]; $updateData = new UpdateData($versions[0]);
if (!$ignoreVersion && $updateData->version <= ManiaControl::VERSION) { if (!$ignoreVersion && $updateData->version <= ManiaControl::VERSION) {
return; return;
} }
@ -315,26 +318,6 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener
}); });
} }
/**
* Check for Update of ManiaControl
*
* @return mixed
*/
private function checkCoreUpdate($ignoreVersion = false) {
$updateChannel = $this->getCurrentUpdateChannelSetting();
$url = self::URL_WEBSERVICE . 'versions?update=1&current=1&channel=' . $updateChannel;
$dataJson = FileUtil::loadFile($url);
$versions = json_decode($dataJson);
if (!$versions || !isset($versions[0])) {
return false;
}
$updateData = $versions[0];
if (!$ignoreVersion && $updateData->version <= ManiaControl::VERSION) {
return false;
}
return $updateData;
}
/** /**
* Perform a Backup of ManiaControl * Perform a Backup of ManiaControl
* *
@ -400,10 +383,10 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener
* Perform a Core Update * Perform a Core Update
* *
* @param object $updateData * @param object $updateData
* @param Players\Player $player * @param $player
* @return bool * @return bool
*/ */
private function performCoreUpdate($updateData, Player $player = null) { private function performCoreUpdate(UpdateData $updateData, Player $player = null) {
if (!$this->checkPermissions()) { if (!$this->checkPermissions()) {
if ($player != null) { if ($player != null) {
$this->maniaControl->chat->sendError('Update failed: Incorrect Filesystem permissions!', $player->login); $this->maniaControl->chat->sendError('Update failed: Incorrect Filesystem permissions!', $player->login);
@ -411,8 +394,9 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener
$this->maniaControl->log('Update failed!'); $this->maniaControl->log('Update failed!');
return false; return false;
} }
var_dump("test");
if (!isset($updateData->url) && !isset($updateData->release_date)) { if (!isset($updateData->url) && !isset($updateData->releaseDate)) {
if ($player != null) { if ($player != null) {
$this->maniaControl->chat->sendError('Update failed: No update Data available!', $player->login); $this->maniaControl->chat->sendError('Update failed: No update Data available!', $player->login);
} }
@ -451,7 +435,7 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener
@rmdir($tempDir); @rmdir($tempDir);
//Set the Nightly Build Date //Set the Nightly Build Date
$this->setNightlyBuildDate($updateData->release_date); $this->setNightlyBuildDate($updateData->releaseDate);
if ($player != null) { if ($player != null) {
$this->maniaControl->chat->sendSuccess('Update finished!', $player->login); $this->maniaControl->chat->sendSuccess('Update finished!', $player->login);
@ -472,26 +456,20 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener
* @return bool * @return bool
*/ */
private function checkPermissions() { private function checkPermissions() {
$writableDirectories = array('core/', 'plugins/'); $writableDirectories = array('/core/', '/plugins/');
$path = str_replace('core', '', realpath(dirname(__FILE__))); $path = ManiaControlDir;
foreach($writableDirectories as $writableDirecotry) { foreach($writableDirectories as $writableDirecotry) {
$files = array(); $dir = new \RecursiveDirectoryIterator($path . $writableDirecotry);
$di = new \RecursiveDirectoryIterator($path . $writableDirecotry); foreach(new \RecursiveIteratorIterator($dir) as $filename => $file) {
foreach(new \RecursiveIteratorIterator($di) as $filename => $file) { if (substr($filename, -1) != '.' && substr($filename, -2) != '..') {
$files[] = $filename; if (!is_writable($filename)) {
} $this->maniaControl->log('Cannot update: the file/directory "' . $filename . '" is not writable!');
foreach($files as $file) {
if (substr($file, -1) != '.' && substr($file, -2) != '..') {
if (!is_writable($file)) {
$this->maniaControl->log('Cannot update: the file/directory "' . $file . '" is not writable!');
return false; return false;
} }
} }
} }
} }
return true; return true;
} }