gallery-edit.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /**
  3. * Per-gallery editor: settings, share link, proxied bulk uploader
  4. * (browser → webhost → S3), and image removal.
  5. */
  6. require dirname(__DIR__) . '/app/bootstrap.php';
  7. auth_require();
  8. $gallery = gallery_load((string)($_GET['g'] ?? ''));
  9. if ($gallery === null) {
  10. flash_set('Gallery not found.', 'error');
  11. redirect('galleries.php');
  12. }
  13. $slug = $gallery['slug'];
  14. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  15. csrf_verify();
  16. $action = $_POST['action'] ?? '';
  17. if ($action === 'settings') {
  18. $gallery['title'] = trim((string)($_POST['title'] ?? '')) ?: $gallery['title'];
  19. $gallery['expires_at'] = trim((string)($_POST['expires_at'] ?? '')) ?: null;
  20. if (!empty($_POST['remove_password'])) {
  21. $gallery['password_hash'] = null;
  22. } elseif (($pw = (string)($_POST['password'] ?? '')) !== '') {
  23. $gallery['password_hash'] = password_hash($pw, PASSWORD_DEFAULT);
  24. }
  25. gallery_save($gallery);
  26. flash_set('Gallery settings saved.');
  27. redirect('gallery-edit.php?g=' . rawurlencode($slug));
  28. }
  29. if ($action === 'delete-image') {
  30. $key = (string)($_POST['key'] ?? '');
  31. foreach ($gallery['images'] ?? [] as $i => $img) {
  32. if (($img['key'] ?? '') === $key) {
  33. s3_delete($img['key']);
  34. if (!empty($img['thumb'])) {
  35. s3_delete($img['thumb']);
  36. }
  37. array_splice($gallery['images'], $i, 1);
  38. gallery_save($gallery);
  39. flash_set('Image deleted.');
  40. break;
  41. }
  42. }
  43. redirect('gallery-edit.php?g=' . rawurlencode($slug));
  44. }
  45. }
  46. // Build the share link from the page the admin is currently on, not from config,
  47. // so it matches whatever host/path this app is actually served under.
  48. $scheme = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http';
  49. $host = $_SERVER['HTTP_HOST'] ?? 'localhost';
  50. // gallery.php lives one directory up from admin/ (cf. the "../gallery.php" link below).
  51. $basePath = str_replace('\\', '/', dirname(dirname($_SERVER['SCRIPT_NAME'] ?? '/admin/gallery-edit.php')));
  52. $basePath = rtrim($basePath, '/');
  53. $shareUrl = $scheme . '://' . $host . $basePath . '/gallery.php?g=' . rawurlencode($slug);
  54. admin_header($gallery['title'], 'galleries');
  55. flash_render();
  56. ?>
  57. <h1><?= e($gallery['title']) ?></h1>
  58. <p class="help" style="margin-bottom:1.5rem">
  59. Share link: <a href="../gallery.php?g=<?= e(rawurlencode($slug)) ?>" target="_blank" rel="noopener"><?= e($shareUrl) ?></a>
  60. </p>
  61. <div class="card">
  62. <h2 style="margin-top:0">Upload images</h2>
  63. <div class="dropzone" id="dropzone"
  64. data-api="api.php"
  65. data-slug="<?= e($slug) ?>"
  66. data-csrf="<?= e(csrf_token()) ?>"
  67. data-thumb-size="<?= (int)config('uploads.thumb_size', 600) ?>"
  68. data-thumb-quality="<?= e((string)config('uploads.thumb_quality', 0.8)) ?>">
  69. Drop images here or click to select.<br>
  70. <small>Uploaded through the site to S3, in full resolution, unmodified. One file at a time.</small>
  71. </div>
  72. <input type="file" id="file-input" accept="image/*" multiple style="display:none">
  73. <div class="upload-list" id="upload-list"></div>
  74. </div>
  75. <form method="post" class="card">
  76. <?= csrf_field() ?>
  77. <input type="hidden" name="action" value="settings">
  78. <h2 style="margin-top:0">Settings</h2>
  79. <label for="t">Title</label>
  80. <input type="text" id="t" name="title" value="<?= e($gallery['title']) ?>">
  81. <label for="ex">Expiry date (blank = never)</label>
  82. <input type="date" id="ex" name="expires_at" value="<?= e($gallery['expires_at'] ?? '') ?>">
  83. <label for="p">Set new password (blank = keep current)</label>
  84. <input type="text" id="p" name="password" autocomplete="off">
  85. <?php if (!empty($gallery['password_hash'])): ?>
  86. <p class="help"><label style="display:inline;text-transform:none;letter-spacing:0">
  87. <input type="checkbox" name="remove_password" value="1"> Remove password protection
  88. </label></p>
  89. <?php endif; ?>
  90. <button type="submit">Save settings</button>
  91. </form>
  92. <h2>Images (<span id="img-count"><?= count($gallery['images'] ?? []) ?></span>)</h2>
  93. <div class="thumb-row">
  94. <?php foreach ($gallery['images'] ?? [] as $img): ?>
  95. <figure>
  96. <img src="<?= e(s3_presign_get($img['thumb'] ?? $img['key'])) ?>" alt="" loading="lazy">
  97. <figcaption title="<?= e($img['name'] ?? '') ?>"><?= e($img['name'] ?? '') ?></figcaption>
  98. <form method="post" onsubmit="return confirm('Delete this image from S3?')">
  99. <?= csrf_field() ?>
  100. <input type="hidden" name="action" value="delete-image">
  101. <input type="hidden" name="key" value="<?= e($img['key']) ?>">
  102. <button>✕</button>
  103. </form>
  104. </figure>
  105. <?php endforeach; ?>
  106. </div>
  107. <script src="../assets/admin.js"></script>
  108. <?php admin_footer(); ?>