prepare("SELECT id FROM tabs WHERE login_token = ?"); $stmt->execute([$token]); $tab = $stmt->fetch(); if ($tab) { // Log in as a temporary guest for this tab $_SESSION['guest_tab_id'] = $tab['id']; // Redirect to the tab view without the token in the URL header('Location: index.php?tab=' . $tab['id']); exit; } else { // Invalid token, redirect to login or show an error header('Location: login.php?error=invalid_token'); exit; } } require_once 'include/auth.php'; // Fetch all settings for theme and layout $stmt = $pdo->query("SELECT key, value FROM settings"); $settings = $stmt->fetchAll(PDO::FETCH_KEY_PAIR); $open_in_new_tab = $settings['open_new_tab'] ?? '1'; $target_attr = $open_in_new_tab === '1' ? 'target="_blank"' : ''; // Get current user ID if logged in $current_user_id = $_SESSION['user_id'] ?? null; $current_user_role = $_SESSION['role'] ?? null; $guest_tab_id = $_SESSION['guest_tab_id'] ?? null; // Base query for fetching tabs $tabs_stmt_sql = "SELECT DISTINCT t.* FROM tabs t "; $params = []; // Logic to determine which tabs are visible based on role and explicit permissions $visibility_conditions = " LEFT JOIN user_tab_visibility utv ON t.id = utv.tab_id AND utv.user_id = :current_user_id WHERE utv.user_id IS NULL "; if ($guest_tab_id) { // Guest user with token can only see the specific tab $tabs_stmt_sql .= "WHERE t.id = :guest_tab_id"; $params[':guest_tab_id'] = $guest_tab_id; } else if ($current_user_id) { if ($current_user_role === 'admin') { // Admin sees all tabs that are not explicitly hidden for them $tabs_stmt_sql .= $visibility_conditions; } else { // 'user' and 'viewer' roles // Users/Viewers see public tabs, their own tabs, or tabs they have access to, // as long as they are not explicitly hidden. $tabs_stmt_sql .= " LEFT JOIN tab_viewer_access tva ON t.id = tva.tab_id {$visibility_conditions} AND (t.user_id IS NULL OR t.user_id = :current_user_id OR tva.user_id = :current_user_id) "; } $params[':current_user_id'] = $current_user_id; } else { // Guest sees only public (ownerless) tabs $tabs_stmt_sql .= "WHERE t.user_id IS NULL"; } $tabs_stmt_sql .= " ORDER BY t.sort_order, t.id"; $tabs_stmt = $pdo->prepare($tabs_stmt_sql); $tabs_stmt->execute($params); $tabs = $tabs_stmt->fetchAll(); // Determine the default tab to show. // If the requested tab is not in the visible list, fall back to the first visible one. $visible_tab_ids = array_column($tabs, 'id'); $current_tab_id = isset($_GET['tab']) ? (int)$_GET['tab'] : ($visible_tab_ids[0] ?? 1); if (!in_array($current_tab_id, $visible_tab_ids)) { $current_tab_id = $visible_tab_ids[0] ?? 1; } // Fetch details for the current tab to get background info $current_tab_data = null; if ($current_tab_id) { $tab_details_stmt = $pdo->prepare("SELECT * FROM tabs WHERE id = :id"); $tab_details_stmt->execute([':id' => $current_tab_id]); $current_tab_data = $tab_details_stmt->fetch(); } // Get the ID of the 'FAVORITES' category (case-insensitive) $fav_cat_stmt = $pdo->prepare("SELECT id FROM categories WHERE UPPER(name) = 'FAVORITES'"); $fav_cat_stmt->execute(); $favorites_cat_id = $fav_cat_stmt->fetchColumn(); // Fetch favorite bookmarks from the 'FAVORITES' category for the current tab $fav_stmt = $pdo->prepare(" SELECT b.* FROM bookmarks b JOIN categories c ON b.category_id = c.id WHERE b.is_favorite = 1 AND c.tab_id = :tab_id ORDER BY b.sort_order ASC, b.title ASC "); $fav_stmt->execute([':tab_id' => $current_tab_id]); $favorites = $fav_stmt->fetchAll(); // Fetch categories for the current tab, excluding 'FAVORITES' $cat_stmt = $pdo->prepare("SELECT * FROM categories WHERE tab_id = :tab_id AND UPPER(name) != 'FAVORITES' ORDER BY sort_order ASC, name ASC"); $cat_stmt->execute([':tab_id' => $current_tab_id]); $categories = $cat_stmt->fetchAll(); // Fetch bookmarks for each category $bookmarks_by_cat = []; if ($categories) { $bookmarks_stmt = $pdo->prepare("SELECT * FROM bookmarks WHERE category_id = :cat_id AND (is_favorite IS NULL OR is_favorite = 0) ORDER BY sort_order ASC, title ASC"); foreach ($categories as $category) { $bookmarks_stmt->execute([':cat_id' => $category['id']]); $bookmarks_by_cat[$category['id']] = $bookmarks_stmt->fetchAll(); } } require_once 'include/header.php'; ?>