1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
if (!defined('BASEPATH')) { |
4
|
|
|
exit('No direct script access allowed'); |
5
|
|
|
} |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* DX Auth Class |
9
|
|
|
* |
10
|
|
|
* Authentication library for Code Igniter. |
11
|
|
|
* |
12
|
|
|
* @author Dexcell |
13
|
|
|
* @version 1.0.4 |
14
|
|
|
* @based on CL Auth by Jason Ashdown (http://http://www.jasonashdown.co.uk/) |
15
|
|
|
* @link http://dexcell.shinsengumiteam.com/dx_auth |
16
|
|
|
* @license MIT License Copyright (c) 2008 Erick Hartanto |
17
|
|
|
* @credits http://dexcell.shinsengumiteam.com/dx_auth/general/credits.html |
18
|
|
|
*/ |
19
|
|
|
class DX_Auth |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
// Private |
23
|
|
|
var $_banned; |
24
|
|
|
|
25
|
|
|
var $_ban_reason; |
26
|
|
|
|
27
|
|
|
var $_auth_error; // Contain user error when login |
28
|
|
|
|
29
|
|
|
var $_captcha_image; |
30
|
|
|
|
31
|
|
|
public function __construct() { |
32
|
|
|
$this->ci = &get_instance(); |
|
|
|
|
33
|
|
|
|
34
|
|
|
log_message('debug', 'DX Auth Initialized'); |
35
|
|
|
|
36
|
|
|
// Load required library |
37
|
|
|
//$this->ci->load->library('Session'); |
38
|
|
|
//$this->ci->load->database(); |
39
|
|
|
// Load DX Auth config |
40
|
|
|
$this->ci->load->config('auth'); |
41
|
|
|
|
42
|
|
|
// Load DX Auth language |
43
|
|
|
// $this->ci->lang->load('dx_auth'); |
44
|
|
|
// Load DX Auth event |
45
|
|
|
$this->ci->load->library('DX_Auth_Event'); |
46
|
|
|
|
47
|
|
|
// Initialize |
48
|
|
|
$this->_init(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/* Private function */ |
52
|
|
|
|
53
|
|
|
public function _init() { |
54
|
|
|
// When we load this library, auto Login any returning users |
55
|
|
|
$this->autologin(); |
56
|
|
|
|
57
|
|
|
// Init helper config variable |
58
|
|
|
$this->email_activation = $this->ci->config->item('DX_email_activation'); |
|
|
|
|
59
|
|
|
|
60
|
|
|
$this->allow_registration = $this->ci->config->item('DX_allow_registration'); |
|
|
|
|
61
|
|
|
$this->captcha_registration = $this->ci->config->item('DX_captcha_registration'); |
|
|
|
|
62
|
|
|
|
63
|
|
|
$this->captcha_login = $this->ci->config->item('DX_captcha_login'); |
|
|
|
|
64
|
|
|
|
65
|
|
|
//Use recaptcha |
66
|
|
|
$this->use_recaptcha = $this->ci->config->item('DX_use_recaptcha'); |
|
|
|
|
67
|
|
|
$this->use_audio_recaptcha = $this->ci->config->item('DX_use_audio_recaptcha'); |
|
|
|
|
68
|
|
|
|
69
|
|
|
// URIs |
70
|
|
|
$this->banned_uri = $this->ci->config->item('DX_banned_uri'); |
|
|
|
|
71
|
|
|
$this->deny_uri = $this->ci->config->item('DX_deny_uri'); |
|
|
|
|
72
|
|
|
$this->login_uri = $this->ci->config->item('DX_login_uri'); |
|
|
|
|
73
|
|
|
$this->logout_uri = $this->ci->config->item('DX_logout_uri'); |
|
|
|
|
74
|
|
|
$this->register_uri = $this->ci->config->item('DX_register_uri'); |
|
|
|
|
75
|
|
|
$this->activate_uri = $this->ci->config->item('DX_activate_uri'); |
|
|
|
|
76
|
|
|
$this->forgot_password_uri = $this->ci->config->item('DX_forgot_password_uri'); |
|
|
|
|
77
|
|
|
$this->reset_password_uri = $this->ci->config->item('DX_reset_password_uri'); |
|
|
|
|
78
|
|
|
$this->change_password_uri = $this->ci->config->item('DX_change_password_uri'); |
|
|
|
|
79
|
|
|
$this->cancel_account_uri = $this->ci->config->item('DX_cancel_account_uri'); |
|
|
|
|
80
|
|
|
|
81
|
|
|
// Forms view |
82
|
|
|
$this->login_view = $this->ci->config->item('DX_login_view'); |
|
|
|
|
83
|
|
|
$this->register_view = $this->ci->config->item('DX_register_view'); |
|
|
|
|
84
|
|
|
$this->forgot_password_view = $this->ci->config->item('DX_forgot_password_view'); |
|
|
|
|
85
|
|
|
$this->change_password_view = $this->ci->config->item('DX_change_password_view'); |
|
|
|
|
86
|
|
|
$this->cancel_account_view = $this->ci->config->item('DX_cancel_account_view'); |
|
|
|
|
87
|
|
|
|
88
|
|
|
// Pages view |
89
|
|
|
$this->deny_view = $this->ci->config->item('DX_deny_view'); |
|
|
|
|
90
|
|
|
$this->banned_view = $this->ci->config->item('DX_banned_view'); |
|
|
|
|
91
|
|
|
$this->logged_in_view = $this->ci->config->item('DX_logged_in_view'); |
|
|
|
|
92
|
|
|
$this->logout_view = $this->ci->config->item('DX_logout_view'); |
|
|
|
|
93
|
|
|
|
94
|
|
|
$this->register_success_view = $this->ci->config->item('DX_register_success_view'); |
|
|
|
|
95
|
|
|
$this->activate_success_view = $this->ci->config->item('DX_activate_success_view'); |
|
|
|
|
96
|
|
|
$this->forgot_password_success_view = $this->ci->config->item('DX_forgot_password_success_view'); |
|
|
|
|
97
|
|
|
$this->reset_password_success_view = $this->ci->config->item('DX_reset_password_success_view'); |
|
|
|
|
98
|
|
|
$this->change_password_success_view = $this->ci->config->item('DX_change_password_success_view'); |
|
|
|
|
99
|
|
|
|
100
|
|
|
$this->register_disabled_view = $this->ci->config->item('DX_register_disabled_view'); |
|
|
|
|
101
|
|
|
$this->activate_failed_view = $this->ci->config->item('DX_activate_failed_view'); |
|
|
|
|
102
|
|
|
$this->reset_password_failed_view = $this->ci->config->item('DX_reset_password_failed_view'); |
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function _gen_pass($len = 8) { |
106
|
|
|
// No Zero (for user clarity); |
107
|
|
|
$pool = '123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
108
|
|
|
|
109
|
|
|
$str = ''; |
110
|
|
View Code Duplication |
for ($i = 0; $i < $len; $i++) { |
111
|
|
|
$str .= substr($pool, mt_rand(0, strlen($pool) - 1), 1); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return $str; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/* |
118
|
|
|
* Function: _encode |
119
|
|
|
* Modified for DX_Auth |
120
|
|
|
* Original Author: FreakAuth_light 1.1 |
121
|
|
|
*/ |
122
|
|
|
|
123
|
|
|
public function _encode($password) { |
124
|
|
|
$majorsalt = ''; |
125
|
|
|
|
126
|
|
|
// if you set your encryption key let's use it |
127
|
|
|
if ($this->ci->config->item('encryption_key') != '') { |
128
|
|
|
// concatenates the encryption key and the password |
129
|
|
|
$_password = $this->ci->config->item('encryption_key') . $password; |
130
|
|
|
} else { |
131
|
|
|
$_password = $password; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
// if PHP5 |
135
|
|
|
if (function_exists('str_split')) { |
136
|
|
|
$_pass = str_split($_password); |
137
|
|
|
} // if PHP4 |
138
|
|
|
else { |
139
|
|
|
$_pass = []; |
140
|
|
|
if (is_string($_password)) { |
141
|
|
|
for ($i = 0; $i < strlen($_password); $i++) { |
142
|
|
|
array_push($_pass, $_password[$i]); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
// encrypts every single letter of the password |
148
|
|
|
foreach ($_pass as $_hashpass) { |
149
|
|
|
$majorsalt .= md5($_hashpass); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
// encrypts the string combinations of every single encrypted letter |
153
|
|
|
// and finally returns the encrypted password |
154
|
|
|
return md5($majorsalt); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
public function _array_in_array($needle, $haystack) { |
158
|
|
|
//Make sure $needle is an array for foreach |
159
|
|
|
if (!is_array($needle)) { |
160
|
|
|
$needle = [$needle]; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
//For each value in $needle, return TRUE if in $haystack |
164
|
|
|
foreach ($needle as $pin) { |
165
|
|
|
if (in_array($pin, $haystack)) { |
166
|
|
|
return TRUE; |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
//Return FALSE if none of the values from $needle are found in $haystack |
170
|
|
|
return FALSE; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
public function _email($to, $from, $subject, $message) { |
174
|
|
|
$this->ci->load->library('Email'); |
175
|
|
|
$email = $this->ci->email; |
176
|
|
|
|
177
|
|
|
$email->from($from); |
178
|
|
|
$email->to($to); |
179
|
|
|
$email->subject($subject); |
180
|
|
|
$email->message($message); |
181
|
|
|
|
182
|
|
|
return $email->send(); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
// Set last ip and last login function when user login |
186
|
|
|
|
187
|
|
|
public function _set_last_ip_and_last_login($user_id) { |
188
|
|
|
$data = []; |
189
|
|
|
|
190
|
|
|
if ($this->ci->config->item('DX_login_record_ip')) { |
191
|
|
|
$data['last_ip'] = $this->ci->input->ip_address(); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
if ($this->ci->config->item('DX_login_record_time')) { |
195
|
|
|
$data['last_login'] = date('Y-m-d H:i:s', time()); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
if (!empty($data)) { |
199
|
|
|
// Load model |
200
|
|
|
$this->ci->load->model('dx_auth/users', 'users'); |
201
|
|
|
// Update record |
202
|
|
|
$this->ci->users->set_user($user_id, $data); |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
// Increase login attempt |
207
|
|
|
|
208
|
|
View Code Duplication |
public function _increase_login_attempt() { |
209
|
|
|
if ($this->ci->config->item('DX_count_login_attempts') AND ! $this->is_max_login_attempts_exceeded()) { |
210
|
|
|
// Load model |
211
|
|
|
$this->ci->load->model('dx_auth/login_attempts', 'login_attempts'); |
212
|
|
|
// Increase login attempts for current IP |
213
|
|
|
$this->ci->login_attempts->increase_attempt($this->ci->input->ip_address()); |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
// Clear login attempts |
218
|
|
|
|
219
|
|
View Code Duplication |
public function _clear_login_attempts() { |
220
|
|
|
if ($this->ci->config->item('DX_count_login_attempts')) { |
221
|
|
|
// Load model |
222
|
|
|
$this->ci->load->model('dx_auth/login_attempts', 'login_attempts'); |
223
|
|
|
// Clear login attempts for current IP |
224
|
|
|
$this->ci->login_attempts->clear_attempts($this->ci->input->ip_address()); |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
// Get role data from database by id, used in _set_session() function |
229
|
|
|
// $parent_roles_id, $parent_roles_name is an array. |
230
|
|
|
|
231
|
|
|
public function _get_role_data($role_id) { |
232
|
|
|
// Load models |
233
|
|
|
$this->ci->load->model('dx_auth/roles', 'roles'); |
234
|
|
|
$this->ci->load->model('dx_auth/permissions', 'permissions'); |
235
|
|
|
|
236
|
|
|
// Clear return value |
237
|
|
|
$role_name = ''; |
|
|
|
|
238
|
|
|
$parent_roles_id = []; |
239
|
|
|
$parent_roles_name = []; |
240
|
|
|
$permission = []; |
|
|
|
|
241
|
|
|
$parent_permissions = []; |
242
|
|
|
|
243
|
|
|
/* Get role_name, parent_roles_id and parent_roles_name */ |
244
|
|
|
|
245
|
|
|
// Get role query from role id |
246
|
|
|
$query = $this->ci->roles->get_role_by_id($role_id); |
247
|
|
|
|
248
|
|
|
// Check if role exist |
249
|
|
|
if ($query->num_rows() > 0) { |
250
|
|
|
// Get row |
251
|
|
|
$role = $query->row(); |
252
|
|
|
|
253
|
|
|
// Get role name |
254
|
|
|
$role_name = $role->name; |
|
|
|
|
255
|
|
|
|
256
|
|
|
/* |
257
|
|
|
Code below will search if user role_id have parent_id > 0 (which mean role_id have parent role_id) |
258
|
|
|
and do it recursively until parent_id reach 0 (no parent) or parent_id not found. |
259
|
|
|
|
260
|
|
|
If anyone have better approach than this code, please let me know. |
261
|
|
|
*/ |
262
|
|
|
|
263
|
|
|
// Check if role has parent id |
264
|
|
|
|
265
|
|
|
if (isset($role->parent_id) && $role->parent_id > 0) { |
266
|
|
|
// Add to result array |
267
|
|
|
$parent_roles_id[] = $role->parent_id; |
268
|
|
|
|
269
|
|
|
// Set variable used in looping |
270
|
|
|
$finished = FALSE; |
271
|
|
|
$parent_id = $role->parent_id; |
272
|
|
|
|
273
|
|
|
// Get all parent id |
274
|
|
|
while ($finished == FALSE) { |
275
|
|
|
$i_query = $this->ci->roles->get_role_by_id($parent_id); |
276
|
|
|
|
277
|
|
|
// If role exist |
278
|
|
|
if ($i_query->num_rows() > 0) { |
279
|
|
|
// Get row |
280
|
|
|
$i_role = $i_query->row(); |
281
|
|
|
|
282
|
|
|
// Check if role doesn't have parent |
283
|
|
|
if ($i_role->parent_id == 0) { |
284
|
|
|
// Get latest parent name |
285
|
|
|
$parent_roles_name[] = $i_role->name; |
286
|
|
|
// Stop looping |
287
|
|
|
$finished = TRUE; |
288
|
|
|
} else { |
289
|
|
|
// Change parent id for next looping |
290
|
|
|
$parent_id = $i_role->parent_id; |
291
|
|
|
|
292
|
|
|
// Add to result array |
293
|
|
|
$parent_roles_id[] = $parent_id; |
294
|
|
|
$parent_roles_name[] = $i_role->name; |
295
|
|
|
} |
296
|
|
|
} else { |
297
|
|
|
// Remove latest parent_roles_id since parent_id not found |
298
|
|
|
array_pop($parent_roles_id); |
299
|
|
|
// Stop looping |
300
|
|
|
$finished = TRUE; |
301
|
|
|
} |
302
|
|
|
} |
303
|
|
|
} |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/* End of Get role_name, parent_roles_id and parent_roles_name */ |
307
|
|
|
|
308
|
|
|
/* Get user and parents permission */ |
309
|
|
|
|
310
|
|
|
// Get user role permission |
311
|
|
|
$permission = $this->ci->permissions->get_permission_data($role_id); |
312
|
|
|
|
313
|
|
|
// Get user role parent permissions |
314
|
|
|
if (!empty($parent_roles_id)) { |
315
|
|
|
$parent_permissions = $this->ci->permissions->get_permissions_data($parent_roles_id); |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
/* End of Get user and parents permission */ |
319
|
|
|
if ($role_id) { |
320
|
|
|
$data['role_name'] = Permitions::checkControlPanelAccess($role_id); |
|
|
|
|
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
// Set return value |
324
|
|
|
//$data['role_name'] = $role_name; |
325
|
|
|
$data['parent_roles_id'] = $parent_roles_id; |
326
|
|
|
$data['parent_roles_name'] = $parent_roles_name; |
327
|
|
|
$data['permission'] = $permission; |
328
|
|
|
$data['parent_permissions'] = $parent_permissions; |
329
|
|
|
|
330
|
|
|
return $data; |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
/* Autologin related function */ |
334
|
|
|
|
335
|
|
|
public function _create_autologin($user_id) { |
336
|
|
|
$result = FALSE; |
337
|
|
|
|
338
|
|
|
// User wants to be remembered |
339
|
|
|
$user = [ |
340
|
|
|
'key_id' => substr(md5(uniqid(rand() . $this->ci->input->cookie($this->ci->config->item('sess_cookie_name')))), 0, 16), |
341
|
|
|
'user_id' => $user_id, |
342
|
|
|
]; |
343
|
|
|
|
344
|
|
|
// Load Models |
345
|
|
|
$this->ci->load->model('dx_auth/user_autologin', 'user_autologin'); |
346
|
|
|
|
347
|
|
|
// Prune keys |
348
|
|
|
$this->ci->user_autologin->prune_keys($user['user_id']); |
349
|
|
|
|
350
|
|
|
if ($this->ci->user_autologin->store_key($user['key_id'], $user['user_id'])) { |
351
|
|
|
// Set Users AutoLogin cookie |
352
|
|
|
$this->_auto_cookie($user); |
353
|
|
|
|
354
|
|
|
$result = TRUE; |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
return $result; |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
public function autologin() { |
361
|
|
|
$result = FALSE; |
362
|
|
|
|
363
|
|
|
//if ($auto = $this->ci->input->cookie($this->ci->config->item('DX_autologin_cookie_name')) AND ! $this->ci->session->userdata('DX_logged_in')) |
364
|
|
|
if ($auto = $this->ci->input->cookie($this->ci->config->item('DX_autologin_cookie_name'))) { |
365
|
|
|
// Extract data |
366
|
|
|
$auto = unserialize($auto); |
367
|
|
|
|
368
|
|
|
if (isset($auto['key_id']) AND $auto['key_id'] AND $auto['user_id']) { |
369
|
|
|
// Load Models |
370
|
|
|
$this->ci->load->model('dx_auth/user_autologin', 'user_autologin'); |
371
|
|
|
|
372
|
|
|
// Get key |
373
|
|
|
$query = $this->ci->user_autologin->get_key($auto['key_id'], $auto['user_id']); |
374
|
|
|
|
375
|
|
|
if ($result = $query->row()) { |
376
|
|
|
// User verified, log them in |
377
|
|
|
$this->_set_session($result); |
378
|
|
|
// Renew users cookie to prevent it from expiring |
379
|
|
|
$this->_auto_cookie($auto); |
380
|
|
|
|
381
|
|
|
// Set last ip and last login |
382
|
|
|
$this->_set_last_ip_and_last_login($auto['user_id']); |
383
|
|
|
|
384
|
|
|
$result = TRUE; |
385
|
|
|
} |
386
|
|
|
} |
387
|
|
|
} |
388
|
|
|
|
389
|
|
|
return $result; |
390
|
|
|
} |
391
|
|
|
|
392
|
|
|
public function _delete_autologin() { |
393
|
|
|
if ($auto = $this->ci->input->cookie($this->ci->config->item('DX_autologin_cookie_name'))) { |
394
|
|
|
// Load Cookie Helper |
395
|
|
|
$this->ci->load->helper('cookie'); |
396
|
|
|
|
397
|
|
|
// Load Models |
398
|
|
|
$this->ci->load->model('dx_auth/user_autologin', 'user_autologin'); |
399
|
|
|
|
400
|
|
|
// Extract data |
401
|
|
|
$auto = unserialize($auto); |
402
|
|
|
|
403
|
|
|
// Delete db entry |
404
|
|
|
$this->ci->user_autologin->delete_key($auto['key_id'], $auto['user_id']); |
405
|
|
|
|
406
|
|
|
// Make cookie expired |
407
|
|
|
set_cookie($this->ci->config->item('DX_autologin_cookie_name'), '', -1); |
408
|
|
|
} |
409
|
|
|
} |
410
|
|
|
|
411
|
|
|
public function _set_session($data) { |
412
|
|
|
// Get role data |
413
|
|
|
$role_data = $this->_get_role_data($data->role_id); |
414
|
|
|
|
415
|
|
|
// Set session data array |
416
|
|
|
$user = [ |
417
|
|
|
'DX_user_id' => $data->id, |
418
|
|
|
'DX_username' => $data->username, |
419
|
|
|
'DX_role_id' => $data->role_id, |
420
|
|
|
'DX_role_name' => $role_data['role_name'], |
421
|
|
|
'DX_parent_roles_id' => $role_data['parent_roles_id'], // Array of parent role_id |
422
|
|
|
'DX_parent_roles_name' => $role_data['parent_roles_name'], // Array of parent role_name |
423
|
|
|
'DX_permission' => $role_data['permission'], |
424
|
|
|
'DX_parent_permissions' => $role_data['parent_permissions'], |
425
|
|
|
'DX_logged_in' => TRUE, |
426
|
|
|
]; |
427
|
|
|
|
428
|
|
|
$this->ci->session->set_userdata($user); |
429
|
|
|
} |
430
|
|
|
|
431
|
|
|
public function _auto_cookie($data) { |
432
|
|
|
// Load Cookie Helper |
433
|
|
|
$this->ci->load->helper('cookie'); |
434
|
|
|
|
435
|
|
|
$cookie = [ |
436
|
|
|
'name' => $this->ci->config->item('DX_autologin_cookie_name'), |
437
|
|
|
'value' => serialize($data), |
438
|
|
|
'expire' => $this->ci->config->item('DX_autologin_cookie_life'), |
439
|
|
|
]; |
440
|
|
|
|
441
|
|
|
set_cookie($cookie); |
442
|
|
|
} |
443
|
|
|
|
444
|
|
|
/* End of Auto login related function */ |
445
|
|
|
|
446
|
|
|
/* Helper function */ |
447
|
|
|
|
448
|
|
|
public function check_uri_permissions() { |
449
|
|
|
// First check if user already logged in or not |
450
|
|
|
if ($this->is_logged_in()) { |
451
|
|
|
// If user is not admin |
452
|
|
|
if (!$this->is_admin()) { |
453
|
|
|
// Get variable from current URI |
454
|
|
|
$controller = '/' . $this->ci->uri->rsegment(1) . '/'; |
455
|
|
|
if ($this->ci->uri->rsegment(2) != '') { |
456
|
|
|
$action = $controller . $this->ci->uri->rsegment(2) . '/'; |
457
|
|
|
} else { |
458
|
|
|
$action = $controller . 'index/'; |
459
|
|
|
} |
460
|
|
|
|
461
|
|
|
// Get URI permissions from role and all parents |
462
|
|
|
// Note: URI permissions is saved in 'uri' key |
463
|
|
|
$roles_allowed_uris = $this->get_permissions_value('uri'); |
464
|
|
|
|
465
|
|
|
// Variable to determine if URI found |
466
|
|
|
$found = FALSE; |
467
|
|
|
// Loop each roles URI permissions |
468
|
|
|
foreach ($roles_allowed_uris as $allowed_uris) { |
469
|
|
|
// Check if user allowed to access URI |
470
|
|
|
if ($this->_array_in_array(['/', $controller, $action], $allowed_uris)) { |
471
|
|
|
$found = TRUE; |
472
|
|
|
// Stop loop |
473
|
|
|
break; |
474
|
|
|
} |
475
|
|
|
} |
476
|
|
|
|
477
|
|
|
// Trigger event |
478
|
|
|
$this->ci->dx_auth_event->checked_uri_permissions($this->get_user_id(), $found); |
479
|
|
|
|
480
|
|
|
if (!$found) { |
481
|
|
|
// User didn't have previlege to access current URI, so we show user 403 forbidden access |
482
|
|
|
$this->deny_access(); |
483
|
|
|
} |
484
|
|
|
} |
485
|
|
|
} else { |
486
|
|
|
// User haven't logged in, so just redirect user to login page |
487
|
|
|
$this->deny_access('login'); |
488
|
|
|
} |
489
|
|
|
} |
490
|
|
|
|
491
|
|
|
/* |
492
|
|
|
Obsolete in 1.0.2 and above, do not use this function. |
493
|
|
|
Use check_uri_permisisons() instead |
494
|
|
|
|
495
|
|
|
public function check_role_uri() |
496
|
|
|
{ |
497
|
|
|
} |
498
|
|
|
*/ |
499
|
|
|
|
500
|
|
|
/* |
501
|
|
|
Get permission value from specified key. |
502
|
|
|
Call this function only when user is logged in already. |
503
|
|
|
$key is permission array key (Note: permissions is saved as array in table). |
504
|
|
|
If $check_parent is TRUE means if permission value not found in user role, it will try to get permission value from parent role. |
505
|
|
|
Returning value if permission found, otherwise returning NULL |
506
|
|
|
*/ |
507
|
|
|
|
508
|
|
|
public function get_permission_value($key, $check_parent = TRUE) { |
509
|
|
|
// Default return value |
510
|
|
|
$result = NULL; |
511
|
|
|
|
512
|
|
|
// Get current user permission |
513
|
|
|
$permission = $this->ci->session->userdata('DX_permission'); |
514
|
|
|
|
515
|
|
|
// Check if key is in user permission array |
516
|
|
|
if (array_key_exists($key, $permission)) { |
517
|
|
|
$result = $permission[$key]; |
518
|
|
|
} // Key not found |
519
|
|
|
else { |
520
|
|
|
if ($check_parent) { |
521
|
|
|
// Get current user parent permissions |
522
|
|
|
$parent_permissions = $this->ci->session->userdata('DX_parent_permissions'); |
523
|
|
|
|
524
|
|
|
// Check parent permissions array |
525
|
|
|
foreach ($parent_permissions as $permission) { |
526
|
|
|
if (array_key_exists($key, $permission)) { |
527
|
|
|
$result = $permission[$key]; |
528
|
|
|
break; |
529
|
|
|
} |
530
|
|
|
} |
531
|
|
|
} |
532
|
|
|
} |
533
|
|
|
|
534
|
|
|
// Trigger event |
535
|
|
|
$this->ci->dx_auth_event->got_permission_value($this->get_user_id(), $key); |
536
|
|
|
|
537
|
|
|
return $result; |
538
|
|
|
} |
539
|
|
|
|
540
|
|
|
/* |
541
|
|
|
Get permissions value from specified key. |
542
|
|
|
Call this function only when user is logged in already. |
543
|
|
|
This will get user permission, and it's parents permissions. |
544
|
|
|
|
545
|
|
|
$array_key = 'default'. Array ordered using 0, 1, 2 as array key. |
546
|
|
|
$array_key = 'role_id'. Array ordered using role_id as array key. |
547
|
|
|
$array_key = 'role_name'. Array ordered using role_name as array key. |
548
|
|
|
|
549
|
|
|
Returning array of value if permission found, otherwise returning NULL. |
550
|
|
|
*/ |
551
|
|
|
|
552
|
|
|
/** |
553
|
|
|
* @param string $key |
554
|
|
|
*/ |
555
|
|
|
public function get_permissions_value($key, $array_key = 'default') { |
556
|
|
|
$result = []; |
557
|
|
|
|
558
|
|
|
$role_id = $this->ci->session->userdata('DX_role_id'); |
559
|
|
|
$role_name = $this->ci->session->userdata('DX_role_name'); |
560
|
|
|
|
561
|
|
|
$parent_roles_id = $this->ci->session->userdata('DX_parent_roles_id'); |
562
|
|
|
$parent_roles_name = $this->ci->session->userdata('DX_parent_roles_name'); |
563
|
|
|
|
564
|
|
|
// Get current user permission |
565
|
|
|
$value = $this->get_permission_value($key, FALSE); |
566
|
|
|
|
567
|
|
View Code Duplication |
if ($array_key == 'role_id') { |
568
|
|
|
$result[$role_id] = $value; |
569
|
|
|
} elseif ($array_key == 'role_name') { |
570
|
|
|
$result[$role_name] = $value; |
571
|
|
|
} else { |
572
|
|
|
array_push($result, $value); |
573
|
|
|
} |
574
|
|
|
|
575
|
|
|
// Get current user parent permissions |
576
|
|
|
$parent_permissions = $this->ci->session->userdata('DX_parent_permissions'); |
577
|
|
|
|
578
|
|
|
$i = 0; |
579
|
|
|
foreach ($parent_permissions as $permission) { |
580
|
|
|
if (array_key_exists($key, $permission)) { |
581
|
|
|
$value = $permission[$key]; |
582
|
|
|
} |
583
|
|
|
|
584
|
|
View Code Duplication |
if ($array_key == 'role_id') { |
585
|
|
|
// It's safe to use $parents_roles_id[$i] because array order is same with permission array |
586
|
|
|
$result[$parent_roles_id[$i]] = $value; |
587
|
|
|
} elseif ($array_key == 'role_name') { |
588
|
|
|
// It's safe to use $parents_roles_name[$i] because array order is same with permission array |
589
|
|
|
$result[$parent_roles_name[$i]] = $value; |
590
|
|
|
} else { |
591
|
|
|
array_push($result, $value); |
592
|
|
|
} |
593
|
|
|
|
594
|
|
|
$i++; |
595
|
|
|
} |
596
|
|
|
|
597
|
|
|
// Trigger event |
598
|
|
|
$this->ci->dx_auth_event->got_permissions_value($this->get_user_id(), $key); |
599
|
|
|
|
600
|
|
|
return $result; |
601
|
|
|
} |
602
|
|
|
|
603
|
|
|
public function deny_access($uri = 'deny') { |
604
|
|
|
$this->ci->load->helper('url'); |
605
|
|
|
|
606
|
|
|
if ($uri == 'login') { |
607
|
|
|
redirect($this->ci->config->item('DX_login_uri'), 'location'); |
608
|
|
|
} else if ($uri == 'banned') { |
609
|
|
|
redirect($this->ci->config->item('DX_banned_uri'), 'location'); |
610
|
|
|
} else { |
611
|
|
|
redirect($this->ci->config->item('DX_deny_uri'), 'location'); |
612
|
|
|
} |
613
|
|
|
exit; |
614
|
|
|
} |
615
|
|
|
|
616
|
|
|
// Get user id |
617
|
|
|
|
618
|
|
|
public function get_user_id() { |
619
|
|
|
return $this->ci->session->userdata('DX_user_id'); |
620
|
|
|
} |
621
|
|
|
|
622
|
|
|
// Get username string |
623
|
|
|
|
624
|
|
|
public function get_username() { |
625
|
|
|
return $this->ci->session->userdata('DX_username'); |
626
|
|
|
} |
627
|
|
|
|
628
|
|
|
// Get useremail string |
629
|
|
|
|
630
|
|
|
public function get_user_email() { |
631
|
|
|
$user = $this->ci->db |
632
|
|
|
->where('id', $this->get_user_id()) |
633
|
|
|
->get('users'); |
634
|
|
|
if ($user) { |
635
|
|
|
$user = $user->row_array(); |
636
|
|
|
return $user['email']; |
637
|
|
|
} else { |
638
|
|
|
return ''; |
639
|
|
|
} |
640
|
|
|
} |
641
|
|
|
|
642
|
|
|
// Get user role id |
643
|
|
|
|
644
|
|
|
public function get_role_id() { |
645
|
|
|
return $this->ci->session->userdata('DX_role_id'); |
646
|
|
|
} |
647
|
|
|
|
648
|
|
|
// Get user role name |
649
|
|
|
|
650
|
|
|
public function get_role_name() { |
651
|
|
|
return $this->ci->session->userdata('DX_role_name'); |
652
|
|
|
} |
653
|
|
|
|
654
|
|
|
// Check is user is has admin privilege |
655
|
|
|
|
656
|
|
|
public function is_admin() { |
657
|
|
|
if (PHP_SAPI == 'cli') { |
658
|
|
|
return true; |
659
|
|
|
} |
660
|
|
|
return strtolower($this->ci->session->userdata('DX_role_name')) == 'admin'; |
661
|
|
|
} |
662
|
|
|
|
663
|
|
|
// Check if user has $roles privilege |
664
|
|
|
// If $use_role_name TRUE then $roles is name such as 'admin', 'editor', 'etc' |
665
|
|
|
// else $roles is role_id such as 0, 1, 2 |
666
|
|
|
// If $check_parent is TRUE means if roles not found in user role, it will check if user role parent has that roles |
667
|
|
|
|
668
|
|
|
public function is_role($roles = [], $use_role_name = TRUE, $check_parent = TRUE) { |
669
|
|
|
// Default return value |
670
|
|
|
$result = FALSE; |
671
|
|
|
|
672
|
|
|
// Build checking array |
673
|
|
|
$check_array = []; |
674
|
|
|
|
675
|
|
|
if ($check_parent) { |
676
|
|
|
// Add parent roles into check array |
677
|
|
|
if ($use_role_name) { |
678
|
|
|
$check_array = $this->ci->session->userdata('DX_parent_roles_name'); |
679
|
|
|
} else { |
680
|
|
|
$check_array = $this->ci->session->userdata('DX_parent_roles_id'); |
681
|
|
|
} |
682
|
|
|
} |
683
|
|
|
|
684
|
|
|
// Add current role into check array |
685
|
|
|
if ($use_role_name) { |
686
|
|
|
array_push($check_array, $this->ci->session->userdata('DX_role_name')); |
687
|
|
|
} else { |
688
|
|
|
array_push($check_array, $this->ci->session->userdata('DX_role_id')); |
689
|
|
|
} |
690
|
|
|
|
691
|
|
|
// If $roles not array then we add it into an array |
692
|
|
|
if (!is_array($roles)) { |
693
|
|
|
$roles = [$roles]; |
694
|
|
|
} |
695
|
|
|
|
696
|
|
|
if ($use_role_name) { |
697
|
|
|
// Convert check array into lowercase since we want case insensitive checking |
698
|
|
|
for ($i = 0; $i < count($check_array); $i++) { |
|
|
|
|
699
|
|
|
$check_array[$i] = strtolower($check_array[$i]); |
700
|
|
|
} |
701
|
|
|
|
702
|
|
|
// Convert roles into lowercase since we want insensitive checking |
703
|
|
|
for ($i = 0; $i < count($roles); $i++) { |
|
|
|
|
704
|
|
|
$roles[$i] = strtolower($roles[$i]); |
705
|
|
|
} |
706
|
|
|
} |
707
|
|
|
|
708
|
|
|
// Check if roles exist in check_array |
709
|
|
|
if ($this->_array_in_array($roles, $check_array)) { |
710
|
|
|
$result = TRUE; |
711
|
|
|
} |
712
|
|
|
|
713
|
|
|
return $result; |
714
|
|
|
} |
715
|
|
|
|
716
|
|
|
// Check if user is logged in |
717
|
|
|
|
718
|
|
|
public function is_logged_in() { |
719
|
|
|
return $this->ci->session->userdata('DX_logged_in'); |
720
|
|
|
} |
721
|
|
|
|
722
|
|
|
// Check if user is a banned user, call this only after calling login() and returning FALSE |
723
|
|
|
|
724
|
|
|
public function is_banned() { |
725
|
|
|
return $this->_banned; |
726
|
|
|
} |
727
|
|
|
|
728
|
|
|
// Get ban reason, call this only after calling login() and returning FALSE |
729
|
|
|
|
730
|
|
|
public function get_ban_reason() { |
731
|
|
|
return $this->_ban_reason; |
732
|
|
|
} |
733
|
|
|
|
734
|
|
|
// Check if username is available to use, by making sure there is no same username in the database |
735
|
|
|
|
736
|
|
View Code Duplication |
public function is_username_available($username) { |
737
|
|
|
// Load Models |
738
|
|
|
$this->ci->load->model('dx_auth/users', 'users'); |
739
|
|
|
$this->ci->load->model('dx_auth/user_temp', 'user_temp'); |
740
|
|
|
|
741
|
|
|
$users = $this->ci->users->check_username($username); |
742
|
|
|
$temp = $this->ci->user_temp->check_username($username); |
743
|
|
|
|
744
|
|
|
return $users->num_rows() + $temp->num_rows() == 0; |
745
|
|
|
} |
746
|
|
|
|
747
|
|
|
// Check if email is available to use, by making sure there is no same email in the database |
748
|
|
|
|
749
|
|
View Code Duplication |
public function is_email_available($email) { |
750
|
|
|
// Load Models |
751
|
|
|
$this->ci->load->model('dx_auth/users', 'users'); |
752
|
|
|
$this->ci->load->model('dx_auth/user_temp', 'user_temp'); |
753
|
|
|
|
754
|
|
|
$users = $this->ci->users->check_email($email); |
755
|
|
|
$temp = $this->ci->user_temp->check_email($email); |
756
|
|
|
|
757
|
|
|
return $users->num_rows() + $temp->num_rows() == 0; |
758
|
|
|
} |
759
|
|
|
|
760
|
|
|
// Check if login attempts bigger than max login attempts specified in config |
761
|
|
|
|
762
|
|
|
public function is_max_login_attempts_exceeded() { |
763
|
|
|
$this->ci->load->model('dx_auth/login_attempts', 'login_attempts'); |
764
|
|
|
|
765
|
|
|
return ($this->ci->login_attempts->check_attempts($this->ci->input->ip_address())->num_rows() >= $this->ci->config->item('DX_max_login_attempts')); |
766
|
|
|
} |
767
|
|
|
|
768
|
|
|
public function get_auth_error() { |
769
|
|
|
return $this->_auth_error; |
770
|
|
|
} |
771
|
|
|
|
772
|
|
|
/* End of Helper function */ |
773
|
|
|
|
774
|
|
|
/* Main function */ |
775
|
|
|
|
776
|
|
|
// $login is username or email or both depending on setting in config file |
777
|
|
|
|
778
|
|
|
public function login($login, $password, $remember = TRUE) { |
779
|
|
|
// Load Models |
780
|
|
|
$this->ci->load->model('dx_auth/users', 'users'); |
781
|
|
|
$this->ci->load->model('dx_auth/user_temp', 'user_temp'); |
782
|
|
|
$this->ci->load->model('dx_auth/login_attempts', 'login_attempts'); |
783
|
|
|
|
784
|
|
|
// Default return value |
785
|
|
|
$result = FALSE; |
786
|
|
|
|
787
|
|
|
if (!empty($login) AND ! empty($password)) { |
788
|
|
|
// Get which function to use based on config |
789
|
|
|
if ($this->ci->config->item('DX_login_using_username') AND $this->ci->config->item('DX_login_using_email')) { |
790
|
|
|
$get_user_function = 'get_login'; |
791
|
|
|
} else if ($this->ci->config->item('DX_login_using_email')) { |
792
|
|
|
$get_user_function = 'get_user_by_email'; |
793
|
|
|
} else { |
794
|
|
|
$get_user_function = 'get_user_by_username'; |
795
|
|
|
} |
796
|
|
|
|
797
|
|
|
// Get user query |
798
|
|
|
if ($query = $this->ci->users->$get_user_function($login) AND $query->num_rows() == 1) { |
799
|
|
|
// Get user record |
800
|
|
|
$row = $query->row(); |
801
|
|
|
|
802
|
|
|
// Check if user is banned or not |
803
|
|
|
if ($row->banned > 0) { |
804
|
|
|
// Set user as banned |
805
|
|
|
$this->_banned = TRUE; |
806
|
|
|
// Set ban reason |
807
|
|
|
$this->_ban_reason = $row->ban_reason; |
808
|
|
|
} else { |
809
|
|
|
// If it's not a banned user then try to login |
810
|
|
|
$password = $this->_encode($password); |
811
|
|
|
$stored_hash = $row->password; |
812
|
|
|
|
813
|
|
|
// Is password matched with hash in database ? |
814
|
|
|
if (crypt($password, $stored_hash) === $stored_hash) { |
815
|
|
|
// Log in user |
816
|
|
|
$this->_set_session($row); |
817
|
|
|
|
818
|
|
|
if ($row->newpass) { |
819
|
|
|
// Clear any Reset Passwords |
820
|
|
|
$this->ci->users->clear_newpass($row->id); |
821
|
|
|
} |
822
|
|
|
|
823
|
|
|
if ($remember) { |
824
|
|
|
// Create auto login if user want to be remembered |
825
|
|
|
$this->_create_autologin($row->id); |
826
|
|
|
} |
827
|
|
|
|
828
|
|
|
// Set last ip and last login |
829
|
|
|
$this->_set_last_ip_and_last_login($row->id); |
830
|
|
|
// Clear login attempts |
831
|
|
|
$this->_clear_login_attempts(); |
832
|
|
|
|
833
|
|
|
// Trigger event |
834
|
|
|
$this->ci->dx_auth_event->user_logged_in($row->id); |
835
|
|
|
|
836
|
|
|
// Set return value |
837
|
|
|
$result = TRUE; |
838
|
|
|
} else { |
839
|
|
|
// Increase login attempts |
840
|
|
|
$this->_increase_login_attempt(); |
841
|
|
|
// Set error message |
842
|
|
|
$this->_auth_error = lang('auth login incorrect password'); |
843
|
|
|
} |
844
|
|
|
} |
845
|
|
|
} elseif ($query = $this->ci->user_temp->$get_user_function($login) AND $query->num_rows() == 1) { |
846
|
|
|
// Check if login is still not activated |
847
|
|
|
// Set error message |
848
|
|
|
$this->_auth_error = lang('auth not activated'); |
849
|
|
|
} else { |
850
|
|
|
// Increase login attempts |
851
|
|
|
$this->_increase_login_attempt(); |
852
|
|
|
// Set error message |
853
|
|
|
$this->_auth_error = lang('auth login username not exist'); |
854
|
|
|
} |
855
|
|
|
} |
856
|
|
|
|
857
|
|
|
return $result; |
858
|
|
|
} |
859
|
|
|
|
860
|
|
|
public function logout() { |
861
|
|
|
// Trigger event |
862
|
|
|
$this->ci->dx_auth_event->user_logging_out($this->ci->session->userdata('DX_user_id')); |
863
|
|
|
|
864
|
|
|
// Delete auto login |
865
|
|
|
if ($this->ci->input->cookie($this->ci->config->item('DX_autologin_cookie_name'))) { |
866
|
|
|
$this->_delete_autologin(); |
867
|
|
|
} |
868
|
|
|
|
869
|
|
|
// Destroy session |
870
|
|
|
$this->ci->session->sess_destroy(); |
871
|
|
|
} |
872
|
|
|
|
873
|
|
|
public function register($username, $password, $email, $address = '', $key, $phone = '', $login_user = TRUE) { |
874
|
|
|
|
875
|
|
|
// Load Models |
876
|
|
|
$this->ci->load->model('dx_auth/users', 'users'); |
877
|
|
|
$this->ci->load->model('dx_auth/user_temp', 'user_temp'); |
878
|
|
|
$this->ci->load->helper('url'); |
879
|
|
|
|
880
|
|
|
// Default return value |
881
|
|
|
$result = FALSE; |
882
|
|
|
|
883
|
|
|
$siteSettings = $this->ci->cms_base->get_settings(); |
884
|
|
|
$new_user = [ |
885
|
|
|
'username' => $username, |
886
|
|
|
'password' => crypt($this->_encode($password)), |
887
|
|
|
'address' => $address, |
888
|
|
|
'email' => $email, |
889
|
|
|
'key' => $key, |
890
|
|
|
'phone' => $phone, |
891
|
|
|
'last_ip' => $this->ci->input->ip_address(), |
892
|
|
|
'role_id' => $siteSettings['users_registration_role_id'] ?: 0, |
893
|
|
|
]; |
894
|
|
|
|
895
|
|
|
// Do we need to send email to activate user |
896
|
|
|
|
897
|
|
|
if ($this->ci->config->item('DX_email_activation')) { |
898
|
|
|
// Add activation key to user array |
899
|
|
|
$new_user['activation_key'] = md5(rand() . microtime()); |
900
|
|
|
|
901
|
|
|
// Create temporary user in database which means the user still unactivated. |
902
|
|
|
$insert = $this->ci->user_temp->create_temp($new_user); |
903
|
|
|
|
904
|
|
|
$from = $this->ci->config->item('DX_webmaster_email'); |
|
|
|
|
905
|
|
|
$subject = sprintf(lang('auth activate subject'), $this->ci->config->item('DX_website_name')); |
|
|
|
|
906
|
|
|
|
907
|
|
|
// Activation Link |
908
|
|
|
$new_user['activate_url'] = site_url($this->ci->config->item('DX_activate_uri') . "{$new_user['email']}/{$new_user['activation_key']}"); |
909
|
|
|
// Trigger event and get email content |
910
|
|
|
$this->ci->dx_auth_event->sending_activation_email($new_user, $message); |
|
|
|
|
911
|
|
|
|
912
|
|
|
// Send email with activation link |
913
|
|
|
// $this->_email($email, $from, $subject, $message); |
914
|
|
|
} else { |
915
|
|
|
// Create user |
916
|
|
|
$insert = $this->ci->users->create_user($new_user); |
917
|
|
|
$last_user_id = $this->ci->db->insert_id(); |
918
|
|
|
|
919
|
|
|
foreach ($this->ci->input->post('custom_field') as $k => $custom_fields) { |
920
|
|
|
$this->ci->db->insert( |
921
|
|
|
'custom_fields_data', |
922
|
|
|
[ |
923
|
|
|
'field_id' => $k, |
924
|
|
|
'entity_id' => $last_user_id, |
925
|
|
|
'field_data' => $custom_fields, |
926
|
|
|
'locale' => \MY_Controller::defaultLocale(), |
927
|
|
|
] |
928
|
|
|
); |
929
|
|
|
} |
930
|
|
|
|
931
|
|
|
// Trigger event |
932
|
|
|
$this->ci->dx_auth_event->user_activated($last_user_id); |
933
|
|
|
} |
934
|
|
|
|
935
|
|
|
if ($insert) { |
936
|
|
|
|
937
|
|
|
// Replace password with plain for email |
938
|
|
|
$new_user['password'] = $password; |
939
|
|
|
$new_user['id_user'] = $last_user_id; |
940
|
|
|
|
941
|
|
|
$result = $new_user; |
942
|
|
|
|
943
|
|
|
// Send email based on config |
944
|
|
|
// Check if user need to activate it's account using email |
945
|
|
|
if ($this->ci->config->item('DX_email_activation')) { |
946
|
|
|
// Create email |
947
|
|
|
$from = $this->ci->config->item('DX_webmaster_email'); |
|
|
|
|
948
|
|
|
$subject = sprintf(lang('auth activate subject'), $this->ci->config->item('DX_website_name')); |
|
|
|
|
949
|
|
|
|
950
|
|
|
// Activation Link |
951
|
|
|
$new_user['activate_url'] = site_url(); |
952
|
|
|
|
953
|
|
|
// Trigger event and get email content |
954
|
|
|
$this->ci->dx_auth_event->sending_account_email($new_user, $message); |
955
|
|
|
|
956
|
|
|
// Send email with activation link |
957
|
|
|
// $this->_email($email, $from, $subject, $message); |
958
|
|
|
} else { |
959
|
|
|
// Check if need to email account details |
960
|
|
|
if ($this->ci->config->item('DX_email_account_details')) { |
961
|
|
|
// Create email |
962
|
|
|
$from = $this->ci->config->item('DX_webmaster_email'); |
|
|
|
|
963
|
|
|
$subject = sprintf(lang('auth account subject'), $this->ci->config->item('DX_website_name')); |
|
|
|
|
964
|
|
|
|
965
|
|
|
// Trigger event and get email content |
966
|
|
|
$this->ci->dx_auth_event->sending_account_email($new_user, $message); |
967
|
|
|
|
968
|
|
|
// Send email with account details |
969
|
|
|
// $this->_email($email, $from, $subject, $message); |
970
|
|
|
} |
971
|
|
|
|
972
|
|
|
$user_variables = [ |
973
|
|
|
'user_name' => $username, |
974
|
|
|
'user_password' => $password, |
975
|
|
|
'user_address' => $address, |
976
|
|
|
'user_email' => $email, |
977
|
|
|
'user_phone' => $phone, |
978
|
|
|
]; |
979
|
|
|
|
980
|
|
|
\cmsemail\email::getInstance()->sendEmail($email, 'create_user', $user_variables); |
981
|
|
|
|
982
|
|
|
if ($login_user) { |
983
|
|
|
if ($this->login($email, $password)) { |
984
|
|
|
if (class_exists('ShopCore')) { |
985
|
|
|
ShopCore::app()->SCart->transferCartData(); |
986
|
|
|
} |
987
|
|
|
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') { |
988
|
|
|
redirect('', 'location'); |
989
|
|
|
} |
990
|
|
|
// if ($_SERVER['HTTP_X_REQUESTED_WITH'] != 'XMLHttpRequest') { |
991
|
|
|
} |
992
|
|
|
} |
993
|
|
|
} |
994
|
|
|
} |
995
|
|
|
|
996
|
|
|
return $result; |
997
|
|
|
} |
998
|
|
|
|
999
|
|
|
public function forgot_password($login) { |
1000
|
|
|
// Default return value |
1001
|
|
|
$result = FALSE; |
1002
|
|
|
|
1003
|
|
|
if ($login) { |
1004
|
|
|
// Load Model |
1005
|
|
|
$this->ci->load->model('dx_auth/users', 'users'); |
1006
|
|
|
// Load Helper |
1007
|
|
|
$this->ci->load->helper('url'); |
1008
|
|
|
|
1009
|
|
|
// Get login and check if it's exist |
1010
|
|
|
if ($query = $this->ci->users->get_login($login) AND $query->num_rows() == 1) { |
1011
|
|
|
// Get User data |
1012
|
|
|
$row = $query->row(); |
1013
|
|
|
|
1014
|
|
|
// Check if there is already new password created but waiting to be activated for this login |
1015
|
|
|
if (strtotime($row->newpass_time) < time()) { |
1016
|
|
|
// Appearantly there is no password created yet for this login, so we create new password |
1017
|
|
|
$data['password'] = $this->_gen_pass(); |
|
|
|
|
1018
|
|
|
|
1019
|
|
|
// Encode & Crypt password |
1020
|
|
|
$encode = crypt($this->_encode($data['password'])); |
1021
|
|
|
|
1022
|
|
|
// Create key |
1023
|
|
|
$data['key'] = md5(rand() . microtime()); |
1024
|
|
|
|
1025
|
|
|
// Create new password (but it haven't activated yet) |
1026
|
|
|
$this->ci->users->newpass($row->id, $encode, $data['key']); |
1027
|
|
|
|
1028
|
|
|
// Create reset password link to be included in email |
1029
|
|
|
$data['reset_password_uri'] = site_url($this->ci->config->item('DX_reset_password_uri') . "{$row->email}/{$data['key']}"); |
1030
|
|
|
|
1031
|
|
|
// Trigger event and get email content |
1032
|
|
|
// $this->ci->dx_auth_event->sending_forgot_password_email($data, $message); |
1033
|
|
|
|
1034
|
|
|
$settings = $this->ci->cms_base->get_settings(); |
1035
|
|
|
$replaceData = [ |
1036
|
|
|
'webSiteName' => $settings['site_title'] ? $settings['site_title'] : $this->ci->config->item('DX_website_name'), |
1037
|
|
|
'resetPasswordUri' => $data['reset_password_uri'], |
1038
|
|
|
'password' => $data['password'], |
1039
|
|
|
'key' => $data['key'], |
1040
|
|
|
'webMasterEmail' => $this->ci->config->item('DX_webmaster_email'), |
1041
|
|
|
]; |
1042
|
|
|
|
1043
|
|
|
\cmsemail\email::getInstance()->sendEmail($row->email, 'forgot_password', $replaceData); |
1044
|
|
|
|
1045
|
|
|
// Send instruction email |
1046
|
|
|
//$this->_email($row->email, $from, $subject, $message); |
1047
|
|
|
|
1048
|
|
|
$result = TRUE; |
1049
|
|
|
} else { |
1050
|
|
|
// There is already new password waiting to be activated |
1051
|
|
|
$this->_auth_error = lang('auth request sent'); |
1052
|
|
|
} |
1053
|
|
|
} else { |
1054
|
|
|
$this->_auth_error = lang('auth username or email not exist'); |
1055
|
|
|
} |
1056
|
|
|
} |
1057
|
|
|
return $result; |
1058
|
|
|
} |
1059
|
|
|
|
1060
|
|
|
public function reset_password($email, $key = '') { |
1061
|
|
|
// Load Models |
1062
|
|
|
$this->ci->load->model('dx_auth/users', 'users'); |
1063
|
|
|
$this->ci->load->model('dx_auth/user_autologin', 'user_autologin'); |
1064
|
|
|
|
1065
|
|
|
// Default return value |
1066
|
|
|
$result = FALSE; |
1067
|
|
|
|
1068
|
|
|
// Default user_id set to none |
1069
|
|
|
$user_id = 0; |
|
|
|
|
1070
|
|
|
|
1071
|
|
|
// Get user id |
1072
|
|
|
if ($query = $this->ci->users->get_user_by_email($email) AND $query->num_rows() == 1) { |
1073
|
|
|
$user_id = $query->row()->id; |
1074
|
|
|
|
1075
|
|
|
// Try to activate new password |
1076
|
|
|
if (!empty($email) AND ! empty($key) AND $this->ci->users->activate_newpass($user_id, $key) AND $this->ci->db->affected_rows() > 0) { |
1077
|
|
|
// Clear previously setup new password and keys |
1078
|
|
|
$this->ci->user_autologin->clear_keys($user_id); |
1079
|
|
|
|
1080
|
|
|
$result = TRUE; |
1081
|
|
|
} |
1082
|
|
|
} |
1083
|
|
|
return $result; |
1084
|
|
|
} |
1085
|
|
|
|
1086
|
|
|
public function activate($email, $key = '') { |
1087
|
|
|
// Load Models |
1088
|
|
|
$this->ci->load->model('dx_auth/users', 'users'); |
1089
|
|
|
$this->ci->load->model('dx_auth/user_temp', 'user_temp'); |
1090
|
|
|
|
1091
|
|
|
// Default return value |
1092
|
|
|
$result = FALSE; |
1093
|
|
|
|
1094
|
|
|
if ($this->ci->config->item('DX_email_activation')) { |
1095
|
|
|
// Delete user whose account expired (not activated until expired time) |
1096
|
|
|
$this->ci->user_temp->prune_temp(); |
1097
|
|
|
} |
1098
|
|
|
|
1099
|
|
|
// Activate user |
1100
|
|
|
if ($query = $this->ci->user_temp->activate_user($email, $key) AND $query->num_rows() > 0) { |
1101
|
|
|
// Get user |
1102
|
|
|
$row = $query->row_array(); |
1103
|
|
|
|
1104
|
|
|
$del = $row['id']; |
1105
|
|
|
|
1106
|
|
|
// Unset any unwanted fields |
1107
|
|
|
unset($row['id']); // We don't want to copy the id across |
1108
|
|
|
unset($row['activation_key']); |
1109
|
|
|
|
1110
|
|
|
// Create user |
1111
|
|
|
if ($this->ci->users->create_user($row)) { |
1112
|
|
|
// Trigger event |
1113
|
|
|
$this->ci->dx_auth_event->user_activated($this->ci->db->insert_id()); |
1114
|
|
|
|
1115
|
|
|
// Delete user from temp |
1116
|
|
|
$this->ci->user_temp->delete_user($del); |
1117
|
|
|
|
1118
|
|
|
$result = TRUE; |
1119
|
|
|
} |
1120
|
|
|
} |
1121
|
|
|
|
1122
|
|
|
return $result; |
1123
|
|
|
} |
1124
|
|
|
|
1125
|
|
|
public function change_password($old_pass, $new_pass) { |
1126
|
|
|
// Load Models |
1127
|
|
|
$this->ci->load->model('dx_auth/users', 'users'); |
1128
|
|
|
|
1129
|
|
|
// Default return value |
1130
|
|
|
$result = FAlSE; |
1131
|
|
|
|
1132
|
|
|
// Search current logged in user in database |
1133
|
|
|
if ($query = $this->ci->users->get_user_by_id($this->ci->session->userdata('DX_user_id')) AND $query->num_rows() > 0) { |
1134
|
|
|
// Get current logged in user |
1135
|
|
|
$row = $query->row(); |
1136
|
|
|
$pass = $this->_encode($old_pass); |
1137
|
|
|
|
1138
|
|
|
// Check if old password correct |
1139
|
|
|
if (crypt($pass, $row->password) === $row->password) { |
1140
|
|
|
// Crypt and encode new password |
1141
|
|
|
$new_pass_for_user = $new_pass; |
1142
|
|
|
$new_pass = crypt($this->_encode($new_pass)); |
1143
|
|
|
|
1144
|
|
|
// Replace old password with new password |
1145
|
|
|
$this->ci->users->change_password($this->ci->session->userdata('DX_user_id'), $new_pass); |
1146
|
|
|
|
1147
|
|
|
// Trigger event |
1148
|
|
|
$this->ci->dx_auth_event->user_changed_password($this->ci->session->userdata('DX_user_id'), $new_pass); |
1149
|
|
|
|
1150
|
|
|
$replaceData = [ |
1151
|
|
|
'user_name' => $row->username, |
1152
|
|
|
'password' => $new_pass_for_user, |
1153
|
|
|
]; |
1154
|
|
|
|
1155
|
|
|
\cmsemail\email::getInstance()->sendEmail($row->email, 'change_password', $replaceData); |
1156
|
|
|
|
1157
|
|
|
$result = TRUE; |
1158
|
|
|
} else { |
1159
|
|
|
$this->_auth_error = lang('auth incorrect old password'); |
1160
|
|
|
} |
1161
|
|
|
} |
1162
|
|
|
|
1163
|
|
|
return $result; |
1164
|
|
|
} |
1165
|
|
|
|
1166
|
|
|
public function cancel_account($password) { |
1167
|
|
|
// Load Models |
1168
|
|
|
$this->ci->load->model('dx_auth/users', 'users'); |
1169
|
|
|
|
1170
|
|
|
// Default return value |
1171
|
|
|
$result = FAlSE; |
1172
|
|
|
|
1173
|
|
|
// Search current logged in user in database |
1174
|
|
|
if ($query = $this->ci->users->get_user_by_id($this->ci->session->userdata('DX_user_id')) AND $query->num_rows() > 0) { |
1175
|
|
|
// Get current logged in user |
1176
|
|
|
$row = $query->row(); |
1177
|
|
|
|
1178
|
|
|
$pass = $this->_encode($password); |
1179
|
|
|
|
1180
|
|
|
// Check if password correct |
1181
|
|
|
if (crypt($pass, $row->password) === $row->password) { |
1182
|
|
|
// Trigger event |
1183
|
|
|
$this->ci->dx_auth_event->user_canceling_account($this->ci->session->userdata('DX_user_id')); |
1184
|
|
|
|
1185
|
|
|
// Delete user |
1186
|
|
|
$result = $this->ci->users->delete_user($this->ci->session->userdata('DX_user_id')); |
1187
|
|
|
|
1188
|
|
|
// Force logout |
1189
|
|
|
$this->logout(); |
1190
|
|
|
} else { |
1191
|
|
|
$this->_auth_error = lang('auth incorrect password'); |
1192
|
|
|
} |
1193
|
|
|
} |
1194
|
|
|
|
1195
|
|
|
return $result; |
1196
|
|
|
} |
1197
|
|
|
|
1198
|
|
|
/* End of main function */ |
1199
|
|
|
|
1200
|
|
|
/* Captcha related function */ |
1201
|
|
|
|
1202
|
|
|
public function captcha() { |
1203
|
|
|
$this->ci->load->helper('dx_captcha'); |
1204
|
|
|
// Load library SESSION |
1205
|
|
|
|
1206
|
|
|
$vals = [ |
1207
|
|
|
'img_path' => $this->ci->config->item('DX_captcha_path'), |
1208
|
|
|
'img_url' => media_url() . 'captcha/', |
1209
|
|
|
'font_path' => $this->ci->config->item('DX_captcha_fonts_path'), |
1210
|
|
|
'font_size' => $this->ci->config->item('DX_captcha_font_size'), |
1211
|
|
|
'img_width' => $this->ci->config->item('DX_captcha_width'), |
1212
|
|
|
'img_height' => $this->ci->config->item('DX_captcha_height'), |
1213
|
|
|
'show_grid' => $this->ci->config->item('DX_captcha_grid'), |
1214
|
|
|
'expiration' => $this->ci->config->item('DX_captcha_expire'), |
1215
|
|
|
]; |
1216
|
|
|
|
1217
|
|
|
$cap = create_captcha($vals); |
|
|
|
|
1218
|
|
|
|
1219
|
|
|
$store = [ |
1220
|
|
|
'captcha_word' => $cap['word'], |
1221
|
|
|
'captcha_time' => $cap['time'], |
1222
|
|
|
]; |
1223
|
|
|
|
1224
|
|
|
// Plain, simple but effective |
1225
|
|
|
$this->ci->session->set_flashdata($store); |
1226
|
|
|
|
1227
|
|
|
// Set our captcha |
1228
|
|
|
$this->_captcha_image = $cap['image']; |
1229
|
|
|
} |
1230
|
|
|
|
1231
|
|
|
public function get_captcha_image() { |
1232
|
|
|
if ($this->use_recaptcha) { |
1233
|
|
|
return $this->_get_recaptcha_data(); |
1234
|
|
|
} else { |
1235
|
|
|
return $this->_captcha_image; |
1236
|
|
|
} |
1237
|
|
|
} |
1238
|
|
|
|
1239
|
|
|
// Check if captcha already expired |
1240
|
|
|
// Use this in callback function in your form validation |
1241
|
|
|
|
1242
|
|
|
public function is_captcha_expired() { |
1243
|
|
|
// Captcha Expired |
1244
|
|
|
list($usec, $sec) = explode(' ', microtime()); |
1245
|
|
|
$now = ((float) $usec + (float) $sec); |
1246
|
|
|
|
1247
|
|
|
// Check if captcha already expired |
1248
|
|
|
|
1249
|
|
|
return (($this->ci->session->flashdata('captcha_time') + $this->ci->config->item('DX_captcha_expire')) < $now); |
1250
|
|
|
} |
1251
|
|
|
|
1252
|
|
|
// Check is captcha match with code |
1253
|
|
|
// Use this in callback function in your form validation |
1254
|
|
|
|
1255
|
|
|
public function is_captcha_match($code) { |
1256
|
|
|
// Just check if code is the same value with flash data captcha_word which created in captcha() function |
1257
|
|
|
if ($this->ci->config->item('DX_captcha_case_sensetive')) { |
1258
|
|
|
return ($code == $this->ci->session->flashdata('captcha_word')); |
1259
|
|
|
} else { |
1260
|
|
|
return (strtolower($code) == strtolower($this->ci->session->flashdata('captcha_word'))); |
1261
|
|
|
} |
1262
|
|
|
} |
1263
|
|
|
|
1264
|
|
|
/* End of captcha related function */ |
1265
|
|
|
|
1266
|
|
|
public function captcha_check($code) { |
1267
|
|
|
$CI = get_instance(); |
1268
|
|
|
$result = TRUE; |
1269
|
|
|
|
1270
|
|
|
if ($this->use_recaptcha) { |
1271
|
|
|
$result = $this->is_recaptcha_match(); |
1272
|
|
|
if (!$result) { |
1273
|
|
|
$CI->form_validation->set_message('captcha_check', lang('Improper protection code')); |
1274
|
|
|
} |
1275
|
|
|
} else { |
1276
|
|
|
if ($this->is_captcha_expired()) { |
1277
|
|
|
// Will replace this error msg with $lang |
1278
|
|
|
$CI->form_validation->set_message('captcha_check', lang('Improper protection code')); |
1279
|
|
|
$result = FALSE; |
1280
|
|
|
} elseif (!$this->is_captcha_match($code)) { |
1281
|
|
|
$CI->form_validation->set_message('captcha_check', lang('Improper protection code')); |
1282
|
|
|
$result = FALSE; |
1283
|
|
|
} |
1284
|
|
|
} |
1285
|
|
|
return $result; |
1286
|
|
|
} |
1287
|
|
|
|
1288
|
|
|
/* Recaptcha function */ |
1289
|
|
|
|
1290
|
|
|
public function get_recaptcha_reload_link($text = 'Get another CAPTCHA') { |
1291
|
|
|
return '<a href="javascript:Recaptcha.reload()">' . $text . '</a>'; |
1292
|
|
|
} |
1293
|
|
|
|
1294
|
|
|
public function get_recaptcha_switch_image_audio_link($switch_image_text = 'Get an image CAPTCHA', $switch_audio_text = 'Get an audio CAPTCHA') { |
1295
|
|
|
return '<div class="recaptcha_only_if_image"><a href="javascript:Recaptcha.switch_type(\'audio\')">' . $switch_audio_text . '</a></div> |
1296
|
|
|
<div class="recaptcha_only_if_audio"><a href="javascript:Recaptcha.switch_type(\'image\')">' . $switch_image_text . '</a></div>'; |
1297
|
|
|
} |
1298
|
|
|
|
1299
|
|
|
public function get_recaptcha_label($image_text = 'Enter the words above', $audio_text = 'Enter the numbers you hear') { |
1300
|
|
|
return '<span class="recaptcha_only_if_image">' . $image_text . '</span> |
1301
|
|
|
<span class="recaptcha_only_if_audio">' . $audio_text . '</span>'; |
1302
|
|
|
} |
1303
|
|
|
|
1304
|
|
|
// Get captcha image |
1305
|
|
|
|
1306
|
|
|
public function get_recaptcha_image() { |
1307
|
|
|
return '<div id="recaptcha_image"></div>'; |
1308
|
|
|
} |
1309
|
|
|
|
1310
|
|
|
// Get captcha input box |
1311
|
|
|
// IMPORTANT: You should at least use this function when showing captcha even for testing, otherwise reCAPTCHA image won't show up |
1312
|
|
|
// because reCAPTCHA javascript will try to find input type with id="recaptcha_response_field" and name="recaptcha_response_field" |
1313
|
|
|
|
1314
|
|
|
public function get_recaptcha_input() { |
1315
|
|
|
return '<input type="text" id="recaptcha_response_field" name="recaptcha_response_field" />'; |
1316
|
|
|
} |
1317
|
|
|
|
1318
|
|
|
// Get recaptcha javascript and non javasript html |
1319
|
|
|
// IMPORTANT: you should put call this function the last, after you are using some of get_recaptcha_xxx function above. |
1320
|
|
|
|
1321
|
|
|
public function get_recaptcha_html() { |
1322
|
|
|
// Load reCAPTCHA helper function |
1323
|
|
|
$this->ci->load->helper('recaptcha'); |
1324
|
|
|
|
1325
|
|
|
// Add custom theme so we can get only image |
1326
|
|
|
$options = "<script> |
1327
|
|
|
var RecaptchaOptions = { |
1328
|
|
|
theme: 'clean', |
1329
|
|
|
custom_theme_widget: 'recaptcha_widget' |
1330
|
|
|
}; |
1331
|
|
|
</script>"; |
1332
|
|
|
|
1333
|
|
|
// Get reCAPTCHA javascript and non javascript HTML |
1334
|
|
|
$html = recaptcha_get_html($this->ci->config->item('DX_recaptcha_public_key')); |
1335
|
|
|
|
1336
|
|
|
return $options . $html; |
1337
|
|
|
} |
1338
|
|
|
|
1339
|
|
|
// Check if entered captcha code match with the image. |
1340
|
|
|
// Use this in callback function in your form validation |
1341
|
|
|
|
1342
|
|
|
public function is_recaptcha_match() { |
1343
|
|
|
$this->ci->load->helper('recaptcha'); |
1344
|
|
|
|
1345
|
|
|
$resp = recaptcha_check_answer($this->ci->config->item('DX_recaptcha_private_key'), $_SERVER['REMOTE_ADDR'], $_POST['recaptcha_challenge_field'], $_POST['recaptcha_response_field']); |
1346
|
|
|
|
1347
|
|
|
return $resp->is_valid; |
1348
|
|
|
} |
1349
|
|
|
|
1350
|
|
|
private function _get_recaptcha_data() { |
1351
|
|
|
return $this->get_recaptcha_html(); |
1352
|
|
|
} |
1353
|
|
|
|
1354
|
|
|
/* End of Recaptcha function */ |
1355
|
|
|
} |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: