You are here

eu_cookie_compliance.module in EU Cookie Compliance (GDPR Compliance) 2.0.x

The main file for the EU Cookie Compliance module.

This module intends to deal with the EU Directive on Privacy and Electronic Communications that comes into effect in the UK on 26th May 2012.

File

eu_cookie_compliance.module
View source
<?php

/**
 * @file
 * The main file for the EU Cookie Compliance module.
 *
 * This module intends to deal with the EU Directive on Privacy and Electronic
 * Communications that comes into effect in the UK on 26th May 2012.
 */
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\eu_cookie_compliance\PersonalInformationFormInterface;

/**
 * Implements hook_help().
 */
function eu_cookie_compliance_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'help.page.eu_cookie_compliance':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('This module intends to deal with the EU Directive on Privacy and Electronic Communications that comes into effect on 26th May 2012. From that date, if you are not compliant or visibly working towards compliance, you run the risk of enforcement action, which can include a fine of up to half a million pounds for a serious breach.') . '</p>';
      $output .= '<h3>' . t('How it works') . '</h3>';
      $output .= '<p>' . t('The module displays a banner at the bottom or the top of website to make users aware of the fact that cookies are being set. The user may then give his/her consent or move to a page that provides more details. Consent is given by user pressing the agree buttons or by continuing browsing the website. Once consent is given another banner appears with a "Thank you" message.') . '</p>';
      $output .= '<p>' . t('The module provides a settings page where the banner can be customized. There are also template files for the banners that can be overridden by your theme.') . '</p>';
      $output .= '<h3>' . t('Installation') . '</h3>';
      $output .= '<ol><li>' . t('Unzip the files to the "sites/all/modules" OR "modules" directory and enable the module.') . '</li>';
      $output .= '<li>' . t('If desired, give the administer EU Cookie Compliance banner permissions that allow users of certain roles access the administration page. You can do so on the admin/user/permissions page.') . '</li>';

      // @codingStandardsIgnoreLine
      $output .= "<label>-&nbsp;</label>" . t("there is also a 'display eu cookie compliance banner' permission that helps you show the banner to the roles you desire.");
      $output .= '<li>' . t('You may want to create a page that would explain how your site uses cookies. Alternatively, if you have a privacy policy, you can link the banner to that page (see next step).') . '</li>';
      $output .= '<li>' . t('Go to the admin/config/system/eu-cookie-compliance page to configure and enable the banner.') . '</li>';
      $output .= '<li>' . t('If you want to customize the banner background and text color, either type in the hex values or simply install https://drupal.org/project/jquery_colorpicker.') . '</li>';
      $output .= '<li>' . t('If you want to theme your banner override the themes in the template file.') . '</li>';
      $output .= '<li>' . t('If you want to show the message in EU countries only, install the Smart IP module: https://drupal.org/project/smart_ip and enable the option on the admin page.') . '</li></ol>';
      $output .= '<p><b>' . t('NOTICE: The module does not audit your cookies nor does it prevent cookies from being set.') . '</b></p>';
      $output .= '<h3>' . t('For developers') . '</h3>';
      $output .= '<p>' . t('If you want to conditionally set cookies in your module, there is a javascript function provided that returns TRUE if the current user has given his consent:') . '</p>';
      $output .= '<p><code>Drupal.eu_cookie_compliance.hasAgreed()</code></p>';
      return [
        '#markup' => $output,
      ];
  }
}

/**
 * Implements hook_page_attachments().
 */
function eu_cookie_compliance_page_attachments(&$variables) {
}

/**
 * Builds the data structure for the cookie banner.
 *
 * @return array
 *   The data needed for the cookie banner.
 *
 */
function eu_cookie_compliance_build_data() {
}

/**
 * Implements hook_theme().
 */
function eu_cookie_compliance_theme($existing, $type, $theme, $path) {
}

/**
 * Converts a multiline, list of key|label|description strings to an array.
 *
 * @param string $string
 *   A multiline string containing key, label and optionally a description
 *   separated by a pipe symbol.
 *
 * @return array
 *   An array of categories the form
 *   [key => ['label' => label, 'description' => description]]
 */
function _eu_cookie_compliance_extract_category_key_label_description(string $string) {
  $categories = [];
  $list = explode("\n", $string);
  $list = array_map('trim', $list);
  $list = array_filter($list, 'strlen');
  foreach ($list as $text) {
    $parts = explode('|', $text);
    $num_parts = count($parts);
    if ($num_parts >= 3) {
      $key = trim($parts[0]);
      $label = trim($parts[1]);
      $description = trim($parts[2]);
    }
    elseif ($num_parts === 2) {
      $key = trim($parts[0]);
      $label = trim($parts[1]);
      $description = '';
    }
    else {
      $key = $label = trim($parts[0]);
      $description = '';
    }
    $categories[$key] = [
      'label' => $label,
      'description' => $description,
    ];
  }
  return $categories;
}

/**
 * Implements hook_form_alter().
 */
function eu_cookie_compliance_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  if ($form_state
    ->getFormObject() instanceof PersonalInformationFormInterface) {

    /** @var \Drupal\eu_cookie_compliance\PersonalInformationFormInterface $form_object */
    $form_object = $form_state
      ->getFormObject();
    $form_object
      ->formInjectGdprCheckbox($form, $form_state);
    $form['#submit'][] = [
      $form_object,
      'formSubmitGdprCheckbox',
    ];
  }
}

/**
 * Check if the user is in the EU.
 */
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,
  ];
}

/**
 * Helper function to set module weight.
 */
function _eu_cookie_compliance_module_set_weight() {
  $weight = 1;
  $exclude_modules = [
    'eu_cookie_compliance',
  ];

  // Allow other modules to exclude themselves.
  \Drupal::moduleHandler()
    ->alter('eu_cookie_compliance_module_weight_exclude', $exclude_modules);
  $extension_config = \Drupal::configFactory()
    ->get('core.extension');

  // Loop through all installed modules to find the highest weight.
  foreach ($extension_config
    ->get('module') as $module_name => $module_weight) {
    if ($module_weight > $weight && !in_array($module_name, $exclude_modules)) {
      $weight = $module_weight + 1;
    }
  }
  module_set_weight('eu_cookie_compliance', $weight);
}

Functions

Namesort descending Description
eu_cookie_compliance_build_data Builds the data structure for the cookie banner.
eu_cookie_compliance_form_alter Implements hook_form_alter().
eu_cookie_compliance_help Implements hook_help().
eu_cookie_compliance_page_attachments Implements hook_page_attachments().
eu_cookie_compliance_theme Implements hook_theme().
eu_cookie_compliance_user_in_eu Check if the user is in the EU.
_eu_cookie_compliance_extract_category_key_label_description Converts a multiline, list of key|label|description strings to an array.
_eu_cookie_compliance_module_set_weight Helper function to set module weight.