list.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. // list.php – receives selected activity IDs, creates/reuses a hash, redirects to packing.php
  3. require_once __DIR__ . '/db.php';
  4. $pdo = get_pdo();
  5. // Sanitize input
  6. $raw = isset($_GET['aID']) && is_array($_GET['aID']) ? $_GET['aID'] : [];
  7. $ids = [];
  8. foreach ($raw as $v) {
  9. $v = (int)$v;
  10. if ($v > 0) $ids[] = $v;
  11. }
  12. $ids = array_unique($ids);
  13. sort($ids);
  14. if (empty($ids)) {
  15. header('Location: index.php?error=no_selection');
  16. exit;
  17. }
  18. // Validate IDs exist in DB
  19. $placeholders = implode(',', array_fill(0, count($ids), '?'));
  20. $stmt = $pdo->prepare("SELECT activityID FROM activities WHERE activityID IN ($placeholders)");
  21. $stmt->execute($ids);
  22. $validIds = $stmt->fetchAll(PDO::FETCH_COLUMN);
  23. if (empty($validIds)) {
  24. header('Location: index.php?error=invalid_selection');
  25. exit;
  26. }
  27. sort($validIds);
  28. $idsStr = implode(',', $validIds);
  29. // Generate deterministic 5-character hash from the sorted activity IDs
  30. $hash = substr(hash('sha256', $idsStr), 0, 5);
  31. // Check if this exact selection already has a hash stored
  32. $stmt = $pdo->prepare("SELECT hash FROM selection_hashes WHERE activity_ids = ?");
  33. $stmt->execute([$idsStr]);
  34. $existing = $stmt->fetchColumn();
  35. if ($existing) {
  36. if ($existing !== $hash) {
  37. // Overwrite old (long) hash with the new short hash
  38. $stmt = $pdo->prepare("UPDATE selection_hashes SET hash = ? WHERE activity_ids = ?");
  39. $stmt->execute([$hash, $idsStr]);
  40. }
  41. } else {
  42. // Store new hash
  43. $stmt = $pdo->prepare("INSERT INTO selection_hashes (hash, activity_ids) VALUES (?, ?)");
  44. $stmt->execute([$hash, $idsStr]);
  45. }
  46. // Redirect to the shareable packing list URL
  47. header("Location: packing.php?h=" . urlencode($hash));
  48. exit;