function eu_cookie_compliance_user_in_eu in EU Cookie Compliance (GDPR Compliance) 2.0.x
Same name and namespace in other branches
- 8 eu_cookie_compliance.module \eu_cookie_compliance_user_in_eu()
- 7.2 eu_cookie_compliance.module \eu_cookie_compliance_user_in_eu()
- 7 eu_cookie_compliance.module \eu_cookie_compliance_user_in_eu()
Check if the user is in the EU.
1 call to eu_cookie_compliance_user_in_eu()
- CheckIfEuCountryJsController::content in src/
Controller/ CheckIfEuCountryJsController.php - Check if visitor is in the EU.
File
- ./
eu_cookie_compliance.module, line 125 - The main file for the EU Cookie Compliance module.
Code
function eu_cookie_compliance_user_in_eu() {
$geoip_match = FALSE;
$eu_countries_default = [
NULL,
'AT',
'AX',
'BE',
'BG',
'CY',
'CZ',
'DE',
'DK',
'EE',
'EL',
'ES',
'EU',
'FI',
'FR',
'GB',
'GF',
'GI',
'GP',
'GR',
'HR',
'HU',
'IE',
'IS',
'IT',
'LI',
'LT',
'LU',
'LV',
'ME',
'MF',
'MQ',
'MT',
'NL',
'NO',
'PL',
'PT',
'RE',
'RO',
'SE',
'SI',
'SK',
'YT',
'UK',
];
// Allow custom array of countries to be loaded from settings.php, defaulting
// to the array above.
$config = \Drupal::config('eu_cookie_compliance.settings');
$eu_countries = !empty($config
->get('eu_countries')) ? $config
->get('eu_countries') : $eu_countries_default;
$ip_address = \Drupal::request()
->getClientIp();
// Try to get country_code by php extension.
$country_code = extension_loaded('geoip') ? geoip_country_code_by_name($ip_address) : '';
// Try to get country_code by smart_ip module.
if (\Drupal::moduleHandler()
->moduleExists('smart_ip')) {
/** @var \Drupal\smart_ip\SmartIpLocation $location_service */
$location_service = \Drupal::service('smart_ip.smart_ip_location');
$country_code = $location_service
->get('countryCode');
}
elseif (Drupal::moduleHandler()
->moduleExists('geoip')) {
$geo_location_service = \Drupal::service('geoip.geolocation');
$geo_ip_session = $geo_location_service
->geolocate($ip_address);
$country_code = !empty($geo_ip_session) ? $geo_ip_session : NULL;
}
// If the CloudFlare provided country header is available, use it as a
// fallback. See:
// https://support.cloudflare.com/hc/en-us/articles/200168236-What-does-Cloudflare-IP-Geolocation-do-
if (empty($country_code) && isset($_SERVER['HTTP_CF_IPCOUNTRY'])) {
$country_code = $_SERVER['HTTP_CF_IPCOUNTRY'];
}
if (in_array($country_code, $eu_countries) || $country_code === '' || $country_code === '-') {
$geoip_match = TRUE;
}
return [
'country' => $country_code,
'in_eu' => $geoip_match,
];
}