diff options
| author | ilotterytea <iltsu@alright.party> | 2025-04-19 20:48:00 +0500 |
|---|---|---|
| committer | ilotterytea <iltsu@alright.party> | 2025-04-19 20:48:00 +0500 |
| commit | a7e961bcd2f1e8d98f2ffbb5ff37c2e7f9b981d1 (patch) | |
| tree | e5279f13563c7f923745990f48eedd89b396ae8d /public/emotes/index.php | |
| parent | 7a66de9852d4683a7b5cdcedb8e88cfdc73f4b56 (diff) | |
feat: show emotes
Diffstat (limited to 'public/emotes/index.php')
| -rw-r--r-- | public/emotes/index.php | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/public/emotes/index.php b/public/emotes/index.php new file mode 100644 index 0000000..a4bf585 --- /dev/null +++ b/public/emotes/index.php @@ -0,0 +1,69 @@ +<?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; + +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"; +} |
