Project

General

Profile

1 13614 aaronmk
<?php
2 13694 aaronmk
function login__from_env()
3
{
4
	return $_SERVER["PHP_AUTH_USER"]
5
		.($_SERVER["PHP_AUTH_PW"] !== "" ? ":".$_SERVER["PHP_AUTH_PW"] : "");
6
}
7
8 13614 aaronmk
function user2path($user) # multiple @ and nested . OK: a@b.c@url -> url?b.c.a
9 13695 aaronmk
{
10
	# remove padding used to visually separate 1st element: _x_@url -> x@url
11
	$user = preg_replace('/^_*([^@]*?)_*(?=@|$)/', '$1', $user);
12
	# translate reverse @-paths into forward .-paths
13
	return implode(".", array_reverse(explode("@", $user)));
14
}
15 13614 aaronmk
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 13668 aaronmk
	$dest = preg_replace('!\b/!', "./", $_SERVER["SCRIPT_URI"])."?";
23
		# append trailing . to host to prevent infinite redirect loop
24 13665 aaronmk
	if ($_SERVER["PHP_AUTH_USER"] !== "") # prepend to query string
25 13694 aaronmk
		$dest .= "."/*force dotpath*/.user2path(login__from_env());
26 13665 aaronmk
	$dest .= $_SERVER["QUERY_STRING"];
27 13625 aaronmk
28
	header("Location: ".$dest);
29 13614 aaronmk
}
30
?>