| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- <?php
- // list.php – receives selected activity IDs, creates/reuses a hash, redirects to packing.php
- require_once __DIR__ . '/db.php';
- $pdo = get_pdo();
- // Sanitize input
- $raw = isset($_GET['aID']) && is_array($_GET['aID']) ? $_GET['aID'] : [];
- $ids = [];
- foreach ($raw as $v) {
- $v = (int)$v;
- if ($v > 0) $ids[] = $v;
- }
- $ids = array_unique($ids);
- sort($ids);
- if (empty($ids)) {
- header('Location: index.php?error=no_selection');
- exit;
- }
- // Validate IDs exist in DB
- $placeholders = implode(',', array_fill(0, count($ids), '?'));
- $stmt = $pdo->prepare("SELECT activityID FROM activities WHERE activityID IN ($placeholders)");
- $stmt->execute($ids);
- $validIds = $stmt->fetchAll(PDO::FETCH_COLUMN);
- if (empty($validIds)) {
- header('Location: index.php?error=invalid_selection');
- exit;
- }
- sort($validIds);
- $idsStr = implode(',', $validIds);
- // Generate deterministic 5-character hash from the sorted activity IDs
- $hash = substr(hash('sha256', $idsStr), 0, 5);
- // Check if this exact selection already has a hash stored
- $stmt = $pdo->prepare("SELECT hash FROM selection_hashes WHERE activity_ids = ?");
- $stmt->execute([$idsStr]);
- $existing = $stmt->fetchColumn();
- if ($existing) {
- if ($existing !== $hash) {
- // Overwrite old (long) hash with the new short hash
- $stmt = $pdo->prepare("UPDATE selection_hashes SET hash = ? WHERE activity_ids = ?");
- $stmt->execute([$hash, $idsStr]);
- }
- } else {
- // Store new hash
- $stmt = $pdo->prepare("INSERT INTO selection_hashes (hash, activity_ids) VALUES (?, ?)");
- $stmt->execute([$hash, $idsStr]);
- }
- // Redirect to the shareable packing list URL
- header("Location: packing.php?h=" . urlencode($hash));
- exit;
|