gallery-edit.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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 === 'downloads') {
  50. $gallery['downloads_enabled'] = !empty($_POST['enabled']);
  51. gallery_save($gallery);
  52. if ($gallery['downloads_enabled']) {
  53. // Queue the first build; the background worker picks it up once the
  54. // gallery has been quiet for archive.settle_seconds.
  55. archive_mark_dirty($slug, $gallery);
  56. flash_set('Downloads enabled. The archive will be built in the background.');
  57. } else {
  58. archive_delete($slug);
  59. flash_set('Downloads disabled and the archive removed.');
  60. }
  61. redirect('gallery-edit.php?g=' . rawurlencode($slug));
  62. }
  63. // Topic management. Assignment is not here: it runs through topics-api.php,
  64. // because drag-and-drop cannot reload the page after every drop.
  65. if (str_starts_with($action, 'topic-')) {
  66. $id = (string)($_POST['id'] ?? '');
  67. if ($action === 'topic-add') {
  68. $added = gallery_topic_add($slug, (string)($_POST['name'] ?? ''));
  69. flash_set(
  70. $added !== null ? 'Topic "' . $added['name'] . '" added.' : 'Give the topic a name.',
  71. $added !== null ? 'ok' : 'error'
  72. );
  73. } elseif ($action === 'topic-rename') {
  74. gallery_topic_rename($slug, $id, (string)($_POST['name'] ?? ''));
  75. flash_set('Topic renamed.');
  76. } elseif ($action === 'topic-delete') {
  77. // The images survive; they simply stop belonging to a topic and
  78. // reappear in the untopiced section at the top.
  79. gallery_topic_delete($slug, $id);
  80. flash_set('Topic removed. Its images are now without a topic.');
  81. } elseif ($action === 'topic-move') {
  82. gallery_topic_move($slug, $id, (string)($_POST['dir'] ?? ''));
  83. }
  84. redirect('gallery-edit.php?g=' . rawurlencode($slug));
  85. }
  86. if ($action === 'delete-image') {
  87. $key = (string)($_POST['key'] ?? '');
  88. foreach ($gallery['images'] ?? [] as $i => $img) {
  89. if (($img['key'] ?? '') === $key) {
  90. s3_delete($img['key']);
  91. if (!empty($img['thumb'])) {
  92. s3_delete($img['thumb']);
  93. }
  94. array_splice($gallery['images'], $i, 1);
  95. gallery_save($gallery);
  96. // The archive no longer matches the gallery's contents.
  97. archive_mark_dirty($slug, $gallery);
  98. flash_set('Image deleted.');
  99. break;
  100. }
  101. }
  102. redirect('gallery-edit.php?g=' . rawurlencode($slug));
  103. }
  104. }
  105. // Build the share link from the page the admin is currently on, not from config,
  106. // so it matches whatever host/path this app is actually served under.
  107. $scheme = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http';
  108. $host = $_SERVER['HTTP_HOST'] ?? 'localhost';
  109. // The site root is one directory up from admin/ (cf. the "../gallery/" link below).
  110. $basePath = str_replace('\\', '/', dirname(dirname($_SERVER['SCRIPT_NAME'] ?? '/admin/gallery-edit.php')));
  111. $basePath = rtrim($basePath, '/');
  112. $shareUrl = $scheme . '://' . $host . $basePath . '/gallery/?g=' . rawurlencode($slug);
  113. // Guest upload link (only when a key is set). Same host/path derivation as above.
  114. $uploadUrl = !empty($gallery['upload_key'])
  115. ? $scheme . '://' . $host . $basePath . '/upload.php?g=' . rawurlencode($slug)
  116. . '&k=' . rawurlencode($gallery['upload_key'])
  117. : '';
  118. // Topics split the gallery into sections; a gallery with none renders exactly
  119. // as it did before they existed.
  120. $topics = gallery_topics($gallery);
  121. admin_header($gallery['title'], 'galleries');
  122. flash_render();
  123. ?>
  124. <h1><?= e($gallery['title']) ?></h1>
  125. <p class="help" style="margin-bottom:1.5rem">
  126. Share link: <a href="../gallery/?g=<?= e(rawurlencode($slug)) ?>" target="_blank" rel="noopener"><?= e($shareUrl) ?></a>
  127. </p>
  128. <div class="card">
  129. <h2 style="margin-top:0">Upload images</h2>
  130. <?php if ($topics !== []): ?>
  131. <!-- Uploading straight into a topic; the alternative is assigning a few
  132. hundred photos one at a time after the fact. -->
  133. <label for="upload-topic">Upload into</label>
  134. <select id="upload-topic">
  135. <option value="">No topic</option>
  136. <?php foreach ($topics as $topic): ?>
  137. <option value="<?= e($topic['id']) ?>"><?= e($topic['name']) ?></option>
  138. <?php endforeach; ?>
  139. </select>
  140. <?php endif; ?>
  141. <div class="dropzone" id="dropzone"
  142. data-api="api.php"
  143. data-slug="<?= e($slug) ?>"
  144. data-csrf="<?= e(csrf_token()) ?>"
  145. data-thumb-size="<?= (int)config('uploads.thumb_size', 600) ?>"
  146. data-thumb-quality="<?= e((string)config('uploads.thumb_quality', 0.8)) ?>"
  147. data-concurrency="<?= (int)config('uploads.concurrency', 3) ?>"
  148. data-max-resolution="<?= (int)($gallery['max_resolution'] ?? 0) ?>"
  149. data-resize-quality="<?= e((string)config('uploads.resize_quality', 0.9)) ?>">
  150. Drop images here or click to select.<br>
  151. <small>
  152. <?php if (isset($gallery['max_resolution'])): ?>
  153. Uploaded through the site to S3, downscaled to <?= (int)$gallery['max_resolution'] ?> px on the longest edge.
  154. <?php else: ?>
  155. Uploaded through the site to S3, in full resolution, unmodified.
  156. <?php endif; ?>
  157. </small>
  158. </div>
  159. <input type="file" id="file-input" accept="image/*" multiple style="display:none">
  160. <div class="upload-list" id="upload-list"></div>
  161. </div>
  162. <div class="card">
  163. <h2 style="margin-top:0">Guest uploads</h2>
  164. <?php if ($uploadUrl !== ''): ?>
  165. <p class="help" style="margin-bottom:1rem">
  166. Share this link so guests can upload into this gallery without an admin
  167. account. It is key-protected and honors the gallery's password and
  168. expiry, if set.
  169. </p>
  170. <p style="margin-bottom:1rem">
  171. <a href="<?= e($uploadUrl) ?>" target="_blank" rel="noopener"><?= e($uploadUrl) ?></a>
  172. </p>
  173. <form method="post" style="display:inline"
  174. onsubmit="return confirm('Generate a new link? The current link will stop working.')">
  175. <?= csrf_field() ?>
  176. <input type="hidden" name="action" value="uploads">
  177. <input type="hidden" name="mode" value="regenerate">
  178. <button style="margin:0">Regenerate link</button>
  179. </form>
  180. <form method="post" style="display:inline"
  181. onsubmit="return confirm('Disable guest uploads? The link will stop working.')">
  182. <?= csrf_field() ?>
  183. <input type="hidden" name="action" value="uploads">
  184. <input type="hidden" name="mode" value="disable">
  185. <button class="btn-danger" style="margin:0">Disable</button>
  186. </form>
  187. <?php else: ?>
  188. <p class="help" style="margin-bottom:1rem">
  189. Guest uploads are disabled. Enable them to get a shareable link that
  190. lets people upload into this gallery without an admin account.
  191. </p>
  192. <form method="post">
  193. <?= csrf_field() ?>
  194. <input type="hidden" name="action" value="uploads">
  195. <input type="hidden" name="mode" value="enable">
  196. <button style="margin:0">Enable guest uploads</button>
  197. </form>
  198. <?php endif; ?>
  199. </div>
  200. <?php $status = archive_status($gallery); ?>
  201. <div class="card" id="archive-card"
  202. data-api="archive-api.php"
  203. data-slug="<?= e($slug) ?>"
  204. data-csrf="<?= e(csrf_token()) ?>">
  205. <h2 style="margin-top:0">Download all</h2>
  206. <p class="help" style="margin-bottom:1rem">
  207. Offers visitors a single ZIP of every photo. It is assembled once and
  208. stored on S3, so the download itself never runs through this webhost —
  209. and it is rebuilt automatically whenever images are added or removed.
  210. While a rebuild is pending the button on the gallery is disabled, so
  211. nobody receives an archive that is missing the newest photos.
  212. </p>
  213. <form method="post" style="margin-bottom:1rem">
  214. <?= csrf_field() ?>
  215. <input type="hidden" name="action" value="downloads">
  216. <input type="hidden" name="enabled" value="<?= empty($gallery['downloads_enabled']) ? '1' : '0' ?>">
  217. <?php if (empty($gallery['downloads_enabled'])): ?>
  218. <button style="margin:0">Enable downloads</button>
  219. <?php else: ?>
  220. <button class="btn-danger" style="margin:0"
  221. onclick="return confirm('Disable downloads and delete the archive from S3?')">
  222. Disable downloads
  223. </button>
  224. <?php endif; ?>
  225. </form>
  226. <?php if (!empty($gallery['downloads_enabled'])): ?>
  227. <p id="archive-status" class="help" style="margin-bottom:.6rem">
  228. <?php if ($status['archive'] && !$status['stale']): ?>
  229. Ready · <?= e(human_bytes((int)$status['archive']['size'])) ?>
  230. · <?= (int)$status['archive']['count'] ?> photos
  231. · built <?= e($status['archive']['built_at']) ?>
  232. <?php elseif ($status['building']): ?>
  233. Building — <?= (int)$status['done'] ?> of <?= (int)$status['total'] ?> photos done.
  234. <?php elseif ($status['queued'] && $status['due_in'] > 0): ?>
  235. Queued — the rebuild starts in about <?= (int)ceil($status['due_in'] / 60) ?> min.
  236. <?php elseif ($status['queued']): ?>
  237. Queued — the rebuild starts shortly.
  238. <?php elseif ($status['archive']): ?>
  239. Out of date — the archive does not match the current images.
  240. <?php else: ?>
  241. No archive yet.
  242. <?php endif; ?>
  243. </p>
  244. <div id="archive-bar" class="archive-bar" hidden><span></span></div>
  245. <button id="archive-build" style="margin:0"
  246. <?= $status['building'] ? 'data-resume="1"' : '' ?>>
  247. <?= $status['building'] ? 'Resume build' : ($status['archive'] ? 'Rebuild now' : 'Build now') ?>
  248. </button>
  249. <button id="archive-cancel" class="btn-ghost" style="margin:0" hidden>Cancel</button>
  250. <script src="../assets/archive.js" defer></script>
  251. <?php endif; ?>
  252. </div>
  253. <form method="post" class="card">
  254. <?= csrf_field() ?>
  255. <input type="hidden" name="action" value="settings">
  256. <h2 style="margin-top:0">Settings</h2>
  257. <label for="t">Title</label>
  258. <input type="text" id="t" name="title" value="<?= e($gallery['title']) ?>">
  259. <label for="ex">Expiry date (blank = never)</label>
  260. <input type="date" id="ex" name="expires_at" value="<?= e($gallery['expires_at'] ?? '') ?>">
  261. <?php resolution_field(isset($gallery['max_resolution']) ? (int)$gallery['max_resolution'] : null) ?>
  262. <label for="p">Set new password (blank = keep current)</label>
  263. <input type="text" id="p" name="password" autocomplete="off">
  264. <?php if (!empty($gallery['password_hash'])): ?>
  265. <p class="help"><label style="display:inline;text-transform:none;letter-spacing:0">
  266. <input type="checkbox" name="remove_password" value="1"> Remove password protection
  267. </label></p>
  268. <?php endif; ?>
  269. <button type="submit">Save settings</button>
  270. </form>
  271. <div class="card">
  272. <h2 style="margin-top:0">Topics</h2>
  273. <p class="help" style="margin-bottom:1rem">
  274. Topics split the gallery into sections — the days of a trip, the stops of
  275. a shoot. They are optional: images without a topic always come first, and
  276. a gallery with no topics looks exactly as it always did. Guests upload
  277. into "<?= e(GALLERY_GUEST_TOPIC_NAME) ?>", which appears by itself the
  278. first time someone uses the guest link.
  279. </p>
  280. <?php if ($topics !== []): ?>
  281. <table style="margin-bottom:1rem">
  282. <tr><th>Topic</th><th style="width:180px">Order</th><th style="width:90px"></th></tr>
  283. <?php foreach ($topics as $i => $topic): ?>
  284. <tr>
  285. <td>
  286. <form method="post" style="display:flex;gap:.5rem;align-items:center"><?= csrf_field() ?>
  287. <input type="hidden" name="action" value="topic-rename">
  288. <input type="hidden" name="id" value="<?= e($topic['id']) ?>">
  289. <input type="text" name="name" value="<?= e($topic['name']) ?>"
  290. maxlength="80" style="margin:0;flex:1">
  291. <button class="btn-ghost" style="margin:0;padding:.3rem .7rem">Rename</button>
  292. </form>
  293. </td>
  294. <td>
  295. <form method="post" style="display:inline"><?= csrf_field() ?>
  296. <input type="hidden" name="action" value="topic-move">
  297. <input type="hidden" name="id" value="<?= e($topic['id']) ?>">
  298. <input type="hidden" name="dir" value="up">
  299. <button class="btn-ghost" style="margin:0;padding:.3rem .7rem" <?= $i === 0 ? 'disabled' : '' ?>>↑</button>
  300. </form>
  301. <form method="post" style="display:inline"><?= csrf_field() ?>
  302. <input type="hidden" name="action" value="topic-move">
  303. <input type="hidden" name="id" value="<?= e($topic['id']) ?>">
  304. <input type="hidden" name="dir" value="down">
  305. <button class="btn-ghost" style="margin:0;padding:.3rem .7rem" <?= $i === count($topics) - 1 ? 'disabled' : '' ?>>↓</button>
  306. </form>
  307. </td>
  308. <td>
  309. <form method="post" style="display:inline"
  310. onsubmit="return confirm('Remove this topic? Its images are kept and move back to no topic.')"><?= csrf_field() ?>
  311. <input type="hidden" name="action" value="topic-delete">
  312. <input type="hidden" name="id" value="<?= e($topic['id']) ?>">
  313. <button class="btn-danger" style="margin:0;padding:.3rem .7rem">Delete</button>
  314. </form>
  315. </td>
  316. </tr>
  317. <?php endforeach; ?>
  318. </table>
  319. <?php endif; ?>
  320. <form method="post"><?= csrf_field() ?>
  321. <input type="hidden" name="action" value="topic-add">
  322. <label for="topic-name">New topic</label>
  323. <input type="text" id="topic-name" name="name" maxlength="80" placeholder="e.g. Day 1 – Reykjavík">
  324. <button type="submit">Add topic</button>
  325. </form>
  326. </div>
  327. <h2>Images (<span id="img-count"><?= count($gallery['images'] ?? []) ?></span>)</h2>
  328. <?php
  329. // Grouped for editing, empty topics included: an empty section is still a drop
  330. // target, and without it a new topic could never receive its first image.
  331. $groups = gallery_groups($gallery, true);
  332. // Position in the flat images array, which is what decides the order inside a
  333. // section. Handed to the browser so a dragged image can be dropped into the
  334. // place the page will show it in after the next reload, rather than at the end.
  335. $order = array_flip(array_column($gallery['images'] ?? [], 'key'));
  336. ?>
  337. <div class="topic-blocks" id="topic-blocks"
  338. data-api="topics-api.php"
  339. data-slug="<?= e($slug) ?>"
  340. data-csrf="<?= e(csrf_token()) ?>">
  341. <?php foreach ($groups as $group): $topic = $group['topic']; ?>
  342. <section class="topic-block" data-topic="<?= e($topic['id'] ?? '') ?>">
  343. <?php if ($topics !== []): ?>
  344. <h3 class="topic-block-head">
  345. <?= $topic === null ? 'No topic' : e($topic['name']) ?>
  346. <span class="topic-count"><?= count($group['images']) ?></span>
  347. </h3>
  348. <?php endif; ?>
  349. <div class="thumb-row">
  350. <?php foreach ($group['images'] as $img): ?>
  351. <figure draggable="true" data-key="<?= e($img['key']) ?>" data-order="<?= (int)($order[$img['key']] ?? 0) ?>">
  352. <img src="<?= e(s3_presign_get($img['thumb'] ?? $img['key'])) ?>" alt="" loading="lazy" draggable="false">
  353. <figcaption title="<?= e($img['name'] ?? '') ?>"><?= e($img['name'] ?? '') ?></figcaption>
  354. <button type="button" class="thumb-menu-btn" aria-haspopup="true" aria-expanded="false"
  355. title="Assign topic or delete">⋯</button>
  356. <div class="thumb-menu" hidden>
  357. <?php if ($topics !== []): ?>
  358. <p class="thumb-menu-label">Assign topic</p>
  359. <button type="button" class="topic-pick<?= $topic === null ? ' is-current' : '' ?>" data-topic="">No topic</button>
  360. <?php foreach ($topics as $t): ?>
  361. <button type="button" class="topic-pick<?= ($topic['id'] ?? null) === $t['id'] ? ' is-current' : '' ?>"
  362. data-topic="<?= e($t['id']) ?>"><?= e($t['name']) ?></button>
  363. <?php endforeach; ?>
  364. <?php else: ?>
  365. <p class="thumb-menu-label">Add a topic above to sort images into sections.</p>
  366. <?php endif; ?>
  367. <!-- Deletion stays a real form post: same server path, same
  368. confirmation, and it keeps working without the menu JS. -->
  369. <form method="post" onsubmit="return confirm('Delete this image from S3?')">
  370. <?= csrf_field() ?>
  371. <input type="hidden" name="action" value="delete-image">
  372. <input type="hidden" name="key" value="<?= e($img['key']) ?>">
  373. <button class="thumb-menu-danger">Delete</button>
  374. </form>
  375. </div>
  376. </figure>
  377. <?php endforeach; ?>
  378. </div>
  379. </section>
  380. <?php endforeach; ?>
  381. </div>
  382. <script src="../assets/admin.js"></script>
  383. <script src="../assets/topics.js"></script>
  384. <?php admin_footer(); ?>