-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpost.php
380 lines (307 loc) · 14.7 KB
/
post.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
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
<?php
session_id("user");
session_start();
require "database.php";
$id = $_GET["id"];
$_SESSION['currentPostID'] = $id;
$loggedUser = empty($_SESSION['userid']) ? "" : $_SESSION['userid'];
// querying post table
$stmt = $mysqli->prepare("select title, link, description, username, userid, time from posts where id=?");
if (!$stmt) {
printf("Query Prep Failed: %s\n", $mysqli->error);
exit;
}
$stmt->bind_param('i', $id);
$stmt->execute();
$stmt->bind_result($title, $link, $description, $username, $userid, $time);
while ($stmt->fetch()) {
}
function show_children($parent, $level = 0)
{
include "database.php";
$id = $_GET["id"];
$sql = "select id, userid, username, comment from comments where posts_id='{$id}' AND parentid " . ($parent ? "= $parent" : "IS NULL");
$result = $mysqli->query($sql);
if (!$result) {
printf("Query Prep Failed: %s\n", $mysqli->error);
exit;
}
$loggedUser = empty($_SESSION['userid']) ? "" : $_SESSION['userid'];
while ($row = $result->fetch_assoc()) { ?>
<div id=<?php echo $row["id"]; ?> class='comment level<?php echo $level ?>'>
<p class='comment__name'> <?php echo $row["username"]; ?> </p>
<p class='comment__text'> <?php echo $row["comment"]; ?> </p>
<?php if ($row["userid"] == $loggedUser) {
?>
<button class="button button--tiny edit--comment">Edit</button>
<form method="post" action="./functions/delete/deletecomment.php?">
<button type="submit" class="button button--tiny delete--comment">X</button>
<input type='hidden' name='id' value='<?php echo $row["id"]; ?>' />
<input type='hidden' name='userid' value='<?php echo $row["userid"]; ?>' />
<input type="hidden" name="token" value="<?php echo $_SESSION['token']; ?>" />
</form>
<?php }; ?>
<?php
if (!empty($_SESSION['userid'])) { ?>
<form class="form form--reply" method="post" action="./functions/create/replycomment.php">
<button class="button button--tiny reply--comment">reply</button>
<input type='hidden' name='id' value='<?php echo $row["id"]; ?>' />
<input type="hidden" name="token" value="<?php echo $_SESSION['token']; ?>" />
</form>
</div>
<?php
}
show_children($row['id'], $level + 1);
};
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?php echo htmlspecialchars($title) ?> </title>
<link rel="stylesheet" href="https://use.typekit.net/jyw0vop.css" />
<link rel="stylesheet" href="./styles/resetstyles.css" />
<link rel="stylesheet" href="./styles/styles.css" />
<link rel="stylesheet" href="./styles/post.css" />
</head>
<body>
<div class="authbar authbar--back">
<a href="./home.php">
bacc</a>
<div class="userInfo">
<p><?php echo empty($_SESSION['username']) ? "" : $_SESSION['username'] ?></p>
<?php if (empty($_SESSION['userid'])) { ?>
<button class="button button--login">Login</button>
<button class="button button--signup">Signup</button>
<?php } else { ?>
<form class="form form--auth" action="./auth/logout.php">
<button class="button button--logout">Logout</button>
</form>
<?php } ?>
</div>
</div>
<div class="page">
<div class="post flow">
<div class="post__content flow">
<div class="post__header">
<a class="post__title" href=<?php echo htmlspecialchars($link) ?>><?php echo htmlspecialchars($title) ?> </a>
<div class='post__user'>Posted by <?php echo htmlspecialchars($username); ?> at <?php echo htmlspecialchars($time) ?>
</div>
</div>
<div class='post__text'> <?php echo htmlspecialchars($description); ?> </div>
<?php if ($userid == $loggedUser) { ?>
<div class="post__controls">
<button class="button button--small edit--post">Edit</button>
<form class="form form--edit" method="post" action="./functions/delete/deletepost.php?">
<button class="button button--small" type="submit" class="delete--post">Delete</button>
<input type='hidden' name='userid' value='<?php echo "$userid"; ?>' />
<input type="hidden" name="token" value="<?php echo $_SESSION['token']; ?>" />
</form>
</div>
<?php }; ?>
<div class="post__addcomment">
<?php if (!empty($_SESSION['userid'])) { ?>
<form class="form form--addcomment" action="./functions/create/addcomment.php" method="post">
<textarea name="commentText" placeholder="Comment here"></textarea>
<input class="button button--small" type="submit" value="Comment">
<input type="hidden" name="token" value="<?php echo $_SESSION['token']; ?>" />
</form>
<?php } else { ?>
<textarea name="commentText" placeholder="Login to comment" readonly></textarea>
<?php } ?>
</div>
</div>
<div class="comments">
<div class="flow">
<h2 id="comments">Comments</h2>
<?php show_children(0); ?>
</div>
</div>
</div>
</div>
<div class="modal modal--signup">
<div class="modal__content">
<div class="modal__header">
<div class="close">
<svg class="close__button close__button--signup" viewBox=" 0 0 20 20" xmlns="http://www.w3.org/2000/svg" class="_14dkERGUnSwisNWFcFX-0T">
<polygon fill="inherit" points="11.649 9.882 18.262 3.267 16.495 1.5 9.881 8.114 3.267 1.5 1.5 3.267 8.114 9.883 1.5 16.497 3.267 18.264 9.881 11.65 16.495 18.264 18.262 16.497"></polygon>
</svg>
</div>
<h2 class="header__title">Signup</h2>
</div>
<div class="modal__body">
<form class="form" method="post" action="./auth/signup.php">
<div class="form__inputs">
<input type="text" name="username" placeholder="username" />
<input type="password" name="password" placeholder="password" />
</div>
<button class="button" type="submit">Signup</button>
</form>
</div>
</div>
</div>
<div class="modal modal--login">
<div class="modal__content">
<div class="modal__header">
<div class="close">
<svg class="close__button close__button--login" viewBox=" 0 0 20 20" xmlns="http://www.w3.org/2000/svg" class="_14dkERGUnSwisNWFcFX-0T">
<polygon fill="inherit" points="11.649 9.882 18.262 3.267 16.495 1.5 9.881 8.114 3.267 1.5 1.5 3.267 8.114 9.883 1.5 16.497 3.267 18.264 9.881 11.65 16.495 18.264 18.262 16.497"></polygon>
</svg>
</div>
<h2 class="header__title">Login</h2>
</div>
<div class="modal__body">
<form class="form" method="post" action="./auth/login.php">
<div class="form__inputs">
<input type="text" name="username" placeholder="username" />
<input type="password" name="password" placeholder="password" />
</div>
<button class="button" type="submit">Login</button>
</form>
</div>
</div>
</div>
</body>
<script>
try {
const postEditButton = document.getElementsByClassName("edit--post")[0];
postEditButton.addEventListener('click', event => {
const post = event.target.parentElement.parentElement;
const postText = post.getElementsByClassName("post__text")[0];
const form = document.createElement("FORM");
form.classList.add("form");
form.classList.add("form--edit");
form.setAttribute('method', "post");
form.setAttribute('action', "./functions/update/editpost.php");
const i = document.createElement("textarea"); //input element, text
i.innerHTML = postText.innerHTML.trim();
i.setAttribute('name', "posttext");
const s = document.createElement("input"); //input element, Submit button
s.setAttribute('type', "submit");
s.setAttribute("name", "editPost")
s.setAttribute('value', "done");
s.classList.add("button");
s.classList.add("button--small");
const h = document.createElement("input"); //input element, token button
h.setAttribute('type', "hidden");
h.setAttribute('name', "token");
<?php if (!empty($_SESSION['token'])) { ?>
h.setAttribute('value', "<?php echo $_SESSION['token']; ?>");
<?php } ?>
form.appendChild(i);
form.appendChild(s);
form.appendChild(h);
post.replaceChild(form, postText);
})
} catch (error) {}
const commentEditButtons = document.getElementsByClassName("edit--comment");
for (const item of commentEditButtons) {
item.addEventListener('click', event => {
const comment = event.target.parentElement;
const commentText = comment.getElementsByClassName("comment__text")[0];
const commentid = comment.id;
try {
const editButton = comment.getElementsByClassName("edit--comment")[0];
const deleteButton = comment.getElementsByClassName("delete--comment")[0];
const replyButton = comment.getElementsByClassName("reply--comment")[0];
editButton.remove();
deleteButton.remove();
replyButton.remove();
} catch (error) {}
const form = document.createElement("FORM");
form.classList.add("form");
form.setAttribute('method', "post");
form.setAttribute('action', "./functions/update/editcomment.php?id=" + commentid);
const i = document.createElement("textarea");
i.innerHTML = commentText.innerHTML.trim();
i.setAttribute('type', "textarea");
i.setAttribute('name', "commenttext");
const s = document.createElement("input"); //input element, Submit button
s.setAttribute('type', "submit");
s.setAttribute('value', "done");
s.classList.add("button");
s.classList.add("button--tiny");
const h = document.createElement("input"); //input element, token button
h.setAttribute('type', "hidden");
h.setAttribute('name', "token");
<?php if (!empty($_SESSION['token'])) { ?> h.setAttribute('value', "<?php echo $_SESSION['token']; ?>");
<?php } ?>
form.appendChild(i);
form.appendChild(s);
form.appendChild(h);
comment.replaceChild(form, commentText);
})
};
const commentReplyButtons = document.getElementsByClassName("reply--comment");
for (const item of commentReplyButtons) {
item.addEventListener('click', event => {
event.preventDefault();
const form = event.target.parentElement;
form.style.display = "flex";
const comment = form.parentElement;
try {
const editButton = comment.getElementsByClassName("edit--comment")[0];
const deleteButton = comment.getElementsByClassName("delete--comment")[0];
const replyButton = comment.getElementsByClassName("reply--comment")[0];
editButton.remove();
deleteButton.remove();
replyButton.remove();
} catch (error) {}
const i = document.createElement("textarea"); //input element, text
i.setAttribute('name', "commentText");
i.setAttribute('placeholder', "Reply here");
const d = document.createElement("div");
d.classList.add("reply__controls")
const s = document.createElement("input"); //input element, Submit button
s.setAttribute('type', "submit");
s.setAttribute('value', "done");
s.classList.add("button");
s.classList.add("button--tiny");
const c = document.createElement("button");
c.classList.add("button");
c.classList.add("button--tiny");
c.innerHTML = "cancel";
c.onclick = (event) => {
event.preventDefault();
location.reload(true);
}
form.appendChild(i);
d.appendChild(s);
d.appendChild(c);
form.appendChild(d);
})
}
<?php if (empty($_SESSION['userid'])) { ?>
const signupModal = document.getElementsByClassName("modal--signup")[0];
const signupOpen = document.getElementsByClassName("button--signup")[0];
const signupClose = document.getElementsByClassName("close__button--signup")[0];
const loginModal = document.getElementsByClassName("modal--login")[0];
const loginOpen = document.getElementsByClassName("button--login")[0];
const loginClose = document.getElementsByClassName("close__button--login")[0];
signupOpen.addEventListener('click', event => {
signupModal.style.display = "block";
})
loginOpen.addEventListener('click', event => {
loginModal.style.display = "block";
})
signupClose.addEventListener('click', event => {
signupModal.style.display = "none";
})
loginClose.addEventListener('click', event => {
loginModal.style.display = "none";
})
window.addEventListener('click', event => {
if (event.target == signupModal) {
signupModal.style.display = "none";
}
})
window.addEventListener('click', event => {
if (event.target == loginModal) {
loginModal.style.display = "none";
}
})
<?php } ?>
</script>
</html>