gallery-edit.php 8.5 KB

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