This repository was archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomments.php
178 lines (159 loc) · 7.88 KB
/
comments.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
<?php
$page_title = "View Post | Rave Ramble";
include('mysqli_connect.php');
include('includes/signin_functions.inc.php');
// Get whatever information you need from either GET, SESSION, or POST
$blogid = mysqli_real_escape_string($dbc, trim($_GET['blogpost_id']));
$updateid = isset($_GET['update_id']) ? mysqli_real_escape_string($dbc, trim($_GET['update_id'])) : '';
$deleteid = isset($_GET['delete_id']) ? mysqli_real_escape_string($dbc, trim($_GET['delete_id'])) : '';
session_start();
// Delete comment check
if (isset($_GET['delete_id'])) {
// Gets user id of comment to be deleted
$query = "SELECT user_id FROM comments WHERE comment_id = '$deleteid'";
$result = mysqli_query($dbc, $query);
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
if (isset($_SESSION['user_id']) && ($_SESSION['user_id'] == $row['user_id'] || $_SESSION['isAdmin'] == 1)) {
// Only the author of the comment or admin can delete
$deleteQuery = "DELETE FROM comments WHERE comment_id = '$deleteid'";
mysqli_query($dbc, $deleteQuery);
$_SESSION['message'] = 'delete';
} else {
// Otherwise they get redirected to the original blogpost page
$_SESSION['message'] = false;
redirect_user('comments?blogpost_id=' . $blogid);
}
}
}
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (isset($_REQUEST['commentbtn'])) {
$valid_form = true;
$comment = isset($_REQUEST['comment']) ?
mysqli_real_escape_string($dbc, trim($_POST['comment'])) : '';
if ($comment == '') {
$_SESSION['message'] = 'empty';
$valid_form = false;
}
if ($valid_form) {
$id = $_SESSION['user_id'];
$_SESSION['message'] = 'add';
$query = "INSERT INTO comments (blogpost_id, user_id, comment_body) VALUES ('$blogid', '$id', '$comment')";
$results = mysqli_query($dbc, $query);
}
} else if (isset($_REQUEST['cancelbtn'])) {
redirect_user('comments?blogpost_id=' . $blogid);
} else {
$valid_form = true;
$update = isset($_REQUEST['update']) ?
mysqli_real_escape_string($dbc, trim($_POST['update'])) : '';
$updateid = $_REQUEST['id'];
if ($update == '') {
$_SESSION['message'] = 'empty';
$valid_form = false;
}
if ($valid_form) {
$_SESSION['message'] = 'edit';
$query = "UPDATE comments SET comment_body = '$update' WHERE comment_id = '$updateid'";
mysqli_query($dbc, $query);
$updateid = '';
}
}
}
include('header.php');
$blogpostQuery = "SELECT * FROM blogposts WHERE blogpost_id = $blogid";
$blogpostResult = mysqli_query($dbc, $blogpostQuery);
//Your SQL Query
$queryComments = "SELECT * FROM comments JOIN users USING (user_id) WHERE blogpost_id = $blogid";
$commentResult = mysqli_query($dbc, $queryComments);
while ($row = mysqli_fetch_array($blogpostResult, MYSQLI_ASSOC)) {
?>
<p class="h2 text-center text-white" style="margin-top:4rem">View & Add Comments</p>
<?php
// Prints messages to give user feedback on actions
if (isset($_SESSION['message'])) {
switch ($_SESSION['message']) {
case 'add':
echo '<div class="alert alert-success text-center">Comment successfully added!</div>';
break;
case 'edit':
echo '<div class="alert alert-success text-center">Comment successfully edited!</div>';
break;
case 'delete':
echo '<div class="alert alert-success text-center">Comment successfully deleted!</div>';
break;
case 'empty':
echo '<div class="alert alert-danger text-center">Your comment cannot be empty!</div>';
break;
default:
echo '<div class="alert alert-danger text-center">You do not have permission for this action!</div>';
}
// Empty message so message does not reappear
$_SESSION['message'] = null;
}
?>
<div class="card bg-dark text-white">
<div class="card-body">
<h5 class="card-title"><?php echo $row['blogpost_title']; ?> | ID <?php echo $row['blogpost_id']; ?></h5>
<p class="card-text"><?php echo $row['blogpost_body']; ?></p>
<p class="card-text">Timestamp: <?php echo $row['blogpost_timestamp']; ?></p>
<?php
}
if (isset($_SESSION['user_id'])) {
// Form not visible if user is not logged in
?>
<form style="margin-top:2rem" action=<?php echo "comments?blogpost_id=" . $blogid; ?> method="post">
<label class="form-label" for="comment">New Comment</label>
<div class="form-outline form-white mb-4">
<textarea name="comment" id="comment" class="form-control form-control-lg" maxlength="1024"><?php if (isset($_POST['comment'])) echo $_POST['comment']; ?></textarea>
</div>
<input class="btn btn-primary" name="commentbtn" type="submit" value="Add Comment">
<form>
<?php
}
?>
</div>
</div>
<div id="comments-container" class="text-light">
<h2 class="text-center">Comments:</h2>
<?php
//Your loop to display everything
while ($row = mysqli_fetch_array($commentResult, MYSQLI_ASSOC)) {
?>
<div class="card bg-dark text-white p-1">
<div class="card-body">
<?php
if (
isset($_SESSION['user_id']) && isset($updateid) && $row['comment_id'] == $updateid &&
($_SESSION['isAdmin'] == 1 || $_SESSION['user_id'] == $row['user_id'])
) {
?>
<form style="margin-top:2rem" action=<?php echo "comments?blogpost_id=" . $row['blogpost_id']; ?> method="post">
<label class="form-label" for="update">Edit Comment</label>
<div class="form-outline form-white mb-4">
<input style="display: none;" name="id" value="<?php echo $row['comment_id'] ?>">
<textarea name="update" id="update" class="form-control form-control-lg" maxlength="1024"><?php echo $row['comment_body']; ?></textarea> <!-- This is not sticky on purpose so as to make spam posting comments harder -->
</div>
<input class="btn btn-primary" type="submit" value="Edit Comment">
<button class="btn btn-danger" name="cancelbtn">Cancel</button>
<form>
<?php
} else {
?>
<h5 class="card-title"><?php echo $row['first_name'];
if ($row['isAdmin'] == 1) echo ' <span style="color: red;">(Admin)</span>'; ?></h5>
<p class="card-text"><?php echo $row['comment_body']; ?></p>
<?php
if (isset($_SESSION['user_id']) && ($_SESSION['isAdmin'] == 1 || $_SESSION['user_id'] == $row['user_id'])) {
?>
<a href=<?php echo "comments?blogpost_id=" . $row['blogpost_id'] . "&update_id=" . $row['comment_id']; ?> class="btn btn-warning">Edit</a>
<a href=<?php echo "comments?blogpost_id=" . $row['blogpost_id'] . "&delete_id=" . $row['comment_id']; ?> class="btn btn-danger">Delete</a>
<?php
}
?>
</div>
</div>
<?php
}
}
?>
</div>