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 linewraps: x_-_.y@url -> x.y@url
16
	# the _ are needed to work in Google spreadsheets
17
	$path = str_replace('_-_', '', $path);
18
	
19
	# remove category comments: x+y@url -> y@url
20
	# this special syntax is shorter than [x_-_]y@url
21
	# a UI can replace + with ' ' to produce a linewrap
22
	# can't use : for this because Firefox will not update the "password" for
23
	#  the website with the new value after the :
24
	$path = preg_replace('^.*?\+', '', $path);
25
	
26
	# remove insertion comments: [c]x[d]@url -> x@url
27
	# use [] because in writing, [] denotes insertion
28
	# insertion indicates that the semantic meaning of the [] portion also
29
	#  applies, even though it's not included in the linked term name
30
	$path = preg_replace('/\[.*?\]/', '', $path);
31
	
32
	# remove deletion comments: (-c_-)x@url -> c_x@url
33
	# use () because in editing, () denotes something to remove
34
	# the -...- indicate strikethrough (deletion)
35
	# deletion indicates that the semantic meaning of the () portion does not
36
	#  apply, even though it's included in the linked term name
37
	$path = preg_replace('/\(-([^)]*?)-\)/', '$1', $path);
38
	
39
	# translate reverse @-paths into forward .-paths
40
	$path = implode(".", array_reverse(explode("@", $path)));
41
	
42
	return $path;
43
}
44

    
45
if (!isset($_SERVER["PHP_AUTH_USER"])) # browser first omits Authorization
46
{
47
	header('WWW-Authenticate: Basic realm="'
48
.'please leave username/password blank or as filled in. '
49
.'**IMPORTANT**: to visit the homepage of this site, you should always '
50
.'append \".\": \"'.$_SERVER["HTTP_HOST"].'.\" "');
51
}
52
else
53
{
54
	$dest = preg_replace('!\b/!', "./", $_SERVER["SCRIPT_URI"])."?";
55
		# append trailing . to host to prevent infinite redirect loop
56
	if ($_SERVER["PHP_AUTH_USER"] !== "") # prepend to query string
57
		$dest .= "."/*force dotpath*/.user2path(login__from_env());
58
	$dest .= $_SERVER["QUERY_STRING"];
59
	
60
	header("Location: ".$dest);
61
}
62
?>
(32-32/34)