// platform_csrf_ok(): bool — validate the token sent with a POST function platform_csrf_token(): string { if (session_status() !== PHP_SESSION_ACTIVE) { @session_start(); } if (empty($_SESSION['platform_tools_csrf'])) { $_SESSION['platform_tools_csrf'] = bin2hex(random_bytes(32)); } return (string) $_SESSION['platform_tools_csrf']; } function platform_csrf_field(): string { return ''; } function platform_csrf_ok(): bool { if (session_status() !== PHP_SESSION_ACTIVE) { @session_start(); } $sent = (string) ($_POST['_pt_csrf'] ?? ''); $have = (string) ($_SESSION['platform_tools_csrf'] ?? ''); $ok = $sent !== '' && $have !== '' && hash_equals($have, $sent); if (!$ok) { // Uma sessão expirada também cai aqui; é no hub, com a frota inteira à // vista, que um padrão se distingue de um formulário esquecido aberto. require_once __DIR__ . '/functions_security_events.php'; platform_security_event(null, 'csrf_failed', (string) (parse_url($_SERVER['REQUEST_URI'] ?? '', PHP_URL_PATH) ?? '')); } return $ok; }