Passed
Push — master ( 4d291c...0712fb )
by Marcel
05:24
created

PageController::report()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 29
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 19
nc 3
nop 0
dl 0
loc 29
rs 9.6333
c 0
b 0
f 0
1
<?php
2
/**
3
 * Analytics
4
 *
5
 * SPDX-FileCopyrightText: 2019-2022 Marcel Scherello
6
 * SPDX-License-Identifier: AGPL-3.0-or-later
7
 */
8
9
namespace OCA\Analytics\Controller;
10
11
use OCA\Analytics\DataSession;
12
use OCA\Analytics\Service\ShareService;
13
use OCP\App\IAppManager;
0 ignored issues
show
Bug introduced by
The type OCP\App\IAppManager was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use OCP\AppFramework\Controller;
0 ignored issues
show
Bug introduced by
The type OCP\AppFramework\Controller was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use OCP\AppFramework\Http\ContentSecurityPolicy;
0 ignored issues
show
Bug introduced by
The type OCP\AppFramework\Http\ContentSecurityPolicy was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use OCP\AppFramework\Http\StandaloneTemplateResponse;
0 ignored issues
show
Bug introduced by
The type OCP\AppFramework\Http\StandaloneTemplateResponse was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use OCP\AppFramework\Http\Template\PublicTemplateResponse;
0 ignored issues
show
Bug introduced by
The type OCP\AppFramework\Http\Te...\PublicTemplateResponse was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
use OCP\AppFramework\Services\IInitialState;
0 ignored issues
show
Bug introduced by
The type OCP\AppFramework\Services\IInitialState was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
use OCP\AppFramework\Http\RedirectResponse;
0 ignored issues
show
Bug introduced by
The type OCP\AppFramework\Http\RedirectResponse was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
20
use OCP\AppFramework\Http\TemplateResponse;
0 ignored issues
show
Bug introduced by
The type OCP\AppFramework\Http\TemplateResponse was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
21
use OCP\IConfig;
0 ignored issues
show
Bug introduced by
The type OCP\IConfig was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
22
use OCP\IRequest;
0 ignored issues
show
Bug introduced by
The type OCP\IRequest was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
use OCP\IURLGenerator;
0 ignored issues
show
Bug introduced by
The type OCP\IURLGenerator was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
24
use OCP\IUserSession;
0 ignored issues
show
Bug introduced by
The type OCP\IUserSession was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
25
use Psr\Log\LoggerInterface;
0 ignored issues
show
Bug introduced by
The type Psr\Log\LoggerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
26
use OCA\Text\Event\LoadEditor;
0 ignored issues
show
Bug introduced by
The type OCA\Text\Event\LoadEditor was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
27
use OCP\EventDispatcher\IEventDispatcher;
0 ignored issues
show
Bug introduced by
The type OCP\EventDispatcher\IEventDispatcher was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
28
29
/**
30
 * Controller class for main page.
31
 */
