-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnew_topic.php
More file actions
32 lines (32 loc) · 1.29 KB
/
new_topic.php
File metadata and controls
32 lines (32 loc) · 1.29 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
<?php
session_start();
if($_SERVER['REQUEST_METHOD'] === 'POST') {
if($_SESSION['LOGGED_IN']) {
$posts = json_decode(file_get_contents("topics.json"), true);
if (array_key_exists($_POST['topic-name'], $posts)) {
http_response_code(409);
echo "Topic with the same name already exists.";
exit();
}
$posts[htmlspecialchars($_POST['topic-name'])] = array(array('author' => $_SESSION['user'],'content' => str_replace("\n","<br>",htmlspecialchars($_POST['content']))));
file_put_contents("topics.json", json_encode($posts));
header("Location: /topic.php?topic=" . urlencode(htmlspecialchars($_POST['topic-name'])));
exit();
} else {
http_response_code(401);
echo "You need to be logged in to post";
}
}
?>
<?php if(!($_SERVER['REQUEST_METHOD'] === 'POST') && $_SESSION['LOGGED_IN']): ?>
<form id="post-form" method="POST" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>">
<h1>Create a new topic | Public topic</h1>
<input type="text" name="topic-name" placeholder="Title"/> <br>
<textarea form="post-form" name="content" placeholder="Topic description or content"></textarea> <br>
<input type="submit"/>
</form>
<?php else: ?>
<?php http_response_code(401); ?>
Please login to post.
<?php endif; ?>
<link rel="stylesheet" href="/proconfig/topic-ui.css">