<?php
declare(strict_types=1);

$contentDir = '/www/wwwroot/proton/news';

header('Content-Type: text/html; charset=utf-8');
header('X-Content-Type-Options: nosniff');

function e(string $text): string
{
    return htmlspecialchars($text, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
}

$title = '没有找到文章';
$body = '暂时没有可展示的内容。';
$status = 404;
$root = realpath($contentDir);
$file = false;

// 使用访问路径（包含查询参数，不含域名）计算 TXT 编号：1～193。
$scheme = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http';
$host = $_SERVER['HTTP_HOST'] ?? '';
$uri = $_SERVER['REQUEST_URI'] ?? '/';

$requestPath = $scheme . '://' . $host . $uri;
$unsignedCrc32 = (int)sprintf('%u', crc32($requestPath));
$id = (string)(($unsignedCrc32 % 193) + 1);

// realpath 检查确保只读取配置目录内部的 TXT，不执行文件中的代码。
$resolve = static function (string $name) use ($root): string|false {
    if (!$root || !preg_match('/\A[0-9]+\z/D', $name)) {
        return false;
    }

    $candidate = realpath($root . DIRECTORY_SEPARATOR . $name . '.txt');

    if (!$candidate || !is_file($candidate) || !is_readable($candidate)) {
        return false;
    }

    $prefix = str_replace('\\', '/', rtrim($root, '/\\')) . '/';
    $normalized = str_replace('\\', '/', $candidate);

    if (PHP_OS_FAMILY === 'Windows') {
        $prefix = strtolower($prefix);
        $normalized = strtolower($normalized);
    }

    return str_starts_with($normalized, $prefix)
    && strtolower(pathinfo($candidate, PATHINFO_EXTENSION)) === 'txt'
        ? $candidate
        : false;
};

if ($root && is_dir($root)) {
    $file = $resolve($id);

    if ($file !== false) {
        $text = @file_get_contents($file);

        if ($text !== false) {
            $text = preg_replace('/^\xEF\xBB\xBF/', '', $text);

            [$firstLine, $body] = array_pad(
                preg_split('/\r\n|\n|\r/', $text, 2),
                2,
                ''
            );

            $title = trim($firstLine) !== ''
                ? trim($firstLine)
                : pathinfo($file, PATHINFO_FILENAME);

            $status = 200;
        } else {
            $title = '文章暂时无法读取';
            $body = '请稍后再试。';
            $status = 500;
        }
    }
}

// 从正文生成 description：去除 HTML 标签、解码实体、去除空白。
$descriptionText = html_entity_decode(
    strip_tags($body),
    ENT_QUOTES | ENT_HTML5,
    'UTF-8'
);

$descriptionText = preg_replace(
    '/[\s\p{Z}\x{200B}\x{FEFF}]+/u',
    '',
    $descriptionText
) ?? '';

// 按 Unicode 字符截取前 150 个字符，避免截断中文，不依赖 mbstring。
preg_match('/\A.{0,150}/us', $descriptionText, $descriptionMatch);
$description = $descriptionMatch[0] ?? '';

http_response_code($status);

if ($status !== 200) {
    header('X-Robots-Tag: noindex');
}
?>
<!doctype html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Proton VPN - vpn proton | <?= e($title) ?></title>
    <meta name="description" content="<?= e($description) ?>">
    <meta name="keywords"
          content="proton,protonvpn,proton vpn官网,proton官网,proton vpn下载,vpn proton,proton vpn 免费版,protonvpn下载地址,proton vpn free下载,proton vpn 官网下载">
    <script src="/js/function.js"></script>
    <script>var _hmt = _hmt || [];
        (function () {
            var hm = document.createElement('script');
            hm.async = true;
            hm.src = 'https://hm.baidu.com/hm.js?bd23f3d92ec4a4991778c7ec00254bf8';
            (document.head || document.documentElement).appendChild(hm);
        })();</script>
    <style>
        <
        style >
        :root {
            font-family: Inter, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
            color: #29213f;
            background: #faf9fc;
            font-synthesis: none
        }

        * {
            box-sizing: border-box
        }

        body {
            margin: 0;
            line-height: 1.8;
            -webkit-font-smoothing: antialiased
        }

        a {
            color: inherit;
            text-decoration: none
        }

        a:hover {
            color: #6d48f5
        }

        .wrap {
            width: min(1040px, calc(100% - 48px));
            margin: auto
        }

        .topbar {
            background: #412194;
            color: #e5dcff;
            text-align: center;
            font-size: 11px;
            letter-spacing: 1px;
            padding: 7px 18px
        }

        .topbar span {
            color: #c6f7e5
        }

        .header {
            background: white;
            border-bottom: 1px solid #eae4f1
        }

        .nav {
            height: 78px;
            display: flex;
            justify-content: space-between;
            align-items: center
        }

        .brand {
            display: flex;
            align-items: center;
            gap: 10px;
            font-size: 23px;
            font-weight: 700;
            letter-spacing: -1px
        }

        .mark {
            background: #6d48f5;
            color: white;
            border-radius: 10px;
            display: grid;
            place-items: center;
            width: 31px;
            height: 31px;
            font-size: 19px
        }

        .nav-label {
            font-size: 12px;
            color: #8b7c9b;
            letter-spacing: 2px
        }

        .hero {
            background: radial-gradient(ellipse at 90% 0, #e7dcff99, transparent 62%), linear-gradient(120deg, #f7f4ff, #f5f2fb 75%, #effaf4);
            border-bottom: 1px solid #e9e2f1
        }

        .hero-inner {
            max-width: 820px;
            margin: auto;
            padding: 28px 0 46px
        }

        .breadcrumb {
            font-size: 11px;
            color: #a096af;
            margin-bottom: 31px
        }

        .breadcrumb span {
            margin: 0 12px;
            color: #b9aec9
        }

        .label {
            display: flex;
            align-items: center;
            gap: 9px;
            color: #7957c0;
            font-size: 10px;
            letter-spacing: 2px;
            margin-bottom: 18px
        }

        .label:before {
            content: '';
            width: 6px;
            height: 6px;
            border-radius: 50%;
            background: #47af8c;
            box-shadow: 0 0 0 4px #47af8c12
        }

        h1 {
            margin: 0;
            font-size: clamp(27px, 3.6vw, 40px);
            font-weight: 650;
            line-height: 1.5;
            letter-spacing: -1px;
            overflow-wrap: anywhere
        }

        .article {
            max-width: 820px;
            margin: 36px auto 58px;
            background: white;
            border: 1px solid #e9e2f1;
            border-radius: 15px;
            overflow: hidden
        }

        .content {
            padding: 36px 42px 42px;
            white-space: pre-wrap;
            overflow-wrap: anywhere;
            font-size: 16px;
            color: #645b72;
            line-height: 2.05
        }

        .article-end {
            padding: 20px 42px;
            border-top: 1px solid #efeaf5;
            display: flex;
            align-items: center;
            justify-content: space-between;
            color: #b1a6bf;
            font-size: 11px;
            background: #fdfcff
        }

        .end {
            display: flex;
            align-items: center;
            gap: 10px;
            letter-spacing: 3px
        }

        .end:before, .end:after {
            content: '';
            height: 1px;
            width: 22px;
            background: #e6dfee
        }

        .article-end a {
            color: #8b6ab9;
            letter-spacing: 0
        }

        .footer {
            border-top: 1px solid #e9e2f1;
            background: white;
            color: #a297af;
            font-size: 11px
        }

        .footer .wrap {
            display: flex;
            align-items: center;
            justify-content: space-between;
            gap: 20px;
            padding: 25px 0
        }

        .footer strong {
            font-size: 14px;
            color: #81718e;
            font-weight: 500
        }

        @media (max-width: 640px) {
            .wrap {
                width: calc(100% - 32px)
            }

            .nav {
                height: 65px
            }

            .brand {
                font-size: 21px
            }

            .nav-label {
                font-size: 10px
            }

            .hero-inner {
                padding: 22px 0 34px
            }

            .breadcrumb {
                margin-bottom: 24px
            }

            .label {
                margin-bottom: 14px
            }

            h1 {
                font-size: 27px
            }

            .article {
                margin: 24px auto 35px;
                border-radius: 12px
            }

            .content {
                padding: 25px 23px 31px;
                font-size: 15px;
                line-height: 2
            }

            .article-end {
                padding: 18px 23px
            }

            .footer .wrap {
                align-items: start;
                flex-direction: column;
                gap: 5px
            }
        }

        a:focus-visible {
            outline: 2px solid #8b65f4;
            outline-offset: 4px
        }
    </style>
</head>
<body id="top">
<div class="topbar">
    <span>保持好奇，持续探索。</span> 发现新知，读懂变化
</div>

<header class="header">
    <div class="wrap nav">
        <div class="brand">
            <span class="mark" aria-hidden="true">知</span>资讯中心
        </div>
        <div class="nav-label">INSIGHT JOURNAL</div>
    </div>
</header>

<main>
    <section class="hero">
        <div class="wrap">
            <div class="hero-inner">
                <div class="breadcrumb">
                    资讯中心<span>/</span>文章详情
                </div>
                <div class="label">INSIGHTS / 资讯与观察</div>
                <h1><?= e($title) ?></h1>
            </div>
        </div>
    </section>

    <div class="wrap">
        <article class="article">
            <div class="content"><?= $body ?></div>
            <div class="article-end">
                <span class="end">全文完</span>
                <a href="#top">返回顶部 ↑</a>
            </div>
        </article>
    </div>
</main>

<footer class="footer">
    <div class="wrap">
        <strong>资讯中心</strong>
        <span>发现新知，读懂变化。</span>
    </div>
</footer>
</body>
</html>