diff options
author | Simon Rettberg | 2016-06-08 13:00:39 +0200 |
---|---|---|
committer | Simon Rettberg | 2016-06-08 13:00:39 +0200 |
commit | 62f218da95ac407c69cc0956c257ddc2ae9293c3 (patch) | |
tree | 4eb8b5a95752920a2fc8de80b01b640caf690451 /modules-available/locations | |
parent | PhpStorm stuff (diff) | |
download | slx-admin-62f218da95ac407c69cc0956c257ddc2ae9293c3.tar.gz slx-admin-62f218da95ac407c69cc0956c257ddc2ae9293c3.tar.xz slx-admin-62f218da95ac407c69cc0956c257ddc2ae9293c3.zip |
[location] Move getLocationRootChain to heler class
Diffstat (limited to 'modules-available/locations')
-rw-r--r-- | modules-available/locations/baseconfig/getconfig.inc.php | 8 | ||||
-rw-r--r-- | modules-available/locations/inc/location.inc.php | 25 |
2 files changed, 26 insertions, 7 deletions
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; @@ -172,6 +178,25 @@ class Location } /** + * 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 */ public static function getSubnets() |