diff options
Diffstat (limited to 'modules-available/locations/inc/location.inc.php')
-rw-r--r-- | modules-available/locations/inc/location.inc.php | 100 |
1 files changed, 54 insertions, 46 deletions
diff --git a/modules-available/locations/inc/location.inc.php b/modules-available/locations/inc/location.inc.php index a29be5c3..807f8577 100644 --- a/modules-available/locations/inc/location.inc.php +++ b/modules-available/locations/inc/location.inc.php @@ -8,7 +8,7 @@ class Location private static $treeCache = false; private static $subnetMapCache = false; - public static function getTree() + public static function getTree(): array { if (self::$treeCache === false) { self::$treeCache = self::queryLocations(); @@ -17,11 +17,11 @@ class Location return self::$treeCache; } - public static function queryLocations() + public static function queryLocations(): array { $res = Database::simpleQuery("SELECT locationid, parentlocationid, locationname FROM location"); $rows = array(); - while ($row = $res->fetch(PDO::FETCH_ASSOC)) { + foreach ($res as $row) { $rows[] = $row; } return $rows; @@ -29,10 +29,9 @@ class Location /** * Return row from location table for $locationId. - * @param $locationId * @return array|bool row from DB, false if not found */ - public static function get($locationId) + public static function get(int $locationId) { return Database::queryFirst("SELECT * FROM location WHERE locationid = :locationId", compact('locationId')); } @@ -42,9 +41,8 @@ class Location * @param int $locationId id of location to get name for * @return string|false Name of location, false if locationId doesn't exist */ - public static function getName($locationId) + public static function getName(int $locationId) { - $locationId = (int)$locationId; if (self::$assocLocationCache === false) { self::getLocationsAssoc(); } @@ -56,15 +54,13 @@ class Location /** * Get all the names of the given location and its parents, up * to the root element. Array keys will be locationids, value the names. - * @param int $locationId * @return array|false locations, from furthest to nearest or false if locationId doesn't exist */ - public static function getNameChain($locationId) + public static function getNameChain(int $locationId) { if (self::$assocLocationCache === false) { self::getLocationsAssoc(); } - $locationId = (int)$locationId; if (!isset(self::$assocLocationCache[$locationId])) return false; $ret = array(); @@ -84,10 +80,10 @@ class Location return self::$assocLocationCache; } - private static function flattenTreeAssoc($tree, $parents = array(), $depth = 0) + private static function flattenTreeAssoc($tree, $parents = array(), $depth = 0): array { if ($depth > 20) { - Util::traceError('Recursive location definition detected at ' . print_r($tree, true)); + ErrorHandler::traceError('Recursive location definition detected at ' . print_r($tree, true)); } $output = array(); foreach ($tree as $node) { @@ -119,9 +115,15 @@ class Location return $output; } - public static function getLocations($selected = 0, $excludeId = 0, $addNoParent = false, $keepArrayKeys = false) + /** + * @param int|int[] $selected Which locationIDs to mark as selected + * @param int $excludeId Which locationID to exclude + * @param bool $addNoParent Add entry for "no location" at the top + * @param bool $keepArrayKeys Keep location IDs as array index + * @return array Locations + */ + public static function getLocations($selected = 0, int $excludeId = 0, bool $addNoParent = false, bool $keepArrayKeys = false): array { - $selected = (int)$selected; if (self::$flatLocationCache === false) { $rows = self::getTree(); $rows = self::flattenTree($rows); @@ -142,7 +144,7 @@ class Location unset($rows[$key]); continue; } - if ((is_array($selected) && in_array($row['locationid'], $selected)) || (int)$row['locationid'] === $selected) { + if ((is_array($selected) && in_array($row['locationid'], $selected)) || $row['locationid'] == $selected) { $row['selected'] = true; } $row['sortIndex'] = $index++; @@ -151,7 +153,8 @@ class Location array_unshift($rows, array( 'locationid' => 0, 'locationname' => '-----', - 'selected' => $selected === 0 + 'selected' => $selected === 0, + 'locationpad' => '', )); } if ($keepArrayKeys) @@ -163,15 +166,15 @@ class Location * Get nested array of all the locations and children of given locationid(s). * * @param int[]|int $idList List of location ids - * @param bool $locationTree used in recursive calls, don't pass + * @param ?array $locationTree used in recursive calls, don't pass * @return array list of passed locations plus their children */ - public static function getRecursive($idList, $locationTree = false) + public static function getRecursive($idList, ?array $locationTree = null): array { if (!is_array($idList)) { $idList = array($idList); } - if ($locationTree === false) { + if ($locationTree === null) { $locationTree = self::getTree(); } $ret = array(); @@ -191,7 +194,7 @@ class Location * @param int[]|int $idList List of location ids * @return array list of passed locations plus their children */ - public static function getRecursiveFlat($idList) + public static function getRecursiveFlat($idList): array { $ret = self::getRecursive($idList); if (!empty($ret)) { @@ -200,7 +203,7 @@ class Location return $ret; } - public static function buildTree($elements, $parentId = 0) + public static function buildTree(array $elements, int $parentId = 0): array { $branch = array(); $sort = array(); @@ -220,10 +223,10 @@ class Location return $branch; } - private static function flattenTree($tree, $depth = 0) + private static function flattenTree(array $tree, int $depth = 0): array { if ($depth > 20) { - Util::traceError('Recursive location definition detected at ' . print_r($tree, true)); + ErrorHandler::traceError('Recursive location definition detected at ' . print_r($tree, true)); } $output = array(); foreach ($tree as $node) { @@ -242,16 +245,16 @@ class Location return $output; } - public static function isLeaf($locationid) { + public static function isLeaf(int $locationid): bool + { $result = Database::queryFirst('SELECT COUNT(locationid) = 0 AS isleaf ' . 'FROM location ' . 'WHERE parentlocationid = :locationid', ['locationid' => $locationid]); $result = $result['isleaf']; - $result = (bool)$result; - return $result; + return (bool)$result; } - public static function extractIds($tree) + public static function extractIds(array $tree): array { $ids = array(); foreach ($tree as $node) { @@ -265,10 +268,11 @@ class Location /** * Get location id for given machine (by uuid) + * * @param string $uuid machine uuid - * @return bool|int locationid, false if no match + * @return false|int locationid, false if no match */ - public static function getFromMachineUuid($uuid) + public static function getFromMachineUuid(string $uuid) { // Only if we have the statistics module which supplies the machine table if (Module::get('statistics') === false) @@ -285,9 +289,9 @@ class Location * * @param string $ip IP address of client * @param bool $honorRoomPlanner consider a fixed location assigned manually by roomplanner - * @return bool|int locationid, or false if no match + * @return false|int locationid, or false if no match */ - public static function getFromIp($ip, $honorRoomPlanner = false) + public static function getFromIp(string $ip, bool $honorRoomPlanner = false) { if (Module::get('statistics') !== false) { // Shortcut - try to use subnetlocationid in machine table @@ -316,17 +320,17 @@ class Location * client, so if it seems too fishy, the UUID will be ignored. * * @param string $ip IP address of client - * @param string $uuid System-UUID of client - * @return int|bool location id, or false if none matches + * @param ?string $uuid System-UUID of client + * @return int|false location id, or false if none matches */ - public static function getFromIpAndUuid($ip, $uuid) + public static function getFromIpAndUuid(string $ip, ?string $uuid) { $locationId = false; $ipLoc = self::getFromIp($ip); if ($ipLoc !== false) { // Set locationId to ipLoc for now, it will be overwritten later if another case applies. $locationId = $ipLoc; - if ($uuid !== false) { + if ($uuid !== null) { // Machine ip maps to a location, and we have a client supplied uuid (which might not be known if the client boots for the first time) $uuidLoc = self::getFromMachineUuid($uuid); if (self::isFixedLocationValid($uuidLoc, $ipLoc)) { @@ -337,8 +341,10 @@ class Location return $locationId; } - public static function isFixedLocationValid($uuidLoc, $ipLoc) + public static function isFixedLocationValid($uuidLoc, $ipLoc): bool { + if ($uuidLoc === false) + return false; $uuidLoc = (int)$uuidLoc; $ipLoc = (int)$ipLoc; if ($uuidLoc === $ipLoc || $uuidLoc === 0) @@ -358,12 +364,10 @@ class Location /** * Get all location IDs from the given location up to the root. * - * @param int $locationId * @return int[] location ids, including $locationId */ - public static function getLocationRootChain($locationId) + public static function getLocationRootChain(int $locationId): array { - $locationId = (int)$locationId; if (self::$assocLocationCache === false) { self::getLocationsAssoc(); } @@ -377,11 +381,11 @@ class Location /** * @return array list of subnets as numeric array */ - public static function getSubnets() + public static function getSubnets(): array { $res = Database::simpleQuery("SELECT startaddr, endaddr, locationid FROM subnet"); $subnets = array(); - while ($row = $res->fetch(PDO::FETCH_ASSOC)) { + foreach ($res as $row) { settype($row['locationid'], 'int'); $subnets[] = $row; } @@ -389,9 +393,9 @@ class Location } /** - * @return array|bool assoc array mapping from locationid to subnets + * @return array assoc array mapping from locationid to subnets */ - public static function getSubnetsByLocation($recursive = false) + public static function getSubnetsByLocation($recursive = false): array { $locs = self::getLocationsAssoc(); $subnets = self::getSubnets(); @@ -425,9 +429,9 @@ class Location * random one will be returned. * * @param string $ip IP to look up - * @return bool|int locationid ip matches, false = no match + * @return false|int locationid ip matches, false = no match */ - public static function mapIpToLocation($ip) + public static function mapIpToLocation(string $ip) { if (self::$subnetMapCache === false) { self::$subnetMapCache = self::getSubnetsByLocation(); @@ -456,7 +460,10 @@ class Location return (int)$best; } - public static function updateMapIpToLocation($uuid, $ip) + /** + * @return false|int newly determined location + */ + public static function updateMapIpToLocation(string $uuid, string $ip) { $loc = self::mapIpToLocation($ip); if ($loc === false) { @@ -464,6 +471,7 @@ class Location } else { Database::exec("UPDATE machine SET subnetlocationid = :loc WHERE machineuuid = :uuid", compact('loc', 'uuid')); } + return $loc; } } |