From 58721c7740493f6dce984257b5f26ac0941128f4 Mon Sep 17 00:00:00 2001 From: Daniel Mejia Date: Mon, 15 Jun 2026 15:02:45 -0400 Subject: [PATCH] Refactor SQL query for country lookup by IP ipTO is not indexed. this force to sort by index (this assumes that there are not overlapping segments) --- core/libraries/Hubzero/Geocode/Geocode.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/core/libraries/Hubzero/Geocode/Geocode.php b/core/libraries/Hubzero/Geocode/Geocode.php index 9cddadfce6a..76c36be2536 100644 --- a/core/libraries/Hubzero/Geocode/Geocode.php +++ b/core/libraries/Hubzero/Geocode/Geocode.php @@ -469,8 +469,10 @@ public static function ipcountry($ip='') // Convert the dotted quad IP address to long: $n_ip = ip2long($ip); - - $sql = "SELECT LOWER(countrySHORT) FROM ipcountry WHERE ipFROM <= " . $gdb->quote($n_ip) . " AND ipTO >= " . $gdb->quote($n_ip); + $sql = "SELECT LOWER(countrySHORT) FROM ipcountry" + . " INNER JOIN (SELECT MAX(ipFROM) AS start FROM ipcountry" + . " WHERE ipFROM <= " . $gdb->quote($n_ip) . ") s ON (ipFROM = s.start)" + . " WHERE ipTO >= " . $gdb->quote($n_ip); $gdb->setQuery($sql); $country = stripslashes($gdb->loadResult()); }