forked from dblencowe/Codeigniter-Authentication-Library
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathQuickauth.php
executable file
·381 lines (349 loc) · 12 KB
/
Quickauth.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
<?php
/*
* @Package Quick Authentication Library
* @author David Blencowe
* @link http://www.syntaxmonster.net
* @version 1.0.0
* @since Version 1.0.0
*/
class Quickauth
{
var $CI;
var $_username;
var $_table = array(
'users' => 'users',
'groups' => 'groups'
);
function __construct()
{
$this->CI =& get_instance();
$this->CI->load->library('session');
$this->CI->load->helper('url');
$this->CI->load->database();
$this->CI->load->helper('email');
$this->CI->load->helper('string');
}
function Quickauth()
{
self::__construct();
}
/**
* Used for restricting users to certain controllers and functions
* by their user level.
* @param String $restrict_to Name of the group
* @return TRUE/Error
*/
function restrict( $restrict_to = NULL, $redirect_to_login = FALSE )
{
if ( $restrict_to !== NULL)
{
if ($this->CI->session->userdata('logged_in') == TRUE)
{
$this->CI->db->where('name', $restrict_to);
$query = $this->CI->db->get($this->_table['groups']);
$level = $query->row_array();
$users_level = $this->CI->session->userdata('group_id');
if ($users_level >= $level['id'])
{
return TRUE;
}
else
{
show_error('You do not have sufficient privileges to access this page. <a href="javascript:history.back();">back</a>');
}
}
else
{
if ($redirect_to_login == FALSE)
{
show_error('You are not logged in. <a href="javascript:history.back();">back</a>');
}
else
{
redirect($redirect_to_login);
}
}
}
else
{
// Page locked to everyone
show_error('You cannot access this page. <a href="javascript:history.back();">back</a>');
}
}
/**
* Check the database too see if the username that is passed to the function exists.
* If it does then it is set to the global $_username variable for later use
* @param String $username
* @return TRUE/FALSE
*/
function _username_exists( $username )
{
$this->CI->db->where('username', $username);
$this->CI->db->limit(1);
$query = $this->CI->db->get($this->_table['users']);
if ($query->num_rows() !== 1)
{
return FALSE;
}
else
{
$this->_username = $username;
return TRUE;
}
}
/**
* Encrypts the submitted password and then checks it in the database
* using the value of the global $_username variable. If True is returned
* then the username and password submitted by the user are correct and they
* should then get logged in (See login function)
* @param String $password
* @return TRUE/FALSE
*/
function _check_correct_password( $password )
{
$this->CI->db->select('password');
$this->CI->db->where('username', $this->_username);
$this->CI->db->limit(1);
$query = $this->CI->db->get($this->_table['users']);
$result = $query->row();
if ($result->password == $this->encrypt($password))
{
return TRUE;
}
else
{
return FALSE;
}
}
/**
* Returns the number of characters in a string after it has been trimemd for
* whitespace.
* @param String $string
* @return Int
*/
function check_string_length( $string )
{
$string = trim($string);
return strlen($string);
}
/**
* This function will encrypt any data passed to it.
* It is primarily used for encrypting passwords before
* querying the database.
* @param String $data
* @return String
*/
function encrypt( $data )
{
if ($this->CI->config->item('encryption_key') !== NULL)
{
return sha1($this->CI->config->item('encryption_key').$data);
}
else
{
show_error('Please set an encryption key in your config file. <a href="javascript:history.back();">back</a>');
}
}
/**
* Check if a user is logged in
*
* @access public
* @param string
* @return bool
*/
function logged_in()
{
return $this->CI->session->userdata('logged_in');
}
/**
* Log a user out (destroy all session variables)
*
* @access public
*/
function logout()
{
$this->CI->session->sess_destroy();
}
/**
* Function for logging users in
* Accepts three arguements which correspond to username and password
* submitted from form and the target to redirect too if logged
* in succesfully
* @param String $username
* @param String $password
* @param String $redirect
*/
function login( $username, $password, $redirect = NULL )
{
if ($this->check_string_length($username) > 30)
{
show_error('Usernames can be no longer than 30 characters. <a href="javascript:history.back();">back</a>');
}
elseif ($this->_username_exists($username) !== TRUE)
{
show_error('The username you provided does not exist. <a href="javascript:history.back();">back</a>');
}
elseif ($this->check_string_length($password) > 32)
{
show_error('Passwords can be no longer than 32 chracters. <a href="javascript:history.back();">back</a>');
}
elseif ($this->_check_correct_password($password) !== TRUE)
{
show_error('You specified an incorrect password. <a href="javascript:history.back();">back</a>');
}
else
{
$this->CI->db->where('username', $username);
$query = $this->CI->db->get($this->_table['users']);
$row = $query->row_array();
if ($row['activated'] === 0)
{
show_error('This account has not been activated yet. <a href="javascript:history.back();">back</a>');
}
else
{
$data = array(
'username' => $username,
'user_id' => $row['id'],
'group_id' => $row['group_id'],
'logged_in' => TRUE
);
$this->CI->session->set_userdata($data);
if ($redirect !== NULL )
{
redirect($redirect);
}
}
}
}
/**
* Function for registering users
* Accepts three arguements from a form {Username, Password, Email}
* Checks for things liek confirm password should be called in
* the calling controller although this function will sanatize data
* for security
*
* @param String $username
* @param String $password
* @param String $email
*/
function register( $username, $password, $email )
{
if ($this->check_string_length($username) > 30)
{
show_error('Usernames can be no longer than 30 characters. <a href="javascript:history.back();">back</a>');
}
elseif ($this->_username_exists($username) === TRUE)
{
show_error('The username you provided is already in the database. <a href="javascript:history.back();">back</a>');
}
elseif($this->check_string_length($password) > 32)
{
show_error('Passwords can be no longer than 32 chracters. <a href="javascript:history.back();">back</a>');
}
elseif (!valid_email($email))
{
show_error('The email you submitted is invalid. <a href="javascript:history.back();">back</a>');
}
else
{
$password = $this->encrypt($password);
$data = array(
"id" => '',
"username" => $username,
"password" => $password,
"email" => $email,
//"ip" => $this->CI->input->ip_address()
);
$this->CI->db->insert($this->_table['users'], $data);
return TRUE;
}
}
/**
* This function is for activating a user account.
* You must supply a valid username from the database.
* @param String $username
*/
function activate_user( $username )
{
if ($username !== NULL)
{
$this->CI->db->set( 'activated', 1 );
$this->CI->db->where('username', $username);
$this->CI->db->update($this->_table['users']);
return TRUE;
}
else
{
return 0;
}
}
/**
* This function will email a user a newly generated password
* $userdata can be a password or an email submitted from a form
* @param String $userdata
*/
function retrieve_password( $userdata )
{
$email['newline'] = "\r\n";
$this->CI->load->library('email', $email);
if (valid_email($userdata))
{
$this->CI->db->where('email', $userdata);
$this->CI->db->limit(1);
$query = $this->CI->db->get($this->_table['users']);
$result = $query->row();
if ($query->num_rows() !== 1)
{
show_error('This email is not registered. <a href="javascript:history.back();">back</a>');
}
else
{
$new_password = random_string('alnum', 9);
$this->CI->db->where('email', $userdata);
$this->CI->db->set('password', $this->encrypt($new_password));
$this->CI->db->update($table['users']);
$message = "Hey there, \r\n";
$message .= "You or someone posing as you recently requested a new password at";
$message .= base_url()."\r\n";
$message .= "Your randomly generated password is: ".$new_password."\r\n";
$message .= "Thanks, \r\n".base_url();
$this->email->from($this->CI->config->site_email(), 'Automated Password Recovery');
$this->email->to($result->email);
$this->email->subject('Password Recovery');
$this->email->message($message);
$this->email->send();
}
}
else
{
$this->CI->db->where('username', $userdata);
$this->CI->db->limit(1);
$query = $this->CI->db->get($this->_table['users']);
$result = $query->row();
if ($query->num_rows() !== 1)
{
show_error('This username is not registered. <a href="javascript:history.back();">back</a>');
}
else
{
$new_password = random_string('alnum', 9);
$this->CI->db->where('username', $userdata);
$this->CI->db->set('password', $this->encrypt($new_password));
$this->CI->db->update($table['users']);
$message = "Hey there, \r\n";
$message .= "You or someone posing as you recently requested a new password at";
$message .= base_url()."\r\n";
$message .= "Your randomly generated password is: ".$new_password."\r\n";
$message .= "Thanks, \r\n".base_url();
$this->email->from($this->CI->config->site_email(), 'Automated Password Recovery');
$this->email->to($result->email);
$this->email->subject('Password Recovery');
$this->email->message($message);
$this->email->send();
}
}
}
}
/* End of file Quickauth.php */
/* Location: ./application/libraries/Quickauth.php */