download-zip.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. declare(strict_types=1);
  3. use App\App\Bootstrap;
  4. use App\Admin\Auth;
  5. use App\Storage\JsonStore;
  6. require dirname(__DIR__) . '/src/autoload.php';
  7. Bootstrap::init();
  8. $auth = new Auth();
  9. $auth->requireLogin();
  10. if (!class_exists('ZipArchive')) {
  11. http_response_code(500);
  12. echo 'ZipArchive ist auf diesem Server nicht verfügbar.';
  13. exit;
  14. }
  15. $id = trim((string) ($_GET['id'] ?? ''));
  16. $store = new JsonStore();
  17. $submission = $store->getSubmissionByKey($id);
  18. if ($submission === null) {
  19. http_response_code(404);
  20. echo 'Antrag nicht gefunden.';
  21. exit;
  22. }
  23. $app = Bootstrap::config('app');
  24. $baseUploads = rtrim((string) $app['storage']['uploads'], '/');
  25. $zipPath = sys_get_temp_dir() . '/antrag_' . $id . '_' . bin2hex(random_bytes(4)) . '.zip';
  26. $zip = new ZipArchive();
  27. if ($zip->open($zipPath, ZipArchive::CREATE | ZipArchive::OVERWRITE) !== true) {
  28. http_response_code(500);
  29. echo 'ZIP konnte nicht erstellt werden.';
  30. exit;
  31. }
  32. foreach ((array) ($submission['uploads'] ?? []) as $field => $files) {
  33. foreach ((array) $files as $file) {
  34. if (!is_array($file)) {
  35. continue;
  36. }
  37. $relativePath = str_replace(['..', '\\'], '', (string) ($file['relative_path'] ?? ''));
  38. $fullPath = $baseUploads . '/' . ltrim($relativePath, '/');
  39. if (!is_file($fullPath)) {
  40. continue;
  41. }
  42. $name = (string) ($file['original_filename'] ?? basename($fullPath));
  43. $name = str_replace(["\r", "\n"], '', $name);
  44. $zipEntry = $field . '/' . $name;
  45. $suffix = 1;
  46. while ($zip->locateName($zipEntry) !== false) {
  47. $zipEntry = $field . '/' . $suffix . '_' . $name;
  48. $suffix++;
  49. }
  50. $zip->addFile($fullPath, $zipEntry);
  51. }
  52. }
  53. $zip->close();
  54. header('Content-Type: application/zip');
  55. header('Content-Length: ' . (string) filesize($zipPath));
  56. header('Content-Disposition: attachment; filename="antrag_' . $id . '.zip"');
  57. readfile($zipPath);
  58. unlink($zipPath);
  59. exit;