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) {
      $_SESSION['form_success'] = false;
      $_SESSION['form_success_message'] = 'Name already in use, please choose a different one.';
      header("Location: " . $url, true, 303);
      exit();
    }
    if ($identity['mac_hash'] == $hashed_mac) {
      $_SESSION['form_success'] = false;
      $_SESSION['form_success_message'] = 'MAC already set up, please remove it first to change name.';
      header("Location: " . $url, true, 303);
      exit();
    }
  }
  array_push($identities, array("name" => $name, "mac_hash" => $hashed_mac));
  file_put_contents(IDENTITIES_FILE, json_encode($identities));
  $_SESSION['form_success'] = true;
  $_SESSION['form_success_message'] = 'Identity successfully saved.';
  header("Location: " . $url, 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));
    $_SESSION['form_success'] = true;
    $_SESSION['form_success_message'] = 'Identity successfully removed.';
    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'); ?>)