Migrate Laysense to upstream LaySite 1.4.3 with external site data
This commit is contained in:
@@ -5,11 +5,20 @@ declare(strict_types=1);
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Content\BlockTemplates;
|
||||
use App\Content\Buttons;
|
||||
use App\Content\CanvasStyle;
|
||||
use App\Content\DesignLibrary;
|
||||
use App\Content\Database;
|
||||
use App\Content\Html;
|
||||
use App\Content\LayoutPage;
|
||||
use App\Content\Licenses;
|
||||
use App\Content\Media;
|
||||
use App\Content\Passwords;
|
||||
use App\Content\Repository;
|
||||
use App\Content\SiteDesign;
|
||||
use App\Content\SystemInfo;
|
||||
use App\Content\UpdateCheckInProgress;
|
||||
use App\Content\Updates;
|
||||
use App\Content\View;
|
||||
use App\Http\Request;
|
||||
use RuntimeException;
|
||||
@@ -117,10 +126,72 @@ class AdminController
|
||||
|
||||
return $this->render($request, 'admin-dashboard', [
|
||||
'title' => '内容管理', 'pages' => $repo->pages(), 'articles' => $repo->articles(public: false, limit: 5),
|
||||
'site' => $repo->site(),
|
||||
'pageTemplateDrawer' => true,
|
||||
]);
|
||||
}
|
||||
|
||||
public function about(Request $request): Response
|
||||
{
|
||||
if ($failure = $this->guard($request)) {
|
||||
return $failure;
|
||||
}
|
||||
|
||||
return $this->render($request, 'admin-about', [
|
||||
'title' => '关于系统',
|
||||
'systemInfo' => [
|
||||
'PHP' => PHP_VERSION,
|
||||
'Webman' => SystemInfo::version('workerman/webman-framework'),
|
||||
'Workerman' => SystemInfo::version('workerman/workerman'),
|
||||
'SQLite' => (string) Database::connect()->query('SELECT sqlite_version()')->fetchColumn(),
|
||||
'运行平台' => SystemInfo::platform(),
|
||||
PHP_OS_FAMILY === 'Linux' ? 'Linux 内核' : '系统版本' => php_uname('r'),
|
||||
],
|
||||
'memory' => SystemInfo::memory(),
|
||||
'mediaUsage' => Media::usage(),
|
||||
'thirdPartyLicenses' => Licenses::dependencies(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function aboutMetrics(Request $request): Response
|
||||
{
|
||||
if ($failure = $this->guard($request)) {
|
||||
return $failure;
|
||||
}
|
||||
|
||||
return $this->json(SystemInfo::sample());
|
||||
}
|
||||
|
||||
public function aboutUpdate(Request $request): Response
|
||||
{
|
||||
if ($failure = $this->guard($request)) {
|
||||
return $failure;
|
||||
}
|
||||
try {
|
||||
return $this->json(Updates::check());
|
||||
} catch (UpdateCheckInProgress $error) {
|
||||
return $this->json(['message' => $error->getMessage()], 409);
|
||||
} catch (RuntimeException $error) {
|
||||
return $this->json(['message' => $error->getMessage()], 502);
|
||||
}
|
||||
}
|
||||
|
||||
public function aboutLicense(Request $request, string $key): Response
|
||||
{
|
||||
if ($failure = $this->guard($request)) {
|
||||
return $failure;
|
||||
}
|
||||
$text = Licenses::text($key);
|
||||
if ($text === null) {
|
||||
return $this->error($request, '找不到许可证。', 404);
|
||||
}
|
||||
|
||||
return new Response(200, [
|
||||
'Content-Type' => 'text/plain; charset=utf-8', 'Cache-Control' => 'no-store',
|
||||
'X-Content-Type-Options' => 'nosniff', 'X-Robots-Tag' => 'noindex',
|
||||
], $text);
|
||||
}
|
||||
|
||||
public function articles(Request $request): Response
|
||||
{
|
||||
if ($failure = $this->guard($request)) {
|
||||
@@ -248,30 +319,36 @@ class AdminController
|
||||
if (!$page) {
|
||||
return $this->error($request, '找不到此页面。', 404);
|
||||
}
|
||||
$site = $repo->site();
|
||||
$layoutKind = $page['layout_kind'];
|
||||
$layout = $layoutKind === null ? null : DesignLibrary::find($page['layout_id'], $layoutKind);
|
||||
$default = $layout && ($site[$layoutKind . '_id'] ?? null) === $layout['id'];
|
||||
$error = '';
|
||||
if ($request->method() === 'POST') {
|
||||
$data = [];
|
||||
foreach (['title', 'description', 'heading', 'accent', 'lead', 'image', 'alt'] as $field) {
|
||||
$data[$field] = $this->input($request, $field);
|
||||
}
|
||||
foreach (['heading', 'accent', 'lead'] as $field) {
|
||||
$data[$field] = CanvasStyle::lineBreaks($data[$field]);
|
||||
}
|
||||
$data['name'] = $request->post('name') === null ? $page['name'] : $this->input($request, 'name');
|
||||
$data['status'] = $request->post('status') === null || $slug === 'home' ? $page['status'] : $this->input($request, 'status');
|
||||
if ($layout) {
|
||||
$data['title'] = $data['name'];
|
||||
}
|
||||
$data['status'] = $layout || $request->post('status') === null || $slug === 'home' ? $page['status'] : $this->input($request, 'status');
|
||||
$scale = $this->input($request, 'image_scale');
|
||||
$data['image_scale'] = $request->post('image_scale') === null ? (int) $page['image_scale']
|
||||
: (ctype_digit($scale) ? (int) $scale : -1);
|
||||
$data['actions'] = [];
|
||||
$validButtons = true;
|
||||
foreach ([1, 2] as $index) {
|
||||
$label = $this->input($request, 'label' . $index);
|
||||
$url = $this->input($request, 'url' . $index);
|
||||
if ($label === '' && $url === '') {
|
||||
continue;
|
||||
}
|
||||
$data['actions'][] = [$url, $label];
|
||||
$validButtons = $validButtons && mb_strlen($label) <= 50 && $label !== '' && strlen($url) <= 1000 && Html::url($url);
|
||||
}
|
||||
$page = array_replace($page, $data, ['version' => $this->version($request)]);
|
||||
try {
|
||||
$data['actions'] = $request->post('hero_actions') === null ? $page['actions']
|
||||
: Buttons::validate(json_decode($this->input($request, 'hero_actions'), true));
|
||||
foreach (['header', 'footer', 'palette'] as $kind) {
|
||||
$key = $kind . '_id';
|
||||
$data[$key] = $request->post($key) === null ? $page[$key] : DesignLibrary::selection($kind, $request->post($key));
|
||||
}
|
||||
$page = array_replace($page, $data);
|
||||
$data['visibility'] = $page['visibility'];
|
||||
foreach (['hero', 'header', 'footer'] as $area) {
|
||||
$value = $request->post('show_' . $area);
|
||||
@@ -283,22 +360,55 @@ class AdminController
|
||||
}
|
||||
$data['visibility'][$area] = $value === '1';
|
||||
}
|
||||
if ($layout) {
|
||||
$default = $request->post('make_default') === '1';
|
||||
$provider = null;
|
||||
if ($layoutKind === 'header' && $request->post('provider_mode') !== null) {
|
||||
foreach (DesignLibrary::PROVIDER_FIELDS as $name) {
|
||||
$provider[$name] = $this->input($request, 'provider_' . $name);
|
||||
}
|
||||
}
|
||||
$data['layoutConfig'] = ['default' => $default, 'provider' => $provider, 'secret' => $this->input($request, 'provider_secret', trimValue: false)];
|
||||
} else {
|
||||
foreach (['header', 'footer'] as $area) {
|
||||
$choice = $request->post($area . '_choice');
|
||||
if ($choice === null) {
|
||||
continue;
|
||||
}
|
||||
$data['visibility'][$area] = $choice !== 'hidden';
|
||||
if ($choice !== 'hidden') {
|
||||
$data[$area . '_id'] = DesignLibrary::selection($area, $choice);
|
||||
}
|
||||
}
|
||||
$page = array_replace($page, array_intersect_key($data, ['header_id' => true, 'footer_id' => true]));
|
||||
}
|
||||
$page['visibility'] = $data['visibility'];
|
||||
$data['alignments'] = $request->post('alignments') === null ? $page['alignments']
|
||||
: $this->alignmentMap(json_decode($this->input($request, 'alignments'), true), ['heading', 'lead', 'actions', 'image']);
|
||||
$page['alignments'] = $data['alignments'];
|
||||
$data['presentation'] = $request->post('canvas_presentation') === null ? $page['presentation']
|
||||
: CanvasStyle::normalize(json_decode($this->input($request, 'canvas_presentation'), true), CanvasStyle::PAGE);
|
||||
$page['presentation'] = $data['presentation'];
|
||||
$data['quicklinks'] = $request->post('quicklinks') === null ? $page['quicklinks'] : SiteDesign::parseLinks($this->input($request, 'quicklinks'));
|
||||
if ($data['name'] === '' || mb_strlen($data['name']) > 100 || !in_array($data['status'], ['draft', 'published'], true)
|
||||
|| $data['title'] === '' || mb_strlen($data['title']) > 160 ||
|
||||
($data['status'] === 'published' && $data['description'] === '') || mb_strlen($data['description']) > 300 ||
|
||||
(($data['visibility']['hero'] ?? true) && $data['heading'] === '') || mb_strlen($data['heading'] . $data['accent']) > 110 ||
|
||||
(!$layout && $page['page_type'] === 'page' && $data['status'] === 'published' && $data['description'] === '') || mb_strlen($data['description']) > 300 ||
|
||||
(($data['visibility']['hero'] ?? true) && !array_key_exists('heroHtml', $data['presentation']) && $data['heading'] === '') || mb_strlen($data['heading'] . $data['accent']) > 110 ||
|
||||
mb_strlen($data['lead']) > 600 || mb_strlen($data['alt']) > 200 ||
|
||||
($data['image_scale'] !== 0 && ($data['image_scale'] < 10 || $data['image_scale'] > 100)) ||
|
||||
strlen($data['image']) > 1000 || ($data['image'] !== '' && !Html::url($data['image'], true)) || !$validButtons) {
|
||||
($data['image_scale'] !== 0 && ($data['image_scale'] < CanvasStyle::MIN_MEDIA_SCALE || $data['image_scale'] > 100)) ||
|
||||
strlen($data['image']) > 1000 || ($data['image'] !== '' && !Html::url($data['image'], true))) {
|
||||
throw new RuntimeException('请检查页面标题、说明、图片地址和按钮链接。');
|
||||
}
|
||||
$sections = $this->canvasSections($request->post('canvas_sections', ''), $page['sections'], $repo->categories());
|
||||
$repo->savePage($slug, $data, $this->version($request), $sections);
|
||||
$order = $request->post('canvas_order', '');
|
||||
if ($order !== '') {
|
||||
$order = is_string($order) ? json_decode($order, true) : null;
|
||||
if (!is_array($order) || !array_is_list($order) || count($order) > count($page['sections']) + 50
|
||||
|| count(array_filter($order, 'is_string')) !== count($order)) {
|
||||
throw new RuntimeException('区块顺序无效。');
|
||||
}
|
||||
}
|
||||
$repo->savePage($slug, $data, $this->version($request), $sections, sectionOrder: $order === '' ? null : $order);
|
||||
|
||||
return $this->success($request, '/admin/pages/' . $slug . '?saved=1');
|
||||
} catch (RuntimeException $exception) {
|
||||
@@ -307,8 +417,11 @@ class AdminController
|
||||
}
|
||||
|
||||
return $this->render($request, 'admin-page', [
|
||||
'title' => '编辑 ' . $page['name'], 'page' => $page, 'categories' => $repo->categories(), 'wideAdmin' => true, 'error' => $error, 'saved' => $request->get('saved') === '1',
|
||||
'pageTemplateDrawer' => true,
|
||||
'title' => '编辑 ' . $page['name'], 'page' => $page, 'categories' => $repo->categories(), 'wideAdmin' => true, 'canvasSourceEditor' => true, 'error' => $error, 'saved' => $request->get('saved') === '1',
|
||||
'pageTemplateDrawer' => !$layout, 'designs' => DesignLibrary::all(), 'layout' => $layout, 'layoutDefault' => $default,
|
||||
'layoutBase' => $layout && ($site['layout_defaults'][$layoutKind] ?? null) === $layout['id'],
|
||||
'siteName' => $site['name'], 'layoutHasMember' => $layoutKind === 'header' && LayoutPage::hasMember($page['sections']),
|
||||
'layoutDefaults' => array_intersect_key($site, ['header_id' => true, 'footer_id' => true]),
|
||||
], $error !== '' ? 422 : 200);
|
||||
}
|
||||
|
||||
@@ -368,10 +481,12 @@ class AdminController
|
||||
$version = $change['version'] ?? null;
|
||||
$fields = $change['fields'] ?? [];
|
||||
$alignments = $change['alignments'] ?? null;
|
||||
$presentation = $change['presentation'] ?? null;
|
||||
$feed = $change['feed'] ?? null;
|
||||
$delete = ($change['delete'] ?? false) === true;
|
||||
$isNew = is_array($change) && array_key_exists('version', $change) && $version === null;
|
||||
if (!is_string($key) || isset($updates[$key]) || !is_array($fields) || (!$delete && $fields === [] && $alignments === null && $feed === null)
|
||||
if (!is_string($key) || isset($updates[$key]) || !is_array($fields)
|
||||
|| (!$delete && $fields === [] && $alignments === null && $presentation === null && $feed === null)
|
||||
|| ($isNew ? (isset($available[$key]) || !preg_match('/^block-[a-f0-9]{32}$/D', $key))
|
||||
: (!isset($available[$key]) || !is_int($version) || $version !== (int) $available[$key]['version']))) {
|
||||
throw new RuntimeException('区块已变更或画布内容无效,请重新打开编辑页。');
|
||||
@@ -421,6 +536,9 @@ class AdminController
|
||||
if ($alignments !== null) {
|
||||
$section['alignments'] = Database::json($this->alignmentMap($alignments, ['title', 'lead']));
|
||||
}
|
||||
if ($presentation !== null) {
|
||||
$section['presentation'] = CanvasStyle::encode(CanvasStyle::normalize($presentation, CanvasStyle::SECTION));
|
||||
}
|
||||
$this->checkSection($section, $section['style']);
|
||||
if (isset($fields['body'])) {
|
||||
$section['body'] = Html::sanitize($section['body']);
|
||||
@@ -650,11 +768,15 @@ class AdminController
|
||||
if ($request->isAjax() && $status >= 400) {
|
||||
return $this->json([
|
||||
'message' => $data['error'] ?? $data['title'],
|
||||
...(!empty($data['field']) ? ['field' => $data['field']] : []),
|
||||
...($status === 403 ? ['csrf' => $token] : []),
|
||||
], $status);
|
||||
}
|
||||
|
||||
return View::render($view, $data + ['token' => $token, 'user' => $this->user, 'adminPath' => $request->path()], $status, true);
|
||||
return View::render($view, $data + [
|
||||
'token' => $token, 'user' => $this->user, 'adminPath' => $request->path(),
|
||||
'laysiteVersion' => Updates::current(),
|
||||
], $status, true);
|
||||
}
|
||||
|
||||
protected function error(Request $request, string $message, int $status): Response
|
||||
|
||||
Reference in New Issue
Block a user