-
Notifications
You must be signed in to change notification settings - Fork 1
/
Env.php
159 lines (139 loc) · 4 KB
/
Env.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
namespace core;
/**
* Safe to read environment-variables.
*/
class Env
{
private static $server = null;
public static function init()
{
if (! isset($_SERVER)) {
user_error("DevErr: Env-class loaded without SERVER-var");
}
self::$server = $_SERVER;
}
// Visitor IP
public static function ip()
{
return self::$server["HTTP_X_REAL_IP"] ?? self::$server["REMOTE_ADDR"];
}
// HTTP-protocol spoken to client.
public static function protocol()
{
return self::$server["SERVER_PROTOCOL"];
}
public static function encoding()
{
$enc = "plain";
$map = [
"application/json" => "json",
"text/html" => "html"
];
$accept = self::$server["HTTP_ACCEPT"] ?? "";
foreach ($map as $k => $m) {
if (strpos($accept, $k) !== false) {
$enc = $m;
}
}
return $enc;
}
// Get browser language and match it on $accept
// If no match is found $accept[0] is chosen.
public static function getBrowserLang(array $accept = ["en"])
{
if (! isset(self::$server['HTTP_ACCEPT_LANGUAGE'])) {
return $accept[0];
}
$raw = self::$server['HTTP_ACCEPT_LANGUAGE'];
$langs = substr($raw, 0, strpos($raw, ";"));
foreach (explode(",", $langs) as $lang) {
$lang = strtolower($lang);
if (strpos($lang, "-") !== false) {
$lang = substr($lang, 0, strpos($lang, "-"));
}
if (in_array($lang, $accept)) {
return $lang;
}
}
return $accept[0];
}
// Get previous page
public static function prev()
{
// https://domain.nl/...
$url = self::$server['HTTP_REFERER'];
return substr($url, strpos($url, "/", 8));
}
// Get previous useragent
public static function userAgent()
{
return self::$server['HTTP_USER_AGENT'] ?? '';
}
// Simple base64 auth
// $users = ["user" => "pass", "user2" => "pass2"...]
public static function blocking_auth($realm, array $users = [])
{
if (count($users) === 0) {
user_error("DevErr: Env::blocking_auth called without users.");
}
$v = self::protocol();
$requser = self::$server["PHP_AUTH_USER"] ?? "";
$reqpass = self::$server["PHP_AUTH_PW"] ?? "";
if ($requser === "" || $reqpass === "") {
header(sprintf('WWW-Authenticate: Basic realm="%s"', $realm));
header("HTTP/$v 401 Unauthorized");
echo "You must enter a valid login ID and password to access this resource\n";
exit;
}
$match = false;
foreach ($users as $user => $pass) {
if ($requser === $user && $reqpass === $pass) {
return;
}
}
header(sprintf('WWW-Authenticate: Basic realm="%s"', $realm));
header("HTTP/$v 403 Unauthorized");
echo "You must enter a valid login ID and password to access this resource\n";
exit;
}
// Read HFast-path
public static function hfastPath()
{
$sane = preg_replace("/[^A-Za-z0-9_\/]/", "", self::$server["DOCUMENT_URI"]);
return str_replace('/action/', '', $sane);
}
public static function method()
{
return self::$server["REQUEST_METHOD"];
}
public static function url_full()
{
return "https://" . self::$server['HTTP_HOST'] . self::$server['REQUEST_URI'] ?? '' . "?" . self::$server['QUERY_STRING'] ?? "";
}
public static function ajax()
{
return isset(self::$server["HTTP_X_REQUESTED_WITH"]) && self::$server["HTTP_X_REQUESTED_WITH"] === "XMLHttpRequest";
}
public static function referer_check() {
$domain = str_replace("https://", "", str_replace("http://", "", self::$server["HTTP_REFERER"]??""));
$domain = substr($domain, 0, strpos($domain, "/"));
return $domain === (self::$server["SERVER_NAME"] ?? "");
}
public static function referer() {
return self::$server["HTTP_REFERER"] ?? null;
}
public static function userPass() {
$requser = Env::$server["PHP_AUTH_USER"] ?? "";
$reqpass = self::$server["PHP_AUTH_PW"] ?? "";
return [$requser, $reqpass];
}
public static function host() {
return self::$server['HTTP_HOST'];
}
/** environment variable */
public static function var($key)
{
return getenv($key);
}
}