<?php
/**
 * Serves robots.txt, llms.txt, llms-full.txt and sitemap.xml to crawlers so
 * their reads can be logged (tracking/crawler_log.php). .htaccess rewrites
 * those four files here only when the User-Agent looks like a crawler;
 * everyone else gets the static file directly. The response is the file,
 * byte for byte.
 */
$files = [
    'robots.txt'    => 'text/plain; charset=utf-8',
    'llms.txt'      => 'text/plain; charset=utf-8',
    'llms-full.txt' => 'text/plain; charset=utf-8',
    'sitemap.xml'   => 'application/xml; charset=utf-8',
];
$name = $_GET['f'] ?? '';
$path = __DIR__ . '/' . $name;
if (!isset($files[$name]) || !is_file($path)) {
    http_response_code(404);
    exit;
}

require_once __DIR__ . '/tracking/crawler_log.php';
salitronic_log_crawler('file', '/' . $name);

$mtime = filemtime($path);
header('Content-Type: ' . $files[$name]);
header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $mtime) . ' GMT');
header('Cache-Control: public, max-age=3600');
readfile($path);
