Fix design validation, canonical routes, media headers and favicon
This commit is contained in:
@@ -648,6 +648,7 @@ 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);
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ declare(strict_types=1);
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Content\Assets;
|
||||
use App\Content\Buttons;
|
||||
use App\Content\DesignFieldError;
|
||||
use App\Content\DesignLibrary;
|
||||
use App\Content\Repository;
|
||||
use App\Content\SiteDesign;
|
||||
@@ -40,13 +40,17 @@ final class DesignController extends AdminController
|
||||
return $this->error($request, '找不到这项设计配置。', 404);
|
||||
}
|
||||
$error = '';
|
||||
$field = '';
|
||||
$secret = '';
|
||||
$links = SiteDesign::linkText($item['payload']['links'] ?? []);
|
||||
$default = $id !== null && ((new Repository())->site()[$kind . '_id'] ?? null) === $id;
|
||||
if ($request->method() === 'POST') {
|
||||
try {
|
||||
$savedProvider = $item['payload']['provider'] ?? [];
|
||||
$secret = '';
|
||||
$item['name'] = $this->input($request, 'name');
|
||||
$item['preset'] = $this->input($request, 'preset');
|
||||
$item['version'] = $this->version($request);
|
||||
$default = $request->post('make_default') === '1';
|
||||
if ($kind === 'palette') {
|
||||
foreach (DesignLibrary::COLORS as $color => $definition) {
|
||||
$item['payload'][$color] = $this->input($request, $color);
|
||||
@@ -54,11 +58,11 @@ final class DesignController extends AdminController
|
||||
} else {
|
||||
$url = $this->input($request, 'action_url');
|
||||
$label = $this->input($request, 'action_label');
|
||||
$links = $this->input($request, 'links', trimValue: false);
|
||||
$item['payload'] = [
|
||||
'components' => $request->post('components', []),
|
||||
'brand' => $this->input($request, 'brand'), 'logo' => $this->input($request, 'logo'),
|
||||
'text' => $this->input($request, 'text'), 'legal' => $this->input($request, 'legal'),
|
||||
'links' => SiteDesign::parseLinks($this->input($request, 'links')),
|
||||
'action' => $url === '' && $label === '' ? [] : [[$url, $label, $this->input($request, 'action_style'), $request->post('action_blank') === '1']],
|
||||
'provider' => $savedProvider,
|
||||
];
|
||||
@@ -69,20 +73,32 @@ final class DesignController extends AdminController
|
||||
}
|
||||
$secret = $this->input($request, 'provider_secret', trimValue: false);
|
||||
}
|
||||
try {
|
||||
$item['payload']['links'] = SiteDesign::parseLinks($links);
|
||||
} catch (RuntimeException $ex) {
|
||||
throw new DesignFieldError('links', $ex->getMessage());
|
||||
}
|
||||
}
|
||||
$default = $request->post('make_default') === '1';
|
||||
$id = DesignLibrary::save($kind, $id, $item['name'], $item['preset'], $item['payload'], $this->version($request), $default, $secret);
|
||||
$id = DesignLibrary::save($kind, $id, $item['name'], $item['preset'], $item['payload'], $item['version'], $default, $secret);
|
||||
|
||||
return $this->success($request, "/admin/design/$kind/$id?saved=1");
|
||||
} catch (RuntimeException $ex) {
|
||||
$error = $this->validationMessage($ex);
|
||||
$item['payload'] = DesignLibrary::defaults($kind);
|
||||
$field = $ex instanceof DesignFieldError ? $ex->field : '';
|
||||
}
|
||||
}
|
||||
|
||||
if ($kind !== 'palette') {
|
||||
$components = $item['payload']['components'];
|
||||
$item['payload']['components'] = is_array($components) ? array_values(array_intersect(
|
||||
array_filter($components, 'is_string'), array_keys(DesignLibrary::COMPONENTS[$kind])
|
||||
)) : [];
|
||||
}
|
||||
|
||||
return $this->render($request, 'admin-design-edit', [
|
||||
'title' => '设计配置', 'kind' => $kind, 'key' => $key, 'item' => $item, 'default' => $default,
|
||||
'error' => $error, 'saved' => $request->get('saved') === '1',
|
||||
'error' => $error, 'field' => $field, 'links' => $links, 'reenterSecret' => $error !== '' && $secret !== '',
|
||||
'saved' => $error === '' && $request->get('saved') === '1',
|
||||
], $error === '' ? 200 : 422);
|
||||
}
|
||||
|
||||
|
||||
@@ -23,23 +23,22 @@ final class MediaController
|
||||
'Content-Type' => $media['mime'], 'X-Content-Type-Options' => 'nosniff',
|
||||
'Content-Security-Policy' => "default-src 'none'; sandbox",
|
||||
'Cache-Control' => 'public, max-age=31536000, immutable',
|
||||
'Access-Control-Allow-Origin' => '*', 'Accept-Ranges' => 'bytes',
|
||||
'Access-Control-Allow-Origin' => '*',
|
||||
];
|
||||
$range = $request->header('range');
|
||||
if ($range === null) {
|
||||
return (new Response(200, $headers + ['Content-Length' => (string) $length]))->withFile($path);
|
||||
return (new Response(200, $headers))->withFile($path);
|
||||
}
|
||||
$unsatisfiable = $headers + ['Accept-Ranges' => 'bytes', 'Content-Range' => "bytes */$length"];
|
||||
if (!preg_match('/^bytes=(\d*)-(\d*)$/D', $range, $parts) || $parts[1] === '' && $parts[2] === '') {
|
||||
return new Response(416, $headers + ['Content-Range' => "bytes */$length"]);
|
||||
return new Response(416, $unsatisfiable);
|
||||
}
|
||||
$start = $parts[1] === '' ? max(0, $length - (int) $parts[2]) : (int) $parts[1];
|
||||
$end = $parts[1] === '' || $parts[2] === '' ? $length - 1 : min($length - 1, (int) $parts[2]);
|
||||
if ($end < $start) {
|
||||
return new Response(416, $headers + ['Content-Range' => "bytes */$length"]);
|
||||
return new Response(416, $unsatisfiable);
|
||||
}
|
||||
$count = $end - $start + 1;
|
||||
|
||||
return (new Response(206, $headers + ['Content-Length' => (string) $count, 'Content-Range' => "bytes $start-$end/$length"]))
|
||||
->withFile($path, $start, $count);
|
||||
return (new Response(206, $headers))->withFile($path, $start, $end - $start + 1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,11 +4,14 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Content\Assets;
|
||||
use App\Content\Html;
|
||||
use App\Content\Repository;
|
||||
use App\Content\View;
|
||||
use FastRoute\Dispatcher;
|
||||
use support\Request;
|
||||
use support\Response;
|
||||
use Webman\Route;
|
||||
|
||||
final class SiteController
|
||||
{
|
||||
@@ -215,14 +218,21 @@ final class SiteController
|
||||
"User-agent: *\nAllow: /\nDisallow: /admin\nSitemap: " . View::absolute('/sitemap.xml') . "\n");
|
||||
}
|
||||
|
||||
public function favicon(Request $request): Response
|
||||
{
|
||||
return View::redirect(Assets::DEFAULT_ICON, 301);
|
||||
}
|
||||
|
||||
public function missing(Request $request): Response
|
||||
{
|
||||
$path = $request->path();
|
||||
if ($request->method() === 'GET' && strlen($path) > 1 && str_ends_with($path, '/')) {
|
||||
if (in_array($request->method(), ['GET', 'HEAD'], true) && strlen($path) > 1 && str_ends_with($path, '/')) {
|
||||
$normal = rtrim($path, '/');
|
||||
$entry = preg_match('~^/(?:admin|account)(?:/|$)~D', $normal)
|
||||
&& Route::dispatch($request->method(), $normal)[0] === Dispatcher::FOUND;
|
||||
$single = preg_match('~^/[a-z0-9-]+$~D', $normal);
|
||||
$known = $single && ((new Repository())->page(substr($normal, 1))['status'] ?? null) === 'published' && $normal !== '/home';
|
||||
if ($known || preg_match('~^/articles(?:/[a-z0-9-]+){1,2}$~D', $normal)) {
|
||||
if ($entry || ($single && $normal !== '/home' && ((new Repository())->page(substr($normal, 1))['status'] ?? null) === 'published')
|
||||
|| preg_match('~^/articles(?:/[a-z0-9-]+){1,2}$~D', $normal)) {
|
||||
$query = $request->queryString();
|
||||
|
||||
return View::redirect($normal . ($query !== '' ? '?' . $query : ''), 301);
|
||||
|
||||
@@ -7,6 +7,7 @@ namespace App\Controller;
|
||||
use App\Content\BlockTemplates;
|
||||
use App\Content\Html;
|
||||
use App\Content\Media;
|
||||
use App\Content\PageDraft;
|
||||
use App\Content\PageTemplates;
|
||||
use App\Content\Repository;
|
||||
use App\Http\Request;
|
||||
@@ -29,7 +30,7 @@ final class StudioController extends AdminController
|
||||
$data['name'] = $this->input($request, 'name');
|
||||
$data['slug'] = $this->input($request, 'slug');
|
||||
try {
|
||||
if (!$this->slug($data['slug']) || in_array($data['slug'], ['admin', 'assets', 'home', 'account', 'mcp', 'palette', 'favicon'], true)
|
||||
if (!$this->slug($data['slug']) || in_array($data['slug'], PageDraft::RESERVED, true)
|
||||
|| $data['name'] === '' || mb_strlen($data['name']) > 100) {
|
||||
throw new RuntimeException('请填写页面名称及可用的网址标识。');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user