Use name to remove, allow duplicate names

This commit is contained in:
Jonathan Flueren 2023-08-17 17:10:55 +02:00
parent 5af65b5f31
commit 840ef38779

View file

@ -39,12 +39,12 @@ function add_identity($name, $mac)
$url = strtok($_SERVER['REQUEST_URI'], '?'); $url = strtok($_SERVER['REQUEST_URI'], '?');
foreach ($identities as $identity) { foreach ($identities as $identity) {
if ($identity['name'] == $name) { //if ($identity['name'] == $name) {
$_SESSION['form_success'] = false; // $_SESSION['form_success'] = false;
$_SESSION['form_success_message'] = 'Name already in use, please choose a different one.'; // $_SESSION['form_success_message'] = 'Name already in use, please choose a different one.';
header("Location: " . $url, true, 303); // header("Location: " . $url, true, 303);
exit(); // exit();
} //}
if ($identity['mac_hash'] == $hashed_mac) { if ($identity['mac_hash'] == $hashed_mac) {
$_SESSION['form_success'] = false; $_SESSION['form_success'] = false;
$_SESSION['form_success_message'] = 'MAC already set up, please remove it first to change name.'; $_SESSION['form_success_message'] = 'MAC already set up, please remove it first to change name.';
@ -62,15 +62,13 @@ function add_identity($name, $mac)
exit(); exit();
} }
function remove_identity($mac) function remove_identity($name)
{ {
if (preg_match('/' . MAC_PATTERN . '/', $mac) != 1) { if (preg_match('/' . NAME_PATTERN . '/', $name) != 1) {
http_response_code(400); http_response_code(400);
die("Bad data"); die("Bad data");
} }
$hashed_mac = hash_mac($mac);
$url = strtok($_SERVER['REQUEST_URI'], '?'); $url = strtok($_SERVER['REQUEST_URI'], '?');
$identities = json_decode(file_get_contents(IDENTITIES_FILE), true); $identities = json_decode(file_get_contents(IDENTITIES_FILE), true);
@ -78,7 +76,7 @@ function remove_identity($mac)
$new_identities = array(); $new_identities = array();
foreach ($identities as $identity) { foreach ($identities as $identity) {
if ($identity['mac_hash'] != $hashed_mac) { if ($identity['name'] != $name) {
array_push($new_identities, $identity); array_push($new_identities, $identity);
} }
} }
@ -101,8 +99,8 @@ if ($_SERVER['REQUEST_METHOD'] == "POST") {
isset($_POST['mac']) isset($_POST['mac'])
) { ) {
add_identity($_POST['name'], $_POST['mac']); add_identity($_POST['name'], $_POST['mac']);
} else if (isset($_POST['remove-mac'])) { } else if (isset($_POST['remove-name'])) {
remove_identity($_POST['remove-mac']); remove_identity($_POST['remove-name']);
} }
} }
?> ?>
@ -176,7 +174,7 @@ if ($_SERVER['REQUEST_METHOD'] == "POST") {
<div class="card-body"> <div class="card-body">
<h5 class="card-title">Gerät entfernen</h5> <h5 class="card-title">Gerät entfernen</h5>
<form method="POST"> <form method="POST">
<input class="form-control" type="text" name="remove-mac" pattern="<?php echo MAC_PATTERN; ?>" placeholder="MAC-Adresse" value="" required /><br> <input class="form-control" type="text" name="remove-name" pattern="<?php echo NAME_PATTERN; ?>" placeholder="Name" value="" required /><br>
<button class="btn btn-primary" type="submit">Entfernen</button> <button class="btn btn-primary" type="submit">Entfernen</button>
</form> </form>
</div> </div>