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
|
<?php
class Page_RunMode extends Page
{
/**
* Called before any page rendering happens - early hook to check parameters etc.
*/
protected function doPreprocess()
{
User::load();
if (!User::isLoggedIn()) {
Message::addError('main.no-permission');
Util::redirect('?do=main');
}
$action = Request::post('action', false, 'string');
if ($action !== false) {
$this->handleAction($action);
Util::redirect('?do=runmode');
}
}
private function handleAction($action)
{
if ($action === 'save-mode') {
$machines = array_filter(Request::post('machines', [], 'array'), 'is_string');
$module = Request::post('module', false, 'string');
$modeId = Request::post('modeid', false, 'string');
// TODO Validate
$active = 0;
foreach ($machines as $machine) {
$ret = RunMode::setRunMode($machine, $module, $modeId, null, null);
if ($ret) {
$active++;
} else {
Message::addError('invalid-module-or-machine', $module, $machine);
}
}
$deleted = Database::exec('DELETE FROM runmode
WHERE module = :module AND modeid = :modeId AND machineuuid NOT IN (:machines)',
compact('module', 'modeId', 'machines'));
Message::addError('enabled-removed-save', $active, $deleted);
$redirect = Request::post('redirect', false, 'string');
if ($redirect !== false) {
Util::redirect($redirect);
}
Util::redirect('?do=runmode&module=' . $module . '&modeid=' . $modeId);
} elseif ($action === 'delete-machine') {
$machineuuid = Request::post('machineuuid', false, 'string');
if ($machineuuid === false) {
}
}
}
protected function doRender()
{
$moduleId = Request::get('module', false, 'string');
if ($moduleId !== false) {
$this->renderModule($moduleId);
return;
}
$this->renderClientList(false);
}
private function renderModule($moduleId)
{
$module = Module::get($moduleId);
if ($module === false) {
Message::addError('main.no-such-module', $moduleId);
Util::redirect('?do=runmode');
}
$module->activate();
$config = RunMode::getModuleConfig($moduleId);
if ($config === false) {
Message::addError('module-hasnt-runmode', $moduleId);
Util::redirect('?do=runmode');
}
// Given modeId?
$modeId = Request::get('modeid', false, 'string');
if ($modeId !== false) {
// Show edit page for specific module-mode combo
$this->renderModuleMode($module, $modeId);
return;
}
// Show list of machines with assigned mode for this module
$this->renderClientList($moduleId);
Render::setTitle(Page::getModule()->getDisplayName() . ' – ' . $module->getDisplayName());
}
private function renderClientList($onlyModule)
{
if ($onlyModule === false) {
$where = '';
} else {
$where = ' AND r.module = :moduleId ';
}
$res = Database::simpleQuery("SELECT m.machineuuid, m.hostname, m.clientip, r.module, r.modeid, r.isclient"
. " FROM runmode r"
. " INNER JOIN machine m ON (m.machineuuid = r.machineuuid $where )"
. " ORDER BY m.hostname ASC, m.clientip ASC", array('moduleId' => $onlyModule));
$modules = array();
while ($row = $res->fetch(PDO::FETCH_ASSOC)) {
if (!isset($modules[$row['module']])) {
if (!Module::isAvailable($row['module']))
continue;
$modules[$row['module']] = array('config' => RunMode::getModuleConfig($row['module']), 'list' => array());
}
if (empty($row['hostname'])) {
$row['hostname'] = $row['clientip'];
}
if ($modules[$row['module']]['config']->getModeName !== false) {
$row['mode_name'] = call_user_func($modules[$row['module']]['config']->getModeName, $row['modeid']);
}
$modules[$row['module']]['list'][] = $row;
}
foreach ($modules as $moduleId => $rows) {
$module = Module::get($moduleId);
if ($module === false)
continue;
Render::addTemplate('module-machine-list', array(
'list' => $rows['list'],
'modulename' => $module->getDisplayName(),
'module' => $moduleId,
));
}
}
/**
* @param \Module $module
* @param string $modeId
*/
private function renderModuleMode($module, $modeId)
{
$moduleId = $module->getIdentifier();
$modeName = RunMode::getModeName($moduleId, $modeId);
if ($modeName === false) {
Message::addError('invalid-modeid', $modeId);
Util::redirect('?do=runmode');
}
Render::addTemplate('machine-selector', [
'module' => $moduleId,
'modeid' => $modeId,
'moduleName' => $module->getDisplayName(),
'modeName' => $modeName,
'machines' => json_encode(RunMode::getForMode($module, $modeId, true)),
'redirect' => Request::get('redirect', '', 'string'),
]);
}
protected function doAjax()
{
$action = Request::any('action', false, 'string');
if ($action === 'getmachines') {
$query = Request::get('query', false, 'string');
$result = Database::simpleQuery('SELECT m.machineuuid, m.macaddr, m.clientip, m.hostname, m.locationid, '
. 'r.module, r.modeid '
. 'FROM machine m '
. 'LEFT JOIN runmode r USING (machineuuid) '
. 'WHERE machineuuid LIKE :query '
. ' OR macaddr LIKE :query '
. ' OR clientip LIKE :query '
. ' OR hostname LIKE :query '
. ' LIMIT 100', ['query' => "%$query%"]);
$returnObject = [
'machines' => $result->fetchAll(PDO::FETCH_ASSOC)
];
echo json_encode($returnObject);
}
}
}
|