[] = " ' . # : * Strips: pipes (|), newlines, null bytes, and other log-corrupting characters. */ public static function sanitize_css_selector($selector) { if (!is_string($selector) || $selector === '') { return ''; } // Strip null bytes $selector = str_replace("\0", '', $selector); // Strip pipes (would corrupt our pipe-delimited format) $selector = str_replace('|', '', $selector); // Strip newlines and carriage returns $selector = preg_replace('/[\r\n]+/', '', $selector); // Strip HTML tags $selector = wp_strip_all_tags($selector); // Limit length to prevent abuse (selectors shouldn't be longer than ~500 chars) if (strlen($selector) > 500) { $selector = substr($selector, 0, 500); } return trim($selector); } public function enqueue_admin_assets($hook) { if ($hook !== 'abst_experiments_page_abst-heatmaps') { return; } wp_enqueue_script( 'abst-heatmap-lib', plugins_url('../js/heatmap.src.js', __FILE__), array(), ABST_VERSION, true ); wp_enqueue_script( 'abst-journeys', ABST_PLUGIN_URI . 'js/journey.js', array('jquery', 'abst-heatmap-lib'), ABST_VERSION, true ); wp_localize_script('abst-journeys', 'abst_journey_data', array( 'page_selector_nonce' => wp_create_nonce('abst_page_selector') )); } function ajax_clear_heatmap_data() { if(!current_user_can('manage_options')){ wp_send_json_error('Unauthorized'); return; } if (!isset($_POST['nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['nonce'])), 'abst_clear_heatmap_data')) { wp_send_json_error('Security check failed'); return; } abst_log('Clearing all heatmap data'); $this->delete_journey_data(0); wp_send_json_success('Journey data cleared successfully'); } public function delete_journey_data($retention_days = false) { //file format abst_journeys__yyyymmdd.txt //delete all journey data older than retention_days abst_log('Deleting old journey data'); $delete_all = ($retention_days === 0 || $retention_days === '0'); if($retention_days === false || $retention_days === null || $retention_days === '') $retention_days = abst_get_admin_setting('abst_heatmap_retention_length'); $journey_files = glob(ABST_JOURNEY_DIR . '/*.txt'); // abst_journeys__20251009.txt $journey_files_gz = glob(ABST_JOURNEY_DIR . '/*.gz'); // abst_journeys__20251009.txt.gz foreach ($journey_files as $journey_file) { $file_date = substr(basename($journey_file, '.txt'), -8); if ($delete_all || strtotime($file_date) < strtotime('-' . $retention_days . ' days')) { wp_delete_file($journey_file); $file_date = gmdate('Y-m-d', strtotime($file_date)); abst_log('Deleted old journey data file for day ' . $file_date); } } foreach ($journey_files_gz as $journey_file) { $file_date = substr(basename($journey_file, '.txt.gz'), -8); if ($delete_all || strtotime($file_date) < strtotime('-' . $retention_days . ' days')) { wp_delete_file($journey_file); $file_date = gmdate('Y-m-d', strtotime($file_date)); abst_log('Deleted old journey data file for day gz ' . $file_date); } } //if gzip functions exist, get yesterdays file and gzip it if (!$delete_all && function_exists('gzencode') && function_exists('gzdecode')) { $yesterday = gmdate('Ymd', strtotime('-1 day')); // ✅ Fixed format $yesterday_file = abst_journey_file($yesterday); $gz_file = abst_journey_file($yesterday, true); // Check if uncompressed file exists and compressed doesn't if (file_exists($yesterday_file) && !file_exists($gz_file)) { $file_size = @filesize($yesterday_file); // Only compress if file exists, has content, and is under 10MB if ($file_size !== false && $file_size > 0 && $file_size < 10485760) { // Read original file $content = @file_get_contents($yesterday_file); if ($content === false) { abst_log('Failed to read journey file for compression: ' . $yesterday); return; } // Compress with level 9 (maximum compression) $gz_data = @gzencode($content, 9); if ($gz_data === false) { abst_log('Failed to compress journey file: ' . $yesterday); return; } // Write compressed file with exclusive lock $written = abst_put_contents($gz_file, $gz_data); if ($written === false) { abst_log('Failed to write compressed journey file: ' . $yesterday); return; } // Verify compressed file is readable before deleting original $verify = @gzdecode(@file_get_contents($gz_file)); if ($verify === false || $verify !== $content) { abst_log('Compressed file verification failed, keeping original: ' . $yesterday); wp_delete_file($gz_file); // Remove bad compressed file return; } // Safe to delete original wp_delete_file($yesterday_file); if (!file_exists($yesterday_file)) { $saved_bytes = $file_size - filesize($gz_file); $saved_percent = round(($saved_bytes / $file_size) * 100, 1); abst_log('Compressed journey file ' . $yesterday . ' - saved ' . $saved_percent . '% (' . round($saved_bytes / 1024, 1) . ' KB)'); } else { abst_log('Failed to delete original journey file after compression: ' . $yesterday); } } } } } /** * Read journey data for a specific date * Handles both compressed (.txt.gz) and uncompressed (.txt) files * * @param string $date Date in Ymd format (e.g., '20250109') * @return array Array of journey records */ public function read_journey_file($date) { // Guard against path traversal: the date must be an 8-digit Ymd string. if ( ! preg_match( '/^\d{8}$/', (string) $date ) ) { return []; } $file_txt = abst_journey_file($date); $file_gz = abst_journey_file($date, true); $journey_file = ''; // Check for gzipped file first (older data) if (file_exists($file_gz)) { $journey_file = $file_gz; } // Fall back to uncompressed file (current day) elseif (file_exists($file_txt)) { $journey_file = $file_txt; } else { // No file found for this date return []; } // Parse into array // Streamed rather than read whole: the old path held the compressed bytes, the // decompressed string and the exploded array simultaneously. $lines = abst_journey_file_lines_iter($journey_file); $data = []; $current_metadata = null; foreach ($lines as $line) { if (empty($line)) continue; $fields = explode('|', $line); // Check if this is a metadata line if (!empty($fields[0]) && $fields[0] === 'meta') { // Store metadata: meta|uuid|experiments|screen_size|user_id if (count($fields) >= 5) { $current_metadata = [ 'uuid' => $fields[1], 'experiments' => $fields[2], 'screen_size' => $fields[3], 'user_id' => $fields[4], 'referrer' => isset($fields[6]) ? $fields[6] : '', ]; } continue; // Don't add metadata lines to results } // Event line: timestamp|type|post_id|url|element|click_x|click_y|meta if (count($fields) >= 8 && $current_metadata) { $data[] = [ 'timestamp' => $fields[0], 'type' => $fields[1], 'post_id' => $fields[2], 'uuid' => $current_metadata['uuid'], 'url' => $fields[3], 'element_id_or_selector' => $fields[4], 'click_x' => $fields[5], 'click_y' => $fields[6], 'screen_size' => $current_metadata['screen_size'], 'meta' => $fields[7], 'referrer' => $current_metadata['referrer'] ?? '', ]; } } return $data; } /** * Check if a URL should be ignored for journey tracking * * @param string $url The URL to check * @return bool True if URL should be ignored, false otherwise */ private function journey_ignore_url($url) { if (empty($url)) { return false; } $ignore_strings = ['abst_heatmap_view', 'elementor-preview']; foreach ($ignore_strings as $ignore_string) { if (strpos($url, $ignore_string) !== false) { return true; } } return false; } public function receive_journey_data() { // Abuse protection for this public endpoint: 120 requests a minute per IP unless filtered. $ip = isset( $_SERVER['REMOTE_ADDR'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ) : '0.0.0.0'; $rate_key = 'abst_jr_' . md5($ip); $rate_count = (int) get_transient($rate_key); if ($rate_count > (int) apply_filters( 'abst_journey_requests_per_minute', 120 )) { wp_send_json_error('Rate limit exceeded', 429); } set_transient($rate_key, $rate_count + 1, MINUTE_IN_SECONDS); // Public journey collection endpoint; payload is sanitized below and rate limited by IP. // phpcs:disable WordPress.Security.NonceVerification.Missing // Bound work before JSON decoding. Normal clients flush at 30,000 JS characters. $payload_limit = max( 1, (int) apply_filters( 'abst_journey_payload_max_bytes', 256 * 1024 ) ); if ( isset( $_POST['data'] ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash -- Length check only; decoded and sanitized below. if ( ! is_string( $_POST['data'] ) || strlen( $_POST['data'] ) > 2 * $payload_limit ) { wp_send_json_error( 'Journey payload is too large', 413 ); } // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- JSON payload decoded and sanitized below. $raw_payload = wp_unslash( $_POST['data'] ); } else { $raw_payload = file_get_contents( 'php://input', false, null, 0, $payload_limit + 1 ); } if ( is_string( $raw_payload ) && strlen( $raw_payload ) > $payload_limit ) { wp_send_json_error( 'Journey payload is too large', 413 ); } if ( ! is_string( $raw_payload ) || $raw_payload === '' ) { wp_send_json_error('Invalid data'); return; } // phpcs:enable WordPress.Security.NonceVerification.Missing $records = json_decode($raw_payload, true); if (empty($records) || !is_array($records)) { abst_log('Invalid data payload'); wp_send_json_error('Invalid data'); } if ( count( $records ) > max( 1, (int) apply_filters( 'abst_journey_payload_max_records', 2048 ) ) ) { wp_send_json_error( 'Too many journey records', 413 ); } $journey_data_string = ''; $uuid_metadata_written = []; // Track which UUIDs we've written metadata for foreach ($records as $record_key => $record) { if (!is_array($record)) { abst_log('Invalid record structure for key ' . $record_key); continue; } $record_type = isset($record['type']) ? sanitize_text_field($record['type']) : ''; $required_fields = ['type', 'timestamp', 'post_id', 'uuid']; if ($record_type === 'meta') { $required_fields[] = 'screen_size'; } $sanitized = []; $record_valid = true; foreach ($required_fields as $field) { if (!isset($record[$field]) || !is_scalar($record[$field]) || $record[$field] === '') { abst_log('Missing field ' . $field . ' in journey record'); $record_valid = false; break; } $sanitized[$field] = str_replace('|', '', sanitize_text_field($record[$field])); } if (!$record_valid) { continue; } // Skip records from preview/editor modes if (isset($record['url']) && $this->journey_ignore_url($record['url'])) { continue; } // Get UUID from record (only set for meta records) $uuid = $sanitized['uuid'] ?? ''; // Check if this is a metadata event if ($sanitized['type'] === 'meta') { if (!isset($uuid_metadata_written[$uuid])) { $uuid_metadata_written[$uuid] = [ 'written' => false, 'scroll_depth' => '', 'viewport_height' => '' ]; } $experiments = isset($record['experiments']) ? str_replace('|', '', sanitize_text_field($record['experiments'])) : ''; $user_id = isset($record['user_id']) ? intval($record['user_id']) : 0; $referrer = isset($record['referrer']) ? sanitize_text_field($record['referrer']) : ''; $referrer = str_replace(['|', "\r", "\n"], '', $referrer); // Strip pipes/newlines to prevent log corruption $scroll_depth = ''; if (isset($record['meta']) && $record['meta'] !== '') { $scroll_candidate = floatval($record['meta']); if (is_finite($scroll_candidate)) { $scroll_candidate = max(0, min(100, $scroll_candidate)); $scroll_depth = (string)round($scroll_candidate, 2); } } $viewport_height = ''; if (isset($record['viewport_height']) && $record['viewport_height'] !== '') { $vh_candidate = floatval($record['viewport_height']); if (is_finite($vh_candidate) && $vh_candidate > 0) { $viewport_height = (string) round($vh_candidate); } } $existing_scroll = $uuid_metadata_written[$uuid]['scroll_depth']; $has_written = $uuid_metadata_written[$uuid]['written']; $existing_viewport = isset($uuid_metadata_written[$uuid]['viewport_height']) ? $uuid_metadata_written[$uuid]['viewport_height'] : ''; if ($viewport_height === '' && $existing_viewport !== '') { $viewport_height = $existing_viewport; } $should_write_meta = !$has_written; if (!$should_write_meta && $scroll_depth !== '') { if ($existing_scroll === '' || floatval($scroll_depth) > floatval($existing_scroll)) { $should_write_meta = true; } } if ($should_write_meta) { $meta_line = [ 'meta', $uuid, $experiments, $sanitized['screen_size'], $user_id, $scroll_depth, $referrer, $viewport_height ]; $journey_data_string .= implode('|', $meta_line) . PHP_EOL; $uuid_metadata_written[$uuid]['written'] = true; $uuid_metadata_written[$uuid]['scroll_depth'] = $scroll_depth; $uuid_metadata_written[$uuid]['viewport_height'] = $viewport_height; } elseif ($scroll_depth !== '' && ($existing_scroll === '' || floatval($scroll_depth) > floatval($existing_scroll))) { // Track the highest observed scroll depth even if we didn't rewrite the metadata line $uuid_metadata_written[$uuid]['scroll_depth'] = $scroll_depth; } // Skip writing the metadata event as a regular event continue; } // Write regular event line (without UUID and screen_size - they're in metadata) $meta_value = isset($record['meta']) ? sanitize_text_field($record['meta']) : ''; // Strip the log delimiter and line breaks so a label can never // corrupt the pipe-delimited journey log format. $meta_value = str_replace(['|', "\r", "\n"], '', $meta_value); // Sanitize click coordinates: must be numeric floats in 0-1 range, strip pipes/newlines $click_x = isset($record['click_x']) && is_numeric($record['click_x']) ? max(0, min(1, round((float) $record['click_x'], 4))) : ''; $click_y = isset($record['click_y']) && is_numeric($record['click_y']) ? max(0, min(1, round((float) $record['click_y'], 4))) : ''; // Use custom sanitizer for CSS selectors to preserve > combinators and [] attribute syntax // sanitize_text_field() encodes > to > which breaks descendant selectors $element_id_or_selector = isset($record['element_id_or_selector']) ? self::sanitize_css_selector($record['element_id_or_selector']) : ''; $url = isset($record['url']) ? str_replace('|', '', sanitize_text_field($record['url'])) : ''; $line_fields = [ $sanitized['timestamp'], $sanitized['type'], $sanitized['post_id'], $url, $element_id_or_selector, $click_x, $click_y, $meta_value ]; $journey_data_string .= implode('|', $line_fields) . PHP_EOL; } if ($journey_data_string === '') { abst_log('No valid journey records after sanitization'); wp_send_json_error('Invalid data'); } //append to file , create folder file if it doesn't exist //file format abst_journeys__yyyymmdd.txt $journey_file = abst_journey_file(gmdate('Ymd')); // Disk protection against runaway or spam traffic: stop writing today's file once it // reaches the daily size (5 MB unless filtered). $journey_daily_bytes = (int) apply_filters( 'abst_journey_daily_max_bytes', 5 * 1024 * 1024 ); if (file_exists($journey_file) && filesize($journey_file) > $journey_daily_bytes) { abst_log('Journey log file for today has reached its size limit, not adding more'); wp_send_json_error('Journey log file for today has reached its size limit'); } $written = false; $maxAttempts = 10; // 200ms total retry window (20 * 10ms) for ($attempt = 0; $attempt < $maxAttempts; $attempt++) { $written = abst_put_contents($journey_file, $journey_data_string, true); if ($written !== false) { break; } usleep(10000); // wait 10ms before retry } if ($written === false) { wp_send_json_error('Unable to write journey log'); } wp_send_json_success('Journey data received'); } } new ABST_Journeys();