1
|
<?php
|
2
|
function login__from_env()
|
3
|
{
|
4
|
return $_SERVER["PHP_AUTH_USER"]
|
5
|
.($_SERVER["PHP_AUTH_PW"] !== "" ? ":".$_SERVER["PHP_AUTH_PW"] : "");
|
6
|
}
|
7
|
|
8
|
function user2path($user) # multiple @ and nested . OK: a@b.c@url -> url?b.c.a
|
9
|
{
|
10
|
# remove padding used to visually separate elements:__x__@y__@url -> x@y@url
|
11
|
$user = preg_replace('/\b__|__\b/', '', $user);
|
12
|
# translate reverse @-paths into forward .-paths
|
13
|
return implode(".", array_reverse(explode("@", $user)));
|
14
|
}
|
15
|
|
16
|
if (!isset($_SERVER["PHP_AUTH_USER"])) # browser first omits Authorization
|
17
|
{
|
18
|
header('WWW-Authenticate: Basic realm="please leave username/password blank or as filled in"');
|
19
|
}
|
20
|
else
|
21
|
{
|
22
|
$dest = preg_replace('!\b/!', "./", $_SERVER["SCRIPT_URI"])."?";
|
23
|
# append trailing . to host to prevent infinite redirect loop
|
24
|
if ($_SERVER["PHP_AUTH_USER"] !== "") # prepend to query string
|
25
|
$dest .= "."/*force dotpath*/.user2path(login__from_env());
|
26
|
$dest .= $_SERVER["QUERY_STRING"];
|
27
|
|
28
|
header("Location: ".$dest);
|
29
|
}
|
30
|
?>
|