From 01263d6a764b5d59e917d14e9b4bc26ebf0c039e Mon Sep 17 00:00:00 2001 From: xiaomlove <353856593@qq.com> Date: Mon, 7 Jun 2021 15:41:47 +0800 Subject: [PATCH] get_ip_location() use geoip --- .env.example | 2 ++ include/functions.php | 66 ++++++++++++++++++++++++++++++++++++------- 2 files changed, 58 insertions(+), 10 deletions(-) diff --git a/.env.example b/.env.example index 0c025d1e..acd14437 100644 --- a/.env.example +++ b/.env.example @@ -60,3 +60,5 @@ GOOGLE_DRIVE_CLIENT_ID= GOOGLE_DRIVE_CLIENT_SECRET= GOOGLE_DRIVE_REFRESH_TOKEN= GOOGLE_DRIVE_FOLDER_ID= + +GEOIP2_DATABASE= diff --git a/include/functions.php b/include/functions.php index fcd9f391..50e6fa96 100644 --- a/include/functions.php +++ b/include/functions.php @@ -1622,22 +1622,37 @@ function get_ip_location($ip) { global $lang_functions; global $Cache; - if (!$ret = $Cache->get_value('location_list')){ + + static $locations; + if (isset($locations[$ip])) { + return $locations[$ip]; + } + $cacheKey = "location_$ip"; + if (!$ret = $Cache->get_value($cacheKey)){ $ret = array(); - $res = sql_query("SELECT * FROM locations") or sqlerr(__FILE__, __LINE__); - while ($row = mysql_fetch_array($res)) - $ret[] = $row; - $Cache->cache_value('location_list', $ret, 152800); + +// $res = sql_query("SELECT * FROM locations") or sqlerr(__FILE__, __LINE__); +// while ($row = mysql_fetch_array($res)) +// $ret[] = $row; + + //get from geoip2 + $row = get_ip_location_from_geoip($ip); + if ($row) { + $ret[] = $row; + } + $Cache->cache_value($cacheKey, $ret, 152800); } $location = array($lang_functions['text_unknown'],""); foreach($ret AS $arr) { - if(in_ip_range(false, $ip, $arr["start_ip"], $arr["end_ip"])) - { - $location = array($arr["name"], $lang_functions['text_user_ip'].": " . $ip . ($arr["location_main"] != "" ? " ".$lang_functions['text_location_main'].": " . $arr["location_main"] : ""). ($arr["location_sub"] != "" ? " ".$lang_functions['text_location_sub'].": " . $arr["location_sub"] : "") . " ".$lang_functions['text_ip_range'].": " . $arr["start_ip"] . " ~ ". $arr["end_ip"]); - break; - } + $location = array($arr["name"], ""); + break; +// if(in_ip_range(false, $ip, $arr["start_ip"], $arr["end_ip"])) +// { +// $location = array($arr["name"], $lang_functions['text_user_ip'].": " . $ip . ($arr["location_main"] != "" ? " ".$lang_functions['text_location_main'].": " . $arr["location_main"] : ""). ($arr["location_sub"] != "" ? " ".$lang_functions['text_location_sub'].": " . $arr["location_sub"] : "") . " ".$lang_functions['text_ip_range'].": " . $arr["start_ip"] . " ~ ". $arr["end_ip"]); +// break; +// } } return $location; } @@ -4999,4 +5014,35 @@ function can_access_torrent($torrent) return false; } +function get_ip_location_from_geoip($ip) +{ + $database = nexus_env('GEOIP2_DATABASE'); + if (empty($database) || !is_readable($database)) { + do_log("no geoip2 database or $database is nor is not readable."); + return false; + } + static $reader; + if (is_null($reader)) { + $reader = new \GeoIp2\Database\Reader($database); + } + $record = $reader->city($ip); + $lang = get_langfolder_cookie(); + $langMap = [ + 'chs' => 'zh-CN', + 'cht' => 'zh-CN', + 'en' => 'en', + ]; + $locale = $langMap[$lang] ?? $lang; + $countryName = $record->country->names[$locale] ?? $record->country->names['en']; + $cityName = $record->city->names[$locale] ?? $record->city->names['en']; + return [ + 'name' => sprintf('%s·%s', $cityName, $countryName), + 'location_main' => '', + 'location_sub' => '', + 'flagpic' => '', + 'start_ip' => $ip, + 'end_ip' => $ip, + ]; +} + ?>