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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
|
<?php
class Page_Statistics extends Page
{
private $show;
/**
* @var bool whether we have a SubPage from the pages/ subdir
*/
private $haveSubpage;
protected function doPreprocess()
{
User::load();
if (!User::isLoggedIn()) {
Message::addError('main.no-permission');
Util::redirect('?do=Main');
}
if (Request::isGet()) {
$this->transformLegacyQuery();
}
/*
Dictionary::translate('submenu_projectors');
Dictionary::translate('submenu_replace');
Dictionary::translate('submenu_hints');
*/
foreach (['projectors', 'replace', 'hints'] as $section) {
Dashboard::addSubmenu('?do=statistics&show=' . $section,
Dictionary::translate('submenu_' . $section));
}
$this->show = Request::any('show', false, 'string');
if ($this->show === false && Request::isGet()) {
if (Request::get('uuid') !== false) {
$this->show = 'machine';
} elseif (User::hasPermission('view.summary')) {
$this->show = 'summary';
} elseif (User::hasPermission('view.list')) {
$this->show = 'list';
} else {
User::assertPermission('view.summary');
}
}
if ($this->show !== false) {
$this->show = preg_replace('/[^a-z0-9_\-]/', '', $this->show);
if (!file_exists('modules/statistics/pages/' . $this->show . '.inc.php')) {
Message::addError('main.invalid-action', $this->show);
} else {
require_once 'modules/statistics/pages/' . $this->show . '.inc.php';
$this->haveSubpage = true;
SubPage::doPreprocess();
}
return;
}
if (!Request::isPost())
return;
// POST
$action = Request::post('action');
if ($action === 'setnotes') {
$uuid = Request::post('uuid', '', 'string');
$res = Database::queryFirst('SELECT locationid FROM machine WHERE machineuuid = :uuid',
array('uuid' => $uuid));
if ($res === false) {
Message::addError('unknown-machine', $uuid);
Util::redirect('?do=statistics');
}
User::assertPermission("machine.note.edit", (int)$res['locationid']);
$text = Request::post('content', null, 'string');
if (empty($text)) {
$text = null;
}
Database::exec('UPDATE machine SET notes = :text WHERE machineuuid = :uuid', array(
'uuid' => $uuid,
'text' => $text,
));
Message::addSuccess('notes-saved');
Util::redirect('?do=statistics&uuid=' . $uuid);
} elseif ($action === 'clear-machines') {
$this->deleteMachines(true);
} elseif ($action === 'delmachines') {
$this->deleteMachines(false);
Util::redirect('?do=statistics', true);
} elseif ($action === 'rebootmachines') {
$this->rebootControl(true);
} elseif ($action === 'shutdownmachines') {
$this->rebootControl(false);
} elseif ($action === 'wol') {
$this->wol();
} elseif ($action === 'benchmark') {
$this->vmstoreBenchmark();
} elseif ($action === 'prepare-exec') {
if (Module::isAvailable('rebootcontrol')) {
RebootControl::prepareExec();
}
}
// Make sure we don't render any content for POST requests - should be handled above and then
// redirected properly
Util::redirect('?do=statistics');
}
private function transformLegacyQuery()
{
if (!Request::isGet())
return;
$query = Request::get('filters', false, 'string');
if ($query === false)
return;
foreach (explode(StatisticsFilter::LEGACY_DELIMITER, $query) as $q) {
if (!preg_match('/^\s*(\w+)\s*([<>=!~]{1,2})\s*(.*?)\s*$/', $q, $out))
continue;
$key = $out[1];
$_GET['filter'][$key] = '1';
$_GET['op'][$key] = $out[2];
$_GET['arg'][$key] = $out[3];
}
unset($_GET['filters']);
if (!empty($_GET['filter'])) {
Util::redirect('?' . http_build_query($_GET));
}
}
private function wol()
{
if (!Module::isAvailable('rebootcontrol'))
return;
$ids = Request::post('uuid', [], 'array');
$ids = array_values($ids);
if (empty($ids)) {
Message::addError('main.parameter-empty', 'uuid');
return;
}
$this->getAllowedMachines(".rebootcontrol.action.wol", $ids, $allowedMachines);
if (empty($allowedMachines))
return;
$taskid = RebootControl::wakeMachines($allowedMachines);
Util::redirect('?do=rebootcontrol&show=task&what=task&taskid=' . $taskid);
}
/**
* @param bool $reboot true = reboot, false = shutdown
*/
private function rebootControl(bool $reboot)
{
if (!Module::isAvailable('rebootcontrol'))
return;
$ids = Request::post('uuid', [], 'array');
$ids = array_values($ids);
if (empty($ids)) {
Message::addError('main.parameter-empty', 'uuid');
return;
}
$this->getAllowedMachines(".rebootcontrol.action." . ($reboot ? 'reboot' : 'shutdown'), $ids, $allowedMachines);
if (empty($allowedMachines))
return;
if ($reboot && Request::post('kexec', false)) {
$action = RebootControl::KEXEC_REBOOT;
} elseif ($reboot) {
$action = RebootControl::REBOOT;
} else {
$action = RebootControl::SHUTDOWN;
}
$task = RebootControl::execute($allowedMachines, $action, 0);
if (Taskmanager::isTask($task)) {
Util::redirect("?do=rebootcontrol&show=task&what=task&taskid=" . $task["id"]);
}
}
/**
* @param bool $reboot true = reboot, false = shutdown
*/
private function vmstoreBenchmark()
{
if (!Module::isAvailable('vmstore'))
return;
$ids = Request::post('uuid', [], 'array');
$ids = array_values($ids);
if (empty($ids)) {
Message::addError('main.parameter-empty', 'uuid');
return;
}
$this->getAllowedMachines(".vmstore.benchmark", $ids, $allowedMachines);
if (empty($allowedMachines))
return;
VmStoreBenchmark::prepareSelectDialog($allowedMachines);
}
private function getAllowedMachines($permission, $ids, &$allowedMachines)
{
$allowedLocations = User::getAllowedLocations($permission);
if (empty($allowedLocations)) {
Message::addError('main.no-permission');
Util::redirect('?do=statistics');
}
$res = Database::simpleQuery('SELECT machineuuid, clientip, macaddr, locationid FROM machine
WHERE machineuuid IN (:ids)', compact('ids'));
$ids = array_flip($ids);
$allowedMachines = [];
$seenLocations = [];
foreach ($res as $row) {
unset($ids[$row['machineuuid']]);
settype($row['locationid'], 'int');
if (in_array($row['locationid'], $allowedLocations)) {
$allowedMachines[] = $row;
} elseif (!isset($seenLocations[$row['locationid']])) {
Message::addError('locations.no-permission-location', $row['locationid']);
}
$seenLocations[$row['locationid']] = true;
}
if (!empty($ids)) {
Message::addWarning('unknown-machine', implode(', ', array_keys($ids)));
}
}
private function deleteMachines($soft)
{
$ids = Request::post('uuid', [], 'array');
$ids = array_values($ids);
if (empty($ids)) {
Message::addError('main.parameter-empty', 'uuid');
return;
}
$allowedLocations = User::getAllowedLocations("machine.delete");
if (empty($allowedLocations)) {
Message::addError('main.no-permission');
Util::redirect('?do=statistics');
}
$res = Database::simpleQuery('SELECT machineuuid, locationid FROM machine WHERE machineuuid IN (:ids)', compact('ids'));
$ids = array_flip($ids);
$delete = [];
foreach ($res as $row) {
unset($ids[$row['machineuuid']]);
if (in_array($row['locationid'], $allowedLocations)) {
$delete[] = $row['machineuuid'];
} else {
Message::addError('locations.no-permission-location', $row['locationid']);
}
}
if (!empty($delete)) {
if ($soft) {
// "Soft delete" -- keep all data, but set IP address to 0.0.0.0, so it will not be assigned to its
// old location anymore. Upon next boot some time in the future, the machine is hopefully relocated
// to somewhere else and will appear in a new location
Database::exec("UPDATE machine SET clientip = '0.0.0.0', fixedlocationid = NULL, subnetlocationid = NULL
WHERE machineuuid IN (:delete)", compact('delete'));
Message::addSuccess('cleared-n-machines', count($delete));
} else {
// Actually purge from DB
Database::exec('DELETE FROM machine WHERE machineuuid IN (:delete)', compact('delete'));
Message::addSuccess('deleted-n-machines', count($delete));
}
}
if (!empty($ids)) {
Message::addWarning('unknown-machine', implode(', ', array_keys($ids)));
}
}
protected function doRender()
{
if ($this->haveSubpage) {
SubPage::doRender();
return;
}
Message::addError('main.value-invalid', 'show', $this->show);
}
protected function doAjax()
{
if (!User::load())
return;
$action = Request::any('action');
if ($action === 'bios') {
require_once 'modules/statistics/pages/machine.inc.php';
SubPage::ajaxCheckBios();
return;
}
if ($action === 'json-lookup') {
$reply = [];
foreach (Request::post('list', [], 'array') as $item) {
$name = PciId::getPciId(PciId::AUTO, $item, true);
if ($name === false) {
$name = '?????';
}
$reply[$item] = $name;
}
header('Content-Type: application/json');
die(json_encode($reply));
}
$param = Request::any('lookup', false, 'string');
if ($param === false) {
die('No lookup given');
}
$add = '';
if (preg_match('/^([a-f0-9]{4}):([a-f0-9]{4})$/', $param, $out)) {
$add = ' (' . $param . ')';
}
$cached = PciId::getPciId(PciId::AUTO, $param, true);
if ($cached === false) {
$cached = 'Unknown';
}
echo $cached, $add;
exit;
}
}
|