logCsvExport(); $filename = 'xamxam-export-' . date('Y-m-d') . '.csv'; header('Content-Type: text/csv; charset=UTF-8'); header('Content-Disposition: attachment; filename="' . $filename . '"'); header('Cache-Control: no-cache, must-revalidate'); echo "\xEF\xBB\xBF"; $out = fopen('php://output', 'w'); fputcsv($out, ExportController::CSV_HEADERS, ',', '"', ''); $rows = $controller->exportAllTheses(); foreach ($rows as $csvLine) { fputcsv($out, $csvLine, ',', '"', ''); } fclose($out); // If only CSV, we're done if (!$doFiles) { exit; } } if ($doFiles) { try { $zipPath = $controller->createExportZip(); $fileSize = filesize($zipPath); $fileCount = count($controller->getAllThesisFiles()); AdminLogger::make()->logFilesExport($fileCount, (int)$fileSize); $filename = 'xamxam-files-' . date('Y-m-d') . '.zip'; header('Content-Type: application/zip'); header('Content-Disposition: attachment; filename="' . $filename . '"'); header('Content-Length: ' . $fileSize); header('Cache-Control: no-cache, must-revalidate'); readfile($zipPath); @unlink($zipPath); } catch (Exception $e) { ErrorHandler::log('export', $e); http_response_code(500); exit('Erreur lors de la création de l\'archive : ' . htmlspecialchars(ErrorHandler::userMessage($e))); } } if ($doDb) { $dbPath = $controller->getDatabasePath(); if (!file_exists($dbPath)) { http_response_code(500); exit('Base de données introuvable.'); } $filename = 'xamxam-db-' . date('Y-m-d') . '.sqlite'; header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename="' . $filename . '"'); header('Content-Length: ' . filesize($dbPath)); header('Cache-Control: no-cache, must-revalidate'); AdminLogger::make()->logDbExport(); readfile($dbPath); } exit;