blob: 538553eb81fdf2168a97b01d27938b6ac67a537d (
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
170
171
172
173
174
175
176
177
178
179
180
181
|
<?php
/* Icinga Web 2 | (c) 2016 Icinga Development Team | GPLv2+ */
namespace Icinga\Module\Eventdb;
use Icinga\Application\Icinga;
use Icinga\Data\QueryInterface;
use Icinga\Exception\IcingaException;
use Icinga\Exception\Json\JsonEncodeException;
use Icinga\Module\Monitoring\Backend\MonitoringBackend;
use Icinga\Web\Controller;
use Icinga\Web\View;
class EventdbController extends Controller
{
/** @var View */
public $view;
/** @var MonitoringBackend */
protected $monitoringBackend;
/**
* Get the EventDB repository
*
* @return Eventdb
*/
protected function getDb()
{
return Eventdb::fromConfig();
}
/**
* {@inheritdoc}
*/
public function getRestrictions($name, $permission = null)
{
$restrictions = array();
if ($this->Auth()->isAuthenticated()) {
foreach ($this->Auth()->getUser()->getRoles() as $role) {
if ($permission !== null && ! in_array($permission, $role->getPermissions())) {
continue;
}
$restrictionsFromRole = $role->getRestrictions($name);
if (empty($restrictionsFromRole)) {
$restrictions = array();
break;
} else {
if (! is_array($restrictionsFromRole)) {
$restrictionsFromRole = array($restrictionsFromRole);
}
$restrictions = array_merge($restrictions, array_values($restrictionsFromRole));
}
}
}
return $restrictions;
}
/**
* Retrieves the Icinga MonitoringBackend
*
* @param string|null $name
*
* @return MonitoringBackend
* @throws IcingaException When monitoring is not enabled
*/
protected function monitoringBackend($name = null)
{
if ($this->monitoringBackend === null) {
if (! Icinga::app()->getModuleManager()->hasEnabled('monitoring')) {
throw new IcingaException('The module "monitoring" must be enabled and configured!');
}
$this->monitoringBackend = MonitoringBackend::instance($name);
}
return $this->monitoringBackend;
}
protected function setViewScript($name)
{
$this->_helper->viewRenderer->setNoController(true);
$this->_helper->viewRenderer->setScriptAction($name);
}
protected function isFormatRequest()
{
return $this->hasParam('format');
}
protected function isApiRequest()
{
$format = $this->getParam('format');
$header = $this->getRequest()->getHeader('Accept');
if ($format === 'json' || preg_match('#application/json(;.+)?#', $header)) {
return true;
} else {
return false;
}
}
protected function isTextRequest()
{
$format = $this->getParam('format');
if ($format === 'text' || $this->isPlainTextRequest()) {
return true;
} else {
return false;
}
}
protected function isPlainTextRequest()
{
$header = $this->getRequest()->getHeader('Accept');
if ($header !== null && preg_match('#text/plain#', $header)) {
return true;
} else {
return false;
}
}
/**
* Send the user a summary of SQL queries
*
* @param array|QueryInterface $queries
*/
protected function sendSqlSummary($queries)
{
if (! is_array($queries)) {
$queries = array($queries);
}
$str = '';
foreach ($queries as $query) {
if ($query !== null) {
$str .= wordwrap($query) . "\n\n";
}
}
$this->sendText($str);
}
/**
* Output JSON data to the requester
*
* @param mixed $data
* @param int $options
* @param int $depth
*
* @throws JsonEncodeException
*/
protected function sendJson($data, $options = 0, $depth = 100)
{
header('Content-Type: application/json');
if (defined('JSON_PARTIAL_OUTPUT_ON_ERROR')) {
$options |= JSON_PARTIAL_OUTPUT_ON_ERROR;
}
$output = json_encode($data, $options, $depth);
if (! $output && json_last_error() !== null) {
throw new JsonEncodeException('JSON error: ' . json_last_error_msg());
}
echo $output;
exit;
}
protected function sendText($str, $script = null)
{
if ($this->isPlainTextRequest()) {
if ($script !== null) {
echo $this->view->render($this->getViewScript($script, true));
} else {
echo $str;
}
exit;
} else {
$this->view->text = $str;
$this->view->partial = $script;
$this->setViewScript('format/text');
}
}
}
|