diff options
| author | ilotterytea <iltsu@alright.party> | 2025-05-16 01:04:52 +0500 |
|---|---|---|
| committer | ilotterytea <iltsu@alright.party> | 2025-05-16 01:15:12 +0500 |
| commit | 0fac4eabc661e4425cf21d461c0e18b542fa8003 (patch) | |
| tree | e4737cd1a1e311a47d59fbff569ff696e33cd46e | |
| parent | 98e7d4bcb8fdf99d97f95be2fb0cc18a0543808b (diff) | |
feat: some code refactoring
| -rw-r--r-- | public/emotes/index.php | 230 | ||||
| -rw-r--r-- | public/emotesets.php | 43 | ||||
| -rw-r--r-- | public/users.php | 228 | ||||
| -rw-r--r-- | src/emote.php | 148 | ||||
| -rw-r--r-- | src/user.php | 106 |
5 files changed, 367 insertions, 388 deletions
diff --git a/public/emotes/index.php b/public/emotes/index.php index 4234297..73aa996 100644 --- a/public/emotes/index.php +++ b/public/emotes/index.php @@ -10,20 +10,64 @@ authorize_user(); $db = new PDO(DB_URL, DB_USER, DB_PASS); -function display_list_emotes(PDO &$db, string $search, string $sort_by, int $page, int $limit): array -{ - $current_user_id = $_SESSION["user_id"] ?? ""; +$user_id = $_SESSION["user_id"] ?? ""; - $user_id = $_SESSION["user_id"] ?? "-1"; - $offset = ($page - 1) * $limit; +$emotes = null; +$emote = null; +$total_emotes = 0; +$total_pages = 0; + +// fetching emote by id +if (isset($_GET["id"])) { + $id = $_GET["id"]; + + $stmt = $db->prepare("SELECT e.id, e.code, e.created_at, e.source, e.visibility, + COALESCE(COUNT(r.rate), 0) as total_rating, + COALESCE(ROUND(AVG(r.rate), 2), 0) AS average_rating, + CASE WHEN up.private_profile = FALSE OR up.id = ? THEN e.uploaded_by ELSE NULL END AS uploaded_by + FROM emotes e + LEFT JOIN user_preferences up ON up.id = e.uploaded_by + LEFT JOIN ratings AS r ON r.emote_id = e.id + WHERE e.id = ? + LIMIT 1 + "); + $stmt->execute([$user_id, $id]); - $sort = match ($sort_by) { + $row = $stmt->fetch(); + + if ($row["id"]) { + // fetching emote tags + $stmt = $db->prepare("SELECT t.code FROM tags t + INNER JOIN tag_assigns ta ON ta.emote_id = ? + WHERE t.id = ta.tag_id + "); + $stmt->execute([$row["id"]]); + $tags = $stmt->fetchAll(PDO::FETCH_ASSOC); + $tags = array_column($tags, "code"); + + $row["tags"] = $tags; + $row["ext"] = "webp"; + $emote = Emote::from_array_with_user($row, $db); + } else { + generate_alert("/404.php", "Emote ID $id does not exists", 404); + exit; + } +} +// fetching all emotes +else { + $sort = $_GET["sort"] ?? "high_ratings"; + $sort = match ($sort) { "low_ratings" => "rating ASC", "recent" => "e.created_at DESC", "oldest" => "e.created_at ASC", default => "rating DESC" }; + $page = max(1, intval($_GET["p"] ?? "1")); + $limit = 50; + $offset = ($page - 1) * $limit; + $search = $_GET["q"] ?? ""; + // fetching emotes $stmt = $db->prepare("SELECT e.*, CASE WHEN up.private_profile = FALSE OR up.id = ? THEN e.uploaded_by ELSE NULL END AS uploaded_by, CASE WHEN EXISTS ( @@ -46,10 +90,10 @@ function display_list_emotes(PDO &$db, string $search, string $sort_by, int $pag "); $sql_search = "%$search%"; - $current_emote_set_id = $_SESSION["user_active_emote_set_id"] ?? ""; + $user_emote_set_id = $_SESSION["user_active_emote_set_id"] ?? ""; - $stmt->bindParam(1, $current_user_id, PDO::PARAM_STR); - $stmt->bindParam(2, $current_emote_set_id, PDO::PARAM_STR); + $stmt->bindParam(1, $user_id, PDO::PARAM_STR); + $stmt->bindParam(2, $user_emote_set_id, PDO::PARAM_STR); $stmt->bindParam(3, $search, PDO::PARAM_STR); $stmt->bindParam(4, $sql_search, PDO::PARAM_STR); $stmt->bindParam(5, $limit, PDO::PARAM_INT); @@ -57,117 +101,15 @@ function display_list_emotes(PDO &$db, string $search, string $sort_by, int $pag $stmt->execute(); + $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); $emotes = []; - $rows = $stmt->fetchAll(); - foreach ($rows as $row) { - $uploader = null; - - if ($row["uploaded_by"]) { - $private_profile = $row["uploaded_by"] == ($_SESSION["user_id"] ?? "") ? "" : "AND up.private_profile = FALSE"; - $stmt = $db->prepare("SELECT u.id, u.username FROM users u - INNER JOIN user_preferences up ON up.id = u.id - WHERE u.id = ? $private_profile - "); - $stmt->execute([$row["uploaded_by"]]); - - $uploader = $stmt->fetch(PDO::FETCH_ASSOC) ?? null; - } - - array_push($emotes, new Emote( - $row["id"], - $row["code"], - "webp", - intval(strtotime($row["created_at"])), - $uploader, - $row["is_in_user_set"], - $row["rating"], - $row["visibility"], - $row["source"], - [] - )); - } - - return $emotes; -} - -function display_emote(PDO &$db, string $id) -{ - $stmt = $db->prepare("SELECT e.*, COALESCE(COUNT(r.rate), 0) as total_rating, - COALESCE(ROUND(AVG(r.rate), 2), 0) AS average_rating, - CASE WHEN up.private_profile = FALSE OR up.id = ? THEN e.uploaded_by ELSE NULL END AS uploaded_by - FROM emotes e - LEFT JOIN user_preferences up ON up.id = e.uploaded_by - LEFT JOIN ratings AS r ON r.emote_id = e.id - WHERE e.id = ?"); - $stmt->execute([$_SESSION["user_id"] ?? "", $id]); - - $emote = null; - - if ($row = $stmt->fetch()) { - if ($row["id"] != null) { - $stmt = $db->prepare("SELECT t.code FROM tags t - INNER JOIN tag_assigns ta ON ta.emote_id = ? - WHERE t.id = ta.tag_id - "); - $stmt->execute([$row["id"]]); - - $tags = $stmt->fetchAll(PDO::FETCH_ASSOC); - $tags = array_column($tags, "code"); - - $emote = new Emote( - $row["id"], - $row["code"], - "webp", - intval(strtotime($row["created_at"])), - $row["uploaded_by"], - false, - ["total" => $row["total_rating"], "average" => $row["average_rating"]], - $row["visibility"], - $row["source"], - $tags - ); - } + array_push($emotes, Emote::from_array_with_user($row, $db)); } - if ($emote == null) { - if (CLIENT_REQUIRES_JSON) { - json_response([ - "status_code" => 404, - "message" => "Emote ID $id does not exist", - "data" => null - ], 404); - exit; - } - - header("Location: /404.php"); - exit; - } - - return $emote; -} - -$emotes = null; -$emote = null; - -$id = $_GET["id"] ?? ""; - -$db = new PDO(DB_URL, DB_USER, DB_PASS); - -$page = max(1, intval($_GET["p"] ?? "1")); -$limit = 50; -$total_emotes = 0; -$total_pages = 0; -$search = $_GET["q"] ?? ""; -$sort_by = $_GET["sort_by"] ?? ""; - -if (empty($id)) { - $emotes = display_list_emotes($db, $search, $sort_by, $page, $limit); $total_emotes = count($emotes); $total_pages = ceil($total_emotes / $limit); -} else { - $emote = display_emote($db, $id); } if (CLIENT_REQUIRES_JSON) { @@ -412,24 +354,13 @@ if (CLIENT_REQUIRES_JSON) { $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, 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()]); + $u = $emote->get_uploaded_by(); + $show_private_badge = $u->private_profile; - if ($row = $stmt->fetch()) { - $show_private_badge = $row["private_profile"]; - - $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"]; - } + $username = $u->username; + $link = "/users.php?id={$u->id}"; + $badge = $u->role; + $custom_badge = $u->custom_badge; } echo "<a href=\"$link\">"; @@ -440,12 +371,12 @@ if (CLIENT_REQUIRES_JSON) { echo " <img src='/static/img/icons/eye.png' alt='(Private)' title='You are the only one who sees this' />"; } - if ($badge && $badge["role_badge_id"]) { - echo ' <img src="/static/userdata/badges/' . $badge["role_badge_id"] . '/1x.webp" alt="## ' . $badge["role_name"] . '" title="' . $badge["role_name"] . '" />'; + if ($badge && $badge->badge) { + echo " <img src='/static/userdata/badges/{$badge->badge->id}/1x.webp' alt='## {$badge->name}' title='{$badge->name}' />"; } if ($custom_badge) { - echo " <img src='/static/userdata/badges/$custom_badge/1x.webp' alt='' title='Personal badge' />"; + echo " <img src='/static/userdata/badges/{$custom_badge->id}/1x.webp' alt='' title='Personal badge' />"; } echo ', <span title="'; @@ -454,25 +385,23 @@ if (CLIENT_REQUIRES_JSON) { ?></td> </tr> <?php - $stmt = $db->prepare("SELECT u.id, u.username, a.created_at, r.name AS role_name, r.badge_id AS role_badge_id, ub.badge_id AS custom_badge_id - FROM users u + $stmt = $db->prepare("SELECT u.id, a.created_at FROM users u INNER JOIN mod_actions a ON a.emote_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 = a.user_id"); $stmt->execute([$emote->get_id()]); if ($row = $stmt->fetch()) { + $approver = User::get_user_by_id($db, $row["id"]); + echo '<tr><th>Approver</th><td>'; - echo '<a href="/users.php?id=' . $row["id"] . '" target="_blank">' . $row["username"] . '</a>'; + echo "<a href='/users.php?id={$approver->id}' target='_blank'>{$approver->username}</a>"; - if ($row["role_badge_id"]) { - echo ' <img src="/static/userdata/badges/' . $row["role_badge_id"] . '/1x.webp" alt="## ' . $row["role_name"] . '" title="' . $row["role_name"] . '" />'; + if ($approver->role && $approver->role->badge) { + echo " <img src='/static/userdata/badges/{$approver->role->badge->id}/1x.webp' alt='## {$approver->role->name}' title='{$approver->role->name}' />"; } - if ($row["custom_badge_id"]) { - echo " <img src='/static/userdata/badges/" . $row["custom_badge_id"] . "/1x.webp' alt='' title='Personal badge' />"; + if ($approver->custom_badge) { + echo " <img src='/static/userdata/badges/{$approver->custom_badge->id}/1x.webp' alt='' title='Personal badge' />"; } echo ', <span title="'; @@ -591,20 +520,7 @@ if (CLIENT_REQUIRES_JSON) { <?php } else { ?> <div class="box content items"> - <?php - foreach ($emotes as $e) { - echo '<a class="box emote" href="/emotes?id=' . $e->get_id() . '">'; - - if ($e->is_added_by_user()) { - echo '<img src="/static/img/icons/yes.png" class="emote-check" />'; - } - - echo '<img src="/static/userdata/emotes/' . $e->get_id() . '/2x.webp" alt="' . $e->get_code() . '"/>'; - echo '<h1>' . $e->get_code() . '</h1>'; - echo '<p>' . ($e->get_uploaded_by() == null ? (ANONYMOUS_DEFAULT_NAME . "*") : $e->get_uploaded_by()["username"]) . '</p>'; - echo '</a>'; - } - ?> + <?php html_display_emotes($emotes); ?> </div> <?php if ($total_pages > 1) { echo '' ?> diff --git a/public/emotesets.php b/public/emotesets.php index 7044091..d393779 100644 --- a/public/emotesets.php +++ b/public/emotesets.php @@ -74,22 +74,14 @@ $emote_sets = null; // fetching emotes if ($emote_set) { - $emotes = fetch_all_emotes_from_emoteset($db, $emote_set["id"], $user_id, null); - $emote_set["emotes"] = $emotes; + $emote_set = Emoteset::from_array_extended($emote_set, $user_id, $db); } elseif (!EMOTESET_PUBLIC_LIST) { generate_alert("/404.php", "The public list of emotesets is disabled", 403); exit; } else { $emote_sets = []; - foreach ($db->query("SELECT es.* FROM emote_sets es", PDO::FETCH_ASSOC) as $row) { - $emote_set_row = $row; - $emote_set_row["emotes"] = fetch_all_emotes_from_emoteset( - $db, - $emote_set_row["id"], - $user_id, - 5 - ); - array_push($emote_sets, $emote_set_row); + foreach ($db->query("SELECT * FROM emote_sets", PDO::FETCH_ASSOC) as $row) { + array_push($emote_sets, Emoteset::from_array_extended($row, $user_id, $db)); } } @@ -125,7 +117,7 @@ if (CLIENT_REQUIRES_JSON) { <?php $title = match ($emote_set == null) { true => count($emote_sets) . ' emotesets', - false => 'Emoteset - ' . $emote_set["name"], + false => "Emoteset - {$emote_set->name}", }; echo "$title - " . INSTANCE_NAME; @@ -148,32 +140,9 @@ if (CLIENT_REQUIRES_JSON) { <div class="box content small-gap items"> <?php if (!empty($emote_sets)) { - foreach ($emote_sets as $set_row) { - ?> - <a href="/emotesets.php?id=<?php echo $set_row["id"] ?>" class="box"> - <div> - <p><?php echo $set_row["name"] ?></p> - </div> - - <div> - <?php - foreach ($set_row["emotes"] as $emm) { - echo '<img src="/static/userdata/emotes/' . $emm["id"] . '/1x.webp" height="' . EMOTE_MAX_SIZE[1] / 4 . '">'; - } - ?> - </div> - </a> - <?php } - - echo '</div></section>'; + html_display_emoteset($emote_sets); } else if (!empty($emote_set)) { - foreach ($emote_set["emotes"] as $emote_row) { - echo '<a class="box emote" href="/emotes?id=' . $emote_row["id"] . '">'; - echo '<img src="/static/userdata/emotes/' . $emote_row["id"] . '/2x.webp" alt="' . $emote_row["code"] . '" />'; - echo '<h1>' . $emote_row["code"] . '</h1>'; - echo '<p>' . ($emote_row["uploaded_by"] == null ? (ANONYMOUS_DEFAULT_NAME . "*") : $emote_row["uploaded_by"]["username"]) . '</p>'; - echo '</a>'; - } + html_display_emotes($emote_set->emotes); } else { echo 'Nothing found...'; } diff --git a/public/users.php b/public/users.php index fcfd92b..3f27745 100644 --- a/public/users.php +++ b/public/users.php @@ -5,6 +5,7 @@ include_once "../src/partials.php"; include_once "../src/utils.php"; include_once "../src/accounts.php"; include_once "../src/alert.php"; +include_once "../src/emote.php"; authorize_user(); @@ -131,117 +132,68 @@ if ($id == "" && $alias_id == "") { exit; } -$stmt = null; +// --- fetching user +$user = null; -if ($id != "") { - $stmt = $db->prepare("SELECT * FROM users WHERE id = ?"); - $stmt->execute([$id]); -} else if ($alias_id != "") { - $stmt = $db->prepare("SELECT u.* FROM users u - INNER JOIN connections co ON (co.alias_id = ? AND co.platform = 'twitch') - WHERE co.user_id = u.id - "); - $stmt->execute([$alias_id]); -} +// fetching user by connection +if (isset($_GET["alias_id"])) { + $alias_id = $_GET["alias_id"]; + $platform = $_GET["platform"] ?? "twitch"; -$user = null; + $stmt = $db->prepare("SELECT u.id FROM users u + INNER JOIN connections co ON co.alias_id = ? AND co.platform = ? + WHERE co.user_id = u.id + "); + $stmt->execute([$alias_id, $platform]); -if ($row = $stmt->fetch()) { - $user = new User($row); + if ($row = $stmt->fetch()) { + $user = User::get_user_by_id($db, $row["id"]); + } +} +// fetching user by internal id +else if (isset($_GET["id"])) { + $user = User::get_user_by_id($db, $_GET["id"]); } -if ($user == null) { +if (!$user) { generate_alert("/404.php", "The user you requested cannot be found", 404); exit; } // User preferences $stmt = $db->prepare("SELECT * FROM user_preferences WHERE id = ?"); -$stmt->execute([$user->id()]); +$stmt->execute([$user->id]); $user_preferences = $stmt->fetch(PDO::FETCH_ASSOC); -$public_profile = !$user_preferences["private_profile"] || $user->id() == ($_SESSION["user_id"] ?? ""); +$public_profile = !$user_preferences["private_profile"] || $user->id == ($_SESSION["user_id"] ?? ""); -// --- EMOTE SETS --- -// TODO: OPTIMIZE IT ASAP!!! -$emote_sets = []; +// fetching emote sets +$emote_sets = Emoteset::get_all_user_emotesets($db, $user->id); $active_emote_set = null; - -// gathering acquired emote sets -$stmt = $db->prepare("SELECT emote_set_id, is_default FROM acquired_emote_sets WHERE user_id = ?"); -$stmt->execute([$user->id()]); - -while ($row = $stmt->fetch()) { - // getting more info about set - $set_stmt = $db->prepare("SELECT id, name FROM emote_sets WHERE id = ?"); - $set_stmt->execute([$row["emote_set_id"]]); - $set = $set_stmt->fetch(); - - // getting info about emote set content - $em_stmt = $db->prepare( - "SELECT e.id, e.created_at, - CASE WHEN up.private_profile = FALSE OR up.id = ? THEN e.uploaded_by ELSE NULL END AS uploaded_by, - CASE - WHEN esc.code IS NOT NULL THEN esc.code - ELSE e.code - END AS code, - CASE - WHEN esc.code IS NOT NULL THEN e.code - ELSE NULL - END AS original_code - FROM emotes e - LEFT JOIN user_preferences up ON up.id = e.uploaded_by - INNER JOIN emote_set_contents AS esc - ON esc.emote_set_id = ? - WHERE esc.emote_id = e.id - " . ($row["is_default"] ? '' : ' LIMIT 5') - ); - $em_stmt->execute([$_SESSION["user_id"] ?? "", $row["emote_set_id"]]); - - $emote_set_emotes = $em_stmt->fetchAll(PDO::FETCH_ASSOC); - foreach ($emote_set_emotes as &$e) { - $e["ext"] = "webp"; - if ($e["uploaded_by"]) { - $uploaded_by_stmt = $db->prepare("SELECT id, username FROM users WHERE id = ?"); - $uploaded_by_stmt->execute([$e["uploaded_by"]]); - $e["uploaded_by"] = $uploaded_by_stmt->fetch(PDO::FETCH_ASSOC); - } +foreach ($emote_sets as $es) { + if ($es->is_default) { + $active_emote_set = $es; + break; } - - $emote_set = [ - "id" => $set["id"], - "name" => $set["name"], - "emotes" => $emote_set_emotes - ]; - - if ($row["is_default"]) { - $active_emote_set = count($emote_sets); - } - - array_push($emote_sets, $emote_set); } -$active_emote_set = &$emote_sets[$active_emote_set]; - // gathering uploaded emotes $uploaded_emotes = []; if ($public_profile) { - $stmt = $db->prepare("SELECT e.*, - CASE WHEN EXISTS ( - SELECT 1 - FROM emote_set_contents ec - INNER JOIN emote_sets es ON es.id = ec.emote_set_id - WHERE ec.emote_id = e.id AND es.owner_id = ? - ) THEN 1 ELSE 0 END AS is_in_user_set + $stmt = $db->prepare("SELECT e.id, e.code, e.uploaded_by, e.source, e.visibility FROM emotes e WHERE e.uploaded_by = ? ORDER BY e.created_at ASC "); - $stmt->execute([$user->id(), $user->id()]); + $stmt->execute([$user->id]); + + $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); - $uploaded_emotes = $stmt->fetchAll(PDO::FETCH_ASSOC); + foreach ($rows as $row) { + array_push($uploaded_emotes, Emote::from_array_with_user($row, $db)); + } } // gathering actions @@ -249,7 +201,7 @@ $actions = []; if ($public_profile) { $stmt = $db->prepare("SELECT a.* FROM actions a WHERE a.user_id = ? ORDER BY a.created_at DESC LIMIT 15"); - $stmt->execute([$user->id()]); + $stmt->execute([$user->id]); $actions = $stmt->fetchAll(PDO::FETCH_ASSOC); } @@ -257,23 +209,23 @@ if ($public_profile) { // calculating contributions $stmt = $db->prepare("SELECT COUNT(*) FROM emotes WHERE uploaded_by = ?"); -$stmt->execute([$user->id()]); +$stmt->execute([$user->id]); $contributions = intval($stmt->fetch()[0]); $stmt = $db->prepare("SELECT COUNT(*) FROM ratings WHERE user_id = ?"); -$stmt->execute([$user->id()]); +$stmt->execute([$user->id]); $contributions += intval($stmt->fetch()[0]); // getting status $stmt = $db->prepare("SELECT * FROM roles r INNER JOIN role_assigns ra ON ra.user_id = ? WHERE ra.role_id = r.id"); -$stmt->execute([$user->id()]); +$stmt->execute([$user->id]); $role = $stmt->fetch(PDO::FETCH_ASSOC) ?? null; // getting reactions $stmt = $db->prepare("SELECT rate, COUNT(*) AS c FROM ratings WHERE user_id = ? GROUP BY rate ORDER BY c DESC"); -$stmt->execute([$user->id()]); +$stmt->execute([$user->id]); $fav_reactions = $stmt->fetchAll(PDO::FETCH_ASSOC); @@ -285,7 +237,7 @@ $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()]); +$stmt->execute([$user->id]); $custom_badge = null; if ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { @@ -293,27 +245,25 @@ if ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { } if ($is_json) { - header("Content-type: application/json"); - echo json_encode([ + $user_data = (array) $user; + + unset($user_data["private_profile"]); + + $user_data["stats"] = [ + "contributions" => $contributions, + "favorite_reaction_id" => $fav_reactions, + "favorite_emote_id" => $fav_emote + ]; + + $user_data["active_emote_set_id"] = $active_emote_set->id; + $user_data["emote_sets"] = $emote_sets; + $user_data["uploaded_emotes"] = $uploaded_emotes; + $user_data["actions"] = $actions; + + json_response([ "status_code" => 200, "message" => null, - "data" => [ - "id" => $user->id(), - "username" => $user->username(), - "joined_at" => $user->joined_at(), - "last_active_at" => $user->last_active_at(), - "stats" => [ - "role" => $role, - "contributions" => $contributions, - "favorite_reaction_id" => $fav_reaction, - "favorite_emote_id" => $fav_emote - ], - "active_emote_set_id" => $active_emote_set["id"], - "emote_sets" => $emote_sets, - "uploaded_emotes" => $uploaded_emotes, - "actions" => $actions, - "custom_badge" => $custom_badge - ] + "data" => $user_data ]); exit; } @@ -322,14 +272,13 @@ if ($is_json) { <html> <head> - <title><?php echo sprintf("%s - %s", $user->username(), INSTANCE_NAME) ?></title> + <title><?php echo sprintf("%s - %s", $user->username, INSTANCE_NAME) ?></title> <link rel="stylesheet" href="/static/style.css"> <link rel="shortcut icon" href="/static/favicon.ico" type="image/x-icon"> </head> <body> - <div class="background" - style="background-image: url('/static/userdata/banners/<?php echo $user->id() ?>/3x.webp');"> + <div class="background" style="background-image: url('/static/userdata/banners/<?php echo $user->id ?>/3x.webp');"> <div class="background-layer"></div> </div> @@ -342,7 +291,7 @@ if ($is_json) { <section class="box"> <div class="box navtab flex items-center small-gap"> <?php - echo $user->username(); + echo $user->username; if ($custom_badge) { echo ' <img src="/static/userdata/badges/' . $custom_badge["id"] . '/1x.webp" alt="" title="Personal badge" />'; @@ -352,8 +301,8 @@ if ($is_json) { <div class="box content justify-center items-center"> <?php echo '<img src="/static/'; - if (is_dir("static/userdata/avatars/" . $user->id())) { - echo 'userdata/avatars/' . $user->id() . '/3x.webp'; + if (is_dir("static/userdata/avatars/" . $user->id)) { + echo 'userdata/avatars/' . $user->id . '/3x.webp'; } else { echo 'img/defaults/profile_picture.png'; } @@ -392,17 +341,17 @@ if ($is_json) { <th><img src="/static/img/icons/door_in.png"> Joined</th> <?php echo '<td title="'; - echo date("M d, Y H:i:s", $user->joined_at()); - echo ' UTC">about ' . format_timestamp(time() - $user->joined_at()) . " ago</td>"; + echo date("M d, Y H:i:s", $user->joined_at); + echo ' UTC">about ' . format_timestamp(time() - $user->joined_at) . " ago</td>"; ?> </tr> <tr> <th><img src="/static/img/icons/clock.png"> Last activity</th> <?php - $diff = time() - $user->last_active_at(); + $diff = time() - $user->last_active_at; if ($diff > 60) { echo '<td title="'; - echo date("M d, Y H:i:s", $user->last_active_at()); + echo date("M d, Y H:i:s", $user->last_active_at); echo ' UTC">about ' . format_timestamp($diff) . " ago</td>"; } else { echo '<td>Online</td>'; @@ -462,19 +411,12 @@ if ($is_json) { <!-- Current emoteset --> <section class="box grow user-tab" id="user-emotes"> <div class="box navtab"> - <?php echo !empty($active_emote_set) ? $active_emote_set["name"] : "Emotes" ?> + <?php echo !empty($active_emote_set) ? $active_emote_set->name : "Emotes" ?> </div> <div class="box content items flex"> <?php if (!empty($active_emote_set)) { - if (!empty($active_emote_set["emotes"])) { - foreach ($active_emote_set["emotes"] as $emote_row) { - echo '<a class="box emote" href="/emotes?id=' . $emote_row["id"] . '">'; - echo '<img src="/static/userdata/emotes/' . $emote_row["id"] . '/2x.webp" alt="' . $emote_row["code"] . '"/>'; - echo '<h1>' . $emote_row["code"] . '</h1>'; - echo '<p>' . ($emote_row["uploaded_by"] == null ? (ANONYMOUS_DEFAULT_NAME . "*") : $emote_row["uploaded_by"]["username"]) . '</p>'; - echo '</a>'; - echo '</a>'; - } + if (!empty($active_emote_set->emotes)) { + html_display_emotes($active_emote_set->emotes); } else { echo '<p>No emotes found... ' . ((($_SESSION["user_id"] ?? "") == $id) ? 'Start adding emotes and they will appear here! :)</p>' : '</p>'); } @@ -492,24 +434,7 @@ if ($is_json) { <div class="box content items"> <?php if (!empty($emote_sets)) { - foreach ($emote_sets as $set_row) { ?> - <a href="/emotesets.php?id=<?php echo $set_row["id"] ?>" class="box"> - <div> - <?php - echo '<p>' . $set_row["name"] . '</p>'; - ?> - </div> - - <div> - <?php - for ($i = 0; $i < clamp(count($set_row["emotes"]), 0, 5); $i++) { - $e = &$set_row["emotes"][$i]; - echo '<img src="/static/userdata/emotes/' . $e["id"] . '/1x.webp">'; - } - ?> - </div> - </a> - <?php } + html_display_emoteset($emote_sets); } else { echo '<p>No emote sets found... ' . ((($_SESSION["user_id"] ?? "") == $id) ? 'Start adding emotes and you will have one! :)</p>' : '</p>'); } @@ -545,7 +470,7 @@ if ($is_json) { echo '<div class="column">'; echo '<p>'; - echo '<i>' . $user->username() . '</i> '; + echo '<i>' . $user->username . '</i> '; $payload = json_decode($action["action_payload"], true); @@ -629,14 +554,7 @@ if ($is_json) { <?php echo $user_preferences["private_profile"] ? " <img src='/static/img/icons/eye.png' alt='(Private)' title='You are the only one who sees this' />" : "" ?> </div> <div class="box content items"> - <?php - foreach ($uploaded_emotes as $emote_row) { - echo '<a class="box emote" href="/emotes?id=' . $emote_row["id"] . '">'; - echo '<img src="/static/userdata/emotes/' . $emote_row["id"] . '/2x.webp" alt="' . $emote_row["code"] . '"/>'; - echo '<h1>' . $emote_row["code"] . '</h1>'; - echo '</a>'; - } - ?> + <?php html_display_emotes($uploaded_emotes); ?> </div> </section> <?php endif; ?> diff --git a/src/emote.php b/src/emote.php index 74db4ca..01a204a 100644 --- a/src/emote.php +++ b/src/emote.php @@ -1,4 +1,6 @@ <?php +include_once "user.php"; + class Emote { public string $id; @@ -14,18 +16,39 @@ class Emote public array $tags; - function __construct($id, $code, $ext, $created_at, $uploaded_by, $is_in_user_set, $rating, $visibility, $source, $tags) + public static function from_array(array $arr): Emote { - $this->id = $id; - $this->code = $code; - $this->ext = $ext; - $this->created_at = $created_at; - $this->uploaded_by = $uploaded_by; - $this->is_in_user_set = $is_in_user_set; - $this->rating = $rating; - $this->visibility = $visibility; - $this->source = $source; - $this->tags = $tags; + $e = new Emote(); + + $e->id = $arr["id"]; + $e->code = $arr["code"]; + $e->ext = $arr["ext"] ?? "webp"; + $e->uploaded_by = $arr["uploaded_by"]; + $e->created_at = strtotime($arr["created_at"] ?? 0); + $e->is_in_user_set = $arr["is_in_user_set"] ?? false; + $e->visibility = $arr["visibility"]; + $e->source = $arr["source"] ?? null; + $e->tags = $arr["tags"] ?? []; + + if (isset($arr["total_rating"], $arr["average_rating"])) { + $e->rating = [ + "total" => $arr["total_rating"], + "average" => $arr["average_rating"] + ]; + } else { + $e->rating = $arr["rating"] ?? null; + } + + return $e; + } + + public static function from_array_with_user(array $arr, PDO &$db): Emote + { + if ($arr["uploaded_by"]) { + $arr["uploaded_by"] = User::get_user_by_id($db, $arr["uploaded_by"]); + } + + return Emote::from_array($arr); } function get_id() @@ -79,6 +102,58 @@ class Emote } } +class Emoteset +{ + public string $id; + public string $name; + public User|null $owner; + public array $emotes; + + public bool $is_default; + + public static function from_array(array $arr): Emoteset + { + $s = new Emoteset(); + + $s->id = $arr["id"]; + $s->name = $arr["name"]; + $s->owner = $arr["owner_id"]; + $s->emotes = $arr["emotes"] ?? []; + $s->is_default = $arr["is_default"] ?? false; + + return $s; + } + + public static function from_array_extended(array $arr, string $user_id, PDO &$db): Emoteset + { + if ($arr["owner_id"]) { + $arr["owner_id"] = User::get_user_by_id($db, $arr["owner_id"]); + } + + $arr["emotes"] = fetch_all_emotes_from_emoteset($db, $arr["id"], $user_id); + + return Emoteset::from_array($arr); + } + + public static function get_all_user_emotesets(PDO &$db, string $user_id): array + { + $stmt = $db->prepare("SELECT es.*, aes.is_default FROM emote_sets es + INNER JOIN acquired_emote_sets aes ON aes.emote_set_id = es.id + WHERE aes.user_id = ? + "); + $stmt->execute([$user_id]); + + $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); + $emote_sets = []; + + foreach ($rows as $row) { + array_push($emote_sets, Emoteset::from_array_extended($row, $user_id, $db)); + } + + return $emote_sets; + } +} + function fetch_all_emotes_from_emoteset(PDO &$db, string $emote_set_id, string $user_id, int|null $limit = null): array { // fetching emotes @@ -106,17 +181,16 @@ function fetch_all_emotes_from_emoteset(PDO &$db, string $emote_set_id, string $ $stmt = $db->prepare($sql); $stmt->execute([$user_id, $emote_set_id]); - $emotes = $stmt->fetchAll(PDO::FETCH_ASSOC); + $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); + $emotes = []; // fetching uploaders - foreach ($emotes as &$e) { - if ($e["uploaded_by"]) { - $stmt = $db->prepare("SELECT id, username FROM users WHERE id = ?"); - $stmt->execute([$e["uploaded_by"]]); - - $e["uploaded_by"] = $stmt->fetch(PDO::FETCH_ASSOC); + foreach ($rows as $row) { + if ($row["uploaded_by"]) { + $row["uploaded_by"] = User::get_user_by_id($db, $row["uploaded_by"]); } - unset($e); + + array_push($emotes, Emote::from_array($row)); } return $emotes; @@ -171,4 +245,40 @@ function html_featured_emote(PDO &$db) <?php ; } +} + +function html_display_emotes(array $emotes) +{ + foreach ($emotes as $e) { + echo '<a class="box emote" href="/emotes?id=' . $e->get_id() . '">'; + + if ($e->is_added_by_user()) { + echo '<img src="/static/img/icons/yes.png" class="emote-check" />'; + } + + echo '<img src="/static/userdata/emotes/' . $e->get_id() . '/2x.webp" alt="' . $e->get_code() . '"/>'; + echo '<h1>' . $e->get_code() . '</h1>'; + echo '<p>' . ($e->get_uploaded_by() == null ? (ANONYMOUS_DEFAULT_NAME . "*") : $e->get_uploaded_by()->username) . '</p>'; + echo '</a>'; + } +} + +function html_display_emoteset(array $emotesets) +{ + foreach ($emotesets as $es) { + echo "<a href='/emotesets.php?id={$es->id}' class='box column small-gap'>"; + + echo '<div>'; + echo "<p>$es->name</p>"; + echo '</div>'; + + echo '<div class="small-gap row">'; + + foreach ($es->emotes as $e) { + echo "<img src='/static/userdata/emotes/{$e->id}/1x.webp' alt='{$e->code}' title='{$e->code}' height='16' />"; + } + + echo '</div></a>'; + + } }
\ No newline at end of file diff --git a/src/user.php b/src/user.php index e9fec0b..d22eeb4 100644 --- a/src/user.php +++ b/src/user.php @@ -1,36 +1,102 @@ <?php -class User +class Badge { - private string $id; - private string $username; - private int $joined_at; - private int $last_active_at; + public string $id; - function __construct($row) + public static function from_array(array $arr, string $prefix = ""): Badge|null { - $this->id = $row["id"]; - $this->username = $row["username"]; - $this->joined_at = strtotime($row["joined_at"]); - $this->last_active_at = strtotime($row["last_active_at"]); - } + if (!empty($prefix)) { + $prefix .= "_"; + } + if (!isset($arr["{$prefix}badge_id"])) { + return null; + } - function id() - { - return $this->id; + $b = new Badge(); + $b->id = $arr["{$prefix}badge_id"]; + + return $b; } +} + +class Role +{ + public string $name; + public Badge|null $badge; - function username() + public static function from_array(array $arr): Role|null { - return $this->username; + if (!isset($arr["role_name"])) { + return null; + } + + $r = new Role(); + + $r->name = $arr["role_name"]; + $r->badge = Badge::from_array($arr, "role"); + + return $r; } +} + +class User +{ + public string $id; + public string $username; + public int $joined_at; + public int $last_active_at; - function joined_at() + public Badge|null $custom_badge; + + public Role|null $role; + + public bool $private_profile; + + public static function from_array(array $arr): User { - return $this->joined_at; + $u = new User(); + + $u->id = $arr["id"]; + $u->username = $arr["username"]; + $u->joined_at = strtotime($arr["joined_at"] ?? "0"); + $u->last_active_at = strtotime($arr["last_active_at"] ?? "0"); + + $u->private_profile = $row["private_profile"] ?? false; + + $u->custom_badge = Badge::from_array($arr, "custom"); + + $u->role = Role::from_array($arr); + + return $u; } - function last_active_at() + public static function get_user_by_id(PDO &$db, string $user_id): User|null { - return $this->last_active_at; + $stmt = $db->prepare("SELECT + u.id, + u.username, + u.joined_at, + u.last_active_at, + + 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([$user_id]); + + $u = null; + + if ($uploader_row = $stmt->fetch()) { + $u = User::from_array($uploader_row); + } + + return $u; } }
\ No newline at end of file |