32
class PageController extends Controller
33
{
34
    /** @var IConfig */
35
    protected $config;
36
    /** @var IUserSession */
37
    private $userSession;
38
    private $logger;
39
    /** @var IURLGenerator */
40
    private $urlGenerator;
41
    /** @var DataSession */
42
    private $DataSession;
43
    /** @var ShareService */
44
    private $ShareService;
45
    /** @var OutputController */
46
    private $outputController;
47
    /** @var IInitialState */
48
    protected $initialState;
49
    private IEventDispatcher $eventDispatcher;
50
	/** @var IAppManager */
51
	private $appManager;
52
53
    public function __construct(
54
        string $appName,
55
        IRequest $request,
56
        LoggerInterface $logger,
57
        IURLGenerator $urlGenerator,
58
        ShareService $ShareService,
59
        IUserSession $userSession,
60
        IConfig $config,
61
        DataSession $DataSession,
62
        IInitialState $initialState,
63
        OutputController $outputController,
64
        IEventDispatcher $eventDispatcher,
65
		IAppManager $appManager
66
    )
67
    {
68
        parent::__construct($appName, $request);
69
        $this->logger = $logger;
70
        $this->urlGenerator = $urlGenerator;
71
        $this->ShareService = $ShareService;
72
        $this->config = $config;
73
        $this->userSession = $userSession;
74
        $this->DataSession = $DataSession;
75
        $this->initialState = $initialState;
76
        $this->outputController = $outputController;
77
        $this->eventDispatcher = $eventDispatcher;
78
		$this->appManager = $appManager;
79
    }
80
81
    /**
82
     * @NoAdminRequired
83
     * @NoCSRFRequired
84
     */
85
    public function report()
86
    {
87
        $params = array();
88
        $params['token'] = '';
89
        $user = $this->userSession->getUser();
90
91
        $this->initialState->provideInitialState(
92
            'wizard',
93
            $this->config->getUserValue($user->getUID(), 'analytics', 'wizzard', 0)
94
        );
95
96
        try {
97
            $translationAvailable = \OCP\Server::get(\OCP\Translation\ITranslationManager::class)->hasProviders();
0 ignored issues
show
Bug introduced by
The type OCP\Translation\ITranslationManager was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Bug introduced by
The type OCP\Server was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
98
            $translationLanguages = \OCP\Server::get(\OCP\Translation\ITranslationManager::class)->getLanguages();
99
        } catch (\Exception $e) {
100
            $translationAvailable = false;
101
            $translationLanguages = false;
102
        }
103
104
        $this->initialState->provideInitialState(
105
            'translationAvailable',
106
            $translationAvailable
107
        );
108
        $this->initialState->provideInitialState(
109
            'translationLanguages',
110
            $translationLanguages
111
        );
112
113
        return new TemplateResponse($this->appName, 'main', $params);
114
    }
115
116
    /**
117
     * @NoAdminRequired
118
     * @NoCSRFRequired
119
     */
120
    public function advanced()
121
    {
122
		$params = array();
123
124
		$this->initialState->provideInitialState(
125
			'contextChatAvailable',
126
			$this->appManager->isEnabledForUser('context_chat')
127
		);
128
129
        return new TemplateResponse($this->appName, 'main_advanced', $params);
130
    }
131
132
    /**
133
     * @NoAdminRequired
134
     * @NoCSRFRequired
135
     */
136
    public function panorama()
137
    {
138
        if (class_exists(LoadEditor::class)) {
139
            $this->eventDispatcher->dispatchTyped(new LoadEditor());
140
        }
141
        return new TemplateResponse($this->appName, 'main_panorama');
142
    }
143
144
    /**
145
     * @PublicPage
146
     * @NoCSRFRequired
147
     * @UseSession
148
     *
149
     * @param string $token
150
     * @param string $password
151
     * @return RedirectResponse|TemplateResponse
152
     */
153
    public function authenticatePassword(string $token, string $password = '')
154
    {
155
        return $this->indexPublic($token, $password);
156
    }
157
158
    /**
159
     * @PublicPage
160
     * @UseSession
161
     * @NoCSRFRequired
162
     * @param $token
163
     * @param string $password
164
     * @return TemplateResponse|RedirectResponse
165
     */
166
    public function indexPublic($token, string $password = '')
167
    {
168
        $share = $this->ShareService->getReportByToken($token);
169
170
        if (empty($share)) {
171
            // Dataset not shared or wrong token
172
            return new RedirectResponse($this->urlGenerator->linkToRoute('core.login.showLoginForm', [
173
                'redirect_url' => $this->urlGenerator->linkToRoute($this->appName . '.page.index', ['token' => $token]),
174
            ]));
175
        } else {
176
            if ($share['password'] !== null) {
177
                $password = $password !== '' ? $password : (string)$this->DataSession->getPasswordForShare($token);
178
                $passwordVerification = $this->ShareService->verifyPassword($password, $share['password']);
179
                if ($passwordVerification === true) {
180
                    $this->DataSession->setPasswordForShare($token, $password);
181
                } else {
182
                    $this->DataSession->removePasswordForShare($token);
183
                    return new TemplateResponse($this->appName, 'authenticate', ['wrongpw' => $password !== '',], 'guest');
184
                }
185
            }
186
            $params = array();
187
            $params['token'] = $token;
188
            $response = new PublicTemplateResponse($this->appName, 'public', $params);
189
            $response->setHeaderTitle('Nextcloud Analytics');
190
            $response->setFooterVisible(false);
191
            return $response;
192
        }
193
    }
194
195
    /**
196
     * @PublicPage
197
     * @UseSession
198
     * @NoCSRFRequired
199
     * @param $token
200
     * @param string $password
201
     * @return TemplateResponse|RedirectResponse
202
     */
203
    public function indexPublicMin($token, string $password = '')
204
    {
205
        $share = $this->ShareService->getReportByToken($token);
206
207
        if (empty($share)) {
208
            // Dataset not shared or wrong token
209
            return new RedirectResponse($this->urlGenerator->linkToRoute('core.login.showLoginForm', [
210
                'redirect_url' => $this->urlGenerator->linkToRoute($this->appName . '.page.index', ['token' => $token]),
211
            ]));
212
        } else {
213
            if ($share['password'] !== null) {
214
                $password = $password !== '' ? $password : (string)$this->DataSession->getPasswordForShare($token);
215
                $passwordVerification = $this->ShareService->verifyPassword($password, $share['password']);
216
                if ($passwordVerification === true) {
217
                    $this->DataSession->setPasswordForShare($token, $password);
218
                } else {
219
                    $this->DataSession->removePasswordForShare($token);
220
                    return new TemplateResponse($this->appName, 'authenticate', ['wrongpw' => $password !== '',], 'guest');
221
                }
222
            }
223
            $params = array();
224
            $params['data'] = $this->outputController->getData($share);
225
            $params['baseurl'] = str_replace('/img/app.svg', '', $this->urlGenerator->imagePath('analytics', 'app.svg'));
226
            $params['nonce'] = \OC::$server->getContentSecurityPolicyNonceManager()->getNonce();
227
            $response = new StandaloneTemplateResponse($this->appName, 'publicMin', $params, '');
228
            $csp = new ContentSecurityPolicy();
229
            $csp->addAllowedScriptDomain('*');
230
            $csp->addAllowedFrameAncestorDomain($share['domain']);
231
            $response->setContentSecurityPolicy($csp);
232
            return $response;
233
        }
234
    }
235
}