Changeset View
Changeset View
Standalone View
Standalone View
src/app/IP4Net.php
Show All 14 Lines | protected $fillable = [ | ||||
'net_mask', | 'net_mask', | ||||
'net_broadcast', | 'net_broadcast', | ||||
'country', | 'country', | ||||
'serial', | 'serial', | ||||
'created_at', | 'created_at', | ||||
'updated_at' | 'updated_at' | ||||
]; | ]; | ||||
public static function getNet($ip, $mask = 32) | public static function getNet($ip) | ||||
{ | { | ||||
$query = " | $where = 'INET_ATON(net_number) <= INET_ATON(?) and INET_ATON(net_broadcast) >= INET_ATON(?)'; | ||||
SELECT id FROM ip4nets | return IP4Net::whereRaw($where, [$ip, $ip]) | ||||
WHERE INET_ATON(net_number) <= INET_ATON(?) | ->orderByRaw('INET_ATON(net_number), net_mask DESC') | ||||
AND INET_ATON(net_broadcast) >= INET_ATON(?) | ->limit(1) | ||||
ORDER BY INET_ATON(net_number), net_mask DESC LIMIT 1 | ->first(); | ||||
"; | |||||
$results = DB::select($query, [$ip, $ip]); | |||||
if (sizeof($results) == 0) { | |||||
return null; | |||||
} | |||||
return \App\IP4Net::find($results[0]->id); | |||||
} | } | ||||
} | } |