<?php
if (!defined('TONSUI_LOADED')) { define('TONSUI_LOADED', true); }
require_once __DIR__ . '/session_init.php';
require_once __DIR__ . '/auth.php';

// Admin-only download of plaintext registration log
header('Cache-Control: no-cache, no-store, must-revalidate');
header('Pragma: no-cache');
header('Expires: 0');
$origin = $_SERVER['HTTP_ORIGIN'] ?? '';
if (!empty($origin)) { header('Access-Control-Allow-Origin: ' . $origin); }
else if (!empty($_SERVER['HTTP_HOST'])) { $scheme = $_SERVER['REQUEST_SCHEME'] ?? 'https'; header('Access-Control-Allow-Origin: ' . $scheme . '://' . $_SERVER['HTTP_HOST']); }
header('Access-Control-Allow-Credentials: true');

requireAdmin(true);

$logPath = realpath(__DIR__ . '/../logs');
$file = $logPath ? ($logPath . '/registrations_raw.txt') : null;
if (!$file || !file_exists($file)) {
  header('Content-Type: application/json');
  http_response_code(404);
  echo json_encode(['success' => false, 'error' => 'No registration log found']);
  exit;
}

$filename = 'registrations_raw_' . date('Ymd_His') . '.txt';
header('Content-Type: text/plain');
header('Content-Disposition: attachment; filename=' . $filename);
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
