-
Notifications
You must be signed in to change notification settings - Fork 1
/
view.php
372 lines (312 loc) · 16.5 KB
/
view.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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<?php
session_start();
include "config.php";
$stylePath = "/assets/css/";
// SELECT
$stmt = $con->prepare("SELECT * FROM stylesheets");
$stmt->execute();
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
echo "<link rel='stylesheet' href='" . $stylePath . $row['fileName'] . "' data-theme='" . $row['title'] . "'>";
}
$stmt->close();
include 'navigate.php';
$id = $_GET['entry'];
$stmt = $con->prepare("SELECT * FROM blogs WHERE id = ?");
$stmt->bind_param("s", $id);
$stmt->execute();
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
$title = $row['title'];
?>
<title><?php echo $title; ?></title>
<?php }
$stmt->close(); ?>
<script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://cdnjs.cloudflare.com/ajax/libs/tinymce/5.10.0/tinymce.min.js"></script>
<script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
</head>
<body>
<main class="container">
<div class="wrapper">
<?php
date_default_timezone_set("US/Eastern");
// shows approve button (might need to change how this works)
if (isset($_GET['approveView'])) {
$id = $_GET['approveView'];
echo '<a id="approve" href="/view.php?approve=' . $id . '">Approve this Entry</a>';
} else if (isset($_GET['entry'])) {
$id = $_GET['entry'];
}
$stmt = $con->prepare("SELECT * FROM blogs WHERE id = ?");
$stmt->bind_param("s", $id);
$stmt->execute();
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
$id = $row['id'];
$name = $row['owner_name'];
$link = $row['owner_link'];
$bio = $row['owner_bio'];
$dateposted = $row['dateposted'];
$timeposted = $row['timeposted'];
$title = $row['title'];
$entry = $row['entry'];
$formattedDate = date("l, F j, Y", strtotime($dateposted));
$formattedTime = date("g:iA", strtotime($timeposted));
$qry = $con->prepare("SELECT * FROM comments WHERE blogid=?");
$qry->bind_param("s", $id);
$qry->execute();
$qry->store_result();
$match = $qry->num_rows();
$randomNum = rand(5, 15);
echo '<article class="blog">';
if (isset($_SESSION['username']) && $_SESSION['username'] == true) {
echo '<div class="flexEdit">';
echo '<a class="editEntry" href="view.php?edit=' . $id . '">Edit</a>';
echo '<a class="deleteEntry" href="view.php?delete=' . $id . '">Delete</a>';
echo '</div>';
//echo "Welcome to the member's area, " . $_SESSION['username'] . "!";
}
echo '<header class="title"><h2>' . $title . '</h2></header>';
echo '<div class="date">Posted on ' . $formattedDate . ' at ' . $formattedTime . '</div>';
echo '<div class="author">by <a href="' . $link . '">' . $name . '</a></div>';
echo '<div class="post">' . $entry . '</div>';
echo '<section class="bioArea"><div><header><h3 class="about">About the Author</h3></header><p>' . $name . ' is ' . $bio . '</p>';
if ($link !== '') {
echo '<p class="link">You can find ' . $name . ' <a href="' . $link . ' target="_blank">here</a>.</p>';
}
echo '</section>';
// shows approve button (might need to change how this works)
if (isset($_GET['approveView'])) {
$id = $_GET['approveView'];
echo '<a id="approve" href="/view.php?approve=' . $id . '">Approve this Entry</a>';
} else if (isset($_GET['entry'])) {
$id = $_GET['entry'];
}
echo '</div>';
echo '<div>';
echo '</div>';
echo '</article>';
echo '</div>';
echo '</div>';
echo '</div>';
echo '<div class="footer"></div>';
}
// APPROVAL LOGIC
if (isset($_GET['approve'])) {
if (isset($_SESSION['username']) && $_SESSION['username'] == true) {
$id = $_GET['approve'];
$stmt = $con->prepare("UPDATE blogs SET approved = 1 WHERE id = ?");
$stmt->bind_param("s", $id);
$stmt->execute();
$stmt->close();
header("Location: approve/");
}
}
// EDIT POST LOGIC - this turns everything 'editable'
if (isset($_GET['edit'])) {
$stmt->close();
$id = $_GET['edit'];
$stmt = $con->prepare("SELECT * FROM blogs WHERE id = ?");
$stmt->bind_param("s", $id);
$stmt->execute();
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
$id = $row['id'];
$name = $row['owner_name'];
$email = $row['owner_email'];
$link = $row['owner_link'];
$bio = $row['owner_bio'];
$dateposted = $row['dateposted'];
$timeposted = $row['timeposted'];
$title = $row['title'];
$entry = $row['entry'];
$formattedDate = date("l, F j, Y", strtotime($dateposted));
$formattedTime = date("g:iA", strtotime($timeposted));
echo '<form id="submitEntry" method="POST" action="submit.php">';
echo '<label>Title</label> <input type="text" name="titleInput" id="titleInput" value="' . $title . '"><br>';
echo '<label></label><textarea name="entryInput" id="entryInput">' . $entry . '</textarea><br>';
echo '<label>Name/Alias</label><input type="text" name="nameInput" id="nameInput" value="' . $name . '"><br>';
echo '<label>Short Bio</label><textarea name="bioInput" id="bioInput">' . $bio . '</textarea>';
echo '<label>Email</label><input type="email" name="emailInput" id="emailInput" value="' . $email . '"><br>';
echo '<label>Link to your site</label><input type="url" name="linkInput" id="linkInput" value="' . $link . '"><br>';
echo '<input type="hidden" name="id" value="' . $id .'">';
echo '<input type="submit" name="submitEdit" value="Submit Edit">';
}
}
// DELETE LOGIC
if (isset($_GET['delete'])) {
// check if logged in
if (!isset($_SESSION["username"])) {
header('location: error.php');
}
$id = $_GET["delete"];
$stmt = $con->prepare("DELETE FROM blogs WHERE id = ?");
$stmt->bind_param("s", $id);
$stmt->execute();
$stmt->close();
header('location: index.php');
}
// DELETE COMMENT LOGIC
if (isset($_GET['deleteComment'])) {
// check if logged in
if (!isset($_SESSION["username"])) {
header('location: error.php');
}
$id = $_GET["deleteComment"];
$stmt = $con->prepare("DELETE FROM comments WHERE id = ?");
$stmt->bind_param("s", $id);
$stmt->execute();
$stmt->close();
header('location: index.php');
}
?>
<?php
// this conditionally hides all of the comment stuff for editing posts
if (!isset($_GET['edit'])) {
?>
<div class="commentsArea">
<div class="leaveComment">
<details open><summary><strong>Leave a comment</strong></summary><br><br>
<div class="form">
<form method='post' action=''>
<input type="hidden" name="id" value="<?php echo $id; ?>">
<input type="hidden" name="honeypot">
<label for="nickname">Nickname</label><br>
<input type="text" class="form-control" name="nickname" id="nickname" required>
<br>
<label for="email">Email</label><br>
<input type="email" class="form-control" name="email" id="email"><br>
<sup>This field will not be posted publicly.</sup>
<br>
<label for="comment">Comment</label><br>
<textarea class="form-control" name="comment" id="comment" required="required" maxlength="10000"></textarea><br>
<button class="btn" type="submit" name="postComment">Post</button>
</form>
</details>
<br><br>
<?php
// post comments
if (isset($_POST['postComment'])){
$nickname = trim($_POST['nickname']);
$email = trim($_POST['email']);
$comment = trim($_POST['comment']);
$dateposted = date("Y-m-d H:i:s");
$id = $_GET['entry'];
// Check fields are empty or not
if ($comment == ''){
$error_message = "You can't submit a blank comment.";
echo $error_message;
}
$honeypot = $_POST['honeypot'];
if(!empty($honeypot)){
return; //you may add code here to echo an error etc.
}else{
// Insert records
$insertSQL = "INSERT INTO comments(blogid, nickname, email, dateposted, comment) values(?,?,?,?,?)";
$stmt = $con->prepare($insertSQL);
$stmt->bind_param("sssss",$id, $nickname, $email, $dateposted, $comment);
$stmt->execute();
$stmt->close();
echo "<script>history.pushState({}, '', '')</script>";
}
}
?>
<div class="comments">
<details><summary>
<?php
// read comments
//$id = $_GET['entry'];
$stmt = $con->prepare("SELECT * FROM comments WHERE blogid = ? ORDER BY blogid ");
$stmt->bind_param("s", $id);
$stmt->execute();
$result = $stmt->get_result();
$number = mysqli_num_rows($result);
if ($number !==0) {
echo "<strong>View comments (" . $number . ")</strong></summary>";
while ($row = $result->fetch_assoc()) {
$id = $row['id'];
$CommentDate=$row["dateposted"];
$Nickname=$row["nickname"];
$Email=$row["email"];
$Comment=$row["comment"];
$formattedCommentDate = date("m/d/y", strtotime($CommentDate));
$formattedCommentTime = date("g:iA", strtotime($CommentDate));
//$match = $stmt->num_rows();
?>
<div class="comment">
<div class="nickname">
<strong>From</strong> <?php echo $Nickname; ?>
<span class="commentDate">
<strong>on</strong> <?php echo $formattedCommentDate ?> at <?php echo $formattedCommentTime ?>
</span>
</div>
<div class="message">
<br>
<?php echo $Comment; ?>
</div>
<?php
if (isset($_SESSION['username']) && $_SESSION['username'] == true) {
echo "<br>";
echo "<a href='view.php?deleteComment=" . $id . "'>delete</a>";
// need to write the delete logic for this
}
?>
<br>
</div>
<?php
}
echo "</details>";
} else {
echo "<strong>View Comments</strong></summary>";
echo "<em>There are no comments.</em>";
}
}
?>
</div>
<?php
?>
</div>
</div>
</div>
<?php include 'random.php'; ?>
</div>
</main>
<style>
.flex {
display:block;
}
.wrapper {
max-width:900px;
}
.blog {
border:none;
}
</style>
<script>
tinymce.init({
selector: "textarea#entryInput",
plugins: 'link image media paste preview spellchecker autosave lists code',
autosave_ask_before_unload: true,
autosave_interval: '10s',
autosave_restore_when_empty: true,
toolbar: 'undo redo aligncenter alignjustify alignleft alignnone alignright numlist bullist indent outdent blockquote bold italic underline code styleselect fontselect fontsizeselect forecolor backcolor removeformat strikethrough spellchecker media wordcount restoredraft preview',
setup: function (editor) {
editor.on('change', function () {
tinymce.triggerSave();
});
}
});
$('.deleteEntry').on("click", function(e) {
e.preventDefault();
console.log('hi');
if(confirm("Are you sure you want to delete this post?")) {
window.location = $(this).attr('href');
}
});
</script>