cleanup.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #!/usr/bin/env php
  2. <?php
  3. declare(strict_types=1);
  4. use App\App\Bootstrap;
  5. use App\Storage\FileSystem;
  6. if (PHP_SAPI !== 'cli') {
  7. http_response_code(403);
  8. exit('Forbidden');
  9. }
  10. require dirname(__DIR__) . '/src/autoload.php';
  11. Bootstrap::init(false);
  12. $app = Bootstrap::config('app');
  13. $draftDir = rtrim((string) $app['storage']['drafts'], '/');
  14. $submissionDir = rtrim((string) $app['storage']['submissions'], '/');
  15. $uploadDir = rtrim((string) $app['storage']['uploads'], '/');
  16. $draftDays = (int) ($app['retention']['draft_days'] ?? 14);
  17. $submissionDays = (int) ($app['retention']['submission_days'] ?? 90);
  18. $now = time();
  19. $deletedDrafts = 0;
  20. $deletedSubmissions = 0;
  21. $draftFiles = glob($draftDir . '/*.json') ?: [];
  22. foreach ($draftFiles as $file) {
  23. $raw = file_get_contents($file);
  24. $data = is_string($raw) ? json_decode($raw, true) : null;
  25. $updatedAt = strtotime((string) ($data['updated_at'] ?? ''));
  26. if ($updatedAt > 0 && ($now - $updatedAt) > ($draftDays * 86400)) {
  27. unlink($file);
  28. $deletedDrafts++;
  29. }
  30. }
  31. $submissionFiles = glob($submissionDir . '/*.json') ?: [];
  32. foreach ($submissionFiles as $file) {
  33. $raw = file_get_contents($file);
  34. $data = is_string($raw) ? json_decode($raw, true) : null;
  35. $submittedAt = strtotime((string) ($data['submitted_at'] ?? ''));
  36. if ($submittedAt > 0 && ($now - $submittedAt) > ($submissionDays * 86400)) {
  37. $applicationKey = (string) ($data['application_key'] ?? pathinfo($file, PATHINFO_FILENAME));
  38. unlink($file);
  39. FileSystem::removeTree($uploadDir . '/' . $applicationKey);
  40. $deletedSubmissions++;
  41. }
  42. }
  43. $message = sprintf(
  44. 'Cleanup abgeschlossen: drafts=%d, submissions=%d',
  45. $deletedDrafts,
  46. $deletedSubmissions
  47. );
  48. Bootstrap::log('cleanup', $message);
  49. if (PHP_SAPI === 'cli') {
  50. echo $message . PHP_EOL;
  51. }