File size: 3,200 Bytes
1e1edce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
// Speichern von Markdown-Dateien mit strikter Pfadvalidierung
// Erlaubte Bereiche:
//  - c:/xampp/htdocs/www/Protokoll/*.md
//  - c:/xampp/htdocs/www/StartAIChatRegeln.md
//  - c:/xampp/htdocs/www/StopAIChatRegeln.md

declare(strict_types=1);
header('Content-Type: application/json; charset=utf-8');

function fail(string $msg, int $code = 400): void {
    http_response_code($code);
    echo json_encode([ 'status' => '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);