setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // Create the logs table if it doesn't exist $db->exec("CREATE TABLE IF NOT EXISTS download_logs ( id INTEGER PRIMARY KEY AUTOINCREMENT, document_name TEXT NOT NULL, download_time DATETIME DEFAULT CURRENT_TIMESTAMP, ip_address TEXT, user_agent TEXT )"); echo "Database initialized successfully."; } catch (PDOException $e) { echo "Connection failed: " . $e->getMessage(); } ?>
'files/police_report_2025.pdf', 'da_emails' => 'files/emails_to_da.pdf', 'psa_transcript'=> 'files/relationship_rape_psa.pdf' 'transprancy'=> 'files/transparency.pdf' 'preservation obligation'=> 'files/preservation_obligation.pdf' ]; // 2. Validate the request token $file_key = $_GET['file'] ?? ''; if (!array_key_exists($file_key, $allowed_files)) { header("HTTP/1.0 404 Not Found"); echo "Document not found."; exit; } $file_path = $allowed_files[$file_key]; if (!file_exists($file_path)) { header("HTTP/1.0 404 Not Found"); echo "The requested file is missing from the server."; exit; } // 3. Extract metadata details securely $ip_address = $_SERVER['REMOTE_ADDR'] ?? 'Unknown'; $user_agent = $_SERVER['HTTP_USER_AGENT'] ?? 'Unknown'; // Check for proxy headers if utilizing a CDN like Cloudflare if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { $ip_address = $_SERVER['HTTP_X_FORWARDED_FOR']; } // 4. Log the interaction to the database try { $db = new PDO('sqlite:tracker.db'); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $stmt = $db->prepare("INSERT INTO download_logs (document_name, ip_address, user_agent) VALUES (:name, :ip, :ua)"); $stmt->execute([ ':name' => $file_key, ':ip' => $ip_address, ':ua' => $user_agent ]); } catch (PDOException $e) { // Fail silently to the user so the download still proceeds if logging fails error_log("Tracking error: " . $e->getMessage()); } // 5. Stream the PDF inline directly to the browser header('Content-Type: application/pdf'); header('Content-Disposition: inline; filename="' . basename($file_path) . '"'); header('Content-Length: ' . filesize($file_path)); header('Cache-Control: private, max-age=0, must-revalidate'); header('Pragma: public'); readfile($file_path); exit;