'Non autorisé'])); } $storyId = $_GET['storyId'] ?? ''; $chapterId = $_GET['chapterId'] ?? ''; if (!$storyId || !$chapterId) { http_response_code(400); exit(json_encode(['error' => 'Paramètres manquants'])); } try { $story = Stories::get($storyId); if (!$story) { throw new Exception('Roman non trouvé'); } $chapter = null; foreach ($story['chapters'] as $ch) { if ($ch['id'] === $chapterId) { $chapter = $ch; break; } } if (!$chapter) { throw new Exception('Chapitre non trouvé'); } // Vérification et correction de la structure du chapitre if (!isset($chapter['title'])) { $chapter['title'] = ''; } // Conversion du contenu HTML en format Delta de Quill si nécessaire if (!isset($chapter['content'])) { $chapter['content'] = '{"ops":[{"insert":"\n"}]}'; } else { // Si le contenu est une chaîne HTML if (is_string($chapter['content']) && !isJson($chapter['content'])) { // On envoie le HTML brut, Quill le convertira côté client $chapter['html'] = $chapter['content']; $chapter['content'] = null; } } echo json_encode($chapter); } catch (Exception $e) { error_log('Erreur dans get-chapter.php: ' . $e->getMessage()); http_response_code(500); echo json_encode([ 'error' => $e->getMessage(), 'details' => [ 'storyId' => $storyId, 'chapterId' => $chapterId, 'file' => __FILE__, 'line' => __LINE__ ] ]); } function isJson($string) { json_decode($string); return json_last_error() === JSON_ERROR_NONE; }