Rework everything

This commit is contained in:
2024-05-11 21:21:54 +02:00
parent 2ddeaf1bfa
commit 01f240cd57
13 changed files with 176 additions and 71 deletions

27
www/.php/auth.inc Normal file
View File

@ -0,0 +1,27 @@
<?php
require "credentials.inc";
function authenticate(): void {
global $CREDENTIALS;
if (!isset($_SEVER['PHP_AUTH_USER']) || !isset($_SEVER['PHP_AUTH_PW']) ||
!array_key_exists($_SERVER['PHP_AUTH_USER'], $CREDENTIALS) ||
$_SERVER['PHP_AUTH_PW'] !== $CREDENTIALS[$_SERVER['PHP_AUTH_USER']])
{
header('Status: 401');
header('WWW-Authenticate: Basic realm="Elwig"');
exit("401 Unauthorized :(\n");
}
}
function authenticate_client(string $client): void {
global $CLIENT_CREDENTIALS;
$credentials = $CLIENT_CREDENTIALS[$client];
if (!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW']) ||
!array_key_exists($_SERVER['PHP_AUTH_USER'], $credentials) ||
$_SERVER['PHP_AUTH_PW'] !== $credentials[$_SERVER['PHP_AUTH_USER']])
{
header('Status: 401');
header('WWW-Authenticate: Basic realm="Elwig"');
exit("401 Unauthorized :(\n");
}
}

View File

@ -0,0 +1,16 @@
<?php
global $GITEA_TOKEN;
global $CREDENTIALS;
global $CLIENT_CREDENTIALS;
$GITEA_TOKEN = 'token';
$CREDENTIALS = [
'username' => 'password',
];
$CLIENT_CREDENTIALS = [
'name' => [
'username' => 'password',
],
];

48
www/.php/format.inc Normal file
View File

@ -0,0 +1,48 @@
<?php
date_default_timezone_set('Europe/Vienna');
function get_fmt(): string {
$fmt = _get_fmt();
if ($fmt === 'ascii') {
header('Status: 303');
header('Location: ?format=text');
exit();
} else if ($fmt !== 'json' && $fmt !== 'html' && $fmt !== 'text') {
header('Status: 300');
header('Content-Type: text/html; charset=UTF-8');
echo "<!DOCTYPE html><html><head></head><body>\n<a href='?format=html'>HTML</a><br/>\n<a href='?format=json'>JSON</a><br/>\n<a href='?format=text'>Text</a>\n</body></html>\n";
exit();
}
return $fmt;
}
function _get_fmt(): string {
if (!empty($_GET['format'])) return $_GET['format'];
$fmts = [];
foreach (explode(',', $_SERVER['HTTP_ACCEPT']) as $acc) {
$acc = explode(';', trim($acc));
$q = 1;
if (sizeof($acc) > 1) {
$qv = trim($acc[1]);
if (str_starts_with($qv, 'q=')) {
$q = (double)substr($qv, 2);
}
}
$fmts[trim($acc[0])] = $q;
}
arsort($fmts, SORT_NUMERIC);
array_filter($fmts, function($k) {
return str_contains($k, '/json') || $k === 'text/html' || $k === 'text/plain' || str_contains($k, 'text/*');
});
$type = sizeof($fmts) > 0 ? array_key_first($fmts) : null;
if (str_contains($type, '/json')) {
return 'json';
} else if ($type === 'text/html') {
return 'html';
} else {
return 'text';
}
}