diff --git a/App/pages/dashboard.php b/App/pages/dashboard.php index 6783c27..2131600 100644 --- a/App/pages/dashboard.php +++ b/App/pages/dashboard.php @@ -1,5 +1 @@ -window.__GGS__ = ' . json_encode(['sessionId' => Session::studentId(), 'teacherId' => Session::teacherId(), 'baseUrl' => BASE_URL, 'basePath' => BASE_PATH], JSON_UNESCAPED_UNICODE) . ';'; -echo str_replace('', $ctx . "\n", $html); +window.__GGS__ = ' . json_encode(['sessionId' => Session::studentId(), 'teacherId' => Session::teacherId(), 'baseUrl' => BASE_URL, 'basePath' => BASE_PATH], JSON_UNESCAPED_UNICODE) . ';'; -echo str_replace('', $ctx . "\n", $html); +window.__GGS__ = ' . json_encode(['sessionId' => Session::studentId(), 'teacherId' => Session::teacherId(), 'baseUrl' => BASE_URL, 'basePath' => BASE_PATH], JSON_UNESCAPED_UNICODE) . ';'; -echo str_replace('', $ctx . "\n", $html); +window.__GGS__ = ' . json_encode(['sessionId' => Session::studentId(), 'teacherId' => Session::teacherId(), 'baseUrl' => BASE_URL, 'basePath' => BASE_PATH], JSON_UNESCAPED_UNICODE) . ';'; -echo str_replace('', $ctx . "\n", $html); +' . "\n" - . 'window.__GGS__ = ' . json_encode([ - 'sessionId' => Session::studentId(), - 'teacherId' => Session::teacherId(), - 'baseUrl' => BASE_URL, - 'basePath' => BASE_PATH, - ], JSON_UNESCAPED_UNICODE) . ";\n" - . ''; - -$html = str_replace('', $sessionScript . "\n", $html); -echo $html; +' . "\n" - . 'window.__GGS__ = ' . json_encode([ - 'sessionId' => Session::studentId(), - 'teacherId' => Session::teacherId(), - 'baseUrl' => BASE_URL, - 'basePath' => BASE_PATH, - ], JSON_UNESCAPED_UNICODE) . ";\n" - . ''; - -$html = str_replace('', $sessionScript . "\n", $html); -echo $html; + injizieren -$sessionScript = ''; - -$html = str_replace('', $sessionScript . "\n", $html); - -echo $html; +window.__GGS__ = ' . json_encode(['sessionId' => Session::studentId(), 'teacherId' => Session::teacherId(), 'baseUrl' => BASE_URL, 'basePath' => BASE_PATH], JSON_UNESCAPED_UNICODE) . ';'; -echo str_replace('', $ctx . "\n", $html); +window.__GGS__ = ' . json_encode(['sessionId' => Session::studentId(), 'teacherId' => Session::teacherId(), 'baseUrl' => BASE_URL, 'basePath' => BASE_PATH], JSON_UNESCAPED_UNICODE) . ';'; -echo str_replace('', $ctx . "\n", $html); +window.__GGS__ = ' . json_encode(['sessionId' => Session::studentId(), 'teacherId' => Session::teacherId(), 'baseUrl' => BASE_URL, 'basePath' => BASE_PATH], JSON_UNESCAPED_UNICODE) . ';'; -echo str_replace('', $ctx . "\n", $html); +window.__GGS__ = ' . json_encode(['sessionId' => Session::studentId(), 'teacherId' => Session::teacherId(), 'baseUrl' => BASE_URL, 'basePath' => BASE_PATH], JSON_UNESCAPED_UNICODE) . ';'; -echo str_replace('', $ctx . "\n", $html); +fetchOne( - 'SELECT * FROM classes WHERE id = ? AND teacher_id = ?', + 'SELECT id, name, school_year, join_code, created_at FROM classes WHERE id = ? AND teacher_id = ?', [$classId, $teacherId] ); if (!$class) Response::error('Klasse nicht gefunden', 404); } // Klassen des Lehrers -$classes = $db->fetchAll('SELECT * FROM classes WHERE teacher_id = ? ORDER BY created_at DESC', [$teacherId]); +$classes = $db->fetchAll('SELECT id, name, school_year, join_code, created_at FROM classes WHERE teacher_id = ? ORDER BY created_at DESC', [$teacherId]); if (!$classId && !empty($classes)) { $classId = $classes[0]['id']; diff --git a/App/php/api/saves.php b/App/php/api/saves.php index 1beabaa..9ad69d1 100644 --- a/App/php/api/saves.php +++ b/App/php/api/saves.php @@ -1,22 +1,19 @@ null]); - } + if (!$sessionId) Response::json(['data' => null]); + $key = $_GET['key'] ?? ''; - if (!$key) Response::error('key fehlt'); + if (!$key || strlen($key) > 100) Response::error('key fehlt oder zu lang'); $row = $db->fetchOne( 'SELECT save_data, save_version FROM game_saves WHERE session_id = ? AND save_key = ?', @@ -31,6 +28,9 @@ if ($method === 'POST') { if (!$body || !isset($body['key']) || !isset($body['data'])) { Response::error('key und data erforderlich'); } + if (strlen($body['key']) > 100 || strlen($body['data']) > 500000) { + Response::error('Payload zu gross', 413); + } $db->execute( 'INSERT INTO game_saves (session_id, save_key, save_data, save_version) diff --git a/App/php/api/sessions.php b/App/php/api/sessions.php index 8fe4c0d..0a0d93f 100644 --- a/App/php/api/sessions.php +++ b/App/php/api/sessions.php @@ -5,8 +5,6 @@ * POST /api/sessions {action: "status"} */ -header('Content-Type: application/json; charset=utf-8'); - if ($_SERVER['REQUEST_METHOD'] !== 'POST') { Response::error('Nur POST erlaubt', 405); } @@ -26,7 +24,7 @@ if ($action === 'join') { Response::error('Klasse nicht gefunden'); } - $displayName = trim($body['displayName'] ?? ''); + $displayName = mb_substr(trim($body['displayName'] ?? ''), 0, 64); $uuid = Session::createStudent((int)$class['id'], $displayName); Response::ok([ diff --git a/App/php/lib/Session.php b/App/php/lib/Session.php index fad9be3..223c9c5 100644 --- a/App/php/lib/Session.php +++ b/App/php/lib/Session.php @@ -22,12 +22,12 @@ class Session { /** 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) + $uuid = sprintf('%s-%s-%s-%s-%s', + bin2hex(random_bytes(4)), + bin2hex(random_bytes(2)), + bin2hex(random_bytes(2)), + bin2hex(random_bytes(2)), + bin2hex(random_bytes(6)) ); $db = Database::get(); @@ -83,6 +83,12 @@ class Session { public static function logout(): void { self::start(); session_destroy(); - setcookie(self::COOKIE_NAME, '', ['expires' => 1, 'path' => BASE_PATH . '/']); + setcookie(self::COOKIE_NAME, '', [ + 'expires' => 1, + 'path' => BASE_PATH . '/', + 'samesite' => 'Lax', + 'secure' => IS_PRODUCTION, + 'httponly' => true, + ]); } } diff --git a/App/php/templates/_scripts.php b/App/php/templates/_scripts.php index b60664a..13adf8c 100644 --- a/App/php/templates/_scripts.php +++ b/App/php/templates/_scripts.php @@ -41,3 +41,15 @@ function injectSessionContext(): void { ]; echo '' . "\n"; } + +/** Seite rendern: HTML laden, Session-Kontext injizieren, ausgeben */ +function renderPage(string $htmlFile): void { + $html = file_get_contents(APP_ROOT . '/' . $htmlFile); + $ctx = ''; + echo str_replace('', $ctx . "\n", $html); +}