Moved GeoIP stuff into its own folder.
This commit is contained in:
parent
8ff10f9d3c
commit
1f22a1e4b9
4 changed files with 41 additions and 4 deletions
|
@ -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,
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
<?php
|
||||
use Misuzu\Application;
|
||||
use Misuzu\Cache;
|
||||
use Misuzu\Database;
|
||||
|
||||
|
|
37
src/Net/geoip.php
Normal file
37
src/Net/geoip.php
Normal file
|
@ -0,0 +1,37 @@
|
|||
<?php
|
||||
use GeoIp2\Database\Reader;
|
||||
|
||||
define('MSZ_GEOIP_INSTANCE_STORE', '_msz_maxmind_geoip');
|
||||
define('MSZ_GEOIP_CACHE_STORE', '_msz_geoip_cache');
|
||||
|
||||
function geoip_init(?string $database = null): void
|
||||
{
|
||||
if (!empty($GLOBALS[MSZ_GEOIP_INSTANCE_STORE])) {
|
||||
$GLOBALS[MSZ_GEOIP_INSTANCE_STORE]->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);
|
||||
});
|
||||
}
|
|
@ -1,6 +1,4 @@
|
|||
<?php
|
||||
use Misuzu\Application;
|
||||
|
||||
define('MSZ_IP_UNKNOWN', 0);
|
||||
define('MSZ_IP_V4', 4);
|
||||
define('MSZ_IP_V6', 6);
|
||||
|
@ -18,7 +16,7 @@ function ip_remote_address(string $fallback = '::1'): string
|
|||
function ip_country_code(string $ipAddr, string $fallback = 'XX'): string
|
||||
{
|
||||
try {
|
||||
return Application::geoip()->country($ipAddr)->country->isoCode ?? $fallback;
|
||||
return geoip_country($ipAddr)->country->isoCode ?? $fallback;
|
||||
} catch (Exception $e) {
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue