File size: 1,383 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
<?php
// Anlage neuer Markdown-Dateien ausschließlich unter Protokoll/

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());
}

$name = $data['name'] ?? '';
$content = $data['content'] ?? '';
if (!is_string($name) || $name === '') fail('Parameter name ist erforderlich');
if (!preg_match('/^[A-Za-z0-9._-]+$/', $name)) fail('Ungültiger Dateiname');
if (!preg_match('/\.md$/i', $name)) fail('Dateiname muss auf .md enden');

$protokollDir = realpath(dirname(__DIR__));
if ($protokollDir === false) fail('Serverpfad nicht auflösbar', 500);

$target = $protokollDir . DIRECTORY_SEPARATOR . $name;
if (file_exists($target)) fail('Datei existiert bereits');

$ok = @file_put_contents($target, (string)$content, LOCK_EX);
if ($ok === false) fail('Anlegen fehlgeschlagen');

echo json_encode([ 'status' => 'ok', 'path' => './' . $name ], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);