summaryrefslogtreecommitdiffstats
path: root/modules-available/rebootcontrol/pages/subnet.inc.php
blob: 946d2d64e260cdbc730b0111d9ba821c6efeb18d (plain) (blame)
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
<?php

class SubPage
{

	public static function doPreprocess()
	{
		$action = Request::post('action', false, 'string');
		if ($action === 'add') {
			self::addSubnet();
		} elseif ($action === 'edit') {
			self::editSubnet();
		}
	}

	/*
	 * POST
	 */

	private static function addSubnet()
	{
		User::assertPermission('subnet.edit');
		$range = [];
		foreach (['start', 'end'] as $key) {
			$range[$key] = Request::post($key, Request::REQUIRED, 'string');
			$range[$key  . '_l'] = ip2long($range[$key]);
			if ($range[$key  . '_l'] === false) {
				Message::addError('invalid-ip-address', $range[$key]);
				return;
			}
		}
		if ($range['start_l'] > $range['end_l']) {
			Message::addError('invalid-range', $range['start'], $range['end']);
			return;
		}
		$ret = Database::exec('INSERT INTO reboot_subnet (start, end, fixed, isdirect)
				VALUES (:start, :end, 1, 0)', [
			'start' => sprintf('%u', $range['start_l']),
			'end' => sprintf('%u', $range['end_l']),
			], true);
		if ($ret === false) {
			Message::addError('subnet-already-exists');
		} else {
			Message::addSuccess('subnet-created');
			Util::redirect('?do=rebootcontrol&show=subnet&what=subnet&id=' . Database::lastInsertId());
		}
	}

	private static function editSubnet()
	{
		User::assertPermission('subnet.flag');
		$id = Request::post('id', Request::REQUIRED, 'int');
		$subnet = Database::queryFirst('SELECT subnetid
				FROM reboot_subnet WHERE subnetid = :id', ['id' => $id]);
		if ($subnet === false) {
			Message::addError('invalid-subnet', $id);
			return;
		}
		$params = [
			'id' => $id,
			'fixed' => !empty(Request::post('fixed', false, 'string')),
			'isdirect' => !empty(Request::post('isdirect', false, 'string')),
		];
		Database::exec('UPDATE reboot_subnet SET fixed = :fixed, isdirect = If(:fixed, :isdirect, isdirect)
				WHERE subnetid = :id', $params);
		if (User::hasPermission('jumphost.assign-subnet')) {
			$hosts = Request::post('jumphost', [], 'array');
			if (!empty($hosts)) {
				$hosts = array_keys($hosts);
				Database::exec('DELETE FROM reboot_jumphost_x_subnet WHERE subnetid = :id AND hostid NOT IN (:hosts)',
					['id' => $id, 'hosts' => $hosts]);
				$hosts = array_map(function($item) use ($id) {
					return [$item, $id];
				}, $hosts);
				Database::exec('INSERT IGNORE INTO reboot_jumphost_x_subnet (hostid, subnetid) VALUES :hosts', ['hosts' => $hosts]);
			}
		}
		Message::addSuccess('subnet-updated');
	}

	/*
	 * Render
	 */

	public static function doRender()
	{
		$what = Request::get('what', 'list', 'string');
		if ($what === 'list') {
			self::showSubnets();
		} elseif ($what === 'subnet') {
			self::showSubnet();
		}
	}

	private static function showSubnets()
	{
		User::assertPermission('subnet.*');
		$nets = [];
		$res = Database::simpleQuery('SELECT subnetid, start, end, fixed, isdirect,
       		lastdirectcheck, lastseen, seencount, Count(hxs.hostid) AS jumphostcount
				FROM reboot_subnet
				LEFT JOIN reboot_jumphost_x_subnet hxs USING (subnetid)
				GROUP BY subnetid, start, end
				ORDER BY start ASC, end DESC');
		$deadline = strtotime('-60 days');
		while ($row = $res->fetch(PDO::FETCH_ASSOC)) {
			$row['start_s'] = long2ip($row['start']);
			$row['end_s'] = long2ip($row['end']);
			$row['lastseen_s'] = Util::prettyTime($row['lastseen']);
			if ($row['lastseen'] && $row['lastseen'] < $deadline) {
				$row['lastseen_class'] = 'text-danger';
			}
			$nets[] = $row;
		}
		$data = ['subnets' => $nets];
		Render::addTemplate('subnet-list', $data);
		Module::isAvailable('js_ip');
	}

	private static function showSubnet()
	{
		User::assertPermission('subnet.*');
		$id = Request::get('id', Request::REQUIRED, 'int');
		$subnet = Database::queryFirst('SELECT subnetid, start, end, fixed, isdirect
				FROM reboot_subnet WHERE subnetid = :id', ['id' => $id]);
		if ($subnet === false) {
			Message::addError('invalid-subnet', $id);
			return;
		}
		$subnet['start_s'] = long2ip($subnet['start']);
		$subnet['end_s'] = long2ip($subnet['end']);
		$res = Database::simpleQuery('SELECT h.hostid, h.host, h.port, hxs.subnetid FROM reboot_jumphost h
				LEFT JOIN reboot_jumphost_x_subnet hxs ON (h.hostid = hxs.hostid AND hxs.subnetid = :id)
				ORDER BY h.host ASC', ['id' => $id]);
		$jh = [];
		while ($row = $res->fetch(PDO::FETCH_ASSOC)) {
			$row['checked'] = $row['subnetid'] === null ? '' : 'checked';
			$jh[] = $row;
		}
		$subnet['jumpHosts'] = $jh;
		Permission::addGlobalTags($subnet['perms'], null, ['subnet.flag', 'jumphost.view', 'jumphost.assign-subnet']);
		Render::addTemplate('subnet-edit', $subnet);
	}

	public static function doAjax()
	{

	}

}