45 lines
1.3 KiB
PHP
45 lines
1.3 KiB
PHP
<?php
|
|
require "credentials.inc";
|
|
|
|
function http_401_unauthorized(?string $mode = null): void {
|
|
header('Status: 401');
|
|
header('WWW-Authenticate: Basic realm="Elwig"');
|
|
if ($mode === 'text' || $mode === 'ascii') {
|
|
header('Content-Type: text/plain; charset=UTF-8');
|
|
header('Content-Length: 17');
|
|
echo "401 Unauthorized\n";
|
|
} else {
|
|
header('Content-Length: 0');
|
|
}
|
|
exit;
|
|
}
|
|
|
|
function http_403_forbidden(?string $mode = null): void {
|
|
header('Status: 403');
|
|
header('WWW-Authenticate: Basic realm="Elwig"');
|
|
if ($mode === 'text' || $mode === 'ascii') {
|
|
header('Content-Type: text/plain; charset=UTF-8');
|
|
header('Content-Length: 14');
|
|
echo "403 Forbidden\n";
|
|
} else {
|
|
header('Content-Length: 0');
|
|
}
|
|
exit;
|
|
}
|
|
|
|
function authenticate(array $scope, ?string $mode = null): void {
|
|
global $CREDENTIALS;
|
|
if (!array_key_exists('PHP_AUTH_USER', $_SERVER) ||
|
|
!array_key_exists('PHP_AUTH_PW', $_SERVER) ||
|
|
!array_key_exists($_SERVER['PHP_AUTH_USER'], $CREDENTIALS))
|
|
{
|
|
http_401_unauthorized($mode);
|
|
}
|
|
$cred = $CREDENTIALS[$_SERVER['PHP_AUTH_USER']];
|
|
if ($_SERVER['PHP_AUTH_PW'] !== $cred[1]) {
|
|
http_401_unauthorized($mode);
|
|
} else if (!in_array($cred[0], $scope)) {
|
|
http_403_forbidden($mode);
|
|
}
|
|
}
|