PluginProbe
CookieAdmin – Cookie Consent Banner / 1.0.6
CookieAdmin – Cookie Consent Banner v1.0.6
1.2.3 1.2.2 1.2.1 1.2.0 1.1.9 trunk 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8
cookieadmin / includes / enduser.php

enduser.php in CookieAdmin – Cookie Consent Banner 1.0.6, at includes/enduser.php

226 lines 7.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace CookieAdmin;
4
5 if(!defined('COOKIEADMIN_VERSION') || !defined('ABSPATH')){
6 die('Hacking Attempt');
7 }
8
9 class Enduser{
10
11 static $http_cookies = array();
12 static $categorized_cookies = array();
13
14 static function enqueue_scripts(){
15 global $wpdb;
16
17 $view = get_option('cookieadmin_law', 'cookieadmin_gdpr');
18 $policy = cookieadmin_load_policy();
19 $table_name = esc_sql($wpdb->prefix . 'cookieadmin_cookies');
20 //cookieadmin_r_print($view);
21 //cookieadmin_r_print($policy);
22
23 if(!empty($policy) && !empty($view)){
24
25 wp_enqueue_style('cookieadmin-style', COOKIEADMIN_PLUGIN_URL . 'assets/css/cookie.css', [], COOKIEADMIN_VERSION);
26
27 wp_enqueue_script('cookieadmin_js', COOKIEADMIN_PLUGIN_URL . 'assets/js/consent.js', [], COOKIEADMIN_VERSION, 'async');
28
29 $policy[$view]['ajax_url'] = admin_url('admin-ajax.php');
30 $policy[$view]['nonce'] = wp_create_nonce('cookieadmin_js_nonce');
31 $policy[$view]['http_cookies'] = self::$http_cookies;
32 $policy[$view]['home_url'] = home_url();
33 $policy[$view]['plugin_url'] = COOKIEADMIN_URL;
34 $policy[$view]['is_pro'] = (defined('COOKIEADMIN_PREMIUM') ? COOKIEADMIN_PREMIUM : 0);
35 // cookieadmin_r_print($policy);die();
36
37 $rows = $wpdb->get_results("SELECT cookie_name, category, expires, description, patterns FROM {$table_name}");
38 $cookie_data = array();
39
40 foreach ($rows as $row) {
41 $cookie_data[$row->cookie_name] = $row;
42 }
43
44 $policy[$view]['categorized_cookies'] = self::$categorized_cookies = $cookie_data;
45
46 wp_localize_script('cookieadmin_js', 'cookieadmin_policy', $policy[$view]);
47 }
48 }
49
50 /* static function cookieadmin_block_cookie_init_php(){
51
52 //New - To catch, remove and send cookies in WP enqueue
53 $http_cookies = array();
54 $headers = headers_list();
55
56 foreach($headers as $header) {
57
58 if (stripos(trim($header), 'Set-Cookie:') === 0) {
59 $header = trim(substr($header, strlen('Set-Cookie:')));
60 $name = trim(explode('=', $header)[0]);
61 $http_cookies[$name]['string'] = trim($header);
62 setcookie($name, '', time() - 999999, '/');
63 }
64 }
65
66 $http_cookies['cookieadmin_consent'] = ["string" => "cookieadmin_consent=CookieAdmin Cookie Initialization"];
67
68 self::$http_cookies = $http_cookies;
69 } */
70
71 static function check_if_cookies_allowed($tag, $handle, $src){
72
73 $cookieadmin_consent = isset($_COOKIE['cookieadmin_consent'])
74 ? json_decode(wp_unslash($_COOKIE['cookieadmin_consent']), true)
75 : [];
76
77 array_walk( $cookieadmin_consent, function( $value, $key ) use ( &$cookieadmin_consent ) {
78 $sanitized_key = sanitize_key( $key );
79 $cookieadmin_consent[ $sanitized_key ] = sanitize_text_field($value);
80 } );
81
82 foreach (self::$categorized_cookies as $item) {
83 $category = strtolower($item->category);
84 $patterns = json_decode($item->patterns, true);
85
86 if (!empty($patterns) && !empty($category)) {
87 foreach ($patterns as $pattern) {
88 if (strpos($src, $pattern) !== false) {
89
90 if ( $category !== 'necessary' &&
91 (empty($cookieadmin_consent) ||
92 (!empty($cookieadmin_consent[$category]) && $cookieadmin_consent[$category] == 'false') ||
93 $cookieadmin_consent['reject'] == 'true')
94 ) {
95
96 // User has NOT consented -> block the script
97
98 // Option 1 - completely remove script:
99 // return '';
100
101 // Option 2 - transform to type="text/plain"
102 $tag = str_replace(
103 '<script ',
104 '<script type="text/plain" data-cookieadmin-category="' . esc_attr($category) . '" ',
105 $tag
106 );
107
108 return $tag;
109 }
110 }
111 }
112 }
113 }
114
115 return $tag;
116 }
117
118 static function cookieadmin_show_banner(){
119
120 $view = get_option('cookieadmin_law', 'cookieadmin_gdpr');
121 $policy = cookieadmin_load_policy();
122
123 $allowed_tags = wp_kses_allowed_html( 'post' );
124
125 // Add input tag for cookie consent form
126 $allowed_tags['input'] = array(
127 'type' => true,
128 'name' => true,
129 'value' => true,
130 'class' => true,
131 'id' => true,
132 'checked' => true,
133 'disabled' => true,
134 'placeholder' => true,
135 );
136
137 $templates = wp_kses(implode("", cookieadmin_load_consent_template($policy[$view], $view)), $allowed_tags);
138
139 // var_dump($policy[$view]);
140 echo $templates;
141 }
142
143 static function cookieadmin_table_exists($table_name) {
144 global $wpdb;
145
146 $query = $wpdb->prepare("SHOW TABLES LIKE %s", $table_name);
147
148 return $wpdb->get_var($query) === $table_name;
149 }
150
151 static function wp_head() {
152
153 $policy = cookieadmin_load_policy();
154
155 $law = get_option('cookieadmin_law', 'cookieadmin_gdpr');
156
157 $cookieadmin_default_allowed = (!empty($policy[$law]['preload']) ? $policy[$law]['preload'] : []);
158 $cookieadmin_default_categories = ['functional', 'analytics', 'marketing', 'accept', 'reject'];
159
160 $cookieadmin_js_preferences = [];
161 foreach ($cookieadmin_default_categories as $category) {
162 $cookieadmin_js_preferences[$category] = (!empty($cookieadmin_default_allowed) && in_array($category, $cookieadmin_default_allowed) ? true : false);
163 }
164
165 $cookieadmin_js_preferences_json = json_encode($cookieadmin_js_preferences);
166 $inline_js = <<<JS
167
168 window.dataLayer = window.dataLayer || [];
169 function gtag(){dataLayer.push(arguments);}
170
171 function cookieadmin_update_gcm(update) {
172
173 let cookieadmin_preferences = $cookieadmin_js_preferences_json;
174
175 const cookieAdminMatch = document.cookie.match(/(?:^|; )cookieadmin_consent=([^;]*)/);
176
177 if (cookieAdminMatch) {
178 try {
179 const cookieadmin_parsed = JSON.parse(decodeURIComponent(cookieAdminMatch[1]));
180 cookieadmin_preferences.functional = cookieadmin_parsed.functional === "true";
181 cookieadmin_preferences.analytics = cookieadmin_parsed.analytics === "true";
182 cookieadmin_preferences.marketing = cookieadmin_parsed.marketing === "true";
183 cookieadmin_preferences.accept = cookieadmin_parsed.accept === "true";
184 cookieadmin_preferences.reject = cookieadmin_parsed.reject === "true";
185 } catch (err) {
186
187 }
188 }
189
190 if (typeof gtag === 'function') {
191
192 let cookieadmin_gtag_mode = update === 1 ? 'update' : 'default';
193
194 try {
195
196 gtag('consent', cookieadmin_gtag_mode, {
197 'ad_storage': cookieadmin_preferences.marketing || cookieadmin_preferences.accept ? 'granted' : 'denied',
198 'analytics_storage': cookieadmin_preferences.analytics || cookieadmin_preferences.accept ? 'granted' : 'denied',
199 'ad_user_data': cookieadmin_preferences.marketing || cookieadmin_preferences.accept ? 'granted' : 'denied',
200 'ad_personalization': cookieadmin_preferences.marketing || cookieadmin_preferences.accept ? 'granted' : 'denied',
201 'personalization_storage': cookieadmin_preferences.marketing || cookieadmin_preferences.accept ? 'granted' : 'denied',
202 'security_storage': 'granted',
203 'functionality_storage': 'granted'
204 });
205
206 } catch (e) {
207
208 }
209 }
210 }
211
212 cookieadmin_update_gcm(0);
213
214 JS;
215
216 wp_register_script('cookieadmin-gcm', '', [], null, false);
217
218 wp_add_inline_script('cookieadmin-gcm', $inline_js);
219
220 wp_enqueue_script('cookieadmin-gcm');
221
222 }
223
224 }
225
226