-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforum.php
More file actions
51 lines (48 loc) · 1.75 KB
/
forum.php
File metadata and controls
51 lines (48 loc) · 1.75 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
<?php
define('POSTS_DIR', 'posts');
// Ensure posts directory exists
if (!is_dir(POSTS_DIR)) {
mkdir(POSTS_DIR);
}
// Utility function to load posts
function get_posts($status = 'open') {
$posts = [];
foreach (glob(POSTS_DIR . '/*.json') as $file) {
$post = json_decode(file_get_contents($file), true);
if ($status === 'all' || $post['status'] === $status) {
$posts[] = $post;
}
}
return $posts;
}
// Handle actions
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'])) {
if ($_POST['action'] === 'add_post') {
$post = [
'id' => uniqid(),
'title' => htmlspecialchars($_POST['title']),
'content' => htmlspecialchars($_POST['content']),
'status' => 'open',
'comments' => []
];
file_put_contents(POSTS_DIR . '/' . $post['id'] . '.json', json_encode($post));
} elseif ($_POST['action'] === 'add_comment') {
$postId = $_POST['post_id'];
$postFile = POSTS_DIR . '/' . $postId . '.json';
if (file_exists($postFile)) {
$post = json_decode(file_get_contents($postFile), true);
$post['comments'][] = htmlspecialchars($_POST['comment']);
file_put_contents($postFile, json_encode($post));
}
} elseif ($_POST['action'] === 'change_status') {
$postId = $_POST['post_id'];
$postFile = POSTS_DIR . '/' . $postId . '.json';
if (file_exists($postFile)) {
$post = json_decode(file_get_contents($postFile), true);
$post['status'] = $_POST['new_status'];
file_put_contents($postFile, json_encode($post));
}
}
header('Location: index.php');
exit;
}