/
SimpleLoginSecure.php
executable file
·293 lines (244 loc) · 7.26 KB
/
SimpleLoginSecure.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
//require_once('phpass-0.4/PasswordHash.php'); // This is for PHP 3 to 5
require_once('phpass-0.5/PasswordHash.php'); // This is for PHP 5 to 7
define('PHPASS_HASH_STRENGTH', 8);
define('PHPASS_HASH_PORTABLE', false);
/**
* SimpleLoginSecure Class
*
* Makes authentication simple and secure.
*
* Simplelogin expects the following database setup. If you are not using
* this setup you may need to do some tweaking.
*
* For MYSQL 5.0 and 5.5 use :
*
* CREATE TABLE `users` (
* `user_id` int(10) unsigned NOT NULL auto_increment,
* `user_email` varchar(255) NOT NULL default '',
* `user_pass` varchar(60) NOT NULL default '',
* `user_date` datetime NOT NULL default '0000-00-00 00:00:00',
* `user_modified` datetime NOT NULL default '0000-00-00 00:00:00',
* `user_last_login` datetime NULL default NULL,
* PRIMARY KEY (`user_id`),
* UNIQUE KEY `user_email` (`user_email`),
* ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
*
* For MYSQL 5.6 and more use :
*
* CREATE TABLE `users` (
* `user_id` int(10) unsigned NOT NULL auto_increment,
* `user_email` varchar(255) NOT NULL default '',
* `user_pass` varchar(60) NOT NULL default '',
* `user_date` datetime NOT NULL default CURRENT_TIMESTAMP,
* `user_modified` datetime NOT NULL default CURRENT_TIMESTAMP,
* `user_last_login` datetime NULL default NULL,
* PRIMARY KEY (`user_id`),
* UNIQUE KEY `user_email` (`user_email`),
* ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
*
*
* @package SimpleLoginSecure
* @version 3.1.1
* @author Stéphane Bourzeix, Pixelmio <stephane[at]bourzeix.com>
* @copyright Copyright (c) 2012-2013, Stéphane Bourzeix
* @license http://www.gnu.org/licenses/gpl-3.0.txt
* @link https://github.com/DaBourz/SimpleLoginSecure
*/
class SimpleLoginSecure
{
protected $CI; // CodeIgniter object
protected $user_table = 'users'; // Table name
/**
* Constructor
* Get the current CI object
*/
public function __construct()
{
// Assign the CodeIgniter super-object
$this->CI =& get_instance();
}
/**
* Create a user account
*
* @access public
* @param string
* @param string
* @param bool
* @return bool
*/
function create($user_email = '', $user_pass = '', $auto_login = true)
{
//Make sure account info was sent
if($user_email == '' OR $user_pass == '') {
return false;
}
//Check against user table
$this->CI->db->where('user_email', $user_email);
$query = $this->CI->db->get_where($this->user_table);
if ($query->num_rows() > 0) //user_email already exists
return false;
//Hash user_pass using phpass
$hasher = new PasswordHash(PHPASS_HASH_STRENGTH, PHPASS_HASH_PORTABLE);
$user_pass_hashed = $hasher->HashPassword($user_pass);
//Insert account into the database
$data = array(
'user_email' => $user_email,
'user_pass' => $user_pass_hashed,
'user_date' => date('c'),
'user_modified' => date('c'),
);
$this->CI->db->set($data);
if(!$this->CI->db->insert($this->user_table)) //There was a problem!
return false;
if($auto_login)
$this->login($user_email, $user_pass);
return true;
}
/**
* Update a user account
*
* Only updates the email, just here for you can
* extend / use it in your own class.
*
* @access public
* @param integer
* @param string
* @param bool
* @return bool
*/
function update($user_id = null, $user_email = '', $auto_login = true)
{
//Make sure account info was sent
if($user_id == null OR $user_email == '') {
return false;
}
//Check against user table
$this->CI->db->where('user_id', $user_id);
$query = $this->CI->db->get_where($this->user_table);
if ($query->num_rows() == 0){ // user don't exists
return false;
}
//Update account into the database
$data = array(
'user_email' => $user_email,
'user_modified' => date('c'),
);
$this->CI->db->where('user_id', $user_id);
if(!$this->CI->db->update($this->user_table, $data)) //There was a problem!
return false;
if($auto_login){
$user_data['user_email'] = $user_email;
$user_data['user'] = $user_data['user_email']; // for compatibility with Simplelogin
$this->CI->session->set_userdata($user_data);
}
return true;
}
/**
* Login and sets session variables
*
* @access public
* @param string
* @param string
* @return bool
*/
function login($user_email = '', $user_pass = '')
{
if($user_email == '' OR $user_pass == '')
return false;
//Check if already logged in
if($this->CI->session->userdata('user_email') == $user_email)
return true;
//Check against user table
$this->CI->db->where('user_email', $user_email);
$query = $this->CI->db->get_where($this->user_table);
if ($query->num_rows() > 0)
{
$user_data = $query->row_array();
$hasher = new PasswordHash(PHPASS_HASH_STRENGTH, PHPASS_HASH_PORTABLE);
if(!$hasher->CheckPassword($user_pass, $user_data['user_pass']))
return false;
//Create a fresh, brand new session
if (CI_VERSION >= '3.0') {
$this->CI->session->sess_regenerate(TRUE);
} else {
//Destroy old session
$this->CI->session->sess_destroy();
$this->CI->session->sess_create();
}
//Update last login date
$data = array('user_last_login' => date('c'));
$this->db->where('user_id', $user_data['user_id']);
$this->db->update($this->user_table, $data);
//Set session data
unset($user_data['user_pass']);
$user_data['user'] = $user_data['user_email']; // for compatibility with Simplelogin
$user_data['logged_in'] = true;
$this->CI->session->set_userdata($user_data);
return true;
}
else
{
return false;
}
}
/**
* Logout user
*
* @access public
* @return void
*/
function logout() {
$this->CI->session->sess_destroy();
}
/**
* Delete user
*
* @access public
* @param integer
* @return bool
*/
function delete($user_id)
{
if(!is_numeric($user_id))
return false;
return $this->CI->db->delete($this->user_table, array('user_id' => $user_id));
}
/**
* Edit a user password
* @author Stéphane Bourzeix, Pixelmio <stephane[at]bourzeix.com>
* @author Diego Castro <castroc.diego[at]gmail.com>
*
* @access public
* @param string
* @param string
* @param string
* @return bool
*/
function edit_password($user_email = '', $old_pass = '', $new_pass = '')
{
// Check if the password is the same as the old one
$this->CI->db->select('user_pass');
$query = $this->CI->db->get_where($this->user_table, array('user_email' => $user_email));
$user_data = $query->row_array();
$hasher = new PasswordHash(PHPASS_HASH_STRENGTH, PHPASS_HASH_PORTABLE);
if (!$hasher->CheckPassword($old_pass, $user_data['user_pass'])){ //old_pass is the same
return FALSE;
}
// Hash new_pass using phpass
$user_pass_hashed = $hasher->HashPassword($new_pass);
// Insert new password into the database
$data = array(
'user_pass' => $user_pass_hashed,
'user_modified' => date('c')
);
$this->CI->db->set($data);
$this->CI->db->where('user_email', $user_email);
if(!$this->CI->db->update($this->user_table, $data)){ // There was a problem!
return FALSE;
} else {
return TRUE;
}
}
}
?>