summaryrefslogtreecommitdiffstats
path: root/modules-available/locations/pages/subnets.inc.php
blob: fb1e1e809a3b66b29fe059d82b59751deeb776b9 (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
<?php

class SubPage
{

	public static function doPreprocess($action)
	{
		if ($action === 'updatesubnets') {
			self::updateSubnets();
			return true;
		}
		return false;
	}

	private static function updateSubnets()
	{
		User::assertPermission('subnets.edit', NULL, '?do=locations');
		$editCount = 0;
		$deleteCount = 0;
		$starts = Request::post('startaddr', false);
		$ends = Request::post('endaddr', false);
		$locs = Request::post('location', false);
		if (!is_array($starts) || !is_array($ends) || !is_array($locs)) {
			Message::addError('main.empty-field');
			Util::redirect('?do=Locations');
		}
		$existingLocs = Location::getLocationsAssoc();
		$stmt = Database::prepare("UPDATE subnet SET startaddr = :startLong, endaddr = :endLong, locationid = :loc WHERE subnetid = :subnetid");
		foreach ($starts as $subnetid => $start) {
			if (!isset($ends[$subnetid]) || !isset($locs[$subnetid]))
				continue;
			$loc = (int)$locs[$subnetid];
			$start = trim($start);
			$end = trim($ends[$subnetid]);
			if (empty($start) && empty($end)) {
				$ret = Database::exec('DELETE FROM subnet WHERE subnetid = :subnetid', compact('subnetid'));
				$deleteCount += $ret;
				continue;
			}
			if (!isset($existingLocs[$loc])) {
				Message::addError('main.value-invalid', 'locationid', $loc);
				continue;
			}
			$range = LocationUtil::rangeToLongVerbose($start, $end);
			if ($range === false)
				continue;
			list($startLong, $endLong) = $range;
			if ($stmt->execute(compact('startLong', 'endLong', 'loc', 'subnetid'))) {
				$editCount += $stmt->rowCount();
			}
		}
		AutoLocation::rebuildAll();
		if ($editCount > 0) {
			Message::addSuccess('subnets-updated', $editCount);
		}
		if ($deleteCount > 0) {
			Message::addSuccess('subnets-deleted', $deleteCount);
		}
		Util::redirect('?do=Locations');
	}

	public static function doRender($getAction)
	{
		if ($getAction === false) {
			User::assertPermission('subnets.edit', NULL, '?do=locations');
			$res = Database::simpleQuery("SELECT subnetid, startaddr, endaddr, locationid
					FROM subnet
					ORDER BY startaddr ASC, endaddr DESC");
			$rows = array();
			while ($row = $res->fetch(PDO::FETCH_ASSOC)) {
				$row['startaddr'] = long2ip($row['startaddr']);
				$row['endaddr'] = long2ip($row['endaddr']);
				$row['locations'] = Location::getLocations($row['locationid']);
				$rows[] = $row;
			}
			$data = array('list' => $rows);
			Permission::addGlobalTags($data['perms'], NULL, ['location.view']);
			Render::addTemplate('subnets', $data);
			return true;
		}
		return false;
	}

	public static function doAjax($action)
	{
		return false;
	}

	/*
	 * Helpers
	 */

}