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 / functions.php

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

347 lines 9.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if (!defined('ABSPATH')){
4 exit;
5 }
6
7 /* function cookieadmin_died(){
8 print_r(error_get_last());
9 }
10 register_shutdown_function('cookieadmin_died'); */
11
12 // Checks if we are to update ?
13 function cookieadmin_update_check(){
14 global $wpdb;
15
16 $current_version = get_option('cookieadmin_version');
17 $version = (int) str_replace('.', '', $current_version);
18
19 // No update required
20 if($current_version == COOKIEADMIN_VERSION){
21 return true;
22 }
23
24 // Save the new Version
25 update_option('cookieadmin_version', COOKIEADMIN_VERSION);
26
27 }
28
29 function cookieadmin_load_plugin(){
30
31 global $cookieadmin, $cookieadmin_settings;
32
33 $cookieadmin_settings = get_option('cookieadmin_settings', array());
34
35 // Check if the installed version is outdated
36 cookieadmin_update_check();
37
38 ///////////////////////////
39 // Common loading
40 ///////////////////////////
41
42 if(wp_doing_ajax()){
43 add_action('wp_ajax_cookieadmin_ajax_handler', 'cookieadmin_ajax_handler');
44 add_action('wp_ajax_nopriv_cookieadmin_ajax_handler', 'cookieadmin_ajax_handler');
45 }
46
47 ///////////////////////////
48 // Admin loading
49 ///////////////////////////
50
51 if(is_admin()){
52 return cookieadmin_load_plugin_admin();
53 }
54
55 ///////////////////////////
56 // Enduser loading
57 ///////////////////////////
58
59 if(!empty($cookieadmin_settings['google_consent_mode_v2'])){
60 add_action('wp_head', '\CookieAdmin\Enduser::wp_head', 5);
61 }
62
63 add_action('wp_enqueue_scripts', '\CookieAdmin\Enduser::enqueue_scripts');
64
65 // Insert Cookie blocker in the head.
66 //add_action('send_headers', '\CookieAdmin\Enduser::cookieadmin_block_cookie_init_php', 100);
67 // add_action('init', '\CookieAdmin\Enduser::cookieadmin_block_cookie_head_js', 0);
68
69 //add Cookie Banner to user page
70 add_action('wp_footer', '\CookieAdmin\Enduser::cookieadmin_show_banner');
71
72 add_filter('script_loader_tag', '\CookieAdmin\Enduser::check_if_cookies_allowed', 10, 3);
73
74 }
75
76 function cookieadmin_load_plugin_admin(){
77
78 global $cookieadmin, $cookieadmin_settings;
79
80 if(!is_admin() || !current_user_can('administrator')){
81 return false;
82 }
83
84 add_action('admin_enqueue_scripts', '\CookieAdmin\Admin::enqueue_scripts');
85
86 add_action('admin_menu', '\CookieAdmin\Admin::cookieadmin_plugin_menu');
87
88 }
89
90 function cookieadmin_ajax_handler(){
91
92 $cookieadmin_fn = (!empty($_REQUEST['cookieadmin_act']) ? sanitize_text_field(wp_unslash($_REQUEST['cookieadmin_act'])) : '');
93
94 if(empty($cookieadmin_fn)){
95 wp_send_json_error(array('message' => 'Action not posted'));
96 }
97
98 // Define a whitelist of allowed functions
99 $user_allowed_actions = array();
100
101 $admin_allowed_actions = array(
102 'scan_cookies' => 'cookieadmin_scan_cookies',
103 'cookieadmin-auto-categorize' => 'cookieadmin_auto_configure_cookies',
104 'cookieadmin-edit-cookie' => 'cookieadmin_edit_cookies',
105 );
106
107 $general_actions = array(
108 'categorize_cookies' => 'cookieadmin_categorize_cookies',
109 );
110
111 if(array_key_exists($cookieadmin_fn, $user_allowed_actions)){
112
113 check_ajax_referer('cookieadmin_js_nonce', 'cookieadmin_security');
114 header_remove('Set-Cookie');
115 call_user_func('\CookieAdmin\Enduser::'.$user_allowed_actions[$cookieadmin_fn]);
116
117 }elseif(array_key_exists($cookieadmin_fn, $admin_allowed_actions)){
118
119 check_ajax_referer('cookieadmin_admin_js_nonce', 'cookieadmin_security');
120
121 if(!current_user_can('administrator')){
122 wp_send_json_error(array('message' => 'Sorry, but you do not have permissions to perform this action'));
123 }
124
125 call_user_func('\CookieAdmin\Admin::'.$admin_allowed_actions[$cookieadmin_fn]);
126
127 }elseif(array_key_exists($cookieadmin_fn, $general_actions)){
128
129 check_ajax_referer('cookieadmin_js_nonce', 'cookieadmin_security');
130 header_remove('Set-Cookie');
131 call_user_func($general_actions[$cookieadmin_fn]);
132
133 }else{
134 wp_send_json_error(array('message' => 'Unauthorized action'));
135 }
136
137 }
138
139 // Load policies from the file and database and merge them.
140 function cookieadmin_load_policy(){
141
142 $policy = get_option('cookieadmin_consent_settings', array());
143
144 if(empty($policy) || !is_array($policy)){
145 $policy = array();
146 }
147
148 if(!file_exists(COOKIEADMIN_DIR.'assets/cookie/policy.json')){
149 return $policy;
150 }
151
152 $j_policy = file_get_contents(COOKIEADMIN_DIR.'assets/cookie/policy.json');
153
154 $j_policy = json_decode($j_policy, true);
155
156 if(empty($j_policy) || !is_array($j_policy)){
157 return $policy;
158 }
159
160 return array_replace_recursive($j_policy, $policy);
161 }
162
163
164 //Loads consent data from file
165 function cookieadmin_load_consent_template($policy, $view){
166
167 if(!file_exists(COOKIEADMIN_DIR.'assets/cookie/template.json')){
168 return false;
169 }
170
171 $content = json_decode(file_get_contents(COOKIEADMIN_DIR.'assets/cookie/template.json'), true);
172
173 if(empty($content)){
174 return false;
175 }
176
177 $template = array();
178 $template[$view] = $content["cookieadmin_layout"][$policy["cookieadmin_layout"]];
179 $template[$view] .= $content["cookieadmin_modal"][$policy["cookieadmin_modal"]];
180 $template[$view] .= str_replace( '[[plugin_url]]', esc_url(COOKIEADMIN_PLUGIN_URL), $content["cookieadmin_reconsent"] );
181
182 return $template;
183
184 }
185
186 // Still in progress| No use for now.
187 function cookieadmin_compare_consent_id($consent_id) {
188
189 if (strlen($consent_id) !== 32) {
190 return false;
191 }
192
193 // Split into random part and signature
194 $random_part = substr($consent_id, 0, 16);
195 $provided_signature = substr($consent_id, 16, 16);
196
197 // Recompute the HMAC
198 $expected_hmac = hash_hmac('sha256', $random_part . $domain, $secret_key);
199 $expected_signature = substr($expected_hmac, 0, 16);
200
201 return hash_equals($provided_signature, $expected_signature);
202 }
203
204 function cookieadmin_r_print($array){
205 echo '<pre>';
206 print_r($array);
207 echo '</pre>';
208 }
209
210 function cookieadmin_load_cookies_csv($cookie_names = array(), $like = 0) {
211 global $wpdb;
212
213 $cookies_list = [];
214
215 $csv_file = COOKIEADMIN_DIR . 'assets/open-cookie-database/list.csv';
216
217 // Check if file exists
218 if ( ! file_exists( $csv_file ) ) {
219 return new WP_Error( 'csv_missing', 'The cookie CSV file is missing: '.$csv_file );
220 }
221
222 if ( ( $handle = fopen( $csv_file, 'r' ) ) !== FALSE ) {
223
224 $cookies_list = [];
225
226 $headers = fgetcsv( $handle, 10000, ",", "\"", "\\" );
227
228 while ( ( $data = fgetcsv( $handle, 10000, ",", "\"", "\\" ) ) !== FALSE ) {
229 // 0: cookie_id, 1: Platform, 2: Category, 3: Cookie / Data Key name,
230 // 4: Domain, 5: Description, 6: Retention period, 7: Data Controller,
231 // 8: User Privacy & GDPR Rights Portals, 9: Wildcard match
232
233 $cookie_id = isset( $data[0] ) ? trim( $data[0] ) : '';
234 $cookie_name = isset( $data[3] ) ? trim( $data[3] ) : '';
235 $platform = isset( $data[1] ) ? trim( $data[1] ) : '';
236 $category = isset( $data[2] ) ? trim( $data[2] ) : '';
237 $domain = isset( $data[4] ) ? trim( $data[4] ) : '';
238 $description = isset( $data[5] ) ? trim( $data[5] ) : '';
239 $retention = isset( $data[6] ) ? trim( $data[6] ) : '';
240 $wildcard = isset( $data[9] ) ? (int) $data[9] : 0;
241 $patterns = isset( $data[10] ) ? trim($data[10]) : '';
242
243 if ( empty( $cookie_id ) || empty( $cookie_name ) ) {
244 continue;
245 }
246
247 if(!empty($cookie_names)){
248
249 if(!empty($like)){
250
251 $matched = 0;
252 foreach($cookie_names as $prefix){
253 if (substr($cookie_name, 0, strlen($prefix)) === $prefix) {
254 $matched = 1;
255 break;
256 }
257 }
258
259 if(empty($matched)){
260 continue;
261 }
262
263 }else{
264 if(!in_array($cookie_name, $cookie_names)){
265 continue;
266 }
267 }
268 }
269
270 // Add the row to the current batch
271 $cookies_list[] = [
272 'cookie_id' => $cookie_id,
273 'cookie_name' => $cookie_name,
274 'platform' => $platform,
275 'category' => $category,
276 'domain' => $domain,
277 'description' => $description,
278 'retention' => $retention,
279 'wildcard' => $wildcard,
280 'patterns' => $patterns
281 ];
282 }
283
284 fclose( $handle );
285
286 } else {
287 return new WP_Error( 'csv_open_fail', 'Failed to open Cookies CSV file: '.$csv_file );
288 }
289
290 return $cookies_list;
291 }
292
293 function cookieadmin_categorize_cookies($cookies = []){
294
295 global $cookieadmin_lang, $cookieadmin_error, $cookieadmin_msg, $wpdb;
296
297 if(!empty($_REQUEST['cookieadmin_cookies'])){
298
299 $raw_cookies = json_decode( wp_unslash( $_REQUEST['cookieadmin_cookies'] ), true );
300
301 if ( is_array( $raw_cookies ) ) {
302 $sanitized_cookies = [];
303
304 array_walk( $raw_cookies, function( $value, $key ) use ( &$sanitized_cookies ) {
305 $sanitized_key = sanitize_key( $key );
306 $sanitized_cookies[ $sanitized_key ] = sanitize_text_field($value);
307 } );
308
309 unset($raw_cookies);
310 }
311 }else{
312 $sanitized_cookies = $cookies;
313 }
314
315
316 if(empty($sanitized_cookies)){
317 return [
318 'success' => false,
319 'data' => null,
320 'error' => 'Please provide valid cookie names.',
321 ];
322 }
323
324 $cookies_info = cookieadmin_load_cookies_csv(array_keys($sanitized_cookies));
325
326 if(empty($cookies_info) || is_wp_error($cookies_info)){
327 return [
328 'success' => false,
329 'data' => null,
330 'error' => 'Failed to load Cookies list',
331 ];
332 }
333
334 foreach($cookies_info as $info){
335 $sanitized_cookies[$info['cookie_name']]['source'] = !empty($info['domain']) ? $info['domain'] : "unknown";
336 $sanitized_cookies[$info['cookie_name']]['category'] = !empty($info['category']) ? strtolower($info['category']) : "un_c";
337 $sanitized_cookies[$info['cookie_name']]['description'] = !empty($info['description']) ? $info['description'] : "unknown";
338 $sanitized_cookies[$info['cookie_name']]['duration'] = !empty($info['retention']) ? $info['retention'] : "unknown";
339 $sanitized_cookies[$info['cookie_name']]['platform'] = !empty($info['platform']) ? $info['platform'] : "unknown";;
340 }
341
342 if(wp_doing_ajax()){
343 wp_send_json_success($sanitized_cookies);
344 }
345
346 return $sanitized_cookies;
347 }