autoupdate setting

This commit is contained in:
kremsy 2014-01-20 21:54:58 +01:00 committed by Steffen Schröder
parent a17037c3cb
commit 64946bf3a1

View File

@ -51,6 +51,7 @@ class UpdateManager implements CallbackListener, CommandListener {
// Register for callbacks
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MC_1_MINUTE, $this, 'handle1Minute');
$this->maniaControl->callbackManager->registerCallbackListener(PlayerManager::CB_PLAYERJOINED, $this, 'handlePlayerJoined');
$this->maniaControl->callbackManager->registerCallbackListener(PlayerManager::CB_PLAYERDISCONNECTED, $this, 'handlePlayerDisconnected');
// Register for chat commands
$this->maniaControl->commandManager->registerCommandListener('checkupdate', $this, 'handle_CheckUpdate', true);
@ -66,15 +67,21 @@ class UpdateManager implements CallbackListener, CommandListener {
$updateCheckEnabled = $this->maniaControl->settingManager->getSetting($this, self::SETTING_ENABLEUPDATECHECK);
if(!$updateCheckEnabled) {
// Automatic update check disabled
if ($this->coreUpdateData) $this->coreUpdateData = null;
if($this->coreUpdateData) {
$this->coreUpdateData = null;
}
return;
}
// Only check once per hour
$updateInterval = $this->maniaControl->settingManager->getSetting($this, self::SETTING_UPDATECHECK_INTERVAL);
if ($this->lastUpdateCheck > time() - $updateInterval * 3600.) return;
if($this->lastUpdateCheck > time() - $updateInterval * 3600.) {
return;
}
$this->lastUpdateCheck = time();
$updateData = $this->checkCoreUpdate();
if (!$updateData) return;
if(!$updateData) {
return;
}
$this->maniaControl->log('New ManiaControl Version ' . $updateData->version . ' available!');
$this->coreUpdateData = $updateData;
}
@ -85,13 +92,47 @@ class UpdateManager implements CallbackListener, CommandListener {
* @param array $callback
*/
public function handlePlayerJoined(array $callback) {
if (!$this->coreUpdateData) return;
if(!$this->coreUpdateData) {
return;
}
// Announce available update
$player = $callback[1];
if (!AuthenticationManager::checkRight($player, AuthenticationManager::AUTH_LEVEL_SUPERADMIN)) return;
if(!AuthenticationManager::checkRight($player, AuthenticationManager::AUTH_LEVEL_SUPERADMIN)) {
return;
}
$this->maniaControl->chat->sendInformation('New ManiaControl Version ' . $this->coreUpdateData->version . ' available!', $player->login);
}
/**
* Perform automatic update as soon as a the Server is empty
*
* @param array $callback
*/
public function handlePlayerDisconnected(array $callback) {
//TODO Setting for autoupdate
if(count($this->maniaControl->playerManager->getPlayers()) > 0) {
return;
}
$updateData = $this->checkCoreUpdate(true);
if(!$updateData) {
return;
}
$this->maniaControl->log("Starting Update to Version v{$updateData->version}...");
$performBackup = $this->maniaControl->settingManager->getSetting($this, self::SETTING_PERFORM_BACKUPS);
if($performBackup && !$this->performBackup()) {
$this->maniaControl->log("Creating Backup failed!");
}
if(!$this->performCoreUpdate($updateData)) {
$this->maniaControl->log("Update failed!");
return;
}
$this->maniaControl->log("Update finished!");
$this->maniaControl->restart();
}
/**
* Handle //checkupdate command
*
@ -112,8 +153,7 @@ class UpdateManager implements CallbackListener, CommandListener {
return;
}
$this->maniaControl->chat->sendSuccess('Update for Version ' . $updateData->version . ' available!', $player->login);
}
else {
} else {
// Special nightly channel updating
$updateData = $this->checkCoreUpdate(true);
$buildDate = $this->getNightlyBuildDate();
@ -136,7 +176,9 @@ class UpdateManager implements CallbackListener, CommandListener {
*/
private function getNightlyBuildDate() {
$nightlyBuildDateFile = ManiaControlDir . '/core/nightly_build.txt';
if (!file_exists($nightlyBuildDateFile)) return false;
if(!file_exists($nightlyBuildDateFile)) {
return false;
}
$fileContent = file_get_contents($nightlyBuildDateFile);
return $fileContent;
}
@ -148,7 +190,7 @@ class UpdateManager implements CallbackListener, CommandListener {
* @param Player $player
*/
public function handle_CoreUpdate(array $chatCallback, Player $player) {
if (!AuthenticationManager::checkRight($player, AuthenticationManager::AUTH_LEVEL_MASTERADMIN)) {
if(!AuthenticationManager::checkRight($player, AuthenticationManager::AUTH_LEVEL_ADMIN)) { //TODO, define permission setting
$this->maniaControl->authenticationManager->sendNotAllowed($player);
return;
}
@ -158,15 +200,19 @@ class UpdateManager implements CallbackListener, CommandListener {
return;
}
$this->maniaControl->chat->sendInformation("Starting Update to Version v{$updateData->version}...", $player->login);
$this->maniaControl->log("Starting Update to Version v{$updateData->version}...");
$performBackup = $this->maniaControl->settingManager->getSetting($this, self::SETTING_PERFORM_BACKUPS);
if($performBackup && !$this->performBackup()) {
$this->maniaControl->chat->sendError('Creating backup failed.', $player->login);
$this->maniaControl->log("Creating backup failed.");
}
if(!$this->performCoreUpdate($updateData)) {
$this->maniaControl->chat->sendError('Update failed!', $player->login);
return;
}
$this->maniaControl->chat->sendSuccess('Update finished!', $player->login);
$this->maniaControl->restart();
}
/**
@ -221,7 +267,9 @@ class UpdateManager implements CallbackListener, CommandListener {
*/
private function performBackup() {
$backupFolder = ManiaControlDir . '/backup/';
if (!is_dir($backupFolder)) mkdir($backupFolder);
if(!is_dir($backupFolder)) {
mkdir($backupFolder);
}
$backupFileName = $backupFolder . 'backup_' . ManiaControl::VERSION . '_' . date('y-m-d') . '_' . time() . '.zip';
$backupZip = new \ZipArchive();
if($backupZip->open($backupFileName, \ZipArchive::CREATE) !== TRUE) {
@ -254,7 +302,9 @@ class UpdateManager implements CallbackListener, CommandListener {
return false;
}
while(false !== ($file = readdir($folderHandle))) {
if (in_array($file, $excludes)) continue;
if(in_array($file, $excludes)) {
continue;
}
$filePath = $folderName . '/' . $file;
$localPath = substr($filePath, $prefixLength);
if(is_file($filePath)) {
@ -286,7 +336,9 @@ class UpdateManager implements CallbackListener, CommandListener {
}
$updateFileContent = file_get_contents($updateData->url);
$tempDir = ManiaControlDir . '/temp/';
if (!is_dir($tempDir)) mkdir($tempDir);
if(!is_dir($tempDir)) {
mkdir($tempDir);
}
$updateFileName = $tempDir . basename($updateData->url);
$bytes = file_put_contents($updateFileName, $updateFileContent);
if(!$bytes || $bytes <= 0) {