summaryrefslogtreecommitdiff
path: root/emotes/index.php
blob: 2310e030267e43d176748a6ff2068ecc1b5cf229 (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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
<?php
include "{$_SERVER['DOCUMENT_ROOT']}/lib/emote.php";
include "{$_SERVER['DOCUMENT_ROOT']}/lib/accounts.php";
include_once "{$_SERVER['DOCUMENT_ROOT']}/lib/config.php";
include "{$_SERVER['DOCUMENT_ROOT']}/lib/partials.php";
include "{$_SERVER['DOCUMENT_ROOT']}/lib/utils.php";
include "{$_SERVER['DOCUMENT_ROOT']}/lib/alert.php";

authorize_user();

$db = new PDO(CONFIG['database']['url'], CONFIG['database']['user'], CONFIG['database']['pass']);

$user_id = $_SESSION["user_id"] ?? "";

$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]);

    $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_by = $_GET["s"] ?? "high_ratings";
    $sort = match ($sort_by) {
        "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 (
        SELECT 1
        FROM emote_set_contents ec
        INNER JOIN emote_sets es ON es.id = ec.emote_set_id
        JOIN acquired_emote_sets aes ON aes.emote_set_id = es.id
        WHERE ec.emote_id = e.id AND es.id = ?
    ) THEN 1 ELSE 0 END AS is_in_user_set, COALESCE(COUNT(r.rate), 0) AS rating
    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
    LEFT JOIN tag_assigns ta ON ta.emote_id = e.id
    LEFT JOIN tags t ON t.id = ta.tag_id
    WHERE (t.code = ? OR e.code LIKE ?) AND e.visibility = 1
    GROUP BY 
    e.id, e.code, e.created_at
    ORDER BY $sort
    LIMIT ? OFFSET ?
    ");

    $sql_search = "%$search%";
    $user_emote_set_id = $_SESSION["user_active_emote_set_id"] ?? "";

    $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);
    $stmt->bindParam(6, $offset, PDO::PARAM_INT);

    $stmt->execute();

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

    foreach ($rows as $row) {
        array_push($emotes, Emote::from_array_with_user($row, $db));
    }

    $stmt = $db->prepare("SELECT COUNT(*) FROM emotes WHERE visibility = 1");
    $stmt->execute();
    $total_emotes = $stmt->fetch()[0];
    $total_pages = ceil($total_emotes / $limit);
}

if (CLIENT_REQUIRES_JSON) {
    json_response([
        "status_code" => 200,
        "message" => null,
        "data" => $emotes ?? $emote
    ]);
    exit;
}
?>

<html>

