Current directory: /home/klas4s23/domains/585455.klas4s23.mid-ica.nl/public_html/Gastenboek/uploads
<?php
session_start();
include "db.php";
// Check if user is logged in
$logged_in = isset($_SESSION['user_id']);
$user_id = $logged_in ? $_SESSION['user_id'] : 0;
// Get search query from GET
$search_query = isset($_GET['query']) ? trim($_GET['query']) : '';
// Get all public lists with creator username and word count
try {
$query = "
SELECT wl.id, wl.name, wl.created_at, wl.score, wl.like_count, wl.dislike_count,
u.id AS creator_id, u.username AS creator, u.profile_photo,
COUNT(lw.id) AS word_count
FROM word_lists wl
JOIN users u ON wl.user_id = u.id
LEFT JOIN list_words lw ON wl.id = lw.list_id
WHERE wl.public = 1
";
// Add search condition if search query exists
if (!empty($search_query)) {
$query .= " AND (wl.name LIKE ? OR u.username LIKE ?)";
$search_param = "%" . $search_query . "%";
$stmt = $conn->prepare($query . " GROUP BY wl.id ORDER BY wl.created_at DESC");
$stmt->execute([$search_param, $search_param]);
} else {
$stmt = $conn->prepare($query . " GROUP BY wl.id ORDER BY wl.created_at DESC");
$stmt->execute();
}
$public_lists = $stmt->fetchAll();
// If user is logged in, get their likes/dislikes
$user_likes = [];
if ($logged_in) {
$stmt = $conn->prepare("
SELECT list_id, is_like FROM list_likes WHERE user_id = ?
");
$stmt->execute([$user_id]);
while ($row = $stmt->fetch()) {
$user_likes[$row['list_id']] = $row['is_like'];
}
}
} catch (PDOException $e) {
echo json_encode([
'error' => "Error fetching public lists: " . $e->getMessage(),
'html' => "<div class='p-4 bg-red-100 text-red-600 rounded-lg'>An error occurred while searching</div>",
'lists' => []
]);
exit();
}
// Function to get preview of list words (limited to first few)
function getListPreview($conn, $list_id, $limit = 5) {
// Cast limit to integer for security
$limit = (int)$limit;
$stmt = $conn->prepare("
SELECT dutch, english FROM list_words
WHERE list_id = ?
ORDER BY id
LIMIT $limit
");
$stmt->execute([$list_id]);
return $stmt->fetchAll();
}
// Generate HTML for the lists grid or no results message
ob_start();
if(count($public_lists) === 0): ?>
<div class="text-center py-10 bg-gray-50 rounded-lg">
<?php if(!empty($search_query)): ?>
<i class="fas fa-search text-gray-400 text-5xl mb-3"></i>
<h3 class="text-lg font-medium text-gray-700 mb-2">No Lists Found</h3>
<p class="text-gray-500 mb-4">No lists match your search for "<?= htmlspecialchars($search_query) ?>"</p>
<button id="resetSearch" class="inline-block bg-blue-500 text-white px-4 py-2 rounded hover:bg-blue-600">
View All Lists
</button>
<?php else: ?>
<i class="fas fa-list-ul text-gray-400 text-5xl mb-3"></i>
<h3 class="text-lg font-medium text-gray-700 mb-2">No Public Lists Available</h3>
<p class="text-gray-500 mb-4">Be the first to share your word lists with the community!</p>
<?php if($logged_in): ?>
<a href="my_lists.php" class="inline-block bg-blue-500 text-white px-4 py-2 rounded hover:bg-blue-600">
Go to My Lists
</a>
<?php else: ?>
<a href="login.php" class="inline-block bg-blue-500 text-white px-4 py-2 rounded hover:bg-blue-600">
Login to Create Lists
</a>
<?php endif; ?>
<?php endif; ?>
</div>
<?php else: ?>
<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4" id="listsGrid">
<?php foreach($public_lists as $list): ?>
<div class="list-card bg-white border rounded-lg shadow-sm overflow-hidden">
<div class="p-4">
<h3 class="font-bold text-lg mb-1 truncate" title="<?= htmlspecialchars($list['name']) ?>">
<?= htmlspecialchars($list['name']) ?>
</h3>
<p class="text-sm text-gray-600 mb-3 flex items-center">
<a href="user_profile.php?id=<?= $list['creator_id'] ?>" class="flex items-center hover:underline" title="View <?= htmlspecialchars($list['creator']) ?>'s profile">
<?php if (!empty($list['profile_photo']) && file_exists($list['profile_photo'])): ?>
<img src="<?= htmlspecialchars($list['profile_photo']) ?>" alt="Creator" class="w-6 h-6 rounded-full object-cover mr-2">
<?php else: ?>
<span class="w-6 h-6 rounded-full bg-gray-200 flex items-center justify-center mr-2">
<i class="fas fa-user text-gray-400 text-xs"></i>
</span>
<?php endif; ?>
By <span class="font-medium ml-1"><?= htmlspecialchars($list['creator']) ?></span>
</a>
</p>
<div class="flex justify-between text-sm text-gray-500 mb-4">
<span><?= $list['word_count'] ?> words</span>
<span><?= date('M j, Y', strtotime($list['created_at'])) ?></span>
</div>
<?php
// Get preview of first few words
$preview = getListPreview($conn, $list['id'], 3);
if(count($preview) > 0):
?>
<div class="bg-gray-50 p-2 rounded text-sm mb-3">
<div class="font-medium mb-1">Preview:</div>
<?php foreach($preview as $index => $word): ?>
<?php if($index < 2): ?>
<div class="mb-1 truncate">
<?= htmlspecialchars($word['dutch']) ?> → <?= htmlspecialchars($word['english']) ?>
</div>
<?php endif; ?>
<?php endforeach; ?>
<?php if(count($preview) > 2): ?>
<div class="text-gray-500">...and more</div>
<?php endif; ?>
</div>
<?php endif; ?>
<?php if($list['score'] !== null): ?>
<div class="mb-3">
<span class="text-sm font-medium">Creator's Score: </span>
<span class="<?= $list['score'] >= 80 ? 'text-green-600' : ($list['score'] >= 50 ? 'text-yellow-600' : 'text-red-600') ?> font-medium">
<?= $list['score'] ?>%
</span>
</div>
<?php endif; ?>
<div class="flex justify-between items-center">
<div class="flex items-center gap-2">
<?php if ($logged_in): ?>
<button class="rating-button like flex items-center <?= isset($user_likes[$list['id']]) && $user_likes[$list['id']] == 1 ? 'active' : '' ?>"
data-list-id="<?= $list['id'] ?>"
data-action="1"
title="Like this list">
<i class="fas fa-thumbs-up"></i>
<span class="like-count ml-1"><?= $list['like_count'] ?></span>
</button>
<span class="text-gray-300">|</span>
<button class="rating-button dislike flex items-center <?= isset($user_likes[$list['id']]) && $user_likes[$list['id']] == 0 ? 'active' : '' ?>"
data-list-id="<?= $list['id'] ?>"
data-action="0"
title="Dislike this list">
<i class="fas fa-thumbs-down"></i>
<span class="dislike-count ml-1"><?= $list['dislike_count'] ?></span>
</button>
<?php else: ?>
<a href="login.php" class="flex items-center text-gray-500" title="Login to like">
<i class="fas fa-thumbs-up"></i>
<span class="ml-1"><?= $list['like_count'] ?></span>
</a>
<span class="text-gray-300">|</span>
<a href="login.php" class="flex items-center text-gray-500" title="Login to dislike">
<i class="fas fa-thumbs-down"></i>
<span class="ml-1"><?= $list['dislike_count'] ?></span>
</a>
<?php endif; ?>
</div>
<div class="flex gap-2">
<a href="?preview=<?= $list['id'] ?>" class="preview-button inline-flex items-center text-blue-600 hover:text-blue-800">
<i class="fas fa-eye mr-1"></i> View
</a>
<?php if($logged_in): ?>
<form method="post">
<input type="hidden" name="list_id" value="<?= $list['id'] ?>">
<button type="submit" name="copy_list" class="inline-flex items-center text-green-600 hover:text-green-800">
<i class="fas fa-copy mr-1"></i> Save
</button>
</form>
<?php endif; ?>
</div>
</div>
</div>
</div>
<?php endforeach; ?>
</div>
<?php endif;
$html = ob_get_clean();
// Return JSON response
echo json_encode([
'html' => $html,
'lists' => array_map(function($list) {
return [
'id' => $list['id'],
'name' => $list['name'],
'creator' => $list['creator'],
'word_count' => $list['word_count'],
'like_count' => $list['like_count'],
'dislike_count' => $list['dislike_count']
];
}, $public_lists)
]);
?>