public function processIP($ip)
 {
     $status = null;
     $result = array();
     $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'] = MarketoRegionalDriver::ipVersion($ip);
     if ($request['type'] == 'IPv4') {
         $isPrivate = MarketoRegionalDriver::isPrivateIP($ip);
         if ($isPrivate) {
             $status = self::setStatus('IP_ADDRESS_RESERVED', null, $status);
             return json_encode(array('status' => $status));
         }
     }
     $reader = new Reader('/usr/share/GeoIP/GeoLite2-City.mmdb');
     $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);
     }
     $geoRegion = null;
     if ($countryCode) {
         $geoRegion = GeoRegion::get()->filter('RegionCode', $countryCode)->first();
         if ($geoRegion && $geoRegion->exists()) {
             $result['location']['marketo_region_name'] = $geoRegion->Name;
             $result['location']['marketo_region_code'] = $geoRegion->RegionCode;
             $result['location']['marketo_region_time_zone'] = $geoRegion->TimeZone;
         }
     }
     if ($status) {
         // do not cache a failure
         return json_encode(array('request' => $request, 'status' => $status, 'result' => $result));
     } else {
         // return cached success message
         $status = self::setStatus('SUCCESS_CACHED', null, $status);
         $this->json = json_encode(array('request' => $request, 'status' => $status, 'result' => $result));
     }
     $dbStatus = self::setStatus('SUCCESS', null, null);
     $dbJson = json_encode(array('request' => $request, 'status' => $dbStatus, 'result' => $result));
     return $dbJson;
 }
 public function processIP($ip)
 {
     // setup the default marketo bject
     $request = Config::inst()->get('DefaultMarketoResponse', 'request');
     $statusArray = Config::inst()->get('DefaultMarketoResponse', 'status');
     $result = Config::inst()->get('DefaultMarketoResponse', 'result');
     $status = null;
     $path = Config::inst()->get('IPInfoCache', 'GeoPathCity');
     if (!$path) {
         $path = $this->defaultPath;
     }
     if (!file_exists($path)) {
         user_error('Error loading Geo database', E_USER_ERROR);
     }
     $request['ip'] = $ip;
     $request['type'] = MarketoRegionalDriver::ipVersion($ip);
     if ($request['type'] == 'IPv4') {
         $isPrivate = MarketoRegionalDriver::isPrivateIP($ip);
         if ($isPrivate) {
             $status = self::setStatus('IP_ADDRESS_RESERVED', null, $status);
         }
         $geo = geoip_open($path, GEOIP_STANDARD);
         $record = geoip_record_by_addr($geo, $ip);
     } else {
         /* Will add IPv6 checking later
            $path = '/usr/share/GeoIP/GeoLiteCityv6.dat';
            $geo = geoip_open($path, GEOIP_STANDARD);
            $record = geoip_record_by_addr_v6($geo, $ip);
            */
     }
     $countryCode = null;
     if ($record && is_object($record)) {
         try {
             $result['location']['continent_code'] = $record->continent_code;
             // fetch continent by continent_code
             //$result['location']['continent_names'] = $record->continent->names;
             $countryCode = $record->country_code;
             $result['location']['country_code'] = $countryCode;
             //$result['location']['country_names'] = $record->country->names;
             $result['location']['postal_code'] = $record->postal_code;
             $result['location']['city_name'] = $record->city;
             $result['location']['latitude'] = $record->latitude;
             $result['location']['longitude'] = $record->longitude;
             // get timezone from region code
             //$result['location']['time_zone'] = $record->location->timeZone;
         } catch (Exception $e) {
             $status = self::setStatus('GEOIP_EXCEPTION', $e, $status);
         }
     }
     $geoRegion = null;
     if ($countryCode) {
         $geoRegion = GeoRegion::get()->filter('RegionCode', $countryCode)->first();
         if ($geoRegion && $geoRegion->exists()) {
             $result['location']['marketo_region_name'] = $geoRegion->Name;
             $result['location']['marketo_region_code'] = $geoRegion->RegionCode;
             $result['location']['marketo_region_time_zone'] = $geoRegion->TimeZone;
         }
     }
     // fetch ISP details
     $pathISP = Config::inst()->get('IPInfoCache', 'GeoPathISP');
     if (!$pathISP) {
         $path = $this->defaultPathISP;
     }
     if (!file_exists($pathISP)) {
         user_error('Error loading Geo ISP database', E_USER_ERROR);
     }
     $isp = geoip_open($pathISP, GEOIP_STANDARD);
     if ($request['type'] == 'IPv4') {
         $record = geoip_name_by_addr($isp, $ip);
     } else {
         /* Will add IPv6 checking later
            $record = geoip_name_by_addr_v6($isp, $ip);
            */
     }
     if ($record) {
         $result['organization']['isp'] = $record;
     }
     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' => $result));
         return null;
     } else {
         // return cached success message
         $statusArray['code'] = self::setStatus('SUCCESS_CACHED', null, $status);
         $statusArray['message'] = self::getStatusMessage($status);
         $this->json = json_encode(array('request' => $request, 'status' => $statusArray, 'result' => $result));
     }
     // we write a different json object with a cached status to the DB
     $statusArray['code'] = self::setStatus('SUCCESS', null);
     $statusArray['message'] = self::getStatusMessage($statusArray['code']);
     $dbJson = json_encode(array('request' => $request, 'status' => $statusArray, 'result' => $result));
     return $dbJson;
 }