v0.9.2
This commit is contained in:
commit
8f8513f802
278 files changed
+18400
No files matched your search
@@ -0,0 +1,109 @@
|
||||
<?php
|
||||
|
||||
namespace IriksIT\Manager\Support;
|
||||
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
/**
|
||||
* The live preview of a page: the blocks that are not saved yet, kept for a few minutes under a signed token, and shown by the real
|
||||
* public page. The editor posts the blocks, gets back the address of the public page with `?mb-preview=<token>`, and shows that in a frame.
|
||||
*
|
||||
* `Blocks::for($slug)` uses the preview blocks by itself when the request carries a token that is signed, not expired and made for that
|
||||
* page, **and** the person is logged in with `pages.edit`. Nobody else can read a draft, whatever token they hold. It works the same for
|
||||
* Inertia, Blade and the page API, as long as the request has the session of the person (a frontend on another origin has no session and
|
||||
* gets the saved blocks; it can build its own preview mode from the schema).
|
||||
*/
|
||||
final class BlockPreview
|
||||
{
|
||||
public const PARAMETER = 'mb-preview';
|
||||
|
||||
/** Whether the editor offers a preview: the site switched it on in `manager.blocks.preview`. */
|
||||
public static function enabled(): bool
|
||||
{
|
||||
return (bool) config('manager.blocks.preview.enabled', false);
|
||||
}
|
||||
|
||||
/** The public address of a page: the pattern in `manager.blocks.preview.url` (`/{slug}`, the home page is `/`), or an invokable class of the site. */
|
||||
public static function address(string $slug): string
|
||||
{
|
||||
$setting = config('manager.blocks.preview.url');
|
||||
|
||||
if (is_string($setting) && $setting !== '' && ! str_contains($setting, '{slug}') && class_exists($setting) && is_callable(new $setting)) {
|
||||
return (string) (new $setting)($slug);
|
||||
}
|
||||
|
||||
$pattern = is_string($setting) && $setting !== '' ? $setting : '/{slug}';
|
||||
|
||||
return $slug === config('manager.blocks.preview.home', 'home') && $setting === null ? '/' : str_replace('{slug}', $slug, $pattern);
|
||||
}
|
||||
|
||||
/**
|
||||
* Keeps the blocks and returns the token with the address to show them at.
|
||||
*
|
||||
* @param array<array-key, mixed> $blocks
|
||||
* @param list<string> $allowed the types the page may hold
|
||||
* @return array{token: string, url: string, expires_at: int}
|
||||
*/
|
||||
public static function store(string $slug, array $blocks, array $allowed): array
|
||||
{
|
||||
$clean = Blocks::clean($blocks, $allowed);
|
||||
$json = (string) json_encode($clean);
|
||||
|
||||
abort_if(strlen($json) > max(1, (int) config('manager.blocks.preview.max_kb', 512)) * 1024, 422, __('This page is too big to preview.'));
|
||||
|
||||
$ttl = max(60, (int) config('manager.blocks.preview.ttl', 600));
|
||||
$id = strtolower((string) Str::ulid());
|
||||
$expires = time() + $ttl;
|
||||
|
||||
Cache::put(self::key($id), $clean, $ttl);
|
||||
|
||||
$token = $id.'.'.$expires.'.'.self::sign($id, $slug, $expires);
|
||||
|
||||
return ['token' => $token, 'url' => self::address($slug).(str_contains(self::address($slug), '?') ? '&' : '?').self::PARAMETER.'='.$token, 'expires_at' => $expires];
|
||||
}
|
||||
|
||||
/**
|
||||
* The blocks of the preview this request asks for, or null: no token, a wrong or expired one, one for another page, no person logged in
|
||||
* with `pages.edit`, or the blocks are gone from the cache.
|
||||
*
|
||||
* @return ?list<array<string, mixed>>
|
||||
*/
|
||||
public static function current(string $slug): ?array
|
||||
{
|
||||
if (! self::enabled() || ! app()->bound('request')) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$token = request()->query(self::PARAMETER);
|
||||
|
||||
if (! is_string($token) || substr_count($token, '.') !== 2) {
|
||||
return null;
|
||||
}
|
||||
|
||||
[$id, $expires, $signature] = explode('.', $token);
|
||||
|
||||
if (! preg_match('/^[0-9a-z]{26}$/', $id) || ! ctype_digit($expires) || (int) $expires < time() || ! hash_equals(self::sign($id, $slug, (int) $expires), $signature)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (! Gate::allows('pages.edit')) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$blocks = Cache::get(self::key($id));
|
||||
|
||||
return is_array($blocks) ? $blocks : null;
|
||||
}
|
||||
|
||||
private static function key(string $id): string
|
||||
{
|
||||
return 'manager:block-preview:'.$id;
|
||||
}
|
||||
|
||||
private static function sign(string $id, string $slug, int $expires): string
|
||||
{
|
||||
return hash_hmac('sha256', $id.'|'.$slug.'|'.$expires, (string) config('app.key'));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user