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 13701 aaronmk
	$path = $user;
11
12 13698 aaronmk
	# remove padding used to visually separate elements:__x__@y__@url -> x@y@url
13 13701 aaronmk
	$path = preg_replace('/\b__|__\b/', '', $path);
14
15 13702 aaronmk
	# 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 13703 aaronmk
	# to put the comment on a separate line when word-wrapping, use c-_:x@url
19
	#  (the extra _ is needed to work in Google spreadsheets)
20 13702 aaronmk
	$elems = explode(':', $path, 2);
21
	$path = array_pop($elems); # last (comment is first)
22
23 13695 aaronmk
	# translate reverse @-paths into forward .-paths
24 13701 aaronmk
	$path = implode(".", array_reverse(explode("@", $path)));
25
26
	return $path;
27 13695 aaronmk
}
28 13614 aaronmk
29
if (!isset($_SERVER["PHP_AUTH_USER"])) # browser first omits Authorization
30
{
31
	header('WWW-Authenticate: Basic realm="please leave username/password blank or as filled in"');
32
}
33
else
34
{
35 13668 aaronmk
	$dest = preg_replace('!\b/!', "./", $_SERVER["SCRIPT_URI"])."?";
36
		# append trailing . to host to prevent infinite redirect loop
37 13665 aaronmk
	if ($_SERVER["PHP_AUTH_USER"] !== "") # prepend to query string
38 13694 aaronmk
		$dest .= "."/*force dotpath*/.user2path(login__from_env());
39 13665 aaronmk
	$dest .= $_SERVER["QUERY_STRING"];
40 13625 aaronmk
41
	header("Location: ".$dest);
42 13614 aaronmk
}
43
?>