summaryrefslogtreecommitdiffstats
path: root/modules-available/locations/inc/locationutil.inc.php
blob: 9111744545938bea3811834be5c0e33afa3032cc (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
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
<?php

class LocationUtil
{

	/**
	 * @param array $overlapSelf List of locations which have subnet definitions that overlap with itself
	 * @param array $overlapOther List of location pairs which have overlapping subnets
	 */
	public static function getOverlappingSubnets(&$overlapSelf = false, &$overlapOther = false)
	{
		if ($overlapSelf === false && $overlapOther === false) {
			return;
		}
		$locs = Location::getLocationsAssoc();
		$subnets = Location::getSubnets();
		if ($overlapSelf) {
			$self = array();
		}
		if ($overlapOther) {
			$other = array();
		}
		$cnt = count($subnets);
		for ($i = 0; $i < $cnt; ++$i) {
			for ($j = $i + 1; $j < $cnt; ++$j) {
				if ($overlapSelf && $subnets[$i]['locationid'] === $subnets[$j]['locationid']
					&& self::overlap($subnets[$i], $subnets[$j])
				) {
					$self[$subnets[$i]['locationid']] = $subnets[$i]['locationid'];
				}
				if ($overlapOther && $subnets[$i]['locationid'] !== $subnets[$j]['locationid']
					&& self::overlap($subnets[$i], $subnets[$j])
				) {
					$a = min($subnets[$i]['locationid'], $subnets[$j]['locationid']);
					$b = max($subnets[$i]['locationid'], $subnets[$j]['locationid']);
					$other["$a|$b"] = array('lid1' => $subnets[$i]['locationid'], 'lid2' => $subnets[$j]['locationid']);
				}
			}
		}
		if ($overlapSelf) {
			$overlapSelf = array();
			foreach ($self as $entry) {
				if (!isset($locs[$entry]))
					continue;
				$overlapSelf[]['locationname'] = $locs[$entry]['locationname'];
			}
		}
		if ($overlapOther) {
			$overlapOther = array();
			foreach ($other as $entry) {
				if (!isset($locs[$entry['lid1']]) && !isset($locs[$entry['lid2']]))
					continue;
				if (in_array($entry['lid1'], $locs[$entry['lid2']]['parents']) || in_array($entry['lid2'], $locs[$entry['lid1']]['parents']))
					continue;
				if (isset($locs[$entry['lid1']])) {
					$entry['name1'] = $locs[$entry['lid1']]['locationname'];
				}
				if (isset($locs[$entry['lid2']])) {
					$entry['name2'] = $locs[$entry['lid2']]['locationname'];
				}
				$overlapOther[] = $entry;
			}
		}
	}

	/**
	 * Get information about machines where the location assigned by roomplanner
	 * mismatches what the subnet configuration says.
	 * If $locationId is 0, return list of all locations where a mismatch occurs,
	 * grouped by the location the client was assigned to via roomplanner.
	 * Otherwise, just return an assoc array with the requested locationid, name
	 * and a list of all clients that are wrongfully assigned to that room.
	 */
	public static function getMachinesWithLocationMismatch(int $locationId = 0, bool $checkPerms = false): array
	{
		if ($checkPerms) {
			if ($locationId !== 0) {
				// Query details for specific location -- use assert and fake array
				User::assertPermission('.roomplanner.edit', $locationId);
				$roomplannerLocs = [$locationId];
			} else {
				// Query summary for all locations -- get actual list
				$roomplannerLocs = User::getAllowedLocations('.roomplanner.edit');
			}
			if (User::hasPermission('subnets.edit')) {
				$ipLocs = [0];
			} else {
				$ipLocs = User::getAllowedLocations('location.edit.subnets');
			}
			if (in_array(0, $ipLocs)) {
				$ipLocs = true;
			}
			if (in_array(0, $roomplannerLocs)) {
				$roomplannerLocs = true;
			}
			if ($ipLocs === true && $roomplannerLocs === true) {
				$checkPerms = false; // User can do everything
			} elseif ($ipLocs === true || $roomplannerLocs === true) {
				$combinedLocs = true;
			} else {
				$combinedLocs = array_unique(array_merge($ipLocs, $roomplannerLocs));
			}
		}
		if ($checkPerms && empty($combinedLocs))
			return [];
		if ($locationId === 0) {
			if (!$checkPerms || $combinedLocs === true) {
				$extra = 'IS NOT NULL';
				$params = [];
			} else {
				$extra = 'IN (:locs)';
				$params = ['locs' => $combinedLocs];
			}
			$query = "SELECT subnetlocationid, fixedlocationid
				FROM machine WHERE fixedlocationid $extra";
		} else {
			$query = "SELECT machineuuid, hostname, clientip, subnetlocationid, fixedlocationid
				FROM machine WHERE fixedlocationid = :locationid";
			$params = ['locationid' => $locationId];
		}
		$res = Database::simpleQuery($query, $params);
		$return = [];
		$locs = false;
		foreach ($res as $row) {
			if (Location::isFixedLocationValid($row['fixedlocationid'], $row['subnetlocationid']))
				continue;
			$lid = (int)$row['fixedlocationid'];
			if (!isset($return[$lid])) {
				if ($locs === false) {
					$locs = Location::getLocationsAssoc();
				}
				$return[$lid] = [
					'locationid' => $lid,
					'locationname' => $locs[$lid]['locationname'],
					'clients' => [],
					'count' => 0,
				];
			}
			if ($locationId === 0) {
				$return[$lid]['count']++;
			} else {
				$slid = (int)$row['subnetlocationid'];
				$data = [
					'machineuuid' => $row['machineuuid'],
					'hostname' => $row['hostname'],
					'clientip' => $row['clientip'],
				];
				if ($slid !== 0) {
					$data += [
						'iplocationid' => $slid,
						'iplocationname' => $locs[$slid]['locationname'],
						'ipisleaf' => empty($locs[$slid]['children']),
						'canmove' => empty($locs[$slid]['children'])
							&& (!$checkPerms || $ipLocs === true || in_array($slid, $ipLocs)), // Can machine be moved to subnet's locationid?
					];
				}
				$return[$lid]['clients'][] = $data;
			}
		}
		if (empty($return))
			return $return;
		if ($locationId === 0)
			return array_values($return);
		return $return[$locationId];
	}

	private static function overlap(array $net1, array $net2): bool
	{
		return ($net1['startaddr'] <= $net2['endaddr'] && $net1['endaddr'] >= $net2['startaddr']);
	}

	public static function rangeToLongVerbose(string $start, string $end): ?array
	{
		$result = self::rangeToLong($start, $end);
		list($startLong, $endLong) = $result;
		if ($startLong === false) {
			Message::addWarning('main.value-invalid', 'start addr', $start);
		}
		if ($endLong === false) {
			Message::addWarning('main.value-invalid', 'end addr', $start);
		}
		if ($startLong === false || $endLong === false)
			return null;
		if ($startLong > $endLong) {
			Message::addWarning('main.value-invalid', 'range', $start . ' - ' . $end);
			return null;
		}
		return $result;
	}

	/** @return array{0: int, 1: int} */
	public static function rangeToLong(string $start, string $end): array
	{
		$startLong = ip2long($start);
		$endLong = ip2long($end);
		return array($startLong, $endLong);
	}

}