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
|
<?php
class Page_Roomplanner extends Page
{
protected function doPreprocess()
{
User::load();
if (!User::hasPermission('superadmin')) {
Message::addError('main.no-permission');
Util::redirect('?do=Main');
}
}
protected function doRender()
{
$locationid = Request::get('locationid', null, 'integer');
if ($locationid === null) {
die('please specify locationid');
}
$subnetMachines = $this->getPotentialMachines($locationid);
Render::addTemplate('page', ['subnetMachines' => json_encode($subnetMachines),
]);
}
protected function doAjax()
{
$action = Request::get('action', null, 'string');
if ($action === 'getmachines') {
$query = Request::get('query', null, 'string');
/* the query could be anything: UUID, IP or macaddr */
// $result = Database::simpleQuery('SELECT machineuuid, macaddr, clientip, hostname '
// . ', MATCH (machineuuid, macaddr, clientip, hostname) AGAINST (:query) AS relevance '
// . 'FROM machine '
// . 'WHERE MATCH (machineuuid, macaddr, clientip, hostname) AGAINST (:query) '
// . 'ORDER BY relevance DESC '
// . 'LIMIT 5'
// , ['query' => $query]);
//
$result = Database::simpleQuery('SELECT machineuuid, macaddr, clientip, hostname '
.'FROM machine '
.'WHERE machineuuid LIKE :query '
.' OR macaddr LIKE :query '
.' OR clientip LIKE :query '
.' OR hostname LIKE :query ', ['query' => "%$query%"]);
$returnObject = ['machines' => []];
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
$returnObject['machines'][] = $row;
}
echo json_encode($returnObject, JSON_PRETTY_PRINT);
}
}
protected function getPotentialMachines($locationid)
{
$result = Database::simpleQuery('SELECT machineuuid, macaddr, clientip, hostname '
.'FROM machine INNER JOIN subnet ON (INET_ATON(clientip) BETWEEN startaddr AND endaddr) '
.'WHERE subnet.locationid = :locationid', ['locationid' => $locationid]);
$machines = [];
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
$row['combined'] = implode(' ', array_values($row));
$machines[] = $row;
}
return $machines;
}
}
|