1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Login support for users plugin |
5
|
|
|
* |
6
|
|
|
* PHP Version 5 |
7
|
|
|
* |
8
|
|
|
* @category Authentication |
9
|
|
|
* @package Users |
10
|
|
|
* @author Hans-Joachim Piepereit <[email protected]> |
11
|
|
|
* @copyright 2013 cSphere Team |
12
|
|
|
* @license http://opensource.org/licenses/bsd-license Simplified BSD License |
13
|
|
|
* @link http://www.csphere.eu |
14
|
|
|
**/ |
15
|
|
|
|
16
|
|
|
namespace csphere\core\authentication; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Login support for users plugin |
20
|
|
|
* |
21
|
|
|
* @category Authentication |
22
|
|
|
* @package Users |
23
|
|
|
* @author Hans-Joachim Piepereit <[email protected]> |
24
|
|
|
* @copyright 2013 cSphere Team |
25
|
|
|
* @license http://opensource.org/licenses/bsd-license Simplified BSD License |
26
|
|
|
* @link http://www.csphere.eu |
27
|
|
|
**/ |
28
|
|
|
|
29
|
|
|
class Users |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* Session object |
33
|
|
|
**/ |
34
|
|
|
private $_session = null; |
|
|
|
|
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Datamapper object for users table |
38
|
|
|
**/ |
39
|
|
|
private $_mapper = null; |
|
|
|
|
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Prepare values that are needed for later usage |
43
|
|
|
* |
44
|
|
|
* @return \csphere\core\authentication\Users |
45
|
|
|
**/ |
|
|
|
|
46
|
|
|
|
47
|
|
|
public function __construct() |
48
|
|
|
{ |
49
|
|
|
// Create session and user table datamapper object |
50
|
|
|
$this->_mapper = new \csphere\core\datamapper\Model('users'); |
51
|
|
|
$this->_session = new \csphere\core\session\Session(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Whether someone is logged in at the moment |
56
|
|
|
* |
57
|
|
|
* @return boolean |
58
|
|
|
**/ |
|
|
|
|
59
|
|
|
|
60
|
|
|
public function status() |
61
|
|
|
{ |
62
|
|
|
// Check if user is still logged in |
63
|
|
|
$result = false; |
64
|
|
|
$user_id = $this->_session->get('user_id'); |
65
|
|
|
|
66
|
|
|
if (!empty($user_id)) { |
67
|
|
|
|
68
|
|
|
// Get user active info and check for existence |
69
|
|
|
$user = $this->_mapper->read($user_id, 'user_id'); |
70
|
|
|
|
71
|
|
|
if (empty($user['user_active'])) { |
72
|
|
|
|
73
|
|
|
// Kick user auth |
74
|
|
|
$this->logout(); |
75
|
|
|
|
76
|
|
|
} else { |
77
|
|
|
|
78
|
|
|
$result = true; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $result; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Attempt to authenticate a user |
87
|
|
|
* |
88
|
|
|
* @param string $name User name |
89
|
|
|
* @param string $password User password |
90
|
|
|
* |
91
|
|
|
* @return boolean |
92
|
|
|
**/ |
|
|
|
|
93
|
|
|
|
94
|
|
|
public function login($name, $password) |
95
|
|
|
{ |
96
|
|
|
// Get user from database |
97
|
|
|
$user = $this->_mapper->read($name, 'user_name'); |
98
|
|
|
$verify = false; |
99
|
|
|
|
100
|
|
|
// Verify password if both vars are not empty |
101
|
|
|
if (isset($user['user_password']) && !empty($password)) { |
102
|
|
|
|
103
|
|
|
$verify = \csphere\core\authentication\Password::compare( |
104
|
|
|
$password, $user['user_password'] |
105
|
|
|
); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
// Only grant access if password check is true |
109
|
|
|
if ($verify === true) { |
110
|
|
|
|
111
|
|
|
$this->_newLogin($user); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return $verify; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Update content on fresh logins |
119
|
|
|
* |
120
|
|
|
* @param array $user Array with user data |
121
|
|
|
* |
122
|
|
|
* @return void |
123
|
|
|
**/ |
|
|
|
|
124
|
|
|
|
125
|
|
|
private function _newLogin(array $user) |
126
|
|
|
{ |
127
|
|
|
// Set session data |
128
|
|
|
$this->_session->set('user_id', $user['user_id']); |
129
|
|
|
$this->_session->set('user_name', $user['user_name']); |
130
|
|
|
$this->_session->set('user_lang', $user['user_lang']); |
131
|
|
|
|
132
|
|
|
// Notify users_logins about new visit |
133
|
|
|
$web = \csphere\core\http\Input::get('server', 'HTTP_USER_AGENT'); |
134
|
|
|
$time = time(); |
135
|
|
|
|
136
|
|
|
$dm_logins = new \csphere\core\datamapper\Model('users', 'logins'); |
137
|
|
|
$login = $dm_logins->create(); |
138
|
|
|
|
139
|
|
|
$login['user_id'] = $user['user_id']; |
140
|
|
|
$login['login_since'] = $time; |
141
|
|
|
$login['login_browser'] = $web; |
142
|
|
|
|
143
|
|
|
$dm_logins->insert($login); |
144
|
|
|
|
145
|
|
|
// Update date of last login |
146
|
|
|
$update = ['user_id' => $user['user_id'], 'user_laston' => $time]; |
147
|
|
|
|
148
|
|
|
$this->_mapper->update($update); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Clear session data |
153
|
|
|
* |
154
|
|
|
* @return boolean |
155
|
|
|
**/ |
|
|
|
|
156
|
|
|
|
157
|
|
|
public function logout() |
158
|
|
|
{ |
159
|
|
|
// Destroy current session |
160
|
|
|
$this->_session->destroy(); |
161
|
|
|
|
162
|
|
|
return true; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|