CZI_presence_detector/website/index.php

89 lines
2.9 KiB
PHP
Raw Normal View History

<?php
$page_title = 'CZI Presence Detector';
$present_file = 'present.json';
$identities_file = 'identities.json';
$name_pattern = "^([ \u00c0-\u01ffa-zA-Z'\-]){1,30}$";
$mac_pattern = '^([0-9A-Fa-f]{2}[:-s]){5}([0-9A-Fa-f]{2})$';
$present = json_decode(file_get_contents($present_file), true);
$tz = 'Europe/Berlin';
$datetime = new DateTime("now", new DateTimeZone($tz));
$datetime->setTimestamp($present["timestamp"]);
if ($_SERVER['REQUEST_METHOD'] == "POST") {
if (
preg_match($name_pattern, $_POST['name']) != 1 ||
preg_match($mac_pattern, $_POST['mac'] != 1)
) {
http_response_code(400);
die("Bad data");
}
$name = $_POST['name'];
$normalized_mac = str_replace('-', ':', strtolower($_POST['mac']));
$hashed_mac = hash('sha256', $normalized_mac);
$identities = json_decode(file_get_contents($identities_file), true);
foreach ($identities as $identity) {
if (
$identity['name'] == $name ||
$identity['mac_hash'] == $hashed_mac
) {
// TODO show error to user
die("Duplicate data");
}
}
// TODO store new identity
header("Location: /", true, 303);
exit();
}
?>
<html>
<head>
<title><?php echo $page_title; ?></title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="bootstrap/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-4bw+/aepP/YC94hEpVNVgiZdgIC5+VKNBQNGCHeKRQN+PtmoHDEXuppvnDJzQIu9" crossorigin="anonymous">
<script src="bootstrap/js/bootstrap.bundle.min.js" integrity="sha384-HwwvtgBNo3bZJJLYd8oVXjrBZt8cqVSpeBNS5n7C8IVInixGAoxmnlMuBnhbgrkm" crossorigin="anonymous"></script>
</head>
<body>
<nav class="navbar navbar-light bg-light justify-content-between" style="padding: 0">
<span class="navbar-brand mb-0 h1" style="padding-left: 1rem"><?php echo $page_title; ?></span>
</nav>
<div id="content" style="padding: 2rem;">
<h2>Jetzt gerade im CZI <small>(letztes Update von <?php echo $datetime->format('H:i:s d.m.Y'); ?>)</small></h2><br>
<table class="table">
<thead>
<tr>
<th>Name</th>
</tr>
</thead>
<?php
foreach ($present["names"] as $name) {
?>
<tr>
<td><?php echo $name; ?></td>
</tr>
<?php
}
?>
</table>
<br><br><br>
<div class="card" style="max-width: 30rem;">
<div class="card-body">
<h5 class="card-title">Neues Gerät tracken</h5>
<form method="POST">
<input class="form-control" type="text" name="name" pattern="<?php print($name_pattern); ?>" placeholder="Gebe hier deinen Namen ein" value="" /><br>
<input class="form-control" type="text" name="mac" pattern="<?php print($mac_pattern); ?>" placeholder="Gebe hier die MAC-Adresse des zu trackenden Gerätes ein" value="" /><br>
<button class="btn btn-primary" type="submit">Speichern</button>
</form></div></div>
</div>
</body>
</html>