false, 'error' => 'Nicht eingeloggt (Admin oder Lehrperson erforderlich).']); exit; } $raw = file_get_contents('php://input'); $data = json_decode($raw, true); if (!is_array($data) || count($data) === 0) { http_response_code(400); echo json_encode(['ok' => false, 'error' => 'invalid or empty json']); exit; } $target = __DIR__ . '/../js/maps-custom.json'; $backupDir = __DIR__ . '/mapbackups'; // vorhandene, nicht-leere Datei zuerst sichern if (file_exists($target)) { $old = trim((string)file_get_contents($target)); if ($old !== '' && $old !== '{}') { if (!is_dir($backupDir)) @mkdir($backupDir, 0777, true); @copy($target, $backupDir . '/maps-custom-' . date('Ymd-His') . '.json'); @copy($target, $backupDir . '/maps-custom.prev.json'); // schneller Griff aufs letzte $files = glob($backupDir . '/maps-custom-*.json'); // max. 40 Backups halten if ($files && count($files) > 40) { sort($files); foreach (array_slice($files, 0, count($files) - 40) as $f) @unlink($f); } } } $ok = file_put_contents($target, json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE)); echo json_encode(['ok' => (bool)$ok, 'file' => 'js/maps-custom.json', 'backed_up' => is_dir($backupDir)]);