<?php
require_once 'config.php';

// Get event from URL parameter
$event_slug = isset($_GET['event']) ? trim($_GET['event']) : '';

if (empty($event_slug)) {
    die('No event specified.');
}

// Sanitize event slug
$event_slug = preg_replace('/[^a-z0-9-]/', '', strtolower($event_slug));

// Get programme filename from database
$conn = getDBConnection();
$stmt = $conn->prepare("SELECT programme_filename FROM events WHERE event_slug = ?");
$stmt->bind_param("s", $event_slug);
$stmt->execute();
$result = $stmt->get_result();

if ($result->num_rows === 0) {
    $stmt->close();
    $conn->close();
    die('Event not found.');
}

$event_data = $result->fetch_assoc();
$stmt->close();
$conn->close();

// Get programme file
$event_dir = __DIR__ . '/events/' . $event_slug;
$programme_file = $event_dir . '/' . $event_data['programme_filename'];

if (!file_exists($programme_file)) {
    die('Programme file not found.');
}

// Set headers for download
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="programme.pdf"');
header('Content-Length: ' . filesize($programme_file));
header('Cache-Control: must-revalidate');
header('Pragma: public');

// Output file
readfile($programme_file);
exit;
?>


