forked from laysense/laysite
Add page publication history and reusable blocks
This commit is contained in:
@@ -94,7 +94,64 @@ final class LayoutPage
|
||||
return false;
|
||||
}
|
||||
|
||||
public static function saveSettings(PDO $pdo, array $page, array $config): void
|
||||
public static function draftSettings(int $id): ?array
|
||||
{
|
||||
$query = Database::connect()->prepare('SELECT payload, encrypted_secret IS NOT NULL AS secret_pending FROM layout_draft_settings WHERE layout_id = ?');
|
||||
$query->execute([$id]);
|
||||
$row = $query->fetch();
|
||||
|
||||
return $row ? json_decode($row['payload'], true, flags: JSON_THROW_ON_ERROR)
|
||||
+ ['secret_pending' => (bool) $row['secret_pending']] : null;
|
||||
}
|
||||
|
||||
public static function stageSettings(PDO $pdo, array $page, array $config): void
|
||||
{
|
||||
$id = $page['layout_id'];
|
||||
$saved = self::draftSettings($id);
|
||||
$config['provider'] ??= $saved['provider'] ?? null;
|
||||
$encrypt = null;
|
||||
$retain = $config['provider'] !== null && ($config['provider'] === ($saved['provider'] ?? null));
|
||||
if ($config['provider'] !== null) {
|
||||
if (!self::hasMember($page['sections'])) {
|
||||
// A removed navigation no longer needs any pending credentials.
|
||||
$config['provider'] = null;
|
||||
$retain = false;
|
||||
} else {
|
||||
$layout = DesignLibrary::find($id, $page['layout_kind']);
|
||||
$payload = array_replace($layout['payload'], ['provider' => $config['provider']]);
|
||||
DesignLibrary::normalize('header', 'member', $payload);
|
||||
if ($config['secret'] !== '') {
|
||||
if (strlen($config['secret']) > 4096 || str_contains($config['secret'], "\0")) {
|
||||
throw new DesignFieldError('provider_secret', '客户端密钥格式无效。');
|
||||
}
|
||||
$encrypt = SiteSecrets::encrypt($config['secret'], 'laysite-layout-draft:' . $id);
|
||||
}
|
||||
}
|
||||
}
|
||||
unset($config['secret']);
|
||||
$pdo->prepare('INSERT INTO layout_draft_settings (layout_id, payload, encrypted_secret) VALUES (?, ?, ?)
|
||||
ON CONFLICT(layout_id) DO UPDATE SET payload = excluded.payload, encrypted_secret = '
|
||||
. ($retain ? 'COALESCE(excluded.encrypted_secret, layout_draft_settings.encrypted_secret)' : 'excluded.encrypted_secret'))
|
||||
->execute([$id, Database::json($config), $encrypt]);
|
||||
}
|
||||
|
||||
public static function publishSettings(PDO $pdo, array $page): void
|
||||
{
|
||||
$id = $page['layout_id'];
|
||||
$query = $pdo->prepare('SELECT payload, encrypted_secret FROM layout_draft_settings WHERE layout_id = ?');
|
||||
$query->execute([$id]);
|
||||
$row = $query->fetch();
|
||||
if (!$row) {
|
||||
return;
|
||||
}
|
||||
$config = json_decode($row['payload'], true, flags: JSON_THROW_ON_ERROR);
|
||||
$config['secret'] = $row['encrypted_secret'] === null ? ''
|
||||
: SiteSecrets::decrypt($row['encrypted_secret'], 'laysite-layout-draft:' . $id);
|
||||
self::saveSettings($pdo, $page, $config);
|
||||
$pdo->prepare('DELETE FROM layout_draft_settings WHERE layout_id = ?')->execute([$id]);
|
||||
}
|
||||
|
||||
private static function saveSettings(PDO $pdo, array $page, array $config): void
|
||||
{
|
||||
$kind = $page['layout_kind'];
|
||||
$id = $page['layout_id'];
|
||||
|
||||
Reference in New Issue
Block a user