improved checkupdate command

This commit is contained in:
kremsy 2014-01-28 16:23:50 +01:00 committed by Steffen Schröder
parent 411dae55b4
commit d6a34bdeea

View File

@ -8,6 +8,7 @@ use ManiaControl\Callbacks\CallbackManager;
use ManiaControl\Commands\CommandListener; use ManiaControl\Commands\CommandListener;
use ManiaControl\Players\Player; use ManiaControl\Players\Player;
use ManiaControl\Players\PlayerManager; use ManiaControl\Players\PlayerManager;
use ManiaControl\Plugins\Plugin;
/** /**
* Manager checking for ManiaControl Core and Plugin Updates * Manager checking for ManiaControl Core and Plugin Updates
@ -71,21 +72,21 @@ class UpdateManager implements CallbackListener, CommandListener {
*/ */
public function handle1Minute(array $callback) { public function handle1Minute(array $callback) {
$updateCheckEnabled = $this->maniaControl->settingManager->getSetting($this, self::SETTING_ENABLEUPDATECHECK); $updateCheckEnabled = $this->maniaControl->settingManager->getSetting($this, self::SETTING_ENABLEUPDATECHECK);
if(!$updateCheckEnabled) { if (!$updateCheckEnabled) {
// Automatic update check disabled // Automatic update check disabled
if($this->coreUpdateData) { if ($this->coreUpdateData) {
$this->coreUpdateData = null; $this->coreUpdateData = null;
} }
return; return;
} }
// Only check once per hour // Only check once per hour
$updateInterval = $this->maniaControl->settingManager->getSetting($this, self::SETTING_UPDATECHECK_INTERVAL); $updateInterval = $this->maniaControl->settingManager->getSetting($this, self::SETTING_UPDATECHECK_INTERVAL);
if($this->lastUpdateCheck > time() - $updateInterval * 3600.) { if ($this->lastUpdateCheck > time() - $updateInterval * 3600.) {
return; return;
} }
$this->lastUpdateCheck = time(); $this->lastUpdateCheck = time();
$updateData = $this->checkCoreUpdate(); $updateData = $this->checkCoreUpdate();
if(!$updateData) { if (!$updateData) {
return; return;
} }
$this->maniaControl->log('New ManiaControl Version ' . $updateData->version . ' available!'); $this->maniaControl->log('New ManiaControl Version ' . $updateData->version . ' available!');
@ -98,12 +99,12 @@ class UpdateManager implements CallbackListener, CommandListener {
* @param array $callback * @param array $callback
*/ */
public function handlePlayerJoined(array $callback) { public function handlePlayerJoined(array $callback) {
if(!$this->coreUpdateData) { if (!$this->coreUpdateData) {
return; return;
} }
// Announce available update // Announce available update
$player = $callback[1]; $player = $callback[1];
if(!AuthenticationManager::checkRight($player, AuthenticationManager::AUTH_LEVEL_SUPERADMIN)) { if (!AuthenticationManager::checkRight($player, AuthenticationManager::AUTH_LEVEL_SUPERADMIN)) {
return; return;
} }
$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);
@ -116,26 +117,26 @@ class UpdateManager implements CallbackListener, CommandListener {
*/ */
public function handlePlayerDisconnected(array $callback) { public function handlePlayerDisconnected(array $callback) {
$performBackup = $this->maniaControl->settingManager->getSetting($this, self::SETTING_AUTO_UPDATE); $performBackup = $this->maniaControl->settingManager->getSetting($this, self::SETTING_AUTO_UPDATE);
if(!$performBackup) { if (!$performBackup) {
return; return;
} }
if(count($this->maniaControl->playerManager->getPlayers()) > 0) { if (count($this->maniaControl->playerManager->getPlayers()) > 0) {
return; return;
} }
$updateData = $this->checkCoreUpdate(true); $updateData = $this->checkCoreUpdate(true);
if(!$updateData) { if (!$updateData) {
return; return;
} }
$this->maniaControl->log("Starting Update to Version v{$updateData->version}..."); $this->maniaControl->log("Starting Update to Version v{$updateData->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()) {
$this->maniaControl->log("Creating Backup failed!"); $this->maniaControl->log("Creating Backup failed!");
} }
if(!$this->performCoreUpdate($updateData)) { if (!$this->performCoreUpdate($updateData)) {
$this->maniaControl->log("Update failed!"); $this->maniaControl->log("Update failed!");
return; return;
} }
@ -151,32 +152,34 @@ class UpdateManager implements CallbackListener, CommandListener {
* @param Player $player * @param Player $player
*/ */
public function handle_CheckUpdate(array $chatCallback, Player $player) { public function handle_CheckUpdate(array $chatCallback, Player $player) {
if(!AuthenticationManager::checkRight($player, AuthenticationManager::AUTH_LEVEL_SUPERADMIN)) { if (!AuthenticationManager::checkRight($player, AuthenticationManager::AUTH_LEVEL_SUPERADMIN)) {
$this->maniaControl->authenticationManager->sendNotAllowed($player); $this->maniaControl->authenticationManager->sendNotAllowed($player);
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 // Check update and send result message
$updateData = $this->checkCoreUpdate(); $updateData = $this->checkCoreUpdate();
if(!$updateData) { if (!$updateData) {
$this->maniaControl->chat->sendInformation('No Update available!', $player->login); $this->maniaControl->chat->sendInformation('No Update available!', $player->login);
return; return;
} }
$this->maniaControl->chat->sendSuccess('Update for Version ' . $updateData->version . ' available!', $player->login); $this->maniaControl->chat->sendSuccess('Update for Version ' . $updateData->version . ' available!', $player->login);
} else { } else {
// Special nightly channel updating // Special nightly channel updating
$updateData = $this->checkCoreUpdate(true); $updateData = $this->checkCoreUpdate(true);
$buildDate = $this->getNightlyBuildDate(); $buildDate = $this->getNightlyBuildDate();
if($buildDate) { $releaseTime = strtotime($updateData->release_date);
$buildTime = strtotime($buildDate); if ($buildDate) {
$releaseTime = strtotime($updateData->release_date); $buildTime = strtotime($buildDate);
if($buildTime >= $releaseTime) { if ($buildTime >= $releaseTime) {
$this->maniaControl->chat->sendInformation('No new Build available!', $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 (' . date("Y-m-d", $releaseTime) . ') available, current build: ' . date("Y-m-d", $buildTime) . '!', $player->login);
} else {
$this->maniaControl->chat->sendSuccess('New Nightly Build (' . date("Y-m-d", $releaseTime) . ') available!', $player->login);
} }
$this->maniaControl->chat->sendSuccess('New Nightly Build available!', $player->login);
} }
} }
@ -187,7 +190,7 @@ class UpdateManager implements CallbackListener, CommandListener {
*/ */
private function getNightlyBuildDate() { private function getNightlyBuildDate() {
$nightlyBuildDateFile = ManiaControlDir . '/core/nightly_build.txt'; $nightlyBuildDateFile = ManiaControlDir . '/core/nightly_build.txt';
if(!file_exists($nightlyBuildDateFile)) { if (!file_exists($nightlyBuildDateFile)) {
return false; return false;
} }
$fileContent = file_get_contents($nightlyBuildDateFile); $fileContent = file_get_contents($nightlyBuildDateFile);
@ -201,30 +204,30 @@ class UpdateManager implements CallbackListener, CommandListener {
* @param Player $player * @param Player $player
*/ */
public function handle_CoreUpdate(array $chatCallback, Player $player) { public function handle_CoreUpdate(array $chatCallback, Player $player) {
if(!$this->maniaControl->authenticationManager->checkPermission($player, self::SETTING_PERMISSION_UPDATE)) { if (!$this->maniaControl->authenticationManager->checkPermission($player, self::SETTING_PERMISSION_UPDATE)) {
$this->maniaControl->authenticationManager->sendNotAllowed($player); $this->maniaControl->authenticationManager->sendNotAllowed($player);
return; return;
} }
$updateData = $this->checkCoreUpdate(true); $updateData = $this->checkCoreUpdate(true);
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;
} }
$this->maniaControl->chat->sendInformation("Starting Update to Version v{$updateData->version}...", $player->login); $this->maniaControl->chat->sendInformation("Starting Update to Version v{$updateData->version}...", $player->login);
$this->maniaControl->log("Starting Update to Version v{$updateData->version}..."); $this->maniaControl->log("Starting Update to Version v{$updateData->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()) {
$this->maniaControl->chat->sendError('Creating backup failed.', $player->login); $this->maniaControl->chat->sendError('Creating backup failed.', $player->login);
$this->maniaControl->log("Creating backup failed."); $this->maniaControl->log("Creating backup failed.");
} }
if(!$this->performCoreUpdate($updateData)) { if (!$this->performCoreUpdate($updateData)) {
$this->maniaControl->chat->sendError('Update failed!', $player->login); $this->maniaControl->chat->sendError('Update failed!', $player->login);
return; return;
} }
$this->maniaControl->chat->sendSuccess('Update finished!', $player->login); $this->maniaControl->chat->sendSuccess('Update finished!', $player->login);
$this->maniaControl->restart(); $this->maniaControl->restart();
} }
/** /**
@ -234,19 +237,20 @@ class UpdateManager implements CallbackListener, CommandListener {
* @return mixed * @return mixed
*/ */
public function checkPluginUpdate($pluginClass) { public function checkPluginUpdate($pluginClass) {
if(is_object($pluginClass)) { if (is_object($pluginClass)) {
$pluginClass = get_class($pluginClass); $pluginClass = get_class($pluginClass);
} }
/** @var Plugin $pluginClass */
$pluginId = $pluginClass::getId(); $pluginId = $pluginClass::getId();
$url = self::URL_WEBSERVICE . 'plugins?id=' . $pluginId; $url = self::URL_WEBSERVICE . 'plugins?id=' . $pluginId;
$dataJson = file_get_contents($url); $dataJson = file_get_contents($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->version <= $pluginVersion) { if ($pluginData->version <= $pluginVersion) {
return false; return false;
} }
return $pluginData; return $pluginData;
@ -262,11 +266,11 @@ class UpdateManager implements CallbackListener, CommandListener {
$url = self::URL_WEBSERVICE . 'versions?update=1&current=1&channel=' . $updateChannel; $url = self::URL_WEBSERVICE . 'versions?update=1&current=1&channel=' . $updateChannel;
$dataJson = file_get_contents($url); $dataJson = file_get_contents($url);
$versions = json_decode($dataJson); $versions = json_decode($dataJson);
if(!$versions || !isset($versions[0])) { if (!$versions || !isset($versions[0])) {
return false; return false;
} }
$updateData = $versions[0]; $updateData = $versions[0];
if(!$ignoreVersion && $updateData->version <= ManiaControl::VERSION) { if (!$ignoreVersion && $updateData->version <= ManiaControl::VERSION) {
return false; return false;
} }
return $updateData; return $updateData;
@ -279,12 +283,12 @@ class UpdateManager implements CallbackListener, CommandListener {
*/ */
private function performBackup() { private function performBackup() {
$backupFolder = ManiaControlDir . '/backup/'; $backupFolder = ManiaControlDir . '/backup/';
if(!is_dir($backupFolder)) { if (!is_dir($backupFolder)) {
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;
} }
@ -309,21 +313,21 @@ class UpdateManager implements CallbackListener, CommandListener {
*/ */
private function zipDirectory(\ZipArchive &$zipArchive, $folderName, $prefixLength, array $excludes = array()) { private function zipDirectory(\ZipArchive &$zipArchive, $folderName, $prefixLength, array $excludes = array()) {
$folderHandle = opendir($folderName); $folderHandle = opendir($folderName);
if(!$folderHandle) { if (!$folderHandle) {
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);
continue; continue;
} }
if(is_dir($filePath)) { if (is_dir($filePath)) {
$zipArchive->addEmptyDir($localPath); $zipArchive->addEmptyDir($localPath);
$this->zipDirectory($zipArchive, $filePath, $prefixLength, $excludes); $this->zipDirectory($zipArchive, $filePath, $prefixLength, $excludes);
continue; continue;
@ -340,30 +344,30 @@ class UpdateManager implements CallbackListener, CommandListener {
* @return bool * @return bool
*/ */
private function performCoreUpdate($updateData = null) { private function performCoreUpdate($updateData = null) {
if(!$this->checkPermissions()) { if (!$this->checkPermissions()) {
return false; return false;
} }
if(!$updateData) { if (!$updateData) {
$updateData = $this->checkCoreUpdate(); $updateData = $this->checkCoreUpdate();
if(!$updateData) { if (!$updateData) {
return false; return false;
} }
} }
$updateFileContent = file_get_contents($updateData->url); $updateFileContent = file_get_contents($updateData->url);
$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.");
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})");
return false; return false;
} }
@ -374,35 +378,35 @@ class UpdateManager implements CallbackListener, CommandListener {
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 = str_replace('core', '', realpath(dirname(__FILE__))); $path = str_replace('core', '', realpath(dirname(__FILE__)));
foreach($writableDirectories as $writableDirecotry) { foreach($writableDirectories as $writableDirecotry) {
$files = array(); $files = array();
$di = new \RecursiveDirectoryIterator($path.$writableDirecotry); $di = new \RecursiveDirectoryIterator($path . $writableDirecotry);
foreach (new \RecursiveIteratorIterator($di) as $filename => $file) { foreach(new \RecursiveIteratorIterator($di) as $filename => $file) {
$files[] = $filename; $files[] = $filename;
} }
foreach($files as $file) { foreach($files as $file) {
if(substr($file, -1) != '.' && substr($file, -2) != '..') { if (substr($file, -1) != '.' && substr($file, -2) != '..') {
echo $file."\n\r"; echo $file . "\n\r";
if(!is_writable($file)) { if (!is_writable($file)) {
$this->maniaControl->log('Cannot update: the file/directory "'.$file.'" is not writable!'); $this->maniaControl->log('Cannot update: the file/directory "' . $file . '" is not writable!');
return false; return false;
} }
} }
} }
} }
return true; return true;
} }
/** /**
* Retrieve the Update Channel Setting * Retrieve the Update Channel Setting
@ -412,7 +416,7 @@ class UpdateManager implements CallbackListener, CommandListener {
private function getCurrentUpdateChannelSetting() { private function getCurrentUpdateChannelSetting() {
$updateChannel = $this->maniaControl->settingManager->getSetting($this, self::SETTING_UPDATECHECK_CHANNEL); $updateChannel = $this->maniaControl->settingManager->getSetting($this, self::SETTING_UPDATECHECK_CHANNEL);
$updateChannel = strtolower($updateChannel); $updateChannel = strtolower($updateChannel);
if(!in_array($updateChannel, array(self::CHANNEL_RELEASE, self::CHANNEL_BETA, self::CHANNEL_NIGHTLY))) { if (!in_array($updateChannel, array(self::CHANNEL_RELEASE, self::CHANNEL_BETA, self::CHANNEL_NIGHTLY))) {
$updateChannel = self::CHANNEL_RELEASE; $updateChannel = self::CHANNEL_RELEASE;
} }
return $updateChannel; return $updateChannel;