Flytta endepunkter inn i api/

This commit is contained in:
Trygve 2025-10-28 21:44:08 +01:00
parent 604fb365c6
commit 4cc7ff821c
14 changed files with 45 additions and 34 deletions

Binary file not shown.

View File

@ -38,7 +38,7 @@
</nav>
<button class="danger" onclick="log_out()">Logg ut</button>
<h1>Søk opp løper</h1>
<form action="/runner.php" method="GET" hx-boost="true" hx-target="#runner_info" hx-swap="show:none">
<form action="/api/runner.php" method="GET" hx-boost="true" hx-target="#runner_info" hx-swap="show:none">
<label>
Navn, startnummer eller klubb
<input type="text" name="search" id="search">
@ -48,7 +48,7 @@
<div id="runner_info" class="profile-card"></div>
<h1>Løpende resultater</h1>
<?php
include("table.php");
include("api/table.php");
liveresult_table($runners);
?>
<script>
@ -60,7 +60,7 @@ liveresult_table($runners);
table.innerHTML = this.responseText;
localStorage.setItem("ETag", this.getResponseHeader("ETag"));
}};
request.open("GET", "table.php?type=liveresultater");
request.open("GET", "api/table.php?type=liveresultater");
request.setRequestHeader("If-None-Match", localStorage.getItem("ETag"));
request.send();
}
@ -75,7 +75,7 @@ liveresult_table($runners);
}
let xmlHttpReq = new XMLHttpRequest();
xmlHttpReq.open("POST", "/is_authorized.php", false);
xmlHttpReq.open("POST", "/api/is_authorized.php", false);
xmlHttpReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8")
xmlHttpReq.send("username=" + localStorage.getItem("navn")+"&"+"password=" + localStorage.getItem("passord"));
if (xmlHttpReq.status != 200){

View File

@ -1,5 +1,5 @@
<?php
$documentRoot = $_SERVER['DOCUMENT_ROOT'];
function write_ini_file($assoc_array, $path) {
$content = "";
foreach ($assoc_array as $section => $values) {
@ -16,9 +16,9 @@ function write_ini_file($assoc_array, $path) {
file_put_contents($path, $content);
}
$config = parse_ini_file("data/config.ini");
$config = parse_ini_file("$documentRoot/data/config.ini");
$hash = file_get_contents("data/hash.txt");
$hash = file_get_contents("$documentRoot/data/hash.txt");
$method = $_SERVER['REQUEST_METHOD'];
if ($method == "POST") {
$start_time = $_POST['start_time'];
@ -28,10 +28,10 @@ if ($method == "POST") {
http_response_code(response_code: 401);
}
else {
$file = 'data/config.ini';
$file = "$documentRoot/data/config.ini";
$config["start_date"] = $start_time . "+01";
write_ini_file($config, $file);
header('Location: config_editor.html');
header("Location: $documentRoot/config_editor.html");
}
}
elseif ($method == "GET"){

View File

@ -1,5 +1,7 @@
<?php
$hash = file_get_contents("data/hash.txt");
$documentRoot = $_SERVER['DOCUMENT_ROOT'];
print_r($documentRoot);
$hash = file_get_contents("$documentRoot/data/hash.txt");
$method = $_SERVER['REQUEST_METHOD'];
if ($method == "POST") {
$password = $_POST['password'];

View File

@ -1,16 +1,18 @@
<?php
$documentRoot = $_SERVER['DOCUMENT_ROOT'];
$control = $_POST['control'];
$runner_id = $_POST['id'];
$time = $_POST['time'];
$password = $_POST['password'];
$hash = file_get_contents("data/hash.txt");
$hash = file_get_contents("$documentRoot/data/hash.txt");
if (!password_verify($password, $hash)) {
http_response_code(response_code: 401);
}
else {
$file = 'data/passering.csv';
$file = "$documentRoot/data/passering.csv";
$current = file_get_contents($file);
$current .= $control . "," . $runner_id . "," . $time . "\n";
file_put_contents($file, $current);

View File

@ -1,6 +1,9 @@
<?php
include("import_runners.php");
$hash = file_get_contents("data/hash.txt");
$documentRoot = $_SERVER['DOCUMENT_ROOT'];
include("$documentRoot/import_runners.php");
$hash = file_get_contents("$documentRoot/data/hash.txt");
$method = $_SERVER['REQUEST_METHOD'];
if ($method == "POST") {
$runner_id = $_POST['id'];
@ -8,7 +11,7 @@ if ($method == "POST") {
$club = $_POST['club'];
$course = $_POST['course'];
$line = $runner_id . ";;" . $name . ";;;" . $club . ";" . $course . "\n";
$line = $runner_id . ";;" . $name . ";;;" . $club . ";" . $course . "\n;;";
$password = $_POST['password'];
if (!password_verify($password, $hash)) {
@ -18,7 +21,7 @@ if ($method == "POST") {
http_response_code(response_code: 400);
}
else {
$file = 'data/db.csv';
$file = "$documentRoot/data/db.csv";
file_put_contents($file, $line, FILE_APPEND);
}
}
@ -45,7 +48,7 @@ if ($method == "GET") {
for ($i = 0; $i < count($filtered); $i++) {
$runner = $filtered[$i];
$response .= "<button class=\"default\" hx-get=\"/runner.php?search=$runner->id\" hx-target=\"#runner_info\" hx-swap=\"show:none\">$runner->id $runner->name</button>";
$response .= "<button class=\"default\" hx-get=\"/api/runner.php?search=$runner->id\" hx-target=\"#runner_info\" hx-swap=\"show:none\">$runner->id $runner->name</button>";
header("HX-Replace-Url: false");
}
echo($response);

View File

@ -1,5 +1,7 @@
<?php
$config = parse_ini_file("data/config.ini");
$documentRoot = $_SERVER['DOCUMENT_ROOT'];
$config = parse_ini_file("$documentRoot/data/config.ini");
date_default_timezone_set('UTC');
$GLOBALS['start_time'] = DateTime::createFromFormat(DateTime::ISO8601, $config["start_date"]);
$GLOBALS['number_of_controls'] = 3;
@ -19,7 +21,7 @@ if(isset($_SERVER['HTTP_IF_NONE_MATCH'])) {
}
*/
include("import_runners.php");
include("$documentRoot/import_runners.php");
function registration_table($runners) {
parse_str($_SERVER['QUERY_STRING'], $query);

View File

@ -24,7 +24,7 @@
</nav>
<h2>Sett dato og tid startskuddet gikk:</h2>
<form action="/config.php" method="POST">
<form action="/api/config.php" method="POST">
<input style="display: none;" type="text" id="password" name="password" required>
<label>
Dato og tid
@ -43,7 +43,7 @@
// Sjekk om brukeren er logga inn. hvis ikke hiv de ut til innloggingskjermen
let xmlHttpReq = new XMLHttpRequest();
xmlHttpReq.open("POST", "/is_authorized.php", false);
xmlHttpReq.open("POST", "/api/is_authorized.php", false);
xmlHttpReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8")
xmlHttpReq.send("username=" + localStorage.getItem("navn")+"&"+"password=" + localStorage.getItem("passord"));
if (xmlHttpReq.status != 200){
@ -51,7 +51,7 @@
}
document.getElementById('password').value = localStorage.getItem("passord");
xmlHttpReq.open("GET", "/config.php", false);
xmlHttpReq.open("GET", "/api/config.php", false);
xmlHttpReq.send();
document.getElementById('start_time').value = xmlHttpReq.response.split('+')[0]
console.log(xmlHttpReq.response)

View File

@ -23,7 +23,7 @@
</menu>
</nav>
<h2>Legg til løper</h2>
<form action="/runner.php" method="POST">
<form action="/api/runner.php" method="POST">
<input style="display: none;" type="text" id="password" name="password" required>
<label for="id">Startnummer:
@ -60,7 +60,7 @@
// Sjekk om brukeren er logga inn. hvis ikke hiv de ut til innloggingskjermen
let xmlHttpReq = new XMLHttpRequest();
xmlHttpReq.open("POST", "/is_authorized.php", false);
xmlHttpReq.open("POST", "/api/is_authorized.php", false);
xmlHttpReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8")
xmlHttpReq.send("username=" + localStorage.getItem("navn")+"&"+"password=" + localStorage.getItem("passord"));
if (xmlHttpReq.status != 200){

View File

@ -1,4 +1,5 @@
<?php
$documentRoot = $_SERVER['DOCUMENT_ROOT'];
class Runner
{
public int $id;
@ -115,8 +116,9 @@ function time_diff(DateTime $date_1, DateTime $date_2) {
}
function read_runners_from_csv(){
$documentRoot = $_SERVER['DOCUMENT_ROOT'];
$runners = [];
$csv_runners = file_get_contents("data/db.csv");
$csv_runners = file_get_contents("$documentRoot/data/db.csv");
$csv_runners = str_getcsv($csv_runners, "\n");
for ($i = 1; $i < count($csv_runners); $i++) {
$line = str_getcsv($csv_runners[$i], ";");
@ -124,7 +126,7 @@ function read_runners_from_csv(){
}
$timings = file_get_contents("data/passering.csv");
$timings = file_get_contents("$documentRoot/data/passering.csv");
$timings = str_getcsv($timings, "\n");
for ($i = 0; $i < count($timings); $i++) {
$line = str_getcsv($timings[$i]);

View File

@ -33,7 +33,7 @@
</div>
<h2>Vi tar forbehold om feil. Dette er ikke offisielle resultater</h2>
<?php
include("table.php");
include("api/table.php");
liveresult_table($runners);
?>
<footer>
@ -49,7 +49,7 @@ liveresult_table($runners);
table.innerHTML = this.responseText;
localStorage.setItem("ETag", this.getResponseHeader("ETag"));
}};
request.open("GET", "table.php?type=liveresultater");
request.open("GET", "api/table.php?type=liveresultater");
request.setRequestHeader("If-None-Match", localStorage.getItem("ETag"));
request.send();
}

View File

@ -8,7 +8,7 @@
<link rel="stylesheet" href="matcha.css">
<main>
<h2>Login</h2>
<form method="POST" action="/is_authorized.php" id="login">
<form method="POST" action="/api/is_authorized.php" id="login">
<label for="username">Navn</label>
<input id="navn" name="username" type="text" required>
@ -26,7 +26,7 @@
<script>
let xmlHttpReq = new XMLHttpRequest();
xmlHttpReq.open("POST", "/is_authorized.php", false);
xmlHttpReq.open("POST", "/api/is_authorized.php", false);
xmlHttpReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8")
xmlHttpReq.send("username=" + localStorage.getItem("navn")+"&"+"password=" + localStorage.getItem("passord"));
if (xmlHttpReq.status == 200){

View File

@ -23,7 +23,7 @@
<div class="flash attention">Startnummer blir tildelt nærmere arrangementet. Prøver å oppdatere lista jevnlig. TBN</div>
<?php
$query = ["type"=>"paameldte"];
include("table.php");
include("api/table.php");
participants_table($runners);
?>
<footer>

View File

@ -103,7 +103,7 @@ function register_runner(id) {
formData.append(name= 'id', value=id);
time = new Date(Date.now()).toISOString().split('.')[0]+"Z"
formData.append('time', time);
response = fetch("passing.php", {
response = fetch("api/passing.php", {
method: "POST",
body: formData,
})
@ -120,7 +120,7 @@ function update() {
const table = document.getElementById("runners");
control = get_control();
filter = document.getElementById("search").value;
let request = new Request(`table.php?type=registrering&control=`+control+`&filter=`+filter);
let request = new Request(`api/table.php?type=registrering&control=`+control+`&filter=`+filter);
fetch(request)
.then((response) => response.text())
.then((text) => {table.innerHTML = text;})
@ -129,7 +129,7 @@ function update() {
// Sjekk om brukeren er logga inn. hvis ikke hiv de ut til innloggingskjermen
let xmlHttpReq = new XMLHttpRequest();
xmlHttpReq.open("POST", "/is_authorized.php", false);
xmlHttpReq.open("POST", "/api/is_authorized.php", false);
xmlHttpReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8")
xmlHttpReq.send("username=" + localStorage.getItem("navn")+"&"+"password=" + localStorage.getItem("passord"));
if (xmlHttpReq.status != 200){