-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemelaboratory.php
More file actions
188 lines (188 loc) · 3.29 KB
/
memelaboratory.php
File metadata and controls
188 lines (188 loc) · 3.29 KB
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
<?php
require_once "db_connect.php";
session_start();
function yid(int $size = 11): string
{
return implode(
"",
array_map(
fn() => "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"[
random_int(0, 63)
],
range(1, $size)
)
);
}
if (!isset($_SESSION["user_id"])) {
header("Location: login.html");
exit();
}
if ($_SERVER["REQUEST_METHOD"] === "POST") {
$userId = $_SESSION["user_id"];
$title = trim($_POST["title"] ?? "");
$content = trim($_POST["description"] ?? "");
$postSpicy = $_POST["spiciness"] ?? 0;
$cappedSpicy = min($postSpicy, 1);
$spicy = (float) max($postSpicy, $cappedSpicy);
$tagIds = array_map("intval", $_POST["tags"] ?? []);
$file = $_FILES["image"] ?? null;
$memeUrl = trim($_POST["meme_url"] ?? "");
$errors = [];
if ($title === "") {
$errors[] = "Title is required.";
}
$useUrl = false;
if (!$file || $file["error"]) {
if (empty($memeUrl)) {
$errors[] = "Either an image file or URL is required.";
} else {
$useUrl = true;
}
}
if (!$useUrl && !$errors) {
$allow = [
"image/avif",
"image/bmp",
"image/gif",
"image/heic",
"image/heif",
"image/jpeg",
"image/jxl",
"image/png",
"image/svg+xml",
"image/tiff",
"image/webp",
];
$mime = mime_content_type($file["tmp_name"]);
if (!in_array($mime, $allow, true)) {
$errors[] = "Unsupported type.";
}
}
if ($errors) {
$_SESSION["upload_errors"] = $errors;
header("Location: memelaboratory.php");
exit();
}
$memeId = yid();
$path = "";
if ($useUrl) {
$path = $memeUrl;
} else {
$uploadId = yid();
$ext = pathinfo($file["name"], PATHINFO_EXTENSION);
$path = "uploads/$uploadId.$ext";
if (!is_dir("uploads")) {
mkdir("uploads");
}
move_uploaded_file($file["tmp_name"], $path);
}
$pdo = getDbConnection();
$pdo->beginTransaction();
try {
$stmt = "
INSERT INTO
memes (
meme_id,
slug,
user_id,
title,
content,
media_url,
status,
visibility,
spiciness,
published_at
)
VALUES
(?, ?, ?, ?, ?, ?, 'published', 'public', ?, NOW())
";
$slug =
strtolower(preg_replace("/[^a-z0-9]+/", "-", $content)) .
"-" .
$memeId;
$pdo->prepare($stmt)->execute([
$memeId,
$slug,
$userId,
$title,
$content,
$path,
$spicy,
]);
if (!$useUrl) {
$stmt = "
INSERT INTO
uploads (
upload_id,
user_id,
file_name,
file_path,
file_type,
file_size,
meme_id,
width,
height
)
VALUES
(?, ?, ?, ?, ?, ?, ?, NULL, NULL)
";
$pdo->prepare($stmt)->execute([
$uploadId,
$userId,
$file["name"],
$path,
$mime,
$file["size"],
$memeId,
]);
}
if ($tagIds) {
$stmt = $pdo->prepare("
INSERT INTO
meme_tags (meme_id, tag_id)
VALUES
(?, ?)
");
foreach ($tagIds as $tid) {
$stmt->execute([$memeId, $tid]);
$pdo->prepare(
"
UPDATE
tags
SET
usage_count = usage_count + 1
WHERE
tag_id = ?
"
)->execute([$tid]);
}
}
$pdo->commit();
header("Location: meme.php?id=$memeId");
exit();
} catch (\PDOException $e) {
throw $e;
header("Location: status.php");
exit();
} catch (\Exception $e) {
throw $e;
}
}
$pdo = getDbConnection();
$tags = $pdo
->query(
"
SELECT
tag_id,
name
FROM
tags
ORDER BY
name
"
)
->fetchAll();
$errors = $_SESSION["upload_errors"] ?? [];
unset($_SESSION["upload_errors"]);
include "memelaboratory.html";
?>