-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDaemonController.php
More file actions
107 lines (89 loc) · 3.6 KB
/
Copy pathDaemonController.php
File metadata and controls
107 lines (89 loc) · 3.6 KB
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
<?php
// SPDX-FileCopyrightText: 2024 Icinga GmbH <https://icinga.com>
// SPDX-License-Identifier: GPL-3.0-or-later
namespace Icinga\Module\Notifications\Controllers;
use Icinga\Application\Icinga;
use ipl\Web\Compat\CompatController;
use ipl\Web\Compat\ViewRenderer;
use Zend_Layout;
class DaemonController extends CompatController
{
protected $requiresAuthentication = false;
public function init(): void
{
/*
* Initialize the controller and disable the view renderer and layout as this controller provides no
* graphical output
*/
/** @var ViewRenderer $viewRenderer */
$viewRenderer = $this->getHelper('viewRenderer');
$viewRenderer->setNoRender();
/** @var Zend_Layout $layout */
$layout = $this->getHelper('layout');
$layout->disableLayout();
}
public function scriptAction(): void
{
/**
* we have to use `getRequest()->getParam` here instead of the usual `$this->param` as the required parameters
* are not submitted by an HTTP request but injected manually {@see icinga-notifications-web/run.php}
*/
$fileName = $this->getRequest()->getParam('file', 'undefined');
$extension = $this->getRequest()->getParam('extension', 'undefined');
$mime = '';
switch ($extension) {
case 'undefined':
$this->httpNotFound(t("File extension is missing."));
// no return
case '.js':
$mime = 'application/javascript';
break;
case '.js.map':
$mime = 'application/json';
break;
}
$root = Icinga::app()
->getModuleManager()
->getModule('notifications')
->getBaseDir() . '/public/js';
$filePath = realpath($root . DIRECTORY_SEPARATOR . 'notifications-' . $fileName . $extension);
if ($filePath === false || substr($filePath, 0, strlen($root)) !== $root) {
if ($fileName === 'undefined') {
$this->httpNotFound(t("No file name submitted"));
}
$this->httpNotFound(sprintf(t("notifications-%s%s does not exist"), $fileName, $extension));
} else {
$fileStat = stat($filePath);
if ($fileStat) {
$eTag = sprintf(
'%x-%x-%x',
$fileStat['ino'],
$fileStat['size'],
(float) str_pad((string) ($fileStat['mtime']), 16, '0')
);
$this->getResponse()->setHeader(
'Cache-Control',
'public, max-age=1814400, stale-while-revalidate=604800',
true
);
if ($this->getRequest()->getServer('HTTP_IF_NONE_MATCH') === $eTag) {
$this->getResponse()->setHttpResponseCode(304);
} else {
$this->getResponse()
->setHeader('ETag', $eTag)
->setHeader('Content-Type', $mime, true)
->setHeader(
'Last-Modified',
gmdate('D, d M Y H:i:s', $fileStat['mtime']) . ' GMT'
);
$file = file_get_contents($filePath);
if ($file) {
$this->getResponse()->setBody($file);
}
}
} else {
$this->httpNotFound(sprintf(t("notifications-%s%s could not be read"), $fileName, $extension));
}
}
}
}