<?php include 'header.php'; ?>
<main>
<?php
$dir = __DIR__ . '/laws/';

if (!isset($_GET['file'])) {
    die('No file specified.');
}

// Sanitize the filename to prevent directory traversal
$filename = basename($_GET['file']);
$filepath = $dir . $filename;

// Check if file exists and is readable
if (!is_file($filepath) || !is_readable($filepath)) {
    die('File not found.');
}

// Read file contents
$content = file_get_contents($filepath);
$name = pathinfo($filename, PATHINFO_FILENAME);
// Convert Markdown to HTML
function markdownToHtml($text) {
    $text = htmlspecialchars($text, ENT_QUOTES, 'UTF-8');
    $text = preg_replace('/\*\*(.*?)\*\*/', '<strong>$1</strong>', $text);
    $text = preg_replace('/\*(.*?)\*/', '<em>$1</em>', $text);
    $text = preg_replace('/\#\#\# (.*?)\n/', '<h3>$1</h3>', $text);
    $text = preg_replace('/\#\# (.*?)\n/', '<h2>$1</h2>', $text);
    $text = preg_replace('/\# (.*?)\n/', '<h1>$1</h1>', $text);
    $text = nl2br($text);
    return $text;
}
echo "<h1>$name</h1>";
echo markdownToHtml($content);
?>
</main>
<?php include 'footer.php'; ?>