PluginProbe
CookieAdmin – Cookie Consent Banner / 1.0.2
CookieAdmin – Cookie Consent Banner v1.0.2
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.2, at includes/functions.php

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