From 62f218da95ac407c69cc0956c257ddc2ae9293c3 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Wed, 8 Jun 2016 13:00:39 +0200 Subject: [location] Move getLocationRootChain to heler class --- .../locations/baseconfig/getconfig.inc.php | 8 +------ modules-available/locations/inc/location.inc.php | 25 ++++++++++++++++++++++ 2 files changed, 26 insertions(+), 7 deletions(-) (limited to 'modules-available/locations') diff --git a/modules-available/locations/baseconfig/getconfig.inc.php b/modules-available/locations/baseconfig/getconfig.inc.php index 2643ced6..d4a4f40b 100644 --- a/modules-available/locations/baseconfig/getconfig.inc.php +++ b/modules-available/locations/baseconfig/getconfig.inc.php @@ -9,13 +9,7 @@ if ($locationId === false) { $matchingLocations = array(); if ($locationId !== false) { // Get all parents - settype($locationId, 'integer'); - $locations = Location::getLocationsAssoc(); - $find = $locationId; - while (isset($locations[$find]) && !in_array($find, $matchingLocations)) { - $matchingLocations[] = $find; - $find = (int)$locations[$find]['parentlocationid']; - } + $matchingLocations = Location::getLocationRootChain($locationId); $configVars["SLX_LOCATIONS"] = implode(' ', $matchingLocations); } diff --git a/modules-available/locations/inc/location.inc.php b/modules-available/locations/inc/location.inc.php index 4bfe9f7a..25e888d8 100644 --- a/modules-available/locations/inc/location.inc.php +++ b/modules-available/locations/inc/location.inc.php @@ -153,6 +153,12 @@ class Location return $ids; } + /** + * Get closest location by matching subnets. Deepest match in tree wins. + * + * @param string $ip IP address of client + * @return bool|int locationid, or false if no match + */ public static function getFromIp($ip) { $locationId = false; @@ -171,6 +177,25 @@ class Location return $locationId; } + /** + * Get all location IDs from the given location up to the root. + * + * @param int $locationId + * @return int[] location ids, including $locationId + */ + public function getLocationRootChain($locationId) + { + settype($locationId, 'integer'); + $locations = Location::getLocationsAssoc(); + $find = $locationId; + $matchingLocations = array(); + while (isset($locations[$find]) && !in_array($find, $matchingLocations)) { + $matchingLocations[] = $find; + $find = (int)$locations[$find]['parentlocationid']; + } + return $matchingLocations; + } + /** * @return array list of subnets as numeric array */ -- cgit v1.2.3-55-g7522