'服务条款', 'privacy' => '隐私政策', ]; private const FALLBACK_LOCALES = [ 'zh-CN' => '简体中文', 'en' => 'English', ]; private const ALLOWED_TAGS = [ 'p', 'div', 'h2', 'h3', 'ul', 'ol', 'li', 'strong', 'em', 'blockquote', 'br', ]; private const DROP_WITH_CONTENT = [ 'script', 'style', 'iframe', 'object', 'embed', 'form', 'input', 'button', 'textarea', 'select', 'option', 'link', 'meta', 'base', 'svg', 'math', 'template', ]; private const MAX_TITLE_LENGTH = 120; private const MAX_CONTENT_LENGTH = 100000; public static function published(string $slug, string $locale, string $siteName): array { $slug = self::slug($slug); $locale = self::locale($locale, false); try { if (self::hasSchema()) { $document = LegalDocument::query() ->where('slug', $slug)->where('locale', $locale)->first(); if ($document !== null && (int) $document->published_revision_id > 0) { $revision = LegalDocumentRevision::query() ->where('id', (int) $document->published_revision_id) ->where('document_id', (int) $document->id)->first(); if ($revision !== null) { return self::publicDto( $slug, $locale, (string) $revision->title, (string) $revision->content_html, (string) $revision->effective_date, (int) $revision->version, (string) $revision->published_at, $siteName, false ); } } } } catch (\Throwable $exception) { // Public legal pages stay available while an existing installation // is applying the additive schema. Administrator writes still fail // closed with an explicit migration error in editorState(). if (!self::$fallbackErrorLogged) { self::$fallbackErrorLogged = true; error_log('Legal document fallback: ' . get_class($exception)); } } $default = self::defaults($slug, $locale); return self::publicDto( $slug, $locale, $default['title'], $default['content_html'], $default['effective_date'], 1, '', $siteName, true ); } public static function editorState(array $actor, string $slug, string $locale): array { self::authorize($actor); $slug = self::slug($slug); $locale = self::locale($locale); $locales = self::locales(); self::assertSchema(); $document = LegalDocument::query() ->where('slug', $slug)->where('locale', $locale)->first(); $default = self::defaults($slug, $locale); $published = null; $history = []; if ($document !== null) { if ((int) $document->published_revision_id > 0) { $published = LegalDocumentRevision::query() ->where('id', (int) $document->published_revision_id) ->where('document_id', (int) $document->id)->first(); } $history = LegalDocumentRevision::query() ->where('document_id', (int) $document->id) ->orderByDesc('version')->limit(20)->get()->map( static fn ($revision): array => self::revisionDto($revision) )->all(); } return [ 'slug' => $slug, 'slug_label' => self::SLUGS[$slug], 'locale' => $locale, 'locale_label' => $locales[$locale] ?? $locale, 'title' => $document === null ? $default['title'] : (string) $document->title, 'content_html' => self::sanitizeHtml( $document === null ? $default['content_html'] : (string) $document->draft_html ), 'effective_date' => $document === null ? $default['effective_date'] : (string) $document->effective_date, 'draft_version' => $document === null ? 0 : (int) $document->draft_version, 'updated_at' => $document === null ? null : (string) $document->updated_at, 'updated_by' => $document === null ? null : (int) $document->updated_by, 'published' => $published === null ? null : self::revisionDto($published), 'history' => $history, 'documents' => self::SLUGS, 'locales' => $locales, ]; } public static function saveDraft(array $actor, string $slug, array $input): array { self::authorize($actor, true); $slug = self::slug($slug); $values = self::validatedInput($input); self::assertSchema(); (new LegalDocument())->getConnection()->transaction( static function () use ($actor, $slug, $values): void { self::writeDraft($actor, $slug, $values); } ); return self::editorState($actor, $slug, $values['locale']); } public static function publish(array $actor, string $slug, array $input): array { self::authorize($actor, true); $slug = self::slug($slug); $values = self::validatedInput($input); self::assertSchema(); (new LegalDocument())->getConnection()->transaction( static function () use ($actor, $slug, $values): void { $document = self::writeDraft($actor, $slug, $values); $lastVersion = (int) LegalDocumentRevision::query() ->where('document_id', (int) $document->id)->lockForUpdate()->max('version'); $now = date('Y-m-d H:i:s'); $revision = new LegalDocumentRevision(); $revision->document_id = (int) $document->id; $revision->version = $lastVersion + 1; $revision->title = (string) $document->title; $revision->content_html = (string) $document->draft_html; $revision->effective_date = (string) $document->effective_date; $revision->published_at = $now; $revision->published_by = (int) $actor['user_id']; $revision->save(); $document->published_revision_id = (int) $revision->id; $document->updated_at = $now; $document->updated_by = (int) $actor['user_id']; $document->save(); } ); return self::editorState($actor, $slug, $values['locale']); } public static function preview(array $actor, string $slug, array $input, string $siteName): array { self::authorize($actor); $slug = self::slug($slug); $values = self::validatedInput($input, false); return self::publicDto( $slug, $values['locale'], $values['title'], $values['content_html'], $values['effective_date'], 0, '', $siteName, true ); } private static function writeDraft(array $actor, string $slug, array $values): LegalDocument { $document = LegalDocument::query()->where('slug', $slug) ->where('locale', $values['locale'])->lockForUpdate()->first(); $actualVersion = $document === null ? 0 : (int) $document->draft_version; if ($actualVersion !== $values['draft_version']) { throw new CloudError('内容已被其他管理员更新,请重新加载后再编辑', 409); } $now = date('Y-m-d H:i:s'); if ($document === null) { $document = new LegalDocument(); $document->slug = $slug; $document->locale = $values['locale']; $document->published_revision_id = null; $document->created_at = $now; } $document->title = $values['title']; $document->draft_html = $values['content_html']; $document->effective_date = $values['effective_date']; $document->draft_version = $actualVersion + 1; $document->updated_at = $now; $document->updated_by = (int) $actor['user_id']; $document->save(); return $document; } private static function publicDto( string $slug, string $locale, string $title, string $html, string $effectiveDate, int $version, string $publishedAt, string $siteName, bool $fallback ): array { $html = self::sanitizeHtml($html); $safeSiteName = htmlspecialchars(trim($siteName), ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'); if ($safeSiteName === '') { $safeSiteName = '本站'; } $title = str_replace('{{site_name}}', html_entity_decode($safeSiteName, ENT_QUOTES, 'UTF-8'), $title); $html = str_replace('{{site_name}}', $safeSiteName, $html); [$html, $outline] = self::decorateHeadings($html); return [ 'slug' => $slug, 'locale' => $locale, 'title' => $title, 'content_html' => $html, 'effective_date' => $effectiveDate, 'version' => $version, 'published_at' => $publishedAt === '' ? null : $publishedAt, 'fallback' => $fallback, 'outline' => $outline, ]; } private static function validatedInput(array $input, bool $withVersion = true): array { $locale = self::locale($input['locale'] ?? null); $title = self::plainText($input['title'] ?? null, '标题', self::MAX_TITLE_LENGTH); $content = $input['content_html'] ?? null; if (!is_string($content) || !mb_check_encoding($content, 'UTF-8') || mb_strlen($content, 'UTF-8') > self::MAX_CONTENT_LENGTH) { throw new CloudError('正文格式不正确或超过 100000 个字符', 400); } $content = self::sanitizeHtml($content); if (trim(strip_tags($content)) === '') { throw new CloudError('正文不能为空', 400); } $effectiveDate = self::date($input['effective_date'] ?? null); $version = 0; if ($withVersion) { $candidate = $input['draft_version'] ?? null; if ((!is_int($candidate) && !is_string($candidate)) || !preg_match('/^[0-9]{1,10}$/D', (string) $candidate)) { throw new CloudError('草稿版本不正确,请重新加载页面', 400); } $version = (int) $candidate; } return [ 'locale' => $locale, 'title' => $title, 'content_html' => $content, 'effective_date' => $effectiveDate, 'draft_version' => $version, ]; } private static function sanitizeHtml(string $html): string { $html = (new AntiXSS())->xss_clean($html); [$dom, $root] = self::fragment($html); $xpath = new DOMXPath($dom); $nodes = []; foreach ($xpath->query('//*') ?: [] as $node) { if ($node instanceof DOMElement && $node !== $root) { $nodes[] = $node; } } foreach ($nodes as $node) { if ($node->parentNode === null) { continue; } $tag = strtolower($node->tagName); if (in_array($tag, self::DROP_WITH_CONTENT, true)) { $node->parentNode->removeChild($node); continue; } if (!in_array($tag, self::ALLOWED_TAGS, true)) { self::unwrap($node); continue; } while ($node->attributes !== null && $node->attributes->length > 0) { $node->removeAttributeNode($node->attributes->item(0)); } } foreach ($xpath->query('//comment()|//processing-instruction()') ?: [] as $node) { if ($node->parentNode !== null) { $node->parentNode->removeChild($node); } } return trim(self::innerHtml($root)); } private static function decorateHeadings(string $html): array { [$dom, $root] = self::fragment($html); $xpath = new DOMXPath($dom); $outline = []; $index = 0; foreach ($xpath->query('.//h2|.//h3', $root) ?: [] as $heading) { if (!$heading instanceof DOMElement) { continue; } $label = trim(preg_replace('/\s+/u', ' ', (string) $heading->textContent)); if ($label === '') { continue; } $index++; $id = 'legal-section-' . $index; $heading->setAttribute('id', $id); $outline[] = ['id' => $id, 'title' => $label, 'level' => strtolower($heading->tagName)]; } return [trim(self::innerHtml($root)), $outline]; } private static function fragment(string $html): array { $dom = new DOMDocument('1.0', 'UTF-8'); $previous = libxml_use_internal_errors(true); $dom->loadHTML( '', LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD ); libxml_clear_errors(); libxml_use_internal_errors($previous); $root = (new DOMXPath($dom))->query('//*[@id="legal-document-root"]')->item(0); if (!$root instanceof DOMElement) { throw new CloudError('正文无法解析', 400); } return [$dom, $root]; } private static function unwrap(DOMNode $node): void { $parent = $node->parentNode; if ($parent === null) { return; } while ($node->firstChild !== null) { $parent->insertBefore($node->firstChild, $node); } $parent->removeChild($node); } private static function innerHtml(DOMElement $root): string { $html = ''; foreach ($root->childNodes as $child) { $html .= $root->ownerDocument->saveHTML($child); } return $html; } private static function revisionDto($revision): array { return [ 'id' => (int) $revision->id, 'version' => (int) $revision->version, 'title' => (string) $revision->title, 'effective_date' => (string) $revision->effective_date, 'published_at' => (string) $revision->published_at, 'published_by' => (int) $revision->published_by, ]; } private static function authorize(array $actor, bool $write = false): void { if ((int) ($actor['user_id'] ?? 0) < 1) { throw new CloudError('请先登录', 401); } if (!($actor['is_admin'] ?? false)) { throw new CloudError('需要管理员权限', 403); } if ($write && (!($actor['is_super_admin'] ?? false) || !($actor['can_manage'] ?? false))) { throw new CloudError('只有超级管理员可以编辑和发布法律页面', 403); } } private static function assertSchema(): void { if (!self::hasSchema()) { throw new CloudError('法律页面数据表尚未安装,请先导入 web/sql/legal_documents.sql', 503); } } private static function hasSchema(): bool { $schema = (new LegalDocument())->getConnection()->getSchemaBuilder(); return $schema->hasTable('legal_documents') && $schema->hasTable('legal_document_revisions'); } private static function slug($value): string { if (!is_string($value) || !array_key_exists($value, self::SLUGS)) { throw new CloudError('法律页面类型不正确', 400); } return $value; } private static function locale($value, bool $requireRegistered = true): string { $locale = PublicLocale::canonicalLocale($value); if ($locale === null || ($requireRegistered && !array_key_exists($locale, self::locales()))) { throw new CloudError('页面语言不正确', 400); } return $locale; } /** @return array */ private static function locales(): array { try { $connection = (new LegalDocument())->getConnection(); $schema = $connection->getSchemaBuilder(); if (!$schema->hasTable('ui_copy_locales')) { return self::FALLBACK_LOCALES; } $locales = ['zh-CN' => '简体中文']; foreach ($connection->table('ui_copy_locales') ->where('enabled', 1) ->orderByDesc('source_claim') ->orderBy('locale') ->get(['locale', 'display_name'])->all() as $row) { $locale = PublicLocale::canonicalLocale( (string) $row->locale ); $displayName = trim((string) $row->display_name); if ($locale === null || $displayName === '' || !mb_check_encoding($displayName, 'UTF-8') || mb_strlen($displayName, 'UTF-8') > 128 || preg_match('/[\x00-\x1F\x7F]/u', $displayName) === 1) { continue; } $locales[$locale] = $displayName; } return $locales; } catch (\Throwable) { return self::FALLBACK_LOCALES; } } private static function plainText($value, string $label, int $limit): string { if (!is_string($value) || !mb_check_encoding($value, 'UTF-8') || preg_match('/[\x00-\x1f\x7f]/', $value)) { throw new CloudError($label . '格式不正确', 400); } $value = trim(strip_tags($value)); if ($value === '' || mb_strlen($value, 'UTF-8') > $limit) { throw new CloudError($label . '不能为空且最多 ' . $limit . ' 个字符', 400); } return $value; } private static function date($value): string { if (!is_string($value) || !preg_match('/^\d{4}-\d{2}-\d{2}$/D', $value)) { throw new CloudError('生效日期格式不正确', 400); } $date = \DateTimeImmutable::createFromFormat('!Y-m-d', $value); if ($date === false || $date->format('Y-m-d') !== $value) { throw new CloudError('生效日期不存在', 400); } return $value; } private static function defaults(string $slug, string $locale): array { // Target-language legal copy is administrator-owned. Until that // locale has a published revision, use the Chinese safety baseline; // never ship an English translation in source. $content = self::DEFAULT_CONTENT[$slug] ?? ''; return [ 'title' => self::SLUGS[$slug], 'content_html' => $content, 'effective_date' => '2026-10-24', ]; } private const DEFAULT_CONTENT = [ 'terms' => <<<'HTML'

接受与适用范围

当你注册账户、购买或使用 {{site_name}} 提供的云服务器及相关功能时,即表示你已阅读并同意本条款。

账户与安全

你应提供真实、准确的信息,并妥善保管密码、验证码和多因素认证凭据。发现异常登录或凭据泄露时,应立即修改密码并联系支持。

服务、账单与续费

实例以订单展示的配置、计费周期与流量额度交付。购买与续费分别形成对应的预付费服务周期;账单、到期时间、宽限期及资源释放规则,以结算页和控制台中的明确提示为准。

可接受使用规则

不得利用服务实施攻击、欺诈、恶意扫描、垃圾信息传播、侵权分发,或任何违反适用法律的行为。我们可对明显危害平台、网络或第三方安全的资源采取限制措施。

暂停、终止与数据处理

未支付续费账单、严重违规或安全风险可能导致网络暂停或服务终止。服务到期后的资源保留和删除期限,以购买时展示的规则为准;资源被永久删除后通常无法恢复。

可用性与责任边界

我们会采取合理措施维护服务稳定与安全,但互联网、上游网络、硬件故障和不可抗力可能导致中断。除非另有书面约定,服务按现状提供,责任范围受适用法律限制。

条款变更与联系

当产品、法律或运营安排发生变化时,我们可能更新本条款。重要变化将通过站内通知、邮件或页面公告告知。对条款有疑问,可通过控制台工单联系支持。

HTML, 'privacy' => <<<'HTML'

我们收集的信息

包括注册与账户资料、联系方式、订单与支付状态、实例及网络资源标识、登录与安全日志、工单沟通内容,以及你主动提交给我们的其他信息。

我们如何使用信息

用于创建和维护账户、交付云服务、处理账单、保障账户和平台安全、响应支持请求、履行法律义务,以及在获得适当授权时改进产品体验。

支付与敏感凭据

支付通常由受支持的支付服务商处理,我们仅保存完成订单和对账所必需的状态信息。密码应以不可逆方式处理;验证码、密钥和令牌仅在实现相应安全功能所需的范围内使用。

Cookie 与设备信息

我们可能使用必要 Cookie 维持登录状态、保存安全偏好和防止滥用。可选分析功能应在适用地区取得必要同意,并提供清晰的关闭方式。

共享与披露

我们不会出售你的个人信息。仅在交付服务所需、获得你的指示、履行法律义务或保护平台与他人合法权益时,向受约束的服务商或主管机构披露必要信息。

保存与安全

我们根据服务履行、争议处理、安全审计及法律要求确定保存期限,并采取访问控制、加密、日志审计和最小权限等措施降低未授权访问风险。

你的权利

在适用法律允许的范围内,你可以请求访问、更正、导出或删除个人信息,也可以撤回特定同意。部分数据可能因账务、安全或合规义务而需要继续保存。

更新与联系

我们可能随业务和法律要求更新本政策,并在生效前提供合理提示。如有隐私问题或权利请求,请通过控制台工单联系我们。

HTML, ]; }