-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackend.php
More file actions
282 lines (248 loc) · 11.7 KB
/
backend.php
File metadata and controls
282 lines (248 loc) · 11.7 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
<?php
include_once "config.php";
include_once "vongClass.php";
/**
* @copyright Tom Scheduikat, 2017
*/
session_start();
$link = mysqli_connect(Config::DB_DOMAIN, Config::DB_USER, Config::DB_PASSWORD, Config::DB_DATABASE);
mysqli_set_charset($link, "utf8");
/* check connection */
if (mysqli_connect_errno()) {
// printf("Connect failed: %s\n", mysqli_connect_error());
// exit();
}
if (!isset($_SESSION["user"])) {
header('Location: http://'.$_SERVER["HTTP_HOST"].'/login.php', true, 303);
die();
}
$action = null;
$editPost = null;
if (isset($_GET["action"]) && $_GET["action"] !== '') {
$action = $_GET["action"];
switch ($action) {
case "new":
if (isset($_POST) && count($_POST) > 0 && isset($_POST["title"]) && isset($_POST["text"])) {
$file = null;
if (isset($_FILES) && isset($_FILES["file"]) && isset($_FILES["file"]["name"]) && $_FILES["file"]["name"] !== '') {
$uploaddir = realpath(__DIR__ . '/img/posts/');
$filename = basename($_FILES['file']['name']);
$uploadfile = $uploaddir . '/' . $filename;
$file = "http://".$_SERVER["HTTP_HOST"].'/img/posts/' . $filename;
if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) {
// echo "Datei ist valide und wurde erfolgreich hochgeladen.\n";
} else {
// echo "Möglicherweise eine Dateiupload-Attacke!\n";
// var_dump($_FILES);
// var_dump($_POST);
// exit(PHP_EOL . __FILE__ . ' on Line: ' . __LINE__ . ' in Function: ' . __FUNCTION__);
}
}
$vongClass = new Vong();
$title = $vongClass->vongarize(strip_tags($_POST["title"]));
$text = $_POST["text"];
$vong = $vongClass->vongarize($text);
$insertNewPost = mysqli_query($link, "INSERT INTO posts VALUES(null,
'".mysqli_real_escape_string($link, $title)."',
'".mysqli_real_escape_string($link, $text)."',
'".mysqli_real_escape_string($link, $vong)."',
'".$file."',
'".time()."'
)");
if ($insertNewPost === false) {
// var_dump($_FILES);
// var_dump($_POST);
// var_dump(mysqli_error($link));
// exit(PHP_EOL . __FILE__ . ' on Line: ' . __LINE__ . ' in Function: ' . __FUNCTION__);
}
}
break;
case "edit":
$editPostQ = mysqli_query($link, "SELECT * FROM posts WHERE id=" . $_GET["id"]);
if ($editPostQ) {
$editPost = mysqli_fetch_assoc($editPostQ);
}
break;
case "update":
if (isset($_POST) && isset($_POST["title"]) && isset($_POST["text"]) && isset($_POST["id"])) {
$file = null;
if (isset($_FILES) && isset($_FILES["file"]) && isset($_FILES["file"]["name"]) && $_FILES["file"]["name"] !== '') {
$uploaddir = realpath(__DIR__ . '/img/posts/');
$filename = basename($_FILES['file']['name']);
$uploadfile = $uploaddir . $filename;
$file = "http://".$_SERVER["HTTP_HOST"].'/img/posts/' . $filename;
if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) {
} else {
// var_dump($_FILES);
// var_dump($_POST);
// exit(PHP_EOL . __FILE__ . ' on Line: ' . __LINE__ . ' in Function: ' . __FUNCTION__);
}
}
$vongClass = new Vong();
$title = $vongClass->vongarize(strip_tags($_POST["title"]));
$text = $_POST["text"];
$vong = $vongClass->vongarize($text);
$editPost = mysqli_query($link, "UPDATE posts SET
title = '".mysqli_real_escape_string($link, $title)."',
text = '".mysqli_real_escape_string($link, $text)."',
vong = '".mysqli_real_escape_string($link, $vong)."',
file = '".$file."',
'".time()."'
WHERE id = ".$_POST["id"]."
");
if ($editPost === false) {
// var_dump($_FILES);
// var_dump($_POST);
// var_dump(mysqli_error($link));
// exit(PHP_EOL . __FILE__ . ' on Line: ' . __LINE__ . ' in Function: ' . __FUNCTION__);
}
}
break;
case "delete":
if (isset($_GET["id"])) {
$deletePostQ = mysqli_query($link, "DELETE FROM posts WHERE id=" . $_GET["id"]);
if ($deletePostQ) {
}
}
break;
case "logout":
$_SESSION["user"] = null;
header('Location: http://'.$_SERVER["HTTP_HOST"].'/login.php', true, 303);
die();
break;
}
}
if ($action !== "edit") {
$posts = mysqli_query($link, "SELECT * FROM posts LIMIT 1000");
if ( $posts === false ) {
var_dump($_FILES);
var_dump($_POST);
var_dump(mysqli_error($link));
exit(PHP_EOL . __FILE__ . ' on Line: ' . __LINE__ . ' in Function: ' . __FUNCTION__);
}
}
?>
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<meta name="description" content="">
<meta name="author" content="">
<link rel="icon" href="/favicon.ico">
<title>Vong Generator Admin - Backend</title>
<!-- Bootstrap core CSS -->
<link href="/css/bootstrap.min.css" rel="stylesheet">
<!-- Bootstrap theme -->
<!-- Custom styles for this template -->
<link href="/css/style.css" rel="stylesheet">
</head>
<body>
<div class="container theme-showcase" role="main">
<div class="row">
<div class="col-sm-10 col-md-10 col-xs-12">
<div class="page-header">
<h1>Vong-generator Admin</h1>
</div>
</div>
<div class="col-sm-2 col-md-2 col-xs-12 text-right">
<br>
<a href="backend.php?action=logout" class="btn btn-danger ">Logout</a>
</div>
</div>
<div class="row">
<div class="col-sm-12 col-md-12 col-xs-12">
<?php if ($action !== "edit") { ?>
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">Neuen Beitrag erstellen</h3>
</div>
<div class="panel-body">
<form action="backend.php?action=new" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="title">Titel des Beitrags</label>
<input type="text" id="title" class="form-control" name="title" required>
</div>
<div class="form-group">
<label for="text">Beitrag</label>
<textarea id="text" name="text" class="form-control" rows="12" required></textarea>
</div>
<div class="form-group">
<label for="file">Beitragsbild</label>
<input type="file" id="file" name="file">
<p class="help-block">Hilfetext hier</p>
</div>
<button type="submit" class="btn btn-default">Speichern</button>
</form>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Beiträge</h3>
</div>
<div class="panel-body">
<table class="table table-striped">
<thead>
<tr>
<th>#</th>
<th>Titel</th>
<th>Erstelldatum</th>
<th> </th>
</tr>
</thead>
<tbody>
<?php while ($post = mysqli_fetch_assoc($posts)) { ?>
<tr>
<th scope="row"><?php echo $post["id"]; ?></th>
<td><?php echo $post["title"]; ?></td>
<td><?php echo date("d.m.Y H:i:s", $post["tstamp"]); ?></td>
<td>
<a href="/backend.php?action=edit&id=<?php echo $post["id"]; ?>" class="btn btn-info">Editieren</a>
<a href="/backend.php?action=delete&id=<?php echo $post["id"]; ?>" class="btn btn-danger">Löschen</a>
</td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
<?php } else if (null !== $editPost) { ?>
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">Beitrag editieren</h3>
</div>
<div class="panel-body">
<form action="backend.php?action=update" method="post" enctype="multipart/form-data">
<input type="hidden" name="id" value="<?php echo $editPost["id"]; ?>">
<div class="form-group">
<label for="title">Titel des Beitrags</label>
<input type="text" id="title" class="form-control" name="title" required value="<?php echo $editPost["title"]; ?>">
</div>
<div class="form-group">
<label for="text">Beitrag</label>
<textarea id="text" name="text" class="form-control" rows="12" required>
<?php echo $editPost["text"]; ?>
</textarea>
</div>
<div class="form-group">
<label for="file">Beitragsbild</label>
<input type="file" id="file" name="file">
<p class="help-block">aktuelles Bild: <?php echo isset($editPost["file"]) ? $editPost["file"]: "Keines vorhanden"; ?></p>
</div>
<button type="submit" class="btn btn-default">Speichern</button>
</form>
</div>
</div>
<?php } ?>
</div><!-- /.col-sm-4 -->
</div>
</div> <!-- /container -->
<!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="/js/jquery-2.2.3.min.js"></script>
<script src="/js/bootstrap.min.js"></script>
</body>
</html>