/
http_auth.php
executable file
·62 lines (55 loc) · 1.44 KB
/
http_auth.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
<?php
namespace PMVC\PlugIn\http_auth;
// \PMVC\l(__DIR__.'/xxx.php');
${_INIT_CONFIG}[_CLASS] = __NAMESPACE__.'\http_auth';
class http_auth extends \PMVC\PlugIn
{
public function auth()
{
$env = \PMVC\plug('getenv');
$authUser = $env->get('PHP_AUTH_USER');
$authPass = $env->get('PHP_AUTH_PW');
if (empty($authUser) && empty($authPass)) {
$this->header();
echo $this->getTip();
exit();
}
if ($this['user'] && $this['pass']) {
if (
0 === strcmp((string)$this['user'], (string)$authUser) &&
0 === strcmp((string)$this['pass'], (string)$authPass)
) {
return true;
}
}
$this->block();
}
public function header()
{
$headers = [
'WWW-Authenticate: Basic realm="'.$this->getTip().'"',
'HTTP/1.0 401 Unauthorized'
];
foreach ($headers as $h) {
header($h);
}
}
public function block()
{
$this->header();
if (!is_null($this['callback'])) {
return call_user_func(
$this['callback']
);
}
trigger_error(403, E_USER_ERROR);
exit();
}
public function getTip()
{
if (empty($this['tip'])) {
$this['tip'] = 'Guard for '.getenv('HTTP_HOST');
}
return $this['tip'];
}
}