<?php // Włączanie raportowania błędów dla debugowania error_reporting(E_ALL); ini_set('display_errors', 1); if ($_SERVER['REQUEST_METHOD'] === 'POST') { try { $id = bin2hex(random_bytes(5)); // Generowanie losowego ID $title = isset($_POST['title']) && !empty($_POST['title']) ? htmlspecialchars($_POST['title']) : 'Brak tytułu'; $content = htmlspecialchars($_POST['content']); // Katalog docelowy $baseDirectory = realpath(__DIR__ . '/../n'); if ($baseDirectory === false) { throw new Exception("Błąd: Katalog bazowy '../n' nie istnieje."); } $directory = $baseDirectory . "/$id"; // Tworzenie katalogu if (!is_dir($directory)) { if (!mkdir($directory, 0777, true)) { throw new Exception("Nie udało się utworzyć katalogu: $directory"); } } $filePath = "$directory/index.php"; $noteContent = " <!DOCTYPE html> <html lang='pl'> <head><meta charset='UTF-8'><title>$title</title></head> <body> <h1>$title</h1> <p>$content</p> </body> </html>"; // Tworzenie pliku if (file_put_contents($filePath, $noteContent) === false) { throw new Exception("Nie udało się zapisać pliku: $filePath"); } // Zwracanie odpowiedzi JSON echo json_encode(['id' => $id, 'status' => 'success']); exit; } catch (Exception $e) { // Obsługa błędów http_response_code(500); echo json_encode(['status' => 'error', 'message' => $e->getMessage()]); exit; } } else { // Obsługa metod innych niż POST http_response_code(405); echo json_encode(['status' => 'error', 'message' => 'Metoda niedozwolona.']); exit; }