| @@ -1,502 +1,513 @@ | ||
| 1 | -<?php | |
| 2 | - | |
| 3 | -namespace CookieAdmin\Admin; | |
| 4 | - | |
| 5 | -if(!defined('COOKIEADMIN_VERSION') || !defined('ABSPATH')){ | |
| 6 | - die('Hacking Attempt'); | |
| 7 | -} | |
| 8 | - | |
| 9 | -class Scan{ | |
| 10 | - | |
| 11 | - static function show_cookies(){ | |
| 12 | - global $cookieadmin_lang, $cookieadmin_error, $cookieadmin_msg, $wpdb; | |
| 13 | - | |
| 14 | - \CookieAdmin\Admin::header_theme(__('Manage Cookies', 'cookieadmin')); | |
| 15 | - | |
| 16 | - $cookieadmin_requires_pro = \CookieAdmin\Admin::is_feature_available(1); | |
| 17 | - | |
| 18 | - $table_name = esc_sql($wpdb->prefix . 'cookieadmin_cookies'); | |
| 19 | - | |
| 20 | - $categorized = []; | |
| 21 | - $categorized_cookies = []; | |
| 22 | - | |
| 23 | - | |
| 24 | - $scanned_cookies = $wpdb->get_results("SELECT * FROM {$table_name}"); | |
| 25 | - | |
| 26 | - foreach($scanned_cookies as $row => $data){ | |
| 27 | - | |
| 28 | - $expires = 0; | |
| 29 | - | |
| 30 | - if(!empty($data->expires) && !empty($data->scan_timestamp)){ | |
| 31 | - $expires = strtotime($data->expires); | |
| 32 | - $timestamp = $data->scan_timestamp; | |
| 33 | - | |
| 34 | - if(!empty($expires) && ($expires > 0) && !empty($timestamp)){ | |
| 35 | - $expires = round(($expires - $timestamp) / 86400); | |
| 36 | - }else{ | |
| 37 | - $expires = 0; | |
| 38 | - } | |
| 39 | - } | |
| 40 | - | |
| 41 | - if($expires < 1){ | |
| 42 | - $exp = __('Session', 'cookieadmin'); | |
| 43 | - }else{ | |
| 44 | - $exp = $expires . ' '.__('Day(s)', 'cookieadmin'); | |
| 45 | - } | |
| 46 | - | |
| 47 | - if(empty($data->category)){ | |
| 48 | - $data->category = 'Unknown'; | |
| 49 | - } | |
| 50 | - | |
| 51 | - if(!isset($categorized[$data->category])){ | |
| 52 | - $categorized[$data->category] = ''; | |
| 53 | - } | |
| 54 | - | |
| 55 | - if(empty($data->description)){ | |
| 56 | - $data->description = 'Not Available'; | |
| 57 | - } | |
| 58 | - | |
| 59 | - $categorized[$data->category] .= '<tr><td>'.esc_html($data->cookie_name).'</td><td>'.esc_html($data->description).'</td><td>'.esc_html($exp).'</td><td> <span class="dashicons dashicons-edit cookieadmin_edit_icon" id="edit_'.esc_attr($data->id).'"></span> <span class="dashicons dashicons-trash cookieadmin_delete_icon" id="delete_'.esc_attr($data->id).'"></span> </td></tr>'; | |
| 60 | - | |
| 61 | - $categorized_cookies[$data->id]['id'] = $data->id; | |
| 62 | - $categorized_cookies[$data->id]['cookie_name'] = $data->cookie_name; | |
| 63 | - $categorized_cookies[$data->id]['description'] = $data->description; | |
| 64 | - $categorized_cookies[$data->id]['category'] = $data->category; | |
| 65 | - $categorized_cookies[$data->id]['expires'] = $expires; | |
| 66 | - | |
| 67 | - } | |
| 68 | - | |
| 69 | - wp_register_script('cookieadmin_categorized_cookies', '', array('jquery'), COOKIEADMIN_VERSION, true); | |
| 70 | - wp_enqueue_script('cookieadmin_categorized_cookies'); | |
| 71 | - wp_localize_script('cookieadmin_categorized_cookies', 'categorized_cookies', $categorized_cookies); | |
| 72 | - | |
| 73 | - $no_cookies = '<tr class="cookieadmin-empty-row"><td colspan=4>'.esc_html__('No Cookies Found!', 'cookieadmin').'</td></tr>'; | |
| 74 | - $no_cookies_hidden = '<tr class="cookieadmin-empty-row" hidden><td colspan=4>'.esc_html__('No Cookies Found!', 'cookieadmin').'</td></tr>'; | |
| 75 | - | |
| 76 | - echo ' | |
| 77 | - <div class="cookieadmin_consent-wrap"> | |
| 78 | - <form action="" method="post"> | |
| 79 | - <div class="cookieadmin_consent-contents"> | |
| 80 | - <div class="cookieadmin_consent_settings"> | |
| 81 | - <div class="cookieadmin-setting cookieadmin-manager-cookie-scan"> | |
| 82 | - <div class="cookieadmin-scan-cookie-info"> | |
| 83 | - <label class="cookieadmin-title">'.esc_html__('Scanned Cookies', 'cookieadmin').'</label> | |
| 84 | - <p class="cookieadmin-desc">'.esc_html__('Scanned cookies will be automatically categorised and displayed here. You can add, edit and delete cookies as per your needs.', 'cookieadmin').'</p> | |
| 85 | - </div> | |
| 86 | - <div class="cookieadmin-setting-contents cookieadmin-buttons-div"> | |
| 87 | - <div class="cookieadmin-setting-contents cookieadmin-add-cookie-div"> | |
| 88 | - <input type="button" class="cookieadmin-btn cookieadmin-btn-primary cookieadmin-add-cookie" value="'.esc_html__('Add Cookie', 'cookieadmin').'" cookieadmin-pro-only="1"></input> | |
| 89 | - '.wp_kses_post($cookieadmin_requires_pro).' | |
| 90 | - </div> | |
| 91 | - <div class="cookieadmin-setting-contents cookieadmin-cookie-scan'.( !empty($cookieadmin_requires_pro) ? ' cookieadmin-tooltip-box' : '').'" data-tip="'.esc_html__('Basic scan might miss some cookies.', 'cookieadmin').'">'. | |
| 92 | - ( !empty($cookieadmin_requires_pro) ? ' | |
| 93 | - <input type="button" class="cookieadmin-btn cookieadmin-btn-primary cookieadmin-scan" value="'.esc_html__('Scan', 'cookieadmin').'"></input> ' : '').' | |
| 94 | - <input type="button" class="cookieadmin-btn cookieadmin-btn-primary cookieadmin-scan" value="'.esc_html__('Full Scan', 'cookieadmin').'" cookieadmin-pro-only="1"> | |
| 95 | - '.wp_kses_post($cookieadmin_requires_pro).' | |
| 96 | - </div> | |
| 97 | - </div> | |
| 98 | - <div class="cookieadmin-manager-result"> | |
| 99 | - <table class="cookieadmin-table cookieadmin-cookie-categorized"> | |
| 100 | - <thead> | |
| 101 | - <tr> | |
| 102 | - <th width="30%">'.esc_html__('Name', 'cookieadmin').'</th> | |
| 103 | - <th width="50%">'.esc_html__('Description', 'cookieadmin').'</th> | |
| 104 | - <th width="10%">'.esc_html__('Expiry', 'cookieadmin').'</th> | |
| 105 | - <th width="10%">'.esc_html__('Action', 'cookieadmin').'</th> | |
| 106 | - </tr> | |
| 107 | - </thead> | |
| 108 | - <tbody id="necessary_tbody"> | |
| 109 | - <tr><td colspan=4>'.esc_html__('Necessary Cookies', 'cookieadmin').'</td></tr> | |
| 110 | - '.( !empty($categorized['Necessary']) ? $no_cookies_hidden . wp_kses_post($categorized['Necessary']) : $no_cookies ).' | |
| 111 | - </tbody> | |
| 112 | - <tbody id="functional_tbody"> | |
| 113 | - <tr><td colspan=4>'.esc_html__('Functional Cookies', 'cookieadmin').'</td></tr> | |
| 114 | - '.( !empty($categorized['Functional']) ? $no_cookies_hidden . wp_kses_post($categorized['Functional']) : $no_cookies ).' | |
| 115 | - </tbody> | |
| 116 | - <tbody id="analytics_tbody"> | |
| 117 | - <tr><td colspan=4>'.esc_html__('Analytical Cookies', 'cookieadmin').'</td></tr> | |
| 118 | - '.( !empty($categorized['Analytics']) ? $no_cookies_hidden . wp_kses_post($categorized['Analytics']) :$no_cookies ).' | |
| 119 | - </tbody> | |
| 120 | - <tbody id="marketing_tbody"> | |
| 121 | - <tr><td colspan=4>'.esc_html__('Marketing Cookies', 'cookieadmin').'</td></tr> | |
| 122 | - '.( !empty($categorized['Marketing']) ? $no_cookies_hidden . wp_kses_post($categorized['Marketing']) : $no_cookies ).' | |
| 123 | - </tbody> | |
| 124 | - <tbody id="unknown_tbody"> | |
| 125 | - <tr><td colspan=4>'.esc_html__('Unknown Cookies', 'cookieadmin').'</td></tr> | |
| 126 | - '.( !empty($categorized['Unknown']) ? $no_cookies_hidden . wp_kses_post($categorized['Unknown']) : $no_cookies ).' | |
| 127 | - </tbody> | |
| 128 | - </table> | |
| 129 | - </div> | |
| 130 | - </div> | |
| 131 | - </div> | |
| 132 | - </div>'; | |
| 133 | - | |
| 134 | - wp_nonce_field('cookieadmin_admin_nonce', 'cookieadmin_security'); | |
| 135 | - | |
| 136 | - echo ' | |
| 137 | - </div> | |
| 138 | - </form> | |
| 139 | - <br/>'; | |
| 140 | - | |
| 141 | - \CookieAdmin\Admin::footer_theme(); | |
| 142 | - | |
| 143 | - echo ' | |
| 144 | - <!-- Modal Overlay --> | |
| 145 | - <div class="cookieadmin_modal-overlay" id="edit-cookie-modal" hidden> | |
| 146 | - <div class="cookieadmin_modal-container"> | |
| 147 | - <div class="cookieadmin_modal-header"> | |
| 148 | - <h2>'.esc_html__('Edit Cookie', 'cookieadmin').'</h2> | |
| 149 | - <button class="cookieadmin_dialog_modal_close_btn">×</button> | |
| 150 | - </div> | |
| 151 | - | |
| 152 | - <div class="cookieadmin_modal-body"> | |
| 153 | - <div class="cookieadmin_form-group"> | |
| 154 | - <label for="cookieadmin-dialog-cookie-category">'.esc_html__('Category', 'cookieadmin').'</label> | |
| 155 | - <select id="cookieadmin-dialog-cookie-category"> | |
| 156 | - <option value="" selected>'.esc_html__('Select a category', 'cookieadmin').'</option> | |
| 157 | - <option value="Necessary">'.esc_html__('Necessary', 'cookieadmin').'</option> | |
| 158 | - <option value="Functional">'.esc_html__('Functional', 'cookieadmin').'</option> | |
| 159 | - <option value="Analytics">'.esc_html__('Analytical', 'cookieadmin').'</option> | |
| 160 | - <option value="Marketing">'.esc_html__('Marketing', 'cookieadmin').'</option> | |
| 161 | - <option value="Unknown">'.esc_html__('Unknown', 'cookieadmin').'</option> | |
| 162 | - </select> | |
| 163 | - </div> | |
| 164 | - | |
| 165 | - <div class="cookieadmin_form-group"> | |
| 166 | - <label for="cookie_id">'.esc_html__('Cookie Name/ID', 'cookieadmin').'</label> | |
| 167 | - <input type="text" id="cookieadmin-dialog-cookie-name" Placeholder="'.esc_html__('Enter Cookie Name or id', 'cookieadmin').'"> | |
| 168 | - </div> | |
| 169 | - | |
| 170 | - <div class="cookieadmin_form-group"> | |
| 171 | - <label for="description">'.esc_html__('Description', 'cookieadmin').'</label> | |
| 172 | - <textarea id="cookieadmin-dialog-cookie-desc" Placeholder="'.esc_html__('Enter Cookie description here', 'cookieadmin').'"></textarea> | |
| 173 | - </div> | |
| 174 | - | |
| 175 | - <div class="cookieadmin_form-group"> | |
| 176 | - <label for="duration">'.esc_html__('Duration', 'cookieadmin').'</label> | |
| 177 | - <input type="number" min=0 id="cookieadmin-dialog-cookie-duration" Placeholder="'.esc_html__('Set 0 for Session or expiry in days', 'cookieadmin').'"> | |
| 178 | - </div> | |
| 179 | - </div> | |
| 180 | - <div class="cookieadmin_modal-footer"> | |
| 181 | - <span id="cookieadmin-message"></span> | |
| 182 | - <button class="cookieadmin-btn cookieadmin-btn-primary" id="cookieadmin_dialog_save_btn" form="edit-cookie-form">'.esc_html__('Save', 'cookieadmin').'</button> | |
| 183 | - </div> | |
| 184 | - </div> | |
| 185 | - </div>'; | |
| 186 | - } | |
| 187 | - | |
| 188 | - | |
| 189 | - | |
| 190 | - static function scan_cookies_ajax(){ | |
| 191 | - global $cookieadmin_error; | |
| 192 | - | |
| 193 | - $urls = []; | |
| 194 | - if(!empty($_REQUEST['urls'])){ | |
| 195 | - $urls = map_deep(wp_unslash($_REQUEST['urls']), 'sanitize_url'); | |
| 196 | - } | |
| 197 | - | |
| 198 | - if(cookieadmin_is_pro()){ | |
| 199 | - $scanner_info = get_option('cookieadmin_pro_scanner', []); | |
| 200 | - | |
| 201 | - if(!empty($scanner_info['last_scan']) && (time() > $scanner_info['last_scan'] + 3600)){ | |
| 202 | - wp_send_json([ | |
| 203 | - 'success' => false, | |
| 204 | - 'message' => __('Cookie Scan can be triggered once an hour', 'cookieadmin') | |
| 205 | - ]); | |
| 206 | - } | |
| 207 | - } | |
| 208 | - | |
| 209 | - self::scan_cookies($urls); | |
| 210 | - | |
| 211 | - if(!empty($cookieadmin_error)){ | |
| 212 | - wp_send_json([ | |
| 213 | - 'success' => false, | |
| 214 | - 'message' => $cookieadmin_error] | |
| 215 | - ); | |
| 216 | - } | |
| 217 | - | |
| 218 | - wp_send_json(['success' => true, 'data' => null]); | |
| 219 | - } | |
| 220 | - | |
| 221 | - // Orchestrator function for scanning cookies | |
| 222 | - static function scan_cookies($urls = []){ | |
| 223 | - global $cookieadmin_error; | |
| 224 | - | |
| 225 | - if(cookieadmin_is_pro()){ | |
| 226 | - | |
| 227 | - if(!method_exists('\CookieAdminPro\Admin', 'cookieadmin_get_site_urls')){ | |
| 228 | - $urls = [home_url()]; | |
| 229 | - } else { | |
| 230 | - $urls = \CookieAdminPro\Admin::cookieadmin_get_site_urls($urls, 1); | |
| 231 | - } | |
| 232 | - | |
| 233 | - $cookieData = apply_filters('cookieadmin_pro_scan_cookies', $urls); | |
| 234 | - | |
| 235 | - //Server side scann - skipped for now - need to discuss. | |
| 236 | - // $cookieData2 = \CookieAdmin\Scanner::start_scan($urls); | |
| 237 | - // $cookieData = array_replace_recursive($cookieData2, $cookieData1); | |
| 238 | - | |
| 239 | - if(!empty($cookieadmin_error)){ | |
| 240 | - update_option('cookieadmin_scan', [ | |
| 241 | - 'status' => 3, | |
| 242 | - 'success' => false, | |
| 243 | - 'message' => $cookieadmin_error, | |
| 244 | - 'update' => time() | |
| 245 | - ]); | |
| 246 | - return; | |
| 247 | - } | |
| 248 | - | |
| 249 | - if(!empty($cookieData)){ | |
| 250 | - | |
| 251 | - if(function_exists('cookieadmin_pro_get_remaining_urls')){ | |
| 252 | - // Check Remaining urls | |
| 253 | - $remainig_urls = cookieadmin_pro_get_remaining_urls($urls); | |
| 254 | - | |
| 255 | - if(!empty($remaining_urls)){ | |
| 256 | - //send next batch for scan | |
| 257 | - wp_schedule_single_event(time() + 5, 'cookieadmin_run_auto_scan_batch', [$remaining_urls]); | |
| 258 | - } | |
| 259 | - } | |
| 260 | - | |
| 261 | - $res = self::save_raw_scan_results($cookieData); | |
| 262 | - if(function_exists('cookieadmin_pro_update_scan_count')){ | |
| 263 | - cookieadmin_pro_update_scan_count($res); | |
| 264 | - } | |
| 265 | - | |
| 266 | - self::cookieadmin_auto_configure_cookies(); | |
| 267 | - return; | |
| 268 | - } | |
| 269 | - | |
| 270 | - }else{ | |
| 271 | - $cookieData = \CookieAdmin\Scanner::start_scan(); | |
| 272 | - if(!empty($cookieData)){ | |
| 273 | - self::save_raw_scan_results($cookieData); | |
| 274 | - return self::cookieadmin_auto_configure_cookies(); | |
| 275 | - } | |
| 276 | - } | |
| 277 | - // cookieadmin_r_print($cookieData); | |
| 278 | - | |
| 279 | - if(defined('DOING_CRON') && get_transient('cookieadmin_auto_scan_in_progress')){ | |
| 280 | - delete_transient('cookieadmin_auto_scan_in_progress'); | |
| 281 | - } | |
| 282 | - | |
| 283 | - update_option('cookieadmin_scan', [ | |
| 284 | - 'status' => 3, | |
| 285 | - 'success' => false, | |
| 286 | - 'message' => __('No Cookies Found', 'cookieadmin'), | |
| 287 | - 'update' => time() | |
| 288 | - ]); | |
| 289 | - | |
| 290 | - return false; | |
| 291 | - } | |
| 292 | - | |
| 293 | - static function save_raw_scan_results(array $found_cookies){ | |
| 294 | - | |
| 295 | - global $wpdb; | |
| 296 | - | |
| 297 | - $table_name = esc_sql($wpdb->prefix . 'cookieadmin_cookies'); | |
| 298 | - | |
| 299 | - if (empty($found_cookies)) { | |
| 300 | - return ['inserted' => 0, 'updated' => 0]; | |
| 301 | - } | |
| 302 | - | |
| 303 | - // Step 1: Fetch all existing cookie names from our database in one efficient query. | |
| 304 | - $existing_cookies_in_db = $wpdb->get_col("SELECT cookie_name FROM {$table_name}"); | |
| 305 | - // Use array_flip for very fast 'isset' lookups instead of slow 'in_array' in a loop. | |
| 306 | - $existing_cookies_lookup = !empty($existing_cookies_in_db) ? array_flip($existing_cookies_in_db) : []; | |
| 307 | - | |
| 308 | - $results = ['inserted' => 0, 'updated' => 0]; | |
| 309 | - | |
| 310 | - // Step 2: Loop through each cookie found by the scanner. | |
| 311 | - foreach ($found_cookies as $cookie_name => $cookie_data) { | |
| 312 | - | |
| 313 | - // Step 3: Check if the cookie exists in our DB. | |
| 314 | - if (isset($existing_cookies_lookup[$cookie_name])) { | |
| 315 | - | |
| 316 | - $wpdb->update( | |
| 317 | - $table_name, | |
| 318 | - [ | |
| 319 | - 'domain' => sanitize_text_field($cookie_data['domain']), | |
| 320 | - 'path' => sanitize_text_field($cookie_data['path']), | |
| 321 | - 'expires' => !empty($cookie_data['session']) ? 0 : (!empty($cookie_data['expires']) ? $cookie_data['expires'] : null), | |
| 322 | - 'samesite' => !empty($cookie_data['samesite']) ? sanitize_text_field($cookie_data['samesite']) : null, | |
| 323 | - 'secure' => (int)($cookie_data['secure'] ?? 0), | |
| 324 | - 'httponly' => (int)($cookie_data['httponly'] ?? 0), | |
| 325 | - 'raw_name' => sanitize_text_field($cookie_name), | |
| 326 | - 'scan_timestamp' => time(), | |
| 327 | - ], // Data to update | |
| 328 | - [ 'cookie_name' => $cookie_name ], // WHERE clause | |
| 329 | - ['%s', '%s', '%s', '%s', '%d', '%d', '%s', '%d'], // Format for the data | |
| 330 | - [ '%s' ] // Format for the WHERE clause | |
| 331 | - ); | |
| 332 | - $results['updated']++; | |
| 333 | - | |
| 334 | - } else { | |
| 335 | - | |
| 336 | - // ------ INSERT a NEW cookie ------ | |
| 337 | - $data = [ | |
| 338 | - 'cookie_name' => sanitize_text_field($cookie_name), | |
| 339 | - 'domain' => sanitize_text_field($cookie_data['domain']), | |
| 340 | - 'path' => sanitize_text_field($cookie_data['path']), | |
| 341 | - 'expires' => !empty($cookie_data['session']) ? 0 : (!empty($cookie_data['expires']) ? $cookie_data['expires'] : null), | |
| 342 | - 'samesite' => !empty($cookie_data['samesite']) ? sanitize_text_field($cookie_data['samesite']) : null, | |
| 343 | - 'secure' => (int)($cookie_data['secure'] ?? 0), | |
| 344 | - 'httponly' => (int)($cookie_data['httponly'] ?? 0), | |
| 345 | - 'raw_name' => sanitize_text_field($cookie_name), | |
| 346 | - 'scan_timestamp' => time(), | |
| 347 | - ]; | |
| 348 | - | |
| 349 | - $formats = ['%s', '%s', '%s', '%s', '%s', '%d', '%d', '%s', '%d']; | |
| 350 | - | |
| 351 | - if ($wpdb->insert($table_name, $data, $formats)) { | |
| 352 | - $results['inserted']++; | |
| 353 | - } else { | |
| 354 | - //error_log("CookieAdmin: Error inserting cookie data: " . $wpdb->last_error); | |
| 355 | - } | |
| 356 | - } | |
| 357 | - } | |
| 358 | - return $results; | |
| 359 | - } | |
| 360 | - | |
| 361 | - static function cookieadmin_auto_configure_cookies(){ | |
| 362 | - global $wpdb, $cookieadmin_error; | |
| 363 | - | |
| 364 | - $table_name = $wpdb->prefix . 'cookieadmin_cookies'; | |
| 365 | - $categorized_cookies = []; | |
| 366 | - $uncategorized_cookies = []; | |
| 367 | - | |
| 368 | - $all_cookies = $wpdb->get_results("SELECT id, cookie_name, category FROM {$table_name}"); | |
| 369 | - | |
| 370 | - foreach($all_cookies as $cookie){ | |
| 371 | - | |
| 372 | - if(!empty($cookie->category)){ | |
| 373 | - $categorized_cookies[$cookie->id] = $cookie->cookie_name; | |
| 374 | - }else{ | |
| 375 | - $uncategorized_cookies[] = $cookie->cookie_name; | |
| 376 | - } | |
| 377 | - } | |
| 378 | - | |
| 379 | - if(!empty($uncategorized_cookies)){ | |
| 380 | - | |
| 381 | - $uncategorized_cookies = array_flip($uncategorized_cookies); | |
| 382 | - $categorized_cookies = array_flip($categorized_cookies); | |
| 383 | - | |
| 384 | - | |
| 385 | - $categorizd_cookies = \CookieAdmin\CookieCategorizer::categorize_cookies($uncategorized_cookies, $categorized_cookies); | |
| 386 | - | |
| 387 | - $remove_cookies = !empty($categorizd_cookies['remove_cookies']) ? $categorizd_cookies['remove_cookies'] : []; | |
| 388 | - | |
| 389 | - unset($categorizd_cookies['remove_cookies']); | |
| 390 | - if(!empty($remove_cookies)){ | |
| 391 | - $placeholders = implode(',', array_fill(0, count($remove_cookies), '%s')); | |
| 392 | - $sql = $wpdb->prepare("DELETE FROM {$table_name} WHERE id IN ({$placeholders})", ...$remove_cookies); | |
| 393 | - $wpdb->query($sql); | |
| 394 | - } | |
| 395 | - | |
| 396 | - foreach($categorizd_cookies as $cookie_data){ | |
| 397 | - | |
| 398 | - $count = $wpdb->update( | |
| 399 | - $table_name, | |
| 400 | - [ 'cookie_name' => $cookie_data['cookie_name'], 'category' => $cookie_data['category'], 'description' => $cookie_data['description'], 'edited' => 1, 'patterns' => $cookie_data['patterns'] ], // Data to update | |
| 401 | - [ 'raw_name' => $cookie_data['raw_name'] ], // WHERE | |
| 402 | - [ '%s', '%s', '%s', '%d', '%s' ], // Format for the data | |
| 403 | - [ '%s' ] // Format for the WHERE clause | |
| 404 | - ); | |
| 405 | - | |
| 406 | - } | |
| 407 | - | |
| 408 | - update_option('cookieadmin_scan', [ | |
| 409 | - 'status' => 3, | |
| 410 | - 'update' => time(), | |
| 411 | - 'success' => true, | |
| 412 | - 'count' => $count | |
| 413 | - ]); | |
| 414 | - | |
| 415 | - $categorized_cookies = $wpdb->get_results("SELECT id, cookie_name, category, expires, scan_timestamp, description FROM {$table_name}"); | |
| 416 | - | |
| 417 | - delete_option('cookieadmin_first_scan'); | |
| 418 | - | |
| 419 | - return $categorized_cookies; | |
| 420 | - } | |
| 421 | - | |
| 422 | - $cookieadmin_error = $cookieadmin_error . ' ' . __('No new cookies Found!', 'cookieadmin'); | |
| 423 | - | |
| 424 | - return false; | |
| 425 | - } | |
| 426 | - | |
| 427 | - | |
| 428 | - static function edit_cookies(){ | |
| 429 | - global $wpdb; | |
| 430 | - | |
| 431 | - $table_name = esc_sql($wpdb->prefix . 'cookieadmin_cookies'); | |
| 432 | - $data = null; | |
| 433 | - | |
| 434 | - if(empty($_REQUEST['cookie_info'])){ | |
| 435 | - wp_send_json(['success' => false, | |
| 436 | - 'data' => null, | |
| 437 | - 'message' => __('Error : Cookie details missing', 'cookieadmin')] | |
| 438 | - ); | |
| 439 | - } | |
| 440 | - | |
| 441 | - $cookie_info = map_deep(wp_unslash($_REQUEST['cookie_info']), 'sanitize_text_field'); | |
| 442 | - | |
| 443 | - $scan_timestamp = $wpdb->get_col($wpdb->prepare("SELECT scan_timestamp FROM {$table_name} WHERE id = %d", $cookie_info['id'])); | |
| 444 | - | |
| 445 | - if(empty($scan_timestamp)){ | |
| 446 | - wp_send_json(['success' => false, | |
| 447 | - 'data' => null, | |
| 448 | - 'message' => __('Error : Invalid cookie record', 'cookieadmin')] | |
| 449 | - ); | |
| 450 | - } | |
| 451 | - | |
| 452 | - $calculated_expiry_seconds = ($cookie_info['duration'] * 86400) + $scan_timestamp[0]; | |
| 453 | - $calculated_expiry = date('Y-m-d H:i:s', $calculated_expiry_seconds); | |
| 454 | - | |
| 455 | - $resp = $wpdb->update( | |
| 456 | - $table_name, | |
| 457 | - [ 'cookie_name' => $cookie_info['name'], 'description' => $cookie_info['description'], 'expires' => $calculated_expiry, 'category' => $cookie_info['type'], 'edited' => 1], // Data to update | |
| 458 | - [ 'id' => $cookie_info['id'] ], // WHERE | |
| 459 | - [ '%s', '%s', '%s', '%s', '%d' ], // Format for the data | |
| 460 | - [ '%d' ] // Format for the WHERE clause | |
| 461 | - ); | |
| 462 | - | |
| 463 | - if ($wpdb->last_error || $resp === false) { | |
| 464 | - //error_log('DB Error: ' . $wpdb->last_error); // Log it | |
| 465 | - wp_send_json(['success' => false, | |
| 466 | - 'data' => null, | |
| 467 | - 'message' => __('Cookie updation Failed, Error: ', 'cookieadmin') . esc_html($wpdb->last_error)]); | |
| 468 | - } | |
| 469 | - | |
| 470 | - wp_send_json(['success' => true, | |
| 471 | - 'data' => $data, | |
| 472 | - 'message' => __('Cookie updation successful', 'cookieadmin')]); | |
| 473 | - | |
| 474 | - } | |
| 475 | - | |
| 476 | - static function delete_cookies(){ | |
| 477 | - global $wpdb; | |
| 478 | - | |
| 479 | - $table_name = esc_sql($wpdb->prefix . 'cookieadmin_cookies'); | |
| 480 | - | |
| 481 | - if(empty($_REQUEST['cookie_raw_id'])){ | |
| 482 | - wp_send_json(['success' => false, | |
| 483 | - 'data' => null, | |
| 484 | - 'message' => __('Error : Cookie Id missing', 'cookieadmin')]); | |
| 485 | - } | |
| 486 | - | |
| 487 | - $cookie_id = (int) sanitize_text_field(wp_unslash($_REQUEST['cookie_raw_id'])); | |
| 488 | - | |
| 489 | - $resp = $wpdb->delete( $table_name, ['id' => $cookie_id], [ '%s' ] ); | |
| 490 | - | |
| 491 | - if ($wpdb->last_error || $resp === false) { | |
| 492 | - //error_log('DB Error: ' . $wpdb->last_error); //Log it | |
| 493 | - wp_send_json(['success' => false, | |
| 494 | - 'data' => null, | |
| 495 | - 'message' => __('Cookie deletion Failed, Error: ', 'cookieadmin') . esc_html($wpdb->last_error)]); | |
| 496 | - } | |
| 497 | - | |
| 498 | - wp_send_json(['success' => true, | |
| 499 | - 'message' => __('Cookie deletion successful', 'cookieadmin')]); | |
| 500 | - } | |
| 501 | - | |
| 502 | -} | |
| 1 | +<?php | |
| 2 | + | |
| 3 | +namespace CookieAdmin\Admin; | |
| 4 | + | |
| 5 | +if(!defined('COOKIEADMIN_VERSION') || !defined('ABSPATH')){ | |
| 6 | + die('Hacking Attempt'); | |
| 7 | +} | |
| 8 | + | |
| 9 | +class Scan{ | |
| 10 | + | |
| 11 | + static function show_cookies(){ | |
| 12 | + global $cookieadmin_lang, $cookieadmin_error, $cookieadmin_msg, $wpdb; | |
| 13 | + | |
| 14 | + \CookieAdmin\Admin::header_theme(__('Manage Cookies', 'cookieadmin')); | |
| 15 | + | |
| 16 | + $cookieadmin_requires_pro = \CookieAdmin\Admin::is_feature_available(1); | |
| 17 | + | |
| 18 | + $table_name = esc_sql($wpdb->prefix . 'cookieadmin_cookies'); | |
| 19 | + | |
| 20 | + $categorized = []; | |
| 21 | + $categorized_cookies = []; | |
| 22 | + | |
| 23 | + | |
| 24 | + $scanned_cookies = $wpdb->get_results("SELECT * FROM {$table_name}"); | |
| 25 | + | |
| 26 | + foreach($scanned_cookies as $row => $data){ | |
| 27 | + | |
| 28 | + $expires = 0; | |
| 29 | + | |
| 30 | + if(!empty($data->expires) && !empty($data->scan_timestamp)){ | |
| 31 | + $expires = strtotime($data->expires); | |
| 32 | + $timestamp = $data->scan_timestamp; | |
| 33 | + | |
| 34 | + if(!empty($expires) && ($expires > 0) && !empty($timestamp)){ | |
| 35 | + $expires = round(($expires - $timestamp) / 86400); | |
| 36 | + }else{ | |
| 37 | + $expires = 0; | |
| 38 | + } | |
| 39 | + } | |
| 40 | + | |
| 41 | + if($expires < 1){ | |
| 42 | + $exp = __('Session', 'cookieadmin'); | |
| 43 | + }else{ | |
| 44 | + $exp = $expires . ' '.__('Day(s)', 'cookieadmin'); | |
| 45 | + } | |
| 46 | + | |
| 47 | + if(empty($data->category)){ | |
| 48 | + $data->category = 'Unknown'; | |
| 49 | + } | |
| 50 | + | |
| 51 | + if(!isset($categorized[$data->category])){ | |
| 52 | + $categorized[$data->category] = ''; | |
| 53 | + } | |
| 54 | + | |
| 55 | + if(empty($data->description)){ | |
| 56 | + $data->description = 'Not Available'; | |
| 57 | + } | |
| 58 | + | |
| 59 | + $categorized[$data->category] .= '<tr><td>'.esc_html($data->cookie_name).'</td><td>'.esc_html($data->description).'</td><td>'.esc_html($exp).'</td><td> <span class="dashicons dashicons-edit cookieadmin_edit_icon" id="edit_'.esc_attr($data->id).'"></span> <span class="dashicons dashicons-trash cookieadmin_delete_icon" id="delete_'.esc_attr($data->id).'"></span> </td></tr>'; | |
| 60 | + | |
| 61 | + $categorized_cookies[$data->id]['id'] = $data->id; | |
| 62 | + $categorized_cookies[$data->id]['cookie_name'] = $data->cookie_name; | |
| 63 | + $categorized_cookies[$data->id]['description'] = $data->description; | |
| 64 | + $categorized_cookies[$data->id]['category'] = $data->category; | |
| 65 | + $categorized_cookies[$data->id]['expires'] = $expires; | |
| 66 | + | |
| 67 | + } | |
| 68 | + | |
| 69 | + wp_register_script('cookieadmin_categorized_cookies', '', array('jquery'), COOKIEADMIN_VERSION, true); | |
| 70 | + wp_enqueue_script('cookieadmin_categorized_cookies'); | |
| 71 | + wp_localize_script('cookieadmin_categorized_cookies', 'categorized_cookies', $categorized_cookies); | |
| 72 | + | |
| 73 | + $no_cookies = '<tr class="cookieadmin-empty-row"><td colspan=4>'.esc_html__('No Cookies Found!', 'cookieadmin').'</td></tr>'; | |
| 74 | + $no_cookies_hidden = '<tr class="cookieadmin-empty-row" hidden><td colspan=4>'.esc_html__('No Cookies Found!', 'cookieadmin').'</td></tr>'; | |
| 75 | + | |
| 76 | + echo ' | |
| 77 | + <div class="cookieadmin_consent-wrap"> | |
| 78 | + <form action="" method="post">'; | |
| 79 | + do_action('cookieadmin_before_scan_results'); | |
| 80 | + | |
| 81 | + echo '<div class="cookieadmin-card"> | |
| 82 | + <div class="cookieadmin-card-header"> | |
| 83 | + <div> | |
| 84 | + <span class="cookieadmin-card-title"><span class="dashicons dashicons-list-view"></span> '.esc_html__('Scanned Cookies', 'cookieadmin').'</span> | |
| 85 | + <p class="cookieadmin-desc">'.esc_html__('Scanned cookies will be automatically categorised and displayed here. You can add, edit and delete cookies as per your needs.', 'cookieadmin').'</p> | |
| 86 | + </div> | |
| 87 | + <div class="cookieadmin-toolbar-actions"> | |
| 88 | + <input type="button" class="cookieadmin-btn cookieadmin-btn-secondary cookieadmin-add-cookie" value="'.esc_html__('Add Cookie', 'cookieadmin').'" cookieadmin-pro-only="1"></input> | |
| 89 | + '.wp_kses_post($cookieadmin_requires_pro). | |
| 90 | + ( !empty($cookieadmin_requires_pro) ? ' | |
| 91 | + <input type="button" class="cookieadmin-btn cookieadmin-btn-primary cookieadmin-scan" value="'.esc_html__('Scan', 'cookieadmin').'"></input> ' : '').' | |
| 92 | + <input type="button" class="cookieadmin-btn cookieadmin-btn-primary cookieadmin-scan" value="'.esc_html__('Full Scan', 'cookieadmin').'" cookieadmin-pro-only="1"> | |
| 93 | + '.wp_kses_post($cookieadmin_requires_pro).' | |
| 94 | + </div> | |
| 95 | + </div> | |
| 96 | + <div class="cookieadmin-card-body"> | |
| 97 | + <div class="cookieadmin-manager-result"> | |
| 98 | + <table class="cookieadmin-table cookieadmin-cookie-categorized"> | |
| 99 | + <thead> | |
| 100 | + <tr> | |
| 101 | + <th width="30%">'.esc_html__('Name', 'cookieadmin').'</th> | |
| 102 | + <th width="50%">'.esc_html__('Description', 'cookieadmin').'</th> | |
| 103 | + <th width="10%">'.esc_html__('Expiry', 'cookieadmin').'</th> | |
| 104 | + <th width="10%">'.esc_html__('Action', 'cookieadmin').'</th> | |
| 105 | + </tr> | |
| 106 | + </thead> | |
| 107 | + <tbody id="necessary_tbody"> | |
| 108 | + <tr><td colspan=4>'.esc_html__('Necessary Cookies', 'cookieadmin').'</td></tr> | |
| 109 | + '.( !empty($categorized['Necessary']) ? $no_cookies_hidden . wp_kses_post($categorized['Necessary']) : $no_cookies ).' | |
| 110 | + </tbody> | |
| 111 | + <tbody id="functional_tbody"> | |
| 112 | + <tr><td colspan=4>'.esc_html__('Functional Cookies', 'cookieadmin').'</td></tr> | |
| 113 | + '.( !empty($categorized['Functional']) ? $no_cookies_hidden . wp_kses_post($categorized['Functional']) : $no_cookies ).' | |
| 114 | + </tbody> | |
| 115 | + <tbody id="analytics_tbody"> | |
| 116 | + <tr><td colspan=4>'.esc_html__('Analytical Cookies', 'cookieadmin').'</td></tr> | |
| 117 | + '.( !empty($categorized['Analytics']) ? $no_cookies_hidden . wp_kses_post($categorized['Analytics']) :$no_cookies ).' | |
| 118 | + </tbody> | |
| 119 | + <tbody id="marketing_tbody"> | |
| 120 | + <tr><td colspan=4>'.esc_html__('Marketing Cookies', 'cookieadmin').'</td></tr> | |
| 121 | + '.( !empty($categorized['Marketing']) ? $no_cookies_hidden . wp_kses_post($categorized['Marketing']) : $no_cookies ).' | |
| 122 | + </tbody> | |
| 123 | + <tbody id="unknown_tbody"> | |
| 124 | + <tr><td colspan=4>'.esc_html__('Unknown Cookies', 'cookieadmin').'</td></tr> | |
| 125 | + '.( !empty($categorized['Unknown']) ? $no_cookies_hidden . wp_kses_post($categorized['Unknown']) : $no_cookies ).' | |
| 126 | + </tbody> | |
| 127 | + </table> | |
| 128 | + </div> | |
| 129 | + </div> | |
| 130 | + </div>'; | |
| 131 | + | |
| 132 | + wp_nonce_field('cookieadmin_admin_nonce', 'cookieadmin_security'); | |
| 133 | + | |
| 134 | + echo ' | |
| 135 | + </div> | |
| 136 | + </form> | |
| 137 | + <br/>'; | |
| 138 | + | |
| 139 | + \CookieAdmin\Admin::footer_theme(); | |
| 140 | + | |
| 141 | + echo ' | |
| 142 | + <!-- Modal Overlay --> | |
| 143 | + <div class="cookieadmin_modal-overlay" id="edit-cookie-modal" hidden> | |
| 144 | + <div class="cookieadmin_modal-container"> | |
| 145 | + <div class="cookieadmin_modal-header"> | |
| 146 | + <h2>'.esc_html__('Edit Cookie', 'cookieadmin').'</h2> | |
| 147 | + <button class="cookieadmin_dialog_modal_close_btn">×</button> | |
| 148 | + </div> | |
| 149 | + | |
| 150 | + <div class="cookieadmin_modal-body"> | |
| 151 | + <div class="cookieadmin_form-group"> | |
| 152 | + <label for="cookieadmin-dialog-cookie-category">'.esc_html__('Category', 'cookieadmin').'</label> | |
| 153 | + <select id="cookieadmin-dialog-cookie-category"> | |
| 154 | + <option value="" selected>'.esc_html__('Select a category', 'cookieadmin').'</option> | |
| 155 | + <option value="Necessary">'.esc_html__('Necessary', 'cookieadmin').'</option> | |
| 156 | + <option value="Functional">'.esc_html__('Functional', 'cookieadmin').'</option> | |
| 157 | + <option value="Analytics">'.esc_html__('Analytical', 'cookieadmin').'</option> | |
| 158 | + <option value="Marketing">'.esc_html__('Marketing', 'cookieadmin').'</option> | |
| 159 | + <option value="Unknown">'.esc_html__('Unknown', 'cookieadmin').'</option> | |
| 160 | + </select> | |
| 161 | + </div> | |
| 162 | + | |
| 163 | + <div class="cookieadmin_form-group"> | |
| 164 | + <label for="cookie_id">'.esc_html__('Cookie Name/ID', 'cookieadmin').'</label> | |
| 165 | + <input type="text" id="cookieadmin-dialog-cookie-name" Placeholder="'.esc_html__('Enter Cookie Name or id', 'cookieadmin').'"> | |
| 166 | + </div> | |
| 167 | + | |
| 168 | + <div class="cookieadmin_form-group"> | |
| 169 | + <label for="description">'.esc_html__('Description', 'cookieadmin').'</label> | |
| 170 | + <textarea id="cookieadmin-dialog-cookie-desc" Placeholder="'.esc_html__('Enter Cookie description here', 'cookieadmin').'"></textarea> | |
| 171 | + </div> | |
| 172 | + | |
| 173 | + <div class="cookieadmin_form-group"> | |
| 174 | + <label for="duration">'.esc_html__('Duration', 'cookieadmin').'</label> | |
| 175 | + <input type="number" min=0 id="cookieadmin-dialog-cookie-duration" Placeholder="'.esc_html__('Set 0 for Session or expiry in days', 'cookieadmin').'"> | |
| 176 | + </div> | |
| 177 | + </div> | |
| 178 | + <div class="cookieadmin_modal-footer"> | |
| 179 | + <span id="cookieadmin-message"></span> | |
| 180 | + <button class="cookieadmin-btn cookieadmin-btn-primary" id="cookieadmin_dialog_save_btn" form="edit-cookie-form">'.esc_html__('Save', 'cookieadmin').'</button> | |
| 181 | + </div> | |
| 182 | + </div> | |
| 183 | + </div>'; | |
| 184 | + } | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + static function scan_cookies_ajax(){ | |
| 189 | + global $cookieadmin_error; | |
| 190 | + | |
| 191 | + $urls = []; | |
| 192 | + if(!empty($_REQUEST['urls'])){ | |
| 193 | + $urls = map_deep(wp_unslash($_REQUEST['urls']), 'sanitize_url'); | |
| 194 | + } | |
| 195 | + | |
| 196 | + if(cookieadmin_is_pro()){ | |
| 197 | + $scanner_info = get_option('cookieadmin_pro_scanner', []); | |
| 198 | + | |
| 199 | + if(!empty($scanner_info['last_scan']) && (time() < $scanner_info['last_scan'] + 3600)){ | |
| 200 | + wp_send_json([ | |
| 201 | + 'success' => false, | |
| 202 | + 'message' => __('Cookie Scan can be triggered once an hour', 'cookieadmin') | |
| 203 | + ]); | |
| 204 | + } | |
| 205 | + } | |
| 206 | + | |
| 207 | + self::scan_cookies($urls); | |
| 208 | + | |
| 209 | + if(!empty($cookieadmin_error)){ | |
| 210 | + wp_send_json([ | |
| 211 | + 'success' => false, | |
| 212 | + 'message' => $cookieadmin_error] | |
| 213 | + ); | |
| 214 | + } | |
| 215 | + | |
| 216 | + wp_send_json(['success' => true, 'data' => null]); | |
| 217 | + } | |
| 218 | + | |
| 219 | + // Orchestrator function for scanning cookies | |
| 220 | + static function scan_cookies($urls = []){ | |
| 221 | + global $cookieadmin_error; | |
| 222 | + | |
| 223 | + if(cookieadmin_is_pro()){ | |
| 224 | + | |
| 225 | + if(!method_exists('\CookieAdminPro\Admin', 'cookieadmin_get_site_urls')){ | |
| 226 | + $urls = [home_url()]; | |
| 227 | + } else { | |
| 228 | + $urls = \CookieAdminPro\Admin::cookieadmin_get_site_urls($urls); | |
| 229 | + } | |
| 230 | + | |
| 231 | + $cookieData = apply_filters('cookieadmin_pro_scan_cookies', $urls); | |
| 232 | + | |
| 233 | + //Server side scann - skipped for now - need to discuss. | |
| 234 | + // $cookieData2 = \CookieAdmin\Scanner::start_scan($urls); | |
| 235 | + // $cookieData = array_replace_recursive($cookieData2, $cookieData1); | |
| 236 | + | |
| 237 | + if(!empty($cookieadmin_error)){ | |
| 238 | + update_option('cookieadmin_scan', [ | |
| 239 | + 'status' => 3, | |
| 240 | + 'success' => false, | |
| 241 | + 'message' => $cookieadmin_error, | |
| 242 | + 'update' => time() | |
| 243 | + ]); | |
| 244 | + return; | |
| 245 | + } | |
| 246 | + | |
| 247 | + if(!empty($cookieData)){ | |
| 248 | + | |
| 249 | + if(function_exists('cookieadmin_pro_get_remaining_urls')){ | |
| 250 | + // Check Remaining urls | |
| 251 | + $remaining_urls = cookieadmin_pro_get_remaining_urls($urls); | |
| 252 | + | |
| 253 | + if(!empty($remaining_urls)){ | |
| 254 | + //send next batch for scan | |
| 255 | + wp_schedule_single_event(time() + 5, 'cookieadmin_run_auto_scan_batch', [$remaining_urls]); | |
| 256 | + } | |
| 257 | + } | |
| 258 | + | |
| 259 | + $res = self::save_raw_scan_results($cookieData); | |
| 260 | + if(function_exists('cookieadmin_pro_update_scan_count')){ | |
| 261 | + cookieadmin_pro_update_scan_count($res); | |
| 262 | + } | |
| 263 | + | |
| 264 | + self::cookieadmin_auto_configure_cookies(); | |
| 265 | + return; | |
| 266 | + } | |
| 267 | + | |
| 268 | + }else{ | |
| 269 | + $cookieData = \CookieAdmin\Scanner::start_scan(); | |
| 270 | + if(!empty($cookieData)){ | |
| 271 | + self::save_raw_scan_results($cookieData); | |
| 272 | + return self::cookieadmin_auto_configure_cookies(); | |
| 273 | + } | |
| 274 | + } | |
| 275 | + // cookieadmin_r_print($cookieData); | |
| 276 | + | |
| 277 | + if(defined('DOING_CRON') && get_transient('cookieadmin_auto_scan_in_progress')){ | |
| 278 | + delete_transient('cookieadmin_auto_scan_in_progress'); | |
| 279 | + } | |
| 280 | + | |
| 281 | + update_option('cookieadmin_scan', [ | |
| 282 | + 'status' => 3, | |
| 283 | + 'success' => false, | |
| 284 | + 'message' => __('No Cookies Found', 'cookieadmin'), | |
| 285 | + 'update' => time() | |
| 286 | + ]); | |
| 287 | + | |
| 288 | + return false; | |
| 289 | + } | |
| 290 | + | |
| 291 | + static function save_raw_scan_results(array $found_cookies){ | |
| 292 | + | |
| 293 | + global $wpdb; | |
| 294 | + | |
| 295 | + $table_name = esc_sql($wpdb->prefix . 'cookieadmin_cookies'); | |
| 296 | + | |
| 297 | + if (empty($found_cookies)) { | |
| 298 | + return ['inserted' => 0, 'updated' => 0]; | |
| 299 | + } | |
| 300 | + | |
| 301 | + // Step 1: Fetch all existing cookie names from our database in one efficient query. | |
| 302 | + $existing_cookies_in_db = $wpdb->get_col("SELECT cookie_name FROM {$table_name}"); | |
| 303 | + // Use array_flip for very fast 'isset' lookups instead of slow 'in_array' in a loop. | |
| 304 | + $existing_cookies_lookup = !empty($existing_cookies_in_db) ? array_flip($existing_cookies_in_db) : []; | |
| 305 | + | |
| 306 | + $results = ['inserted' => 0, 'updated' => 0]; | |
| 307 | + | |
| 308 | + // Step 2: Loop through each cookie found by the scanner. | |
| 309 | + foreach ($found_cookies as $cookie_name => $cookie_data) { | |
| 310 | + | |
| 311 | + // Step 3: Check if the cookie exists in our DB. | |
| 312 | + if (isset($existing_cookies_lookup[$cookie_name])) { | |
| 313 | + $wpdb->update( | |
| 314 | + $table_name, | |
| 315 | + [ | |
| 316 | + 'domain' => sanitize_text_field($cookie_data['domain']), | |
| 317 | + 'path' => sanitize_text_field($cookie_data['path']), | |
| 318 | + 'expires' => !empty($cookie_data['session']) ? 0 : (!empty($cookie_data['expires']) ? $cookie_data['expires'] : null), | |
| 319 | + 'samesite' => !empty($cookie_data['samesite']) ? sanitize_text_field($cookie_data['samesite']) : null, | |
| 320 | + 'secure' => (int)($cookie_data['secure'] ?? 0), | |
| 321 | + 'httponly' => (int)($cookie_data['httponly'] ?? 0), | |
| 322 | + 'raw_name' => sanitize_text_field($cookie_name), | |
| 323 | + 'scan_timestamp' => time(), | |
| 324 | + ], // Data to update | |
| 325 | + [ 'cookie_name' => $cookie_name ], // WHERE clause | |
| 326 | + ['%s', '%s', '%s', '%s', '%d', '%d', '%s', '%d'], // Format for the data | |
| 327 | + [ '%s' ] // Format for the WHERE clause | |
| 328 | + ); | |
| 329 | + $results['updated']++; | |
| 330 | + | |
| 331 | + } else { | |
| 332 | + | |
| 333 | + if($cookie_name == 'cookieadmin_consent' && empty($cookie_data['expires'])){ | |
| 334 | + $view = get_option('cookieadmin_law', 'cookieadmin_gdpr'); | |
| 335 | + $policy = cookieadmin_load_policy(); | |
| 336 | + | |
| 337 | + if(!empty($policy) && !empty($policy[$view])){ | |
| 338 | + $cookie_exp_days = (int) (!empty($policy[$view]['cookieadmin_days']) ? $policy[$view]['cookieadmin_days'] : 365); | |
| 339 | + $utc_time = gmdate('Y-m-d H:i:s', strtotime('+'.$cookie_exp_days.' days')); | |
| 340 | + } else { | |
| 341 | + $utc_time = gmdate('Y-m-d H:i:s', strtotime('+365 days')); | |
| 342 | + } | |
| 343 | + | |
| 344 | + $cookie_data['expires'] = $utc_time; | |
| 345 | + } | |
| 346 | + | |
| 347 | + // ------ INSERT a NEW cookie ------ | |
| 348 | + $data = [ | |
| 349 | + 'cookie_name' => sanitize_text_field($cookie_name), | |
| 350 | + 'domain' => sanitize_text_field($cookie_data['domain']), | |
| 351 | + 'path' => sanitize_text_field($cookie_data['path']), | |
| 352 | + 'expires' => !empty($cookie_data['session']) ? 0 : (!empty($cookie_data['expires']) ? $cookie_data['expires'] : null), | |
| 353 | + 'samesite' => !empty($cookie_data['samesite']) ? sanitize_text_field($cookie_data['samesite']) : null, | |
| 354 | + 'secure' => (int)($cookie_data['secure'] ?? 0), | |
| 355 | + 'httponly' => (int)($cookie_data['httponly'] ?? 0), | |
| 356 | + 'raw_name' => sanitize_text_field($cookie_name), | |
| 357 | + 'scan_timestamp' => time(), | |
| 358 | + ]; | |
| 359 | + | |
| 360 | + $formats = ['%s', '%s', '%s', '%s', '%s', '%d', '%d', '%s', '%d']; | |
| 361 | + | |
| 362 | + if ($wpdb->insert($table_name, $data, $formats)) { | |
| 363 | + $results['inserted']++; | |
| 364 | + } else { | |
| 365 | + //error_log("CookieAdmin: Error inserting cookie data: " . $wpdb->last_error); | |
| 366 | + } | |
| 367 | + } | |
| 368 | + } | |
| 369 | + return $results; | |
| 370 | + } | |
| 371 | + | |
| 372 | + static function cookieadmin_auto_configure_cookies(){ | |
| 373 | + global $wpdb, $cookieadmin_error; | |
| 374 | + | |
| 375 | + $table_name = $wpdb->prefix . 'cookieadmin_cookies'; | |
| 376 | + $categorized_cookies = []; | |
| 377 | + $uncategorized_cookies = []; | |
| 378 | + | |
| 379 | + $all_cookies = $wpdb->get_results("SELECT id, cookie_name, category FROM {$table_name}"); | |
| 380 | + | |
| 381 | + foreach($all_cookies as $cookie){ | |
| 382 | + | |
| 383 | + if(!empty($cookie->category)){ | |
| 384 | + $categorized_cookies[$cookie->id] = $cookie->cookie_name; | |
| 385 | + }else{ | |
| 386 | + $uncategorized_cookies[] = $cookie->cookie_name; | |
| 387 | + } | |
| 388 | + } | |
| 389 | + | |
| 390 | + if(!empty($uncategorized_cookies)){ | |
| 391 | + | |
| 392 | + $uncategorized_cookies = array_flip($uncategorized_cookies); | |
| 393 | + $categorized_cookies = array_flip($categorized_cookies); | |
| 394 | + | |
| 395 | + | |
| 396 | + $categorizd_cookies = \CookieAdmin\CookieCategorizer::categorize_cookies($uncategorized_cookies, $categorized_cookies); | |
| 397 | + | |
| 398 | + $remove_cookies = !empty($categorizd_cookies['remove_cookies']) ? $categorizd_cookies['remove_cookies'] : []; | |
| 399 | + | |
| 400 | + unset($categorizd_cookies['remove_cookies']); | |
| 401 | + if(!empty($remove_cookies)){ | |
| 402 | + $placeholders = implode(',', array_fill(0, count($remove_cookies), '%s')); | |
| 403 | + $sql = $wpdb->prepare("DELETE FROM {$table_name} WHERE id IN ({$placeholders})", ...$remove_cookies); | |
| 404 | + $wpdb->query($sql); | |
| 405 | + } | |
| 406 | + | |
| 407 | + foreach($categorizd_cookies as $cookie_data){ | |
| 408 | + | |
| 409 | + $count = $wpdb->update( | |
| 410 | + $table_name, | |
| 411 | + [ 'cookie_name' => $cookie_data['cookie_name'], 'category' => $cookie_data['category'], 'description' => $cookie_data['description'], 'edited' => 1, 'patterns' => $cookie_data['patterns'] ], // Data to update | |
| 412 | + [ 'raw_name' => $cookie_data['raw_name'] ], // WHERE | |
| 413 | + [ '%s', '%s', '%s', '%d', '%s' ], // Format for the data | |
| 414 | + [ '%s' ] // Format for the WHERE clause | |
| 415 | + ); | |
| 416 | + | |
| 417 | + } | |
| 418 | + | |
| 419 | + update_option('cookieadmin_scan', [ | |
| 420 | + 'status' => 3, | |
| 421 | + 'update' => time(), | |
| 422 | + 'success' => true, | |
| 423 | + 'count' => $count | |
| 424 | + ]); | |
| 425 | + | |
| 426 | + $categorized_cookies = $wpdb->get_results("SELECT id, cookie_name, category, expires, scan_timestamp, description FROM {$table_name}"); | |
| 427 | + | |
| 428 | + delete_option('cookieadmin_first_scan'); | |
| 429 | + | |
| 430 | + return $categorized_cookies; | |
| 431 | + } | |
| 432 | + | |
| 433 | + $cookieadmin_error = $cookieadmin_error . ' ' . __('No new cookies Found!', 'cookieadmin'); | |
| 434 | + | |
| 435 | + return false; | |
| 436 | + } | |
| 437 | + | |
| 438 | + | |
| 439 | + static function edit_cookies(){ | |
| 440 | + global $wpdb; | |
| 441 | + | |
| 442 | + $table_name = esc_sql($wpdb->prefix . 'cookieadmin_cookies'); | |
| 443 | + $data = null; | |
| 444 | + | |
| 445 | + if(empty($_REQUEST['cookie_info'])){ | |
| 446 | + wp_send_json(['success' => false, | |
| 447 | + 'data' => null, | |
| 448 | + 'message' => __('Error : Cookie details missing', 'cookieadmin')] | |
| 449 | + ); | |
| 450 | + } | |
| 451 | + | |
| 452 | + $cookie_info = map_deep(wp_unslash($_REQUEST['cookie_info']), 'sanitize_text_field'); | |
| 453 | + | |
| 454 | + $scan_timestamp = $wpdb->get_col($wpdb->prepare("SELECT scan_timestamp FROM {$table_name} WHERE id = %d", $cookie_info['id'])); | |
| 455 | + | |
| 456 | + if(empty($scan_timestamp)){ | |
| 457 | + wp_send_json(['success' => false, | |
| 458 | + 'data' => null, | |
| 459 | + 'message' => __('Error : Invalid cookie record', 'cookieadmin')] | |
| 460 | + ); | |
| 461 | + } | |
| 462 | + | |
| 463 | + $calculated_expiry_seconds = ($cookie_info['duration'] * 86400) + $scan_timestamp[0]; | |
| 464 | + $calculated_expiry = date('Y-m-d H:i:s', $calculated_expiry_seconds); | |
| 465 | + | |
| 466 | + $resp = $wpdb->update( | |
| 467 | + $table_name, | |
| 468 | + [ 'cookie_name' => $cookie_info['name'], 'description' => $cookie_info['description'], 'expires' => $calculated_expiry, 'category' => $cookie_info['type'], 'edited' => 1], // Data to update | |
| 469 | + [ 'id' => $cookie_info['id'] ], // WHERE | |
| 470 | + [ '%s', '%s', '%s', '%s', '%d' ], // Format for the data | |
| 471 | + [ '%d' ] // Format for the WHERE clause | |
| 472 | + ); | |
| 473 | + | |
| 474 | + if ($wpdb->last_error || $resp === false) { | |
| 475 | + //error_log('DB Error: ' . $wpdb->last_error); // Log it | |
| 476 | + wp_send_json(['success' => false, | |
| 477 | + 'data' => null, | |
| 478 | + 'message' => __('Cookie updation Failed, Error: ', 'cookieadmin') . esc_html($wpdb->last_error)]); | |
| 479 | + } | |
| 480 | + | |
| 481 | + wp_send_json(['success' => true, | |
| 482 | + 'data' => $data, | |
| 483 | + 'message' => __('Cookie updation successful', 'cookieadmin')]); | |
| 484 | + | |
| 485 | + } | |
| 486 | + | |
| 487 | + static function delete_cookies(){ | |
| 488 | + global $wpdb; | |
| 489 | + | |
| 490 | + $table_name = esc_sql($wpdb->prefix . 'cookieadmin_cookies'); | |
| 491 | + | |
| 492 | + if(empty($_REQUEST['cookie_raw_id'])){ | |
| 493 | + wp_send_json(['success' => false, | |
| 494 | + 'data' => null, | |
| 495 | + 'message' => __('Error : Cookie Id missing', 'cookieadmin')]); | |
| 496 | + } | |
| 497 | + | |
| 498 | + $cookie_id = (int) sanitize_text_field(wp_unslash($_REQUEST['cookie_raw_id'])); | |
| 499 | + | |
| 500 | + $resp = $wpdb->delete( $table_name, ['id' => $cookie_id], [ '%s' ] ); | |
| 501 | + | |
| 502 | + if ($wpdb->last_error || $resp === false) { | |
| 503 | + //error_log('DB Error: ' . $wpdb->last_error); //Log it | |
| 504 | + wp_send_json(['success' => false, | |
| 505 | + 'data' => null, | |
| 506 | + 'message' => __('Cookie deletion Failed, Error: ', 'cookieadmin') . esc_html($wpdb->last_error)]); | |
| 507 | + } | |
| 508 | + | |
| 509 | + wp_send_json(['success' => true, | |
| 510 | + 'message' => __('Cookie deletion successful', 'cookieadmin')]); | |
| 511 | + } | |
| 512 | + | |
| 513 | +} | |