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

View File

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

View File

@ -1,5 +1,7 @@
<?php <?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']; $method = $_SERVER['REQUEST_METHOD'];
if ($method == "POST") { if ($method == "POST") {
$password = $_POST['password']; $password = $_POST['password'];

View File

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

View File

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

View File

@ -1,5 +1,7 @@
<?php <?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'); date_default_timezone_set('UTC');
$GLOBALS['start_time'] = DateTime::createFromFormat(DateTime::ISO8601, $config["start_date"]); $GLOBALS['start_time'] = DateTime::createFromFormat(DateTime::ISO8601, $config["start_date"]);
$GLOBALS['number_of_controls'] = 3; $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) { function registration_table($runners) {
parse_str($_SERVER['QUERY_STRING'], $query); parse_str($_SERVER['QUERY_STRING'], $query);

View File

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

View File

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

View File

@ -1,4 +1,5 @@
<?php <?php
$documentRoot = $_SERVER['DOCUMENT_ROOT'];
class Runner class Runner
{ {
public int $id; public int $id;
@ -115,8 +116,9 @@ function time_diff(DateTime $date_1, DateTime $date_2) {
} }
function read_runners_from_csv(){ function read_runners_from_csv(){
$documentRoot = $_SERVER['DOCUMENT_ROOT'];
$runners = []; $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"); $csv_runners = str_getcsv($csv_runners, "\n");
for ($i = 1; $i < count($csv_runners); $i++) { for ($i = 1; $i < count($csv_runners); $i++) {
$line = str_getcsv($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"); $timings = str_getcsv($timings, "\n");
for ($i = 0; $i < count($timings); $i++) { for ($i = 0; $i < count($timings); $i++) {
$line = str_getcsv($timings[$i]); $line = str_getcsv($timings[$i]);

View File

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

View File

@ -8,7 +8,7 @@
<link rel="stylesheet" href="matcha.css"> <link rel="stylesheet" href="matcha.css">
<main> <main>
<h2>Login</h2> <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> <label for="username">Navn</label>
<input id="navn" name="username" type="text" required> <input id="navn" name="username" type="text" required>
@ -26,7 +26,7 @@
<script> <script>
let xmlHttpReq = new XMLHttpRequest(); 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.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8")
xmlHttpReq.send("username=" + localStorage.getItem("navn")+"&"+"password=" + localStorage.getItem("passord")); xmlHttpReq.send("username=" + localStorage.getItem("navn")+"&"+"password=" + localStorage.getItem("passord"));
if (xmlHttpReq.status == 200){ 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> <div class="flash attention">Startnummer blir tildelt nærmere arrangementet. Prøver å oppdatere lista jevnlig. TBN</div>
<?php <?php
$query = ["type"=>"paameldte"]; $query = ["type"=>"paameldte"];
include("table.php"); include("api/table.php");
participants_table($runners); participants_table($runners);
?> ?>
<footer> <footer>

View File

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