Project

General

Profile

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
{ return implode(".", array_reverse(explode("@", $user))); }
10

    
11
if (!isset($_SERVER["PHP_AUTH_USER"])) # browser first omits Authorization
12
{
13
	header('WWW-Authenticate: Basic realm="please leave username/password blank or as filled in"');
14
}
15
else
16
{
17
	$dest = preg_replace('!\b/!', "./", $_SERVER["SCRIPT_URI"])."?";
18
		# append trailing . to host to prevent infinite redirect loop
19
	if ($_SERVER["PHP_AUTH_USER"] !== "") # prepend to query string
20
		$dest .= "."/*force dotpath*/.user2path(login__from_env());
21
	$dest .= $_SERVER["QUERY_STRING"];
22
	
23
	header("Location: ".$dest);
24
}
25
?>
(33-33/35)