/** * Returns the Location for the provided IPv6 address. * * @param string $ip6 IPv6 address. * * @throws ProviderGeoIpNotFound * @return Location */ public function getLocationFromIPv6($ip6) { // create a cidr block $ip = $this->str($ip6)->explode(':'); do { $ip->removeLast(); } while ($ip->count() > 3); $subNet = $ip->implode(':')->val(); $ipStart = floatval(Ipv6Helper::ip2LongV6($subNet . ':8000:0:0:0:0')); $cityBlock = CityBlockIp6Entity::findOne(['rangeStart' => $ipStart]); if (!$cityBlock) { $ipStart = floatval(Ipv6Helper::ip2LongV6($subNet . ':0:0:0:0:0')); $cityBlock = CityBlockIp6Entity::findOne(['rangeStart' => $ipStart]); } // verify that end ip is within range if (!$cityBlock || !(Ipv6Helper::ip2LongV6($ip6) <= $cityBlock->rangeEnd)) { throw new ProviderGeoIpNotFound('GeoIp entry not found'); } // get location info $locationInfo = LocationEntity::findOne(['geoId' => $cityBlock->geoId]); // populate location variable $location = new Location(); $location->setContinent($locationInfo->continentCode, $locationInfo->continentName); $location->setCountry($locationInfo->countryCode, $locationInfo->countryName); $location->setCityName($locationInfo->cityName); $location->setSubdivision1($locationInfo->subdivision1Code, $locationInfo->subdivision1Name); $location->setSubdivision2($locationInfo->subdivision2Code, $locationInfo->subdivision2Name); $location->setTimeZone($locationInfo->timeZone); return $location; }
/** * Import location entries. */ public function importLocations() { // locations db file $locationsFile = $this->dbFolder . 'GeoLite2-City-Locations-' . $this->config->MaxMind->Language . '.csv'; // start the import /** * @var $mongo Mongo */ echo "\nImporting locations ... please give it couple of minutes to finish."; $handle = fopen($locationsFile, "r"); fgetcsv($handle, 0, ","); // remove the header row while (($ld = fgetcsv($handle, 0, ",")) !== false) { $locationEntity = new LocationEntity(); $locationEntity->geoId = $ld[0]; $locationEntity->continentCode = strtoupper($ld[2]); $locationEntity->continentName = $ld[3]; $locationEntity->countryCode = strtoupper($ld[4]); $locationEntity->countryName = $ld[5]; $locationEntity->subdivision1Code = $ld[6]; $locationEntity->subdivision1Name = $ld[7]; $locationEntity->subdivision2Code = $ld[8]; $locationEntity->subdivision2Name = $ld[9]; $locationEntity->cityName = $ld[10]; $locationEntity->timeZone = $ld[12]; $locationEntity->save(); } fclose($handle); // ensure location index $index = new SingleIndex('geoId', 'geoId'); $this->mongo->createIndex('GeoIpLocation', $index); echo "\nLocation import done\n"; }