From 4e29d4041e7cf254041e22d2052e94a7c53c9066 Mon Sep 17 00:00:00 2001 From: ilotterytea Date: Wed, 14 May 2025 21:40:06 +0500 Subject: feat: personal badges --- public/account/index.php | 39 +++++++++++++++++++++++++++++++++++++++ public/emotes/index.php | 9 ++++++++- public/users.php | 25 ++++++++++++++++++++++--- 3 files changed, 69 insertions(+), 4 deletions(-) (limited to 'public') diff --git a/public/account/index.php b/public/account/index.php index d62f87f..6cf3fc7 100644 --- a/public/account/index.php +++ b/public/account/index.php @@ -72,6 +72,28 @@ if ($_SERVER['REQUEST_METHOD'] == "POST") { } } + if (isset($_FILES["badge"]) && !empty($_FILES["badge"]["tmp_name"])) { + $badge = $_FILES["badge"]; + $badge_id = bin2hex(random_bytes(16)); + if ( + $err = create_image_bundle( + $badge["tmp_name"], + $_SERVER["DOCUMENT_ROOT"] . "/static/userdata/badges/" . $badge_id, + ACCOUNT_BADGE_MAX_SIZE[0], + ACCOUNT_BADGE_MAX_SIZE[1], + true, + true + ) + ) { + generate_alert("/account", sprintf("Error occurred while processing the personal badge (%d)", $err)); + exit; + } + + $db->prepare("DELETE FROM user_badges WHERE badge_id != ? AND user_id = ?")->execute([$badge_id, $_SESSION["user_id"]]); + $db->prepare("INSERT INTO badges(id, uploaded_by) VALUES (?, ?)")->execute([$badge_id, $_SESSION["user_id"]]); + $db->prepare("INSERT INTO user_badges(badge_id, user_id) VALUES (?, ?)")->execute([$badge_id, $_SESSION["user_id"]]); + } + $db = null; generate_alert("/account", "Your changes have been applied!", 200); exit; @@ -119,6 +141,23 @@ if ($_SERVER['REQUEST_METHOD'] == "POST") { ?> +

Personal badge

+ prepare("SELECT badge_id FROM user_badges WHERE user_id = ?"); + $stmt->execute([$_SESSION["user_id"]]); + + if ($row = $stmt->fetch()) { + echo '
'; + echo ''; + echo ''; + echo ''; + echo '
'; + } else { + echo "

You don't have personal badge

"; + } + ?> + +

Username

"> diff --git a/public/emotes/index.php b/public/emotes/index.php index 9a33d1d..ad4df09 100644 --- a/public/emotes/index.php +++ b/public/emotes/index.php @@ -338,13 +338,15 @@ if (CLIENT_REQUIRES_JSON) { $link = "#"; $show_private_badge = false; $badge = null; + $custom_badge = null; if ($emote->get_uploaded_by()) { - $stmt = $db->prepare("SELECT u.username, up.private_profile, r.name AS role_name, r.badge_id AS role_badge_id + $stmt = $db->prepare("SELECT u.username, up.private_profile, r.name AS role_name, r.badge_id AS role_badge_id, ub.badge_id AS custom_badge_id FROM users u INNER JOIN user_preferences up ON up.id = u.id LEFT JOIN role_assigns ra ON ra.user_id = u.id LEFT JOIN roles r ON r.id = ra.role_id + LEFT JOIN user_badges ub ON ub.user_id = u.id WHERE u.id = ? "); $stmt->execute([$emote->get_uploaded_by()]); @@ -355,6 +357,7 @@ if (CLIENT_REQUIRES_JSON) { $username = $row["username"]; $link = "/users.php?id=" . $emote->get_uploaded_by(); $badge = ["role_name" => $row["role_name"], "role_badge_id" => $row["role_badge_id"]]; + $custom_badge = $row["custom_badge_id"]; } } @@ -370,6 +373,10 @@ if (CLIENT_REQUIRES_JSON) { echo ' ## ' . $badge['; } + if ($custom_badge) { + echo " "; + } + echo ', get_created_at()); echo ' UTC">about ' . format_timestamp(time() - $emote->get_created_at()) . " ago"; diff --git a/public/users.php b/public/users.php index fced3eb..fcfd92b 100644 --- a/public/users.php +++ b/public/users.php @@ -280,6 +280,18 @@ $fav_reactions = $stmt->fetchAll(PDO::FETCH_ASSOC); // getting favorite emote $fav_emote = 1; +// getting custom badge +$stmt = $db->prepare("SELECT b.* FROM badges b + INNER JOIN user_badges ub ON ub.user_id = ? + WHERE b.id = ub.badge_id +"); +$stmt->execute([$user->id()]); + +$custom_badge = null; +if ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { + $custom_badge = $row; +} + if ($is_json) { header("Content-type: application/json"); echo json_encode([ @@ -299,7 +311,8 @@ if ($is_json) { "active_emote_set_id" => $active_emote_set["id"], "emote_sets" => $emote_sets, "uploaded_emotes" => $uploaded_emotes, - "actions" => $actions + "actions" => $actions, + "custom_badge" => $custom_badge ] ]); exit; @@ -327,8 +340,14 @@ if ($is_json) {