-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
298 lines (251 loc) · 10.4 KB
/
index.php
File metadata and controls
298 lines (251 loc) · 10.4 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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
<?php
session_start();
require "core/config.php";
// User Create
if (isset($_POST["create-user"])) {
$username = strtolower($_POST["username"]);
$email = $_POST["email"];
$name = $_POST["name"];
$incorrect_chars = ['(', ')', '{', '}', '[', ']', '<', '>', '|', '/', '\\', '`', '~', '!', '?', '@', '#', '$', '%', '^', '&', '*', '-', '+', '=', ',', '.', 'username', 'admin', 'user', 'account'];
foreach ($incorrect_chars as $char) {
if (strpos($username, $char)) {
$_SESSION["message"] = "Incorrect username";
$_SESSION["message-type"] = "danger";
header("location: index.php");
exit();
}
}
try {
$user_query = $connection->query("SELECT username FROM user WHERE username = '$username'");
if ($user_query->num_rows >= 1) {
$_SESSION["message"] = "This username already exists";
$_SESSION["message-type"] = "danger";
header("location: index.php");
exit();
}
$connection->query("INSERT INTO user (username, email, name) VALUES ('$username', '$email', '$name')");
$_SESSION["message"] = "User was created successfully";
$_SESSION["message-type"] = "primary";
header("location: index.php");
exit();
} catch (Exception $e) {
$_SESSION["message"] = "Something went wrong";
$_SESSION["message-type"] = "warning";
header("location: index.php");
exit();
}
}
// Post Create
if (isset($_POST["create-post"])) {
$author = $_POST["author"];
$title = $_POST["title"];
$body = $_POST["body"];
try {
$user_query = $connection->query("SELECT username FROM user WHERE username = '$author'");
$user = mysqli_fetch_array($user_query);
if ($user) {
$connection->query("INSERT INTO post (author, title, body) VALUES ('$author', '$title', '$body')");
$_SESSION["message"] = "Post was created successfully";
$_SESSION["message-type"] = "primary";
header("location: index.php");
exit();
} else {
$_SESSION["message"] = "This author does not exist";
$_SESSION["message-type"] = "danger";
header("location: index.php");
exit();
}
} catch (Exception $e) {
$_SESSION["message"] = "Something went wrong";
$_SESSION["message-type"] = "warning";
header("location: index.php");
exit();
}
}
// Relation Create
if (isset($_POST["create-relation"])) {
$from_user = $_POST["from_user"];
$to_user = $_POST["to_user"];
try {
$from_user_query = $connection->query("SELECT username FROM user WHERE username = '$from_user'");
$to_user_query = $connection->query("SELECT username FROM user WHERE username = '$to_user'");
if ($from_user_query->num_rows < 1) {
$_SESSION["message"] = "The `From User` does not exist";
$_SESSION["message-type"] = "danger";
header("location: index.php");
exit();
}
if ($to_user_query->num_rows < 1) {
$_SESSION["message"] = "The `To User` does not exist";
$_SESSION["message-type"] = "danger";
header("location: index.php");
exit();
}
$relation_query = $connection->query("SELECT * FROM relation WHERE from_user = '$from_user' AND to_user = '$to_user'");
if ($relation_query->num_rows > 0) {
$_SESSION["message"] = "This relation already exists";
$_SESSION["message-type"] = "danger";
header("location: index.php");
exit();
}
$connection->query("INSERT INTO relation (from_user, to_user) VALUES ('$from_user', '$to_user')");
$_SESSION["message"] = "Relation was created successfully";
$_SESSION["message-type"] = "primary";
header("location: index.php");
exit();
} catch (Exception $e) {
$_SESSION["message"] = "Something went wrong";
$_SESSION["message-type"] = "warning";
header("location: index.php");
exit();
}
}
// Users List
$users_query = $connection->query("SELECT * FROM user ORDER BY created DESC");
$users = mysqli_fetch_all($users_query, MYSQLI_ASSOC);
// Posts List
$posts_query = $connection->query("SELECT * FROM post ORDER BY created DESC");
$posts = mysqli_fetch_all($posts_query, MYSQLI_ASSOC);
// Relations List
$relations_query = $connection->query("SELECT * FROM relation ORDER BY created DESC");
$relations = mysqli_fetch_all($relations_query, MYSQLI_ASSOC);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PHP - CRUD</title>
<link rel="stylesheet" href="css/bootstrap.css">
<link rel="stylesheet" href="css/style.css">
<style>
body {
background-color:rgb(213, 217, 220);
}
</style>
</head>
<body>
<div class="content">
<header>
<h2>PHP CRUD APPLICATION</h2>
<h4>made by <span class="text-light">armin_emami</span></h4>
</header>
<br>
<?php include 'includes/message.php' ?>
<!-- User Section -->
<div class="user-group">
<!-- Create User Form -->
<h2>~User Section</h2>
<form action="" method="POST">
<input type="text" name="username" placeholder="Username" maxlength="200" class="form-control" required>
<input type="email" name="email" placeholder="Email" maxlength="200" class="form-control" required>
<input type="text" name="name" placeholder="Name" maxlength="200" class="form-control" required>
<input type="submit" value="Create User" name="create-user" class="btn btn-primary">
</form>
<br>
<!-- Users Table -->
<table class="table">
<thead>
<tr>
<th>Username</th>
<th>Email</th>
<th>Name</th>
<th>Actions</th>
</tr>
</thead>
<?php if (!empty($users)): ?>
<?php foreach ($users as $user): ?>
<tbody>
<tr>
<th><?php echo $user["username"]; ?></th>
<th><?php echo $user["email"]; ?></th>
<th><?php echo $user["name"]; ?></th>
<th class="actions">
<a href="<?php echo ($URLS['user_edit'] . $user['username']); ?>" class="text-primary">Edit</a>
<a href="<?php echo ($URLS['user_delete'] . $user['username']); ?>" class="text-danger">Delete</a>
</th>
</tr>
</tbody>
<?php endforeach; ?>
<?php endif; ?>
</table>
</div>
<br><br>
<!-- Post Section -->
<div class="post-group">
<!-- Create Post Form -->
<h2>~Post Section</h2>
<form action="" method="POST">
<input type="text" name="author" placeholder="Author" maxlength="200" class="form-control" required>
<input type="text" name="title" placeholder="Title" maxlength="50" class="form-control" required>
<textarea name="body" rows="5" placeholder="Body" class="form-control" required></textarea>
<input type="submit" value="Create Post" name="create-post" class="btn btn-primary">
</form>
<br>
<!-- Posts Table -->
<table class="table">
<thead>
<tr>
<th>Author</th>
<th>Title</th>
<th>Created at</th>
<th>Actions</th>
</tr>
</thead>
<?php if (!empty($posts)): ?>
<?php foreach ($posts as $post): ?>
<tbody>
<tr>
<th><?php echo $post["author"]; ?></th>
<th><?php echo $post["title"]; ?></th>
<th><?php echo $post["created"]; ?></th>
<th class="actions">
<a href="<?php echo ($URLS['post_view'] . $post['ID']); ?>" class="text-primary">View</a>
<a href="<?php echo ($URLS['post_edit'] . $post['ID']); ?>" class="text-primary">Edit</a>
<a href="<?php echo ($URLS['post_delete'] . $post['ID']); ?>" class="text-danger">Delete</a>
</th>
</tr>
</tbody>
<?php endforeach; ?>
<?php endif; ?>
</table>
</div>
<br><br>
<!-- Relation Section -->
<div class="relation-group">
<!-- Create Relation Form -->
<h2>~Relation Section</h2>
<form action="" method="POST">
<input type="text" name="from_user" placeholder="From User" maxlength="200" class="form-control" required>
<input type="text" name="to_user" placeholder="To User" maxlength="50" class="form-control" required>
<input type="submit" value="Create Relation" name="create-relation" class="btn btn-primary">
</form>
<br>
<!-- Relation Table -->
<table class="table">
<thead>
<tr>
<th>From User</th>
<th>To User</th>
<th>Actions</th>
</tr>
</thead>
<?php if (!empty($relations)): ?>
<?php foreach ($relations as $relation): ?>
<tbody>
<tr>
<th><?php echo $relation["from_user"]; ?></th>
<th><?php echo $relation["to_user"]; ?></th>
<th class="actions">
<a href="<?php echo ($URLS['relation_delete'] . $relation['ID']); ?>" class="text-danger">Delete</a>
</th>
</tr>
</tbody>
<?php endforeach; ?>
<?php endif; ?>
</table>
</div>
</div>
<script src="js/script.js"></script>
</body>
</html>