'error', 'error' => $msg ], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); exit; } try { $raw = file_get_contents('php://input'); if ($raw === false) fail('Konnte Request-Body nicht lesen'); $data = json_decode($raw, true, 512, JSON_THROW_ON_ERROR); } catch (Throwable $e) { fail('Ungültiger JSON-Body: ' . $e->getMessage()); } $path = $data['path'] ?? null; $content = $data['content'] ?? null; if (!is_string($path) || !is_string($content)) fail('Parameter path und content sind erforderlich'); $apiDir = __DIR__; $protokollDir = realpath(dirname(__DIR__)); // .../www/Protokoll $rootDir = realpath(dirname(dirname(__DIR__))); // .../www if ($protokollDir === false || $rootDir === false) fail('Serverpfade nicht auflösbar', 500); // Nur .md Dateien if (!preg_match('/\.md$/i', $path)) fail('Nur .md Dateien sind erlaubt'); // Pfadabbildung: Dashboard liefert Pfade relativ zur Seite Protokoll/Projektsteuerung.html // - "./DATEI.md" => Protokoll/DATEI.md // - "../StartAIChatRegeln.md" bzw. "../StopAIChatRegeln.md" => Projektwurzel $targetAbs = null; if (str_starts_with($path, './')) { $rel = ltrim(substr($path, 2), '/\\'); $targetAbs = $protokollDir . DIRECTORY_SEPARATOR . $rel; } elseif (str_starts_with($path, '../')) { $rel = ltrim(substr($path, 3), '/\\'); $targetAbs = $rootDir . DIRECTORY_SEPARATOR . $rel; } else { fail('Ungültiges Pfadformat'); } // Auflösen und validieren $targetReal = realpath($targetAbs) ?: $targetAbs; // Beim Speichern darf existierende Datei verlangt sein $ext = strtolower(pathinfo($targetReal, PATHINFO_EXTENSION)); if ($ext !== 'md') fail('Nur .md Dateien sind erlaubt'); // Nur existierende Dateien speichern (keine Neuanlage hier) if (!file_exists($targetReal)) fail('Datei nicht gefunden: ' . $path, 404); // Sicherheitsgrenzen: Datei muss innerhalb erlaubter Bereiche liegen $allowed = false; $protokollPrefix = rtrim(str_replace('\\', '/', $protokollDir), '/') . '/'; $rootPrefix = rtrim(str_replace('\\', '/', $rootDir), '/') . '/'; $targetNorm = rtrim(str_replace('\\', '/', realpath($targetReal) ?: $targetReal), '/'); if (str_starts_with($targetNorm, $protokollPrefix)) { $allowed = true; } else { // Root nur für die beiden Regeldateien erlauben if (str_starts_with($targetNorm, $rootPrefix)) { $base = basename($targetNorm); if (in_array($base, ['StartAIChatRegeln.md', 'StopAIChatRegeln.md'], true)) { $allowed = true; } } } if (!$allowed) fail('Speichern an diesem Ort ist nicht erlaubt'); // Schreiben $ok = @file_put_contents($targetReal, $content, LOCK_EX); if ($ok === false) fail('Speichern fehlgeschlagen'); echo json_encode([ 'status' => 'ok' ], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);