SHOW:
|
|
- or go back to the newest paste.
1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); | |
2 | ||
3 | class User extends DataMapper { | |
4 | ||
5 | public $table = "users"; | |
6 | ||
7 | public function __construct() | |
8 | { | |
9 | parent::__construct(); | |
10 | ||
11 | $this->load->config('auth'); | |
12 | $this->load->helper('string'); | |
13 | ||
14 | $this->_default_rounds = $this->config->item('default_rounds'); | |
15 | $this->_random_rounds = $this->config->item('random_rounds'); | |
16 | $this->_min_rounds = $this->config->item('min_rounds'); | |
17 | $this->_max_rounds = $this->config->item('max_rounds'); | |
18 | $this->_password_recovery_key_expire = $this->config->item('password_recovery_key_expire'); | |
19 | ||
20 | if ($this->_random_rounds) | |
21 | { | |
22 | $rand = rand($this->_min_rounds,$this->_max_rounds); | |
23 | $rounds = array('rounds' => $rand); | |
24 | } | |
25 | else | |
26 | { | |
27 | $rounds = array('rounds' => $this->_default_rounds); | |
28 | } | |
29 | ||
30 | // Load Bcrypt | |
31 | $this->load->library('bcrypt', $rounds); | |
32 | } | |
33 | ||
34 | /** | |
35 | * email_exists | |
36 | * | |
37 | * Checks if given email exists | |
38 | * | |
39 | * @param string $email | |
40 | * @return bool | |
41 | */ | |
42 | function email_exists($email) | |
43 | { | |
44 | $this->where('email', $email)->get(); | |
45 | ||
46 | if ($this->exists()) | |
47 | { | |
48 | return TRUE; | |
49 | } | |
50 | } | |
51 | ||
52 | /** | |
53 | * check_password | |
54 | * | |
55 | * This function takes a password and validates it | |
56 | * against an entry in the users table. | |
57 | * | |
58 | * @param string $password The password the user provides | |
59 | * @param string $hashed_password The hashed password stored in the database | |
60 | * @return bool | |
61 | */ | |
62 | function check_password($password, $hashed_password) | |
63 | { | |
64 | $CI =& get_instance(); | |
65 | ||
66 | return $CI->bcrypt->verify($password, $hashed_password); | |
67 | } | |
68 | ||
69 | /** | |
70 | * change_password | |
71 | * | |
72 | * @param int $user_id | |
73 | * @param string $password | |
74 | * @return bool | |
75 | */ | |
76 | function change_password($user_id, $password) | |
77 | { | |
78 | $this->where('id', $user_id)->get(); | |
79 | ||
80 | $this->password = self::_encrypt($password); | |
81 | ||
82 | if ($this->save()) | |
83 | { | |
84 | return TRUE; | |
85 | } | |
86 | } | |
87 | ||
88 | /** | |
89 | * is_valid_activation_key | |
90 | * | |
91 | * @param string $key | |
92 | * @return bool | |
93 | */ | |
94 | function is_valid_activation_key($key) | |
95 | { | |
96 | $this->get_by_activation_key($key); | |
97 | ||
98 | if ($this->exists()) | |
99 | { | |
100 | return TRUE; | |
101 | } | |
102 | } | |
103 | ||
104 | /** | |
105 | * set_activation_key | |
106 | * | |
107 | * @param int $user_id | |
108 | * @return bool | |
109 | */ | |
110 | function set_activation_key($user_id) | |
111 | { | |
112 | $this->where('id', $user_id); | |
113 | $this->activation_key = self::_generate_unique_str(); | |
114 | ||
115 | if ($this->save()) | |
116 | { | |
117 | return TRUE; | |
118 | } | |
119 | } | |
120 | ||
121 | /** | |
122 | * is_activated | |
123 | * | |
124 | * @param int $user_id | |
125 | * @return bool | |
126 | */ | |
127 | function is_activated($user_id) | |
128 | { | |
129 | $this->where('id', $user_id)->get(); | |
130 | ||
131 | if ($this->activated) | |
132 | { | |
133 | return TRUE; | |
134 | } | |
135 | } | |
136 | ||
137 | /** | |
138 | * activate | |
139 | * | |
140 | * @param string $key | |
141 | * @return bool | |
142 | */ | |
143 | function activate($key) | |
144 | { | |
145 | if (self::is_valid_activation_key($key)) | |
146 | { | |
147 | $this->where('activation_key', $key)->get(); | |
148 | ||
149 | $this->activated = TRUE; | |
150 | $this->activation_key = NULL; // Remove activation key | |
151 | ||
152 | if ($this->save()) | |
153 | { | |
154 | return TRUE; | |
155 | } | |
156 | } | |
157 | } | |
158 | ||
159 | /** | |
160 | * is_valid_password_recovery_key | |
161 | * | |
162 | * @param int $user_id | |
163 | * @param string $key | |
164 | * @return bool | |
165 | */ | |
166 | function is_valid_password_recovery_key($user_id, $key) | |
167 | { | |
168 | $this->where('id', $user_id); | |
169 | $this->where('recover_password_key', $key); | |
170 | $this->where('recover_password_key_requested >', time() - $this->_password_recovery_key_expire); | |
171 | $this->get(); | |
172 | ||
173 | if ($this->exists()) | |
174 | { | |
175 | return TRUE; | |
176 | } | |
177 | } | |
178 | ||
179 | /** | |
180 | * set_password_recovery_key | |
181 | * | |
182 | * If the current recovery key isn't old enough | |
183 | * only update when this function was last | |
184 | * requested to prevent sabotage | |
185 | * | |
186 | * @param string $email | |
187 | */ | |
188 | function set_password_recovery_key($email) | |
189 | { | |
190 | $this->where('email', $email)->get(); | |
191 | ||
192 | if ($this->recover_password_key_requested < time() - $this->_password_recovery_key_expire){ | |
193 | $this->recover_password_key = self::_generate_unique_str(); | |
194 | $this->recover_password_key_requested = time(); | |
195 | }else{ | |
196 | //$this->recover_password_key = $this->recover_password_key; | |
197 | $this->recover_password_key_requested = time(); | |
198 | } | |
199 | ||
200 | if ($this->save()) | |
201 | { | |
202 | return TRUE; | |
203 | } | |
204 | } | |
205 | ||
206 | /** | |
207 | * reset_password | |
208 | * | |
209 | * @param int $user_id | |
210 | * @param string $key | |
211 | * @param string $password | |
212 | * @return bool | |
213 | */ | |
214 | function reset_password($user_id, $key, $password) | |
215 | { | |
216 | if (self::is_valid_password_recovery_key($user_id, $key)) | |
217 | { | |
218 | $this->password = self::_encrypt($password); | |
219 | $this->recover_password_key = NULL; | |
220 | $this->recover_password_key_requested = NULL; | |
221 | ||
222 | if ($this->save()) | |
223 | { | |
224 | return TRUE; | |
225 | } | |
226 | } | |
227 | } | |
228 | ||
229 | /** | |
230 | * login | |
231 | * | |
232 | * @param string $username | |
233 | * @param string $password | |
234 | * @return bool | |
235 | */ | |
236 | function login($username, $password) | |
237 | { | |
238 | $CI =& get_instance(); | |
239 | ||
240 | $this->where('username', $username)->get(); | |
241 | ||
242 | if ($this->exists() && self::check_password($password, $this->password)) | |
243 | { | |
244 | $this->last_login = time(); | |
245 | $this->save(); | |
246 | ||
247 | return TRUE; | |
248 | } | |
249 | ||
250 | } | |
251 | ||
252 | /** | |
253 | * register | |
254 | * | |
255 | * @param mixed $data | |
256 | * @param bool $require_activation | |
257 | * @return bool | |
258 | */ | |
259 | function register($data, $require_activation) | |
260 | { | |
261 | $CI =& get_instance(); | |
262 | ||
263 | $this->username = $data['username']; | |
264 | $this->password = self::_encrypt($data['password']); | |
265 | $this->email = $data['email']; | |
266 | $this->first_name = $data['first_name']; | |
267 | $this->last_name = $data['last_name']; | |
268 | $this->activated = $require_activation === TRUE ? FALSE : TRUE; | |
269 | $this->activation_key = $require_activation === TRUE ? self::_generate_unique_str() : NULL; | |
270 | $this->ip_address = $this->_ip_address; | |
271 | $this->created_on = time(); | |
272 | ||
273 | if ($this->save()) | |
274 | { | |
275 | return TRUE; | |
276 | } | |
277 | } | |
278 | ||
279 | /** | |
280 | * _generate_unique_key | |
281 | * | |
282 | * Generates a unique string (32 chars in lenght) | |
283 | * | |
284 | * @return string | |
285 | */ | |
286 | function _generate_unique_str() | |
287 | { | |
288 | return random_string('unique'); | |
289 | } | |
290 | ||
291 | /** | |
292 | * _encrypt | |
293 | * | |
294 | * @param string $password | |
295 | */ | |
296 | function _encrypt($password) | |
297 | { | |
298 | $CI =& get_instance(); | |
299 | ||
300 | return $CI->bcrypt->hash($password); | |
301 | } | |
302 | } |