init: 我的博客项目
This commit is contained in:
+69
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
require_once __DIR__ . "/includes/config.php";
|
||||
require_once __DIR__ . "/includes/functions.php";
|
||||
require_once __DIR__ . "/includes/auth.php";
|
||||
$id = isset($_GET["id"]) ? (int)$_GET["id"] : 0;
|
||||
$stmt = db()->prepare("SELECT a.*, c.name cat_name, u.username FROM articles a LEFT JOIN categories c ON a.category_id=c.id JOIN users u ON a.user_id=u.id WHERE a.id=? AND a.status=?");
|
||||
$stmt->execute([$id, "published"]);
|
||||
$article = $stmt->fetch();
|
||||
if (!$article) { header("Location: " . SITE_URL . "index.php"); exit; }
|
||||
$pageTitle = $article["title"];
|
||||
$pageName = "";
|
||||
db()->prepare("UPDATE articles SET views=views+1 WHERE id=?")->execute([$id]);
|
||||
record_visit("article/" . $id);
|
||||
$tags = get_article_tags($id);
|
||||
$err = ""; $ok = "";
|
||||
if ($_SERVER["REQUEST_METHOD"] === "POST" && isset($_POST["comment"])) {
|
||||
$author = trim($_POST["author"] ?? "");
|
||||
$content = trim($_POST["content"] ?? "");
|
||||
$parent = isset($_POST["parent_id"]) ? (int)$_POST["parent_id"] : 0;
|
||||
if ($author === "" || $content === "") { $err = "昵称和内容不能为空"; }
|
||||
else {
|
||||
$st = db()->prepare("INSERT INTO comments (article_id, parent_id, author, content, status) VALUES (?,?,?,?,'approved')");
|
||||
$st->execute([$id, $parent ?: null, $author, $content]);
|
||||
$ok = "评论已提交";
|
||||
}
|
||||
}
|
||||
$cmtStmt = db()->prepare("SELECT * FROM comments WHERE article_id=? AND status=? ORDER BY created_at ASC");
|
||||
$cmtStmt->execute([$id, "approved"]);
|
||||
$comments = $cmtStmt->fetchAll();
|
||||
require_once __DIR__ . "/includes/header.php";
|
||||
?>
|
||||
<article class="card glass article-card">
|
||||
<h1><?php echo e($article["title"]); ?></h1>
|
||||
<div class="article-meta">
|
||||
<span><?php echo e($article["cat_name"] ?: "未分类"); ?></span>
|
||||
<span><?php echo e(substr($article["created_at"],0,10)); ?></span>
|
||||
<span><?php echo (int)$article["views"]; ?> 次阅读</span>
|
||||
<span>作者: <?php echo e($article["username"]); ?></span>
|
||||
</div>
|
||||
<div class="article-body"><?php echo markdown($article["content"]); ?></div>
|
||||
<?php if ($tags) { ?>
|
||||
<div class="card-meta" style="margin-top:24px;">
|
||||
<?php foreach ($tags as $t) { ?>
|
||||
<a class="glass-chip" href="<?php echo e(SITE_URL); ?>tag.php?id=<?php echo (int)$t["id"]; ?>"><?php echo e($t["name"]); ?></a>
|
||||
<?php } ?>
|
||||
</div>
|
||||
<?php } ?>
|
||||
</article>
|
||||
|
||||
<div class="card glass comments">
|
||||
<h3>评论 (<?php echo count($comments); ?>)</h3>
|
||||
<?php if ($ok) { ?><div class="msg"><?php echo e($ok); ?></div><?php } ?>
|
||||
<?php if ($err) { ?><div class="alert error"><?php echo e($err); ?></div><?php } ?>
|
||||
<?php foreach ($comments as $c) { ?>
|
||||
<div class="comment-item glass<?php echo $c["parent_id"] ? " reply" : ""; ?>">
|
||||
<div class="comment-head"><span><?php echo e($c["author"]); ?></span><span><?php echo e($c["created_at"]); ?></span></div>
|
||||
<div class="comment-body"><?php echo nl2br(e($c["content"])); ?></div>
|
||||
</div>
|
||||
<?php } ?>
|
||||
<?php if (!$comments) { ?><p style="color:var(--text-muted);font-size:14px;">暂无评论</p><?php } ?>
|
||||
<form method="post" class="comment-form" style="margin-top:20px;">
|
||||
<input type="hidden" name="comment" value="1">
|
||||
<div class="field"><label>昵称</label><input class="glass-input" name="author" required></div>
|
||||
<div class="field"><label>内容</label><textarea class="glass-input" name="content" required></textarea></div>
|
||||
<button class="glass-btn primary">发表评论</button>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
<?php require_once __DIR__ . "/includes/footer.php"; ?>
|
||||
Reference in New Issue
Block a user