list.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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 hash from the sorted activity IDs
  30. $hash = hash('sha256', $idsStr);
  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. $hash = $existing;
  37. } else {
  38. // Store new hash
  39. $stmt = $pdo->prepare("INSERT INTO selection_hashes (hash, activity_ids) VALUES (?, ?)");
  40. $stmt->execute([$hash, $idsStr]);
  41. }
  42. // Redirect to the shareable packing list URL
  43. header("Location: packing.php?h=" . urlencode($hash));
  44. exit;