-
Notifications
You must be signed in to change notification settings - Fork 0
/
comments_preview.php
288 lines (251 loc) · 11 KB
/
comments_preview.php
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
require_once('auth.php');
// Connect to the database
$db = new SQLite3('database.sqlite');
$stmt = $db->prepare("CREATE TABLE IF NOT EXISTS comments (id INTEGER PRIMARY KEY, imageid TEXT, email TEXT, comment TEXT, created_at DATETIME)");
$stmt->execute();
// Get the imageid from the query string
$imageId = $_GET['imageid'];
// Get the image information from the database
$stmt = $db->prepare("SELECT * FROM images WHERE id=:imageid");
$stmt->bindValue(':imageid', $imageId, SQLITE3_TEXT);
$image = $stmt->execute()->fetchArray(SQLITE3_ASSOC);
// Check if the image exists
if (!$image) {
// Redirect to the homepage if not
header('Location: index.php');
exit();
}
// Check if the form was submitted for adding a new comment
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['comment'])) {
// Get the comment from the form data
$comment = filter_var($_POST['comment'], FILTER_SANITIZE_FULL_SPECIAL_CHARS, FILTER_FLAG_STRIP_LOW);
$comment = nl2br($comment);
$email = $_SESSION['email'];
// Check if the comment is not empty
if (!empty(trim($comment))) {
// Get the current date in the format "years/month/day"
$currentDate = date("Y/m/d");
// Insert the comment into the database
$stmt = $db->prepare("INSERT INTO comments (imageid, email, comment, created_at) VALUES (:imageid, :email, :comment, :created_at)");
$stmt->bindValue(':imageid', $imageId, SQLITE3_TEXT);
$stmt->bindValue(':email', $email, SQLITE3_TEXT);
$stmt->bindValue(':comment', $comment, SQLITE3_TEXT);
$stmt->bindValue(':created_at', $currentDate, SQLITE3_TEXT); // Bind the current date
$stmt->execute();
}
// Redirect back to the image page
$currentURL = $_SERVER['REQUEST_URI'];
$redirectURL = $currentURL;
header("Location: $redirectURL");
exit();
}
// Check if the form was submitted for updating or deleting a comment
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['action'])) {
$action = $_POST['action'];
$comment_id = $_POST['comment_id'];
// Get the email of the current user
$email = $_SESSION['email'];
// Check if the comment belongs to the current user
$stmt = $db->prepare("SELECT * FROM comments WHERE id=:comment_id AND email=:email");
$stmt->bindValue(':comment_id', $comment_id, SQLITE3_INTEGER);
$stmt->bindValue(':email', $email, SQLITE3_TEXT);
$comment = $stmt->execute()->fetchArray(SQLITE3_ASSOC);
if ($comment) {
if ($action == 'delete') {
// Delete the comment from the comments table
$stmt = $db->prepare("DELETE FROM comments WHERE id=:comment_id");
$stmt->bindValue(':comment_id', $comment_id, SQLITE3_INTEGER);
$stmt->execute();
// Delete the corresponding replies from the reply_comments table
$stmt = $db->prepare("DELETE FROM reply_comments WHERE comment_id=:comment_id");
$stmt->bindValue(':comment_id', $comment_id, SQLITE3_INTEGER);
$stmt->execute();
}
}
// Redirect back to the image page
$currentURL = $_SERVER['REQUEST_URI'];
$redirectURL = $currentURL;
header("Location: $redirectURL");
exit();
}
// Set the number of comments to display per page
$comments_per_page = 100;
// Get the current page from the URL, or default to 1
$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
// Calculate the starting offset for the current page
$offset = ($page - 1) * $comments_per_page;
// Get the total number of comments for the current image
$total_comments_stmt = $db->prepare("SELECT COUNT(*) FROM comments WHERE imageid=:imageid");
$total_comments_stmt->bindValue(':imageid', $imageId, SQLITE3_TEXT);
$total_comments = $total_comments_stmt->execute()->fetchArray()[0];
// Calculate the total number of pages
$total_pages = ceil($total_comments / $comments_per_page);
?>
<!DOCTYPE html>
<html lang="en" data-bs-theme="<?php include($_SERVER['DOCUMENT_ROOT'] . '/appearance/mode.php'); ?>">
<head>
<title>Comment Section</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/png" href="icon/favicon.png">
<?php include('bootstrapcss.php'); ?>
<style>
/* For Webkit-based browsers */
::-webkit-scrollbar {
width: 0;
height: 0;
border-radius: 10px;
}
::-webkit-scrollbar-track {
border-radius: 0;
}
::-webkit-scrollbar-thumb {
border-radius: 0;
}
.text-stroke {
-webkit-text-stroke: 3px;
}
</style>
</head>
<body>
<div class="dropdown">
<button class="btn btn-sm fw-bold rounded-pill ms-2 my-2 btn-outline-<?php include($_SERVER['DOCUMENT_ROOT'] . '/appearance/opposite.php'); ?> dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false">
<i class="bi bi-images"></i> sort by
</button>
<ul class="dropdown-menu">
<li><a href="?by=newest&imageid=<?php echo $imageId; ?>&page=<?php echo isset($_GET['page']) ? $_GET['page'] : '1'; ?>" class="dropdown-item fw-bold <?php if(!isset($_GET['by']) || $_GET['by'] == 'newest') echo 'active'; ?>">newest</a></li>
<li><a href="?by=oldest&imageid=<?php echo $imageId; ?>&page=<?php echo isset($_GET['page']) ? $_GET['page'] : '1'; ?>" class="dropdown-item fw-bold <?php if(isset($_GET['by']) && $_GET['by'] == 'oldest') echo 'active'; ?>">oldest</a></li>
<li><a href="?by=top&imageid=<?php echo $imageId; ?>&page=<?php echo isset($_GET['page']) ? $_GET['page'] : '1'; ?>" class="dropdown-item fw-bold <?php if(isset($_GET['by']) && $_GET['by'] == 'top') echo 'active'; ?>">top comments</a></li>
</ul>
</div>
<?php
if(isset($_GET['by'])){
$sort = $_GET['by'];
switch ($sort) {
case 'newest':
include "comments_preview_desc.php";
break;
case 'oldest':
include "comments_preview_asc.php";
break;
case 'top':
include "comments_preview_top.php";
break;
}
}
else {
include "comments_preview_desc.php";
}
?>
<div class="fixed-bottom w-100">
<form action="" method="POST">
<div class="input-group w-100 rounded-0 shadow-lg rounded-4 rounded-bottom-0">
<textarea id="message-input" name="comment" class="form-control fw-medium bg-body-tertiary border-0 rounded-start-4 focus-ring focus-ring-<?php include($_SERVER['DOCUMENT_ROOT'] . '/appearance/mode.php'); ?>" style="height: 40px; max-height: 150px;" placeholder="Post your comment..." aria-label="Type a message..." aria-describedby="basic-addon2"
onkeydown="if(event.keyCode == 13) { this.style.height = (parseInt(this.style.height) + 10) + 'px'; return true; }"
onkeyup="this.style.height = '40px'; var newHeight = (this.scrollHeight + 10 * (this.value.split(/\r?\n/).length - 1)) + 'px'; if (parseInt(newHeight) > 150) { this.style.height = '150px'; } else { this.style.height = newHeight; }"></textarea>
<button type="submit" class="btn bg-body-tertiary border-0 rounded-end-4"><i class="bi bi-send-fill"></i></button>
</div>
</form>
</div>
<br><br><br>
<style>
.text-stroke {
-webkit-text-stroke: 1px;
}
</style>
<script>
let lazyloadImages = document.querySelectorAll(".lazy-load");
let imageContainer = document.getElementById("image-container");
// Set the default placeholder image
const defaultPlaceholder = "icon/bg.png";
if ("IntersectionObserver" in window) {
let imageObserver = new IntersectionObserver(function(entries, observer) {
entries.forEach(function(entry) {
if (entry.isIntersecting) {
let image = entry.target;
image.src = image.dataset.src;
imageObserver.unobserve(image);
}
});
});
lazyloadImages.forEach(function(image) {
image.src = defaultPlaceholder; // Apply default placeholder
imageObserver.observe(image);
image.style.filter = "blur(5px)"; // Apply initial blur to all images
// Remove blur and apply custom blur to NSFW images after they load
image.addEventListener("load", function() {
image.style.filter = ""; // Remove initial blur
if (image.classList.contains("nsfw")) {
image.style.filter = "blur(4px)"; // Apply blur to NSFW images
// Add overlay with icon and text
let overlay = document.createElement("div");
overlay.classList.add("overlay", "rounded");
let icon = document.createElement("i");
icon.classList.add("bi", "bi-eye-slash-fill", "text-white");
overlay.appendChild(icon);
let text = document.createElement("span");
text.textContent = "R-18";
text.classList.add("shadowed-text", "fw-bold", "text-white");
overlay.appendChild(text);
image.parentNode.appendChild(overlay);
}
});
});
} else {
let lazyloadThrottleTimeout;
function lazyload() {
if (lazyloadThrottleTimeout) {
clearTimeout(lazyloadThrottleTimeout);
}
lazyloadThrottleTimeout = setTimeout(function() {
let scrollTop = window.pageYOffset;
lazyloadImages.forEach(function(img) {
if (img.offsetTop < window.innerHeight + scrollTop) {
img.src = img.dataset.src;
img.classList.remove("lazy-load");
}
});
lazyloadImages = Array.from(lazyloadImages).filter(function(image) {
return image.classList.contains("lazy-load");
});
if (lazyloadImages.length === 0) {
document.removeEventListener("scroll", lazyload);
window.removeEventListener("resize", lazyload);
window.removeEventListener("orientationChange", lazyload);
}
}, 20);
}
document.addEventListener("scroll", lazyload);
window.addEventListener("resize", lazyload);
window.addEventListener("orientationChange", lazyload);
}
// Infinite scrolling
let loading = false;
function loadMoreImages() {
if (loading) return;
loading = true;
// Simulate loading delay for demo purposes
setTimeout(function() {
for (let i = 0; i < 10; i++) {
if (lazyloadImages.length === 0) {
break;
}
let image = lazyloadImages[0];
imageContainer.appendChild(image);
lazyloadImages = Array.from(lazyloadImages).slice(1);
}
loading = false;
}, 1000);
}
window.addEventListener("scroll", function() {
if (window.innerHeight + window.scrollY >= imageContainer.clientHeight) {
loadMoreImages();
}
});
// Initial loading
loadMoreImages();
</script>
<?php include('bootstrapjs.php'); ?>
</body>
</html>