<?php
// Корневая папка для файлов
$baseDir = __DIR__; // корень сайта, /opt/www/files.zepg.com.ua

// Получаем путь файла из GET-параметра
$requested = $_GET['file'] ?? '';
$requested = str_replace(['..', "\0"], '', $requested); // защита от directory traversal
$requested = ltrim($requested, '/'); // на случай слэша в начале

// Полный путь к файлу
$filepath = realpath($baseDir . '/' . $requested);

// Проверка: файл существует и находится внутри $baseDir
if (!$filepath || !is_file($filepath) || strpos($filepath, $baseDir) !== 0) {
    http_response_code(404);
    exit('File not found');
}

// Определяем MIME тип по расширению
$mimeTypes = [
    'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
    'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
    'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
    'pdf'  => 'application/pdf',
    'txt'  => 'text/plain',
];
$ext = strtolower(pathinfo($filepath, PATHINFO_EXTENSION));
$contentType = $mimeTypes[$ext] ?? 'application/octet-stream';

// Заголовки для отключения кэша
header('Content-Type: ' . $contentType);
header('Content-Disposition: inline; filename="' . basename($filepath) . '"');
header('Cache-Control: no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0, private, must-revalidate');
header('Pragma: no-cache');
header('Expires: 0');
header('Content-Length: ' . filesize($filepath));
header('Accept-Ranges: none');

// Отдаём файл
readfile($filepath);
exit;