Example #1
0
 /**
  * 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;
 }
Example #2
0
 /**
  * Import IPv6 entries.
  */
 public function importIp6CityBlock()
 {
     // city blocks db file
     $cbFile = $this->dbFolder . 'GeoLite2-City-Blocks-IPv6.csv';
     // start the import
     echo "\nImporting city IPv6 block ... please give it couple of minutes to finish. (about 5min)";
     $handle = fopen($cbFile, "r");
     fgetcsv($handle, 0, ",");
     // remove the header row
     while (($row = fgetcsv($handle, 0, ",")) !== false) {
         // calculate ip range block
         $cidrRange = Ipv6Helper::calculateIpv6CidrRange($row[0]);
         // save the record
         $cityBlockEntity = new CityBlockIp6Entity();
         $cityBlockEntity->rangeStart = $cidrRange['start'];
         $cityBlockEntity->rangeEnd = $cidrRange['end'];
         $cityBlockEntity->geoId = (int) $row[1];
         $cityBlockEntity->save();
     }
     fclose($handle);
     // ensure range index
     $index = new SingleIndex('ipStart', 'rangeStart');
     $this->mongo->createIndex('GeoIpCityBlockIp6', $index);
     echo "\nCity IPv6 block import done\n";
 }