<head>
    <title><?php
    echo ($emote != null ? "Emote " . $emote->get_code() : "Emotes") . ' - ' . CONFIG['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="sidebar">
                    <?php
                    html_navigation_search();
                    html_featured_emote($db);
                    html_random_emote($db);
                    ?>
                </section>
                <section class="content">
                    <?php display_alert() ?>
                    <section class="box">
                        <div class="box navtab row">
                            <?php
                            if ($emote != null) {
                                echo "Emote - " . $emote->get_code();
                                echo '<div class="row small-gap" style="margin-left:auto">';

                                $original_path = "/static/userdata/emotes/" . $emote->get_id();
                                $files = glob($_SERVER["DOCUMENT_ROOT"] . $original_path . "/original.*");

                                if (!empty($files)) {
                                    $filename = basename($files[0]);
                                    echo "<a href='$original_path/$filename' target='_BLANK'><img src='/static/img/icons/emotes/emote.png' alt='[Show original]' title='Show original' /></a>";
                                }

                                $stmt = $db->prepare("
                                    SELECT MAX(es.is_featured) AS is_featured, MAX(es.is_global) AS is_global
                                    FROM emote_sets es
                                    JOIN emote_set_contents esc ON esc.emote_set_id = es.id
                                    JOIN emotes e ON esc.emote_id = e.id
                                    WHERE e.id = ?
                                ");
                                $stmt->execute([$emote->get_id()]);

                                if ($row = $stmt->fetch()) {
                                    if ($row["is_featured"]) {
                                        echo '<img src="/static/img/icons/star.png" title="Featured emote" alt="Featured" />';
                                    }
                                    if ($row["is_global"]) {
                                        echo '<img src="/static/img/icons/world.png" title="Global emote" alt="Global" />';
                                    }
                                }
                                echo '</div>';
                            } else {
                                echo "<div class='grow'>Emotes - Page $page/$total_pages</div>";
                                html_emotelist_mode();
                            }
                            ?>
                        </div>
                        <?php
                        if ($emote != null) { ?>
                            <div class="box content">
                                <div class="emote-showcase items-bottom">
                                    <?php
                                    for ($size = 1; $size < 4; $size++) {
                                        echo '<div class="column items-center small-gap">';

                                        echo '<img src="/static/userdata/emotes/';
                                        echo $emote->get_id();
                                        echo "/{$size}x.webp\"";
                                        echo 'title="' . $emote->get_code() . '" />';

                                        $path = $_SERVER["DOCUMENT_ROOT"] . '/static/userdata/emotes/' . $emote->get_id() . "/{$size}x.webp";

                                        echo '<div class="column items-center">';

                                        if ($file_size = filesize($path)) {
                                            $kb = sprintf("%.2f", $file_size / 1024);
                                            echo "<p class='font-small'>{$kb}KB</p>";
                                        }

                                        if ($image_size = getimagesize($path)) {
                                            echo "<p class='font-small'>$image_size[0]x$image_size[1]</p>";
                                        }

                                        echo '</div></div>';
                                    }
                                    ?>
                                </div>
                            </div>
                        </section>
                        <section class="box items row">
                            <?php if (isset($_SESSION["user_id"])) {
                                echo '' ?>
                                <div class="items row left full">
                                    <?php
                                    $added = false;

                                    if (isset($_SESSION["user_active_emote_set_id"])) {
                                        $stmt = $db->prepare("SELECT id, code FROM emote_set_contents WHERE emote_set_id = ? AND emote_id = ?");
                                        $stmt->execute([$_SESSION["user_active_emote_set_id"], $emote->get_id()]);

                                        $added = false;

                                        if ($row = $stmt->fetch()) {
                                            $added = true;
                                            $emote_current_name = $row["code"] ?? $emote->get_code();
                                        }
                                    }

                                    if (isset($_SESSION["user_role"]) && $_SESSION["user_role"]["permission_emoteset_own"]) {
                                        echo '' ?>
                                        <form action="/emotes/setmanip.php" method="POST">
                                            <input type="text" name="id" value="<?= $emote->get_id() ?>" style="display: none;">
                                            <input type="text" name="emote_set_id"
                                                value="<?= $_SESSION["user_active_emote_set_id"] ?>" style="display: none;">
                                            <?php
                                            if ($added) {
                                                ?>
                                                <input type="text" name="action" value="remove" style="display: none;">
                                                <button type="submit" class="red">Remove from my channel</button>
                                            </form>
                                            <form action="/emotes/setmanip.php" method="POST" class="row">
                                                <input type="text" name="id" value="<?= $emote->get_id() ?>" style="display: none;">
                                                <input type="text" name="emote_set_id"
                                                    value="<?= $_SESSION["user_active_emote_set_id"] ?>" style="display: none;">
                                                <input type="text" name="value" id="emote-alias-input"
                                                    value="<?= $emote_current_name ?>" placeholder="<?= $emote->get_code() ?>">
                                                <input type="text" name="action" value="alias" style="display: none;">
                                                <button type="submit" class="transparent"><img src="/static/img/icons/pencil.png"
                                                        alt="Rename" title="Rename"></button>
                                                <?php
                                            } else { ?>
                                                <input type="text" name="action" value="add" style="display: none;">
                                                <button type="submit" class="green">Add to my channel</button>
                                                <?php
                                            }
                                            ?>
                                        </form>
                                        <?php
                                        ;
                                    }
                                    ?>

                                    <?php if ($emote->get_uploaded_by() === $_SESSION["user_id"]): ?>
                                        <form action="/emotes/delete.php" method="post">
                                            <input type="text" name="id" value="<?= $emote->get_id() ?>" style="display: none;">
                                            <button type="submit" class="transparent">
                                                <img src="/static/img/icons/bin.png" alt="Delete emote" title="Delete emote">
                                            </button>
                                        </form>
                                        <form action="/emotes/delete.php" method="post">
                                            <input type="text" name="id" value="<?= $emote->get_id() ?>" style="display: none;">
                                            <input type="text" name="unlink" value="1" style="display:none">
                                            <button type="submit" class="transparent">
                                                <img src="/static/img/icons/link_break.png" alt="Remove your authorship"
                                                    title="Remove your authorship">
                                            </button>
                                        </form>
                                    <?php endif; ?>
                                </div>
                                <div class="items row right full">
                                    <?php
                                    if (isset($_SESSION["user_role"])) {
                                        if ($_SESSION["user_role"]["permission_rate"]) {
                                            $stmt = $db->prepare("SELECT rate FROM ratings WHERE user_id = ? AND emote_id = ?");
                                            $stmt->execute([$_SESSION["user_id"], $id]);

                                            if ($row = $stmt->fetch()) {
                                                echo 'You gave <img src="/static/img/icons/ratings/' . $row["rate"] . '.png" width="16" height="16"';
                                                echo 'title="' . CONFIG['rating']['names'][$row["rate"]] . '">';
                                            } else {
                                                foreach (CONFIG['rating']['names'] as $key => $value) {
                                                    echo '<form action="/emotes/rate.php" method="POST">';
                                                    echo '<input type="text" name="id" value="' . $emote->get_id() . '"style="display: none;">';
                                                    echo "<input type=\"text\" name=\"rate\" value=\"$key\" style=\"display:none;\">";
                                                    echo '<button type="submit" class="transparent">';
                                                    echo "<img
                                                    src=\"/static/img/icons/ratings/$key.png\" alt=\"$value!\"
                                                    title=\"IT'S A $value!\">";
                                                    echo '</button></form>';
                                                }
                                            }
                                        }
                                        if (CONFIG['reports']['enable'] && $_SESSION["user_role"]["permission_report"]) {
                                            echo "<a class='button red' href='/report?emote_id={$emote->id}'>Report emote</a>";
                                        }
                                    }
                                    ?>
                                </div>
                                <?php
                            } else {
                                echo '' ?>
                                <p><a href="/account/login">Log in</a> to get additional features...</p>
                                <?php
                            }
                            ?>
                        </section>

                        <section class="box">
                            <table class="vertical">
                                <?php if (!empty($emote->get_tags())): ?>
                                    <tr>
                                        <th>Tags</th>
                                        <td>
                                            <?php
                                            foreach ($emote->get_tags() as $tag) {
                                                echo "<a href='/emotes/?q=$tag'>$tag</a> ";
                                            }
                                            ?>
                                        </td>
                                    </tr>
                                <?php endif; ?>
                                <tr>
                                    <th>Uploader</th>
                                    <td><?php
                                    $username = CONFIG['anonymous']['defaultname'];
                                    $link = "#";
                                    $show_private_badge = false;
                                    $badge = null;
                                    $custom_badge = null;

                                    if ($emote->get_uploaded_by()) {
                                        $u = $emote->get_uploaded_by();
                                        $show_private_badge = $u->private_profile;

                                        $username = $u->username;
                                        $link = "/users.php?id={$u->id}";
                                        $badge = $u->role;
                                        $custom_badge = $u->custom_badge;
                                    }

                                    echo "<a href=\"$link\">";
                                    echo $username;
                                    echo "</a>";

                                    if ($show_private_badge) {
                                        echo " <img src='/static/img/icons/eye.png' alt='(Private)' title='You are the only one who sees this' />";
                                    }

                                    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->id}/1x.webp' alt='' title='Personal badge' />";
                                    }

                                    echo ', <span title="';
                                    echo date("M d, Y H:i:s", $emote->get_created_at());
                                    echo ' UTC">about ' . format_timestamp(time() - $emote->get_created_at()) . " ago</span>";
                                    ?></td>
                                </tr>
                                <?php
                                $stmt = $db->prepare("SELECT u.id, a.created_at FROM users u
                                    INNER JOIN mod_actions a ON a.emote_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={$approver->id}' target='_blank'>{$approver->username}</a>";

                                    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 ($approver->custom_badge) {
                                        echo " <img src='/static/userdata/badges/{$approver->custom_badge->id}/1x.webp' alt='' title='Personal badge' />";
                                    }

                                    echo ', <span title="';
                                    echo date("M d, Y H:i:s", strtotime($row["created_at"])) . ' UTC">';
                                    echo format_timestamp(strtotime($row["created_at"]) - $emote->get_created_at()) . ' after upload';
                                    echo '</span></td></tr>';
                                }

                                if (CONFIG['rating']['enable']): ?>
                                    <tr>
                                        <th>Rating</th>
                                        <?php
                                        if ($emote->get_rating()["total"] < CONFIG['rating']['minvotes']) {
                                            echo '<td>Not rated (' . $emote->get_rating()["total"] . ')</td>';
                                        } else {

                                            $rating = $emote->get_rating()["average"];

                                            // TODO: make it customizable
                                            list($rating_classname, $rating_name) = match (true) {
                                                in_range($rating, 0.75, 1.0) => [
                                                    "gemerald",
                                                    "<img src='/static/img/icons/ratings/1.png'>
                                        <img src='/static/img/icons/ratings/1.png'>
                                        <img src='/static/img/icons/ratings/1.png'> Shiny Gemerald! 
                                        <img src='/static/img/icons/ratings/1.png'>
                                        <img src='/static/img/icons/ratings/1.png'>
                                        <img src='/static/img/icons/ratings/1.png'>
                                        "
                                                ],
                                                in_range($rating, 0.25, 0.75) => ["gem", "<img src='/static/img/icons/ratings/1.png'> Gem <img src='/static/img/icons/ratings/1.png'>"],
                                                in_range($rating, -0.25, 0.25) => ["iron", "Iron"],
                                                in_range($rating, -0.75, -0.25) => ["coal", "<img src='/static/img/icons/ratings/-1.png'> Coal <img src='/static/img/icons/ratings/-1.png'>"],
                                                in_range($rating, -1.0, -0.75) => [
                                                    "brimstone",
                                                    "
                                        <img src='/static/img/icons/ratings/brimstone.webp'>
                                        <img src='/static/img/icons/ratings/-1.png'>
                                        <img src='/static/img/icons/ratings/brimstone.webp'>
                                        !!!AVOID THIS CANCER-GIVING BRIMSTONE!!!
                                        <img src='/static/img/icons/ratings/brimstone.webp'>
                                        <img src='/static/img/icons/ratings/-1.png'>
                                        <img src='/static/img/icons/ratings/brimstone.webp'>
                                        "
                                                ]
                                            };

                                            echo '<td>';
                                            echo "<span class=\"rating $rating_classname\">$rating_name</span>";
                                            echo ' (' . $emote->get_rating()["total"] . ')';
                                            echo '</td>';
                                        }
                                        ?>
                                    </tr>
                                <?php endif; ?>
                                <tr>
                                    <th>Visibility</th>
                                    <td><?php
                                    switch ($emote->get_visibility()) {
                                        case 0:
                                            echo 'Unlisted';
                                            break;
                                        case 1:
                                            echo 'Public';
                                            break;
                                        case 2:
                                            echo 'Pending approval (unlisted for a moment)';
                                            break;
                                        default:
                                            echo 'N/A';
                                            break;
                                    }
                                    ?></td>
                                </tr>
                                <?php if ($emote->get_source()): ?>
                                    <tr>
                                        <th>Source</th>
                                        <td>
                                            <a href="<?= $emote->get_source() ?>"
                                                target="_blank"><?= $emote->get_source() ?></a>
                                        </td>
                                    </tr>
                                <?php endif; ?>
                            </table>
                        </section>

                        <section class="box">
                            <div class="content">
                                <?php
                                $stmt = $db->prepare("SELECT users.id, users.username
                            FROM users
                            INNER JOIN emote_sets AS es ON es.owner_id = users.id
                            INNER JOIN emote_set_contents AS ec ON ec.emote_set_id = es.id
                            INNER JOIN acquired_emote_sets AS aes ON aes.emote_set_id = es.id
                            WHERE ec.emote_id = ? AND aes.is_default = TRUE");

                                $stmt->execute([$emote->get_id()]);
                                $count = $stmt->rowCount();

                                $db = null;

                                if ($count > 0) {
                                    echo "<p>Added in $count channels</p>";
                                } else {
                                    echo "No one has added this emote yet... :'(";
                                }
                                ?>
                                <div class="items row">
                                    <?php
                                    while ($row = $stmt->fetch()) {
                                        echo '<a href="/users.php?id=' . $row["id"] . '">' . $row["username"] . '</a>';
                                    }
                                    ?>
                                </div>
                            </div>
                            <?php
                        } else { ?>
                            <div class="box content items">
                                <?php html_display_emotes($emotes); ?>
                            </div>
                            <?php if ($total_pages > 1) {
                                echo '' ?>
                            </section>
                            <section class="box center row">
                                <?php
                                html_pagination(
                                    $total_pages,
                                    $page,
                                    "/emotes?q=" . substr($search, 1, strlen($search) - 2) . "&s=$sort_by"
                                );
                            }
                        }
                        ?>
                    </section>
                </section>
            </section>
        </div>
    </div>
</body>

</html>