From 65eb2ff002015f066f47495bbd41e0412e9071d7 Mon Sep 17 00:00:00 2001 From: ilotterytea Date: Thu, 1 May 2025 01:24:11 +0500 Subject: feat: emote aliases --- public/emotes/index.php | 23 +++++++++++-- public/emotes/setmanip.php | 66 +++++++++++++++++++++++++++---------- public/emotesets.php | 50 ++++++++++++++++++++++++---- public/static/img/icons/pencil.png | Bin 0 -> 450 bytes public/users.php | 11 ++++++- src/config.php | 3 ++ 6 files changed, 124 insertions(+), 29 deletions(-) create mode 100644 public/static/img/icons/pencil.png diff --git a/public/emotes/index.php b/public/emotes/index.php index 04dec65..1606b17 100644 --- a/public/emotes/index.php +++ b/public/emotes/index.php @@ -199,9 +199,15 @@ if (CLIENT_REQUIRES_JSON) { $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 = $db->prepare("SELECT id, name 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; + + $added = false; + + if ($row = $stmt->fetch()) { + $added = true; + $emote_current_name = $row["name"] ?? $emote->get_code(); + } } if (isset($_SESSION["user_role"]) && $_SESSION["user_role"]["permission_emoteset_own"]) { @@ -210,9 +216,20 @@ if (CLIENT_REQUIRES_JSON) { + if ($added) { + ?> + +
+ + + + diff --git a/public/emotes/setmanip.php b/public/emotes/setmanip.php index 5f3174f..8b43b54 100644 --- a/public/emotes/setmanip.php +++ b/public/emotes/setmanip.php @@ -2,6 +2,7 @@ include_once "../../src/config.php"; include "../../src/accounts.php"; include "../../src/alert.php"; +include_once "../../src/utils.php"; if (!authorize_user(true)) { return; @@ -66,29 +67,58 @@ $stmt->execute([$emote_set_id, $emote_id]); $action = $_POST["action"]; -if ($action == "add") { - if ($stmt->rowCount() != 0) { - generate_alert("/emotes?id=$emote_id", "This emote has been already added!"); - exit; - } +switch ($action) { + case "add": { + if ($stmt->rowCount() != 0) { + generate_alert("/emotes?id=$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]); + $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; + $db = null; + + generate_alert("/emotes?id=$emote_id", "This emote has been added to your set. Enjoy!", 200); + break; + } + case "remove": { + if ($row = $stmt->fetch()) { + $stmt = $db->prepare("DELETE FROM emote_set_contents WHERE id = ?"); + $stmt->execute([$row["id"]]); + } else { + generate_alert("/emotes?id=$emote_id", "This emote wasn't added!"); + $db = null; + exit; + } - generate_alert("/emotes?id=$emote_id", "This emote has been added to your set. Enjoy!", 200); -} else { - if ($row = $stmt->fetch()) { - $stmt = $db->prepare("DELETE FROM emote_set_contents WHERE id = ?"); - $stmt->execute([$row["id"]]); - } else { - generate_alert("/emotes?id=$emote_id", "This emote wasn't added!"); $db = null; - exit; + + generate_alert("/emotes?id=$emote_id", "This emote has been removed from your set.", 200); + break; } + case "alias": { + if (!isset($_POST["value"])) { + generate_alert("/emotes?id=$emote_id", "No value field"); + exit; + } - $db = null; + $value = str_safe($_POST["value"], EMOTE_NAME_MAX_LENGTH); - generate_alert("/emotes?id=$emote_id", "This emote has been removed from your set.", 200); + if (empty($value)) { + $value = null; + } + + $stmt = $db->prepare("UPDATE emote_set_contents SET name = ? WHERE emote_set_id = ? AND emote_id = ?"); + $stmt->execute([$value, $emote_set_id, $emote_id]); + + $db = null; + + generate_alert("/emotes?id=$emote_id", "Updated emote name!", 200); + break; + } + default: { + generate_alert("/emotes?id=$emote_id", "Unknown action"); + break; + } } \ No newline at end of file diff --git a/public/emotesets.php b/public/emotesets.php index fdf32d5..8dfdab0 100644 --- a/public/emotesets.php +++ b/public/emotesets.php @@ -24,9 +24,19 @@ if ($id == "global") { if ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { $emote_set = $row; - $stmt = $db->prepare("SELECT e.* FROM emotes e + $stmt = $db->prepare("SELECT + e.*, + CASE + WHEN esc.name IS NOT NULL THEN esc.name + ELSE e.code + END AS code, + CASE + WHEN esc.name IS NOT NULL THEN e.code + ELSE NULL + END AS original_code + FROM emotes e JOIN emote_set_contents esc ON esc.emote_id = e.id - WHERE emote_set_id = ?"); + WHERE esc.emote_set_id = ?"); $stmt->execute([$emote_set["id"]]); $emote_set["emotes"] = $stmt->fetchAll(PDO::FETCH_ASSOC); @@ -51,10 +61,18 @@ if ($id == "global") { $emote_sets = $stmt->fetchAll(PDO::FETCH_ASSOC); foreach ($emote_sets as &$e) { - $stmt = $db->prepare("SELECT e.* FROM emotes e + $stmt = $db->prepare("SELECT e.*, + CASE + WHEN esc.name IS NOT NULL THEN esc.name + ELSE e.code + END AS code, + CASE + WHEN esc.name IS NOT NULL THEN e.code + ELSE NULL + END AS original_code + FROM emotes e JOIN emote_set_contents esc ON esc.emote_set_id = ? - WHERE e.id = esc.emote_id - LIMIT 5"); + WHERE e.id = esc.emote_id"); $stmt->execute([$e["id"]]); $e["emotes"] = $stmt->fetchAll(PDO::FETCH_ASSOC); @@ -81,7 +99,16 @@ if ($id == "global") { if ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { $emote_set = $row; - $stmt = $db->prepare("SELECT e.* FROM emotes e + $stmt = $db->prepare("SELECT e.*, + CASE + WHEN esc.name IS NOT NULL THEN esc.name + ELSE e.code + END AS code, + CASE + WHEN esc.name IS NOT NULL THEN e.code + ELSE NULL + END AS original_code + FROM emotes e JOIN emote_set_contents esc ON esc.emote_set_id = ? WHERE esc.emote_id = e.id"); $stmt->execute([$emote_set["id"]]); @@ -104,7 +131,16 @@ if ($id == "global") { if ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { $emote_set = $row; - $stmt = $db->prepare("SELECT e.* FROM emotes e + $stmt = $db->prepare("SELECT e.*, + CASE + WHEN esc.name IS NOT NULL THEN esc.name + ELSE e.code + END AS code, + CASE + WHEN esc.name IS NOT NULL THEN e.code + ELSE NULL + END AS original_code + FROM emotes e JOIN emote_set_contents esc ON esc.emote_set_id = ? WHERE esc.emote_id = e.id"); $stmt->execute([$emote_set["id"]]); diff --git a/public/static/img/icons/pencil.png b/public/static/img/icons/pencil.png new file mode 100644 index 0000000..0bfecd5 Binary files /dev/null and b/public/static/img/icons/pencil.png differ diff --git a/public/users.php b/public/users.php index 914f890..8b62a8a 100644 --- a/public/users.php +++ b/public/users.php @@ -160,7 +160,16 @@ while ($row = $stmt->fetch()) { // getting info about emote set content $em_stmt = $db->prepare( - "SELECT e.id, e.code, e.mime, e.ext, e.created_at, e.uploaded_by FROM emotes e + "SELECT e.id, e.mime, e.ext, e.created_at, e.uploaded_by, + CASE + WHEN esc.name IS NOT NULL THEN esc.name + ELSE e.code + END AS code, + CASE + WHEN esc.name IS NOT NULL THEN e.code + ELSE NULL + END AS original_code + FROM emotes e INNER JOIN emote_set_contents AS esc ON esc.emote_set_id = ? WHERE esc.emote_id = e.id diff --git a/src/config.php b/src/config.php index e224b72..d75e9f1 100644 --- a/src/config.php +++ b/src/config.php @@ -16,6 +16,9 @@ define("RATING_NAMES", [ define("ANONYMOUS_UPLOAD", false); define("ANONYMOUS_DEFAULT_NAME", "chud"); +// EMOTES +define("EMOTE_NAME_MAX_LENGTH", 100); + // ACCOUNTS define("ACCOUNT_USERNAME_REGEX", "/^[A-Za-z0-9_]+$/"); define("ACCOUNT_USERNAME_MAX_LENGTH", 20); -- cgit v1.2.3