1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/../config.php';
include_once $_SERVER['DOCUMENT_ROOT'] . '/../lib/partials.php';
include_once $_SERVER['DOCUMENT_ROOT'] . '/../lib/utils.php';
include_once $_SERVER['DOCUMENT_ROOT'] . '/../lib/alert.php';
session_start();
if (!isset($_SESSION['is_moderator']) && !FILE_CATALOG_PUBLIC) {
http_response_code(403);
exit;
}
$db = new PDO(DB_URL, DB_USER, DB_PASS);
$page = max(intval($_GET['p'] ?? '1') - 1, 0);
$limit = FILE_CATALOG_LIMIT;
$sort_option = $_GET['sort'] ?? 'recent';
$sort = match ($sort_option) {
'oldest' => 'ORDER BY uploaded_at ASC',
'most_viewed' => 'ORDER BY views DESC',
'least_viewed' => 'ORDER BY views ASC',
default => 'ORDER by uploaded_at DESC'
};
// counting max pages
$stmt = $db->query("SELECT COUNT(id) AS all_files FROM files WHERE id NOT IN (SELECT id FROM file_bans) $sort");
$stmt->execute();
$max_pages = ceil(($stmt->fetch(PDO::FETCH_ASSOC)['all_files'] ?: 0) / $limit);
$page = min($page, $max_pages - 1);
// getting files
$offset = $page * $limit;
$stmt = $db->query("SELECT f.id, f.mime, f.extension, f.title
FROM files f
WHERE f.id NOT IN (SELECT id FROM file_bans)
$sort
LIMIT $limit OFFSET $offset
");
$stmt->execute();
$files = $stmt->fetchAll();
foreach ($files as &$f) {
if (str_starts_with($f['mime'], 'video/')) {
$f['color'] = 'blue';
} else if ($f['mime'] == 'application/x-shockwave-flash') {
$f['color'] = 'red';
}
$f['name'] = $f['title'] ?: sprintf('%s.%s', $f['id'], $f['extension']);
$f['thumb_title'] = "{$f['name']} // {$f['mime']} ({$f['extension']})";
}
unset($f);
?>
<!DOCTYPE html>
<html>
<head>
<title>File Catalogue (Page <?= $page + 1 ?>/<?= $max_pages ?>) - <?= INSTANCE_NAME ?></title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="/static/style.css">
<link rel="shortcut icon" href="/static/favicon.ico" type="image/x-icon">
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<meta name="theme-color" content="#ffe1d4">
</head>
<body>
<main class="full-size">
<?php html_mini_navbar('Page ' . ($page + 1) . '/' . $max_pages, "Library of " . INSTANCE_NAME) ?>
<div class="grow row gap-8">
<!-- SIDE BAR -->
<div class="column gap-8">
<form action="/catalogue.php" method="get">
<div class="box">
<div class="tab">
Search
</div>
<div class="content column gap-8">
<label for="sort">Sort by</label>
<select name="sort" id="sort">
<option value="recent" <?= $sort_option == 'recent' ? 'selected' : '' ?>>Recent</option>
<option value="oldest" <?= $sort_option == 'oldest' ? 'selected' : '' ?>>Oldest</option>
<option value="most_viewed" <?= $sort_option == 'most_viewed' ? 'selected' : '' ?>>Most
viewed</option>
<option value="least_viewed" <?= $sort_option == 'least_viewed' ? 'selected' : '' ?>>Least
viewed</option>
</select>
<button type="submit">Search</button>
</div>
</div>
</form>
</div>
<!-- CONTENT -->
<div class="column gap-8 grow">
<!-- FILES -->
<div class="box">
<div class="tab">
<p>Files</p>
</div>
<div class="content wall">
<?php foreach ($files as $file): ?>
<div class="brick<?= isset($file['color']) ? " {$file['color']}" : '' ?>">
<a href="/<?= sprintf('%s.%s', $file['id'], $file['extension']) ?>">
<i title="<?= $file['thumb_title'] ?>">
<?php if (str_starts_with($file['mime'], 'image/') || str_starts_with($file['mime'], 'video/')): ?>
<img src="<?= sprintf('%s/%s.webp', FILE_THUMBNAIL_DIRECTORY_PREFIX, $file['id']) ?>"
alt="No thumbnail." loading="lazy">
<?php elseif (str_starts_with($file['mime'], 'audio/')): ?>
<img src="/static/img/icons/file_audio.png" alt="No thumbnail." loading="lazy"
class="thumbnail stock">
<?php elseif (str_starts_with($file['mime'], 'text/')): ?>
<img src="/static/img/icons/file_text.png" alt="No thumbnail." loading="lazy"
class="thumbnail stock">
<?php elseif ($file['mime'] == 'application/x-shockwave-flash'): ?>
<img src="/static/img/icons/file_flash.png" alt="No thumbnail." loading="lazy"
class="thumbnail stock">
<?php else: ?>
<img src="/static/img/icons/file.png" alt="No thumbnail." class="thumbnail stock">
<?php endif; ?>
</i>
</a>
</div>
<?php endforeach; ?>
</div>
</div>
<!-- SCROLL -->
<div class="row">
<div class="box row gap-8">
<?php if ($page - 1 >= 0): ?>
<a href="/catalogue.php?p=<?= $page ?>&sort=<?= $sort_option ?>">
<button>Previous</button>
</a>
<?php endif; ?>
<?php if ($page + 2 <= $max_pages): ?>
<a href="/catalogue.php?p=<?= $page + 2 ?>&sort=<?= $sort_option ?>" style="margin-left:auto">
<button>Next</button>
</a>
<?php endif; ?>
</div>
</div>
</div>
</div>
<?php html_mini_footer() ?>
</main>
</body>
</html>
|