/
HttpBasicAuth.php
104 lines (95 loc) · 2.77 KB
/
HttpBasicAuth.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
<?php
/**
* Description of HttpBasicAuth
*
* @author usersio
*/
class HttpBasicAuth extends \Slim\Middleware
{
/**
* @var string
*/
protected $realm;
/**
* Constructor
*
* @param string $realm The HTTP Authentication realm
*/
public function __construct($realm = 'Protected Area')
{
$this->realm = $realm;
}
/**
* Deny Access
*
*/
public function deny_access() {
$res = $this->app->response();
$res->status(401);
$res->header('WWW-Authenticate', sprintf('Basic realm="%s"', $this->realm));
}
/**
* Authenticate
*
* @param string $username The HTTP Authentication username
* @param string $password The HTTP Authentication password
*
*/
public function authenticate($username, $password) {
if(!ctype_alnum($username))
return false;
if(isset($username) && isset($password)) {
$user=R::findOne('users','login="'.$username.'" AND password="'.sha1($password).'"');
if($user){
/*
* Deleting all beans older then 5mn
*/
$date2=time()-(60);
$date=date('Y-m-d h:i:s',$date2);
$beans=R::findAll('log','date<"'.$date.'"');
R::trashAll($beans);
/*
* checking if the users can do more requests
*/
$number=R::count('log','user_id= ?',[$user->id]);
if($number>250)
{
return false;
}else
{
/*
* saving the logs in the database
*/
$log=R::dispense('log');
$log->date=date('Y-m-d h:i:s');
$log->user=$user;
R::store($log);
return true;
}
}
}
else
{
return false;
}
}
/**
* Call
*
* This method will check the HTTP request headers for previous authentication. If
* the request has already authenticated, the next middleware is called. Otherwise,
* a 401 Authentication Required response is returned to the client.
*/
public function call()
{
$req = $this->app->request();
$res = $this->app->response();
$authUser = $req->headers('PHP_AUTH_USER');
$authPass = $req->headers('PHP_AUTH_PW');
if ($this->authenticate($authUser, $authPass)) {
$this->next->call();
} else {
$this->deny_access();
}
}
}