<?php if (!defined('STORE_ROOT')) { http_response_code(403); exit; } class Telemetry { public static function trackPageView($page) { $sessionId = self::getSessionId(); $ip = hash('sha256', $_SERVER['REMOTE_ADDR'] ?? ''); $existing = Database::one( "SELECT id, pages_viewed FROM visitors WHERE session_id = :sid", [':sid' => $sessionId] ); if ($existing) { Database::execute( "UPDATE visitors SET pages_viewed = pages_viewed + 1, last_seen = NOW() WHERE id = :id", [':id' => $existing['id']] ); } else { Database::insert( "INSERT INTO visitors (session_id, ip_address, user_agent, pages_viewed, created_at, last_seen)                  VALUES (:sid, :ip, :ua, 1, NOW(), NOW())", [ ':sid' => $sessionId, ':ip' => $ip, ':ua' => substr($_SERVER['HTTP_USER_AGENT'] ?? '', 0, 500), ] ); } } public static function getSessionId() { if (!empty($_SESSION['telemetry_session_id'])) { return $_SESSION['telemetry_session_id']; } $_SESSION['telemetry_session_id'] = bin2hex(random_bytes(16)); return $_SESSION['telemetry_session_id']; } public static function logEvent($category, $action, $targetType = null, $targetId = null, $meta = null) { try { Database::insert( "INSERT INTO events (category, action, target_type, target_id, ip_address, admin_id, meta, created_at)                  VALUES (:cat, :act, :tt, :tid, :ip, :aid, :meta, NOW())", [ ':cat' => $category, ':act' => $action, ':tt' => $targetType, ':tid' => $targetId, ':ip' => hash('sha256', $_SERVER['REMOTE_ADDR'] ?? ''), ':aid' => $_SESSION['admin_id'] ?? null, ':meta' => $meta ? json_encode($meta) : null, ] ); } catch (\Throwable $e) { } } public static function getVisitorRiskScore($sessionId) { return (int)Database::value( "SELECT risk_score FROM visitors WHERE session_id = :sid", [':sid' => $sessionId] ); } } 
