where('ip_start', '<=', $addrStart)->orderBy('ip_start', 'desc')->first(); $c1 = isset($res->country) ? $res->country : null; $res = DbipLookup2::where('addr_type', $addrType)->where('ip_start', '<=', $addrStart)->orderBy('ip_start', 'desc')->first(); $c2 = isset($res->country) ? $res->country : null; if($c1 == $c2){ return $c1; } $res = DbipLookup3::where('addr_type', $addrType)->where('ip_start', '<=', $addrStart)->orderBy('ip_start', 'desc')->first(); if(isset($res->country)){ return $res->country; } //look in api return false; } static private function addrType($addr) { if (ip2long($addr) !== false) { return "ipv4"; } else if (preg_match('/^[0-9a-fA-F:]+$/', $addr) && @inet_pton($addr)) { return "ipv6"; } throw new DBIPException("unknown address type for {$addr}"); } static public function insert($addr, $country) { $lookup = new DbipLookup3(); $lookup->addr_type = self::addrType($addr); $lookup->ip_start = inet_pton($addr); $lookup->ip_end = inet_pton($addr); $lookup->country = strtoupper($country); $lookup->save(); } }