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 insertion comments: [c]x[d]@url -> x@url
|
20
|
# use [] because in writing, [] denotes insertion
|
21
|
# can't use : for this because Firefox will not update the "password" for
|
22
|
# the website with the new value after the :
|
23
|
$path = preg_replace('/\[.*?\]/', '', $path);
|
24
|
|
25
|
# remove deletion comments: (c_)x(_d)@url -> c_x_d@url
|
26
|
# use () because in editing, () denotes something to remove
|
27
|
# deletion indicates that the semantic meaning of the () portion does not
|
28
|
# apply, even though it's included in the linked term name
|
29
|
$path = preg_replace('/[()]/', '', $path);
|
30
|
|
31
|
# translate reverse @-paths into forward .-paths
|
32
|
$path = implode(".", array_reverse(explode("@", $path)));
|
33
|
|
34
|
return $path;
|
35
|
}
|
36
|
|
37
|
if (!isset($_SERVER["PHP_AUTH_USER"])) # browser first omits Authorization
|
38
|
{
|
39
|
header('WWW-Authenticate: Basic realm="please leave username/password blank or as filled in"');
|
40
|
}
|
41
|
else
|
42
|
{
|
43
|
$dest = preg_replace('!\b/!', "./", $_SERVER["SCRIPT_URI"])."?";
|
44
|
# append trailing . to host to prevent infinite redirect loop
|
45
|
if ($_SERVER["PHP_AUTH_USER"] !== "") # prepend to query string
|
46
|
$dest .= "."/*force dotpath*/.user2path(login__from_env());
|
47
|
$dest .= $_SERVER["QUERY_STRING"];
|
48
|
|
49
|
header("Location: ".$dest);
|
50
|
}
|
51
|
?>
|