setTimestamp($present["timestamp"]);
function hash_mac($mac) {
$normalized_mac = str_replace('-', ':', strtolower($mac));
$hashed_mac = hash('sha256', $normalized_mac);
return $hashed_mac;
}
function add_identity($name, $mac) {
if (
preg_match('/' . $name_pattern . '/', $name) != 1 ||
preg_match('/' . $mac_pattern . '/', $mac) != 1
) {
http_response_code(400);
die("Bad data");
}
$hashed_mac = hash_mac($mac);
$identities = json_decode(file_get_contents(IDENTITIES_FILE), true);
$url = strtok($_SERVER['REQUEST_URI'], '?');
foreach ($identities as $identity) {
if ($identity['name'] == $name) {
header("Location: " . $url . "?dup_name", true, 303);
exit();
}
if ($identity['mac_hash'] == $hashed_mac) {
header("Location: " . $url . "?dup_mac", true, 303);
exit();
}
}
array_push($identities, array("name" => $name, "mac_hash" => $hashed_mac));
file_put_contents(IDENTITIES_FILE, json_encode($identities));
header("Location: " . $url . "?succ", true, 303);
exit();
}
function remove_identity($mac) {
if (preg_match('/' . $mac_pattern . '/', $mac) != 1) {
http_response_code(400);
die("Bad data");
}
$hashed_mac = hash_mac($mac);
$identities = json_decode(file_get_contents(IDENTITIES_FILE), true);
$new_identities = array();
foreach ($identities as $identity) {
if ($identity['mac_hash'] != $hashed_mac) {
array_push($new_identities, $identity);
}
file_put_contents(IDENTITIES_FILE, json_encode($new_identities));
header("Location: " . strtok($_SERVER['REQUEST_URI'], '?'), true, 303);
exit();
}
}
if ($_SERVER['REQUEST_METHOD'] == "POST") {
if (
isset($_POST['name']) &&
isset($_POST['mac'])
) {
add_identity($_POST['name'], $_POST['mac']);
}
else if (isset($_POST['remove-mac'])) {
remove_identity($_POST['remove-mac']);
}
}
?>
Jetzt gerade im CZI (letztes Update von format('H:i:s d.m.Y'); ?>)
Name already in use, please choose a different one.
MAC already set up, please remove it first to change name.
Identity successfully saved.