summaryrefslogtreecommitdiff
path: root/emotesets/index.php
blob: 5be3bda1b90fd10f97cf98492f1e31178db007bc (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
<?php
include_once "{$_SERVER['DOCUMENT_ROOT']}/lib/utils.php";
include_once "{$_SERVER['DOCUMENT_ROOT']}/lib/config.php";
include_once "{$_SERVER['DOCUMENT_ROOT']}/lib/accounts.php";
include_once "{$_SERVER['DOCUMENT_ROOT']}/lib/partials.php";
include_once "{$_SERVER['DOCUMENT_ROOT']}/lib/alert.php";
include_once "{$_SERVER['DOCUMENT_ROOT']}/lib/emote.php";

authorize_user();

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

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

// searching requested emoteset
$emote_set = null;

// global emoteset
if ($id == "global") {
    $rows = $db->query("SELECT * FROM emote_sets WHERE is_global = TRUE LIMIT 1", PDO::FETCH_ASSOC);

    if ($rows->rowCount()) {
        $emote_set = $rows->fetch();
    } else {
        generate_alert("/404.php", "Global emoteset is not found", 404);
        exit;
    }
}
// featured emoteset
else if ($id == "featured") {
    $rows = $db->query("SELECT * FROM emote_sets WHERE is_featured = TRUE LIMIT 1", PDO::FETCH_ASSOC);

    if ($rows->rowCount()) {
        $emote_set = $rows->fetch();
    } else {
        generate_alert("/404.php", "Featured emoteset is not found", 404);
        exit;
    }
}
// connected emoteset
else if (isset($_GET["alias_id"])) {
    $alias_id = $_GET["alias_id"];
    $platform = $_GET["platform"] ?? "twitch";

    $stmt = $db->prepare("SELECT es.* FROM emote_sets es
        INNER JOIN connections co ON co.alias_id = ? AND co.platform = ?
        INNER JOIN acquired_emote_sets aes ON aes.user_id = co.user_id
        WHERE aes.is_default = TRUE
    ");
    $stmt->execute([$alias_id, $platform]);

    if ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
        $emote_set = $row;
    } else {
        generate_alert("/404.php", "Emoteset is not found for alias ID $alias_id ($platform)", 404);
        exit;
    }
}
// specified emoteset
else if (!empty($id)) {
    $stmt = $db->prepare("SELECT es.* FROM emote_sets es WHERE es.id = ?");
    $stmt->execute([$id]);

    if ($row = $stmt->fetch()) {
        $emote_set = $row;
    } else {
        generate_alert("/404.php", "Emoteset ID $id is not found", 404);
        exit;
    }
}

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

// fetching emotes
if ($emote_set) {
    $emote_set = Emoteset::from_array_extended($emote_set, $user_id, $db);
} elseif (!CONFIG['emoteset']['public']) {
    generate_alert("/404.php", "The public list of emotesets is disabled", 403);
    exit;
} else {
    $emote_sets = [];
    foreach ($db->query("SELECT * FROM emote_sets", PDO::FETCH_ASSOC) as $row) {
        array_push($emote_sets, Emoteset::from_array_extended($row, $user_id, $db));
    }
}

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
        $title = match ($emote_set == null) {
            true => count($emote_sets) . ' emotesets',
            false => "Emoteset - {$emote_set->name}",
        };

        echo "$title - " . 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="content">
                    <?php display_alert() ?>

                    <section class="box">
                        <div class="box navtab row">
                            <div class="grow">
                                <?= $title ?>
                            </div>
                            <?php
                            if (!empty($emote_set)) {
                                html_emotelist_mode();
                            }
                            ?>
                        </div>
                        <div class="box content small-gap items">
                            <?php
                            if (!empty($emote_sets)) {
                                html_display_emoteset($emote_sets);
                            } else if (!empty($emote_set)) {
                                html_display_emotes($emote_set->emotes);
                            } else {
                                echo 'Nothing found...';
                            }
                            ?>
                    </section>

                    <?php if (!empty($emote_set)): ?>
                        <section class="box">
                            <?php if (empty($emote_set->editors)): ?>
                                <p>This emoteset does not have any editors.</p>
                            <?php else: ?>
                                <p>
                                    Editors:
                                    <?php foreach ($emote_set->editors as $e): ?>
                                        <a href="/users.php?id=<?= $e['id'] ?>">
                                            <?= $e['username'] ?>
                                        </a>
                                        <?php if (isset($emote_set->owner, $_SESSION['user_id']) && $emote_set->owner->id == $_SESSION['user_id'] && $e['id'] !== $emote_set->owner->id): ?>
                                            <button>
                                                <a
                                                    href="/emotesets/deleditor.php?id=<?= $emote_set->id ?>&username=<?= $e['username'] ?>">
                                                    <img src="/static/img/icons/no.png" alt="X">
                                                </a>
                                            </button>
                                        <?php endif; ?>
                                    <?php endforeach; ?>
                                </p>
                            <?php endif; ?>
                        </section>
                        <?php if (isset($_SESSION['user_id'], $emote_set->owner) && $emote_set->owner->id === $_SESSION['user_id']): ?>
                            <section class="box">
                                <div class="navtab box">
                                    <p>Actions</p>
                                </div>
                                <div class="content box">
                                    <form action="/emotesets/addeditor.php" method="post" class="row small-gap">
                                        <p>Add editor:</p>
                                        <input type="text" name="id" value="<?= $emote_set->id ?>" style="display:none">
                                        <input type="text" name="username" placeholder="TinyEmotes username">
                                        <button type="submit"><img src="/static/img/icons/yes.png" alt="Add"></button>
                                    </form>
                                </div>
                            </section>
                        <?php endif; ?>
                    <?php endif; ?>
                </section>
            </section>
        </div>
    </div>
</body>

</html>