TrackManiaControl/application/core/UpdateManager.php

425 lines
14 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;
2014-01-31 00:04:40 +01:00
use ManiaControl\Callbacks\TimerListener;
2013-12-17 17:38:44 +01:00
use ManiaControl\Commands\CommandListener;
use ManiaControl\Players\Player;
use ManiaControl\Players\PlayerManager;
2014-01-28 16:23:50 +01:00
use ManiaControl\Plugins\Plugin;
2013-12-17 17:38:44 +01:00
/**
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
*/
2014-01-31 00:04:40 +01:00
class UpdateManager implements CallbackListener, CommandListener, TimerListener {
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
2014-01-31 00:04:40 +01:00
$this->maniaControl->timerManager->registerTimerListening($this, 'handle1Minute', 1000 * 60);
2013-12-17 17:38:44 +01:00
$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
*
2014-01-31 00:04:40 +01:00
* @param $time
2013-12-17 17:38:44 +01:00
*/
2014-01-31 00:04:40 +01:00
public function handle1Minute($time) {
2013-12-17 17:38:44 +01:00
$updateCheckEnabled = $this->maniaControl->settingManager->getSetting($this, self::SETTING_ENABLEUPDATECHECK);
2014-01-28 16:23:50 +01:00
if (!$updateCheckEnabled) {
2013-12-31 17:17:40 +01:00
// Automatic update check disabled
2014-01-28 16:23:50 +01:00
if ($this->coreUpdateData) {
2014-01-20 21:54:58 +01:00
$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-28 16:23:50 +01:00
if ($this->lastUpdateCheck > time() - $updateInterval * 3600.) {
2014-01-20 21:54:58 +01:00
return;
}
2013-12-17 17:38:44 +01:00
$this->lastUpdateCheck = time();
2014-01-20 21:54:58 +01:00
$updateData = $this->checkCoreUpdate();
2014-01-28 16:23:50 +01:00
if (!$updateData) {
2014-01-20 21:54:58 +01:00
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-28 16:23:50 +01:00
if (!$this->coreUpdateData) {
2014-01-20 21:54:58 +01:00
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-28 16:23:50 +01:00
if (!AuthenticationManager::checkRight($player, AuthenticationManager::AUTH_LEVEL_SUPERADMIN)) {
2014-01-20 21:54:58 +01:00
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);
2014-01-28 16:23:50 +01:00
if (!$performBackup) {
2014-01-21 19:12:55 +01:00
return;
}
2014-01-28 16:23:50 +01:00
if (count($this->maniaControl->playerManager->getPlayers()) > 0) {
2014-01-20 21:54:58 +01:00
return;
}
$updateData = $this->checkCoreUpdate(true);
2014-01-28 16:23:50 +01:00
if (!$updateData) {
2014-01-20 21:54:58 +01:00
return;
}
$this->maniaControl->log("Starting Update to Version v{$updateData->version}...");
$performBackup = $this->maniaControl->settingManager->getSetting($this, self::SETTING_PERFORM_BACKUPS);
2014-01-28 16:23:50 +01:00
if ($performBackup && !$this->performBackup()) {
2014-01-20 21:54:58 +01:00
$this->maniaControl->log("Creating Backup failed!");
}
2014-01-28 16:23:50 +01:00
if (!$this->performCoreUpdate($updateData)) {
2014-01-20 21:54:58 +01:00
$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-28 16:23:50 +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-28 16:23:50 +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-28 16:23:50 +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
2014-01-28 16:23:50 +01:00
$updateData = $this->checkCoreUpdate(true);
$buildDate = $this->getNightlyBuildDate();
$releaseTime = strtotime($updateData->release_date);
if ($buildDate) {
$buildTime = strtotime($buildDate);
if ($buildTime >= $releaseTime) {
$this->maniaControl->chat->sendInformation('No new Build available, current build: ' . date("Y-m-d", $buildTime) . '!', $player->login);
2014-01-19 22:32:09 +01:00
return;
}
2014-01-28 16:23:50 +01:00
$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);
2014-01-19 22:32:09 +01:00
}
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-28 16:23:50 +01:00
if (!file_exists($nightlyBuildDateFile)) {
2014-01-20 21:54:58 +01:00
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-28 16:23:50 +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-28 16:23:50 +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
2014-01-28 16:23:50 +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-28 16:23:50 +01:00
if (is_object($pluginClass)) {
2013-12-17 17:38:44 +01:00
$pluginClass = get_class($pluginClass);
}
2014-01-28 16:23:50 +01:00
/** @var Plugin $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-28 16:23:50 +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-28 16:23:50 +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);
2014-01-28 16:23:50 +01:00
if (!$versions || !isset($versions[0])) {
2013-12-17 17:38:44 +01:00
return false;
}
$updateData = $versions[0];
2014-01-28 16:23:50 +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-28 16:23:50 +01:00
if (!is_dir($backupFolder)) {
2014-01-20 21:54:58 +01:00
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();
2014-01-28 16:23:50 +01:00
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-28 16:23:50 +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))) {
2014-01-28 16:23:50 +01:00
if (in_array($file, $excludes)) {
2014-01-20 21:54:58 +01:00
continue;
}
$filePath = $folderName . '/' . $file;
2013-12-31 18:25:07 +01:00
$localPath = substr($filePath, $prefixLength);
2014-01-28 16:23:50 +01:00
if (is_file($filePath)) {
2013-12-31 18:25:07 +01:00
$zipArchive->addFile($filePath, $localPath);
continue;
}
2014-01-28 16:23:50 +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) {
2014-01-28 16:23:50 +01:00
if (!$this->checkPermissions()) {
return false;
}
2014-01-28 16:23:50 +01:00
if (!$updateData) {
2013-12-27 23:26:27 +01:00
$updateData = $this->checkCoreUpdate();
2014-01-28 16:23:50 +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/';
2014-01-28 16:23:50 +01:00
if (!is_dir($tempDir)) {
2014-01-20 21:54:58 +01:00
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);
2014-01-28 16:23:50 +01:00
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-28 16:23:50 +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;
}
2014-01-28 16:23:50 +01:00
/**
* Function checks if ManiaControl has sufficient access to files to update them.
*
* @return bool
*/
private function checkPermissions() {
$writableDirectories = array('core/', 'plugins/');
$path = str_replace('core', '', realpath(dirname(__FILE__)));
foreach($writableDirectories as $writableDirecotry) {
$files = array();
$di = new \RecursiveDirectoryIterator($path . $writableDirecotry);
foreach(new \RecursiveIteratorIterator($di) as $filename => $file) {
$files[] = $filename;
}
foreach($files as $file) {
if (substr($file, -1) != '.' && substr($file, -2) != '..') {
echo $file . "\n\r";
if (!is_writable($file)) {
$this->maniaControl->log('Cannot update: the file/directory "' . $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-28 16:23:50 +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
}