-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin.php
More file actions
85 lines (68 loc) · 2.77 KB
/
admin.php
File metadata and controls
85 lines (68 loc) · 2.77 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
<?php
session_start();
$pdo = new PDO('sqlite:forum.db');
// Перевірка чи користувач має права адміна
$stmt = $pdo->prepare("SELECT admin FROM users WHERE username = :username");
$stmt->bindParam(':username', $_SESSION['username']);
$stmt->execute();
$user = $stmt->fetch();
if (!$user || $user['admin'] != 1) {
echo "У вас немає прав адміністратора.";
exit();
}
// Редагування теми
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['edit_topic'])) {
$topic_id = $_POST['topic_id'];
$title = $_POST['title'];
$stmt = $pdo->prepare("UPDATE topics SET title = :title WHERE id = :topic_id");
$stmt->bindParam(':title', $title, PDO::PARAM_STR);
$stmt->bindParam(':topic_id', $topic_id, PDO::PARAM_INT);
$stmt->execute();
header("Location: admin.php");
exit();
}
// Видалення теми та пов'язаних дописів
if (isset($_GET['action']) && $_GET['action'] === 'delete_topic' && isset($_GET['topic_id'])) {
$topic_id = $_GET['topic_id'];
// Видалення дописів
$stmt = $pdo->prepare("DELETE FROM posts WHERE topic_id = :topic_id");
$stmt->bindParam(':topic_id', $topic_id, PDO::PARAM_INT);
$stmt->execute();
// Видалення теми
$stmt = $pdo->prepare("DELETE FROM topics WHERE id = :topic_id");
$stmt->bindParam(':topic_id', $topic_id, PDO::PARAM_INT);
$stmt->execute();
header("Location: admin.php");
exit();
}
// Отримання тем для відображення
$topics = $pdo->query("SELECT * FROM topics")->fetchAll(PDO::FETCH_ASSOC);
?>
<!DOCTYPE html>
<html>
<head>
<title>Адмін-панель</title>
<link rel="stylesheet" href="style.css">
<link href="https://fonts.googleapis.com/css2?family=Montserrat:wght@400;500;700&display=swap" rel="stylesheet">
</head>
<body>
<h1>Адмін-панель</h1>
<!-- Кнопка виходу -->
<a href="index.php" class="logout-button">На головну</a>
<!-- Список тем -->
<div class="topic">
<h2>Теми:</h2>
<ul>
<?php foreach ($topics as $topicItem): ?>
<li>
<?= htmlspecialchars($topicItem['title']) ?>
<form style="display:inline-block; margin-left:20px" action="admin.php?action=delete_topic&topic_id=<?= $topicItem['id'] ?>" method="post" onsubmit="return confirm('Ви впевнені?')">
<input type="submit" value="Видалити">
</form>
<a href="edit_topic.php?topic_id=<?= $topicItem['id'] ?>">Редагувати</a>
</li>
<?php endforeach; ?>
</ul>
</div>
</body>
</html>