From 1f22a1e4b928117a2a96ebc2510e18c6637ed98c Mon Sep 17 00:00:00 2001 From: flashwave Date: Fri, 5 Oct 2018 13:00:37 +0200 Subject: [PATCH] Moved GeoIP stuff into its own folder. --- misuzu.php | 3 +++ public/index.php | 1 - src/Net/geoip.php | 37 +++++++++++++++++++++++++++++++++++++ src/Net/ip.php | 4 +--- 4 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 src/Net/geoip.php diff --git a/misuzu.php b/misuzu.php index 5db6315d..5e5f136d 100644 --- a/misuzu.php +++ b/misuzu.php @@ -49,6 +49,7 @@ require_once 'src/Forum/post.php'; require_once 'src/Forum/topic.php'; require_once 'src/Forum/validate.php'; require_once 'src/Net/cidr.php'; +require_once 'src/Net/geoip.php'; require_once 'src/Net/ip.php'; require_once 'src/Parsers/parse.php'; require_once 'src/Users/login_attempt.php'; @@ -253,6 +254,8 @@ MIG; config_get_default('', 'Cache', 'prefix') ); + geoip_init(config_get_default('', 'GeoIP', 'database_path')); + tpl_init([ 'debug' => MSZ_DEBUG, 'auto_reload' => MSZ_DEBUG, diff --git a/public/index.php b/public/index.php index fe33cb71..df6412aa 100644 --- a/public/index.php +++ b/public/index.php @@ -1,5 +1,4 @@ close(); + } + + $GLOBALS[MSZ_GEOIP_INSTANCE_STORE] = new Reader($database ?? config_get('GeoIP', 'database_path')); +} + +function geoip_cache(string $section, string $ipAddress, callable $value) +{ + if (empty($GLOBALS[MSZ_GEOIP_CACHE_STORE][$ipAddress][$section])) { + $GLOBALS[MSZ_GEOIP_CACHE_STORE][$ipAddress][$section] = $value(); + } + + return $GLOBALS[MSZ_GEOIP_CACHE_STORE][$ipAddress][$section] ?? null; +} + +function geoip_country(string $ipAddress) +{ + return geoip_cache('country', $ipAddress, function () { + return $GLOBALS[MSZ_GEOIP_INSTANCE_STORE]->country($ipAddress); + }); +} + +function geoip_anonymous(string $ipAddress) +{ + return geoip_cache('anonymous', $ipAddress, function () { + return $GLOBALS[MSZ_GEOIP_INSTANCE_STORE]->anonymousIp($ipAddress); + }); +} diff --git a/src/Net/ip.php b/src/Net/ip.php index d9f4c34f..b480eceb 100644 --- a/src/Net/ip.php +++ b/src/Net/ip.php @@ -1,6 +1,4 @@ country($ipAddr)->country->isoCode ?? $fallback; + return geoip_country($ipAddr)->country->isoCode ?? $fallback; } catch (Exception $e) { }