TrackManiaControl/application/core/UpdateManager.php

411 lines
13 KiB
PHP
Raw Normal View History

2013-12-17 17:38:44 +01:00
<?php
namespace ManiaControl;
use ManiaControl\Admin\AuthenticationManager;
use ManiaControl\Callbacks\CallbackListener;
use ManiaControl\Callbacks\CallbackManager;
use ManiaControl\Commands\CommandListener;
use ManiaControl\Players\Player;
use ManiaControl\Players\PlayerManager;
/**
2013-12-31 18:42:54 +01:00
* Manager checking for ManiaControl Core and Plugin Updates
2013-12-17 17:38:44 +01:00
*
* @author steeffeen & kremsy
*/
class UpdateManager implements CallbackListener, CommandListener {
2014-01-19 22:32:09 +01:00
/*
2013-12-17 17:38:44 +01:00
* Constants
*/
2014-01-20 21:54:58 +01:00
const SETTING_ENABLEUPDATECHECK = 'Enable Automatic Core Update Check';
2013-12-17 17:38:44 +01:00
const SETTING_UPDATECHECK_INTERVAL = 'Core Update Check Interval (Hours)';
2014-01-20 21:54:58 +01:00
const SETTING_UPDATECHECK_CHANNEL = 'Core Update Channel (release, beta, nightly)';
const SETTING_PERFORM_BACKUPS = 'Perform Backup before Updating';
2014-01-21 19:12:55 +01:00
const SETTING_AUTO_UPDATE = 'Perform update automatically';
2014-01-21 19:36:09 +01:00
const SETTING_PERMISSION_UPDATE = 'Update Core';
2014-01-20 21:54:58 +01:00
const URL_WEBSERVICE = 'http://ws.maniacontrol.com/';
const CHANNEL_RELEASE = 'release';
const CHANNEL_BETA = 'beta';
const CHANNEL_NIGHTLY = 'nightly';
2014-01-19 22:32:09 +01:00
/*
2013-12-17 17:38:44 +01:00
* Private Properties
*/
private $maniaControl = null;
private $lastUpdateCheck = -1;
private $coreUpdateData = null;
/**
2014-01-19 22:32:09 +01:00
* Create a new Update Manager
2013-12-17 17:38:44 +01:00
*
2013-12-31 17:17:40 +01:00
* @param ManiaControl $maniaControl
2013-12-17 17:38:44 +01:00
*/
public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl;
2014-01-20 21:54:58 +01:00
2013-12-17 17:38:44 +01:00
// Init settings
$this->maniaControl->settingManager->initSetting($this, self::SETTING_ENABLEUPDATECHECK, true);
2014-01-21 19:12:55 +01:00
$this->maniaControl->settingManager->initSetting($this, self::SETTING_AUTO_UPDATE, true);
2013-12-17 17:38:44 +01:00
$this->maniaControl->settingManager->initSetting($this, self::SETTING_UPDATECHECK_INTERVAL, 24.);
2014-01-19 23:32:13 +01:00
$this->maniaControl->settingManager->initSetting($this, self::SETTING_UPDATECHECK_CHANNEL, self::CHANNEL_NIGHTLY); //TODO just temp until release
2013-12-31 18:25:07 +01:00
$this->maniaControl->settingManager->initSetting($this, self::SETTING_PERFORM_BACKUPS, true);
2014-01-20 21:54:58 +01:00
2014-01-21 19:12:55 +01:00
2013-12-17 17:38:44 +01:00
// Register for callbacks
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MC_1_MINUTE, $this, 'handle1Minute');
$this->maniaControl->callbackManager->registerCallbackListener(PlayerManager::CB_PLAYERJOINED, $this, 'handlePlayerJoined');
2014-01-20 21:54:58 +01:00
$this->maniaControl->callbackManager->registerCallbackListener(PlayerManager::CB_PLAYERDISCONNECTED, $this, 'handlePlayerDisconnected');
2014-01-21 19:36:09 +01:00
$this->maniaControl->authenticationManager->definePermissionLevel(self::SETTING_PERMISSION_UPDATE, AuthenticationManager::AUTH_LEVEL_ADMIN);
2014-01-20 21:54:58 +01:00
2013-12-17 17:38:44 +01:00
// Register for chat commands
$this->maniaControl->commandManager->registerCommandListener('checkupdate', $this, 'handle_CheckUpdate', true);
2013-12-31 18:25:07 +01:00
$this->maniaControl->commandManager->registerCommandListener('coreupdate', $this, 'handle_CoreUpdate', true);
2013-12-17 17:38:44 +01:00
}
/**
* Handle ManiaControl 1Minute callback
*
2013-12-31 17:17:40 +01:00
* @param array $callback
2013-12-17 17:38:44 +01:00
*/
public function handle1Minute(array $callback) {
$updateCheckEnabled = $this->maniaControl->settingManager->getSetting($this, self::SETTING_ENABLEUPDATECHECK);
2014-01-20 21:54:58 +01:00
if(!$updateCheckEnabled) {
2013-12-31 17:17:40 +01:00
// Automatic update check disabled
2014-01-20 21:54:58 +01:00
if($this->coreUpdateData) {
$this->coreUpdateData = null;
}
2013-12-17 17:38:44 +01:00
return;
}
2013-12-31 17:17:40 +01:00
// Only check once per hour
2013-12-17 17:38:44 +01:00
$updateInterval = $this->maniaControl->settingManager->getSetting($this, self::SETTING_UPDATECHECK_INTERVAL);
2014-01-20 21:54:58 +01:00
if($this->lastUpdateCheck > time() - $updateInterval * 3600.) {
return;
}
2013-12-17 17:38:44 +01:00
$this->lastUpdateCheck = time();
2014-01-20 21:54:58 +01:00
$updateData = $this->checkCoreUpdate();
if(!$updateData) {
return;
}
2013-12-17 17:38:44 +01:00
$this->maniaControl->log('New ManiaControl Version ' . $updateData->version . ' available!');
$this->coreUpdateData = $updateData;
}
/**
* Handle ManiaControl PlayerJoined callback
*
2013-12-31 17:17:40 +01:00
* @param array $callback
2013-12-17 17:38:44 +01:00
*/
public function handlePlayerJoined(array $callback) {
2014-01-20 21:54:58 +01:00
if(!$this->coreUpdateData) {
return;
}
2013-12-31 17:17:40 +01:00
// Announce available update
2013-12-17 17:38:44 +01:00
$player = $callback[1];
2014-01-20 21:54:58 +01:00
if(!AuthenticationManager::checkRight($player, AuthenticationManager::AUTH_LEVEL_SUPERADMIN)) {
return;
}
2014-01-19 22:32:09 +01:00
$this->maniaControl->chat->sendInformation('New ManiaControl Version ' . $this->coreUpdateData->version . ' available!', $player->login);
2013-12-17 17:38:44 +01:00
}
2014-01-20 21:54:58 +01:00
/**
* Perform automatic update as soon as a the Server is empty
*
* @param array $callback
*/
public function handlePlayerDisconnected(array $callback) {
2014-01-21 19:12:55 +01:00
$performBackup = $this->maniaControl->settingManager->getSetting($this, self::SETTING_AUTO_UPDATE);
if(!$performBackup) {
return;
}
2014-01-20 21:54:58 +01:00
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();
}
2013-12-17 17:38:44 +01:00
/**
* Handle //checkupdate command
*
2014-01-20 21:54:58 +01:00
* @param array $chatCallback
2013-12-31 17:17:40 +01:00
* @param Player $player
2013-12-17 17:38:44 +01:00
*/
public function handle_CheckUpdate(array $chatCallback, Player $player) {
2014-01-20 21:54:58 +01:00
if(!AuthenticationManager::checkRight($player, AuthenticationManager::AUTH_LEVEL_SUPERADMIN)) {
2013-12-17 17:38:44 +01:00
$this->maniaControl->authenticationManager->sendNotAllowed($player);
return;
}
2014-01-19 22:32:09 +01:00
$updateChannel = $this->maniaControl->settingManager->getSetting($this, self::SETTING_UPDATECHECK_CHANNEL);
2014-01-20 21:54:58 +01:00
if($updateChannel != self::CHANNEL_NIGHTLY) {
2014-01-19 22:32:09 +01:00
// Check update and send result message
$updateData = $this->checkCoreUpdate();
2014-01-20 21:54:58 +01:00
if(!$updateData) {
2014-01-19 22:32:09 +01:00
$this->maniaControl->chat->sendInformation('No Update available!', $player->login);
return;
}
$this->maniaControl->chat->sendSuccess('Update for Version ' . $updateData->version . ' available!', $player->login);
2014-01-20 21:54:58 +01:00
} else {
2014-01-19 22:32:09 +01:00
// Special nightly channel updating
$updateData = $this->checkCoreUpdate(true);
2014-01-20 21:54:58 +01:00
$buildDate = $this->getNightlyBuildDate();
if($buildDate) {
$buildTime = strtotime($buildDate);
2014-01-19 22:32:09 +01:00
$releaseTime = strtotime($updateData->release_date);
2014-01-20 21:54:58 +01:00
if($buildTime >= $releaseTime) {
2014-01-19 22:32:09 +01:00
$this->maniaControl->chat->sendInformation('No new Build available!', $player->login);
return;
}
}
$this->maniaControl->chat->sendSuccess('New Nightly Build available!', $player->login);
2013-12-17 17:38:44 +01:00
}
2014-01-19 22:32:09 +01:00
}
/**
* Get the Build Date of the local Nightly Build Version
*
* @return mixed
*/
private function getNightlyBuildDate() {
$nightlyBuildDateFile = ManiaControlDir . '/core/nightly_build.txt';
2014-01-20 21:54:58 +01:00
if(!file_exists($nightlyBuildDateFile)) {
return false;
}
2014-01-19 22:32:09 +01:00
$fileContent = file_get_contents($nightlyBuildDateFile);
return $fileContent;
2013-12-17 17:38:44 +01:00
}
2013-12-31 18:25:07 +01:00
/**
* Handle //coreupdate command
*
2014-01-20 21:54:58 +01:00
* @param array $chatCallback
2013-12-31 18:25:07 +01:00
* @param Player $player
*/
public function handle_CoreUpdate(array $chatCallback, Player $player) {
2014-01-21 19:36:09 +01:00
if(!$this->maniaControl->authenticationManager->checkPermission($player, self::SETTING_PERMISSION_UPDATE)) {
2013-12-31 18:25:07 +01:00
$this->maniaControl->authenticationManager->sendNotAllowed($player);
return;
}
2013-12-31 18:42:54 +01:00
$updateData = $this->checkCoreUpdate(true);
2014-01-20 21:54:58 +01:00
if(!$updateData) {
2013-12-31 18:42:54 +01:00
$this->maniaControl->chat->sendError('Update is currently not possible!', $player->login);
return;
}
2014-01-20 21:54:58 +01:00
$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();
}
2013-12-17 17:38:44 +01:00
/**
* Check given Plugin Class for Update
*
2013-12-31 17:17:40 +01:00
* @param string $pluginClass
2013-12-17 17:38:44 +01:00
* @return mixed
*/
public function checkPluginUpdate($pluginClass) {
2014-01-20 21:54:58 +01:00
if(is_object($pluginClass)) {
2013-12-17 17:38:44 +01:00
$pluginClass = get_class($pluginClass);
}
2014-01-20 21:54:58 +01:00
$pluginId = $pluginClass::getId();
$url = self::URL_WEBSERVICE . 'plugins?id=' . $pluginId;
$dataJson = file_get_contents($url);
2013-12-17 17:38:44 +01:00
$pluginVersions = json_decode($dataJson);
2014-01-20 21:54:58 +01:00
if(!$pluginVersions || !isset($pluginVersions[0])) {
2013-12-17 17:38:44 +01:00
return false;
}
2014-01-20 21:54:58 +01:00
$pluginData = $pluginVersions[0];
2013-12-17 17:38:44 +01:00
$pluginVersion = $pluginClass::getVersion();
2014-01-20 21:54:58 +01:00
if($pluginData->version <= $pluginVersion) {
2013-12-17 17:38:44 +01:00
return false;
}
return $pluginData;
}
/**
* Check for Update of ManiaControl
*
* @return mixed
*/
2013-12-31 18:42:54 +01:00
private function checkCoreUpdate($ignoreVersion = false) {
2013-12-27 23:26:27 +01:00
$updateChannel = $this->getCurrentUpdateChannelSetting();
2014-01-20 21:54:58 +01:00
$url = self::URL_WEBSERVICE . 'versions?update=1&current=1&channel=' . $updateChannel;
$dataJson = file_get_contents($url);
$versions = json_decode($dataJson);
if(!$versions || !isset($versions[0])) {
2013-12-17 17:38:44 +01:00
return false;
}
$updateData = $versions[0];
2014-01-20 21:54:58 +01:00
if(!$ignoreVersion && $updateData->version <= ManiaControl::VERSION) {
2013-12-17 17:38:44 +01:00
return false;
}
return $updateData;
}
2013-12-27 23:26:27 +01:00
2013-12-31 18:25:07 +01:00
/**
* Perform a Backup of ManiaControl
*
* @return bool
*/
private function performBackup() {
$backupFolder = ManiaControlDir . '/backup/';
2014-01-20 21:54:58 +01:00
if(!is_dir($backupFolder)) {
mkdir($backupFolder);
}
2014-01-19 22:32:09 +01:00
$backupFileName = $backupFolder . 'backup_' . ManiaControl::VERSION . '_' . date('y-m-d') . '_' . time() . '.zip';
2014-01-20 21:54:58 +01:00
$backupZip = new \ZipArchive();
if($backupZip->open($backupFileName, \ZipArchive::CREATE) !== TRUE) {
2013-12-31 18:25:07 +01:00
trigger_error("Couldn't create Backup Zip!");
return false;
}
2014-01-20 21:54:58 +01:00
$excludes = array('.', '..', 'backup', 'logs', 'ManiaControl.log');
$pathInfo = pathInfo(ManiaControlDir);
2013-12-31 18:25:07 +01:00
$parentPath = $pathInfo['dirname'] . '/';
2014-01-20 21:54:58 +01:00
$dirName = $pathInfo['basename'];
2013-12-31 18:25:07 +01:00
$backupZip->addEmptyDir($dirName);
$this->zipDirectory($backupZip, ManiaControlDir, strlen($parentPath), $excludes);
$backupZip->close();
return true;
}
/**
* Add a complete Directory to the ZipArchive
*
* @param \ZipArchive $zipArchive
2014-01-20 21:54:58 +01:00
* @param string $folderName
* @param int $prefixLength
* @param array $excludes
2013-12-31 18:25:07 +01:00
* @return bool
*/
private function zipDirectory(\ZipArchive &$zipArchive, $folderName, $prefixLength, array $excludes = array()) {
$folderHandle = opendir($folderName);
2014-01-20 21:54:58 +01:00
if(!$folderHandle) {
2013-12-31 18:25:07 +01:00
trigger_error("Couldn't open Folder '{$folderName}' for Backup!");
return false;
}
2014-01-20 21:54:58 +01:00
while(false !== ($file = readdir($folderHandle))) {
if(in_array($file, $excludes)) {
continue;
}
$filePath = $folderName . '/' . $file;
2013-12-31 18:25:07 +01:00
$localPath = substr($filePath, $prefixLength);
2014-01-20 21:54:58 +01:00
if(is_file($filePath)) {
2013-12-31 18:25:07 +01:00
$zipArchive->addFile($filePath, $localPath);
continue;
}
2014-01-20 21:54:58 +01:00
if(is_dir($filePath)) {
2013-12-31 18:25:07 +01:00
$zipArchive->addEmptyDir($localPath);
$this->zipDirectory($zipArchive, $filePath, $prefixLength, $excludes);
continue;
}
}
closedir($folderHandle);
return true;
}
2013-12-27 23:26:27 +01:00
/**
* Perform a Core Update
*
2013-12-31 17:17:40 +01:00
* @param object $updateData
2013-12-27 23:26:27 +01:00
* @return bool
*/
private function performCoreUpdate($updateData = null) {
if(!$this->checkPermissions()) {
return false;
}
2014-01-20 21:54:58 +01:00
if(!$updateData) {
2013-12-27 23:26:27 +01:00
$updateData = $this->checkCoreUpdate();
2014-01-20 21:54:58 +01:00
if(!$updateData) {
2013-12-27 23:26:27 +01:00
return false;
}
}
$updateFileContent = file_get_contents($updateData->url);
2014-01-20 21:54:58 +01:00
$tempDir = ManiaControlDir . '/temp/';
if(!is_dir($tempDir)) {
mkdir($tempDir);
}
2013-12-27 23:26:27 +01:00
$updateFileName = $tempDir . basename($updateData->url);
2014-01-20 21:54:58 +01:00
$bytes = file_put_contents($updateFileName, $updateFileContent);
if(!$bytes || $bytes <= 0) {
2013-12-27 23:26:27 +01:00
trigger_error("Couldn't save Update Zip.");
return false;
}
2014-01-20 21:54:58 +01:00
$zip = new \ZipArchive();
2013-12-27 23:26:27 +01:00
$result = $zip->open($updateFileName);
2014-01-20 21:54:58 +01:00
if($result !== true) {
2013-12-27 23:26:27 +01:00
trigger_error("Couldn't open Update Zip. ({$result})");
return false;
}
2013-12-31 18:42:54 +01:00
$zip->extractTo(ManiaControlDir);
2013-12-27 23:26:27 +01:00
$zip->close();
2013-12-31 18:25:07 +01:00
unlink($updateFileName);
@rmdir($tempDir);
2013-12-27 23:26:27 +01:00
return true;
}
private function checkPermissions() {
$writableDirectories = array('core/', 'plugins/');
$ignore = array('.', '..');
$path = str_replace('core', '', realpath(dirname(__FILE__)));
foreach($writableDirectories as $writableDirecotry) {
$files = scandir($path.$writableDirecotry);
foreach($files as $file) {
if(!in_array($file, $ignore)) {
if(!is_writable($path.$writableDirecotry.$file)) {
$this->maniaControl->log('Cannot update: the file/directory "'.$path.$writableDirecotry.$file.'" is not writable!');
return false;
}
}
}
}
return true;
}
2013-12-27 23:26:27 +01:00
/**
* Retrieve the Update Channel Setting
*
* @return string
*/
private function getCurrentUpdateChannelSetting() {
$updateChannel = $this->maniaControl->settingManager->getSetting($this, self::SETTING_UPDATECHECK_CHANNEL);
$updateChannel = strtolower($updateChannel);
2014-01-20 21:54:58 +01:00
if(!in_array($updateChannel, array(self::CHANNEL_RELEASE, self::CHANNEL_BETA, self::CHANNEL_NIGHTLY))) {
2013-12-27 23:26:27 +01:00
$updateChannel = self::CHANNEL_RELEASE;
}
return $updateChannel;
}
2013-12-17 17:38:44 +01:00
}