upload-api.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. /**
  3. * Public guest upload endpoint used by the browser-side uploader (assets/admin.js)
  4. * on upload.php. Same one-multipart-POST-per-image contract as admin/api.php, but
  5. * authenticated by the per-gallery upload key instead of an admin session.
  6. *
  7. * Fields: slug, key, original (required), thumb, batch, seq (optional; batch and
  8. * seq carry the file's place in the visitor's selection, so parallel uploads are
  9. * stored in the order they were picked). Access requires the
  10. * gallery to have guest uploads enabled, the key to match, the gallery to be
  11. * unexpired, and — if the gallery has a password — the visitor to have unlocked
  12. * it in this session (via upload.php). Any failure returns a uniform 403.
  13. *
  14. * Uploads are image-only here, so a public link cannot store arbitrary files.
  15. */
  16. require __DIR__ . '/app/bootstrap.php';
  17. session_boot();
  18. if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
  19. json_response(['error' => 'POST only'], 405);
  20. }
  21. csrf_verify();
  22. // Take the session-held facts now, then release the session file before the slow
  23. // S3 leg. PHP locks it exclusively for the whole request, so without this every
  24. // parallel upload from the browser would queue behind the previous one and the
  25. // uploader would be serial again no matter how many requests it starts.
  26. $unlockedGalleries = (array)($_SESSION['gallery_unlocked'] ?? []);
  27. session_write_close();
  28. // One image per request; a single file may still be large, so lift the time cap.
  29. @set_time_limit(0);
  30. // When a request body exceeds post_max_size, PHP discards $_POST and $_FILES
  31. // entirely — surface that as a clear 413 instead of a misleading "no file".
  32. if ((int)($_SERVER['CONTENT_LENGTH'] ?? 0) > 0 && !$_POST && !$_FILES) {
  33. json_response(['error' => 'Upload exceeds the server post_max_size limit'], 413);
  34. }
  35. $gallery = gallery_load((string)($_POST['slug'] ?? ''));
  36. // Uniform 403 for every access failure (no key, wrong key, expired, locked) so
  37. // the endpoint reveals nothing a guest shouldn't already know from the link.
  38. $authorized = $gallery !== null
  39. && !empty($gallery['upload_key'])
  40. && hash_equals((string)$gallery['upload_key'], (string)($_POST['key'] ?? ''))
  41. && !gallery_is_expired($gallery)
  42. && (empty($gallery['password_hash']) || !empty($unlockedGalleries[$gallery['slug']]));
  43. if (!$authorized) {
  44. json_response(['error' => 'Not authorized'], 403);
  45. }
  46. // Image-only: a public link must not be usable to store arbitrary file types.
  47. [$status, $payload] = gallery_store_s3_upload(
  48. $gallery,
  49. $_FILES['original'] ?? null,
  50. $_FILES['thumb'] ?? null,
  51. true,
  52. $_POST
  53. );
  54. json_response($payload, $status);