The formattening, part 2

This commit is contained in:
2025-03-12 00:38:57 +01:00
parent b6697e5750
commit da8afe735c
35 changed files with 1066 additions and 1033 deletions

View File

@@ -1,93 +1,95 @@
<?php
require_once dirname(dirname(__DIR__)) . implode(DIRECTORY_SEPARATOR, ['', 'inc', 'include.php']);
declare(strict_types=1);
require_once dirname(__DIR__, 2) . implode(\DIRECTORY_SEPARATOR, ['', 'inc', 'include.php']);
header('Content-Type: application/json');
$door = new \pvv\side\Door($pdo);
$door = new pvv\side\Door($pdo);
if($_SERVER['REQUEST_METHOD'] === 'POST') {
if (isset($_SERVER["HTTP_AUTHORIZATION"])) {
list($type, $data) = explode(" ", $_SERVER["HTTP_AUTHORIZATION"], 2);
if (strcasecmp($type, "Bearer") == 0) {
if (hash_equals($data, $DOOR_SECRET)) {
handleSetState();
} else {
echo '{"status": "error", "message": "Invalid authentication key"}';
die();
}
} else {
echo '{"status": "error", "message": "Invalid authentication method"}';
die();
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (isset($_SERVER['HTTP_AUTHORIZATION'])) {
[$type, $data] = explode(' ', $_SERVER['HTTP_AUTHORIZATION'], 2);
if (strcasecmp($type, 'Bearer') == 0) {
if (hash_equals($data, $DOOR_SECRET)) {
handleSetState();
} else {
echo '{"status": "error", "message": "Invalid authentication key"}';
exit;
}
} else {
echo '{"status": "error", "message": "Missing authentication"}';
die();
echo '{"status": "error", "message": "Invalid authentication method"}';
exit;
}
} else {
echo '{"status": "error", "message": "Missing authentication"}';
exit;
}
} elseif ($_SERVER['REQUEST_METHOD'] === 'GET') {
if (isset($_GET["period"])) {
$period = (string)htmlspecialchars($_GET["period"]);
if ($period == "day") {
$startTime = time() - (60*60*24);
} else if ($period == "week") {
$startTime = time() - (60*60*24*7);
} else {
echo '{"status": "error", "message": "Invalid period"}';
die();
}
if (isset($_GET['period'])) {
$period = (string) htmlspecialchars($_GET['period']);
$lines = $door->getEntriesAfter($startTime);
if (isset($_GET["edgeonly"]) && (bool)htmlspecialchars($_GET["edgeonly"])) {
//Ignore repeats
$lines = getChanges($lines);
}
echo json_encode([
'status' => "OK",
'entries' => $lines
]);
if ($period == 'day') {
$startTime = time() - (60 * 60 * 24);
} elseif ($period == 'week') {
$startTime = time() - (60 * 60 * 24 * 7);
} else {
//Only last entry
$line = (object)$door->getCurrent();
echo json_encode([
'status' => "OK",
'time' => $line->time,
'open' => $line->open
]);
echo '{"status": "error", "message": "Invalid period"}';
exit;
}
$lines = $door->getEntriesAfter($startTime);
if (isset($_GET['edgeonly']) && (bool) htmlspecialchars($_GET['edgeonly'])) {
// Ignore repeats
$lines = getChanges($lines);
}
echo json_encode([
'status' => 'OK',
'entries' => $lines,
]);
} else {
// Only last entry
$line = (object) $door->getCurrent();
echo json_encode([
'status' => 'OK',
'time' => $line->time,
'open' => $line->open,
]);
}
}
function handleSetState() {
global $door;
function handleSetState(): void {
global $door;
$jsonobj = file_get_contents('php://input');
$event = json_decode($jsonobj);
$jsonobj = file_get_contents('php://input');
$event = json_decode($jsonobj);
if ((!isset($event->time)) || (!is_numeric($event->time))) {
echo '{"status": "error", "message": "Invalid timestamp"}';
die();
}
if ((!isset($event->isDoorOpen)) || (!is_bool($event->isDoorOpen))) {
echo '{"status": "error", "message": "Invalid door state"}';
die();
}
if ((!isset($event->time)) || (!is_numeric($event->time))) {
echo '{"status": "error", "message": "Invalid timestamp"}';
exit;
}
if ((!isset($event->isDoorOpen)) || (!is_bool($event->isDoorOpen))) {
echo '{"status": "error", "message": "Invalid door state"}';
exit;
}
$door->createEvent((int)($event->time), $event->isDoorOpen ? 1 : 0);
echo '{"status": "OK"}';
}
$door->createEvent((int) $event->time, $event->isDoorOpen ? 1 : 0);
echo '{"status": "OK"}';
}
function getChanges($items) {
$prevState = 2;
$res = [];
$prevState = 2;
$res = [];
foreach($items as $item) {
if ($item["open"] !== $prevState) {
array_push($res, $item);
$prevState = $item["open"];
}
foreach ($items as $item) {
if ($item['open'] !== $prevState) {
$res[] = $item;
$prevState = $item['open'];
}
}
return $res;
return $res;
}