= 70300) { setcookie('ueditor_ai_csrf', $t, [ 'expires' => time() + 7200, 'path' => '/', 'secure' => $secure, 'httponly' => false, 'samesite' => 'Lax', ]); } else { setcookie('ueditor_ai_csrf', $t, time() + 7200, '/', '', $secure, false); } } return $t; } /** * 同时接受 JSON 内 _csrf_token 与 Cookie ueditor_ai_csrf(任一与 session 一致即通过),避免 body 陈旧而 Cookie 已刷新。 */ public static function validate($submitted): bool { $expected = session(self::SESSION_KEY); if ($expected === null || $expected === '') { return false; } $expected = (string) $expected; $fromBody = is_string($submitted) ? $submitted : ''; $fromCookie = (!empty($_COOKIE['ueditor_ai_csrf']) && is_string($_COOKIE['ueditor_ai_csrf'])) ? (string) $_COOKIE['ueditor_ai_csrf'] : ''; foreach ([$fromBody, $fromCookie] as $cand) { if ($cand === '') { continue; } if (strlen($expected) === strlen($cand) && hash_equals($expected, $cand)) { return true; } } return false; } }