forked from laysense/laysite
Launch LaySite visual editor and MCP workspace
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http;
|
||||
|
||||
use support\Request;
|
||||
use support\Response;
|
||||
use Workerman\Protocols\Http\Session;
|
||||
|
||||
final class VisitorSession
|
||||
{
|
||||
private const COOKIE = 'LAYSITE_VISITOR';
|
||||
|
||||
public static function open(?Request $request): ?Session
|
||||
{
|
||||
$id = $request?->cookie(self::COOKIE);
|
||||
return is_string($id) && preg_match('/^visitor-[a-f0-9]{64}$/D', $id) ? new Session($id) : null;
|
||||
}
|
||||
|
||||
public static function create(): Session
|
||||
{
|
||||
return new Session('visitor-' . bin2hex(random_bytes(32)));
|
||||
}
|
||||
|
||||
public static function cookie(Response $response, ?Session $session, int $seconds): Response
|
||||
{
|
||||
return $response->cookie(self::COOKIE, $session?->getId() ?? '', $seconds, '/',
|
||||
secure: getenv('LAYSITE_SECURE_COOKIES') === '1', httpOnly: true, sameSite: 'Lax');
|
||||
}
|
||||
|
||||
public static function profile(?Request $request): ?array
|
||||
{
|
||||
$profile = self::open($request)?->get('profile');
|
||||
|
||||
return is_array($profile) && ($profile['expires'] ?? 0) > time() ? $profile : null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user