diff options
| author | ilotterytea <iltsu@alright.party> | 2025-04-20 18:02:53 +0500 |
|---|---|---|
| committer | ilotterytea <iltsu@alright.party> | 2025-04-20 18:02:53 +0500 |
| commit | ac5b69edf724d164c2150dfc96129335b9225299 (patch) | |
| tree | 3968df45db02ff04f9a266bee8d47a9136811e91 | |
| parent | ac515bdb95db1b3628381a7356dbae1d2715e0a8 (diff) | |
feat: emote addition
| -rw-r--r-- | database.sql | 24 | ||||
| -rw-r--r-- | public/emotes/add.php | 67 | ||||
| -rw-r--r-- | public/emotes/index.php | 2 | ||||
| -rw-r--r-- | src/emotes/single_page.php | 33 |
4 files changed, 121 insertions, 5 deletions
diff --git a/database.sql b/database.sql index 3436b2c..cff3e93 100644 --- a/database.sql +++ b/database.sql @@ -22,4 +22,28 @@ CREATE TABLE IF NOT EXISTS emotes ( ext TEXT NOT NULL, uploaded_by INTEGER REFERENCES users(id), created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP +); + +CREATE TABLE IF NOT EXISTS emote_sets ( + id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT, + owner_id INTEGER NOT NULL REFERENCES users(id), + linked_to INTEGER REFERENCES emote_sets(id), + name TEXT NOT NULL, + size INTEGER +); + +CREATE TABLE IF NOT EXISTS emote_set_contents ( + id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT, + emote_set_id INTEGER NOT NULL REFERENCES emote_sets(id), + emote_id INTEGER NOT NULL REFERENCES emotes(id), + name TEXT, + added_by INTEGER NOT NULL REFERENCES users(id), + created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP +); + +CREATE TABLE IF NOT EXISTS acquired_emote_sets ( + id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT, + user_id INTEGER NOT NULL, + emote_set_id INTEGER NOT NULL, + is_default BOOLEAN NOT NULL DEFAULT false );
\ No newline at end of file diff --git a/public/emotes/add.php b/public/emotes/add.php new file mode 100644 index 0000000..a5c480d --- /dev/null +++ b/public/emotes/add.php @@ -0,0 +1,67 @@ +<?php +include_once "../../src/config.php"; +include "../../src/accounts.php"; +include "../../src/alert.php"; + +if (!authorize_user(true)) { + return; +} + +$db = new PDO(DB_URL, DB_USER, DB_PASS); + +// checking emote +$emote_id = $_POST["id"]; +$stmt = $db->prepare("SELECT id FROM emotes WHERE id = ?"); +$stmt->execute([$emote_id]); +if ($stmt->rowCount() == 0) { + generate_alert("/emotes/$emote_id", "Emote not found", 404); + exit; +} + +$user_id = $_SESSION["user_id"]; + +// obtaining or creating a emote set +$stmt = $db->prepare("SELECT emote_set_id FROM acquired_emote_sets WHERE user_id = ? AND is_default = true"); +$stmt->execute([$user_id]); +$emote_set_id = null; + +if ($row = $stmt->fetch()) { + $emote_set_id = $row["emote_set_id"]; + + // checking ownership + $stmt = $db->prepare("SELECT id FROM emote_sets WHERE id = ? AND owner_id = ?"); + $stmt->execute([$emote_set_id, $user_id]); + + if ($stmt->rowCount() == 0) { + $_SESSION["user_emote_set_id"] = ""; + generate_alert("/emotes/$emote_id", "Bad ownership permissions on active emoteset", 403); + exit; + } +} + +if ($emote_set_id == null) { + $stmt = $db->prepare("INSERT INTO emote_sets(owner_id, name) VALUES (?, ?)"); + $stmt->execute([$user_id, $_SESSION["user_name"] . "'s emoteset"]); + $emote_set_id = $db->lastInsertId(); + + $stmt = $db->prepare("INSERT INTO acquired_emote_sets(user_id, emote_set_id, is_default) VALUES (?, ?, true)"); + $stmt->execute([$user_id, $emote_set_id]); +} + +$_SESSION["user_emote_set_id"] = $emote_set_id; + +// inserting emote +$stmt = $db->prepare("SELECT id FROM emote_set_contents WHERE emote_set_id = ? AND emote_id = ?"); +$stmt->execute([$emote_set_id, $emote_id]); + +if ($stmt->rowCount() != 0) { + generate_alert("/emotes/$emote_id", "This emote has been already added!"); + exit; +} + +$stmt = $db->prepare("INSERT INTO emote_set_contents(emote_set_id, emote_id, added_by) VALUES (?, ?, ?)"); +$stmt->execute([$emote_set_id, $emote_id, $user_id]); + +$db = null; + +generate_alert("/emotes/$emote_id", "Successfully added a new emote!", 200);
\ No newline at end of file diff --git a/public/emotes/index.php b/public/emotes/index.php index ea47834..9421e65 100644 --- a/public/emotes/index.php +++ b/public/emotes/index.php @@ -69,6 +69,8 @@ $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"); diff --git a/src/emotes/single_page.php b/src/emotes/single_page.php index 900003c..b76fda8 100644 --- a/src/emotes/single_page.php +++ b/src/emotes/single_page.php @@ -17,6 +17,7 @@ include_once "../../src/config.php"; <?php html_navigation_search(); ?> </section> <section class="content"> + <?php display_alert() ?> <section class="box"> <div class="box navtab"> Emote - <?php echo $emote->get_code() ?> @@ -37,11 +38,33 @@ include_once "../../src/config.php"; <?php if (isset($_SESSION["user_id"])) { echo '' ?> <div class="items row left full"> - <form action="/emotes/add.php" method="POST"> - <input type="text" name="id" value="<?php echo $emote->get_id() ?>" - style="display: none;"> - <button type="submit" class="green">Add to my channel</button> - </form> + <?php + $db = new PDO(DB_URL, DB_USER, DB_PASS); + $added = false; + + if (isset($_SESSION["user_emote_set_id"])) { + $stmt = $db->prepare("SELECT id FROM emote_set_contents WHERE emote_set_id = ? AND emote_id = ?"); + $stmt->execute([$_SESSION["user_emote_set_id"], $emote->get_id()]); + $added = $stmt->rowCount() > 0; + } + + $db = null; + ?> + <?php + if ($added) { ?> + <form action="/emotes/remove.php" method="POST"> + <input type="text" name="id" value="<?php echo $emote->get_id() ?>" + style="display: none;"> + <button type="submit" class="red">Remove from my channel</button> + </form><?php + } else { ?> + <form action="/emotes/add.php" method="POST"> + <input type="text" name="id" value="<?php echo $emote->get_id() ?>" + style="display: none;"> + <button type="submit" class="green">Add to my channel</button> + </form><?php + } + ?> </div> <div class="items row right full"> <form action="/emotes/rate.php" method="POST"> |
