'服务条款', 'privacy' => '隐私政策', ]; private const LOCALES = ['zh-CN' => '简体中文', 'en' => '英语']; 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); 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); 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' => self::LOCALES[$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' => self::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( '
当你注册账户、购买或使用 {{site_name}} 提供的云服务器及相关功能时,即表示你已阅读并同意本条款。
你应提供真实、准确的信息,并妥善保管密码、验证码和多因素认证凭据。发现异常登录或凭据泄露时,应立即修改密码并联系支持。
实例以订单展示的配置、计费周期与流量额度交付。购买与续费分别形成对应的预付费服务周期;账单、到期时间、宽限期及资源释放规则,以结算页和控制台中的明确提示为准。
不得利用服务实施攻击、欺诈、恶意扫描、垃圾信息传播、侵权分发,或任何违反适用法律的行为。我们可对明显危害平台、网络或第三方安全的资源采取限制措施。
未支付续费账单、严重违规或安全风险可能导致网络暂停或服务终止。服务到期后的资源保留和删除期限,以购买时展示的规则为准;资源被永久删除后通常无法恢复。
我们会采取合理措施维护服务稳定与安全,但互联网、上游网络、硬件故障和不可抗力可能导致中断。除非另有书面约定,服务按现状提供,责任范围受适用法律限制。
当产品、法律或运营安排发生变化时,我们可能更新本条款。重要变化将通过站内通知、邮件或页面公告告知。对条款有疑问,可通过控制台工单联系支持。
HTML, 'privacy' => <<<'HTML'包括注册与账户资料、联系方式、订单与支付状态、实例及网络资源标识、登录与安全日志、工单沟通内容,以及你主动提交给我们的其他信息。
用于创建和维护账户、交付云服务、处理账单、保障账户和平台安全、响应支持请求、履行法律义务,以及在获得适当授权时改进产品体验。
支付通常由受支持的支付服务商处理,我们仅保存完成订单和对账所必需的状态信息。密码应以不可逆方式处理;验证码、密钥和令牌仅在实现相应安全功能所需的范围内使用。
我们可能使用必要 Cookie 维持登录状态、保存安全偏好和防止滥用。可选分析功能应在适用地区取得必要同意,并提供清晰的关闭方式。
我们不会出售你的个人信息。仅在交付服务所需、获得你的指示、履行法律义务或保护平台与他人合法权益时,向受约束的服务商或主管机构披露必要信息。
我们根据服务履行、争议处理、安全审计及法律要求确定保存期限,并采取访问控制、加密、日志审计和最小权限等措施降低未授权访问风险。
在适用法律允许的范围内,你可以请求访问、更正、导出或删除个人信息,也可以撤回特定同意。部分数据可能因账务、安全或合规义务而需要继续保存。
我们可能随业务和法律要求更新本政策,并在生效前提供合理提示。如有隐私问题或权利请求,请通过控制台工单联系我们。
HTML, ]; }