-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpost.php
More file actions
230 lines (215 loc) · 6.03 KB
/
post.php
File metadata and controls
230 lines (215 loc) · 6.03 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<?php
include_once(__DIR__."/lib/autoload.php");
if (isset($_GET['new']) || isset($_GET['edit']) || isset($_POST['new']) || isset($_POST['edit'])) {
if (!$auth->hasRole([ \Bloggr\Roles::ADMIN, \Bloggr\Roles::AUTHOR ])) {
header('Location: /');
die();
}
}
$errors = [];
$action = '';
$data = [];
$success = false;
$title = "404 Not Found";
if (isset($_GET['view'])) {
if (isset($_POST['comment'])) {
$result = $auth->commentPost($_GET['view'], $_POST['comment']);
if (is_array($result)) {
$errors = $result;
}
}
$result = $auth->getPost($_GET['view']);
$result_comments = $auth->getPostComments($_GET['view']);
if(!$result) {
array_push($errors, '404 Not Found');
} else {
$title = $result['title'];
$action = 'view';
$data = $result;
}
}
else if (isset($_GET['new'])) {
$action = 'new';
$title = "Neuer Beitrag";
}
else if (isset($_GET['edit'])) {
$action = 'edit';
$title = "Beitrag bearbeiten";
}
else {
array_push($errors, '404 Not Found');
}
if ($action == 'new' && isset($_POST['new'])) {
$result = $auth->newPost($_POST['title'], $_POST['text']);
if (is_array($result)) {
$errors = $result;
} else {
header("Location: /post.php?view=".$result);
}
}
$ptitle = "";
$text = "";
if ($action == 'edit' && isset($_POST['edit'])) {
$result = $auth->editPost($_GET['edit'], $_POST['title'], $_POST['text']);
if (is_array($result)) {
$errors = $result;
} else {
$success = true;
}
}
if ($action == 'edit' && isset($_POST['delete'])) {
$result = $auth->removePost($_GET['edit']);
if (is_array($result)) {
$errors = $result;
} else {
header("Location: /");
}
}
if ($action == 'edit') {
$result = $auth->getPost($_GET['edit']);
if(!$result) {
array_push($errors, '404 Not Found');
} else {
$data = $result;
}
}
?>
<!DOCTYPE html>
<html>
<?php
require_once(__DIR__."/inc/head.php");
?>
<body>
<?php require_once(__DIR__."/inc/nav.php"); ?>
<section class="main">
<?php
if ($action == 'edit' && (count($errors) <= 0)) {
echo '<a href="/post.php?view='.$data["id"].'">Zurück</a>';
} else {
echo '<a href="/">Zurück</a>';
}
?>
<?php
if ($action == 'view'):
?>
<div>
<header><h2><?= $data['title'] ?></h2></header>
<section>
<p><?= nl2br($data['text']) ?></p>
</section>
<footer><small>von <?= $data['user'] ?> am <?= date('d.m.Y H:i', $data['created_at']) ?><br>
<?php
if($data['updated_by']):
?>
zuletzt bearbeitet: <?= date('d.m.Y H:i',$data['updated_at']).' von '.$data['updated_by'] ?>
<?php
endif;
if ($auth->canEditPost($data["id"]) == true) echo '<br><a href="post.php?edit='.$data["id"].'">Edit Post</a>';
echo '</small></footer>';
?>
</div>
<?php
foreach ($errors as $key=>$value):
?>
<br><span style="color: red;">
<?= $value ?>
</span>
<?php
endforeach;
?>
<?php
if ($auth->isLoggedIn()) {
?>
<p>
<form action="<?= htmlspecialchars($_SERVER['REQUEST_URI']) ?>" method="post">
<textarea name="comment" id="comment" cols="30" rows="2"></textarea>
<input type="submit" value="Comment">
</form>
</p>
<?php
}
echo '<h3>Kommentare</h3>';
if(is_array($result_comments)) {
foreach($result_comments as $comment) {
?>
<article class="card">
<section>
<b><?= $comment['user']; ?></b> <small>am <?= date('H:i d.m.Y',$comment['created_at']) ?></small>
</section>
<footer>
<?= $comment['comment']; ?>
</footer>
</article>
<?php
}
}
endif;
if ($action == 'new'):
?>
<h2>Neuer Beitrag</h2>
<?php
foreach ($errors as $key=>$value):
?>
<span style="color: red;">
<?= $value ?>
</span><br>
<?php
endforeach;
?>
<form action="<?= htmlspecialchars($_SERVER['REQUEST_URI']) ?>" method="post" class="clearfix">
<label for="title">Titel</label>
<input type="text" name="title" id="title" value="<?= (isset($_POST['title'])) ? htmlspecialchars($_POST['title']) : ''; ?>"><br>
<label for="text">Text</label>
<textarea rows="4" cols="50" name="text" id="text"><?= (isset($_POST['text'])) ? htmlspecialchars($_POST['text']) : ''; ?></textarea>
<input type="submit" name="new" value="Erstellen">
</form>
<?php
endif;
if ($action == 'edit'):
?>
<h2>Beitrag bearbeiten</h2>
<?php
if($success == true) {
echo '<span style="color: green;">Post bearbeitet!</span><br>';
}
foreach ($errors as $key=>$value):
?>
<span style="color: red;">
<?= $value ?>
</span><br>
<?php
endforeach;
?>
<form action="<?= htmlspecialchars($_SERVER['REQUEST_URI']) ?>" method="post" class="clearfix">
<label for="title">Titel</label>
<input type="text" name="title" id="title" value="<?= (isset($data['title'])) ? $data['title'] : $ptitle; ?>"><br>
<label for="text">Text</label>
<textarea rows="4" cols="50" name="text" id="text"><?= (isset($data['text'])) ? $data['text'] : $text; ?></textarea>
<input type="submit" name="edit" value="Speichern">
<label for="modal_1" class="button warning">Löschen</label>
<div class="modal">
<input id="modal_1" type="checkbox" />
<label for="modal_1" class="overlay"></label>
<article>
<header>
<h3>Beitrag wirklich löschen?</h3>
<label for="modal_1" class="close">×</label>
</header>
<section class="content">
Sicher dass der Beitrag gelöscht werden soll? Das löschen eines Beitrags löscht alle seine Kommentare! <b>Die Daten sind nicht wiederherstellbar!</b>
</section>
<footer>
<input class="dangerous warning" type="submit" name="delete" value="Trotzdem löschen">
<label for="modal_1" class="button">
Abbrechen
</label>
</footer>
</article>
</div>
</form>
<?php
endif;
?>
</section>
</body>
</html>