Example #1
0
 public static function getCountry($fallback = true)
 {
     if (($country = self::getEnv("envCountry")) === null) {
         try {
             $mmdb = array_reverse(glob(Config::$geoip . "GeoLite2-Country*.mmdb"))[0];
             $reader = new \GeoIp2\Database\Reader($mmdb);
             $ip = Request::getIp();
             $record = $reader->country($ip);
             $country = $record->country->isoCode;
             self::setCountry($country);
         } catch (\Exception $e) {
             Logger::instance()->logError($e->getMessage());
             if ($fallback) {
                 self::setCountry(Config::$defaultCountry);
             }
         }
     }
     return $country;
 }
 public static function getUserData($service, $ip, $params = array())
 {
     if ($service == 'mod_geoip2') {
         if (isset($_SERVER[$params['country_code']]) && isset($_SERVER[$params['country_name']])) {
             $normalizedObject = new stdClass();
             $normalizedObject->country_code = strtolower($_SERVER[$params['country_code']]);
             $normalizedObject->country_name = strtolower($_SERVER[$params['country_name']]);
             $normalizedObject->city = isset($_SERVER[$params['mod_geo_ip_city_name']]) ? $_SERVER[$params['mod_geo_ip_city_name']] : '';
             $normalizedObject->city .= isset($params['mod_geo_ip_region_name']) && isset($_SERVER[$params['mod_geo_ip_region_name']]) ? ', ' . $_SERVER[$params['mod_geo_ip_region_name']] : '';
             $normalizedObject->lat = isset($_SERVER[$params['mod_geo_ip_latitude']]) ? $_SERVER[$params['mod_geo_ip_latitude']] : '0';
             $normalizedObject->lon = isset($_SERVER[$params['mod_geo_ip_longitude']]) ? $_SERVER[$params['mod_geo_ip_longitude']] : '0';
             return $normalizedObject;
         } else {
             return false;
         }
     } elseif ($service == 'php_geoip') {
         if (function_exists('geoip_record_by_name')) {
             $data = @geoip_record_by_name($ip);
             if ($data !== null) {
                 $normalizedObject = new stdClass();
                 $normalizedObject->country_code = isset($data['country_code']) ? strtolower($data['country_code']) : '';
                 $normalizedObject->country_name = isset($data['country_name']) ? strtolower($data['country_name']) : '';
                 $normalizedObject->city = isset($data['city']) ? strtolower($data['city']) : '';
                 $normalizedObject->city .= isset($data['region']) ? ', ' . strtolower($data['region']) : '';
                 $normalizedObject->lat = isset($data['latitude']) ? strtolower($data['latitude']) : '';
                 $normalizedObject->lon = isset($data['longitude']) ? strtolower($data['longitude']) : '';
                 return $normalizedObject;
             } else {
                 return false;
             }
         } else {
             return false;
         }
     } elseif ($service == 'max_mind') {
         if ($params['detection_type'] == 'country') {
             try {
                 $reader = new GeoIp2\Database\Reader('var/external/geoip/GeoLite2-Country.mmdb');
                 $countryData = $reader->country($ip);
                 $normalizedObject = new stdClass();
                 $normalizedObject->country_code = strtolower($countryData->raw['country']['iso_code']);
                 $normalizedObject->country_name = $countryData->raw['country']['names']['en'];
                 $normalizedObject->city = '';
                 $normalizedObject->lat = '';
                 $normalizedObject->lon = '';
                 return $normalizedObject;
             } catch (Exception $e) {
                 return false;
             }
         } elseif ($params['detection_type'] == 'city') {
             try {
                 $reader = new GeoIp2\Database\Reader(isset($params['city_file']) && $params['city_file'] != '' ? $params['city_file'] : 'var/external/geoip/GeoLite2-City.mmdb');
                 $countryData = $reader->city($ip);
                 $normalizedObject = new stdClass();
                 $normalizedObject->country_code = strtolower($countryData->raw['country']['iso_code']);
                 $normalizedObject->country_name = $countryData->raw['country']['names']['en'];
                 $normalizedObject->lat = isset($countryData->raw['location']['latitude']) ? $countryData->raw['location']['latitude'] : '0';
                 $normalizedObject->lon = isset($countryData->raw['location']['longitude']) ? $countryData->raw['location']['longitude'] : '0';
                 try {
                     $normalizedObject->city = $countryData->city->name != '' ? $countryData->city->name : (isset($countryData->raw['location']['time_zone']) ? $countryData->raw['location']['time_zone'] : '');
                     $regionName = isset($countryData->raw['mostSpecificSubdivision']['name']) ? ', ' . $countryData->raw['mostSpecificSubdivision']['name'] : '';
                     $normalizedObject->city .= isset($countryData->mostSpecificSubdivision->isoCode) ? ', ' . $countryData->mostSpecificSubdivision->isoCode : '';
                     $normalizedObject->city .= $regionName;
                 } catch (Exception $e) {
                     // Just in case of city error
                 }
                 return $normalizedObject;
             } catch (Exception $e) {
                 return false;
             }
         }
         return false;
     } elseif ($service == 'freegeoip') {
         $response = self::executeRequest('http://freegeoip.net/json/' . $ip);
         if (!empty($response)) {
             $responseData = json_decode($response);
             if (is_object($responseData)) {
                 $normalizedObject = new stdClass();
                 $normalizedObject->country_code = strtolower($responseData->country_code);
                 $normalizedObject->country_name = $responseData->country_name;
                 $normalizedObject->lat = $responseData->latitude;
                 $normalizedObject->lon = $responseData->longitude;
                 $normalizedObject->city = $responseData->city . ($responseData->region_name != '' ? ', ' . $responseData->region_name : '');
                 return $normalizedObject;
             }
         }
         return false;
     } elseif ($service == 'ipinfodbcom') {
         $response = self::executeRequest("http://api.ipinfodb.com/v3/ip-city/?key={$params['api_key']}&ip={$ip}&format=json");
         if (!empty($response)) {
             $responseData = json_decode($response);
             if (is_object($responseData)) {
                 if ($responseData->statusCode == 'OK') {
                     $normalizedObject = new stdClass();
                     $normalizedObject->country_code = strtolower($responseData->countryCode);
                     $normalizedObject->country_name = $responseData->countryName;
                     $normalizedObject->lat = $responseData->latitude;
                     $normalizedObject->lon = $responseData->longitude;
                     $normalizedObject->city = $responseData->cityName . ($responseData->regionName != '' ? ', ' . $responseData->regionName : '');
                     return $normalizedObject;
                 }
             }
         }
         return false;
     } elseif ($service == 'locatorhq') {
         $ip = isset($params['ip']) && !empty($params['ip']) ? $params['ip'] : $ip;
         $response = self::executeRequest("http://api.locatorhq.com/?user={$params['username']}&key={$params['api_key']}&ip={$ip}&format=json");
         if (!empty($response)) {
             $responseData = json_decode($response);
             if (is_object($responseData)) {
                 $normalizedObject = new stdClass();
                 $normalizedObject->country_code = strtolower($responseData->countryCode);
                 $normalizedObject->country_name = $responseData->countryName;
                 $normalizedObject->lat = $responseData->latitude;
                 $normalizedObject->lon = $responseData->longitude;
                 $normalizedObject->city = $responseData->city . ($responseData->region != '' ? ', ' . $responseData->region : '');
                 return $normalizedObject;
             }
             return false;
         }
     }
     return false;
 }
 /**
  * Get the client record
  *
  * @return GeoIp2\Model\Country
  */
 public function getClientRecord()
 {
     if ($this->clientRecord) {
         return $this->clientRecord;
     }
     //skip the process incase mode is not frontend or GeoIp is deactivated
     if (!$this->isGeoIpEnabled()) {
         return null;
     }
     // Get stats controller to get client ip
     $statsComponentContoller = $this->getComponent('Stats');
     if (!$statsComponentContoller) {
         return null;
     }
     //Get the country name and code by using the ipaddress through GeoIp2 library
     $countryDb = $this->getDirectory() . '/Data/GeoLite2-Country.mmdb';
     $activeLocale = \FWLanguage::getLanguageCodeById(FRONTEND_LANG_ID);
     $locale = in_array($activeLocale, $this->availableLocale) ? $activeLocale : $this->defaultLocale;
     try {
         $reader = new \GeoIp2\Database\Reader($countryDb, array($locale));
         $this->clientRecord = $reader->country($statsComponentContoller->getCounterInstance()->getClientIp());
         return $this->clientRecord;
     } catch (\Exception $e) {
         \DBG::log($e->getMessage());
         return null;
     }
 }
 /**
  * Handle an incoming request.
  *
  * @param  \Illuminate\Http\Request  $request
  * @param  \Closure  $next
  * @return mixed
  */
 public function handle($request, Closure $next)
 {
     // Get country code from IP address
     $reader = new \GeoIp2\Database\Reader(storage_path('geoip2/GeoLite2-Country.mmdb'));
     try {
         $record = $reader->country(Request::ip());
         $countryCode = strtolower($record->country->isoCode);
     } catch (\GeoIp2\Exception\AddressNotFoundException $e) {
         $countryCode = Config::get('locale.default');
     }
     // Get language from browser
     if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
         $languageCode = strtolower(substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2));
     }
     // Do we have this country set-up?
     $locales = Config::get('locale.locales', []);
     $redirects = Locale::getRedirects();
     if (isset($locales[$countryCode]) or isset($locales[$countryCode . '-' . $languageCode]) or isset($redirects[$countryCode])) {
         // Check to see if this is the current country, if not we need to redirect
         if (Locale::getUrlPrefix() != $countryCode . '-' . $languageCode and Locale::getUrlPrefix() != $countryCode or !Request::segment(1)) {
             // Find the best locale based upon country and language
             if (isset($locales[$countryCode . '-' . $languageCode])) {
                 return redirect(Locale::getAlternateUrl($countryCode . '-' . $languageCode));
             }
             if (isset($locales[$countryCode])) {
                 return redirect(Locale::getAlternateUrl($countryCode));
             }
             if (isset($redirects[$countryCode])) {
                 return redirect(Locale::getAlternateUrl($redirects[$countryCode]));
             }
         }
     }
     return redirect(Locale::getAlternateUrl(Config::get('locale.default')));
 }