summaryrefslogtreecommitdiffstats
path: root/application/controllers/MigrateController.php
blob: c14eef7e6b76d5daa2606ecfdc1529b5320babb8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php

/* Icinga DB Web | (c) 2020 Icinga GmbH | GPLv2 */

namespace Icinga\Module\Icingadb\Controllers;

use Exception;
use GuzzleHttp\Psr7\ServerRequest;
use Icinga\Application\Hook;
use Icinga\Application\Icinga;
use Icinga\Exception\IcingaException;
use Icinga\Module\Icingadb\Compat\UrlMigrator;
use Icinga\Module\Icingadb\Forms\SetAsBackendForm;
use Icinga\Module\Icingadb\Hook\IcingadbSupportHook;
use Icinga\Module\Icingadb\Web\Controller;
use ipl\Html\HtmlString;
use ipl\Stdlib\Filter;
use ipl\Web\Filter\QueryString;
use ipl\Web\Url;

class MigrateController extends Controller
{
    public function monitoringUrlAction()
    {
        $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 = [];
        $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);
            $nonStrictOriginalFilter = QueryString::render($filter);
            $filter = UrlMigrator::transformLegacyWildcardFilter($filter);
            $nonStrictUpdatedFilter = QueryString::render($filter);
            if ($nonStrictUpdatedFilter !== $nonStrictOriginalFilter) {
                // The original filter might be formatted loosely, so if we render it again,
                // it might look different although nothing changed
                $result[] = rawurldecode($url->setParams($params)->setFilter($filter)->getAbsoluteUrl());
            } else {
                $result[] = $urlString;
            }
        }

        $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();
    }
}