summaryrefslogtreecommitdiff
path: root/public/catalogue.php
blob: f2a5cd6ed963705a4678ed6403f4e1e6c77cd7fe (plain)
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
<?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 = 20;

// counting max pages
$stmt = $db->query('SELECT COUNT(id) AS all_files FROM files WHERE id NOT IN (SELECT id FROM file_bans)');
$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
    FROM files f
    WHERE f.id NOT IN (SELECT id FROM file_bans)
    ORDER BY f.uploaded_at DESC
    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['thumb_title'] = "{$f['mime']} ({$f['extension']})";
}
unset($f);
?>
<!DOCTYPE html>
<html>

<head>
    <title>File Catalogue &lpar;Page <?= $page + 1 ?>/<?= $max_pages ?>&rpar; - <?= 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>
        <?php html_mini_navbar('Page ' . ($page + 1) . '/' . $max_pages) ?>

        <section class="row align-center">
            <?php if ($page - 1 >= 0): ?>
                <a href="/catalogue.php?p=<?= $page ?>">&larr; Previous page</a>
            <?php endif; ?>
            <?php if ($page + 2 <= $max_pages): ?>
                <a href="/catalogue.php?p=<?= $page + 2 ?>" style="margin-left:auto">&rarr; Next page</a>
            <?php endif; ?>
        </section>

        <section class="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/') || $file['mime'] == 'application/x-shockwave-flash'): ?>
                                <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">
                            <?php elseif (str_starts_with($file['mime'], 'text/')): ?>
                                <img src="/static/img/icons/file_text.png" alt="No thumbnail." loading="lazy">
                            <?php else: ?>
                                <img src="/static/img/icons/file.png" alt="No thumbnail.">
                            <?php endif; ?>
                        </i>
                    </a>
                </div>
            <?php endforeach; ?>
        </section>
    </main>
</body>

</html>