summaryrefslogtreecommitdiff
path: root/public/emotesets.php
blob: 41257e99fddf30e2f0dc08dfedaf7ea7878623b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
<?php
include_once "../src/utils.php";
include_once "../src/config.php";
include_once "../src/accounts.php";
include_once "../src/partials.php";
include_once "../src/alert.php";
authorize_user();

$id = $_GET["id"] ?? "";
$alias_id = $_GET["alias_id"] ?? "";

$db = new PDO(DB_URL, DB_USER, DB_PASS);

$emote_sets = null;
$emote_set = null;

$page = max(1, intval($_GET["p"] ?? "0"));
$total_emotesets = 1;
$total_pages = 1;

if ($id == "global") {
    $stmt = $db->prepare("SELECT * FROM emote_sets WHERE is_global = true");
    $stmt->execute();

    if ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
        $emote_set = $row;

        $stmt = $db->prepare("SELECT 
        e.*, 
        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,
        CASE WHEN up.private_profile = FALSE OR up.id = ? THEN e.uploaded_by ELSE NULL END AS uploaded_by
        FROM emotes e
        JOIN user_preferences up ON up.id = e.uploaded_by
        JOIN emote_set_contents esc ON esc.emote_id = e.id
        WHERE esc.emote_set_id = ?");
        $stmt->execute([$_SESSION["user_id"] ?? "", $emote_set["id"]]);

        $emote_set["emotes"] = $stmt->fetchAll(PDO::FETCH_ASSOC);

        foreach ($emote_set["emotes"] as &$e) {
            if ($uploader_id = $e["uploaded_by"]) {
                $stmt = $db->prepare("SELECT id, username FROM users WHERE id = ?");
                $stmt->execute([$uploader_id]);
                $e["uploaded_by"] = $stmt->fetch(PDO::FETCH_ASSOC);
            }
        }
    }
} else if (intval($id) <= 0 && intval($alias_id) <= 0) {
    if (!EMOTESET_PUBLIC_LIST) {
        generate_alert("/404.php", "The public list of emotesets is disabled", 403);
        exit;
    }

    $limit = 20;
    $offset = ($page - 1) * $limit;

    $stmt = $db->prepare("SELECT * FROM emote_sets LIMIT ? OFFSET ?");
    $stmt->bindParam(1, $limit, PDO::PARAM_INT);
    $stmt->bindParam(2, $offset, PDO::PARAM_INT);
    $stmt->execute();

    $emote_sets = $stmt->fetchAll(PDO::FETCH_ASSOC);

    foreach ($emote_sets as &$e) {
        $stmt = $db->prepare("SELECT e.*, 
        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,
        CASE WHEN up.private_profile = FALSE OR up.id = ? THEN e.uploaded_by ELSE NULL END AS uploaded_by
        FROM emotes e
        JOIN user_preferences up ON up.id = e.uploaded_by
        JOIN emote_set_contents esc ON esc.emote_set_id = ?
        WHERE e.id = esc.emote_id");
        $stmt->execute([$_SESSION["user_id"] ?? "", $e["id"]]);

        $e["emotes"] = $stmt->fetchAll(PDO::FETCH_ASSOC);
        foreach ($e["emotes"] as &$em) {
            if ($em["uploaded_by"]) {
                $stmt = $db->prepare("SELECT id, username FROM users WHERE id = ?");
                $stmt->execute([$em["uploaded_by"]]);
                $em["uploaded_by"] = $stmt->fetch(PDO::FETCH_ASSOC);
            }
        }
    }
    $count_stmt = $db->prepare("SELECT COUNT(*) FROM emote_sets");
    $count_stmt->execute();
    $total_emotesets = intval($count_stmt->fetch()[0]);
    $total_pages = ceil($total_emotesets / $limit);
} else if (intval($alias_id) > 0) {
    $alias_id = intval($alias_id);
    $stmt = $db->prepare("SELECT es.* FROM emote_sets es
    INNER JOIN connections co ON co.alias_id = ?
    WHERE co.user_id = es.owner_id
    ");
    $stmt->execute([$alias_id]);

    if ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
        $emote_set = $row;

        $stmt = $db->prepare("SELECT e.*, 
        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,
        CASE WHEN up.private_profile = FALSE OR up.id = ? THEN e.uploaded_by ELSE NULL END AS uploaded_by
        FROM emotes e
        JOIN user_preferences up ON up.id = e.uploaded_by
        JOIN emote_set_contents esc ON esc.emote_set_id = ?
        WHERE esc.emote_id = e.id");
        $stmt->execute([$_SESSION["user_id"] ?? "", $emote_set["id"]]);

        $emote_set["emotes"] = $stmt->fetchAll(PDO::FETCH_ASSOC);

        foreach ($emote_set["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);
            }
        }
    }
} else {
    $stmt = $db->prepare("SELECT * FROM emote_sets WHERE id = ?");
    $stmt->execute([$id]);

    if ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
        $emote_set = $row;

        $stmt = $db->prepare("SELECT e.*, 
        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,
        CASE WHEN up.private_profile = FALSE OR up.id = ? THEN e.uploaded_by ELSE NULL END AS uploaded_by
        FROM emotes e
        JOIN user_preferences up ON up.id = e.uploaded_by
        JOIN emote_set_contents esc ON esc.emote_set_id = ?
        WHERE esc.emote_id = e.id");
        $stmt->execute([$_SESSION["user_id"] ?? "", $emote_set["id"]]);

        $emote_set["emotes"] = $stmt->fetchAll(PDO::FETCH_ASSOC);
        foreach ($emote_set["emotes"] as &$e) {
            $e["ext"] = "webp";
            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);
            }
        }
    }
}

if (CLIENT_REQUIRES_JSON) {
    if ($emote_sets != null) {
        json_response([
            "status_code" => 200,
            "message" => null,
            "data" => $emote_sets
        ]);
        exit;
    } else if ($emote_set != null) {
        json_response([
            "status_code" => 200,
            "message" => null,
            "data" => $emote_set
        ]);
        exit;
    } else {
        json_response([
            "status_code" => 404,
            "message" => "Emoteset(s) not found",
            "data" => null
        ], 404);
        exit;
    }
}
?>
<html>

<head>
    <title>
        <?php
        echo $emote_sets != null ? (count($emote_sets) . " emotesets") : ('"' . $emote_set["name"] . '" emoteset');
        echo ' - ' . 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="container">
        <div class="wrapper">
            <?php html_navigation_bar() ?>
            <section class="content row">
                <section class="content">
                    <section class="box">
                        <div class="box navtab">
                            <?php echo $emote_set != null ? ('"' . $emote_set["name"] . '" emoteset') : "$total_emotesets emotesets - Page $page/$total_pages" ?>
                        </div>
                        <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
                                            foreach ($set_row["emotes"] as $emm) {
                                                echo '<img src="/static/userdata/emotes/' . $emm["id"] . '/1x.webp">';
                                            }
                                            ?>
                                        </div>
                                    </a>
                                <?php }

                                echo '</div></section>';

                                if ($total_pages > 1) {
                                    echo '' ?>
                                    <section class="box center row">
                                        <?php
                                        html_pagination($total_pages, $page, "/emotesets.php");
                                        ?>
                                        <?php
                                }
                            } 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>';
                                }
                            } else {
                                echo 'No emotesets found...';
                            }
                            ?>
                            </section>
                    </section>
                </section>
        </div>
    </div>
</body>

</html>