You are here

function eu_cookie_compliance_page_build in EU Cookie Compliance (GDPR Compliance) 7

Same name and namespace in other branches
  1. 7.2 eu_cookie_compliance.module \eu_cookie_compliance_page_build()

Implements hook_page_build().

1 call to eu_cookie_compliance_page_build()
eu_cookie_compliance_page_alter in ./eu_cookie_compliance.module
Implements hook_page_alter().

File

./eu_cookie_compliance.module, line 119
EU cookie compliance primary module file.

Code

function eu_cookie_compliance_page_build(&$page) {
  $popup_settings = eu_cookie_compliance_get_settings();
  global $theme, $user;

  // Check Add/Remove domains.
  $domain_allow = TRUE;
  $domain_option = isset($popup_settings['domains_option']) ? $popup_settings['domains_option'] : 1;
  if (!empty($popup_settings['domains_list'])) {
    global $base_url;
    $domains_list = str_replace(array(
      "\r\n",
      "\r",
    ), "\n", $popup_settings['domains_list']);
    $domains_list = explode("\n", $domains_list);
    $domains_list = preg_replace('{/$}', '', $domains_list);
    $domain_match = in_array($base_url, $domains_list);
    if ($domain_option && $domain_match) {
      $domain_allow = FALSE;
    }
    if (!$domain_option && !$domain_match) {
      $domain_allow = FALSE;
    }
  }

  // Check exclude paths.
  $path_match = FALSE;
  if (isset($popup_settings['exclude_paths'])) {
    $path = drupal_strtolower(drupal_get_path_alias($_GET['q']));
    $path_match = drupal_match_path($path, $popup_settings['exclude_paths']);
    if ($path != $_GET['q']) {
      $path_match = $path_match || drupal_match_path($_GET['q'], $popup_settings['exclude_paths']);
    }
    drupal_alter('eu_cookie_compliance_path_match', $path_match, $path, $popup_settings['exclude_paths']);
  }

  // Check exclude admin pages.
  $admin_page_match = FALSE;
  if (!empty($popup_settings['exclude_admin_pages']) && path_is_admin(current_path())) {
    $admin_page_match = TRUE;
  }
  $geoip_match = array(
    'in_eu' => TRUE,
  );
  if (isset($popup_settings['eu_only']) && $popup_settings['eu_only']) {
    $geoip_match = eu_cookie_compliance_user_in_eu();
  }

  // Allow other modules to alter the geo IP matching logic.
  drupal_alter('eu_cookie_compliance_geoip_match', $geoip_match);
  $uid1_match = TRUE;
  if ($user->uid == 1 && isset($popup_settings['exclude_uid_1']) && $popup_settings['exclude_uid_1']) {
    $uid1_match = FALSE;
  }

  // Allow other modules to alter if the banner needs to be shown or not.
  $modules_allow_popup = TRUE;
  drupal_alter('eu_cookie_compliance_show_popup', $modules_allow_popup);
  if (!empty($popup_settings['popup_enabled']) && user_access('display EU Cookie Compliance popup') && $geoip_match['in_eu'] && $domain_allow && !$path_match && !$admin_page_match && $uid1_match && $modules_allow_popup) {
    global $language;
    $method = !empty($popup_settings['method']) ? $popup_settings['method'] : 'opt_in';

    // Set default template (based on earlier version of module).
    if (!isset($popup_settings['info_template'])) {
      $popup_settings['info_template'] = $method === 'default' ? 'legacy' : 'new';
    }

    // Array storage for caching full client data.
    if (module_exists('domain')) {
      global $_domain;
      $cid = 'eu_cookie_compliance_client_settings:' . $language->language . ':' . $theme . ':' . $_domain['domain_id'];
    }
    else {
      $cid = 'eu_cookie_compliance_client_settings:' . $language->language . ':' . $theme;
    }
    $data = array();
    if ($cache = cache_get($cid, 'cache')) {
      $data = $cache->data;
    }
    else {

      // Initialize some needed popup settings messages.
      $popup_settings_messages = array(
        'popup_agree_button_message' => t('Accept'),
        'popup_disagree_button_message' => t('More info'),
        'popup_hide_button_message' => t('Hide'),
        'popup_find_more_button_message' => t('More info'),
      );
      foreach ($popup_settings_messages as $key => $value) {
        if (!isset($popup_settings[$key])) {
          $popup_settings[$key] = $value;
        }
      }

      // Color overrides.
      $data['css'] = '';
      $position = $popup_settings['popup_position'] ? 'top' : 'bottom';
      if (empty($popup_settings['use_bare_css'])) {
        if ($popup_settings['popup_bg_hex'] != '') {
          $data['css'] = '#sliding-popup.sliding-popup-' . $position . ', #sliding-popup.sliding-popup-' . $position . ' .eu-cookie-withdraw-banner, .eu-cookie-withdraw-tab {background:#' . check_plain($popup_settings['popup_bg_hex']) . ';}
          #sliding-popup.sliding-popup-' . $position . '.eu-cookie-withdraw-wrapper {background: transparent}';
        }
        if ($popup_settings['popup_text_hex'] != '') {
          $data['css'] .= '#sliding-popup .popup-content #popup-text h1, #sliding-popup .popup-content #popup-text h2, #sliding-popup .popup-content #popup-text h3, #sliding-popup .popup-content #popup-text p, #sliding-popup label, #sliding-popup div, .eu-cookie-compliance-secondary-button, .eu-cookie-withdraw-tab {color: #' . check_plain($popup_settings['popup_text_hex']) . ' !important;}
          .eu-cookie-withdraw-tab { border-color: #' . check_plain($popup_settings['popup_text_hex']) . ';}';
        }
        if ($popup_settings['info_template'] === 'new') {
          $data['css'] .= '.eu-cookie-compliance-more-button {color: #' . check_plain($popup_settings['popup_text_hex']) . ' !important;}';
        }
      }
      if (!empty($popup_settings['popup_position']) && $popup_settings['popup_position'] && !empty($popup_settings['fixed_top_position']) && $popup_settings['fixed_top_position']) {
        $data['css'] .= '#sliding-popup.sliding-popup-top { position: fixed; }';
      }
      $show_disagree_buttons = TRUE;
      if (array_key_exists('show_disagree_button', $popup_settings) && $popup_settings['show_disagree_button'] === 0) {
        $show_disagree_buttons = FALSE;
      }
      if ($method === 'auto') {
        $dnt = isset($_SERVER['HTTP_DNT']) ? $_SERVER['HTTP_DNT'] : NULL;
        if ((int) $dnt === 0 && $dnt !== NULL) {
          $method = 'default';
        }
        else {
          $method = 'opt_in';
        }
      }
      $withdraw_button_on_info_popup = $popup_settings['withdraw_button_on_info_popup'];
      $save_preferences_button_label = '';
      $info_templates = array(
        'legacy' => 'eu_cookie_compliance_popup_info_consent_default',
        'new' => 'eu_cookie_compliance_popup_info',
      );
      switch ($method) {
        case 'default':
          $click_confirmation = isset($popup_settings['popup_clicking_confirmation']) ? $popup_settings['popup_clicking_confirmation'] : TRUE;
          $scroll_confirmation = isset($popup_settings['popup_scrolling_confirmation']) ? $popup_settings['popup_scrolling_confirmation'] : FALSE;
          $primary_button_label = !empty($popup_settings['popup_agree_button_message']) ? filter_xss($popup_settings['popup_agree_button_message']) : 'Accept';
          $primary_button_class = 'agree-button eu-cookie-compliance-default-button';
          $secondary_button_label = '';
          $secondary_button_class = '';
          $privacy_settings_tab_label = FALSE;
          $withdraw_button_on_info_popup = FALSE;
          $popup_info_template = $info_templates[$popup_settings['info_template']];
          break;
        case 'opt_in':
          $click_confirmation = FALSE;
          $scroll_confirmation = FALSE;
          $primary_button_label = !empty($popup_settings['popup_agree_button_message']) ? filter_xss($popup_settings['popup_agree_button_message']) : 'Accept';
          $primary_button_class = 'agree-button eu-cookie-compliance-secondary-button';
          $secondary_button_label = !empty($popup_settings['disagree_button_label']) ? filter_xss($popup_settings['disagree_button_label']) : 'Decline';
          $secondary_button_class = 'decline-button eu-cookie-compliance-default-button';
          $popup_info_template = 'eu_cookie_compliance_popup_info';
          $privacy_settings_tab_label = $withdraw_button_on_info_popup ? $popup_settings['withdraw_tab_button_label'] : FALSE;
          break;
        case 'categories':
          $click_confirmation = FALSE;
          $scroll_confirmation = FALSE;
          if ($popup_settings['enable_save_preferences_button']) {
            $save_preferences_button_label = $popup_settings['save_preferences_button_label'];
            $primary_button_label = $popup_settings['accept_all_categories_button_label'];
          }
          else {
            $save_preferences_button_label = '';
            $primary_button_label = $popup_settings['popup_agree_button_message'];
          }
          $primary_button_class = 'agree-button eu-cookie-compliance-default-button';
          $secondary_button_label = $popup_settings['withdraw_action_button_label'];
          $secondary_button_class = 'eu-cookie-withdraw-button eu-cookie-compliance-hidden';
          $privacy_settings_tab_label = $withdraw_button_on_info_popup ? $popup_settings['withdraw_tab_button_label'] : FALSE;
          $popup_info_template = $info_templates[$popup_settings['info_template']];
          break;
        case 'opt_out':
          $click_confirmation = FALSE;
          $scroll_confirmation = FALSE;
          $primary_button_label = !empty($popup_settings['disagree_button_label']) ? filter_xss($popup_settings['disagree_button_label']) : 'Decline';
          $primary_button_class = 'decline-button eu-cookie-compliance-secondary-button';
          $secondary_button_label = !empty($popup_settings['popup_agree_button_message']) ? filter_xss($popup_settings['popup_agree_button_message']) : 'Accept';
          $secondary_button_class = 'agree-button eu-cookie-compliance-default-button';
          $privacy_settings_tab_label = $withdraw_button_on_info_popup ? $popup_settings['withdraw_tab_button_label'] : FALSE;
          $popup_info_template = 'eu_cookie_compliance_popup_info';
          break;
      }
      if (module_exists('translation') && isset($popup_settings['popup_link_translate']) && $popup_settings['popup_link_translate']) {
        $node_parts = explode('/', $popup_settings['popup_link']);
        if (isset($node_parts[1])) {
          $node = node_load($node_parts[1]);
          if ($node) {
            $translations = translation_node_get_translations($node->tnid);
            if (isset($translations[$language->language])) {
              $popup_settings['popup_link'] = $node_parts[0] . '/' . $translations[$language->language]->nid;
            }
          }
        }
      }
      $popup_text_info = str_replace(array(
        "\r",
        "\n",
      ), '', filter_xss_admin($popup_settings['popup_info']['value']));
      $popup_text_agreed = str_replace(array(
        "\r",
        "\n",
      ), '', filter_xss_admin($popup_settings['popup_agreed']['value']));
      $mobile_popup_text_info = str_replace(array(
        "\r",
        "\n",
      ), '', !empty($popup_settings['mobile_popup_info']['value']) ? filter_xss_admin($popup_settings['mobile_popup_info']['value']) : '');
      $withdraw_markup = str_replace(array(
        "\r",
        "\n",
      ), '', !empty($popup_settings['withdraw_message']['value']) ? filter_xss_admin($popup_settings['withdraw_message']['value']) : '');
      $cookie_categories = variable_get('eu_cookie_compliance_categories', array());
      $cookie_categories = $method === 'categories' ? $cookie_categories : FALSE;
      $html_info = theme($popup_info_template, array(
        'message' => check_markup($popup_text_info, $popup_settings['popup_info']['format'], FALSE),
        'agree_button' => $primary_button_label,
        'disagree_button' => $show_disagree_buttons ? filter_xss($popup_settings['popup_disagree_button_message']) : FALSE,
        'secondary_button_label' => $secondary_button_label,
        'primary_button_class' => $primary_button_class,
        'secondary_button_class' => $secondary_button_class,
        'cookie_categories' => $cookie_categories,
        'save_preferences_button_label' => $save_preferences_button_label,
        'privacy_settings_tab_label' => $privacy_settings_tab_label,
        'withdraw_button_on_info_popup' => $withdraw_button_on_info_popup,
        'method' => $method,
      ));
      $mobile_html_info = theme($popup_info_template, array(
        'message' => check_markup($mobile_popup_text_info, $popup_settings['popup_info']['format'], FALSE),
        'agree_button' => $primary_button_label,
        'disagree_button' => $show_disagree_buttons ? filter_xss($popup_settings['popup_disagree_button_message']) : FALSE,
        'secondary_button_label' => $secondary_button_label,
        'primary_button_class' => $primary_button_class,
        'secondary_button_class' => $secondary_button_class,
        'cookie_categories' => $cookie_categories,
        'save_preferences_button_label' => $save_preferences_button_label,
        'privacy_settings_tab_label' => $privacy_settings_tab_label,
        'withdraw_button_on_info_popup' => $withdraw_button_on_info_popup,
        'method' => $method,
      ));
      $html_agreed = theme('eu_cookie_compliance_popup_agreed', array(
        'message' => check_markup($popup_text_agreed, $popup_settings['popup_agreed']['format'], FALSE),
        'hide_button' => filter_xss($popup_settings['popup_hide_button_message']),
        'find_more_button' => $show_disagree_buttons ? filter_xss($popup_settings['popup_find_more_button_message']) : FALSE,
      ));
      $withdraw_markup = theme('eu_cookie_compliance_withdraw', array(
        'message' => check_markup($withdraw_markup, $popup_settings['withdraw_message']['format'], FALSE),
        'withdraw_tab_button_label' => !empty($popup_settings['withdraw_tab_button_label']) ? filter_xss($popup_settings['withdraw_tab_button_label']) : '',
        'withdraw_action_button_label' => !empty($popup_settings['withdraw_action_button_label']) ? filter_xss($popup_settings['withdraw_action_button_label']) : '',
      ));

      // Check if theme_debug is enabled.
      if (variable_get('theme_debug') == 1) {

        // Remove unwanted HTML comments.
        $html_info = preg_replace('/<!--(.|\\s)*?-->/', '', $html_info);
        $mobile_html_info = preg_replace('/<!--(.|\\s)*?-->/', '', $mobile_html_info);
        $html_agreed = preg_replace('/<!--(.|\\s)*?-->/', '', $html_agreed);
        $withdraw_markup = preg_replace('/<!--(.|\\s)*?-->/', '', $withdraw_markup);
      }
      $cookie_categories = variable_get('eu_cookie_compliance_categories', array());
      $data['variables'] = array(
        'cookie_policy_version' => isset($popup_settings['cookie_policy_version']) ? $popup_settings['cookie_policy_version'] : '1.0.0',
        'popup_enabled' => $popup_settings['popup_enabled'],
        'popup_agreed_enabled' => $popup_settings['popup_agreed_enabled'],
        'popup_hide_agreed' => isset($popup_settings['popup_hide_agreed']) ? $popup_settings['popup_hide_agreed'] : FALSE,
        'popup_clicking_confirmation' => $click_confirmation,
        'popup_scrolling_confirmation' => $scroll_confirmation,
        'popup_html_info' => empty($html_info) ? FALSE : trim($html_info),
        'use_mobile_message' => !empty($popup_settings['use_mobile_message']) ? $popup_settings['use_mobile_message'] : FALSE,
        'mobile_popup_html_info' => $popup_settings['popup_enabled'] ? $mobile_html_info : FALSE,
        'mobile_breakpoint' => !empty($popup_settings['mobile_breakpoint']) ? $popup_settings['mobile_breakpoint'] : '768',
        'popup_html_agreed' => empty($html_agreed) ? FALSE : trim($html_agreed),
        'popup_use_bare_css' => empty($popup_settings['use_bare_css']) ? FALSE : $popup_settings['use_bare_css'],
        'popup_height' => $popup_settings['popup_height'] !== '' ? (int) $popup_settings['popup_height'] : 'auto',
        'popup_width' => drupal_substr($popup_settings['popup_width'], -1) === '%' ? $popup_settings['popup_width'] : (int) $popup_settings['popup_width'],
        'popup_delay' => (int) $popup_settings['popup_delay'],
        'popup_link' => url(token_replace($popup_settings['popup_link'])),
        'popup_link_new_window' => isset($popup_settings['popup_link_new_window']) ? $popup_settings['popup_link_new_window'] : 1,
        'popup_position' => empty($popup_settings['popup_position']) ? NULL : $popup_settings['popup_position'],
        'fixed_top_position' => empty($popup_settings['fixed_top_position']) ? FALSE : $popup_settings['fixed_top_position'],
        'popup_language' => $language->language,
        'store_consent' => $popup_settings['consent_storage_method'] !== 'do_not_store',
        'better_support_for_screen_readers' => isset($popup_settings['better_support_for_screen_readers']) ? $popup_settings['better_support_for_screen_readers'] : 0,
        'reload_page' => isset($popup_settings['reload_page']) ? $popup_settings['reload_page'] : 0,
        'domain' => variable_get('eu_cookie_compliance_domain', ''),
        'domain_all_sites' => variable_get('eu_cookie_compliance_domain_all_sites'),
        'popup_eu_only_js' => isset($popup_settings['eu_only_js']) ? $popup_settings['eu_only_js'] : 0,
        'cookie_lifetime' => variable_get('eu_cookie_compliance_cookie_lifetime', 100),
        'cookie_session' => empty($popup_settings['cookie_session']) ? FALSE : $popup_settings['cookie_session'],
        'disagree_do_not_show_popup' => isset($popup_settings['disagree_do_not_show_popup']) ? $popup_settings['disagree_do_not_show_popup'] : 0,
        'method' => $method,
        'allowed_cookies' => !empty($popup_settings['allowed_cookies']) ? $popup_settings['allowed_cookies'] : '',
        'withdraw_markup' => $withdraw_markup,
        'withdraw_enabled' => !empty($popup_settings['withdraw_enabled']) ? $popup_settings['withdraw_enabled'] : FALSE,
        'withdraw_button_on_info_popup' => $popup_settings['withdraw_button_on_info_popup'],
        'cookie_categories' => is_array($cookie_categories) ? array_keys($cookie_categories) : FALSE,
        'cookie_categories_details' => $cookie_categories,
        'enable_save_preferences_button' => $popup_settings['enable_save_preferences_button'],
        'cookie_name' => !empty($popup_settings['cookie_name']) ? $popup_settings['cookie_name'] : '',
        'cookie_value_disagreed' => !empty($popup_settings['cookie_value_disagreed']) ? $popup_settings['cookie_value_disagreed'] : '0',
        'cookie_value_agreed_show_thank_you' => !empty($popup_settings['cookie_value_agreed_show_thank_you']) ? $popup_settings['cookie_value_agreed_show_thank_you'] : '1',
        'cookie_value_agreed' => !empty($popup_settings['cookie_value_agreed']) ? $popup_settings['cookie_value_agreed'] : '2',
        'containing_element' => !empty($popup_settings['containing_element']) ? $popup_settings['containing_element'] : 'body',
        'automatic_cookies_removal' => !empty($popup_settings['automatic_cookies_removal']) ? $popup_settings['automatic_cookies_removal'] : '',
      );

      // For some reason, we're getting the wrong language when editing the
      // localized form, so we shouldn't cache.
      if (empty($_GET['variable_realm_key_language'])) {
        cache_set($cid, $data, 'cache', CACHE_PERMANENT);
      }
    }
    if ($data['css']) {
      $cid = md5($data['css']);
      ctools_include('css');
      $filename = ctools_css_retrieve($cid);
      if (empty($filename)) {
        $filename = ctools_css_store($cid, $data['css'], FALSE);
      }
      drupal_add_css($filename, array(
        'weight' => 1000,
      ));
    }
    $script_scope = isset($popup_settings['script_scope']) ? $popup_settings['script_scope'] : 'footer';

    // Add inline javascript.
    $disabled_javascripts = isset($popup_settings['disabled_javascripts']) ? filter_xss($popup_settings['disabled_javascripts']) : '';
    $load_disabled_scripts = '';
    if ($disabled_javascripts != '') {
      $load_disabled_scripts = '';
      $disabled_javascripts = _eu_cookie_compliance_explode_multiple_lines($disabled_javascripts, FALSE);
      $disabled_javascripts = array_filter($disabled_javascripts, 'strlen');
      foreach ($disabled_javascripts as $script) {
        $parts = explode(':', $script);
        $category = NULL;
        if (count($parts) > 1 && $popup_settings['method'] === 'categories') {
          $category = array_shift($parts);
        }
        $script = implode(':', $parts);

        // Split the string if a | is present.
        // The second parameter (after the |) will be used to trigger a script
        // attach.
        $attach_name = '';
        if (strpos($script, '|') !== FALSE) {

          // Swallow a notice in case there is no behavior name.
          @(list($script, $attach_name) = explode('|', $script));
        }
        _eu_cookie_compliance_convert_relative_uri($script);
        if (substr($script, 0, 4) !== 'http' && substr($script, 0, 2) !== '//') {
          $script = '/' . $script;
        }
        if ($category) {
          $load_disabled_scripts .= 'if (category === "' . $category . '") {';
        }
        $load_disabled_scripts .= 'var scriptTag = document.createElement("script");';
        $load_disabled_scripts .= 'scriptTag.src = ' . drupal_json_encode($script) . ';';
        $load_disabled_scripts .= 'document.body.appendChild(scriptTag);';

        // The script will not immediately load, so we need to trigger the
        // attach in an interval function.
        if ($attach_name) {
          $load_disabled_scripts .= 'var EUCookieInterval' . $attach_name . '= setInterval(function() { if (Drupal.behaviors.' . $attach_name . ' !== undefined) { Drupal.behaviors.' . $attach_name . '.attach(document, Drupal.settings);clearInterval(EUCookieInterval' . $attach_name . ')};}, 100);';
        }
        if ($category) {
          $load_disabled_scripts .= '}';
        }
      }
    }
    if ($load_disabled_scripts) {
      drupal_add_js('window.euCookieComplianceLoadScripts = function(category) {' . $load_disabled_scripts . '}', array(
        'type' => 'inline',
        'scope' => $script_scope,
      ));
    }

    // Add the cookie name inline, since Drupal.settings will not be available
    // if the script is loaded in the header.
    drupal_add_js('window.eu_cookie_compliance_cookie_name = ' . drupal_json_encode(!empty($popup_settings['cookie_name']) ? $popup_settings['cookie_name'] : '') . ';', array(
      'type' => 'inline',
      'scope' => $script_scope,
    ));
    drupal_add_js(array(
      'eu_cookie_compliance' => $data['variables'],
    ), array(
      'type' => 'setting',
      'scope' => $script_scope,
    ));
    if (!isset($popup_settings['use_bare_css']) || $popup_settings['use_bare_css'] == 0) {
      drupal_add_css(drupal_get_path('module', 'eu_cookie_compliance') . '/css/eu_cookie_compliance.css');
    }
    else {
      drupal_add_css(drupal_get_path('module', 'eu_cookie_compliance') . '/css/eu_cookie_compliance.bare.css');
    }
    drupal_add_library('system', 'jquery.cookie');
    drupal_add_js(drupal_get_path('module', 'eu_cookie_compliance') . '/js/eu_cookie_compliance.js', array(
      'type' => 'file',
      'scope' => $script_scope,
      'group' => JS_DEFAULT,
      'weight' => 0,
      'defer' => TRUE,
    ));
  }
}