-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
227 lines (227 loc) · 4.97 KB
/
index.php
File metadata and controls
227 lines (227 loc) · 4.97 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
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
<?php
require_once "db_connect.php";
session_start();
if (!isset($_SESSION["user_id"])) {
if (isset($_COOKIE["remember_token"])) {
$token = $_COOKIE["remember_token"];
try {
$pdo = getDbConnection();
$stmt = $pdo->prepare("
SELECT
u.user_id,
u.handle
FROM
user_sessions AS s
JOIN users AS u ON s.user_id = u.user_id
WHERE
s.session_id = ?
AND s.expires_at > NOW()
");
$stmt->execute([$token]);
$user = $stmt->fetch();
if ($user) {
$_SESSION["user_id"] = $user["user_id"];
$_SESSION["handle"] = $user["handle"];
$update = $pdo->prepare("
UPDATE
user_sessions
SET
last_activity = CURRENT_TIMESTAMP
WHERE
session_id = ?
");
$update->execute([$token]);
} else {
setcookie("remember_token", "", time() - 3600, "/");
header("Location: index.html");
exit();
}
} catch (\PDOException $e) {
throw $e;
header("Location: status.php");
exit();
} catch (\Exception $e) {
throw $e;
header("Location: index.html");
exit();
}
} else {
header("Location: index.html");
exit();
}
}
try {
$pdo = getDbConnection();
$stmt = $pdo->prepare("
SELECT
handle,
display_name,
avatar,
joined_at
FROM
users
WHERE
user_id = ?
");
$stmt->execute([$_SESSION["user_id"]]);
$user = $stmt->fetch();
$stmt = $pdo->prepare("
SELECT
m.meme_id,
m.title,
m.slug,
m.media_url,
m.content,
m.spiciness,
m.view_count,
m.like_count,
m.comment_count,
u.handle AS creator
FROM
memes AS m
JOIN users AS u ON m.user_id = u.user_id
WHERE
m.status = 'published'
AND m.visibility = 'public'
ORDER BY
(
m.like_count + m.upvote_count * 2 + m.comment_count * 3
) / (TIMESTAMPDIFF(HOUR, m.published_at, NOW()) + 1) DESC,
m.published_at DESC
LIMIT
3
");
$stmt->execute();
$trendingMemes = $stmt->fetchAll();
foreach ($trendingMemes as $key => $meme) {
$stmt = $pdo->prepare("
SELECT
t.name,
t.slug
FROM
tags AS t
JOIN meme_tags mt ON t.tag_id = mt.tag_id
WHERE
mt.meme_id = ?
");
$stmt->execute([$meme["meme_id"]]);
$tags = $stmt->fetchAll(PDO::FETCH_ASSOC);
$trendingMemes[$key]["tags"] = $tags;
}
$stmt = $pdo->prepare("
SELECT
m.meme_id,
m.title,
m.slug,
m.media_url,
m.content,
m.spiciness,
m.view_count,
m.like_count,
m.comment_count,
u.handle AS creator
FROM
memes AS m
JOIN users AS u ON m.user_id = u.user_id
WHERE
m.status = 'published'
AND m.visibility = 'public'
AND m.meme_id NOT IN (?)
ORDER BY
RAND()
LIMIT
3
");
$trendingIds = array_column($trendingMemes, "meme_id");
$idsToExclude = !empty($trendingIds) ? implode(",", $trendingIds) : "0";
$stmt->execute([$idsToExclude]);
$recommendedMemes = $stmt->fetchAll();
foreach ($recommendedMemes as $key => $meme) {
$stmt = $pdo->prepare("
SELECT
t.name,
t.slug
FROM
tags t
JOIN meme_tags mt ON t.tag_id = mt.tag_id
WHERE
mt.meme_id = ?
");
$stmt->execute([$meme["meme_id"]]);
$tags = $stmt->fetchAll(PDO::FETCH_ASSOC);
$recommendedMemes[$key]["tags"] = $tags;
}
} catch (\PDOException $e) {
throw $e;
header("Location: status.php");
exit();
} catch (\Exception $e) {
throw $e;
}
$joinDate = new DateTime($user["joined_at"] ?? date("Y-m-d H:i:s"));
$joinDateFormatted = $joinDate->format("F Y");
$displayName = !empty($user["display_name"])
? $user["display_name"]
: $user["handle"] ?? "Anon";
if (empty($trendingMemes)) {
$trendingMemes = [
[
"meme_id" => "placeholder1",
"title" => "When the code finally works, but you don't know why",
"creator" => "CringeCoder",
"like_count" => 4200,
"comment_count" => 69,
"spiciness" => 0.0,
"media_url" => null,
"tags" => [],
],
[
"meme_id" => "placeholder2",
"title" => "POV: You're explaining memes to your parents",
"creator" => "BoomerTranslator",
"like_count" => 2800,
"comment_count" => 42,
"spiciness" => 0.0,
"media_url" => null,
"tags" => [],
],
[
"meme_id" => "placeholder3",
"title" => "No one: My brain at 3 a.m.",
"creator" => "InsomniaGuru",
"like_count" => 6900,
"comment_count" => 420,
"spiciness" => 0.0,
"media_url" => null,
"tags" => [],
],
];
}
$categoryMappings = [
"for-you" => ["name" => "For You", "icon" => "🔥"],
"dank" => ["name" => "Dank", "icon" => "🎭"],
"cursed" => ["name" => "Cursed", "icon" => "💀"],
"shitpost" => ["name" => "Shitpost", "icon" => "🤡"],
"big-brain" => ["name" => "Big Brain", "icon" => "🧠"],
"surreal" => ["name" => "Surreal", "icon" => "👽"],
"tech" => ["name" => "Tech", "icon" => "🖥️"],
"gaming" => ["name" => "Gaming", "icon" => "🎮"],
"social-media" => ["name" => "Social Media", "icon" => "📱"],
"classic" => ["name" => "Classic", "icon" => "🗿"],
];
$activeSlug = "for-you";
if (isset($_GET["category"])) {
$requestedSlug = $_GET["category"];
if (array_key_exists($requestedSlug, $categoryMappings)) {
$activeSlug = $requestedSlug;
}
}
$categories = [];
foreach ($categoryMappings as $slug => $details) {
$category = $details;
$category["slug"] = $slug;
$category["isActive"] = $slug === $activeSlug;
$categories[] = $category;
}
include "index_logged_in.html";
?>