From b18bc644404e02b57635bfcc8258e85abb141146 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 13 Apr 2024 13:44:46 +0200 Subject: Adding upstream version 1.1.1. Signed-off-by: Daniel Baumann --- application/controllers/MigrateController.php | 161 ++++++++++++++++++++++++++ 1 file changed, 161 insertions(+) create mode 100644 application/controllers/MigrateController.php (limited to 'application/controllers/MigrateController.php') diff --git a/application/controllers/MigrateController.php b/application/controllers/MigrateController.php new file mode 100644 index 0000000..811b5d0 --- /dev/null +++ b/application/controllers/MigrateController.php @@ -0,0 +1,161 @@ +assertHttpMethod('post'); + if (! $this->getRequest()->isApiRequest()) { + $this->httpBadRequest('No API request'); + } + + if ( + ! preg_match('/([^;]*);?/', $this->getRequest()->getHeader('Content-Type'), $matches) + || $matches[1] !== 'application/json' + ) { + $this->httpBadRequest('No JSON content'); + } + + $urls = $this->getRequest()->getPost(); + + $result = []; + $errors = []; + foreach ($urls as $urlString) { + $url = Url::fromPath($urlString); + if (UrlMigrator::isSupportedUrl($url)) { + try { + $urlString = rawurldecode(UrlMigrator::transformUrl($url)->getAbsoluteUrl()); + } catch (Exception $e) { + $errors[$urlString] = [ + IcingaException::describe($e), + IcingaException::getConfidentialTraceAsString($e) + ]; + $urlString = false; + } + } + + $result[] = $urlString; + } + + $response = $this->getResponse()->json(); + if (empty($errors)) { + $response->setSuccessData($result); + } else { + $response->setFailData([ + 'result' => $result, + 'errors' => $errors + ]); + } + + $response->sendResponse(); + } + + public function searchUrlAction() + { + $this->assertHttpMethod('post'); + if (! $this->getRequest()->isApiRequest()) { + $this->httpBadRequest('No API request'); + } + + if ( + ! preg_match('/([^;]*);?/', $this->getRequest()->getHeader('Content-Type'), $matches) + || $matches[1] !== 'application/json' + ) { + $this->httpBadRequest('No JSON content'); + } + + $urls = $this->getRequest()->getPost(); + + $result = []; + foreach ($urls as $urlString) { + $url = Url::fromPath($urlString); + $params = $url->onlyWith(['sort', 'limit', 'view', 'columns', 'page'])->getParams(); + $filter = $url->without(['sort', 'limit', 'view', 'columns', 'page'])->getParams(); + $filter = QueryString::parse((string) $filter); + $filter = UrlMigrator::transformLegacyWildcardFilter($filter); + $result[] = rawurldecode($url->setParams($params)->setFilter($filter)->getAbsoluteUrl()); + } + + $response = $this->getResponse()->json(); + $response->setSuccessData($result); + + $response->sendResponse(); + } + + public function checkboxStateAction() + { + $this->assertHttpMethod('get'); + + $form = new SetAsBackendForm(); + $form->setAction(Url::fromPath('icingadb/migrate/checkbox-submit')->getAbsoluteUrl()); + + $this->getDocument()->addHtml($form); + } + + public function checkboxSubmitAction() + { + $this->assertHttpMethod('post'); + $this->addPart(HtmlString::create('"bogus"'), 'Behavior:Migrate'); + + (new SetAsBackendForm())->handleRequest(ServerRequest::fromGlobals()); + } + + public function backendSupportAction() + { + $this->assertHttpMethod('post'); + if (! $this->getRequest()->isApiRequest()) { + $this->httpBadRequest('No API request'); + } + + if ( + ! preg_match('/([^;]*);?/', $this->getRequest()->getHeader('Content-Type'), $matches) + || $matches[1] !== 'application/json' + ) { + $this->httpBadRequest('No JSON content'); + } + + $moduleSupportStates = []; + if ( + Icinga::app()->getModuleManager()->hasEnabled('monitoring') + && $this->Auth()->hasPermission('module/monitoring') + ) { + $supportList = []; + foreach (Hook::all('Icingadb/IcingadbSupport') as $hook) { + /** @var IcingadbSupportHook $hook */ + $supportList[$hook->getModule()->getName()] = $hook->supportsIcingaDb(); + } + + $moduleSupportStates = []; + foreach ($this->getRequest()->getPost() as $moduleName) { + if (isset($supportList[$moduleName])) { + $moduleSupportStates[] = $supportList[$moduleName]; + } else { + $moduleSupportStates[] = false; + } + } + } + + $this->getResponse() + ->json() + ->setSuccessData($moduleSupportStates) + ->sendResponse(); + } +} -- cgit v1.2.3