-
Notifications
You must be signed in to change notification settings - Fork 790
/
Login.php
150 lines (133 loc) · 3.97 KB
/
Login.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
<?php
/**
* class Login
* handles the user login/logout/session
*
* @author Panique <[email protected]>
* @version 1.2
*/
class Login extends Auth
{
/**
* the User status
* @var boolean
*/
private $is_logged_in = false; // status of login
/**
* The constructor handles login/logout action
*/
public function __construct()
{
parent::__construct();
// create/read session
$sessionId = session_id();
if (empty($sessionId)) {
session_start();
}
//the action to take
$action = filter_input(
INPUT_GET,
'action',
FILTER_SANITIZE_STRING,
FILTER_REQUIRE_SCALAR|FILTER_FLAG_STRIP_LOW|FILTER_FLAG_STRIP_HIGH
);
if (isset($_SESSION['user_token'])) {
$this->is_logged_in = $this->loginWithSessionData();
} elseif ('login' == $action) {
$this->is_logged_in = $this->loginWithPostData();
}
if (! $this->is_logged_in || filter_has_var(INPUT_GET, 'logout')) {
$this->doLogout();
}
}
/**
* perform the logout
*/
public function doLogout()
{
$this->is_logged_in = false;
$_SESSION = array();
session_destroy();
}
/**
* simply return the current state of the user's login
*
* @return boolean user's login status
*/
public function isUserLoggedIn()
{
return $this->is_logged_in;
}
/**
* Connect a user depending on his session data
*/
private function loginWithSessionData()
{
$this->errors = array();
//1 - Input Filtering and Validation
if (! $this->isValidToken($_SESSION['user_token'])) {
$this->errors['user_token'] = self::DATA_INVALID;
return false;
}
//2 - User Authentification
$auth = explode('|', $_SESSION['user_token']);
$login = filter_var($auth[0], FILTER_CALLBACK, array('options' => 'Auth::isValidUserName'));
if (! $login) {
$this->errors['user_name'] = self::DATA_INVALID;
return false;
} elseif (! ($user = $this->getUserByName($login))) {
$this->errors['user_name'] = self::USER_UNKNOWN;
return false;
}
//3 - Session Update
$this->loadSession($user);
return true;
}
/**
* Connect a user depending on his submitted post data
*
* @return boolean
*/
private function loginWithPostData()
{
$this->errors = array();
//1 - Input Filtering and Validation
$arguments = array(
'user_name' => array('filter' => FILTER_CALLBACK, 'options' => 'Auth::isValidUserName'),
'user_password' => array('filter' => FILTER_CALLBACK, 'options' => 'Auth::isValidPassword'),
);
$params = filter_input_array(INPUT_POST, $arguments);
$this->errors = array_map(array($this, 'isDataValid'), $params);
foreach ($this->errors as $keys => $value) {
if ($value == self::DATA_OK) {
unset($this->errors[$keys]);
}
}
if (count($this->errors)) {
return false;
}
//2 - User Authentification
$user = $this->getUserByName($params['user_name']);
if (! $user) {
$this->errors['user_name'] = self::USER_UNKNOWN;
return false;
} elseif (! password_verify($params['user_password'], $user['user_password_hash'])) {
$this->errors['user_password'] = self::DATA_INVALID;
return false;
}
//3 - Session Update
$this->loadSession($user);
return true;
}
/**
* load User Data into the session
* @return void
*/
private function loadSession(array $user)
{
foreach ($user as $key => $value) {
$_SESSION[$key] = $value;
}
$_SESSION['user_token'] = $this->generateToken($user['user_name']);
}
}