async plugin update check
This commit is contained in:
		
				
					committed by
					
						 Steffen Schröder
						Steffen Schröder
					
				
			
			
				
	
			
			
			
						parent
						
							1011fbdde1
						
					
				
				
					commit
					9dc3ba395b
				
			| @@ -38,10 +38,6 @@ class PluginManager { | ||||
|  | ||||
| 		$this->pluginMenu = new PluginMenu($maniaControl); | ||||
| 		$this->maniaControl->configurator->addMenu($this->pluginMenu); | ||||
|  | ||||
| 		/*$this->fetchPluginList(function ($data) { | ||||
| 			var_dump($data); | ||||
| 		});*/ | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| @@ -353,16 +349,12 @@ class PluginManager { | ||||
| 	 * @param      $function | ||||
| 	 * @param bool $ignoreVersion | ||||
| 	 */ | ||||
| 	private function fetchPluginList($function) { | ||||
| 	public function fetchPluginList($function) { | ||||
| 		$url = ManiaControl::URL_WEBSERVICE . 'plugins'; | ||||
|  | ||||
| 		$this->maniaControl->fileReader->loadFile($url, function ($dataJson, $error) use (&$function) { | ||||
| 			$data = json_decode($dataJson); | ||||
| 			if (!$data || !isset($data[0])) { | ||||
| 				return; | ||||
| 			} | ||||
|  | ||||
| 			call_user_func($function, $data[0]); | ||||
| 			call_user_func($function, $data, $error); | ||||
| 		}); | ||||
| 	} | ||||
| } | ||||
|   | ||||
| @@ -3,8 +3,8 @@ | ||||
| namespace ManiaControl\Update; | ||||
|  | ||||
| use ManiaControl\Admin\AuthenticationManager; | ||||
| use ManiaControl\Callbacks\CallbackManager; | ||||
| use ManiaControl\Callbacks\CallbackListener; | ||||
| use ManiaControl\Callbacks\CallbackManager; | ||||
| use ManiaControl\Callbacks\TimerListener; | ||||
| use ManiaControl\Commands\CommandListener; | ||||
| use ManiaControl\Files\FileUtil; | ||||
| @@ -162,7 +162,7 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener | ||||
| 		} | ||||
|  | ||||
| 		$version = $this->maniaControl->client->getVersion(); | ||||
| 		if($this->coreUpdateData->minDedicatedBuild > $version->build){ | ||||
| 		if ($this->coreUpdateData->minDedicatedBuild > $version->build) { | ||||
| 			return; | ||||
| 		} | ||||
|  | ||||
| @@ -203,7 +203,7 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener | ||||
| 				} | ||||
|  | ||||
| 				$version = $self->maniaControl->client->getVersion(); | ||||
| 				if($updateData->minDedicatedBuild > $version->build){ | ||||
| 				if ($updateData->minDedicatedBuild > $version->build) { | ||||
| 					$self->maniaControl->chat->sendError("No new Build for this Server-version available!", $player->login); | ||||
| 					return; | ||||
| 				} | ||||
| @@ -220,7 +220,7 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener | ||||
| 				} | ||||
|  | ||||
| 				$version = $self->maniaControl->client->getVersion(); | ||||
| 				if($updateData->minDedicatedBuild > $version->build){ | ||||
| 				if ($updateData->minDedicatedBuild > $version->build) { | ||||
| 					$self->maniaControl->chat->sendError("No new Build for this Server-version available!", $player->login); | ||||
| 					return; | ||||
| 				} | ||||
| @@ -253,20 +253,20 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener | ||||
| 		$login  = $callback[1][1]; | ||||
| 		$player = $this->maniaControl->playerManager->getPlayer($login); | ||||
|  | ||||
| 		if($update) { | ||||
| 		if ($update) { | ||||
| 			$pluginClass = substr($actionId, strlen(PluginMenu::ACTION_PREFIX_UPDATEPLUGIN)); | ||||
| 			if($pluginClass == 'All') { | ||||
| 			if ($pluginClass == 'All') { | ||||
| 				$this->checkPluginsUpdate($player); | ||||
| 			} else { | ||||
| 				$newUpdate = $this->checkPluginUpdate($pluginClass); | ||||
| 				if($newUpdate != false) { | ||||
| 				if ($newUpdate != false) { | ||||
| 					$newUpdate->pluginClass = $pluginClass; | ||||
| 					$this->updatePlugin($newUpdate, $player, true); | ||||
| 				} | ||||
| 			} | ||||
| 		} | ||||
|  | ||||
| 		if($install) { | ||||
| 		if ($install) { | ||||
| 			$pluginId = substr($actionId, strlen(PluginInstallMenu::ACTION_PREFIX_INSTALLPLUGIN)); | ||||
|  | ||||
| 			$url            = ManiaControl::URL_WEBSERVICE . 'plugins?id=' . $pluginId; | ||||
| @@ -328,13 +328,13 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener | ||||
| 		} | ||||
|  | ||||
| 		$self = $this; | ||||
| 		$this->checkCoreUpdateAsync(function (UpdateData $updateData) use (&$self,&$player) { | ||||
| 		$this->checkCoreUpdateAsync(function (UpdateData $updateData) use (&$self, &$player) { | ||||
| 			if (!$updateData) { | ||||
| 				$self->maniaControl->chat->sendError('Update is currently not possible!', $player->login); | ||||
| 				return; | ||||
| 			} | ||||
| 			$version = $self->maniaControl->client->getVersion(); | ||||
| 			if($updateData->minDedicatedBuild > $version->build){ | ||||
| 			if ($updateData->minDedicatedBuild > $version->build) { | ||||
| 				$self->maniaControl->chat->sendError("ManiaControl update version requires a newer Dedicated Server version!", $player->login); | ||||
| 				return; | ||||
| 			} | ||||
| @@ -368,36 +368,51 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener | ||||
|  | ||||
| 	/** | ||||
| 	 * Checks if there are outdated plugins active. | ||||
| 	 * | ||||
| 	 * @param Player $player | ||||
| 	 */ | ||||
| 	public function checkPluginsUpdate(Player $player = null) { | ||||
| 		$this->maniaControl->log('[UPDATE] Checking plugins for newer versions ...'); | ||||
|  | ||||
| 		$self = $this; | ||||
| 		$this->maniaControl->pluginManager->fetchPluginList(function ($data, $error) use (&$self, &$player) { | ||||
| 			$outdatedPlugins = array(); | ||||
|  | ||||
| 		foreach ($this->maniaControl->pluginManager->getPluginClasses() as $pluginClass) { | ||||
| 			$pluginData = $this->checkPluginUpdate($pluginClass); | ||||
| 			if ($pluginData != false) { | ||||
| 				$pluginData->pluginClass = $pluginClass; | ||||
| 				$outdatedPlugins[] = $pluginData; | ||||
| 				$this->maniaControl->log('[UPDATE] '.$pluginClass.': There is a newer version available: '.$pluginData->currentVersion->version.'!'); | ||||
| 			if (!$data || $error) { | ||||
| 				$self->maniaControl->log('[UPDATE] Error while checking plugins for newer version'); | ||||
| 				return; | ||||
| 			} | ||||
|  | ||||
| 			$pluginClasses = $self->maniaControl->pluginManager->getPluginClasses(); | ||||
|  | ||||
| 			foreach($data as $plugin) { | ||||
| 				foreach($pluginClasses as $pluginClass) { | ||||
| 					$id = $pluginClass::getId(); | ||||
| 					if ($plugin->id == $id) { | ||||
| 						if ($plugin->currentVersion->version > $pluginClass::getVersion()) { | ||||
| 							$outdatedPlugins[] = $plugin; | ||||
| 							$self->maniaControl->log('[UPDATE] ' . $pluginClass . ': There is a newer version available: ' . $plugin->currentVersion->version . '!'); | ||||
| 						} | ||||
| 					} | ||||
| 				} | ||||
| 			} | ||||
|  | ||||
| 			if (count($outdatedPlugins) > 0) { | ||||
| 			$this->maniaControl->log('[UPDATE] Checking plugins: COMPLETE, there are '.count($outdatedPlugins).' outdated plugins, now updating ...'); | ||||
| 				$self->maniaControl->log('[UPDATE] Checking plugins: COMPLETE, there are ' . count($outdatedPlugins) . ' outdated plugins, now updating ...'); | ||||
| 				if ($player) { | ||||
| 				$this->maniaControl->chat->sendInformation('Checking plugins: COMPLETE, there are '.count($outdatedPlugins).' outdated plugins, now updating ...', $player->login); | ||||
| 					$self->maniaControl->chat->sendInformation('Checking plugins: COMPLETE, there are ' . count($outdatedPlugins) . ' outdated plugins, now updating ...', $player->login); | ||||
| 				} | ||||
| 			$this->performPluginsBackup(); | ||||
| 			foreach ($outdatedPlugins as $plugin) { | ||||
| 				$this->updatePlugin($plugin, $player); | ||||
| 				$self->performPluginsBackup(); | ||||
| 				foreach($outdatedPlugins as $plugin) { | ||||
| 					$self->updatePlugin($plugin, $player); | ||||
| 				} | ||||
| 			} else { | ||||
| 			$this->maniaControl->log('[UPDATE] Checking plugins: COMPLETE, all plugins are up-to-date!'); | ||||
| 				$self->maniaControl->log('[UPDATE] Checking plugins: COMPLETE, all plugins are up-to-date!'); | ||||
| 				if ($player) { | ||||
| 				$this->maniaControl->chat->sendInformation('Checking plugins: COMPLETE, all plugins are up-to-date!', $player->login); | ||||
| 					$self->maniaControl->chat->sendInformation('Checking plugins: COMPLETE, all plugins are up-to-date!', $player->login); | ||||
| 				} | ||||
| 			} | ||||
| 		}); | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| @@ -449,14 +464,14 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener | ||||
| 		/** @var  Plugin $pluginClass */ | ||||
| 		foreach($this->maniaControl->pluginManager->getPluginClasses() as $pluginClass) { | ||||
| 			$pluginId = $pluginClass::getId(); | ||||
| 			if(array_key_exists($pluginId, $pluginsWS)) { | ||||
| 				if($pluginsWS[$pluginId]->currentVersion->version > $pluginClass::getVersion()) { | ||||
| 			if (array_key_exists($pluginId, $pluginsWS)) { | ||||
| 				if ($pluginsWS[$pluginId]->currentVersion->version > $pluginClass::getVersion()) { | ||||
| 					$pluginUpdates[$pluginId] = $pluginsWS[$pluginId]; | ||||
| 				} | ||||
| 			} | ||||
| 		} | ||||
|  | ||||
| 		if(empty($pluginUpdates)) { | ||||
| 		if (empty($pluginUpdates)) { | ||||
| 			return false; | ||||
| 		} | ||||
| 		return $pluginUpdates; | ||||
| @@ -472,9 +487,9 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener | ||||
| 	private function updatePlugin($pluginData, Player $player = null, $reopen = false) { | ||||
| 		$self = $this; | ||||
| 		$this->maniaControl->fileReader->loadFile($pluginData->currentVersion->url, function ($updateFileContent, $error) use (&$self, &$updateData, &$player, &$pluginData, &$reopen) { | ||||
| 			$self->maniaControl->log('[UPDATE] Now updating '.$pluginData->name.' ...'); | ||||
| 			$self->maniaControl->log('[UPDATE] Now updating ' . $pluginData->name . ' ...'); | ||||
| 			if ($player) { | ||||
| 				$self->maniaControl->chat->sendInformation('Now updating '.$pluginData->name.' ...', $player->login); | ||||
| 				$self->maniaControl->chat->sendInformation('Now updating ' . $pluginData->name . ' ...', $player->login); | ||||
| 			} | ||||
| 			$tempDir = ManiaControlDir . '/temp/'; | ||||
| 			if (!is_dir($tempDir)) { | ||||
| @@ -500,14 +515,14 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener | ||||
| 				return false; | ||||
| 			} | ||||
|  | ||||
| 			$zip->extractTo(ManiaControlDir.'/plugins'); | ||||
| 			$zip->extractTo(ManiaControlDir . '/plugins'); | ||||
| 			$zip->close(); | ||||
| 			unlink($updateFileName); | ||||
| 			@rmdir($tempDir); | ||||
|  | ||||
| 			$self->maniaControl->log('[UPDATE] Successfully updated '.$pluginData->name.'!'); | ||||
| 			$self->maniaControl->log('[UPDATE] Successfully updated ' . $pluginData->name . '!'); | ||||
| 			if ($player) { | ||||
| 				$self->maniaControl->chat->sendSuccess('Successfully updated '.$pluginData->name.'!', $player->login); | ||||
| 				$self->maniaControl->chat->sendSuccess('Successfully updated ' . $pluginData->name . '!', $player->login); | ||||
| 				$self->maniaControl->pluginManager->deactivatePlugin($pluginData->pluginClass); | ||||
| 				$self->maniaControl->pluginManager->activatePlugin($pluginData->pluginClass); | ||||
|  | ||||
| @@ -532,8 +547,8 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener | ||||
| 			$pluginsDirectory = ManiaControlDir . '/plugins/'; | ||||
| 			$pluginFiles      = scandir($pluginsDirectory); | ||||
|  | ||||
| 			$self->maniaControl->log('[UPDATE] Now installing '.$pluginData->name.' ...'); | ||||
| 			$self->maniaControl->chat->sendInformation('Now installing '.$pluginData->name.' ...', $player->login); | ||||
| 			$self->maniaControl->log('[UPDATE] Now installing ' . $pluginData->name . ' ...'); | ||||
| 			$self->maniaControl->chat->sendInformation('Now installing ' . $pluginData->name . ' ...', $player->login); | ||||
|  | ||||
| 			$tempDir = ManiaControlDir . '/temp/'; | ||||
| 			if (!is_dir($tempDir)) { | ||||
| @@ -555,7 +570,7 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener | ||||
| 				return false; | ||||
| 			} | ||||
|  | ||||
| 			$zip->extractTo(ManiaControlDir.'/plugins'); | ||||
| 			$zip->extractTo(ManiaControlDir . '/plugins'); | ||||
| 			$zip->close(); | ||||
| 			unlink($installFileName); | ||||
| 			@rmdir($tempDir); | ||||
| @@ -601,8 +616,8 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener | ||||
| 				} | ||||
| 			} | ||||
|  | ||||
| 			$self->maniaControl->log('[UPDATE] Successfully installed '.$pluginData->name.'!'); | ||||
| 			$self->maniaControl->chat->sendSuccess('Successfully installed '.$pluginData->name.'!', $player->login); | ||||
| 			$self->maniaControl->log('[UPDATE] Successfully installed ' . $pluginData->name . '!'); | ||||
| 			$self->maniaControl->chat->sendSuccess('Successfully installed ' . $pluginData->name . '!', $player->login); | ||||
|  | ||||
| 			if ($reopen) { | ||||
| 				$menuId = $self->maniaControl->configurator->getMenuId('Install Plugins'); | ||||
| @@ -687,11 +702,11 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener | ||||
| 			return false; | ||||
| 		} | ||||
| 		$excludes   = array('.', '..'); | ||||
| 		$pathInfo   = pathInfo(ManiaControlDir.'/plugins'); | ||||
| 		$pathInfo   = pathInfo(ManiaControlDir . '/plugins'); | ||||
| 		$parentPath = $pathInfo['dirname'] . '/'; | ||||
| 		$dirName    = $pathInfo['basename']; | ||||
| 		$backupZip->addEmptyDir($dirName); | ||||
| 		$this->zipDirectory($backupZip, ManiaControlDir.'/plugins', strlen($parentPath), $excludes); | ||||
| 		$this->zipDirectory($backupZip, ManiaControlDir . '/plugins', strlen($parentPath), $excludes); | ||||
| 		$backupZip->close(); | ||||
| 		return true; | ||||
| 	} | ||||
|   | ||||
		Reference in New Issue
	
	Block a user