mirror of
https://github.com/lkddi/nexusphp.git
synced 2026-04-24 12:07:23 +08:00
get_ip_location() use geoip
This commit is contained in:
@@ -60,3 +60,5 @@ GOOGLE_DRIVE_CLIENT_ID=
|
|||||||
GOOGLE_DRIVE_CLIENT_SECRET=
|
GOOGLE_DRIVE_CLIENT_SECRET=
|
||||||
GOOGLE_DRIVE_REFRESH_TOKEN=
|
GOOGLE_DRIVE_REFRESH_TOKEN=
|
||||||
GOOGLE_DRIVE_FOLDER_ID=
|
GOOGLE_DRIVE_FOLDER_ID=
|
||||||
|
|
||||||
|
GEOIP2_DATABASE=
|
||||||
|
|||||||
+56
-10
@@ -1622,22 +1622,37 @@ function get_ip_location($ip)
|
|||||||
{
|
{
|
||||||
global $lang_functions;
|
global $lang_functions;
|
||||||
global $Cache;
|
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();
|
$ret = array();
|
||||||
$res = sql_query("SELECT * FROM locations") or sqlerr(__FILE__, __LINE__);
|
|
||||||
while ($row = mysql_fetch_array($res))
|
// $res = sql_query("SELECT * FROM locations") or sqlerr(__FILE__, __LINE__);
|
||||||
$ret[] = $row;
|
// while ($row = mysql_fetch_array($res))
|
||||||
$Cache->cache_value('location_list', $ret, 152800);
|
// $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'],"");
|
$location = array($lang_functions['text_unknown'],"");
|
||||||
|
|
||||||
foreach($ret AS $arr)
|
foreach($ret AS $arr)
|
||||||
{
|
{
|
||||||
if(in_ip_range(false, $ip, $arr["start_ip"], $arr["end_ip"]))
|
$location = array($arr["name"], "");
|
||||||
{
|
break;
|
||||||
$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"]);
|
// if(in_ip_range(false, $ip, $arr["start_ip"], $arr["end_ip"]))
|
||||||
break;
|
// {
|
||||||
}
|
// $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;
|
return $location;
|
||||||
}
|
}
|
||||||
@@ -4999,4 +5014,35 @@ function can_access_torrent($torrent)
|
|||||||
return false;
|
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,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
?>
|
?>
|
||||||
|
|||||||
Reference in New Issue
Block a user