<?php

require_once __DIR__ . '/common.php';

$pdo = db();


$siteTitle       = get_setting('site.title', '六柱博客');
$siteSubtitle    = get_setting('site.subtitle', '六柱个人博客');
$siteDescription = get_setting('site.description', '六柱个人博客');

$token = isset($_GET['token']) ? trim((string)$_GET['token']) : '';
$message = '';
$ok = false;

if ($token === '') {
    $message = '缺少验证参数。';
} else {
    try {
        $now = time();
        $stmt = $pdo->prepare("
            SELECT evt.id, evt.user_id, evt.expires_at, u.email_verified
            FROM email_verification_tokens evt
            INNER JOIN users u ON u.id = evt.user_id
            WHERE evt.token = :token
            LIMIT 1
        ");
        $stmt->execute([':token' => $token]);
        $row = $stmt->fetch();

        if (!$row) {
            $message = '验证链接无效或已使用。';
        } elseif (!empty($row['expires_at']) && $now > (int)$row['expires_at']) {
            $message = '验证链接已过期，请重新注册或申请新的验证邮件。';
            
        } elseif ((int)$row['email_verified'] === 1) {
            $message = '该账号已完成验证，请直接登录。';
            $ok = true;
        } else {
            
            $pdo->beginTransaction();

            $stmt = $pdo->prepare("UPDATE users SET email_verified = 1, updated_at = :now WHERE id = :uid");
            $stmt->execute([
                ':now' => $now,
                ':uid' => (int)$row['user_id'],
            ]);

            
            $stmt = $pdo->prepare("DELETE FROM email_verification_tokens WHERE id = :id");
            $stmt->execute([':id' => (int)$row['id']]);

            $pdo->commit();

            $message = '邮箱验证成功！现在可以使用该账号登录并发表评论 / 投稿。';
            $ok = true;
        }
    } catch (Throwable $e) {
        if ($pdo->inTransaction()) {
            $pdo->rollBack();
        }
        $message = '验证过程中出现错误：' . $e->getMessage();
    }
}


$currentUser = current_user();


$siteFooterBrand        = get_setting('site.footer_brand', '六柱php轻型博客源码');
$siteFooterOfficialUrl  = get_setting('site.footer_official_url', 'https://www.6zjz.com');
$siteFooterOfficialText = get_setting('site.footer_official_text', 'www.6zjz.com');

require_once __DIR__ . '/includes/sidebar-data.php';
extract(load_common_sidebar_data($pdo), EXTR_OVERWRITE);

?><!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>邮箱验证 - <?php echo h($siteTitle); ?></title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="<?php echo h($siteDescription); ?>">
    <style>
        :root {
            --bg: #f5f5f7;
            --bg-elevated: #ffffff;
            --bg-header: #ffffff;
            --border-subtle: #e5e7eb;
            --text-main: #111827;
            --text-muted: #6b7280;
            --accent: #2563eb;
            --accent-soft: #dbeafe;
            --accent-strong: #1d4ed8;
        }
        body[data-theme="dark"] {
            --bg: #0f172a;
            --bg-elevated: #111827;
            --bg-header: #020617;
            --border-subtle: #1f2937;
            --text-main: #e5e7eb;
            --text-muted: #9ca3af;
            --accent: #3b82f6;
            --accent-soft: #1d4ed8;
            --accent-strong: #60a5fa;
        }
        html, body { height:100%;  margin:0; padding:0; }
        body { margin:0; padding:0; font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",system-ui,sans-serif; background:var(--bg); color:var(--text-main); }
        a { color:var(--accent); text-decoration:none; }
        a:hover { text-decoration:underline; }

        .header { background:var(--bg-header); border-bottom:1px solid var(--border-subtle);  margin:0;  padding:0;  margin-top:-18px; padding-top:18px; }
        .header-inner { max-width:1200px; margin:0 auto; padding:0 16px 6px; display:grid; grid-template-columns:220px minmax(0,1fr) 260px; gap:16px; align-items:flex-start; }
        .logo { font-size:20px; font-weight:700; white-space:nowrap; }
        .subtitle { font-size:12px; color:var(--text-muted); }
                .search-box { width:100%; min-width:0; display:flex; align-items:center; gap:0; }
        .search-input { flex:1; min-width:0; position:relative; }
        .search-input input { width:100%; padding:6px 10px; border-radius:999px; border:1px solid var(--border-subtle); background:var(--bg); color:var(--text-main); font-size:14px; }
                .search-box form, form.search-box { width:100%; display:flex; align-items:center; height:40px; background:var(--bg); border:1px solid var(--border-subtle); border-radius:999px; overflow:hidden; }
.search-box form:focus-within, form.search-box:focus-within { border-color:var(--accent); box-shadow:0 0 0 2px rgba(37,99,235,0.08); }
.search-box input[type="text"], form.search-box input[type="text"] { flex:1; min-width:0; height:100%; padding:0 12px; border:0; background:transparent; color:var(--text-main); font-size:14px; }
.search-box input[type="text"]::placeholder, form.search-box input[type="text"]::placeholder { color:var(--text-muted); }
.search-btn { min-width:72px; height:100%; white-space:nowrap; padding:0 16px; border:0; border-left:1px solid var(--border-subtle); background:linear-gradient(135deg, var(--accent), var(--accent-strong)); color:#fff; font-size:13px; font-weight:600; display:inline-flex; align-items:center; justify-content:center; cursor:pointer; transition:filter .12s, transform .12s; }
.search-btn:hover { filter:brightness(1.06); transform:none; }
        .header-actions { display:flex; align-items:flex-start; justify-content:flex-end; gap:8px; flex-wrap:wrap; }
        .btn, .btn-ghost { font-size:13px; padding:6px 10px; border-radius:999px; border:1px solid var(--accent); cursor:pointer; }
        .btn { background:var(--accent-soft); color:var(--accent-strong); }
        .btn-ghost { background:transparent; color:var(--text-muted); border-color:transparent; }

        .page { max-width:1200px; margin:0 auto; padding:16px; }
        .layout { display:grid; grid-template-columns:220px minmax(0,1fr) 260px; gap:16px; }
        .column { display:flex; flex-direction:column; gap:12px; }

        .card { background:var(--bg-elevated); border-radius:6px; border:1px solid var(--border-subtle); padding:10px 12px; }
        .card-title { font-size:14px; font-weight:600; margin-bottom:6px; position:relative; padding-left:6px; }
        ul { list-style:none; margin:0; padding:0; }
        li { font-size:12px; padding:2px 0; }
        .small-text { font-size:11px; color:var(--text-muted); }

        .category-name a { color:var(--text-main); font-weight:500; }
        .category-pill { display:flex; align-items:flex-start; justify-content:flex-start; padding:4px 8px; border-radius:999px; background:rgba(37,99,235,0.05); margin-bottom:4px; transition:background 0.15s, transform 0.15s; }
        .category-pill:hover { background:rgba(37,99,235,0.12); transform:translateX(2px); }
        .category-count { font-size:11px; color:var(--text-muted); }
        .category-count-num { display:inline-block; min-width:18px; height:16px; line-height:16px; padding:0 5px; border-radius:999px; text-align:center; font-size:10px; font-weight:600; font-variant-numeric:tabular-nums; background:rgba(37,99,235,0.12); color:var(--accent-strong); }

        .contact-list li { display:flex; align-items:flex-start; margin-bottom:4px; }
        .contact-icon { width:20px; height:20px; border-radius:999px; background:rgba(37,99,235,0.12); color:var(--accent-strong); display:flex; align-items:flex-start; justify-content:center; font-size:12px; margin-right:6px; }
        .contact-text { flex:1; }
        .friend-links { display:grid; grid-template-columns:repeat(2, minmax(0,1fr)); gap:6px 8px; }
        .friend-links li { padding:0; }
        .friend-links + .small-text { text-align:left; }
        .friend-links li a {
            text-align:left; display:inline-flex; align-items:center; width:auto; max-width:100%; padding:4px 8px; border-radius:999px; background:rgba(37,99,235,0.04); transition:background 0.15s, transform 0.15s; }
        .friend-links li a:hover { background:rgba(37,99,235,0.12); transform:translateX(2px); }
        .new-sites-list li a { display:inline-block; padding:3px 0; }

        .list-compact li { border-bottom:1px dashed #e5e7eb; }
        .site-stats-list { list-style:none; margin:0; padding:0; border:0; border-radius:0; background:transparent; }
        .site-stats-item { display:flex; align-items:center; justify-content:space-between; min-height:34px; padding:0 2px; border-bottom:1px solid #eef2f7; font-size:13px; }
        .site-stats-item:last-child { border-bottom:none; }
        .site-stats-label { display:inline-flex; align-items:center; gap:8px; color:var(--text-muted); }
        .site-stats-value { color:var(--text-main); font-size:13px; font-weight:700; font-variant-numeric:tabular-nums; }
        .site-stats-item-highlight .site-stats-value { color:var(--accent-strong); }
        .site-stats-icon { width:14px; height:14px; flex:0 0 14px; display:inline-flex; align-items:center; justify-content:center; color:#64748b; }
        .site-stats-item-highlight .site-stats-icon { color:var(--accent); }
        .site-stats-icon svg { width:14px; height:14px; display:block; fill:none; stroke:currentColor; stroke-width:1.8; stroke-linecap:round; stroke-linejoin:round; }
        body[data-theme="dark"] .site-stats-item { border-bottom-color:#1f2937; }
        body[data-theme="dark"] .site-stats-icon { color:#94a3b8; }

        .msg { padding:8px 10px; border-radius:4px; font-size:13px; margin-bottom:10px; }
        .msg-ok { background:#ecfdf5; border:1px solid #bbf7d0; color:#166534; }
        .msg-error { background:#fef2f2; border:1px solid #fecaca; color:#991b1b; }

                .member-card-header {
            display:flex;
            align-items:center;
            justify-content:space-between;
            gap:8px;
            margin-bottom:6px;
        }
                .member-card-title-main {
            font-size:14px;
            font-weight:600;
        }
                .member-card-title-sub {
            display:inline-flex;
            align-items:center;
            height:20px;
            padding:0 8px;
            border-radius:999px;
            font-size:11px;
            color:#166534;
            background:#dcfce7;
            border:1px solid #86efac;
            line-height:20px;
            white-space:nowrap;
        }
                .member-actions {
            display:grid;
            grid-template-columns:repeat(3, minmax(0, 1fr));
            gap:8px;
            margin-top:8px;
        }
        .member-actions .btn {
            display:flex;
            align-items:center;
            justify-content:center;
            height:30px;
            margin-top:0;
            padding:0 8px;
            font-size:12px;
            text-align:center;
            white-space:nowrap;
        }
        .member-actions .btn.btn-logout {
            border-color:#fdba74;
            background:#fff7ed;
            color:#9a3412;
        }
        .member-welcome {
            margin:0 0 8px;
            font-size:12px;
            color:var(--text-muted);
            line-height:1.5;
        }
        .member-more {
            margin-top:6px;
            text-align:right;
            font-size:12px;
            color:var(--text-muted);
        }
        .member-guest-actions { display:flex; gap:8px; margin-top:6px; }
        .member-guest-actions .btn { flex:1; font-size:12px; padding:5px 0; }

        .site-footer { margin-top:16px; border-top:1px solid var(--border-subtle); background:var(--bg-header); }
        .footer-inner { max-width:1200px; margin:0 auto; padding:10px 16px; font-size:11px; color:var(--text-muted); display:flex; justify-content:center; flex-wrap:wrap; gap:8px; }
    </style>
</head>
<body style="margin:0;padding:0;" data-theme="light"><header class="header">
    <div class="header-inner">
        <div>
            <div class="logo"><?php echo h($siteTitle); ?></div>
            <div class="subtitle"><?php echo h($siteSubtitle); ?></div>
        </div>
        <div class="search-box">
            <div class="search-input">
                <form method="get" action="search.php">
                    <input type="text" name="q" placeholder="搜索文章（标题/正文）">
                
                    <button type="submit" class="search-btn">搜索</button>
                </form>
            </div>
        </div>
        <div class="header-actions">
            <button class="btn-ghost" id="theme-toggle">夜间模式</button>
            <a class="btn" href="member_post.php">发布文章</a>
            <button class="btn" id="fav-btn">收藏本站</button>
        </div>
    </div>
</header>

<main class="page">
    <div class="layout">
        
        <aside class="column">
            <section class="card">
                <div class="card-title">分类导航</div>
                <ul>
                    <li>
                        <div class="category-pill">
                            <span class="category-name">
                                <?php echo render_home_nav_link('index.php'); ?>
                            </span>
                            <span class="category-count">&nbsp;</span>
                        </div>
                    </li>
                    <?php foreach ($categories as $cat): ?>
                        <li>
                            <div class="category-pill">
                                <span class="category-name">
                                    <a href="category.php?id=<?php echo (int)$cat['id']; ?>">
                                        <?php if (!empty($cat['icon_path'])): ?>
                                            <img src="<?php echo h($cat['icon_path']); ?>" alt="" style="width:16px;height:16px;border-radius:3px;vertical-align:-3px;margin-right:4px;">
                                        <?php endif; ?>
                                        <?php echo h($cat['name']); ?>
                                    </a>
                                </span>
                                <span class="category-count category-count-num"><?php echo (int)$cat['article_count']; ?></span>
                            </div>
                        </li>
                    <?php endforeach; ?>
                    <?php if (!$categories): ?><li class="small-text">暂无分类</li><?php endif; ?>
                </ul>
            </section>

            <section class="card" style="order: <?php echo (int)$sidebarOrderContact; ?>;<?php if (!$showContactCard) echo 'display:none;'; ?>">
                <div class="card-title"><?php echo h($titleContactCard); ?></div>
                <ul class="contact-list">
                    <?php if (!empty($contactItems)): ?>
                        <?php foreach ($contactItems as $ci): ?>
                            <li>
                                <div class="contact-icon"><?php echo h($ci['icon']); ?></div>
                                <div class="contact-text small-text"><?php echo h($ci['label']); ?>：<?php echo h($ci['value']); ?></div>
                            </li>
                        <?php endforeach; ?>
                    <?php else: ?>
                        <li class="small-text">可在后台“站点设置 &gt; 联系方式”中配置联系方式。</li>
                    <?php endif; ?>
                </ul>
            </section>

            <section class="card" style="order: <?php echo (int)$sidebarOrderImportantLinks; ?>;<?php if (!$showImportantLinksCard) echo 'display:none;'; ?>">
                <div class="card-title"><?php echo h($titleImportantLinksCard); ?></div>
                <ul class="friend-links">
                    <?php if ($importantFriendLinks): foreach ($importantFriendLinks as $fl): ?>
                        <li>
                            <a href="<?php echo h($fl['url']); ?>" target="_blank"><?php echo h($fl['name']); ?></a>
                        </li>
                    <?php endforeach; else: ?>
                        <li class="small-text">可在后台“友链与最新网址”中添加友情链接。</li>
                    <?php endif; ?>
                </ul>
                <div class="small-text" style="margin-top:4px;">
                    <a href="links.php">查看全部友链 / 提交申请</a>
                </div>
            </section>

            <section class="card" style="order: <?php echo (int)$sidebarOrderNewSites; ?>;<?php if (!$showNewSitesCard) echo 'display:none;'; ?>">
                <div class="card-title"><?php echo h($titleNewSitesCard); ?></div>
                <ul class="new-sites-list">
                    <?php if ($newSites): foreach ($newSites as $ns): ?>
                        <li><?php echo render_new_site_anchor($ns); ?></li>
                    <?php endforeach; else: ?>
                        <li class="small-text">可在后台添加防封用域名。</li>
                    <?php endif; ?>
                </ul>
            </section>

            
            <section class="card" style="order: <?php echo (int)$sidebarOrderTags; ?>;">
                <div class="card-title">热门标签</div>
                <div>
                    <?php if ($tags): foreach ($tags as $t): ?>
                        <a class="small-text" href="tag.php?id=<?php echo (int)$t['id']; ?>" style="margin-right:4px;">#<?php echo h($t['name']); ?></a>
                    <?php endforeach; else: ?>
                        <span class="small-text">暂无标签。</span>
                    <?php endif; ?>
                </div>
            </section>
</aside>

        
        <section class="column">
            <section class="card">
                <div class="card-title">邮箱验证</div>
                <div class="msg <?php echo $ok ? 'msg-ok' : 'msg-error'; ?>">
                    <?php echo h($message); ?>
                </div>
                <div class="small-text">
                    <a href="login.php">前往登录</a> · <a href="index.php">返回首页</a>
                </div>
            </section>
        </section>

        
        <aside class="column">
            <section class="card">
                <div class="member-card-header">
                    <div class="member-card-title-main">会员中心</div>
                    <?php if ($currentUser): ?>
                        <div class="member-card-title-sub">已登录</div>
                    <?php endif; ?>
                </div>
                <?php if ($currentUser): ?>
                    <div class="member-welcome">欢迎，<?php echo h($currentUser['nickname'] ?? '会员'); ?>（ID：<?php echo (int)$currentUser['id']; ?>）
                    </div>
                    <div class="member-actions">
                        <a class="btn" href="profile.php">资料 / 头像</a>
                        <a class="btn" href="member_password.php">密码修改</a>
                        <a class="btn btn-logout" href="logout.php">退出登录</a>
                    </div>
                <?php else: ?>
                    <div class="member-guest-actions">
                        <a class="btn" href="login.php">会员登录</a>
                        <a class="btn btn-ghost" href="register.php">注册账号</a>
                    </div>
                <?php endif; ?>
            </section>

            <section class="card">
                <div class="card-title">最新评论</div>
                <ul class="list-compact">
                    <?php if ($recentComments): foreach ($recentComments as $c): ?>
                        <li>
                            <?php $name = $c['is_guest'] ? ($c['guest_name'] ?: '匿名') : ($c['nickname'] ?: '会员'); ?>
                            <?php $text = mb_substr($c['content'], 0, 30, 'UTF-8'); ?>
                            <?php echo h($name); ?>：<?php echo h($text); ?>...
                        </li>
                    <?php endforeach; else: ?>
                        <li class="small-text">暂无评论。</li>
                    <?php endif; ?>
                </ul>
            </section>

            <section class="card">
                <div class="card-title">最多点赞</div>
                <ul class="list-compact">
                    <?php if ($mostLikedArticles): foreach ($mostLikedArticles as $ma): ?>
                        <li><a href="post.php?id=<?php echo (int)$ma['id']; ?>"><?php echo h($ma['title']); ?></a>（<?php echo (int)$ma['like_count']; ?> 赞）</li>
                    <?php endforeach; else: ?>
                        <li class="small-text">暂无点赞数据。</li>
                    <?php endif; ?>
                </ul>
            </section>

            <section class="card">
                <div class="card-title">最热文章</div>
                <ul class="list-compact">
                    <?php if ($hotArticles): foreach ($hotArticles as $ha): ?>
                        <li><a href="post.php?id=<?php echo (int)$ha['id']; ?>"><?php echo h($ha['title']); ?></a>（<?php echo (int)$ha['view_count']; ?> 浏览 / <?php echo (int)$ha['like_count']; ?> 赞）</li>
                    <?php endforeach; else: ?>
                        <li class="small-text">暂无热门文章。</li>
                    <?php endif; ?>
                </ul>
            </section>

            <section class="card">
                <div class="card-title">站点统计</div>
                <ul class="site-stats-list">
                    <li class="site-stats-item site-stats-item-highlight">
                        <span class="site-stats-label"><span class="site-stats-icon" aria-hidden="true"><svg viewBox="0 0 24 24"><circle cx="12" cy="7" r="4"></circle><path d="M4 21c1.5-4 4.2-6 8-6s6.5 2 8 6"></path></svg></span>今日访客</span>
                        <span class="site-stats-value"><?php echo (int)$statsToday['uv']; ?></span>
                    </li>
                    <li class="site-stats-item site-stats-item-highlight">
                        <span class="site-stats-label"><span class="site-stats-icon" aria-hidden="true"><svg viewBox="0 0 24 24"><path d="M2 12s3.8-6 10-6 10 6 10 6-3.8 6-10 6-10-6-10-6"></path><circle cx="12" cy="12" r="2.8"></circle></svg></span>今日浏览</span>
                        <span class="site-stats-value"><?php echo (int)$statsToday['pv']; ?></span>
                    </li>
                    <li class="site-stats-item">
                        <span class="site-stats-label"><span class="site-stats-icon" aria-hidden="true"><svg viewBox="0 0 24 24"><line x1="5" y1="20" x2="19" y2="20"></line><rect x="6" y="11" width="2.8" height="7"></rect><rect x="11" y="8" width="2.8" height="10"></rect><rect x="16" y="5" width="2.8" height="13"></rect></svg></span>总浏览量</span>
                        <span class="site-stats-value"><?php echo (int)$statsTotal['pv']; ?></span>
                    </li>
                    <li class="site-stats-item">
                        <span class="site-stats-label"><span class="site-stats-icon" aria-hidden="true"><svg viewBox="0 0 24 24"><path d="M7 3h7l5 5v13H7z"></path><path d="M14 3v5h5"></path></svg></span>文章数目</span>
                        <span class="site-stats-value"><?php echo (int)($statsTotal['articles'] ?? 0); ?></span>
                    </li>
                    <li class="site-stats-item">
                        <span class="site-stats-label"><span class="site-stats-icon" aria-hidden="true"><svg viewBox="0 0 24 24"><path d="M4 6h16v9H7l-3 3z"></path></svg></span>评论数目</span>
                        <span class="site-stats-value"><?php echo (int)($statsTotal['comments'] ?? 0); ?></span>
                    </li>
                    <li class="site-stats-item">
                        <span class="site-stats-label"><span class="site-stats-icon" aria-hidden="true"><svg viewBox="0 0 24 24"><circle cx="12" cy="12" r="8"></circle><path d="M12 8v4l2.8 1.6"></path></svg></span>运行天数</span>
                        <span class="site-stats-value"><?php echo (int)($statsTotal['runtime_days'] ?? 1); ?> 天</span>
                    </li>
                </ul>
            </section></aside>
    </div>
</main>

<footer class="site-footer">
    <div class="footer-inner">
        <div class="footer-main">
            <?php echo h($siteFooterBrand); ?> &copy; <?php echo date('Y'); ?>
            <?php if ($siteFooterOfficialUrl && $siteFooterOfficialText): ?>
                · 官方站点：<a href="<?php echo h($siteFooterOfficialUrl); ?>" target="_blank"><?php echo h($siteFooterOfficialText); ?></a>
            <?php endif; ?>
        </div>
    </div>
</footer>

<script>
    (function () {
        var key  = 'lzblog-theme';
        var body = document.body;
        var btnTheme = document.getElementById('theme-toggle');
        var btnFav   = document.getElementById('fav-btn');

        function applyTheme(theme) {
            body.setAttribute('data-theme', theme);
            if (btnTheme) {
                btnTheme.textContent = theme === 'dark' ? '日间模式' : '夜间模式';
            }
        }

        var saved = localStorage.getItem(key);
        applyTheme(saved === 'dark' ? 'dark' : 'light');

        if (btnTheme) {
            btnTheme.addEventListener('click', function () {
                var next = body.getAttribute('data-theme') === 'dark' ? 'light' : 'dark';
                localStorage.setItem(key, next);
                applyTheme(next);
            });
        }

        if (btnFav) {
            btnFav.addEventListener('click', function () {
                alert('请使用浏览器的收藏功能（例如：按 Ctrl + D）来收藏本站。');
            });
        }
    })();
</script>
</body>
</html>


















