You are here

it_cookie_compliance.module in IT Cookie Compliance 7

IT Cookie Compliance module main file.

This module allows to make a website Cookies Compliant with respect to Italian Law Specifications by preemptively blocking cookies.

File

it_cookie_compliance.module
View source
<?php

/**
 * @file
 * IT Cookie Compliance module main file.
 *
 * This module allows to make a website Cookies Compliant
 * with respect to Italian Law Specifications by preemptively blocking cookies.
 */
function it_cookie_compliance_boot() {

  // Exit if the current script is running in a command-line environment.
  if (drupal_is_cli()) {
    return;
  }
  require_once "includes/path.inc";
  require_once "includes/common.inc";
  $url = request_path();
  $params = drupal_get_query_parameters();
  $acceptance_param = isset($_GET['cookie-blocked']);
  $not_accepted = it_cookie_compliance_are_cookie_not_accepted();
  if ($not_accepted && !$acceptance_param) {
    $params['cookie-blocked'] = 1;
    drupal_goto($url, array(
      'query' => $params,
    ));
  }
  elseif (!$not_accepted && $acceptance_param) {
    unset($params['cookie-blocked']);
    drupal_goto($url, array(
      'query' => $params,
    ));
  }
}

/**
 * Implements hook_permission().
 */
function it_cookie_compliance_permission() {
  return array(
    'administer preemptive cookie block' => array(
      'title' => 'administer preemptive cookie block',
    ),
  );
}

/**
 * Implements hook_menu().
 */
function it_cookie_compliance_menu() {
  $items = array();
  $items['admin/config/system/it_cookie_compliance'] = array(
    'title' => 'IT Cookie Compliance',
    'description' => 'Define the javascripts and blocks to be preemptively blocked when the cookies have not been yet accepted.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'it_cookie_compliance_admin_settings',
    ),
    'access arguments' => array(
      'administer preemptive cookie block',
    ),
    'type' => MENU_NORMAL_ITEM,
    'file' => 'it_cookie_compliance.admin.inc',
  );
  return $items;
}

/**
 * Implements hook_help().
 */
function it_cookie_compliance_help($path, $arg) {
  switch ($path) {
    case 'admin/help#it_cookie_compliance':
      $output = file_get_contents(drupal_get_path('module', 'it_cookie_compliance') . '/README.txt');
      return module_exists('markdown') ? filter_xss_admin(module_invoke('markdown', 'filter', 'process', 0, -1, $output)) : '<pre>' . check_plain($output) . '</pre>';
  }
}

/**
 * Implements hook_page_alter().
 *
 * Alter the pages to remove specified blocks and JSs when cookies have not been accepted.
 */
function it_cookie_compliance_page_alter(&$page) {
  if (it_cookie_compliance_are_cookie_not_accepted()) {
    it_cookie_compliance_exclude_js_from_page($page);
    it_cookie_compliance_exclude_blocks_from_page($page);
  }
}

/**
 * Implements hook_node_view_alter().
 *
 * Alters the node view to hide specified fields when cookies have not been accepted.
 */
function it_cookie_compliance_node_view_alter(&$build) {
  if (it_cookie_compliance_are_cookie_not_accepted()) {
    it_cookie_compliance_exclude_fields_from_node($build);
  }
}

/**
 * Checks whether cookies have not been accepted yet.
 *
 * @return boolean
 */
function it_cookie_compliance_are_cookie_not_accepted() {
  return !isset($_COOKIE['cookie-agreed']) || isset($_COOKIE['cookie-agreed']) && $_COOKIE['cookie-agreed'] < 2;
}

/**
 * Excludes all the javascripts specified in the configuration.
 *
 * @param array|string $page
 *   The pages where to remove the JSs.
 */
function it_cookie_compliance_exclude_js_from_page(&$page) {
  $scripts_to_exclude = preg_split("/[\\s,]+/", variable_get('it_cookie_compliance_javascript', ''));
  $scripts =& drupal_static('drupal_add_js', array());
  $scripts_copy = $scripts;
  foreach ($scripts_to_exclude as $script_to_exclude) {
    $script_to_exclude = truncate_utf8($script_to_exclude, 100);
    if ($script_to_exclude) {
      foreach ($scripts_copy as $script_key => $script_array) {
        if (is_string($script_array['data']) && strpos($script_array['data'], $script_to_exclude) !== FALSE) {
          unset($scripts[$script_key]);
        }
      }
    }
  }
}

/**
 * Excludes all the blocks specified in the configuration.
 *
 * @param array|string $page
 *   The pages where to remove the blocks.
 */
function it_cookie_compliance_exclude_blocks_from_page(&$page) {
  $blocks_to_exclude = preg_split("/[\\s,]+/", variable_get('it_cookie_compliance_blocks', ''));
  foreach ($blocks_to_exclude as $block_to_exclude) {
    $block_to_exclude = truncate_utf8($block_to_exclude, 100);
    if ($block_to_exclude) {
      foreach ($page as $region_name => $region) {
        if (is_array($region)) {
          foreach ($region as $possible_block_name => $possible_block) {
            if (isset($possible_block['#block'])) {
              if (strpos($possible_block_name, $block_to_exclude) !== FALSE) {
                $page[$region_name][$possible_block_name]['#access'] = FALSE;
              }
            }
          }
        }
      }
    }
  }
}
function it_cookie_compliance_exclude_fields_from_node(&$build) {
  $fields_to_exclude = preg_split("/[\\s,]+/", variable_get('it_cookie_compliance_fields', ''));
  foreach ($fields_to_exclude as $field_to_exclude) {
    $field_to_exclude = truncate_utf8($field_to_exclude, 100);
    if ($field_to_exclude) {
      $build[$field_to_exclude]['#access'] = false;
    }
  }
}

Functions

Namesort descending Description
it_cookie_compliance_are_cookie_not_accepted Checks whether cookies have not been accepted yet.
it_cookie_compliance_boot @file IT Cookie Compliance module main file.
it_cookie_compliance_exclude_blocks_from_page Excludes all the blocks specified in the configuration.
it_cookie_compliance_exclude_fields_from_node
it_cookie_compliance_exclude_js_from_page Excludes all the javascripts specified in the configuration.
it_cookie_compliance_help Implements hook_help().
it_cookie_compliance_menu Implements hook_menu().
it_cookie_compliance_node_view_alter Implements hook_node_view_alter().
it_cookie_compliance_page_alter Implements hook_page_alter().
it_cookie_compliance_permission Implements hook_permission().