Ejemplo n.º 1
0
 public function testIsp()
 {
     $reader = new Reader('maxmind-db/test-data/GeoIP2-ISP-Test.mmdb');
     $ipAddress = '1.128.0.0';
     $record = $reader->isp($ipAddress);
     $this->assertEquals(1221, $record->autonomousSystemNumber);
     $this->assertEquals('Telstra Pty Ltd', $record->autonomousSystemOrganization);
     $this->assertEquals('Telstra Internet', $record->isp);
     $this->assertEquals('Telstra Internet', $record->organization);
     $this->assertEquals($ipAddress, $record->ipAddress);
     $reader->close();
 }
 public function processIP($ip)
 {
     $status = null;
     $path = Config::inst()->get('IPInfoCache', 'GeoPath');
     if (!$path) {
         $path = $this->defaultPath;
     }
     if (!file_exists($path)) {
         user_error('Error loading Geo database', E_USER_ERROR);
     }
     $request['ip'] = $ip;
     $request['type'] = self::ipVersion($ip);
     if ($request['type'] == 'IPv4') {
         $isPrivate = self::isPrivateIP($ip);
         if ($isPrivate) {
             $status = self::setStatus('IP_ADDRESS_RESERVED', null, $status);
             return json_encode(array('status' => $status));
         }
     }
     $reader = new Reader($path);
     $record = $reader->city($ip);
     $countryCode = null;
     try {
         $result['location']['continent_code'] = $record->continent->code;
         $result['location']['continent_names'] = $record->continent->names;
         $countryCode = $record->country->isoCode;
         $result['location']['country_code'] = $countryCode;
         $result['location']['country_names'] = $record->country->names;
         $result['location']['postal_code'] = $record->postal->code;
         $result['location']['city_names'] = $record->city->names;
         $result['location']['latitude'] = $record->location->latitude;
         $result['location']['longitude'] = $record->location->longitude;
         $result['location']['time_zone'] = $record->location->timeZone;
     } catch (Exception $e) {
         $status = self::setStatus('GEOIP_EXCEPTION', $e, $status);
     }
     $pathISP = Config::inst()->get('IPInfoCache', 'GeoPathISP');
     if (!$pathISP) {
         $pathISP = $this->defaultPathISP;
     }
     if (!file_exists($pathISP)) {
         user_error('Error loading Geo ISP database', E_USER_ERROR);
     }
     $reader = new Reader($pathISP);
     $record = $reader->isp($ip);
     if ($record) {
         $result['organization']['name'] = $record->organization;
         $result['organization']['isp'] = $record->isp;
     }
     if ($status) {
         $statusArray['code'] = self::setStatus(null, null, $status);
         $statusArray['message'] = self::getStatusMessage($status);
         // do not cache a failure
         $this->json = json_encode(array('request' => $request, 'status' => $statusArray, 'result' => array('maxmind-geoip2' => $result)));
     } else {
         // return cached success message
         $statusArray['code'] = self::setStatus('SUCCESS_CACHED', null, null);
         $statusArray['message'] = self::getStatusMessage($statusArray['code']);
         $this->json = json_encode(array('request' => $request, 'status' => $statusArray, 'result' => array('maxmind-geoip2' => $result)));
     }
     // we write a different json object with a cached status to the DB
     $statusArray['code'] = self::setStatus('SUCCESS', null, $status);
     $statusArray['message'] = self::getStatusMessage($statusArray['code']);
     $dbJson = json_encode(array('request' => $request, 'status' => $statusArray, 'result' => array('maxmind-geoip2' => $result)));
     return $dbJson;
 }