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
|
<?php
include "../../src/accounts.php";
include_once "../../src/config.php";
include_once "../../src/alert.php";
if (!EMOTE_UPLOAD) {
generate_alert("/404.php", "Emote upload is disabled", 403);
exit;
}
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)
{
$stmt = $db->prepare("DELETE FROM emotes WHERE id = ?");
$stmt->execute([$id]);
$db = null;
array_map("unlink", glob("$path/*.*"));
rmdir($path);
}
include "../../src/utils.php";
include "../../src/images.php";
$max_width = EMOTE_MAX_SIZE[0];
$max_height = EMOTE_MAX_SIZE[1];
if ($_SERVER['REQUEST_METHOD'] != "POST") {
include "../../src/partials.php";
echo '' ?>
<html>
<head>
<title>Upload an emote - <?php 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" style="width: 400px;">
<?php display_alert() ?>
<section class="box">
<div class="box navtab">
<div>
<b>Upload a new emote</b>
<p style="font-size:8px;">You can just upload, btw. Anything you want.</p>
</div>
</div>
<div class="box content">
<form action="/emotes/upload.php" method="POST" enctype="multipart/form-data">
<h3>Image<span style="color:red;">*</span></h3>
<input type="file" name="file" id="form-file" accept=".gif,.jpg,.jpeg,.png,.webp" required>
<div id="form-manual-files" style="display:none;">
<input type="file" name="file-1x" id="form-file-1x" accept=".gif,.jpg,.jpeg,.png,.webp">
<label class="inline"
for="file-1x"><?php echo sprintf("%dx%d", EMOTE_MAX_SIZE[0] / 4, EMOTE_MAX_SIZE[1] / 4) ?></label>
<input type="file" name="file-2x" id="form-file-2x" accept=".gif,.jpg,.jpeg,.png,.webp">
<label class="inline"
for="file-2x"><?php echo sprintf("%dx%d", EMOTE_MAX_SIZE[0] / 2, EMOTE_MAX_SIZE[1] / 2) ?></label>
<input type="file" name="file-3x" id="form-file-3x" accept=".gif,.jpg,.jpeg,.png,.webp">
<label class="inline"
for="file-3x"><?php echo sprintf("%dx%d", EMOTE_MAX_SIZE[0], EMOTE_MAX_SIZE[1]) ?></label>
</div>
<div>
<label for="manual" class="inline">Manual resize</label>
<input type="checkbox" name="manual" value="1" onchange="display_manual_resize()">
</div>
<h3>Emote name<span style="color:red;">*</span></h3>
<input type="text" name="code" id="code" required>
<div>
<label for="visibility" class="inline">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>
</div>
<label for="notes">Approval notes (optional)</label>
<textarea name="notes" id="form-notes"></textarea>
<div>
<label for="tos" class="inline">Do you accept <a href="/rules.php" target="_BLANK">the
rules</a>?<span style="color:red;">*</span></label>
<input type="checkbox" name="tos" value="1" required>
</div>
<button type="submit" id="upload-button">Upload as
<?php echo $uploader_name ?></button>
</form>
</div>
</section>
<section class="box column" 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>
<p style="font-size: 12px;">The result may differ.</p>
</section>
</section>
</div>
</div>
</body>
<script>
const max_width = <?php echo EMOTE_MAX_SIZE[0] ?>;
const max_height = <?php echo EMOTE_MAX_SIZE[1] ?>;
const fileInput = document.getElementById("form-file");
const showcase = document.getElementById("emote-showcase");
const reader = new FileReader();
let manual = false;
fileInput.addEventListener("change", (e) => {
if (manual) return;
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;
for (let i = 3; i > 0; i--) {
place_image(i, m, e, image);
m *= 2;
}
};
};
});
const code = document.getElementById("code");
let validCode = "";
code.addEventListener("input", (e) => {
const regex = <?php echo EMOTE_NAME_REGEX ?>;
if (regex.test(e.target.value) && e.target.value.length <= <?php echo EMOTE_NAME_MAX_LENGTH ?>) {
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);
// Manual resize
function display_manual_resize() {
const manual_files = document.getElementById("form-manual-files");
// resetting previous values
const files = document.querySelectorAll("input[type=file]");
for (let file of files) {
file.value = null;
file.removeAttribute("required");
}
const fileImages = document.querySelectorAll(".emote-image img");
for (let file of fileImages) {
file.setAttribute("src", "");
file.setAttribute("width", "0");
file.setAttribute("height", "0");
}
const fileSizes = document.querySelectorAll(".emote-image .size");
for (let file of fileImages) {
file.innerHTML = "";
}
manual = !manual;
if (manual) {
manual_files.style.display = "block";
fileInput.style.display = "none";
const elements = document.querySelectorAll("#form-manual-files input[type=file]");
for (let elem of elements) {
elem.setAttribute("required", "true");
}
} else {
manual_files.style.display = "none";
fileInput.style.display = "block";
fileInput.setAttribute("required", "true");
}
showcase.style.display = "none";
}
document.getElementById("form-file-1x").addEventListener("change", (e) => {
showcase.style.display = "flex";
place_image(1, 4, e, null);
});
document.getElementById("form-file-2x").addEventListener("change", (e) => {
showcase.style.display = "flex";
place_image(2, 2, e, null);
});
document.getElementById("form-file-3x").addEventListener("change", (e) => {
showcase.style.display = "flex";
place_image(3, 1, e, null);
});
function place_image(image_index, multiplier, e, image) {
let ee = e;
if (image == null) {
reader.readAsDataURL(e.target.files[0]);
reader.onload = (e) => {
const image = new Image();
image.src = e.target.result;
image.onload = () => {
insert_image(image_index, multiplier, e, image);
};
}
} else {
insert_image(image_index, multiplier, e, image);
}
function insert_image(i, m, e, image) {
const max_w = max_width / multiplier;
const max_h = max_height / multiplier;
const parentId = `emote-image-${image_index}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")}`;
}
}
</script>
</html>
<?php
exit;
}
$is_manual = intval($_POST["manual"] ?? "0") == 1;
if ($is_manual && !isset($_FILES["file-1x"], $_FILES["file-2x"], $_FILES["file-3x"])) {
generate_alert("/emotes/upload.php", "No files set");
exit;
}
if (!$is_manual && !isset($_FILES["file"])) {
generate_alert("/emotes/upload.php", "No file set");
exit;
}
$code = str_safe($_POST["code"] ?? "", EMOTE_NAME_MAX_LENGTH);
if ($code == "" || !preg_match(EMOTE_NAME_REGEX, $code)) {
generate_alert("/emotes/upload.php", "Invalid code");
exit;
}
$notes = str_safe($_POST["notes"] ?? "", EMOTE_COMMENT_MAX_LENGTH);
if (empty($notes)) {
$notes = null;
}
$visibility = clamp(intval($_POST["visibility"], EMOTE_VISIBILITY_DEFAULT), 0, 2);
if (MOD_EMOTES_APPROVE && $visibility == 1 && EMOTE_VISIBILITY_DEFAULT != 1) {
$visibility = 2;
}
// creating a new emote record
$db = new PDO(DB_URL, DB_USER, DB_PASS);
$id = bin2hex(random_bytes(16));
$stmt = $db->prepare("INSERT INTO emotes(id, code, notes, uploaded_by, visibility) VALUES (?, ?, ?, ?, ?)");
$stmt->execute([$id, $code, $notes, $uploaded_by, $visibility]);
$path = "../static/userdata/emotes/$id";
if (!is_dir($path)) {
mkdir($path, 0777, true);
}
if ($is_manual) {
$image_1x = $_FILES["file-1x"];
$image_2x = $_FILES["file-2x"];
$image_3x = $_FILES["file-3x"];
$file_1x = does_file_meet_requirements($image_1x["tmp_name"], $max_width / 4, $max_height / 4);
$file_2x = does_file_meet_requirements($image_2x["tmp_name"], $max_width / 2, $max_height / 2);
$file_3x = does_file_meet_requirements($image_3x["tmp_name"], $max_width, $max_height);
if (!$file_1x[0] || !$file_2x[0] || !$file_3x[0]) {
generate_alert("/emotes/upload.php", "Files don't meet requirements");
abort_upload($path, $db, $id);
exit;
}
if (
!move_uploaded_file($image_1x["tmp_name"], "$path/1x.$file_1x[1]") ||
!move_uploaded_file($image_2x["tmp_name"], "$path/2x.$file_2x[1]") ||
!move_uploaded_file($image_3x["tmp_name"], "$path/3x.$file_3x[1]")
) {
generate_alert("/emotes/upload.php", "Failed to move the uploaded files");
abort_upload($path, $db, $id);
exit;
}
} else {
$image = $_FILES["file"];
// resizing the image
if ($err = create_image_bundle($image["tmp_name"], $path, $max_width, $max_height)) {
generate_alert("/emotes/upload.php", "Error occurred while processing images ($err)", 500);
abort_upload($path, $db, $id);
exit;
}
if (EMOTE_STORE_ORIGINAL) {
$ext = get_file_extension($image["tmp_name"]) ?? "";
move_uploaded_file($image["tmp_name"], "$path/original.$ext");
}
}
if (ACCOUNT_LOG_ACTIONS && $uploaded_by != null) {
$db->prepare("INSERT INTO actions(user_id, action_type, action_payload) VALUES (?, ?, ?)")
->execute([
$uploaded_by,
"EMOTE_CREATE",
json_encode([
"emote" => [
"id" => $id,
"code" => $code,
"visibility" => $visibility,
"uploaded_by" => $uploaded_by,
]
])
]);
}
$db = null;
if (CLIENT_REQUIRES_JSON) {
json_response([
"status_code" => 201,
"message" => null,
"data" => [
"id" => $id,
"code" => $code,
"uploaded_by" => $uploaded_by
]
], 201);
exit;
}
header("Location: /emotes?id=$id", true, 307);
|