| 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 |
if(!cookieadmin_is_editor_mode()){ |
| 71 |
add_action('wp_footer', '\CookieAdmin\Enduser::cookieadmin_show_banner'); |
| 72 |
} |
| 73 |
|
| 74 |
add_filter('script_loader_tag', '\CookieAdmin\Enduser::check_if_cookies_allowed', 10, 3); |
| 75 |
|
| 76 |
} |
| 77 |
|
| 78 |
function cookieadmin_load_plugin_admin(){ |
| 79 |
|
| 80 |
global $cookieadmin, $cookieadmin_settings; |
| 81 |
|
| 82 |
if(!is_admin() || !current_user_can('administrator')){ |
| 83 |
return false; |
| 84 |
} |
| 85 |
|
| 86 |
add_action('admin_enqueue_scripts', '\CookieAdmin\Admin::enqueue_scripts'); |
| 87 |
|
| 88 |
add_action('admin_menu', '\CookieAdmin\Admin::cookieadmin_plugin_menu'); |
| 89 |
|
| 90 |
// === Plugin Update Notice === // |
| 91 |
$plugin_update_notice = get_option('softaculous_plugin_update_notice', []); |
| 92 |
$available_update_list = get_site_transient('update_plugins'); |
| 93 |
$plugin_path_slug = 'cookieadmin/cookieadmin.php'; |
| 94 |
|
| 95 |
if( |
| 96 |
!empty($available_update_list) && |
| 97 |
is_object($available_update_list) && |
| 98 |
!empty($available_update_list->response) && |
| 99 |
!empty($available_update_list->response[$plugin_path_slug]) && |
| 100 |
(empty($plugin_update_notice) || empty($plugin_update_notice[$plugin_path_slug]) || (!empty($plugin_update_notice[$plugin_path_slug]) && |
| 101 |
version_compare($plugin_update_notice[$plugin_path_slug], $available_update_list->response[$plugin_path_slug]->new_version, '<'))) |
| 102 |
){ |
| 103 |
add_action('admin_notices', '\CookieAdmin\Admin::plugin_update_notice'); |
| 104 |
add_filter('softaculous_plugin_update_notice', '\CookieAdmin\Admin::plugin_update_notice_filter'); |
| 105 |
} |
| 106 |
// === Plugin Update Notice END === // |
| 107 |
|
| 108 |
} |
| 109 |
|
| 110 |
function cookieadmin_is_editor_mode(){ |
| 111 |
|
| 112 |
if (isset($_GET['pagelayer-live']) || isset($_GET['fl_builder'])) { |
| 113 |
return true; |
| 114 |
} |
| 115 |
|
| 116 |
if(isset($_GET['vc_action']) && $_GET['vc_action'] == 'vc_inline'){ |
| 117 |
return true; |
| 118 |
} |
| 119 |
|
| 120 |
if(isset($_GET['elementor-preview']) || (isset($_GET['action']) && $_GET['action'] == 'elementor')){ |
| 121 |
return true; |
| 122 |
} |
| 123 |
|
| 124 |
return false; |
| 125 |
|
| 126 |
} |
| 127 |
|
| 128 |
function cookieadmin_ajax_handler(){ |
| 129 |
|
| 130 |
$cookieadmin_fn = (!empty($_REQUEST['cookieadmin_act']) ? sanitize_text_field(wp_unslash($_REQUEST['cookieadmin_act'])) : ''); |
| 131 |
|
| 132 |
if(empty($cookieadmin_fn)){ |
| 133 |
wp_send_json_error(array('message' => 'Action not posted')); |
| 134 |
} |
| 135 |
|
| 136 |
// Define a whitelist of allowed functions |
| 137 |
$user_allowed_actions = array(); |
| 138 |
|
| 139 |
$admin_allowed_actions = array( |
| 140 |
'scan_cookies' => 'cookieadmin_scan_cookies', |
| 141 |
'cookieadmin-auto-categorize' => 'cookieadmin_auto_configure_cookies', |
| 142 |
'cookieadmin-edit-cookie' => 'cookieadmin_edit_cookies', |
| 143 |
'close-update-notice' => 'close_plugin_update_notice' |
| 144 |
); |
| 145 |
|
| 146 |
$general_actions = array( |
| 147 |
'categorize_cookies' => 'cookieadmin_categorize_cookies', |
| 148 |
); |
| 149 |
|
| 150 |
if(array_key_exists($cookieadmin_fn, $user_allowed_actions)){ |
| 151 |
|
| 152 |
check_ajax_referer('cookieadmin_js_nonce', 'cookieadmin_security'); |
| 153 |
header_remove('Set-Cookie'); |
| 154 |
call_user_func('\CookieAdmin\Enduser::'.$user_allowed_actions[$cookieadmin_fn]); |
| 155 |
|
| 156 |
}elseif(array_key_exists($cookieadmin_fn, $admin_allowed_actions)){ |
| 157 |
|
| 158 |
check_ajax_referer('cookieadmin_admin_js_nonce', 'cookieadmin_security'); |
| 159 |
|
| 160 |
if(!current_user_can('administrator')){ |
| 161 |
wp_send_json_error(array('message' => 'Sorry, but you do not have permissions to perform this action')); |
| 162 |
} |
| 163 |
|
| 164 |
call_user_func('\CookieAdmin\Admin::'.$admin_allowed_actions[$cookieadmin_fn]); |
| 165 |
|
| 166 |
}elseif(array_key_exists($cookieadmin_fn, $general_actions)){ |
| 167 |
|
| 168 |
check_ajax_referer('cookieadmin_js_nonce', 'cookieadmin_security'); |
| 169 |
header_remove('Set-Cookie'); |
| 170 |
call_user_func($general_actions[$cookieadmin_fn]); |
| 171 |
|
| 172 |
}else{ |
| 173 |
wp_send_json_error(array('message' => 'Unauthorized action')); |
| 174 |
} |
| 175 |
|
| 176 |
} |
| 177 |
|
| 178 |
// Load policies from the file and database and merge them. |
| 179 |
function cookieadmin_load_policy(){ |
| 180 |
|
| 181 |
$policy = get_option('cookieadmin_consent_settings', array()); |
| 182 |
|
| 183 |
if(empty($policy) || !is_array($policy)){ |
| 184 |
$policy = array(); |
| 185 |
} |
| 186 |
|
| 187 |
if(!file_exists(COOKIEADMIN_DIR.'assets/cookie/policy.json')){ |
| 188 |
return $policy; |
| 189 |
} |
| 190 |
|
| 191 |
$j_policy = file_get_contents(COOKIEADMIN_DIR.'assets/cookie/policy.json'); |
| 192 |
|
| 193 |
$j_policy = json_decode($j_policy, true); |
| 194 |
|
| 195 |
if(empty($j_policy) || !is_array($j_policy)){ |
| 196 |
return $policy; |
| 197 |
} |
| 198 |
|
| 199 |
return array_replace_recursive($j_policy, $policy); |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
//Loads consent data from file |
| 204 |
function cookieadmin_load_consent_template($policy, $view){ |
| 205 |
|
| 206 |
if(!file_exists(COOKIEADMIN_DIR.'assets/cookie/template.json')){ |
| 207 |
return false; |
| 208 |
} |
| 209 |
|
| 210 |
$content = json_decode(file_get_contents(COOKIEADMIN_DIR.'assets/cookie/template.json'), true); |
| 211 |
|
| 212 |
if(empty($content)){ |
| 213 |
return false; |
| 214 |
} |
| 215 |
|
| 216 |
$template = array(); |
| 217 |
$template[$view] = $content["cookieadmin_layout"][$policy["cookieadmin_layout"]]; |
| 218 |
$template[$view] .= $content["cookieadmin_modal"][$policy["cookieadmin_modal"]]; |
| 219 |
$template[$view] .= str_replace( '[[plugin_url]]', esc_url(COOKIEADMIN_PLUGIN_URL), $content["cookieadmin_reconsent"] ); |
| 220 |
|
| 221 |
return $template; |
| 222 |
|
| 223 |
} |
| 224 |
|
| 225 |
// Still in progress| No use for now. |
| 226 |
function cookieadmin_compare_consent_id($consent_id) { |
| 227 |
|
| 228 |
if (strlen($consent_id) !== 32) { |
| 229 |
return false; |
| 230 |
} |
| 231 |
|
| 232 |
// Split into random part and signature |
| 233 |
$random_part = substr($consent_id, 0, 16); |
| 234 |
$provided_signature = substr($consent_id, 16, 16); |
| 235 |
|
| 236 |
// Recompute the HMAC |
| 237 |
$expected_hmac = hash_hmac('sha256', $random_part . $domain, $secret_key); |
| 238 |
$expected_signature = substr($expected_hmac, 0, 16); |
| 239 |
|
| 240 |
return hash_equals($provided_signature, $expected_signature); |
| 241 |
} |
| 242 |
|
| 243 |
function cookieadmin_r_print($array){ |
| 244 |
echo '<pre>'; |
| 245 |
print_r($array); |
| 246 |
echo '</pre>'; |
| 247 |
} |
| 248 |
|
| 249 |
function cookieadmin_load_cookies_csv($cookie_names = array(), $like = 0) { |
| 250 |
global $wpdb; |
| 251 |
|
| 252 |
$cookies_list = []; |
| 253 |
|
| 254 |
$csv_file = COOKIEADMIN_DIR . 'assets/open-cookie-database/list.csv'; |
| 255 |
|
| 256 |
// Check if file exists |
| 257 |
if ( ! file_exists( $csv_file ) ) { |
| 258 |
return new WP_Error( 'csv_missing', 'The cookie CSV file is missing: '.$csv_file ); |
| 259 |
} |
| 260 |
|
| 261 |
if ( ( $handle = fopen( $csv_file, 'r' ) ) !== FALSE ) { |
| 262 |
|
| 263 |
$cookies_list = []; |
| 264 |
|
| 265 |
$headers = fgetcsv( $handle, 10000, ",", "\"", "\\" ); |
| 266 |
|
| 267 |
while ( ( $data = fgetcsv( $handle, 10000, ",", "\"", "\\" ) ) !== FALSE ) { |
| 268 |
// 0: cookie_id, 1: Platform, 2: Category, 3: Cookie / Data Key name, |
| 269 |
// 4: Domain, 5: Description, 6: Retention period, 7: Data Controller, |
| 270 |
// 8: User Privacy & GDPR Rights Portals, 9: Wildcard match |
| 271 |
|
| 272 |
$cookie_id = isset( $data[0] ) ? trim( $data[0] ) : ''; |
| 273 |
$cookie_name = isset( $data[3] ) ? trim( $data[3] ) : ''; |
| 274 |
$platform = isset( $data[1] ) ? trim( $data[1] ) : ''; |
| 275 |
$category = isset( $data[2] ) ? trim( $data[2] ) : ''; |
| 276 |
$domain = isset( $data[4] ) ? trim( $data[4] ) : ''; |
| 277 |
$description = isset( $data[5] ) ? trim( $data[5] ) : ''; |
| 278 |
$retention = isset( $data[6] ) ? trim( $data[6] ) : ''; |
| 279 |
$wildcard = isset( $data[9] ) ? (int) $data[9] : 0; |
| 280 |
$patterns = isset( $data[10] ) ? trim($data[10]) : ''; |
| 281 |
|
| 282 |
if ( empty( $cookie_id ) || empty( $cookie_name ) ) { |
| 283 |
continue; |
| 284 |
} |
| 285 |
|
| 286 |
if(!empty($cookie_names)){ |
| 287 |
|
| 288 |
if(!empty($like)){ |
| 289 |
|
| 290 |
$matched = 0; |
| 291 |
foreach($cookie_names as $prefix){ |
| 292 |
if (substr($cookie_name, 0, strlen($prefix)) === $prefix) { |
| 293 |
$matched = 1; |
| 294 |
break; |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
if(empty($matched)){ |
| 299 |
continue; |
| 300 |
} |
| 301 |
|
| 302 |
}else{ |
| 303 |
if(!in_array($cookie_name, $cookie_names)){ |
| 304 |
continue; |
| 305 |
} |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
// Add the row to the current batch |
| 310 |
$cookies_list[] = [ |
| 311 |
'cookie_id' => $cookie_id, |
| 312 |
'cookie_name' => $cookie_name, |
| 313 |
'platform' => $platform, |
| 314 |
'category' => $category, |
| 315 |
'domain' => $domain, |
| 316 |
'description' => $description, |
| 317 |
'retention' => $retention, |
| 318 |
'wildcard' => $wildcard, |
| 319 |
'patterns' => $patterns |
| 320 |
]; |
| 321 |
} |
| 322 |
|
| 323 |
fclose( $handle ); |
| 324 |
|
| 325 |
} else { |
| 326 |
return new WP_Error( 'csv_open_fail', 'Failed to open Cookies CSV file: '.$csv_file ); |
| 327 |
} |
| 328 |
|
| 329 |
return $cookies_list; |
| 330 |
} |
| 331 |
|
| 332 |
function cookieadmin_categorize_cookies($cookies = []){ |
| 333 |
|
| 334 |
global $cookieadmin_lang, $cookieadmin_error, $cookieadmin_msg, $wpdb; |
| 335 |
|
| 336 |
if(!empty($_REQUEST['cookieadmin_cookies'])){ |
| 337 |
|
| 338 |
$raw_cookies = json_decode( wp_unslash( $_REQUEST['cookieadmin_cookies'] ), true ); |
| 339 |
|
| 340 |
if ( is_array( $raw_cookies ) ) { |
| 341 |
$sanitized_cookies = []; |
| 342 |
|
| 343 |
array_walk( $raw_cookies, function( $value, $key ) use ( &$sanitized_cookies ) { |
| 344 |
$sanitized_key = sanitize_key( $key ); |
| 345 |
$sanitized_cookies[ $sanitized_key ] = sanitize_text_field($value); |
| 346 |
} ); |
| 347 |
|
| 348 |
unset($raw_cookies); |
| 349 |
} |
| 350 |
}else{ |
| 351 |
$sanitized_cookies = $cookies; |
| 352 |
} |
| 353 |
|
| 354 |
|
| 355 |
if(empty($sanitized_cookies)){ |
| 356 |
return [ |
| 357 |
'success' => false, |
| 358 |
'data' => null, |
| 359 |
'error' => 'Please provide valid cookie names.', |
| 360 |
]; |
| 361 |
} |
| 362 |
|
| 363 |
$cookies_info = cookieadmin_load_cookies_csv(array_keys($sanitized_cookies)); |
| 364 |
|
| 365 |
if(empty($cookies_info) || is_wp_error($cookies_info)){ |
| 366 |
return [ |
| 367 |
'success' => false, |
| 368 |
'data' => null, |
| 369 |
'error' => 'Failed to load Cookies list', |
| 370 |
]; |
| 371 |
} |
| 372 |
|
| 373 |
foreach($cookies_info as $info){ |
| 374 |
$sanitized_cookies[$info['cookie_name']]['source'] = !empty($info['domain']) ? $info['domain'] : "unknown"; |
| 375 |
$sanitized_cookies[$info['cookie_name']]['category'] = !empty($info['category']) ? strtolower($info['category']) : "un_c"; |
| 376 |
$sanitized_cookies[$info['cookie_name']]['description'] = !empty($info['description']) ? $info['description'] : "unknown"; |
| 377 |
$sanitized_cookies[$info['cookie_name']]['duration'] = !empty($info['retention']) ? $info['retention'] : "unknown"; |
| 378 |
$sanitized_cookies[$info['cookie_name']]['platform'] = !empty($info['platform']) ? $info['platform'] : "unknown";; |
| 379 |
} |
| 380 |
|
| 381 |
if(wp_doing_ajax()){ |
| 382 |
wp_send_json_success($sanitized_cookies); |
| 383 |
} |
| 384 |
|
| 385 |
return $sanitized_cookies; |
| 386 |
} |
| 387 |
|
| 388 |
function cookieadmin_is_pro(){ |
| 389 |
return defined('COOKIEADMIN_PREMIUM'); |
| 390 |
} |
| 391 |
|