f22c5ebbfe
- PHP/MySQL Backend (XAMPP + Produktionsserver) - Front-Controller, API-Endpunkte, Session-Management - Flussmanagement-Simulation (Echtzeit, Punkt-basierter Fluss) - Stadt & Raumplanung (Prototyp, Top-Down Kachelsystem) - Klimawaechter 3D: Deiche kleiner, Baeume kippen, Budget angepasst - persistence.ts: Dualer Speicher (localStorage + Server-API) - 6 Unit-Test-Dateien fuer bestehende Simulationen Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
89 lines
2.5 KiB
PHP
89 lines
2.5 KiB
PHP
<?php
|
|
/**
|
|
* GeoGraSim — Session Management
|
|
* Schueler: UUID-Cookie (anonym, kein Login)
|
|
* Lehrkraefte: PHP-Session mit teacher_id
|
|
*/
|
|
|
|
class Session {
|
|
const COOKIE_NAME = 'ggs_session';
|
|
const COOKIE_TTL = 86400 * 30; // 30 Tage
|
|
|
|
public static function start(): void {
|
|
if (session_status() === PHP_SESSION_NONE) {
|
|
session_start();
|
|
}
|
|
}
|
|
|
|
/** Schueler-Session-ID aus Cookie */
|
|
public static function studentId(): ?string {
|
|
return $_COOKIE[self::COOKIE_NAME] ?? null;
|
|
}
|
|
|
|
/** Neue Schueler-Session erstellen */
|
|
public static function createStudent(int $classId, string $displayName = ''): string {
|
|
$uuid = sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
|
|
mt_rand(0, 0xffff), mt_rand(0, 0xffff),
|
|
mt_rand(0, 0xffff),
|
|
mt_rand(0, 0x0fff) | 0x4000,
|
|
mt_rand(0, 0x3fff) | 0x8000,
|
|
mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
|
|
);
|
|
|
|
$db = Database::get();
|
|
$db->execute(
|
|
'INSERT INTO student_sessions (id, class_id, display_name) VALUES (?, ?, ?)',
|
|
[$uuid, $classId, $displayName]
|
|
);
|
|
|
|
setcookie(self::COOKIE_NAME, $uuid, [
|
|
'expires' => time() + self::COOKIE_TTL,
|
|
'path' => BASE_PATH . '/',
|
|
'samesite' => 'Lax',
|
|
'secure' => IS_PRODUCTION,
|
|
'httponly' => true,
|
|
]);
|
|
|
|
return $uuid;
|
|
}
|
|
|
|
/** API-Guard: 401 wenn keine Session */
|
|
public static function requireStudent(): string {
|
|
$id = self::studentId();
|
|
if (!$id) {
|
|
http_response_code(401);
|
|
echo json_encode(['error' => 'Keine Session']);
|
|
exit;
|
|
}
|
|
return $id;
|
|
}
|
|
|
|
/** Lehrer eingeloggt? */
|
|
public static function teacherId(): ?int {
|
|
return $_SESSION['teacher_id'] ?? null;
|
|
}
|
|
|
|
/** Lehrer-Login */
|
|
public static function loginTeacher(int $teacherId): void {
|
|
self::start();
|
|
$_SESSION['teacher_id'] = $teacherId;
|
|
}
|
|
|
|
/** Lehrer-Guard: 401 wenn nicht eingeloggt */
|
|
public static function requireTeacher(): int {
|
|
$id = self::teacherId();
|
|
if (!$id) {
|
|
http_response_code(401);
|
|
echo json_encode(['error' => 'Nicht eingeloggt']);
|
|
exit;
|
|
}
|
|
return $id;
|
|
}
|
|
|
|
public static function logout(): void {
|
|
self::start();
|
|
session_destroy();
|
|
setcookie(self::COOKIE_NAME, '', ['expires' => 1, 'path' => BASE_PATH . '/']);
|
|
}
|
|
}
|