| 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(empty($settings) || empty($settings['block_scripts'])){ |
| 105 |
return; |
| 106 |
} |
| 107 |
|
| 108 |
$view = get_option('cookieadmin_law', 'cookieadmin_gdpr'); |
| 109 |
$policy = cookieadmin_load_policy(); |
| 110 |
if(empty($policy) || empty($view)){ |
| 111 |
return; |
| 112 |
} |
| 113 |
|
| 114 |
ob_start([__CLASS__, 'update_tracking_scripts']); |
| 115 |
} |
| 116 |
|
| 117 |
static function update_tracking_scripts($html){ |
| 118 |
|
| 119 |
if(stripos($html, '<script') === false){ |
| 120 |
return $html; |
| 121 |
} |
| 122 |
|
| 123 |
if(empty(self::$categorized_cookies)){ |
| 124 |
return $html; |
| 125 |
} |
| 126 |
|
| 127 |
$cookieadmin_consent = isset($_COOKIE['cookieadmin_consent']) |
| 128 |
? json_decode(wp_unslash($_COOKIE['cookieadmin_consent']), true) |
| 129 |
: []; |
| 130 |
|
| 131 |
// Sanitizing cookies |
| 132 |
array_walk( $cookieadmin_consent, function( $value, $key ) use ( &$cookieadmin_consent ) { |
| 133 |
$sanitized_key = sanitize_key( $key ); |
| 134 |
$cookieadmin_consent[ $sanitized_key ] = sanitize_text_field($value); |
| 135 |
} ); |
| 136 |
|
| 137 |
$html = preg_replace_callback( |
| 138 |
'/<script\b([^>]*)>([\s\S]*?)<\/script>/i', |
| 139 |
function($match) use ($cookieadmin_consent){ |
| 140 |
$attrs = $match[1]; |
| 141 |
$content = $match[2]; |
| 142 |
$full_tag = $match[0]; |
| 143 |
|
| 144 |
if(preg_match('/\btype\s*=\s*["\']text\/plain["\']/i', $attrs)){ |
| 145 |
return $full_tag; |
| 146 |
} |
| 147 |
|
| 148 |
if(preg_match('/\b(id|src)\s*=\s*["\'][^"\']*cookieadmin[^"\']*["\']/i', $attrs)){ |
| 149 |
return $full_tag; |
| 150 |
} |
| 151 |
|
| 152 |
if(preg_match('/\btype\s*=\s*["\']([^"\']+)["\']/i', $attrs, $type_match)){ |
| 153 |
$type = strtolower(trim($type_match[1])); |
| 154 |
if($type !== 'text/javascript' && $type !== 'module'){ |
| 155 |
return $full_tag; |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
$src = ''; |
| 160 |
if(preg_match('/\bsrc\s*=\s*["\']([^"\']*)["\']/i', $attrs, $src_match)){ |
| 161 |
$src = $src_match[1]; |
| 162 |
} |
| 163 |
|
| 164 |
$match_against = !empty($src) ? $src : trim($attrs . ' ' . $content); |
| 165 |
|
| 166 |
if(empty($match_against)){ |
| 167 |
return $full_tag; |
| 168 |
} |
| 169 |
|
| 170 |
foreach (self::$categorized_cookies as $item) { |
| 171 |
$category = !empty($item->category) ? strtolower($item->category) : ''; |
| 172 |
$patterns = !empty($item->patterns) ? json_decode($item->patterns, true) : ''; |
| 173 |
|
| 174 |
if(empty($patterns) || empty($category)){ |
| 175 |
continue; |
| 176 |
} |
| 177 |
|
| 178 |
foreach ($patterns as $pattern) { |
| 179 |
if(strpos($match_against, $pattern) !== false){ |
| 180 |
if($category !== 'necessary' && |
| 181 |
(empty($cookieadmin_consent) || |
| 182 |
(!empty($cookieadmin_consent[$category]) && $cookieadmin_consent[$category] == 'false') || |
| 183 |
(!empty($cookieadmin_consent['reject']) && $cookieadmin_consent['reject'] == 'true') |
| 184 |
) |
| 185 |
){ |
| 186 |
if($attrs === ''){ |
| 187 |
return '<script type="text/plain" data-cookieadmin-category="' . esc_attr($category) . '">' . $content . '</script>'; |
| 188 |
} |
| 189 |
return '<script type="text/plain" data-cookieadmin-category="' . esc_attr($category) . '"' . $attrs . '>' . $content . '</script>'; |
| 190 |
} |
| 191 |
} |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
return $full_tag; |
| 196 |
}, |
| 197 |
$html |
| 198 |
); |
| 199 |
|
| 200 |
return $html; |
| 201 |
} |
| 202 |
|
| 203 |
static function cookieadmin_show_banner(){ |
| 204 |
|
| 205 |
$view = get_option('cookieadmin_law', 'cookieadmin_gdpr'); |
| 206 |
$policy = cookieadmin_load_policy(); |
| 207 |
|
| 208 |
// Do not show banner if banner is off via Geo rule |
| 209 |
if(empty($view)){ |
| 210 |
return; |
| 211 |
} |
| 212 |
|
| 213 |
$raw_template = cookieadmin_load_consent_template($policy[$view], $view); |
| 214 |
|
| 215 |
if(!is_array($raw_template) || empty($raw_template)){ |
| 216 |
return false; |
| 217 |
} |
| 218 |
|
| 219 |
$templates = implode('', $raw_template); |
| 220 |
|
| 221 |
$allowed_tags = cookieadmin_kses_allowed_html(); |
| 222 |
|
| 223 |
$templates = apply_filters('cookieadmin_after_banner', $templates); |
| 224 |
|
| 225 |
// var_dump($policy[$view]); |
| 226 |
echo wp_kses($templates, $allowed_tags); |
| 227 |
} |
| 228 |
|
| 229 |
static function cookieadmin_table_exists($table_name) { |
| 230 |
global $wpdb; |
| 231 |
|
| 232 |
$query = $wpdb->prepare("SHOW TABLES LIKE %s", $table_name); |
| 233 |
|
| 234 |
return $wpdb->get_var($query) === $table_name; |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
|