summaryrefslogtreecommitdiff
path: root/public/emotes/upload.php
blob: 5563323e8205d7e86819ceda9e937c65fbfb599e (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
<?php
include "../../src/accounts.php";
include_once "../../src/config.php";
include_once "../../src/alert.php";

authorize_user();

if (!ANONYMOUS_UPLOAD && isset($_SESSION["user_role"]) && !$_SESSION["user_role"]["permission_upload"]) {
    generate_alert("/404.php", "Not enough permissions", 403);
    exit;
}

$uploaded_by = null;
$uploader_name = ANONYMOUS_DEFAULT_NAME;

if (isset($_SESSION["user_role"]) && $_SESSION["user_role"]["permission_upload"]) {
    $uploaded_by = $_SESSION["user_id"] ?? null;
    $uploader_name = $_SESSION["user_name"] ?? ANONYMOUS_DEFAULT_NAME;
}

function abort_upload(string $path, PDO $db, string $id, string $response_text, int $response_code = 400)
{
    $stmt = $db->prepare("DELETE FROM emotes WHERE id = ?");
    $stmt->execute([$id]);
    $db = null;

    array_map("unlink", glob("$path/*.*"));
    rmdir($path);
    http_response_code($response_code);
    exit($response_text);
}

include "../../src/utils.php";
include "../../src/images.php";

// TODO: make it configurable later
$max_width = max(128, 1);
$max_height = max(128, 1);

if ($_SERVER['REQUEST_METHOD'] != "POST") {
    include "../../src/partials.php";

    echo '' ?>
    <html>

    <head>
        <title>Upload an emote at alright.party</title>
        <link rel="stylesheet" href="/static/style.css">
    </head>

    <body>
        <div class="container">
            <div class="wrapper">
                <?php html_navigation_bar() ?>

                <section class="content" style="width: 50%;">
                    <section class="box">
                        <div class="box navtab">
                            <div>
                                <b>Upload a new emote</b>
                                <p style="font-size:8px;">Btw, you can upload anything. Anything you want.</p>
                            </div>
                        </div>
                        <div class="box content">
                            <form action="/emotes/upload.php" method="POST" enctype="multipart/form-data">
                                <h3>Emote name</h3>
                                <input type="text" name="code" id="code" required>
                                <h3>Image </h3>
                                <input type="file" name="file" id="file" accept=".gif,.jpg,.jpeg,.png,.webp" required>

                                <div>
                                    <label for="visibility">Emote visibility: </label>
                                    <select name="visibility" id="form-visibility">
                                        <option value="1">Public</option>
                                        <option value="0">Unlisted</option>
                                    </select><br>
                                    <p id="form-visibility-description" style="font-size: 10px;">test</p>
                                    <label for="tos">Do you accept <a href="/rules">the rules</a>?</label>
                                    <input type="checkbox" name="tos" required>
                                </div>

                                <button type="submit" id="upload-button">Upload as
                                    <?php echo $uploader_name ?></button>
                            </form>
                        </div>
                    </section>

                    <section class="box" id="emote-showcase" style="display: none;">
                        <div class="emote-showcase">
                            <div class="emote-image">
                                <img src="" alt="" id="emote-image-1x">
                                <p>1x</p>
                                <p class="size"></p>
                            </div>
                            <div class="emote-image">
                                <img src="" alt="" id="emote-image-2x">
                                <p>2x</p>
                                <p class="size"></p>
                            </div>
                            <div class="emote-image">
                                <img src="" alt="" id="emote-image-3x">
                                <p>3x</p>
                                <p class="size"></p>
                            </div>
                        </div>
                    </section>
                </section>
            </div>
        </div>
    </body>

    <script>
        const fileInput = document.getElementById("file");
        const showcase = document.getElementById("emote-showcase");
        const reader = new FileReader();
        let isImage = false;
        fileInput.addEventListener("change", (e) => {
            isImage = false;
            showcase.style.display = "flex";
            reader.readAsDataURL(e.target.files[0]);
            reader.onload = (e) => {
                const image = new Image();
                image.src = e.target.result;
                image.onload = () => {
                    let m = 1;
                    let max_width = 128;
                    let max_height = 128;
                    isImage = true;

                    for (let i = 3; i > 0; i--) {
                        let max_w = max_width / m;
                        let max_h = max_height / m;

                        const parentId = `emote-image-${i}x`;
                        const img = document.getElementById(parentId);
                        img.setAttribute("src", e.target.result);

                        let ratio = Math.min(max_w / image.width, max_h / image.height);

                        img.setAttribute("width", Math.floor(image.width * ratio));
                        img.setAttribute("height", Math.floor(image.height * ratio));

                        const sizeElement = document.querySelector(`.emote-image:has(#${parentId}) .size`);
                        sizeElement.innerHTML = `${img.getAttribute("width")}x${img.getAttribute("height")}`;

                        m *= 2;
                    }
                };
            };
        });

        const code = document.getElementById("code");
        let validCode = "";

        code.addEventListener("input", (e) => {
            const regex = /^[a-zA-Z0-9]*$/;

            if (regex.test(e.target.value) && e.target.value.length <= 100) {
                validCode = e.target.value;
            } else {
                e.target.value = validCode;
            }
        });

        const visibility = document.getElementById("form-visibility");
        visibility.addEventListener("change", (e) => {
            set_form_visibility_description(visibility.value);
        });

        function set_form_visibility_description(visibility) {
            const p = document.getElementById("form-visibility-description");

            if (visibility == 1) {
                p.innerHTML = "Emote won't appear on the public list until it passes a moderator's review. It still can be added to chats.";
            } else {
                p.innerHTML = "Emote doesn't appear on the public list and won't be subject to moderation checks. It still can be added to chats.";
            }
        }

        set_form_visibility_description(visibility.value);
    </script>

    </html>

    <?php
    exit;
}

if (!isset($_FILES["file"])) {
    http_response_code(400);
    echo json_encode([
        "status_code" => 400,
        "message" => "No file set",
        "data" => null
    ]);
    exit;
}

$code = str_safe($_POST["code"] ?? "", 500);

if ($code == "") {
    http_response_code(400);
    echo json_encode([
        "status_code" => 400,
        "message" => "Invalid code",
        "data" => null
    ]);
    exit;
}

$image = $_FILES["file"];

if (is_null(list($mime, $ext) = get_mime_and_ext($image["tmp_name"]))) {
    http_response_code(400);
    echo json_encode([
        "status_code" => 400,
        "message" => "Not a valid image",
        "data" => null
    ]);
    exit;
}

$visibility = intval($_GET["visibility"], "0");

// creating a new emote record
$db = new PDO(DB_URL, DB_USER, DB_PASS);

$stmt = $db->prepare("INSERT INTO emotes(code, mime, ext, uploaded_by, visibility) VALUES (?, ?, ?, ?, ?)");
$stmt->execute([$code, $mime, $ext, $uploaded_by, $visibility]);

$id = $db->lastInsertId();

if ($id == 0) {
    $db = null;
    http_response_code(500);
    echo json_encode([
        "status_code" => 500,
        "message" => "Failed to create an emote record",
        "data" => null
    ]);
    exit;
}

$path = "../static/userdata/emotes/$id";

if (!is_dir($path)) {
    mkdir($path, 0777, true);
}

// resizing the image

// 3x image
$resized_image = resize_image($image["tmp_name"], "$path/3x", $max_width, $max_height);
if ($resized_image) {
    abort_upload($path, $db, $id, $resized_image);
}

// 2x image
$resized_image = resize_image($image["tmp_name"], "$path/2x", $max_width / 2, $max_height / 2);
if ($resized_image) {
    abort_upload($path, $db, $id, $resized_image);
}

// 1x image
$resized_image = resize_image($image["tmp_name"], "$path/1x", $max_width / 4, $max_height / 4);
if ($resized_image) {
    abort_upload($path, $db, $id, $resized_image);
}

$db = null;

if (isset($_SERVER["HTTP_ACCEPT"]) && $_SERVER["HTTP_ACCEPT"] == "application/json") {
    http_response_code(201);
    echo json_encode([
        "status_code" => 201,
        "message" => null,
        "data" => [
            "id" => $id,
            "code" => $code,
            "ext" => $ext,
            "mime" => $mime,
            "uploaded_by" => $uploaded_by
        ]
    ]);
    exit;
}

header("Location: /emotes?id=$id", true, 307);