<?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(): 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) ||
        $_SERVER['PHP_AUTH_PW'] !== $CREDENTIALS[$_SERVER['PHP_AUTH_USER']])
    {
        http_401_unauthorized();
    }
}
