This repository was archived by the owner on Nov 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdashboard.php
More file actions
188 lines (164 loc) · 6.72 KB
/
dashboard.php
File metadata and controls
188 lines (164 loc) · 6.72 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
<?php
// Log IP address, page URL, username (if logged in), and timestamp
$requestLog = [
'ip' => $_SERVER['REMOTE_ADDR'],
'url' => $_SERVER['REQUEST_URI'],
'username' => isset($_SESSION['username']) ? $_SESSION['username'] : null,
'timestamp' => date('Y-m-d H:i:s')
];
// Load existing request logs
$requestLogs = json_decode(file_get_contents('db/request_logs.json'), true);
// Add the new request log to the existing logs
$requestLogs[] = $requestLog;
// Save the updated request logs
file_put_contents('db/request_logs.json', json_encode($requestLogs));
?>
<?php
$configs = include('config.php');
session_start();
// Check if the user is logged in
if (!isset($_SESSION['username'])) {
echo "You are not logged in.";
exit;
}
// Load messages data
$messages = json_decode(file_get_contents('db/messages.json'), true);
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (isset($_POST['content'])) {
$content = strip_tags($_POST['content'], "<a><p><b><i><blink><marquee><br>"); // Strip tags as soon as we can to prevent XSS,
// Generate a unique post ID
$postId = uniqid();
// Create a new post
$messages[$postId] = [
'content' => $content,
'author' => $_SESSION['username'],
'likes' => [] // Initialize the likes array for the new post
];
// Save the updated messages data
file_put_contents('db/messages.json', json_encode($messages));
header('Location: dashboard.php');
exit;
} elseif (isset($_POST['like'])) {
$postId = $_POST['post_id'];
$username = $_SESSION['username'];
// Check if the post exists and has the 'likes' key
if (isset($messages[$postId]) && array_key_exists('likes', $messages[$postId])) {
// Check if the user has already liked the post
if (!in_array($username, $messages[$postId]['likes'])) {
// Add the user to the likes array of the post
$messages[$postId]['likes'][] = $username;
// Save the updated messages data
file_put_contents('db/messages.json', json_encode($messages));
}
}
} elseif (isset($_POST['unlike'])) {
$postId = $_POST['post_id'];
$username = $_SESSION['username'];
// Check if the post exists and has the 'likes' key
if (isset($messages[$postId]) && array_key_exists('likes', $messages[$postId])) {
// Check if the user has liked the post
$likeIndex = array_search($username, $messages[$postId]['likes']);
if ($likeIndex !== false) {
// Remove the user from the likes array of the post
unset($messages[$postId]['likes'][$likeIndex]);
// Save the updated messages data
file_put_contents('db/messages.json', json_encode($messages));
}
}
} elseif (isset($_POST['logout'])) {
// Logout user
session_destroy();
header('Location: index.php');
exit;
} elseif (isset($_POST['reply'])) {
$postId = $_POST['post_id'];
$content = $_POST['reply_content'];
// Check if the post exists
if (isset($messages[$postId])) {
// Create a new reply for the post
$replyId = uniqid();
$reply = [
'content' => $content,
'author' => $_SESSION['username']
];
// Add the reply to the post
$messages[$postId]['replies'][$replyId] = $reply;
// Save the updated messages data
file_put_contents('db/messages.json', json_encode($messages));
}
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title><?php echo $configs["sitename"] ?> - Dashboard</title>
<link rel="stylesheet" href="css/<?php echo $configs["theme"] ?>">
</head>
<body>
<div class="header">
<div class="login">
<a>@<?php echo $_SESSION['username']; ?></a>
<form method="POST" action="">
<input type="submit" name="logout" value="Logout">
</form>
</div>
<h2><?php echo $configs["sitename"] ?></h2>
</div>
<div class="wrapper">
<?php
if ($_SESSION['admin'] == true and $configs["adminEnabled"] === true) {
echo "<a href='admin/'>Admin panel</a>";
}
?>
<h3>Create a Post:</h3>
<div class="createpost">
<form method="POST" action="">
<textarea name="content" placeholder="Enter your message here..." required class="post-textarea"></textarea><br><br>
<p>Allowed tags: <b>, <p>, <a>, <i></p>
<input type="submit" value="Create Post">
</form>
</div>
<hr>
<?php
if ($messages == null){
die;
}
?>
<?php foreach (array_reverse($messages) as $postId => $post): ?>
<div>
<p>@<?php echo $post['author']; ?></p>
<p><?php echo $post['content']; ?></p>
<!-- Like button -->
<form method="POST" action="">
<input type="hidden" name="post_id" value="<?php echo $postId; ?>">
<?php if (isset($post['likes']) && in_array($_SESSION['username'], $post['likes'])): ?>
<input type="submit" name="unlike" value="Unlike <?php echo isset($post['likes']) ? count($post['likes']) : 0; ?>">
<?php else: ?>
<input type="submit" name="like" value="Like <?php echo isset($post['likes']) ? count($post['likes']) : 0; ?>">
<?php endif; ?>
</form><br>
<!-- Reply form -->
<div class="replies">
<!-- Display replies -->
<?php if (isset($post['replies'])): ?>
<?php foreach ($post['replies'] as $replyId => $reply): ?>
<div>
<p>@<?php echo $reply['author']; ?></p>
<p><?php echo $reply['content']; ?></p>
</div>
<?php endforeach; ?>
<?php endif; ?>
<form method="POST" action="">
<input type="hidden" name="post_id" value="<?php echo $postId; ?>">
<textarea name="reply_content" placeholder="Reply to this post..." required class="replybox"></textarea><br>
<input type="submit" name="reply" value="Reply">
</form>
</div>
</div>
<hr>
<?php endforeach; ?>
<br>
</div>
</body>
</html>