gallery-edit.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. data-concurrency="<?= (int)config('uploads.concurrency', 3) ?>">
  92. Drop images here or click to select.<br>
  93. <small>Uploaded through the site to S3, in full resolution, unmodified.</small>
  94. </div>
  95. <input type="file" id="file-input" accept="image/*" multiple style="display:none">
  96. <div class="upload-list" id="upload-list"></div>
  97. </div>
  98. <div class="card">
  99. <h2 style="margin-top:0">Guest uploads</h2>
  100. <?php if ($uploadUrl !== ''): ?>
  101. <p class="help" style="margin-bottom:1rem">
  102. Share this link so guests can upload into this gallery without an admin
  103. account. It is key-protected and honors the gallery's password and
  104. expiry, if set.
  105. </p>
  106. <p style="margin-bottom:1rem">
  107. <a href="<?= e($uploadUrl) ?>" target="_blank" rel="noopener"><?= e($uploadUrl) ?></a>
  108. </p>
  109. <form method="post" style="display:inline"
  110. onsubmit="return confirm('Generate a new link? The current link will stop working.')">
  111. <?= csrf_field() ?>
  112. <input type="hidden" name="action" value="uploads">
  113. <input type="hidden" name="mode" value="regenerate">
  114. <button style="margin:0">Regenerate link</button>
  115. </form>
  116. <form method="post" style="display:inline"
  117. onsubmit="return confirm('Disable guest uploads? The link will stop working.')">
  118. <?= csrf_field() ?>
  119. <input type="hidden" name="action" value="uploads">
  120. <input type="hidden" name="mode" value="disable">
  121. <button class="btn-danger" style="margin:0">Disable</button>
  122. </form>
  123. <?php else: ?>
  124. <p class="help" style="margin-bottom:1rem">
  125. Guest uploads are disabled. Enable them to get a shareable link that
  126. lets people upload into this gallery without an admin account.
  127. </p>
  128. <form method="post">
  129. <?= csrf_field() ?>
  130. <input type="hidden" name="action" value="uploads">
  131. <input type="hidden" name="mode" value="enable">
  132. <button style="margin:0">Enable guest uploads</button>
  133. </form>
  134. <?php endif; ?>
  135. </div>
  136. <form method="post" class="card">
  137. <?= csrf_field() ?>
  138. <input type="hidden" name="action" value="settings">
  139. <h2 style="margin-top:0">Settings</h2>
  140. <label for="t">Title</label>
  141. <input type="text" id="t" name="title" value="<?= e($gallery['title']) ?>">
  142. <label for="ex">Expiry date (blank = never)</label>
  143. <input type="date" id="ex" name="expires_at" value="<?= e($gallery['expires_at'] ?? '') ?>">
  144. <label for="p">Set new password (blank = keep current)</label>
  145. <input type="text" id="p" name="password" autocomplete="off">
  146. <?php if (!empty($gallery['password_hash'])): ?>
  147. <p class="help"><label style="display:inline;text-transform:none;letter-spacing:0">
  148. <input type="checkbox" name="remove_password" value="1"> Remove password protection
  149. </label></p>
  150. <?php endif; ?>
  151. <button type="submit">Save settings</button>
  152. </form>
  153. <h2>Images (<span id="img-count"><?= count($gallery['images'] ?? []) ?></span>)</h2>
  154. <div class="thumb-row">
  155. <?php foreach ($gallery['images'] ?? [] as $img): ?>
  156. <figure>
  157. <img src="<?= e(s3_presign_get($img['thumb'] ?? $img['key'])) ?>" alt="" loading="lazy">
  158. <figcaption title="<?= e($img['name'] ?? '') ?>"><?= e($img['name'] ?? '') ?></figcaption>
  159. <form method="post" onsubmit="return confirm('Delete this image from S3?')">
  160. <?= csrf_field() ?>
  161. <input type="hidden" name="action" value="delete-image">
  162. <input type="hidden" name="key" value="<?= e($img['key']) ?>">
  163. <button>✕</button>
  164. </form>
  165. </figure>
  166. <?php endforeach; ?>
  167. </div>
  168. <script src="../assets/admin.js"></script>
  169. <?php admin_footer(); ?>