Pathologic was missing because of a .git folder inside.
[yaffs-website] / web / modules / contrib / eu_cookie_compliance / eu_cookie_compliance.module
1 <?php
2 /**
3  * @file
4  * This module intends to deal with the EU Directive on Privacy and Electronic
5  * Communications that comes into effect in the UK on 26th May 2012.
6  */
7
8 use Drupal\Core\Form\FormStateInterface;
9 use Drupal\Core\Path\AliasManager;
10 use Drupal\Core\Path\AliasStorage;
11 use Drupal\Core\Path\AliasWhitelist;
12 use Drupal\Core\Extension\ModuleHandler;
13 use Drupal\Core\Database\Database;
14 use Drupal\Core\Cache\Cache;
15 use Drupal\Core\Cache\DatabaseBackend;
16 use Drupal\Core\Language\LanguageManager;
17 use Drupal\Core\Url;
18 use Drupal\Component\Utility\SafeMarkup;
19 use Drupal\Component\Utility\Unicode;
20 use Drupal\Component\Utility\Html;
21
22 /**
23  * Implements hook_page_attachments().
24  */
25 function eu_cookie_compliance_page_attachments(&$attachments) {
26   $config = \Drupal::config('eu_cookie_compliance.settings');
27
28   // Check Add/Remove domains.
29   $domain_allow = TRUE;
30   $domain_option = $config->get('domains_option');
31
32   if (!empty($config->get('domains_list'))) {
33     global $base_url;
34
35     $domains_list = str_replace(array("\r\n", "\r"), "\n", $config->get('domains_list'));
36     $domains_list = explode("\n", $domains_list);
37     $domain_match = in_array($base_url, $domains_list);
38
39     if ($domain_option && $domain_match) {
40       $domain_allow = FALSE;
41     }
42
43     // @TODO: Ask about this thing.
44     if (!$domain_option && !$domain_match) {
45       $domain_allow = FALSE;
46     }
47   }
48
49   // Check exclude paths.
50   $path_match = FALSE;
51
52   if (!empty($config->get('exclude_paths'))) {
53     // @TODO: make this more robust.
54     $path = \Drupal::service('path.current')->getPath();
55     $path_match = \Drupal::service('path.matcher')->matchPath($path, $config->get('exclude_paths'));
56   }
57
58   // Unsupported GeoIP feature until those modules update their code.
59   $geoip_match = TRUE;
60
61   if ($config->get('popup_enabled') && \Drupal::currentUser()->hasPermission('display eu cookie compliance popup') && $geoip_match && $domain_allow && !$path_match) {
62     $language = Drupal::languageManager()->getCurrentLanguage();
63     // Array storage for caching full client data.
64
65     $data = array();
66     if ($cache = \Drupal::cache()->get('eu_cookie_compliance_client_settings.' . $language->getId())) {
67       $data = $cache->data;
68     }
69     else {
70       // Initialize some needed popup settings messages.
71       $popup_settings_messages = array(
72         'popup_agree_button_message',
73         'popup_disagree_button_message',
74         'popup_hide_button_message',
75         'popup_find_more_button_message'
76       );
77       foreach ($popup_settings_messages as $key) {
78         if (!isset($popup_settings[$key])) {
79           $popup_settings[$key] = '';
80         }
81       }
82       $data['css'] = '';
83       // Color overrides.
84       if ($config->get('popup_bg_hex') !== '' and $config->get('popup_text_hex') !== '') {
85         $position = $config->get('popup_position') ? 'top' : 'bottom';
86         $data['css'] = '#sliding-popup.sliding-popup-' . $position . ' {background:#' . Html::escape($config->get('popup_bg_hex')) . ';}'
87              . '#sliding-popup .popup-content #popup-text h2, #sliding-popup .popup-content #popup-text p {color:#' . Html::escape($config->get('popup_text_hex')) . ' !important;}';
88       }
89       $popup_text_info = str_replace(array("\r", "\n"), '', $config->get('popup_info.value'));
90       $popup_text_agreed = str_replace(array("\r", "\n"), '', $config->get('popup_agreed.value'));
91       $html_info = array(
92         '#theme' => 'eu_cookie_compliance_popup_info',
93         '#message' => check_markup($popup_text_info, $config->get('popup_info.format'), FALSE),
94         '#agree_button' => $config->get('popup_agree_button_message'),
95         '#disagree_button' => $config->get('popup_disagree_button_message'),
96       );
97       $html_agreed = array(
98         '#theme' => 'eu_cookie_compliance_popup_agreed',
99         '#message' => check_markup($popup_text_agreed, $config->get('popup_agreed.format'), FALSE),
100         '#hide_button' => $config->get('popup_hide_button_message'),
101         '#find_more_button' => $config->get('popup_find_more_button_message'),
102       );
103
104       $was_debugging = FALSE;
105
106       /**
107        * @var $twig_service Twig_Environment
108        */
109       $twig_service = \Drupal::service('twig');
110
111       if ($twig_service->isDebug()) {
112         $was_debugging = TRUE;
113         $twig_service->disableDebug();
114       }
115
116       $html_info = \Drupal::service('renderer')->renderRoot($html_info)->__toString();
117       $html_agreed = \Drupal::service('renderer')->renderRoot($html_agreed)->__toString();
118
119       if ($was_debugging) {
120         $twig_service->enableDebug();
121       }
122
123       $data['variables'] = array(
124         'popup_enabled'        => $config->get('popup_enabled'),
125         'popup_agreed_enabled' => $config->get('popup_agreed_enabled'),
126         'popup_hide_agreed'    => $config->get('popup_hide_agreed'),
127         'popup_clicking_confirmation' => $config->get('popup_clicking_confirmation'),
128         'popup_html_info'      => $config->get('popup_enabled') ? $html_info : FALSE,
129         'popup_html_agreed'    => $config->get('popup_agreed_enabled') ? $html_agreed : FALSE,
130         'popup_height'         => !empty($config->get('popup_height')) ? $config->get('popup_height') : 'auto',
131         'popup_width'          => !empty($config->get('popup_width')) ? $config->get('popup_width') : '100%',
132         'popup_delay'          => (int) ($config->get('popup_delay') * 1000),
133         'popup_link'           => $config->get('popup_link'),
134         'popup_link_new_window' => $config->get('popup_link_new_window'),
135         'popup_link'           => $config->get('popup_link'),
136         'popup_link_new_window' => !empty($config->get('popup_link_new_window')) ? $config->get('popup_link_new_window') : 1,
137         'popup_position'       => $config->get('popup_position'),
138         'popup_language'       => $language->getId(),
139         'popup_bg_hex'         => !empty($config->get('popup_bg_hex')) ? $config->get('popup_bg_hex') : FALSE,
140         'popup_text_hex'       => !empty($config->get('popup_text_hex')) ? $config->get('popup_text_hex') : FALSE,
141         'domain'               => $config->get('domain'),
142       );
143       \Drupal::cache()->set('eu_cookie_compliance_client_settings.' . $language->getId(), $data);
144     }
145
146     $attachments['#attached']['drupalSettings']['eu_cookie_compliance'] = $data['variables'];
147     $attachments['#attached']['library'][] = 'eu_cookie_compliance/eu_cookie_compliance';
148     $cache_tags = isset($attachments['#cache']['tags']) ? $attachments['#cache']['tags'] : [];
149     $attachments['#cache']['tags'] = Cache::mergeTags($cache_tags, $config->getCacheTags());
150   }
151 }
152
153 /**
154  * Implements hook_theme().
155  */
156 function eu_cookie_compliance_theme($existing, $type, $theme, $path) {
157   return array(
158     'eu_cookie_compliance_popup_info' => array(
159       'template' => 'eu_cookie_compliance_popup_info',
160       'variables' => array(
161         'message' => NULL,
162         'agree_button' => NULL,
163         'disagree_button' => NULL
164       ),
165     ),
166     'eu_cookie_compliance_popup_agreed' => array(
167       'template' => 'eu_cookie_compliance_popup_agreed',
168       'variables' => array(
169         'message' => NULL,
170         'hide_button' => NULL,
171         'find_more_button' => NULL
172       ),
173     ),
174   );
175 }
176
177 /**
178  * Validate field for a HEX value if a value is set.
179  */
180 function eu_cookie_compliance_validate_hex($element, FormStateInterface &$form_state) {
181   if (!empty($element['#value']) && !preg_match('/^[0-9a-fA-F]{3,6}$/', $element['#value'])) {
182     $form_state->setError($element, t('%name must be a HEX value (without leading #) or empty.', array('%name' => $element['#title'])));
183   }
184 }