<?php
/* DentalMarq: search and AI crawlers asking for a plain page are routed here by .htaccess. The visit is noted, then the very
   same file is sent with the same answer the server would give (200, 304 or 404). People are never routed here. */
require __DIR__ . '/botnote.php';
$path = rawurldecode((string)(parse_url((string)($_SERVER['REQUEST_URI'] ?? '/'), PHP_URL_PATH) ?: '/'));
$rel = $path === '/' ? 'index.html' : ltrim($path, '/');
$root = realpath(__DIR__);
$f = preg_match('~^[a-z0-9._-]+\.(html|txt|xml)$~i', $rel) ? realpath($root . DIRECTORY_SEPARATOR . $rel) : false;
$head = ($_SERVER['REQUEST_METHOD'] ?? 'GET') === 'HEAD';
if (!$f || dirname($f) !== $root || !is_file($f) || basename($f) === '404.html') {
  bot_note(404); http_response_code(404); header('Content-Type: text/html; charset=UTF-8');
  if (!$head) readfile($root . DIRECTORY_SEPARATOR . '404.html');
  exit;
}
$types = ['html' => 'text/html; charset=UTF-8', 'txt' => 'text/plain; charset=UTF-8', 'xml' => 'application/xml; charset=UTF-8'];
$mt = (int)filemtime($f);
header('Content-Type: ' . $types[strtolower(pathinfo($f, PATHINFO_EXTENSION))]);
header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $mt) . ' GMT');
header('Cache-Control: max-age=0');
$ims = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? strtotime((string)$_SERVER['HTTP_IF_MODIFIED_SINCE']) : false;
if ($ims !== false && $ims >= $mt) { bot_note(304); http_response_code(304); exit; }
bot_note(200);
if (!$head) readfile($f);
