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

function display_list_emotes(int $page, int $limit): array
{
    $db = new SQLite3("../../database.db");
    $stmt = $db->prepare("SELECT * FROM emotes ORDER BY created_at ASC LIMIT :limit OFFSET :offset");
    $stmt->bindValue(":offset", $page * $limit, SQLITE3_INTEGER);
    $stmt->bindValue(":limit", $limit, SQLITE3_INTEGER);
    $results = $stmt->execute();

    $emotes = [];

    while ($row = $results->fetchArray()) {
        array_push($emotes, new Emote(
            $row["id"],
            $row["code"],
            $row["ext"],
            intval(strtotime($row["created_at"]))
        ));
    }

    return $emotes;
}

function display_emote(int $id)
{
    $db = new SQLite3("../../database.db");
    $stmt = $db->prepare("SELECT * FROM emotes WHERE id = :id");
    $stmt->bindValue(":id", $id, SQLITE3_INTEGER);
    $results = $stmt->execute();

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

    if ($emote == null) {
        echo "not found";
        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";

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";
}