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
{
10
	$path = $user;
11
	
12
	# remove padding used to visually separate elements:__x__@y__@url -> x@y@url
13
	$path = preg_replace('/\b__|__\b/', '', $path);
14
	
15
	# remove any comment prefix: c:x@url -> x@url
16
	# *note*: when URL is used without http:// , you must include a non-letter
17
	#  in the comment (eg. c_:x@url) to work in Firefox
18
	$elems = explode(':', $path, 2);
19
	$path = array_pop($elems); # last (comment is first)
20
	
21
	# translate reverse @-paths into forward .-paths
22
	$path = implode(".", array_reverse(explode("@", $path)));
23
	
24
	return $path;
25
}
26

    
27
if (!isset($_SERVER["PHP_AUTH_USER"])) # browser first omits Authorization
28
{
29
	header('WWW-Authenticate: Basic realm="please leave username/password blank or as filled in"');
30
}
31
else
32
{
33
	$dest = preg_replace('!\b/!', "./", $_SERVER["SCRIPT_URI"])."?";
34
		# append trailing . to host to prevent infinite redirect loop
35
	if ($_SERVER["PHP_AUTH_USER"] !== "") # prepend to query string
36
		$dest .= "."/*force dotpath*/.user2path(login__from_env());
37
	$dest .= $_SERVER["QUERY_STRING"];
38
	
39
	header("Location: ".$dest);
40
}
41
?>
(33-33/35)