Initial commit
This commit is contained in:
22
www/.php/auth.inc
Normal file
22
www/.php/auth.inc
Normal file
@ -0,0 +1,22 @@
|
||||
<?php
|
||||
require "credentials.inc";
|
||||
|
||||
function http_401_unauthorized(): void {
|
||||
header('Status: 401');
|
||||
header('WWW-Authenticate: Basic realm="Elwig"');
|
||||
header('Content-Type: text/plain; charset=UTF-8');
|
||||
header('Content-Length: 17');
|
||||
exit("401 Unauthorized\n");
|
||||
}
|
||||
|
||||
function authenticate(string $client): void {
|
||||
global $CREDENTIALS;
|
||||
$credentials = $CREDENTIALS[$client];
|
||||
if (!array_key_exists('PHP_AUTH_USER', $_SERVER) ||
|
||||
!array_key_exists('PHP_AUTH_PW', $_SERVER) ||
|
||||
!array_key_exists($_SERVER['PHP_AUTH_USER'], $credentials) ||
|
||||
$_SERVER['PHP_AUTH_PW'] !== $credentials[$_SERVER['PHP_AUTH_USER']])
|
||||
{
|
||||
http_401_unauthorized();
|
||||
}
|
||||
}
|
8
www/.php/credentials.sample.inc
Normal file
8
www/.php/credentials.sample.inc
Normal file
@ -0,0 +1,8 @@
|
||||
<?php
|
||||
global $CREDENTIALS;
|
||||
|
||||
$CREDENTIALS = [
|
||||
'name' => [
|
||||
'username' => 'password',
|
||||
],
|
||||
];
|
50
www/.php/format.inc
Normal file
50
www/.php/format.inc
Normal file
@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
date_default_timezone_set('Europe/Vienna');
|
||||
|
||||
function get_fmt(): string {
|
||||
$fmt = _get_fmt();
|
||||
if ($fmt === 'ascii') {
|
||||
header('Status: 303');
|
||||
header('Location: ?format=text');
|
||||
header('Content-Length: 14');
|
||||
exit("303 See Other\n");
|
||||
} else if ($fmt !== 'json' && $fmt !== 'html' && $fmt !== 'text') {
|
||||
header('Status: 300');
|
||||
header('Content-Type: text/html; charset=UTF-8');
|
||||
header('Content-Length: 162');
|
||||
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/plain') {
|
||||
return 'text';
|
||||
} else {
|
||||
return 'html';
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user