<?php
require_once __DIR__ . '/config.php';
if (!IS_INSTALLED) { header('Location: setup.php'); exit; }
require_once ROOT_PATH . '/includes/db.php';
require_once ROOT_PATH . '/includes/functions.php';
require_once ROOT_PATH . '/includes/auth.php';
require_once ROOT_PATH . '/includes/markdown.php';
$topicSlug = trim($_GET['topic'] ?? '');
$docSlug = trim($_GET['slug'] ?? '');
$topic = $topicSlug ? get_topic($topicSlug) : null;
if (!$topic) { http_response_code(404); die('Topic not found.'); }
$doc = $docSlug ? get_document($topic['id'], $docSlug) : null;
if (!$doc || ($doc['status'] !== 'published' && !has_role('contributor'))) {
http_response_code(404); die('Document not found.');
}
// Access control (same rules as document.php)
$urlToken = trim($_GET['token'] ?? '');
$accessType = $doc['access_type'] ?? 'public';
if (!has_role('contributor')) {
if ($accessType === 'link') {
if (empty($doc['access_token']) || $urlToken !== $doc['access_token']) {
http_response_code(403); die('Access denied. This document requires a valid private link.');
}
} elseif ($accessType === 'password') {
$sessionKey = 'doc_unlocked_' . $doc['id'];
if (empty($_SESSION[$sessionKey])) {
http_response_code(403); die('Access denied. Please open the document page and enter the password first.');
}
}
}
$tags = get_document_tags($doc['id']);
?><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?= h($doc['title']) ?> — <?= h($topic['title']) ?> — <?= h(get_setting('site_title', SITE_NAME)) ?></title>
<link rel="stylesheet" href="<?= h(base_url('assets/css/print.css')) ?>">
<style>
@media screen {
body { max-width:8.5in; margin:0 auto; padding:0.75in; font-family:Georgia,'Times New Roman',serif; }
.print-toolbar { background:#f5f5f5; padding:8px 12px; margin-bottom:20px; border:1px solid #ccc; font-size:13px; }
.print-toolbar button { margin-right:8px; }
}
</style>
</head>
<body>
<div class="print-toolbar no-print">
<button onclick="window.print()">🖨 Print</button>
<a href="javascript:window.close()">✕ Close</a>
<em>This document is formatted for 8.5×11 paper.</em>
</div>
<article class="print-content markdown-body">
<?= md($doc['content']) ?>
</article>
<script>
// Auto-trigger print dialog when opened directly
if (!document.referrer || document.referrer === '') {
// Don't auto-print; let user review first
}
</script>
</body>
</html>