forked from laysense/laysite
Prepare LaySite 1.4.4 editor templates and MCP settings
This commit is contained in:
@@ -12,6 +12,8 @@ final class BlockTemplates
|
||||
|
||||
public const CATALOG_FEED = '{"categories":["catalog"],"mode":"catalog","limit":30}';
|
||||
|
||||
public const DESIGN_STUDIO_IMAGE = '/assets/system/design-studio.webp';
|
||||
|
||||
public const ELEMENT_GROUPS = [
|
||||
'text-elements' => ['文字与列表', [
|
||||
'paragraph' => ['段落', '<p>填写正文。</p>'],
|
||||
@@ -88,6 +90,7 @@ final class BlockTemplates
|
||||
'quote' => ['引文', '<blockquote><p>在这里填写引用或用户反馈。</p></blockquote>'],
|
||||
'metrics' => ['关键数据', '<dl class="studio-metrics"><div><dt>累计项目</dt><dd>100</dd></div><div><dt>服务覆盖</dt><dd>12</dd></div><div><dt>合作伙伴</dt><dd>8</dd></div></dl>'],
|
||||
'testimonials' => ['用户评价', '<div class="studio-two-column studio-testimonials"><blockquote><p>填写用户的评价,并说明他们获得的结果。</p><p class="studio-attribution">用户姓名 · 公司或角色</p></blockquote><blockquote><p>填写另一条用户评价。</p><p class="studio-attribution">用户姓名 · 公司或角色</p></blockquote></div>'],
|
||||
'plan-comparison' => ['两种方案对比', '<div class="studio-two-column studio-comparison"><article class="content-card"><p class="overline">适合个人</p><h3>轻量方案</h3><ul><li>核心功能</li><li>按需使用</li></ul></article><article class="content-card"><p class="overline">适合团队</p><h3>协作方案</h3><ul><li>共享工作空间</li><li>团队支持</li></ul></article></div>'],
|
||||
]],
|
||||
'details' => ['表格与问答', [
|
||||
'table' => ['对比表格', '<table><thead><tr><th scope="col">项目</th><th scope="col">说明</th></tr></thead><tbody><tr><td>项目一</td><td>填写说明</td></tr><tr><td>项目二</td><td>填写说明</td></tr></tbody></table>'],
|
||||
@@ -96,6 +99,7 @@ final class BlockTemplates
|
||||
'checklist' => ['清单', '<ul class="studio-checklist"><li>填写必备材料</li><li>填写提交条件</li><li>填写确认事项</li></ul>'],
|
||||
'changelog' => ['更新记录', '<div class="studio-releases"><article><h3>版本 2.0</h3><p>填写主要功能或改进。</p></article><article><h3>版本 1.0</h3><p>填写上一版本的更新内容。</p></article></div>'],
|
||||
'pricing' => ['双方案报价', '<div class="card-grid price-grid studio-pricing"><article class="content-card price-card"><h3>基础方案</h3><p class="price-value">填写价格</p><p>填写方案包含的服务。</p><a class="button button-outline" href="/#contact">咨询基础方案</a></article><article class="content-card price-card"><h3>进阶方案</h3><p class="price-value">填写价格</p><p>填写扩展服务或权益。</p><a class="button button-primary" href="/#contact">咨询进阶方案</a></article></div>'],
|
||||
'reference-code' => ['说明与代码示例', '<div class="studio-split"><div><p class="overline">操作示例</p><h3>运行一次检查</h3><p>说明命令的适用环境和执行后的结果。</p></div><pre class="studio-code"><code>curl -I https://example.com</code></pre></div>'],
|
||||
]],
|
||||
'actions' => ['导航与引导', [
|
||||
'service-catalog' => ['目录卡片', '', '', self::CATALOG_FEED],
|
||||
@@ -103,11 +107,13 @@ final class BlockTemplates
|
||||
'cta' => ['行动按钮', '<p>说明下一步。</p><p><a class="button button-primary" href="#contact">联系站点</a></p>'],
|
||||
'resources' => ['资料与链接', '<ul class="studio-resources"><li><a href="/articles">浏览资料</a><span>应用下载</span></li><li><a href="/articles">浏览动态与文档</a><span>网站资源</span></li></ul>'],
|
||||
'contact-card' => ['联系方式', '<div class="studio-contact"><div><h3>联系团队</h3><p>填写咨询范围与回复时间。</p></div><dl class="studio-definitions"><dt>电子邮件</dt><dd><a href="mailto:hello@example.com">hello@example.com</a></dd><dt>服务时间</dt><dd>填写工作时间</dd></dl></div>'],
|
||||
'support-channels' => ['支持渠道', '<div class="studio-two-column"><article class="content-card"><h3>查找自助资料</h3><p>按问题类型查阅文档与最新公告。</p><a href="/articles">查看帮助文章</a></article><article class="content-card"><h3>联系支持团队</h3><p>请注明当前情况、操作步骤及希望得到的帮助。</p><a href="mailto:hello@example.com">发送邮件</a></article></div>'],
|
||||
]],
|
||||
'events' => ['活动与场地', [
|
||||
'event-agenda' => ['当日议程', '<ol class="studio-agenda"><li><p>09:30</p><div><h3>签到与开场</h3><p>开放入口并介绍当天安排。</p></div><span>主会场</span></li><li><p>10:30</p><div><h3>专题工作坊</h3><p>跟随主持人完成一次实践。</p></div><span>工作区</span></li><li><p>14:00</p><div><h3>圆桌交流</h3><p>分享实践经验并回答问题。</p></div><span>交流区</span></li></ol>'],
|
||||
'speaker-lineup' => ['嘉宾阵容', '<div class="card-grid studio-speakers"><article class="content-card"><p class="overline">主题分享</p><h3>嘉宾姓名</h3><p>填写嘉宾的角色和主题。</p></article><article class="content-card"><p class="overline">工作坊</p><h3>主持人姓名</h3><p>介绍实践主题和适合的听众。</p></article><article class="content-card"><p class="overline">圆桌交流</p><h3>主持人姓名</h3><p>介绍本场将讨论的议题。</p></article></div>'],
|
||||
'venue-guide' => ['到场指南', '<div class="studio-two-column"><article class="content-card"><h3>开放与入场</h3><p>填写开放时间、预约方式和入口位置。</p></article><article class="content-card"><h3>交通与无障碍</h3><p>填写推荐交通方式和无障碍通道位置。</p></article></div>'],
|
||||
'event-registration' => ['活动报名', '<div class="studio-banner studio-banner-two"><div><p class="overline">参加活动</p><h3>预留你的席位</h3><p>报名时请附上姓名和到场人数。</p></div><a class="button button-primary" href="mailto:hello@example.com">邮件报名</a></div>'],
|
||||
]],
|
||||
'commerce' => ['商品与服务', [
|
||||
'product-spotlight' => ['商品展示', '<div class="studio-split studio-product"><img src="/assets/system/sample.webp" alt="商品展示示例" loading="lazy"><div><p class="overline">系列名称</p><h3>填写商品名称</h3><p>说明规格、适用场景与供货信息。</p><a class="button button-primary" href="#contact">联系订购</a></div></div>'],
|
||||
@@ -123,8 +129,48 @@ final class BlockTemplates
|
||||
public static function groups(): array
|
||||
{
|
||||
static $groups;
|
||||
if ($groups === null) {
|
||||
$groups = self::GROUPS + LayoutPage::groups();
|
||||
foreach (self::splitLayouts() as $group => $layouts) {
|
||||
$groups[$group][1] += $layouts;
|
||||
}
|
||||
}
|
||||
|
||||
return $groups ??= self::GROUPS + LayoutPage::groups();
|
||||
return $groups;
|
||||
}
|
||||
|
||||
private static function splitLayouts(): array
|
||||
{
|
||||
$sample = '<img src="/assets/system/sample.webp" alt="示例界面" loading="lazy">';
|
||||
$photo = '<img src="' . self::DESIGN_STUDIO_IMAGE . '" alt="工作台上的陶器、笔记本与亚麻布" loading="lazy">';
|
||||
$split = static fn (string $class, string $first, string $second): string => '<div class="studio-split ' . $class . '">' . $first . $second . '</div>';
|
||||
$intro = '<div><h3>看见使用场景</h3><p>用图片展示工作场景,并解释访客能在其中完成什么。</p></div>';
|
||||
$cover = '<div><p class="overline">工作室</p><h3>把想法做成可以使用的作品</h3><p>介绍你的团队、服务以及下一步。</p><a class="button button-primary" href="/articles">探索作品</a></div>';
|
||||
$guide = '<div><h3>准备开始</h3><p>在图片旁说明操作条件和完成目标。</p><ul class="studio-checklist"><li>准备工作环境</li><li>打开操作入口</li><li>确认初次运行结果</li></ul></div>';
|
||||
$story = '<div><p class="overline">幕后故事</p><h3>从材料开始做决定</h3><p>把一个细节讲清楚,让读者了解作品的背景和过程。</p><a href="/articles">阅读完整故事</a></div>';
|
||||
$captioned = '<figure class="studio-captioned">' . $photo . '<figcaption>设计工作台与选材样本</figcaption></figure>';
|
||||
$features = '<div><h3>从日常使用开始</h3><p>说明这项功能在真实工作流程里怎么使用。</p></div>';
|
||||
$video = '<video controls preload="none" poster="/assets/system/sample.webp"></video>';
|
||||
|
||||
return [
|
||||
'cover' => [
|
||||
'cover-photo-left' => ['大图封面 · 图左', $split('studio-split-cover studio-split-media-left', $photo, $cover)],
|
||||
'cover-photo-right' => ['大图封面 · 图右', $split('studio-split-cover studio-split-media-right', $cover, $photo)],
|
||||
],
|
||||
'layout' => [
|
||||
'split-balanced-left' => ['均分图文 · 图左', $split('studio-split-balanced', $sample, $intro)],
|
||||
'split-balanced-right' => ['均分图文 · 图右', $split('studio-split-balanced', $intro, $sample)],
|
||||
'split-guide-left' => ['大图与清单 · 图左', $split('studio-split-guide studio-split-media-left', $sample, $guide)],
|
||||
'split-guide-right' => ['大图与清单 · 图右', $split('studio-split-guide studio-split-media-right', $guide, $sample)],
|
||||
'split-story-left' => ['故事与图注 · 图左', $split('studio-split-story', $captioned, $story)],
|
||||
'split-story-right' => ['故事与图注 · 图右', $split('studio-split-story', $story, $captioned)],
|
||||
'split-alternating' => ['双行交错图文', '<div class="studio-alternating">' . $split('studio-split-balanced', $sample, $features)
|
||||
. $split('studio-split-balanced', $story, $photo) . '</div>'],
|
||||
],
|
||||
'media' => [
|
||||
'video-demo-right' => ['视频演示 · 视频右', $split('studio-split-balanced', '<div><h3>观看演示</h3><p>说明视频包含的功能和观看顺序。</p></div>', $video)],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public static function find(string $key): ?array
|
||||
|
||||
@@ -11,7 +11,7 @@ use Throwable;
|
||||
|
||||
final class Database
|
||||
{
|
||||
private const SCHEMA_VERSION = 6;
|
||||
private const SCHEMA_VERSION = 7;
|
||||
// Avoid reusing a deleted ID that may still be open in an editing form.
|
||||
public const NEXT_ARTICLE_ID = 'MAX(COALESCE((SELECT MAX(id) FROM articles), 0),
|
||||
COALESCE((SELECT MAX(article_id) FROM article_deletions), 0)) + 1';
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Content;
|
||||
|
||||
use App\Config\Environment;
|
||||
use PDO;
|
||||
use RuntimeException;
|
||||
|
||||
final class McpSettings
|
||||
{
|
||||
private const CONTEXT = 'laysite-mcp';
|
||||
public const PATH = '/mcp';
|
||||
|
||||
public static function read(): array
|
||||
{
|
||||
$row = self::stored(Database::connect());
|
||||
if ($row === null) {
|
||||
$token = self::legacyToken();
|
||||
|
||||
return ['enabled' => $token !== null, 'version' => 0, 'token' => $token, 'legacy' => $token !== null];
|
||||
}
|
||||
|
||||
$enabled = (bool) $row['enabled'];
|
||||
$token = null;
|
||||
if ($enabled && $row['encrypted_secret'] !== null) {
|
||||
try {
|
||||
$token = SiteSecrets::decrypt($row['encrypted_secret'], self::CONTEXT);
|
||||
} catch (RuntimeException) {
|
||||
// Keep the settings accessible so an administrator can replace an unreadable key.
|
||||
}
|
||||
}
|
||||
|
||||
return ['enabled' => $enabled, 'version' => (int) $row['version'], 'token' => $token, 'legacy' => false];
|
||||
}
|
||||
|
||||
public static function save(bool $enabled, bool $rotate, int $version): void
|
||||
{
|
||||
if ($rotate && !$enabled) {
|
||||
throw new RuntimeException('启用 MCP 后才能生成新密钥。');
|
||||
}
|
||||
Database::write(static function (PDO $pdo) use ($enabled, $rotate, $version): void {
|
||||
$row = self::stored($pdo);
|
||||
if ($version !== (int) ($row['version'] ?? 0)) {
|
||||
throw new RuntimeException('MCP 设置已更新,请刷新后再保存。');
|
||||
}
|
||||
$encrypted = $row['encrypted_secret'] ?? null;
|
||||
$legacy = $row === null ? self::legacyToken() : null;
|
||||
if ($rotate || ($enabled && $encrypted === null && $legacy === null)) {
|
||||
$encrypted = SiteSecrets::encrypt(bin2hex(random_bytes(32)), self::CONTEXT);
|
||||
} elseif ($legacy !== null) {
|
||||
$encrypted = SiteSecrets::encrypt($legacy, self::CONTEXT);
|
||||
}
|
||||
$pdo->prepare('INSERT INTO mcp_settings (id, enabled, encrypted_secret, version) VALUES (1, ?, ?, 1)
|
||||
ON CONFLICT (id) DO UPDATE SET enabled = excluded.enabled, encrypted_secret = excluded.encrypted_secret,
|
||||
version = mcp_settings.version + 1')->execute([(int) $enabled, $encrypted]);
|
||||
});
|
||||
}
|
||||
|
||||
private static function stored(PDO $pdo): ?array
|
||||
{
|
||||
return $pdo->query('SELECT enabled, encrypted_secret, version FROM mcp_settings WHERE id = 1')->fetch() ?: null;
|
||||
}
|
||||
|
||||
private static function legacyToken(): ?string
|
||||
{
|
||||
$value = Environment::get('LAYSITE_MCP_TOKEN');
|
||||
|
||||
return is_string($value) && strlen($value) >= 32 ? $value : null;
|
||||
}
|
||||
}
|
||||
@@ -10,26 +10,16 @@ use RuntimeException;
|
||||
|
||||
final class OAuthCredentials
|
||||
{
|
||||
private const CIPHER = 'aes-256-gcm';
|
||||
private const KEY_BYTES = 32;
|
||||
private const NONCE_BYTES = 12;
|
||||
private const TAG_BYTES = 16;
|
||||
|
||||
public static function store(PDO $pdo, int $layout, string $secret): void
|
||||
{
|
||||
if ($secret === '' || strlen($secret) > 4096 || str_contains($secret, "\0")) {
|
||||
throw new DesignFieldError('provider_secret', '客户端密钥为空或格式无效。');
|
||||
}
|
||||
$revision = bin2hex(random_bytes(16));
|
||||
$nonce = random_bytes(self::NONCE_BYTES);
|
||||
$ciphertext = openssl_encrypt($secret, self::CIPHER, self::key(create: true), OPENSSL_RAW_DATA, $nonce,
|
||||
$tag, self::context($layout, $revision), self::TAG_BYTES);
|
||||
if ($ciphertext === false) {
|
||||
throw new RuntimeException('无法加密客户端密钥。');
|
||||
}
|
||||
$encrypted = SiteSecrets::encrypt($secret, self::context($layout, $revision));
|
||||
$pdo->prepare('INSERT INTO layout_oauth_credentials (layout_id, revision, encrypted_secret) VALUES (?, ?, ?)
|
||||
ON CONFLICT (layout_id) DO UPDATE SET revision = excluded.revision, encrypted_secret = excluded.encrypted_secret')
|
||||
->execute([$layout, $revision, base64_encode($nonce . $tag . $ciphertext)]);
|
||||
->execute([$layout, $revision, $encrypted]);
|
||||
}
|
||||
|
||||
public static function read(int $layout, string $revision): string
|
||||
@@ -37,18 +27,11 @@ final class OAuthCredentials
|
||||
$query = Database::connect()->prepare('SELECT encrypted_secret FROM layout_oauth_credentials WHERE layout_id = ? AND revision = ?');
|
||||
$query->execute([$layout, $revision]);
|
||||
$value = $query->fetchColumn();
|
||||
$encrypted = is_string($value) ? base64_decode($value, true) : false;
|
||||
if ($encrypted === false || strlen($encrypted) <= self::NONCE_BYTES + self::TAG_BYTES) {
|
||||
if (!is_string($value)) {
|
||||
throw new RuntimeException('客户端密钥尚未配置或已更新。');
|
||||
}
|
||||
$secret = openssl_decrypt(substr($encrypted, self::NONCE_BYTES + self::TAG_BYTES), self::CIPHER, self::key(),
|
||||
OPENSSL_RAW_DATA, substr($encrypted, 0, self::NONCE_BYTES), substr($encrypted, self::NONCE_BYTES, self::TAG_BYTES),
|
||||
self::context($layout, $revision));
|
||||
if ($secret === false) {
|
||||
throw new RuntimeException('无法解密客户端密钥。');
|
||||
}
|
||||
|
||||
return $secret;
|
||||
return SiteSecrets::decrypt($value, self::context($layout, $revision));
|
||||
}
|
||||
|
||||
public static function migrateLegacy(PDO $pdo): void
|
||||
@@ -76,41 +59,4 @@ final class OAuthCredentials
|
||||
{
|
||||
return 'laysite-oauth:' . $layout . ':' . $revision;
|
||||
}
|
||||
|
||||
private static function key(bool $create = false): string
|
||||
{
|
||||
$path = Database::path() . '.key';
|
||||
if (is_link($path)) {
|
||||
throw new RuntimeException('密钥文件不能使用符号链接。');
|
||||
}
|
||||
$mask = umask(0077);
|
||||
try {
|
||||
$file = is_file($path) || $create ? @fopen($path, $create ? 'c+b' : 'rb') : false;
|
||||
} finally {
|
||||
umask($mask);
|
||||
}
|
||||
if ($file === false) {
|
||||
throw new RuntimeException('无法打开站点密钥文件。');
|
||||
}
|
||||
try {
|
||||
if (!flock($file, $create ? LOCK_EX : LOCK_SH) || (fstat($file)['mode'] & 0077) !== 0) {
|
||||
throw new RuntimeException('站点密钥文件需能上锁,并且只能由运行用户访问。');
|
||||
}
|
||||
$key = stream_get_contents($file, self::KEY_BYTES + 1);
|
||||
if ($create && $key === '') {
|
||||
$key = random_bytes(self::KEY_BYTES);
|
||||
if (fwrite($file, $key) !== self::KEY_BYTES || !fflush($file)) {
|
||||
ftruncate($file, 0);
|
||||
throw new RuntimeException('无法写入站点密钥文件。');
|
||||
}
|
||||
}
|
||||
if (!is_string($key) || strlen($key) !== self::KEY_BYTES) {
|
||||
throw new RuntimeException('站点密钥文件格式无效。');
|
||||
}
|
||||
|
||||
return $key;
|
||||
} finally {
|
||||
fclose($file);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Content;
|
||||
|
||||
use RuntimeException;
|
||||
|
||||
final class SiteSecrets
|
||||
{
|
||||
private const CIPHER = 'aes-256-gcm';
|
||||
private const KEY_BYTES = 32;
|
||||
private const NONCE_BYTES = 12;
|
||||
private const TAG_BYTES = 16;
|
||||
|
||||
public static function encrypt(string $secret, string $context): string
|
||||
{
|
||||
$nonce = random_bytes(self::NONCE_BYTES);
|
||||
$ciphertext = openssl_encrypt($secret, self::CIPHER, self::key(create: true), OPENSSL_RAW_DATA, $nonce,
|
||||
$tag, $context, self::TAG_BYTES);
|
||||
if ($ciphertext === false) {
|
||||
throw new RuntimeException('无法加密站点密钥。');
|
||||
}
|
||||
|
||||
return base64_encode($nonce . $tag . $ciphertext);
|
||||
}
|
||||
|
||||
public static function decrypt(string $value, string $context): string
|
||||
{
|
||||
$encrypted = base64_decode($value, true);
|
||||
if ($encrypted === false || strlen($encrypted) <= self::NONCE_BYTES + self::TAG_BYTES) {
|
||||
throw new RuntimeException('站点密钥格式无效。');
|
||||
}
|
||||
$secret = openssl_decrypt(substr($encrypted, self::NONCE_BYTES + self::TAG_BYTES), self::CIPHER, self::key(),
|
||||
OPENSSL_RAW_DATA, substr($encrypted, 0, self::NONCE_BYTES), substr($encrypted, self::NONCE_BYTES, self::TAG_BYTES),
|
||||
$context);
|
||||
if ($secret === false) {
|
||||
throw new RuntimeException('无法解密站点密钥。');
|
||||
}
|
||||
|
||||
return $secret;
|
||||
}
|
||||
|
||||
private static function key(bool $create = false): string
|
||||
{
|
||||
$path = Database::path() . '.key';
|
||||
if (is_link($path)) {
|
||||
throw new RuntimeException('密钥文件不能使用符号链接。');
|
||||
}
|
||||
$mask = umask(0077);
|
||||
try {
|
||||
$file = is_file($path) || $create ? @fopen($path, $create ? 'c+b' : 'rb') : false;
|
||||
} finally {
|
||||
umask($mask);
|
||||
}
|
||||
if ($file === false) {
|
||||
throw new RuntimeException('无法打开站点密钥文件。');
|
||||
}
|
||||
try {
|
||||
if (!flock($file, $create ? LOCK_EX : LOCK_SH) || (fstat($file)['mode'] & 0077) !== 0) {
|
||||
throw new RuntimeException('站点密钥文件需能上锁,并且只能由运行用户访问。');
|
||||
}
|
||||
$key = stream_get_contents($file, self::KEY_BYTES + 1);
|
||||
if ($create && $key === '') {
|
||||
$key = random_bytes(self::KEY_BYTES);
|
||||
if (fwrite($file, $key) !== self::KEY_BYTES || !fflush($file)) {
|
||||
ftruncate($file, 0);
|
||||
throw new RuntimeException('无法写入站点密钥文件。');
|
||||
}
|
||||
}
|
||||
if (!is_string($key) || strlen($key) !== self::KEY_BYTES) {
|
||||
throw new RuntimeException('站点密钥文件格式无效。');
|
||||
}
|
||||
|
||||
return $key;
|
||||
} finally {
|
||||
fclose($file);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user