Simplify: PHP page dedup, secure UUID, input validation, remove redundant headers

- pages/*.php: 11 files reduced to 1-liners via shared renderPage() helper
- Session: UUID generation uses random_bytes() instead of mt_rand()
- Session: logout cookie uses same security options as create
- API saves: size limits on key (100) and data (500KB)
- API sessions: displayName capped at 64 chars
- API: removed redundant Content-Type headers (Response::json handles it)
- API dashboard: replaced SELECT * with explicit columns

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-13 16:54:43 +02:00
parent f22c5ebbfe
commit 32ed869f23
17 changed files with 47 additions and 121 deletions
+1 -5
View File
@@ -1,5 +1 @@
<?php <?php renderPage('dashboard.html');
/** GeoGraSim — Lehrkraefte-Dashboard */
$html = file_get_contents(APP_ROOT . '/dashboard.html');
$ctx = '<script>window.__GGS__ = ' . json_encode(['sessionId' => Session::studentId(), 'teacherId' => Session::teacherId(), 'baseUrl' => BASE_URL, 'basePath' => BASE_PATH], JSON_UNESCAPED_UNICODE) . ';</script>';
echo str_replace('</body>', $ctx . "\n</body>", $html);
+1 -5
View File
@@ -1,5 +1 @@
<?php <?php renderPage('energiemix.html');
/** GeoGraSim — Energiemix-Simulator */
$html = file_get_contents(APP_ROOT . '/energiemix.html');
$ctx = '<script>window.__GGS__ = ' . json_encode(['sessionId' => Session::studentId(), 'teacherId' => Session::teacherId(), 'baseUrl' => BASE_URL, 'basePath' => BASE_PATH], JSON_UNESCAPED_UNICODE) . ';</script>';
echo str_replace('</body>', $ctx . "\n</body>", $html);
+1 -5
View File
@@ -1,5 +1 @@
<?php <?php renderPage('erdbeben.html');
/** GeoGraSim — Erdbeben-Simulator */
$html = file_get_contents(APP_ROOT . '/erdbeben.html');
$ctx = '<script>window.__GGS__ = ' . json_encode(['sessionId' => Session::studentId(), 'teacherId' => Session::teacherId(), 'baseUrl' => BASE_URL, 'basePath' => BASE_PATH], JSON_UNESCAPED_UNICODE) . ';</script>';
echo str_replace('</body>', $ctx . "\n</body>", $html);
+1 -5
View File
@@ -1,5 +1 @@
<?php <?php renderPage('fluss.html');
/** GeoGraSim — Flussmanagement-Simulation */
$html = file_get_contents(APP_ROOT . '/fluss.html');
$ctx = '<script>window.__GGS__ = ' . json_encode(['sessionId' => Session::studentId(), 'teacherId' => Session::teacherId(), 'baseUrl' => BASE_URL, 'basePath' => BASE_PATH], JSON_UNESCAPED_UNICODE) . ';</script>';
echo str_replace('</body>', $ctx . "\n</body>", $html);
+1 -18
View File
@@ -1,18 +1 @@
<?php <?php renderPage('game-3d.html');
/**
* GeoGraSim — Klimawächter (3D)
*/
$html = file_get_contents(APP_ROOT . '/game-3d.html');
$sessionScript = '<script>' . "\n"
. 'window.__GGS__ = ' . json_encode([
'sessionId' => Session::studentId(),
'teacherId' => Session::teacherId(),
'baseUrl' => BASE_URL,
'basePath' => BASE_PATH,
], JSON_UNESCAPED_UNICODE) . ";\n"
. '</script>';
$html = str_replace('</body>', $sessionScript . "\n</body>", $html);
echo $html;
+1 -18
View File
@@ -1,18 +1 @@
<?php <?php renderPage('game.html');
/**
* GeoGraSim — Klimawächter (2D)
*/
$html = file_get_contents(APP_ROOT . '/game.html');
$sessionScript = '<script>' . "\n"
. 'window.__GGS__ = ' . json_encode([
'sessionId' => Session::studentId(),
'teacherId' => Session::teacherId(),
'baseUrl' => BASE_URL,
'basePath' => BASE_PATH,
], JSON_UNESCAPED_UNICODE) . ";\n"
. '</script>';
$html = str_replace('</body>', $sessionScript . "\n</body>", $html);
echo $html;
+1 -21
View File
@@ -1,21 +1 @@
<?php <?php renderPage('index.html');
/**
* GeoGraSim — Startseite (Landing Page)
* Laedt die bestehende index.html und injiziert Session-Kontext.
*/
$html = file_get_contents(APP_ROOT . '/index.html');
// Session-Kontext vor </body> injizieren
$sessionScript = '<script>' . "\n"
. 'window.__GGS__ = ' . json_encode([
'sessionId' => Session::studentId(),
'teacherId' => Session::teacherId(),
'baseUrl' => BASE_URL,
'basePath' => BASE_PATH,
], JSON_UNESCAPED_UNICODE) . ";\n"
. '</script>';
$html = str_replace('</body>', $sessionScript . "\n</body>", $html);
echo $html;
+1 -5
View File
@@ -1,5 +1 @@
<?php <?php renderPage('lieferketten.html');
/** GeoGraSim — Lieferketten-Simulator */
$html = file_get_contents(APP_ROOT . '/lieferketten.html');
$ctx = '<script>window.__GGS__ = ' . json_encode(['sessionId' => Session::studentId(), 'teacherId' => Session::teacherId(), 'baseUrl' => BASE_URL, 'basePath' => BASE_PATH], JSON_UNESCAPED_UNICODE) . ';</script>';
echo str_replace('</body>', $ctx . "\n</body>", $html);
+1 -5
View File
@@ -1,5 +1 @@
<?php <?php renderPage('regenwald.html');
/** GeoGraSim — Regenwald-Explorer */
$html = file_get_contents(APP_ROOT . '/regenwald.html');
$ctx = '<script>window.__GGS__ = ' . json_encode(['sessionId' => Session::studentId(), 'teacherId' => Session::teacherId(), 'baseUrl' => BASE_URL, 'basePath' => BASE_PATH], JSON_UNESCAPED_UNICODE) . ';</script>';
echo str_replace('</body>', $ctx . "\n</body>", $html);
+1 -5
View File
@@ -1,5 +1 @@
<?php <?php renderPage('sim.html');
/** GeoGraSim — Simulationsseite (POE-Workflow) */
$html = file_get_contents(APP_ROOT . '/sim.html');
$ctx = '<script>window.__GGS__ = ' . json_encode(['sessionId' => Session::studentId(), 'teacherId' => Session::teacherId(), 'baseUrl' => BASE_URL, 'basePath' => BASE_PATH], JSON_UNESCAPED_UNICODE) . ';</script>';
echo str_replace('</body>', $ctx . "\n</body>", $html);
+1 -5
View File
@@ -1,5 +1 @@
<?php <?php renderPage('stilauswahl.html');
/** GeoGraSim — Stilauswahl */
$html = file_get_contents(APP_ROOT . '/stilauswahl.html');
$ctx = '<script>window.__GGS__ = ' . json_encode(['sessionId' => Session::studentId(), 'teacherId' => Session::teacherId(), 'baseUrl' => BASE_URL, 'basePath' => BASE_PATH], JSON_UNESCAPED_UNICODE) . ';</script>';
echo str_replace('</body>', $ctx . "\n</body>", $html);
-2
View File
@@ -4,8 +4,6 @@
* POST /api/assessment {simId, processLog, predictions, results, reflections, duration, completedPhases} * POST /api/assessment {simId, processLog, predictions, results, reflections, duration, completedPhases}
*/ */
header('Content-Type: application/json; charset=utf-8');
if ($_SERVER['REQUEST_METHOD'] !== 'POST') { if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
Response::error('Nur POST erlaubt', 405); Response::error('Nur POST erlaubt', 405);
} }
+2 -4
View File
@@ -4,8 +4,6 @@
* GET /api/dashboard?class_id=X * GET /api/dashboard?class_id=X
*/ */
header('Content-Type: application/json; charset=utf-8');
if ($_SERVER['REQUEST_METHOD'] !== 'GET') { if ($_SERVER['REQUEST_METHOD'] !== 'GET') {
Response::error('Nur GET erlaubt', 405); Response::error('Nur GET erlaubt', 405);
} }
@@ -18,14 +16,14 @@ $classId = (int)($_GET['class_id'] ?? 0);
// Verifizieren dass die Klasse dem Lehrer gehoert // Verifizieren dass die Klasse dem Lehrer gehoert
if ($classId) { if ($classId) {
$class = $db->fetchOne( $class = $db->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] [$classId, $teacherId]
); );
if (!$class) Response::error('Klasse nicht gefunden', 404); if (!$class) Response::error('Klasse nicht gefunden', 404);
} }
// Klassen des Lehrers // 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)) { if (!$classId && !empty($classes)) {
$classId = $classes[0]['id']; $classId = $classes[0]['id'];
+8 -8
View File
@@ -1,22 +1,19 @@
<?php <?php
/** /**
* API: Spielstaende speichern/laden * API: Spielstaende speichern/laden
* GET /api/saves?key=klimawaechter-save → Spielstand laden * GET /api/saves?key=klimawaechter-save
* POST /api/saves {key, data, version} → Spielstand speichern * POST /api/saves {key, data, version}
*/ */
header('Content-Type: application/json; charset=utf-8');
$method = $_SERVER['REQUEST_METHOD']; $method = $_SERVER['REQUEST_METHOD'];
$db = Database::get(); $db = Database::get();
if ($method === 'GET') { if ($method === 'GET') {
$sessionId = Session::studentId(); $sessionId = Session::studentId();
if (!$sessionId) { if (!$sessionId) Response::json(['data' => null]);
Response::json(['data' => null]);
}
$key = $_GET['key'] ?? ''; $key = $_GET['key'] ?? '';
if (!$key) Response::error('key fehlt'); if (!$key || strlen($key) > 100) Response::error('key fehlt oder zu lang');
$row = $db->fetchOne( $row = $db->fetchOne(
'SELECT save_data, save_version FROM game_saves WHERE session_id = ? AND save_key = ?', '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'])) { if (!$body || !isset($body['key']) || !isset($body['data'])) {
Response::error('key und data erforderlich'); Response::error('key und data erforderlich');
} }
if (strlen($body['key']) > 100 || strlen($body['data']) > 500000) {
Response::error('Payload zu gross', 413);
}
$db->execute( $db->execute(
'INSERT INTO game_saves (session_id, save_key, save_data, save_version) 'INSERT INTO game_saves (session_id, save_key, save_data, save_version)
+1 -3
View File
@@ -5,8 +5,6 @@
* POST /api/sessions {action: "status"} * POST /api/sessions {action: "status"}
*/ */
header('Content-Type: application/json; charset=utf-8');
if ($_SERVER['REQUEST_METHOD'] !== 'POST') { if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
Response::error('Nur POST erlaubt', 405); Response::error('Nur POST erlaubt', 405);
} }
@@ -26,7 +24,7 @@ if ($action === 'join') {
Response::error('Klasse nicht gefunden'); Response::error('Klasse nicht gefunden');
} }
$displayName = trim($body['displayName'] ?? ''); $displayName = mb_substr(trim($body['displayName'] ?? ''), 0, 64);
$uuid = Session::createStudent((int)$class['id'], $displayName); $uuid = Session::createStudent((int)$class['id'], $displayName);
Response::ok([ Response::ok([
+13 -7
View File
@@ -22,12 +22,12 @@ class Session {
/** Neue Schueler-Session erstellen */ /** Neue Schueler-Session erstellen */
public static function createStudent(int $classId, string $displayName = ''): string { public static function createStudent(int $classId, string $displayName = ''): string {
$uuid = sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x', $uuid = sprintf('%s-%s-%s-%s-%s',
mt_rand(0, 0xffff), mt_rand(0, 0xffff), bin2hex(random_bytes(4)),
mt_rand(0, 0xffff), bin2hex(random_bytes(2)),
mt_rand(0, 0x0fff) | 0x4000, bin2hex(random_bytes(2)),
mt_rand(0, 0x3fff) | 0x8000, bin2hex(random_bytes(2)),
mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff) bin2hex(random_bytes(6))
); );
$db = Database::get(); $db = Database::get();
@@ -83,6 +83,12 @@ class Session {
public static function logout(): void { public static function logout(): void {
self::start(); self::start();
session_destroy(); 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,
]);
} }
} }
+12
View File
@@ -41,3 +41,15 @@ function injectSessionContext(): void {
]; ];
echo '<script>window.__GGS__ = ' . json_encode($ctx, JSON_UNESCAPED_UNICODE) . ';</script>' . "\n"; echo '<script>window.__GGS__ = ' . json_encode($ctx, JSON_UNESCAPED_UNICODE) . ';</script>' . "\n";
} }
/** Seite rendern: HTML laden, Session-Kontext injizieren, ausgeben */
function renderPage(string $htmlFile): void {
$html = file_get_contents(APP_ROOT . '/' . $htmlFile);
$ctx = '<script>window.__GGS__ = ' . json_encode([
'sessionId' => Session::studentId(),
'teacherId' => Session::teacherId(),
'baseUrl' => BASE_URL,
'basePath' => BASE_PATH,
], JSON_UNESCAPED_UNICODE) . ';</script>';
echo str_replace('</body>', $ctx . "\n</body>", $html);
}