gallery-edit.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 === 'uploads') {
  30. $mode = (string)($_POST['mode'] ?? '');
  31. if ($mode === 'enable' && empty($gallery['upload_key'])) {
  32. $gallery['upload_key'] = random_token(24);
  33. gallery_save($gallery);
  34. flash_set('Guest uploads enabled.');
  35. } elseif ($mode === 'regenerate') {
  36. $gallery['upload_key'] = random_token(24);
  37. gallery_save($gallery);
  38. flash_set('New upload link generated; the old link no longer works.');
  39. } elseif ($mode === 'disable') {
  40. $gallery['upload_key'] = null;
  41. gallery_save($gallery);
  42. flash_set('Guest uploads disabled.');
  43. }
  44. redirect('gallery-edit.php?g=' . rawurlencode($slug));
  45. }
  46. if ($action === 'delete-image') {
  47. $key = (string)($_POST['key'] ?? '');
  48. foreach ($gallery['images'] ?? [] as $i => $img) {
  49. if (($img['key'] ?? '') === $key) {
  50. s3_delete($img['key']);
  51. if (!empty($img['thumb'])) {
  52. s3_delete($img['thumb']);
  53. }
  54. array_splice($gallery['images'], $i, 1);
  55. gallery_save($gallery);
  56. flash_set('Image deleted.');
  57. break;
  58. }
  59. }
  60. redirect('gallery-edit.php?g=' . rawurlencode($slug));
  61. }
  62. }
  63. // Build the share link from the page the admin is currently on, not from config,
  64. // so it matches whatever host/path this app is actually served under.
  65. $scheme = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http';
  66. $host = $_SERVER['HTTP_HOST'] ?? 'localhost';
  67. // gallery.php lives one directory up from admin/ (cf. the "../gallery.php" link below).
  68. $basePath = str_replace('\\', '/', dirname(dirname($_SERVER['SCRIPT_NAME'] ?? '/admin/gallery-edit.php')));
  69. $basePath = rtrim($basePath, '/');
  70. $shareUrl = $scheme . '://' . $host . $basePath . '/gallery.php?g=' . rawurlencode($slug);
  71. // Guest upload link (only when a key is set). Same host/path derivation as above.
  72. $uploadUrl = !empty($gallery['upload_key'])
  73. ? $scheme . '://' . $host . $basePath . '/upload.php?g=' . rawurlencode($slug)
  74. . '&k=' . rawurlencode($gallery['upload_key'])
  75. : '';
  76. admin_header($gallery['title'], 'galleries');
  77. flash_render();
  78. ?>
  79. <h1><?= e($gallery['title']) ?></h1>
  80. <p class="help" style="margin-bottom:1.5rem">
  81. Share link: <a href="../gallery.php?g=<?= e(rawurlencode($slug)) ?>" target="_blank" rel="noopener"><?= e($shareUrl) ?></a>
  82. </p>
  83. <div class="card">
  84. <h2 style="margin-top:0">Upload images</h2>
  85. <div class="dropzone" id="dropzone"
  86. data-api="api.php"
  87. data-slug="<?= e($slug) ?>"
  88. data-csrf="<?= e(csrf_token()) ?>"
  89. data-thumb-size="<?= (int)config('uploads.thumb_size', 600) ?>"
  90. data-thumb-quality="<?= e((string)config('uploads.thumb_quality', 0.8)) ?>">
  91. Drop images here or click to select.<br>
  92. <small>Uploaded through the site to S3, in full resolution, unmodified. One file at a time.</small>
  93. </div>
  94. <input type="file" id="file-input" accept="image/*" multiple style="display:none">
  95. <div class="upload-list" id="upload-list"></div>
  96. </div>
  97. <div class="card">
  98. <h2 style="margin-top:0">Guest uploads</h2>
  99. <?php if ($uploadUrl !== ''): ?>
  100. <p class="help" style="margin-bottom:1rem">
  101. Share this link so guests can upload into this gallery without an admin
  102. account. It is key-protected and honors the gallery's password and
  103. expiry, if set.
  104. </p>
  105. <p style="margin-bottom:1rem">
  106. <a href="<?= e($uploadUrl) ?>" target="_blank" rel="noopener"><?= e($uploadUrl) ?></a>
  107. </p>
  108. <form method="post" style="display:inline"
  109. onsubmit="return confirm('Generate a new link? The current link will stop working.')">
  110. <?= csrf_field() ?>
  111. <input type="hidden" name="action" value="uploads">
  112. <input type="hidden" name="mode" value="regenerate">
  113. <button style="margin:0">Regenerate link</button>
  114. </form>
  115. <form method="post" style="display:inline"
  116. onsubmit="return confirm('Disable guest uploads? The link will stop working.')">
  117. <?= csrf_field() ?>
  118. <input type="hidden" name="action" value="uploads">
  119. <input type="hidden" name="mode" value="disable">
  120. <button class="btn-danger" style="margin:0">Disable</button>
  121. </form>
  122. <?php else: ?>
  123. <p class="help" style="margin-bottom:1rem">
  124. Guest uploads are disabled. Enable them to get a shareable link that
  125. lets people upload into this gallery without an admin account.
  126. </p>
  127. <form method="post">
  128. <?= csrf_field() ?>
  129. <input type="hidden" name="action" value="uploads">
  130. <input type="hidden" name="mode" value="enable">
  131. <button style="margin:0">Enable guest uploads</button>
  132. </form>
  133. <?php endif; ?>
  134. </div>
  135. <form method="post" class="card">
  136. <?= csrf_field() ?>
  137. <input type="hidden" name="action" value="settings">
  138. <h2 style="margin-top:0">Settings</h2>
  139. <label for="t">Title</label>
  140. <input type="text" id="t" name="title" value="<?= e($gallery['title']) ?>">
  141. <label for="ex">Expiry date (blank = never)</label>
  142. <input type="date" id="ex" name="expires_at" value="<?= e($gallery['expires_at'] ?? '') ?>">
  143. <label for="p">Set new password (blank = keep current)</label>
  144. <input type="text" id="p" name="password" autocomplete="off">
  145. <?php if (!empty($gallery['password_hash'])): ?>
  146. <p class="help"><label style="display:inline;text-transform:none;letter-spacing:0">
  147. <input type="checkbox" name="remove_password" value="1"> Remove password protection
  148. </label></p>
  149. <?php endif; ?>
  150. <button type="submit">Save settings</button>
  151. </form>
  152. <h2>Images (<span id="img-count"><?= count($gallery['images'] ?? []) ?></span>)</h2>
  153. <div class="thumb-row">
  154. <?php foreach ($gallery['images'] ?? [] as $img): ?>
  155. <figure>
  156. <img src="<?= e(s3_presign_get($img['thumb'] ?? $img['key'])) ?>" alt="" loading="lazy">
  157. <figcaption title="<?= e($img['name'] ?? '') ?>"><?= e($img['name'] ?? '') ?></figcaption>
  158. <form method="post" onsubmit="return confirm('Delete this image from S3?')">
  159. <?= csrf_field() ?>
  160. <input type="hidden" name="action" value="delete-image">
  161. <input type="hidden" name="key" value="<?= e($img['key']) ?>">
  162. <button>✕</button>
  163. </form>
  164. </figure>
  165. <?php endforeach; ?>
  166. </div>
  167. <script src="../assets/admin.js"></script>
  168. <?php admin_footer(); ?>