-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin.php
323 lines (286 loc) · 14.4 KB
/
admin.php
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
<?php
require 'config.php';
session_start();
$librarian_login_sanitized = filter_input(INPUT_POST, 'librarian_login', FILTER_SANITIZE_EMAIL);
$librarian_login_sanitized = strtolower($librarian_login_sanitized);
// password hashing
if (isset($_POST['librarian_pass'])) {
$librarian_pass = password_hash($_POST['librarian_pass'], PASSWORD_ARGON2I, ['memory_cost' => 2048, 'time_cost' => 4, 'threads' => 3]);
}
///////////////////////////////
class MyDB extends SQLite3
{
function __construct($dbfile)
{
$this->open($dbfile);
}
}
$db = new MyDB($dbfile);
// librarians' account support form
$action_librarian = filter_input(INPUT_POST, 'librarian', FILTER_VALIDATE_INT);
$librarian_action_message_class="";
function check_user($db, $librarian_login_sanitized){
$stm = $db->prepare("SELECT COUNT(*) as count FROM librarian WHERE librarian_name = :librarian_name");
$stm->bindValue(':librarian_name', $librarian_login_sanitized);
$score = $stm->execute();
$count = $score->fetchArray(1)['count'];
return $count;
}
// 0 add, 1 remove, 2 update
if ($action_librarian === 0) {
if (!empty($librarian_login_sanitized) && !empty($_POST['librarian_pass'])) {
$count_user = check_user($db, $librarian_login_sanitized);
if ($count_user == 0 ) {
$stm = $db->prepare("INSERT INTO librarian (librarian_name, librarian_pass) VALUES (:librarian_sanitized, :librarian_pass )");
$stm->bindValue(':librarian_sanitized', $librarian_login_sanitized);
$stm->bindValue(':librarian_pass', $librarian_pass);
$stm->execute();
$librarian_action_message_class = "ok";
$librarian_action_message = $info['add_librarian_account'] . $librarian_login_sanitized . "<br>";
} else {
$librarian_action_message_class = "alert";
$librarian_action_message = $info['account_exists'] . $librarian_login_sanitized . "<br>";
}
} else {
$librarian_action_message_class = "alert";
$librarian_action_message = $info['wrong_email_password'];
}
} elseif ($action_librarian == 1) {
$count_all_librarian = $db->querySingle("SELECT COUNT(*) as count FROM librarian");
if ($count_all_librarian <= 1) {
$librarian_action_message_class = "alert";
$librarian_action_message = $info['last_librarian'] . "<br>";
} else {
if (!empty($librarian_login_sanitized)) {
$stm = $db->prepare("DELETE from librarian where librarian_name = :librarian_sanitized");
$stm->bindValue(':librarian_sanitized', $librarian_login_sanitized);
$stm->execute();
$librarian_action_message_class = "ok";
$librarian_action_message = $info['account_deleted'] . $librarian_login_sanitized . "<br>";
} else {
$librarian_action_message_class = "alert";
$librarian_action_message = $info['enter_correctly_account'];
}
}
} elseif ($action_librarian == 2) {
if (!empty($librarian_login_sanitized) && !empty($_POST['librarian_pass'])) {
$count_user = check_user($db, $librarian_login_sanitized);
if ($count_user > 0) {
$stm = $db->prepare("UPDATE librarian set librarian_pass = :librarian_pass where librarian_name = :librarian_sanitized");
$stm->bindValue(':librarian_sanitized', $librarian_login_sanitized);
$stm->bindValue(':librarian_pass', $librarian_pass);
$stm->execute();
$librarian_action_message_class = "ok";
$librarian_action_message = $info['password_librarian_changed'] . $librarian_login_sanitized . "<br>";
} else {
$librarian_action_message_class = "alert";
$librarian_action_message = $info['account_not_exist'] . $librarian_login_sanitized . "<br>";
}
} else {
$librarian_action_message_class = "alert";
$librarian_action_message = $info['wrong_email_password'];
}
}
// list of librarians
$stm = $db->query("SELECT librarian_name FROM librarian");
while ($row = $stm->fetchArray(1)) {
$librarian_name = $row['librarian_name'];
$librarian = "<li>" . $librarian_name . ".</li>";
$all_librarians .= $librarian;
}
// machine management form
$action_machine = filter_input(INPUT_POST, 'machine', FILTER_VALIDATE_INT);
$unit_id = filter_input(INPUT_POST, 'machine_id', FILTER_VALIDATE_INT);
$unit_name = filter_input(INPUT_POST, 'machine_name', FILTER_SANITIZE_STRING);
$unit_address = filter_input(INPUT_POST, 'machine_address', FILTER_SANITIZE_STRING);
$unit_size = filter_input(INPUT_POST, 'machine_size', FILTER_VALIDATE_INT);
$unit_column = filter_input(INPUT_POST, 'machine_column', FILTER_VALIDATE_INT);
$machine_action_message_class="";
function check_machine($db, $unit_id){
$stm = $db->prepare("SELECT COUNT(*) as count FROM unit WHERE unit_id = :unit_id");
$stm->bindValue(':unit_id', $unit_id);
$score = $stm->execute();
$count = $score->fetchArray(1)['count'];
return $count;
}
// 0 add, 1 remove, 2 update
if ($action_machine === 0) {
if (empty($unit_id) && !empty($unit_name) && !empty($unit_address) && !empty($unit_size) && !empty($unit_column)) {
$stm = $db->prepare("INSERT INTO unit (unit_name, unit_address, number_box, number_columns) VALUES (:unit_name, :unit_address, :unit_size, :unit_column )");
$stm->bindValue(':unit_name', $unit_name);
$stm->bindValue(':unit_address', $unit_address);
$stm->bindValue(':unit_size', $unit_size);
$stm->bindValue(':unit_column', $unit_column);
$stm->execute();
$machine_action_message_class = "ok";
$machine_action_message = $info['machine_added'] . $unit_name . "<br>";
} else {
$machine_action_message_class = "alert";
$machine_action_message = $info['not_all_data_entered_correctly'];
}
} elseif ($action_machine == 1) {
$count_all_machines = $db->querySingle("SELECT COUNT(*) as count FROM unit");
if ($count_all_machines <= 1) {
$machine_action_message_class = "alert";
$machine_action_message = $info['last_machine'] . "<br>";
} else {
if (!empty($unit_id)) {
$count_machine = check_machine($db, $unit_id);
if ($count_machine > 0) {
$stm = $db->prepare("DELETE from unit where unit_id = :unit_id");
$stm->bindValue(':unit_id', $unit_id);
$stm->execute();
$machine_action_message_class = "ok";
$machine_action_message = $info['machine_deleted'] . $unit_id . "<br>";
} else {
$machine_action_message_class = "alert";
$machine_action_message = $info['no_machine_with_id'] . $unit_id . "<br>";
}
} else {
$machine_action_message_class = "alert";
$machine_action_message = $info['enter_correct_id_machine'];
}
}
} elseif ($action_machine == 2) {
if (!empty($unit_id) && !empty($unit_name) && !empty($unit_address) && !empty($unit_size) && !empty($unit_column)) {
$count_machine = check_machine($db, $unit_id);
if ($count_machine > 0) {
$stm = $db->prepare("UPDATE unit set unit_name = :unit_name, unit_address = :unit_address, number_box = :unit_size, number_columns= :unit_column where unit_id = :unit_id ");
$stm->bindValue(':unit_id', $unit_id);
$stm->bindValue(':unit_name', $unit_name);
$stm->bindValue(':unit_address', $unit_address);
$stm->bindValue(':unit_size', $unit_size);
$stm->bindValue(':unit_column', $unit_column);
$stm->execute();
$machine_action_message_class = "ok";
$machine_action_message = $info['machine_data_changed'] . $unit_id . "<br>";
} else {
$machine_action_message_class = "alert";
$machine_action_message = $info['no_machine'] . $unit_id . "<br>";
}
} else {
$machine_action_message_class = "alert";
$machine_action_message = $info['not_all_data_entered_correctly'];
}
}
// machine list
$stm = $db->query("SELECT unit_id, unit_name, unit_address, number_box FROM unit");
while ($row = $stm->fetchArray(1)) {
$unit_id = $row['unit_id'];
$unit_name = $row['unit_name'];
$unit_address = $row['unit_address'];
$unit_size = $row['number_box'];
$unit = "<li>" . $info['id'] . $unit_id . ". " . $unit_name . ". " . $unit_address .". ". $info['lockers'] . $unit_size . ".</li>";
$all_units .= $unit;
}
////////////////////
echo $page_head . "\n\t\t<title>" . $info['admin_title']; ?></title>
</head>
<body>
<?php
if (isset($_SESSION["name"])) {
?>
<header class="page-header">
<h1><?php echo $info['admin_message']; ?></h1>
<?php echo $info['admin_login_info'] . " <b>" . $_SESSION['name']; ?></b>.<br>
</header>
<nav>
<ul>
<li><?php echo $info['admin_logout']; ?></li>
<li><?php echo $info['machine_page']; ?></li>
</ul>
</nav>
<main role="main">
<section>
<h2 class=""><?php echo $info['admin_librarian_managment']; ?></h2>
<div class="edycja">
<div class="<?php echo $librarian_action_message_class; ?>">
<h2><?php echo $librarian_action_message; ?></h2>
</div>
<form name="admin_librarians" method="post" action="" class="">
<div class="flekser">
<label for="librarian_login"><?php echo $info['login_name']; ?></label> <input type="text" class="" id="id-librarian_login" name="librarian_login">
</div>
<div class="flekser">
<label for="librarian_pass"><?php echo $info['login_pass']; ?></label> <input type="password" class="" id="id-librarian_pass" name="librarian_pass">
</div>
<div>
<div>
<input type="radio" id="librarian_add" name="librarian" value="0"> <label for="librarian_add"><?php echo $info['add'] . " " . $info['account']; ?></label>
</div>
<div>
<input type="radio" id="librarian_remove" name="librarian" value="1"> <label for="librarian_remove"><?php echo $info['remove'] . " " . $info['account']; ?></label>
</div>
<div>
<input type="radio" id="librarian_update" name="librarian" value="2"> <label for="librarian_update"><?php echo $info['update'] . " " . $info['password']; ?></label>
</div>
</div><br>
<input type="submit" name="librarian_submit" value="<?php echo $info['send_button']; ?>">
</form>
</div>
<h3><?php echo $info['admin_list_librarians']; ?></h3>
<ul>
<?php echo $all_librarians; ?>
</ul>
</section>
<section>
<h2 class=""><?php echo $info['admin_managing_machines']; ?></h2>
<div class="edycja">
<div class="<?php echo $machine_action_message_class; ?>">
<h2><?php echo $machine_action_message; ?></h2>
</div>
<form name="admin_machines" method="post" action="" class="">
<div class="flekser">
<label for="machine_id"><?php echo $info['admin_machine_id']; ?></label>
<input type="text" class="" id="id-machine_id" name="machine_id">
</div>
<div class="flekser">
<label for="machine_name"><?php echo $info['admin_machine_name']; ?></label>
<input type="text" class="" id="id-machine_name" name="machine_name">
</div>
<div class="flekser">
<label for="machine_address"><?php echo $info['admin_machine_address']; ?></label>
<input type="text" class="" id="id-machine_address" name="machine_address">
</div>
<div class="flekser">
<label for="machine_size"><?php echo $info['admin_machine_size']; ?></label>
<input type="number" class="" id="id-machine_size" name="machine_size">
</div>
<div class="flekser">
<label for="machine_column"><?php echo $info['admin_machine_column']; ?></label>
<input type="number" class="" id="id-machine_column" name="machine_column">
</div>
<div>
<div>
<input type="radio" id="machine_add" name="machine" value="0">
<label for="machine_add"><?php echo $info['add'] . " " . $info['machine']; ?></label>
</div>
<div>
<input type="radio" id="machine_remove" name="machine" value="1">
<label for="machine_remove"><?php echo $info['remove'] . " " . $info['machine']; ?></label>
</div>
<div>
<input type="radio" id="machine_update" name="machine" value="2">
<label for="machine_update"><?php echo $info['update'] . " " . $info['machine']; ?></label>
</div>
</div><br>
<input type="submit" name="machine_submit" value="<?php echo $info['send_button']; ?>">
</form>
</div>
<h3><?php echo $info['admin_list_machines']; ?></h3>
<ul>
<?php echo $all_units; ?>
</ul>
</section>
</main>
<?php
} else {
echo '<header class="page-header">
<h1>' . $info['admin_message'] . '</h1>
<h2><a href="login.php">' . $info["login_first"] . '</a></h2>
</header>';
}
?>
<script src="js/script.js"></script>
</body>
</html>