summaryrefslogtreecommitdiff
path: root/public/emotes/index.php
blob: 9421e65b37353457c9b04f462472f8b7c3af518a (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
<?php
include "../../src/emote.php";
include "../../src/accounts.php";
include_once "../../src/config.php";

authorize_user();

function display_list_emotes(int $page, int $limit): array
{
    $offset = $page * $limit;
    $db = new PDO(DB_URL, DB_USER, DB_PASS);
    $stmt = $db->prepare("SELECT * FROM emotes ORDER BY created_at ASC LIMIT ? OFFSET ?");
    $stmt->bindParam(1, $limit, PDO::PARAM_INT);
    $stmt->bindParam(2, $offset, PDO::PARAM_INT);
    $stmt->execute();

    $emotes = [];

    $rows = $stmt->fetchAll();

    foreach ($rows as $row) {
        array_push($emotes, new Emote(
            $row["id"],
            $row["code"],
            $row["ext"],
            intval(strtotime($row["created_at"])),
            $row["uploaded_by"]
        ));
    }

    return $emotes;
}

function display_emote(int $id)
{
    $db = new PDO(DB_URL, DB_USER, DB_PASS);
    $stmt = $db->prepare("SELECT * FROM emotes WHERE id = ?");
    $stmt->execute([$id]);

    $emote = null;

    if ($row = $stmt->fetch()) {
        $emote = new Emote(
            $row["id"],
            $row["code"],
            $row["ext"],
            intval(strtotime($row["created_at"])),
            $row["uploaded_by"]
        );
    }

    if ($emote == null) {
        header("Location: /404.php");
        exit;
    }

    return $emote;
}

$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' ||
    $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
$current_url = $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];

$id = parse_url($current_url, PHP_URL_PATH);
$id = substr($id, 8);
$id = str_replace("/", "", $id);

$emotes = null;
$emote = null;

include "../../src/partials.php";
include "../../src/utils.php";
include "../../src/alert.php";

if ($id == "" || !is_numeric($id)) {
    $page = intval($_GET["p"] ?? "0");
    $limit = 50;
    $emotes = display_list_emotes($page, $limit);
    include "../../src/emotes/multiple_page.php";
} else {
    $emote = display_emote(intval($id));
    include "../../src/emotes/single_page.php";
}