Changeset View
Changeset View
Standalone View
Standalone View
src/app/Utils.php
Show All 40 Lines | class Utils | ||||
/** | /** | ||||
* Return the country ISO code for an IP address. | * Return the country ISO code for an IP address. | ||||
* | * | ||||
* @return string | * @return string | ||||
*/ | */ | ||||
public static function countryForIP($ip) | public static function countryForIP($ip) | ||||
{ | { | ||||
if (strpos($ip, ':') === false) { | if (strpos($ip, ':') === false) { | ||||
$query = " | $net = \App\IP4Net::getNet($ip); | ||||
SELECT country FROM ip4nets | |||||
WHERE INET_ATON(net_number) <= INET_ATON(?) | |||||
AND INET_ATON(net_broadcast) >= INET_ATON(?) | |||||
ORDER BY INET_ATON(net_number), net_mask DESC LIMIT 1 | |||||
"; | |||||
} else { | } else { | ||||
$query = " | $net = \App\IP6Net::getNet($ip); | ||||
SELECT id FROM ip6nets | |||||
WHERE INET6_ATON(net_number) <= INET6_ATON(?) | |||||
AND INET6_ATON(net_broadcast) >= INET6_ATON(?) | |||||
ORDER BY INET6_ATON(net_number), net_mask DESC LIMIT 1 | |||||
"; | |||||
} | } | ||||
$nets = \Illuminate\Support\Facades\DB::select($query, [$ip, $ip]); | return $net && $net->country ? $net->country : 'CH'; | ||||
if (sizeof($nets) > 0) { | |||||
return $nets[0]->country; | |||||
} | |||||
return 'CH'; | |||||
} | } | ||||
/** | /** | ||||
* Return the country ISO code for the current request. | * Return the country ISO code for the current request. | ||||
*/ | */ | ||||
public static function countryForRequest() | public static function countryForRequest() | ||||
{ | { | ||||
$request = \request(); | $request = \request(); | ||||
▲ Show 20 Lines • Show All 494 Lines • Show Last 20 Lines |