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 13696 aaronmk
	# supports embedded @ inside highlighted part: _x@y_@url
12
	$user = preg_replace('/^_*(.*?)_*(?=@|$)/', '$1', $user);
13 13695 aaronmk
	# translate reverse @-paths into forward .-paths
14
	return implode(".", array_reverse(explode("@", $user)));
15
}
16 13614 aaronmk
17
if (!isset($_SERVER["PHP_AUTH_USER"])) # browser first omits Authorization
18
{
19
	header('WWW-Authenticate: Basic realm="please leave username/password blank or as filled in"');
20
}
21
else
22
{
23 13668 aaronmk
	$dest = preg_replace('!\b/!', "./", $_SERVER["SCRIPT_URI"])."?";
24
		# append trailing . to host to prevent infinite redirect loop
25 13665 aaronmk
	if ($_SERVER["PHP_AUTH_USER"] !== "") # prepend to query string
26 13694 aaronmk
		$dest .= "."/*force dotpath*/.user2path(login__from_env());
27 13665 aaronmk
	$dest .= $_SERVER["QUERY_STRING"];
28 13625 aaronmk
29
	header("Location: ".$dest);
30 13614 aaronmk
}
31
?>