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

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

153 lines 4.4 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 }
152
153