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

234 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) && !cookieadmin_is_editor_mode()){
24
25 wp_enqueue_style('cookieadmin-style', COOKIEADMIN_PLUGIN_URL . 'assets/css/consent.css', [], COOKIEADMIN_VERSION);
26
27 $js_deps = [];
28 // Free consent.js is the base script from where the functionality gets triggered
29 // So we need to make sure the dependencies of free script gets loaded first
30 // Like the pro/consent.js is a dependency of the free one.
31 if(defined('COOKIEADMIN_PREMIUM')){
32 $js_deps[] = 'cookieadmin_pro_js';
33 }
34
35 wp_enqueue_script('cookieadmin_js', COOKIEADMIN_PLUGIN_URL . 'assets/js/consent.js', $js_deps, COOKIEADMIN_VERSION);
36
37 $policy[$view]['ajax_url'] = admin_url('admin-ajax.php');
38 $policy[$view]['nonce'] = wp_create_nonce('cookieadmin_js_nonce');
39 $policy[$view]['http_cookies'] = self::$http_cookies;
40 $policy[$view]['home_url'] = home_url();
41 $policy[$view]['plugin_url'] = COOKIEADMIN_URL;
42 $policy[$view]['is_pro'] = (defined('COOKIEADMIN_PREMIUM') ? COOKIEADMIN_PREMIUM : 0);
43 $policy[$view]['ssl'] = is_ssl();
44
45 $base_path = parse_url(home_url(), PHP_URL_PATH) ?: '/';
46 $base_path = ($base_path !== '/') ? rtrim($base_path, '/') . '/' : '/';
47
48 // Used for setting cookie
49 $policy[$view]['base_path'] = $base_path;
50
51 // NOTE: Check the polylang string registration if changing these
52 $policy[$view]['lang']['show_less'] = __('Show less', 'cookieadmin');
53 $policy[$view]['lang']['duration'] = __('Duration', 'cookieadmin');
54 $policy[$view]['lang']['session'] = __('Session', 'cookieadmin');
55 $policy[$view]['lang']['days'] = __('Days', 'cookieadmin');
56
57 // cookieadmin_r_print($policy);die();
58
59 $rows = $wpdb->get_results("SELECT cookie_name, category, expires, description, patterns FROM {$table_name}");
60 $cookie_data = array();
61
62 foreach ($rows as $row) {
63 $cookie_data[$row->cookie_name] = $row;
64 }
65
66 $policy[$view]['categorized_cookies'] = self::$categorized_cookies = $cookie_data;
67
68 $policy[$view] = apply_filters('cookieadmin_before_localize', $policy[$view]);
69
70 wp_localize_script('cookieadmin_js', 'cookieadmin_policy', $policy[$view]);
71
72 }
73 }
74
75 /* static function cookieadmin_block_cookie_init_php(){
76
77 //New - To catch, remove and send cookies in WP enqueue
78 $http_cookies = array();
79 $headers = headers_list();
80
81 foreach($headers as $header) {
82
83 if (stripos(trim($header), 'Set-Cookie:') === 0) {
84 $header = trim(substr($header, strlen('Set-Cookie:')));
85 $name = trim(explode('=', $header)[0]);
86 $http_cookies[$name]['string'] = trim($header);
87 setcookie($name, '', time() - 999999, '/');
88 }
89 }
90
91 $http_cookies['cookieadmin_consent'] = ["string" => "cookieadmin_consent=CookieAdmin Cookie Initialization"];
92
93 self::$http_cookies = $http_cookies;
94 } */
95
96 static function block_scripts(){
97
98 if(wp_doing_ajax() || is_admin() || defined('REST_REQUEST') || defined('COOKIEADMIN_SCANNER') || cookieadmin_is_editor_mode()){
99 return;
100 }
101
102 $settings = get_option('cookieadmin_settings');
103
104 // If block scripts is disabled, we don't need to make any changes
105 if(empty($settings) || empty($settings['block_scripts'])){
106 return;
107 }
108
109 $view = get_option('cookieadmin_law', 'cookieadmin_gdpr');
110 $policy = cookieadmin_load_policy();
111 if(empty($policy) || empty($view)){
112 return;
113 }
114
115 ob_start([__CLASS__, 'update_tracking_scripts']);
116 }
117
118 static function update_tracking_scripts($html){
119
120 if(stripos($html, '<script') === false){
121 return $html;
122 }
123
124 if(empty(self::$categorized_cookies)){
125 return $html;
126 }
127
128 $cookieadmin_consent = isset($_COOKIE['cookieadmin_consent'])
129 ? json_decode(wp_unslash($_COOKIE['cookieadmin_consent']), true)
130 : [];
131
132 // Sanitizing cookies
133 array_walk( $cookieadmin_consent, function( $value, $key ) use ( &$cookieadmin_consent ) {
134 $sanitized_key = sanitize_key( $key );
135 $cookieadmin_consent[ $sanitized_key ] = sanitize_text_field($value);
136 } );
137
138 $html = preg_replace_callback(
139 '/<script\b([^>]*)>([\s\S]*?)<\/script>/i',
140 function($match) use ($cookieadmin_consent){
141 $attrs = $match[1];
142 $content = $match[2];
143 $full_tag = $match[0];
144
145 if(preg_match('/\btype\s*=\s*["\']text\/plain["\']/i', $attrs)){
146 return $full_tag;
147 }
148
149 if(preg_match('/\b(id|src)\s*=\s*["\'][^"\']*cookieadmin[^"\']*["\']/i', $attrs)){
150 return $full_tag;
151 }
152
153 if(preg_match('/\btype\s*=\s*["\']([^"\']+)["\']/i', $attrs, $type_match)){
154 $type = strtolower(trim($type_match[1]));
155 if($type !== 'text/javascript' && $type !== 'module'){
156 return $full_tag;
157 }
158 }
159
160 $src = '';
161 if(preg_match('/\bsrc\s*=\s*["\']([^"\']*)["\']/i', $attrs, $src_match)){
162 $src = $src_match[1];
163 }
164
165 $match_against = !empty($src) ? $src : trim($attrs . ' ' . $content);
166
167 if(empty($match_against)){
168 return $full_tag;
169 }
170
171 foreach (self::$categorized_cookies as $item) {
172 $category = !empty($item->category) ? strtolower($item->category) : '';
173 $patterns = !empty($item->patterns) ? json_decode($item->patterns, true) : '';
174
175 if(empty($patterns) || empty($category)){
176 continue;
177 }
178
179 foreach ($patterns as $pattern) {
180 if(strpos($match_against, $pattern) !== false){
181 if($category !== 'necessary' &&
182 (empty($cookieadmin_consent) ||
183 (!empty($cookieadmin_consent[$category]) && $cookieadmin_consent[$category] == 'false') ||
184 (!empty($cookieadmin_consent['reject']) && $cookieadmin_consent['reject'] == 'true')
185 )
186 ){
187 if($attrs === ''){
188 return '<script type="text/plain" data-cookieadmin-category="' . esc_attr($category) . '">' . $content . '</script>';
189 }
190 return '<script type="text/plain" data-cookieadmin-category="' . esc_attr($category) . '"' . $attrs . '>' . $content . '</script>';
191 }
192 }
193 }
194 }
195
196 return $full_tag;
197 },
198 $html
199 );
200
201 return $html;
202 }
203
204 static function cookieadmin_show_banner(){
205
206 $view = get_option('cookieadmin_law', 'cookieadmin_gdpr');
207 $policy = cookieadmin_load_policy();
208
209 $raw_template = cookieadmin_load_consent_template($policy[$view], $view);
210
211 if(!is_array($raw_template) || empty($raw_template)){
212 return false;
213 }
214
215 $templates = implode('', $raw_template);
216
217 $allowed_tags = cookieadmin_kses_allowed_html();
218
219 $templates = apply_filters('cookieadmin_after_banner', $templates);
220
221 // var_dump($policy[$view]);
222 echo wp_kses($templates, $allowed_tags);
223 }
224
225 static function cookieadmin_table_exists($table_name) {
226 global $wpdb;
227
228 $query = $wpdb->prepare("SHOW TABLES LIKE %s", $table_name);
229
230 return $wpdb->get_var($query) === $table_name;
231 }
232 }
233
234