PluginProbe
CookieAdmin – Cookie Consent Banner / 1.0.7
CookieAdmin – Cookie Consent Banner v1.0.7
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.7, at includes/enduser.php

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