Revision 1289
Added by kweitzel almost 15 years ago
class.login.php | ||
---|---|---|
1 |
<?php |
|
2 |
|
|
3 |
// $Id$ |
|
4 |
|
|
5 |
/* |
|
6 |
|
|
7 |
Website Baker Project <http://www.websitebaker.org/> |
|
8 |
Copyright (C) 2004-2009, Ryan Djurovich |
|
9 |
|
|
10 |
Website Baker is free software; you can redistribute it and/or modify |
|
11 |
it under the terms of the GNU General Public License as published by |
|
12 |
the Free Software Foundation; either version 2 of the License, or |
|
13 |
(at your option) any later version. |
|
14 |
|
|
15 |
Website Baker is distributed in the hope that it will be useful, |
|
16 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
17 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
18 |
GNU General Public License for more details. |
|
19 |
|
|
20 |
You should have received a copy of the GNU General Public License |
|
21 |
along with Website Baker; if not, write to the Free Software |
|
22 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
23 |
|
|
24 |
*/ |
|
25 |
|
|
26 |
/* |
|
27 |
|
|
28 |
Login class |
|
29 |
|
|
30 |
This class will be used to with the login application |
|
31 |
|
|
32 |
*/ |
|
33 |
|
|
34 |
// Stop this file from being accessed directly |
|
35 |
if(!defined('WB_URL')) { |
|
36 |
header('Location: ../index.php'); |
|
37 |
exit(0); |
|
38 |
} |
|
39 |
|
|
40 |
define('LOGIN_CLASS_LOADED', true); |
|
41 |
|
|
42 |
// Load the other required class files if they are not already loaded |
|
43 |
require_once(WB_PATH."/framework/class.admin.php"); |
|
44 |
|
|
45 |
class login extends admin { |
|
46 |
function login($config_array) { |
|
47 |
// Get language vars |
|
48 |
global $MESSAGE; |
|
49 |
$this->wb(); |
|
50 |
// Get configuration values |
|
51 |
$this->USERS_TABLE = $config_array['USERS_TABLE']; |
|
52 |
$this->GROUPS_TABLE = $config_array['GROUPS_TABLE']; |
|
53 |
$this->username_fieldname = $config_array['USERNAME_FIELDNAME']; |
|
54 |
$this->password_fieldname = $config_array['PASSWORD_FIELDNAME']; |
|
55 |
$this->remember_me_option = $config_array['REMEMBER_ME_OPTION']; |
|
56 |
$this->max_attemps = $config_array['MAX_ATTEMPS']; |
|
57 |
$this->warning_url = $config_array['WARNING_URL']; |
|
58 |
$this->login_url = $config_array['LOGIN_URL']; |
|
59 |
$this->template_dir = $config_array['TEMPLATE_DIR']; |
|
60 |
$this->template_file = $config_array['TEMPLATE_FILE']; |
|
61 |
$this->frontend = $config_array['FRONTEND']; |
|
62 |
$this->forgotten_details_app = $config_array['FORGOTTEN_DETAILS_APP']; |
|
63 |
$this->max_username_len = $config_array['MAX_USERNAME_LEN']; |
|
64 |
$this->max_password_len = $config_array['MAX_PASSWORD_LEN']; |
|
65 |
if (array_key_exists('REDIRECT_URL',$config_array)) |
|
66 |
$this->redirect_url = $config_array['REDIRECT_URL']; |
|
67 |
else |
|
68 |
$this->redirect_url = ''; |
|
69 |
// Get the supplied username and password |
|
70 |
if ($this->get_post('username_fieldname') != ''){ |
|
71 |
$username_fieldname = $this->get_post('username_fieldname'); |
|
72 |
$password_fieldname = $this->get_post('password_fieldname'); |
|
73 |
} else { |
|
74 |
$username_fieldname = 'username'; |
|
75 |
$password_fieldname = 'password'; |
|
76 |
} |
|
77 |
$this->username = $this->add_slashes(strtolower($this->get_post($username_fieldname))); |
|
78 |
$this->password = $this->get_post($password_fieldname); |
|
79 |
// Figure out if the "remember me" option has been checked |
|
80 |
if($this->get_post('remember') == 'true') { |
|
81 |
$this->remember = $this->get_post('remember'); |
|
82 |
} else { |
|
83 |
$this->remember = false; |
|
84 |
} |
|
85 |
// Get the length of the supplied username and password |
|
86 |
if($this->get_post($username_fieldname) != '') { |
|
87 |
$this->username_len = strlen($this->username); |
|
88 |
$this->password_len = strlen($this->password); |
|
89 |
} |
|
90 |
// If the url is blank, set it to the default url |
|
91 |
$this->url = $this->get_post('url'); |
|
92 |
if ($this->redirect_url!='') { |
|
93 |
$this->url = $this->redirect_url; |
|
94 |
} |
|
95 |
if(strlen($this->url) < 2) { |
|
96 |
$this->url = $config_array['DEFAULT_URL']; |
|
97 |
} |
|
98 |
if($this->is_authenticated() == true) { |
|
99 |
// User already logged-in, so redirect to default url |
|
100 |
header('Location: '.$this->url); |
|
101 |
exit(); |
|
102 |
} elseif($this->is_remembered() == true) { |
|
103 |
// User has been "remembered" |
|
104 |
// Get the users password |
|
105 |
$database = new database(); |
|
106 |
$query_details = $database->query("SELECT * FROM ".$this->USERS_TABLE." WHERE user_id = '".$this->get_safe_remember_key()."' LIMIT 1"); |
|
107 |
$fetch_details = $query_details->fetchRow(); |
|
108 |
$this->username = $fetch_details['username']; |
|
109 |
$this->password = $fetch_details['password']; |
|
110 |
// Check if the user exists (authenticate them) |
|
111 |
if($this->authenticate()) { |
|
112 |
// Authentication successful |
|
113 |
header("Location: ".$this->url); |
|
114 |
exit(0); |
|
115 |
} else { |
|
116 |
$this->message = $MESSAGE['LOGIN']['AUTHENTICATION_FAILED']; |
|
117 |
$this->increase_attemps(); |
|
118 |
} |
|
119 |
} elseif($this->username == '' AND $this->password == '') { |
|
120 |
$this->message = $MESSAGE['LOGIN']['BOTH_BLANK']; |
|
121 |
$this->increase_attemps(); |
|
122 |
} elseif($this->username == '') { |
|
123 |
$this->message = $MESSAGE['LOGIN']['USERNAME_BLANK']; |
|
124 |
$this->increase_attemps(); |
|
125 |
} elseif($this->password == '') { |
|
126 |
$this->message = $MESSAGE['LOGIN']['PASSWORD_BLANK']; |
|
127 |
$this->increase_attemps(); |
|
128 |
} elseif($this->username_len < $config_array['MIN_USERNAME_LEN']) { |
|
129 |
$this->message = $MESSAGE['LOGIN']['USERNAME_TOO_SHORT']; |
|
130 |
$this->increase_attemps(); |
|
131 |
} elseif($this->password_len < $config_array['MIN_PASSWORD_LEN']) { |
|
132 |
$this->message = $MESSAGE['LOGIN']['PASSWORD_TOO_SHORT']; |
|
133 |
$this->increase_attemps(); |
|
134 |
} elseif($this->username_len > $config_array['MAX_USERNAME_LEN']) { |
|
135 |
$this->message = $MESSAGE['LOGIN']['USERNAME_TOO_LONG']; |
|
136 |
$this->increase_attemps(); |
|
137 |
} elseif($this->password_len > $config_array['MAX_PASSWORD_LEN']) { |
|
138 |
$this->message = $MESSAGE['LOGIN']['PASSWORD_TOO_LONG']; |
|
139 |
$this->increase_attemps(); |
|
140 |
} else { |
|
141 |
// Check if the user exists (authenticate them) |
|
142 |
$this->password = md5($this->password); |
|
143 |
if($this->authenticate()) { |
|
144 |
// Authentication successful |
|
145 |
//echo $this->url;exit(); |
|
146 |
header("Location: ".$this->url); |
|
147 |
exit(0); |
|
148 |
} else { |
|
149 |
$this->message = $MESSAGE['LOGIN']['AUTHENTICATION_FAILED']; |
|
150 |
$this->increase_attemps(); |
|
151 |
} |
|
152 |
} |
|
153 |
} |
|
154 |
|
|
155 |
// Authenticate the user (check if they exist in the database) |
|
156 |
function authenticate() { |
|
157 |
// Get user information |
|
158 |
$database = new database(); |
|
159 |
$query = "SELECT * FROM ".$this->USERS_TABLE." WHERE username = '".$this->username."' AND password = '".$this->password."' AND active = '1'"; |
|
160 |
$results = $database->query($query); |
|
161 |
$results_array = $results->fetchRow(); |
|
162 |
$num_rows = $results->numRows(); |
|
163 |
if($num_rows) { |
|
164 |
$user_id = $results_array['user_id']; |
|
165 |
$this->user_id = $user_id; |
|
166 |
$_SESSION['USER_ID'] = $user_id; |
|
167 |
$_SESSION['GROUP_ID'] = $results_array['group_id']; |
|
168 |
$_SESSION['GROUPS_ID'] = $results_array['groups_id']; |
|
169 |
$_SESSION['USERNAME'] = $results_array['username']; |
|
170 |
$_SESSION['DISPLAY_NAME'] = $results_array['display_name']; |
|
171 |
$_SESSION['EMAIL'] = $results_array['email']; |
|
172 |
$_SESSION['HOME_FOLDER'] = $results_array['home_folder']; |
|
173 |
// Run remember function if needed |
|
174 |
if($this->remember == true) { |
|
175 |
$this->remember($this->user_id); |
|
176 |
} |
|
177 |
// Set language |
|
178 |
if($results_array['language'] != '') { |
|
179 |
$_SESSION['LANGUAGE'] = $results_array['language']; |
|
180 |
} |
|
181 |
// Set timezone |
|
182 |
if($results_array['timezone'] != '-72000') { |
|
183 |
$_SESSION['TIMEZONE'] = $results_array['timezone']; |
|
184 |
} else { |
|
185 |
// Set a session var so apps can tell user is using default tz |
|
186 |
$_SESSION['USE_DEFAULT_TIMEZONE'] = true; |
|
187 |
} |
|
188 |
// Set date format |
|
189 |
if($results_array['date_format'] != '') { |
|
190 |
$_SESSION['DATE_FORMAT'] = $results_array['date_format']; |
|
191 |
} else { |
|
192 |
// Set a session var so apps can tell user is using default date format |
|
193 |
$_SESSION['USE_DEFAULT_DATE_FORMAT'] = true; |
|
194 |
} |
|
195 |
// Set time format |
|
196 |
if($results_array['time_format'] != '') { |
|
197 |
$_SESSION['TIME_FORMAT'] = $results_array['time_format']; |
|
198 |
} else { |
|
199 |
// Set a session var so apps can tell user is using default time format |
|
200 |
$_SESSION['USE_DEFAULT_TIME_FORMAT'] = true; |
|
201 |
} |
|
202 |
// Get group information |
|
203 |
// $query = "SELECT * FROM ".$this->GROUPS_TABLE." WHERE group_id = '".$this->get_session('GROUP_ID')."'"; |
|
204 |
// $results = $database->query($query); |
|
205 |
// $results_array = $results->fetchRow(); |
|
206 |
// $_SESSION['GROUP_NAME'] = $results_array['name']; |
|
207 |
// // Set system permissions |
|
208 |
// if($results_array['system_permissions'] != '') { |
|
209 |
// $_SESSION['SYSTEM_PERMISSIONS'] = explode(',', $results_array['system_permissions']); |
|
210 |
// } else { |
|
211 |
// $_SESSION['SYSTEM_PERMISSIONS'] = array(); |
|
212 |
// } |
|
213 |
// // Set module permissions |
|
214 |
// if($results_array['module_permissions'] != '') { |
|
215 |
// $_SESSION['MODULE_PERMISSIONS'] = explode(',', $results_array['module_permissions']); |
|
216 |
// } else { |
|
217 |
// $_SESSION['MODULE_PERMISSIONS'] = array(); |
|
218 |
// } |
|
219 |
// // Set template permissions |
|
220 |
// if($results_array['template_permissions'] != '') { |
|
221 |
// $_SESSION['TEMPLATE_PERMISSIONS'] = explode(',', $results_array['template_permissions']); |
|
222 |
// } else { |
|
223 |
// $_SESSION['TEMPLATE_PERMISSIONS'] = array(); |
|
224 |
// } |
|
225 |
|
|
226 |
$_SESSION['SYSTEM_PERMISSIONS'] = array(); |
|
227 |
$_SESSION['MODULE_PERMISSIONS'] = array(); |
|
228 |
$_SESSION['TEMPLATE_PERMISSIONS'] = array(); |
|
229 |
$_SESSION['GROUP_NAME'] = array(); |
|
230 |
|
|
231 |
$first_group = true; |
|
232 |
foreach (split(",", $this->get_session('GROUPS_ID')) as $cur_group_id) { |
|
233 |
$query = "SELECT * FROM ".$this->GROUPS_TABLE." WHERE group_id = '".$cur_group_id."'"; |
|
234 |
$results = $database->query($query); |
|
235 |
$results_array = $results->fetchRow(); |
|
236 |
$_SESSION['GROUP_NAME'][$cur_group_id] = $results_array['name']; |
|
237 |
// Set system permissions |
|
238 |
if($results_array['system_permissions'] != '') { |
|
239 |
$_SESSION['SYSTEM_PERMISSIONS'] = array_merge($_SESSION['SYSTEM_PERMISSIONS'], explode(',', $results_array['system_permissions'])); |
|
240 |
} |
|
241 |
// Set module permissions |
|
242 |
if($results_array['module_permissions'] != '') { |
|
243 |
if ($first_group) { |
|
244 |
$_SESSION['MODULE_PERMISSIONS'] = explode(',', $results_array['module_permissions']); |
|
245 |
} else { |
|
246 |
$_SESSION['MODULE_PERMISSIONS'] = array_intersect($_SESSION['MODULE_PERMISSIONS'], explode(',', $results_array['module_permissions'])); |
|
247 |
} |
|
248 |
} |
|
249 |
// Set template permissions |
|
250 |
if($results_array['template_permissions'] != '') { |
|
251 |
if ($first_group) { |
|
252 |
$_SESSION['TEMPLATE_PERMISSIONS'] = explode(',', $results_array['template_permissions']); |
|
253 |
} else { |
|
254 |
$_SESSION['TEMPLATE_PERMISSIONS'] = array_intersect($_SESSION['TEMPLATE_PERMISSIONS'], explode(',', $results_array['template_permissions'])); |
|
255 |
} |
|
256 |
} |
|
257 |
$first_group = false; |
|
258 |
} |
|
259 |
|
|
260 |
// Update the users table with current ip and timestamp |
|
261 |
$get_ts = time(); |
|
262 |
$get_ip = $_SERVER['REMOTE_ADDR']; |
|
263 |
$query = "UPDATE ".$this->USERS_TABLE." SET login_when = '$get_ts', login_ip = '$get_ip' WHERE user_id = '$user_id'"; |
|
264 |
$database->query($query); |
|
265 |
} |
|
266 |
// Return if the user exists or not |
|
267 |
return $num_rows; |
|
268 |
} |
|
269 |
|
|
270 |
// Increase the count for login attemps |
|
271 |
function increase_attemps() { |
|
272 |
if(!isset($_SESSION['ATTEMPS'])) { |
|
273 |
$_SESSION['ATTEMPS'] = 0; |
|
274 |
} else { |
|
275 |
$_SESSION['ATTEMPS'] = $this->get_session('ATTEMPS')+1; |
|
276 |
} |
|
277 |
$this->display_login(); |
|
278 |
} |
|
279 |
|
|
280 |
// Function to set a "remembering" cookie for the user |
|
281 |
function remember($user_id) { |
|
282 |
$remember_key = ''; |
|
283 |
// Generate user id to append to the remember key |
|
284 |
$length = 11-strlen($user_id); |
|
285 |
if($length > 0) { |
|
286 |
for($i = 1; $i <= $length; $i++) { |
|
287 |
$remember_key .= '0'; |
|
288 |
} |
|
289 |
} |
|
290 |
// Generate remember key |
|
291 |
$remember_key .= $user_id.'_'; |
|
292 |
$salt = "abchefghjkmnpqrstuvwxyz0123456789"; |
|
293 |
srand((double)microtime()*1000000); |
|
294 |
$i = 0; |
|
295 |
while ($i <= 10) { |
|
296 |
$num = rand() % 33; |
|
297 |
$tmp = substr($salt, $num, 1); |
|
298 |
$remember_key = $remember_key . $tmp; |
|
299 |
$i++; |
|
300 |
} |
|
301 |
$remember_key = $remember_key; |
|
302 |
// Update the remember key in the db |
|
303 |
$database = new database(); |
|
304 |
$database->query("UPDATE ".$this->USERS_TABLE." SET remember_key = '$remember_key' WHERE user_id = '$user_id' LIMIT 1"); |
|
305 |
if($database->is_error()) { |
|
306 |
return false; |
|
307 |
} else { |
|
308 |
// Workout options for the cookie |
|
309 |
$cookie_name = 'REMEMBER_KEY'; |
|
310 |
$cookie_value = $remember_key; |
|
311 |
$cookie_expire = time()+60*60*24*30; |
|
312 |
// Set the cookie |
|
313 |
if(setcookie($cookie_name, $cookie_value, $cookie_expire, '/')) { |
|
314 |
return true; |
|
315 |
} else { |
|
316 |
return false; |
|
317 |
} |
|
318 |
} |
|
319 |
} |
|
320 |
|
|
321 |
// Function to check if a user has been remembered |
|
322 |
function is_remembered() { |
|
323 |
if(isset($_COOKIE['REMEMBER_KEY']) AND $_COOKIE['REMEMBER_KEY'] != '') { |
|
324 |
// Check if the remember key is correct |
|
325 |
$database = new database(); |
|
326 |
$sql = "SELECT `user_id` FROM `" . $this->USERS_TABLE . "` WHERE `remember_key` = '"; |
|
327 |
$sql .= $this->get_safe_remember_key() . "' LIMIT 1"; |
|
328 |
$check_query = $database->query($sql); |
|
329 |
|
|
330 |
if($check_query->numRows() > 0) { |
|
331 |
$check_fetch = $check_query->fetchRow(); |
|
332 |
$user_id = $check_fetch['user_id']; |
|
333 |
// Check the remember key prefix |
|
334 |
$remember_key_prefix = ''; |
|
335 |
$length = 11-strlen($user_id); |
|
336 |
if($length > 0) { |
|
337 |
for($i = 1; $i <= $length; $i++) { |
|
338 |
$remember_key_prefix .= '0'; |
|
339 |
} |
|
340 |
} |
|
341 |
$remember_key_prefix .= $user_id.'_'; |
|
342 |
$length = strlen($remember_key_prefix); |
|
343 |
if(substr($_COOKIE['REMEMBER_KEY'], 0, $length) == $remember_key_prefix) { |
|
344 |
return true; |
|
345 |
} else { |
|
346 |
return false; |
|
347 |
} |
|
348 |
} else { |
|
349 |
return false; |
|
350 |
} |
|
351 |
} else { |
|
352 |
return false; |
|
353 |
} |
|
354 |
} |
|
355 |
|
|
356 |
// Display the login screen |
|
357 |
function display_login() { |
|
358 |
// Get language vars |
|
359 |
global $MESSAGE; |
|
360 |
global $MENU; |
|
361 |
global $TEXT; |
|
362 |
// If attemps more than allowed, warn the user |
|
363 |
if($this->get_session('ATTEMPS') > $this->max_attemps) { |
|
364 |
$this->warn(); |
|
365 |
} |
|
366 |
// Show the login form |
|
367 |
if($this->frontend != true) { |
|
368 |
require_once(WB_PATH.'/include/phplib/template.inc'); |
|
369 |
$template = new Template($this->template_dir); |
|
370 |
$template->set_file('page', $this->template_file); |
|
371 |
$template->set_block('page', 'mainBlock', 'main'); |
|
372 |
if($this->remember_me_option != true) { |
|
373 |
$template->set_var('DISPLAY_REMEMBER_ME', 'none'); |
|
374 |
} else { |
|
375 |
$template->set_var('DISPLAY_REMEMBER_ME', ''); |
|
376 |
} |
|
377 |
$template->set_var(array( |
|
378 |
'ACTION_URL' => $this->login_url, |
|
379 |
'ATTEMPS' => $this->get_session('ATTEMPS'), |
|
380 |
'USERNAME' => $this->username, |
|
381 |
'USERNAME_FIELDNAME' => $this->username_fieldname, |
|
382 |
'PASSWORD_FIELDNAME' => $this->password_fieldname, |
|
383 |
'MESSAGE' => $this->message, |
|
384 |
'INTERFACE_DIR_URL' => ADMIN_URL.'/interface', |
|
385 |
'MAX_USERNAME_LEN' => $this->max_username_len, |
|
386 |
'MAX_PASSWORD_LEN' => $this->max_password_len, |
|
387 |
'WB_URL' => WB_URL, |
|
388 |
'THEME_URL' => THEME_URL, |
|
389 |
'LANGUAGE' => strtolower(LANGUAGE), |
|
390 |
'FORGOTTEN_DETAILS_APP' => $this->forgotten_details_app, |
|
391 |
'TEXT_FORGOTTEN_DETAILS' => $TEXT['FORGOTTEN_DETAILS'], |
|
392 |
'TEXT_USERNAME' => $TEXT['USERNAME'], |
|
393 |
'TEXT_PASSWORD' => $TEXT['PASSWORD'], |
|
394 |
'TEXT_REMEMBER_ME' => $TEXT['REMEMBER_ME'], |
|
395 |
'TEXT_LOGIN' => $TEXT['LOGIN'], |
|
396 |
'TEXT_HOME' => $TEXT['HOME'], |
|
397 |
'PAGES_DIRECTORY' => PAGES_DIRECTORY, |
|
398 |
'SECTION_LOGIN' => $MENU['LOGIN'] |
|
399 |
) |
|
400 |
); |
|
401 |
if(defined('DEFAULT_CHARSET')) { |
|
402 |
$charset=DEFAULT_CHARSET; |
|
403 |
} else { |
|
404 |
$charset='utf-8'; |
|
405 |
} |
|
406 |
|
|
407 |
$template->set_var('CHARSET', $charset); |
|
408 |
|
|
409 |
|
|
410 |
$template->parse('main', 'mainBlock', false); |
|
411 |
$template->pparse('output', 'page'); |
|
412 |
} |
|
413 |
} |
|
414 |
|
|
415 |
// sanities the REMEMBER_KEY cookie to avoid SQL injection |
|
416 |
function get_safe_remember_key() { |
|
417 |
if (!((strlen($_COOKIE['REMEMBER_KEY']) == 23) && (substr($_COOKIE['REMEMBER_KEY'], 11, 1) == '_'))) return ''; |
|
418 |
// create a clean cookie (XXXXXXXXXXX_YYYYYYYYYYY) where X:= numeric, Y:= hash |
|
419 |
$clean_cookie = sprintf('%011d', (int) substr($_COOKIE['REMEMBER_KEY'], 0, 11)) . substr($_COOKIE['REMEMBER_KEY'], 11); |
|
420 |
return ($clean_cookie == $_COOKIE['REMEMBER_KEY']) ? $this->add_slashes($clean_cookie) : ''; |
|
421 |
} |
|
422 |
|
|
423 |
// Warn user that they have had to many login attemps |
|
424 |
function warn() { |
|
425 |
header('Location: '.$this->warning_url); |
|
426 |
exit(0); |
|
427 |
} |
|
428 |
|
|
429 |
} |
|
430 |
|
|
1 |
<?php |
|
2 |
/** |
|
3 |
* |
|
4 |
* @category backend |
|
5 |
* @package login |
|
6 |
* @author WebsiteBaker Project |
|
7 |
* @copyright 2004-2009, Ryan Djurovich |
|
8 |
* @copyright 2009-2010, Website Baker Org. e.V. |
|
9 |
* @link http://www.websitebaker2.org/ |
|
10 |
* @license http://www.gnu.org/licenses/gpl.html |
|
11 |
* @platform WebsiteBaker 2.8.x |
|
12 |
* @requirements PHP 4.3.4 and higher |
|
13 |
* @version $Id$ |
|
14 |
* @filesource $HeadURL$ |
|
15 |
* @lastmodified $Date$ |
|
16 |
* |
|
17 |
*/ |
|
18 |
|
|
19 |
// Stop this file from being accessed directly |
|
20 |
if(!defined('WB_URL')) { |
|
21 |
header('Location: ../index.php'); |
|
22 |
exit(0); |
|
23 |
} |
|
24 |
|
|
25 |
define('LOGIN_CLASS_LOADED', true); |
|
26 |
|
|
27 |
// Load the other required class files if they are not already loaded |
|
28 |
require_once(WB_PATH."/framework/class.admin.php"); |
|
29 |
// Get WB version |
|
30 |
require_once(ADMIN_PATH.'/interface/version.php'); |
|
31 |
|
|
32 |
class login extends admin { |
|
33 |
function login($config_array) { |
|
34 |
// Get language vars |
|
35 |
global $MESSAGE; |
|
36 |
$this->wb(); |
|
37 |
// Get configuration values |
|
38 |
$this->USERS_TABLE = $config_array['USERS_TABLE']; |
|
39 |
$this->GROUPS_TABLE = $config_array['GROUPS_TABLE']; |
|
40 |
$this->username_fieldname = $config_array['USERNAME_FIELDNAME']; |
|
41 |
$this->password_fieldname = $config_array['PASSWORD_FIELDNAME']; |
|
42 |
$this->remember_me_option = $config_array['REMEMBER_ME_OPTION']; |
|
43 |
$this->max_attemps = $config_array['MAX_ATTEMPS']; |
|
44 |
$this->warning_url = $config_array['WARNING_URL']; |
|
45 |
$this->login_url = $config_array['LOGIN_URL']; |
|
46 |
$this->template_dir = $config_array['TEMPLATE_DIR']; |
|
47 |
$this->template_file = $config_array['TEMPLATE_FILE']; |
|
48 |
$this->frontend = $config_array['FRONTEND']; |
|
49 |
$this->forgotten_details_app = $config_array['FORGOTTEN_DETAILS_APP']; |
|
50 |
$this->max_username_len = $config_array['MAX_USERNAME_LEN']; |
|
51 |
$this->max_password_len = $config_array['MAX_PASSWORD_LEN']; |
|
52 |
if (array_key_exists('REDIRECT_URL',$config_array)) |
|
53 |
$this->redirect_url = $config_array['REDIRECT_URL']; |
|
54 |
else |
|
55 |
$this->redirect_url = ''; |
|
56 |
// Get the supplied username and password |
|
57 |
if ($this->get_post('username_fieldname') != ''){ |
|
58 |
$username_fieldname = $this->get_post('username_fieldname'); |
|
59 |
$password_fieldname = $this->get_post('password_fieldname'); |
|
60 |
} else { |
|
61 |
$username_fieldname = 'username'; |
|
62 |
$password_fieldname = 'password'; |
|
63 |
} |
|
64 |
$this->username = $this->add_slashes(strtolower($this->get_post($username_fieldname))); |
|
65 |
$this->password = $this->get_post($password_fieldname); |
|
66 |
// Figure out if the "remember me" option has been checked |
|
67 |
if($this->get_post('remember') == 'true') { |
|
68 |
$this->remember = $this->get_post('remember'); |
|
69 |
} else { |
|
70 |
$this->remember = false; |
|
71 |
} |
|
72 |
// Get the length of the supplied username and password |
|
73 |
if($this->get_post($username_fieldname) != '') { |
|
74 |
$this->username_len = strlen($this->username); |
|
75 |
$this->password_len = strlen($this->password); |
|
76 |
} |
|
77 |
// If the url is blank, set it to the default url |
|
78 |
$this->url = $this->get_post('url'); |
|
79 |
if ($this->redirect_url!='') { |
|
80 |
$this->url = $this->redirect_url; |
|
81 |
} |
|
82 |
if(strlen($this->url) < 2) { |
|
83 |
$this->url = $config_array['DEFAULT_URL']; |
|
84 |
} |
|
85 |
if($this->is_authenticated() == true) { |
|
86 |
// User already logged-in, so redirect to default url |
|
87 |
header('Location: '.$this->url); |
|
88 |
exit(); |
|
89 |
} elseif($this->is_remembered() == true) { |
|
90 |
// User has been "remembered" |
|
91 |
// Get the users password |
|
92 |
$database = new database(); |
|
93 |
$query_details = $database->query("SELECT * FROM ".$this->USERS_TABLE." WHERE user_id = '".$this->get_safe_remember_key()."' LIMIT 1"); |
|
94 |
$fetch_details = $query_details->fetchRow(); |
|
95 |
$this->username = $fetch_details['username']; |
|
96 |
$this->password = $fetch_details['password']; |
|
97 |
// Check if the user exists (authenticate them) |
|
98 |
if($this->authenticate()) { |
|
99 |
// Authentication successful |
|
100 |
header("Location: ".$this->url); |
|
101 |
exit(0); |
|
102 |
} else { |
|
103 |
$this->message = $MESSAGE['LOGIN']['AUTHENTICATION_FAILED']; |
|
104 |
$this->increase_attemps(); |
|
105 |
} |
|
106 |
} elseif($this->username == '' AND $this->password == '') { |
|
107 |
$this->message = $MESSAGE['LOGIN']['BOTH_BLANK']; |
|
108 |
$this->increase_attemps(); |
|
109 |
} elseif($this->username == '') { |
|
110 |
$this->message = $MESSAGE['LOGIN']['USERNAME_BLANK']; |
|
111 |
$this->increase_attemps(); |
|
112 |
} elseif($this->password == '') { |
|
113 |
$this->message = $MESSAGE['LOGIN']['PASSWORD_BLANK']; |
|
114 |
$this->increase_attemps(); |
|
115 |
} elseif($this->username_len < $config_array['MIN_USERNAME_LEN']) { |
|
116 |
$this->message = $MESSAGE['LOGIN']['USERNAME_TOO_SHORT']; |
|
117 |
$this->increase_attemps(); |
|
118 |
} elseif($this->password_len < $config_array['MIN_PASSWORD_LEN']) { |
|
119 |
$this->message = $MESSAGE['LOGIN']['PASSWORD_TOO_SHORT']; |
|
120 |
$this->increase_attemps(); |
|
121 |
} elseif($this->username_len > $config_array['MAX_USERNAME_LEN']) { |
|
122 |
$this->message = $MESSAGE['LOGIN']['USERNAME_TOO_LONG']; |
|
123 |
$this->increase_attemps(); |
|
124 |
} elseif($this->password_len > $config_array['MAX_PASSWORD_LEN']) { |
|
125 |
$this->message = $MESSAGE['LOGIN']['PASSWORD_TOO_LONG']; |
|
126 |
$this->increase_attemps(); |
|
127 |
} else { |
|
128 |
// Check if the user exists (authenticate them) |
|
129 |
$this->password = md5($this->password); |
|
130 |
if($this->authenticate()) { |
|
131 |
// Authentication successful |
|
132 |
//echo $this->url;exit(); |
|
133 |
header("Location: ".$this->url); |
|
134 |
exit(0); |
|
135 |
} else { |
|
136 |
$this->message = $MESSAGE['LOGIN']['AUTHENTICATION_FAILED']; |
|
137 |
$this->increase_attemps(); |
|
138 |
} |
|
139 |
} |
|
140 |
} |
|
141 |
|
|
142 |
// Authenticate the user (check if they exist in the database) |
|
143 |
function authenticate() { |
|
144 |
// Get user information |
|
145 |
$database = new database(); |
|
146 |
$query = "SELECT * FROM ".$this->USERS_TABLE." WHERE username = '".$this->username."' AND password = '".$this->password."' AND active = '1'"; |
|
147 |
$results = $database->query($query); |
|
148 |
$results_array = $results->fetchRow(); |
|
149 |
$num_rows = $results->numRows(); |
|
150 |
if($num_rows) { |
|
151 |
$user_id = $results_array['user_id']; |
|
152 |
$this->user_id = $user_id; |
|
153 |
$_SESSION['USER_ID'] = $user_id; |
|
154 |
$_SESSION['GROUP_ID'] = $results_array['group_id']; |
|
155 |
$_SESSION['GROUPS_ID'] = $results_array['groups_id']; |
|
156 |
$_SESSION['USERNAME'] = $results_array['username']; |
|
157 |
$_SESSION['DISPLAY_NAME'] = $results_array['display_name']; |
|
158 |
$_SESSION['EMAIL'] = $results_array['email']; |
|
159 |
$_SESSION['HOME_FOLDER'] = $results_array['home_folder']; |
|
160 |
// Run remember function if needed |
|
161 |
if($this->remember == true) { |
|
162 |
$this->remember($this->user_id); |
|
163 |
} |
|
164 |
// Set language |
|
165 |
if($results_array['language'] != '') { |
|
166 |
$_SESSION['LANGUAGE'] = $results_array['language']; |
|
167 |
} |
|
168 |
// Set timezone |
|
169 |
if($results_array['timezone'] != '-72000') { |
|
170 |
$_SESSION['TIMEZONE'] = $results_array['timezone']; |
|
171 |
} else { |
|
172 |
// Set a session var so apps can tell user is using default tz |
|
173 |
$_SESSION['USE_DEFAULT_TIMEZONE'] = true; |
|
174 |
} |
|
175 |
// Set date format |
|
176 |
if($results_array['date_format'] != '') { |
|
177 |
$_SESSION['DATE_FORMAT'] = $results_array['date_format']; |
|
178 |
} else { |
|
179 |
// Set a session var so apps can tell user is using default date format |
|
180 |
$_SESSION['USE_DEFAULT_DATE_FORMAT'] = true; |
|
181 |
} |
|
182 |
// Set time format |
|
183 |
if($results_array['time_format'] != '') { |
|
184 |
$_SESSION['TIME_FORMAT'] = $results_array['time_format']; |
|
185 |
} else { |
|
186 |
// Set a session var so apps can tell user is using default time format |
|
187 |
$_SESSION['USE_DEFAULT_TIME_FORMAT'] = true; |
|
188 |
} |
|
189 |
// Get group information |
|
190 |
// $query = "SELECT * FROM ".$this->GROUPS_TABLE." WHERE group_id = '".$this->get_session('GROUP_ID')."'"; |
|
191 |
// $results = $database->query($query); |
|
192 |
// $results_array = $results->fetchRow(); |
|
193 |
// $_SESSION['GROUP_NAME'] = $results_array['name']; |
|
194 |
// // Set system permissions |
|
195 |
// if($results_array['system_permissions'] != '') { |
|
196 |
// $_SESSION['SYSTEM_PERMISSIONS'] = explode(',', $results_array['system_permissions']); |
|
197 |
// } else { |
|
198 |
// $_SESSION['SYSTEM_PERMISSIONS'] = array(); |
|
199 |
// } |
|
200 |
// // Set module permissions |
|
201 |
// if($results_array['module_permissions'] != '') { |
|
202 |
// $_SESSION['MODULE_PERMISSIONS'] = explode(',', $results_array['module_permissions']); |
|
203 |
// } else { |
|
204 |
// $_SESSION['MODULE_PERMISSIONS'] = array(); |
|
205 |
// } |
|
206 |
// // Set template permissions |
|
207 |
// if($results_array['template_permissions'] != '') { |
|
208 |
// $_SESSION['TEMPLATE_PERMISSIONS'] = explode(',', $results_array['template_permissions']); |
|
209 |
// } else { |
|
210 |
// $_SESSION['TEMPLATE_PERMISSIONS'] = array(); |
|
211 |
// } |
|
212 |
|
|
213 |
$_SESSION['SYSTEM_PERMISSIONS'] = array(); |
|
214 |
$_SESSION['MODULE_PERMISSIONS'] = array(); |
|
215 |
$_SESSION['TEMPLATE_PERMISSIONS'] = array(); |
|
216 |
$_SESSION['GROUP_NAME'] = array(); |
|
217 |
|
|
218 |
$first_group = true; |
|
219 |
foreach (explode(",", $this->get_session('GROUPS_ID')) as $cur_group_id) |
|
220 |
{ |
|
221 |
$query = "SELECT * FROM ".$this->GROUPS_TABLE." WHERE group_id = '".$cur_group_id."'"; |
|
222 |
$results = $database->query($query); |
|
223 |
$results_array = $results->fetchRow(); |
|
224 |
$_SESSION['GROUP_NAME'][$cur_group_id] = $results_array['name']; |
|
225 |
// Set system permissions |
|
226 |
if($results_array['system_permissions'] != '') { |
|
227 |
$_SESSION['SYSTEM_PERMISSIONS'] = array_merge($_SESSION['SYSTEM_PERMISSIONS'], explode(',', $results_array['system_permissions'])); |
|
228 |
} |
|
229 |
// Set module permissions |
|
230 |
if($results_array['module_permissions'] != '') { |
|
231 |
if ($first_group) { |
|
232 |
$_SESSION['MODULE_PERMISSIONS'] = explode(',', $results_array['module_permissions']); |
|
233 |
} else { |
|
234 |
$_SESSION['MODULE_PERMISSIONS'] = array_intersect($_SESSION['MODULE_PERMISSIONS'], explode(',', $results_array['module_permissions'])); |
|
235 |
} |
|
236 |
} |
|
237 |
// Set template permissions |
|
238 |
if($results_array['template_permissions'] != '') { |
|
239 |
if ($first_group) { |
|
240 |
$_SESSION['TEMPLATE_PERMISSIONS'] = explode(',', $results_array['template_permissions']); |
|
241 |
} else { |
|
242 |
$_SESSION['TEMPLATE_PERMISSIONS'] = array_intersect($_SESSION['TEMPLATE_PERMISSIONS'], explode(',', $results_array['template_permissions'])); |
|
243 |
} |
|
244 |
} |
|
245 |
$first_group = false; |
|
246 |
} |
|
247 |
|
|
248 |
// Update the users table with current ip and timestamp |
|
249 |
$get_ts = time(); |
|
250 |
$get_ip = $_SERVER['REMOTE_ADDR']; |
|
251 |
$query = "UPDATE ".$this->USERS_TABLE." SET login_when = '$get_ts', login_ip = '$get_ip' WHERE user_id = '$user_id'"; |
|
252 |
$database->query($query); |
|
253 |
} |
|
254 |
// Return if the user exists or not |
|
255 |
return $num_rows; |
|
256 |
} |
|
257 |
|
|
258 |
// Increase the count for login attemps |
|
259 |
function increase_attemps() { |
|
260 |
if(!isset($_SESSION['ATTEMPS'])) { |
|
261 |
$_SESSION['ATTEMPS'] = 0; |
|
262 |
} else { |
|
263 |
$_SESSION['ATTEMPS'] = $this->get_session('ATTEMPS')+1; |
|
264 |
} |
|
265 |
$this->display_login(); |
|
266 |
} |
|
267 |
|
|
268 |
// Function to set a "remembering" cookie for the user |
|
269 |
function remember($user_id) { |
|
270 |
$remember_key = ''; |
|
271 |
// Generate user id to append to the remember key |
|
272 |
$length = 11-strlen($user_id); |
|
273 |
if($length > 0) { |
|
274 |
for($i = 1; $i <= $length; $i++) { |
|
275 |
$remember_key .= '0'; |
|
276 |
} |
|
277 |
} |
|
278 |
// Generate remember key |
|
279 |
$remember_key .= $user_id.'_'; |
|
280 |
$salt = "abchefghjkmnpqrstuvwxyz0123456789"; |
|
281 |
srand((double)microtime()*1000000); |
|
282 |
$i = 0; |
|
283 |
while ($i <= 10) { |
|
284 |
$num = rand() % 33; |
|
285 |
$tmp = substr($salt, $num, 1); |
|
286 |
$remember_key = $remember_key . $tmp; |
|
287 |
$i++; |
|
288 |
} |
|
289 |
$remember_key = $remember_key; |
|
290 |
// Update the remember key in the db |
|
291 |
$database = new database(); |
|
292 |
$database->query("UPDATE ".$this->USERS_TABLE." SET remember_key = '$remember_key' WHERE user_id = '$user_id' LIMIT 1"); |
|
293 |
if($database->is_error()) { |
|
294 |
return false; |
|
295 |
} else { |
|
296 |
// Workout options for the cookie |
|
297 |
$cookie_name = 'REMEMBER_KEY'; |
|
298 |
$cookie_value = $remember_key; |
|
299 |
$cookie_expire = time()+60*60*24*30; |
|
300 |
// Set the cookie |
|
301 |
if(setcookie($cookie_name, $cookie_value, $cookie_expire, '/')) { |
|
302 |
return true; |
|
303 |
} else { |
|
304 |
return false; |
|
305 |
} |
|
306 |
} |
|
307 |
} |
|
308 |
|
|
309 |
// Function to check if a user has been remembered |
|
310 |
function is_remembered() { |
|
311 |
if(isset($_COOKIE['REMEMBER_KEY']) AND $_COOKIE['REMEMBER_KEY'] != '') { |
|
312 |
// Check if the remember key is correct |
|
313 |
$database = new database(); |
|
314 |
$sql = "SELECT `user_id` FROM `" . $this->USERS_TABLE . "` WHERE `remember_key` = '"; |
|
315 |
$sql .= $this->get_safe_remember_key() . "' LIMIT 1"; |
|
316 |
$check_query = $database->query($sql); |
|
317 |
|
|
318 |
if($check_query->numRows() > 0) { |
|
319 |
$check_fetch = $check_query->fetchRow(); |
|
320 |
$user_id = $check_fetch['user_id']; |
|
321 |
// Check the remember key prefix |
|
322 |
$remember_key_prefix = ''; |
|
323 |
$length = 11-strlen($user_id); |
|
324 |
if($length > 0) { |
|
325 |
for($i = 1; $i <= $length; $i++) { |
|
326 |
$remember_key_prefix .= '0'; |
|
327 |
} |
|
328 |
} |
|
329 |
$remember_key_prefix .= $user_id.'_'; |
|
330 |
$length = strlen($remember_key_prefix); |
|
331 |
if(substr($_COOKIE['REMEMBER_KEY'], 0, $length) == $remember_key_prefix) { |
|
332 |
return true; |
|
333 |
} else { |
|
334 |
return false; |
|
335 |
} |
|
336 |
} else { |
|
337 |
return false; |
|
338 |
} |
|
339 |
} else { |
|
340 |
return false; |
|
341 |
} |
|
342 |
} |
|
343 |
|
|
344 |
// Display the login screen |
|
345 |
function display_login() { |
|
346 |
// Get language vars |
|
347 |
global $MESSAGE; |
|
348 |
global $MENU; |
|
349 |
global $TEXT; |
|
350 |
// If attemps more than allowed, warn the user |
|
351 |
if($this->get_session('ATTEMPS') > $this->max_attemps) { |
|
352 |
$this->warn(); |
|
353 |
} |
|
354 |
// Show the login form |
|
355 |
if($this->frontend != true) { |
|
356 |
require_once(WB_PATH.'/include/phplib/template.inc'); |
|
357 |
$template = new Template($this->template_dir); |
|
358 |
$template->set_file('page', $this->template_file); |
|
359 |
$template->set_block('page', 'mainBlock', 'main'); |
|
360 |
if($this->remember_me_option != true) { |
|
361 |
$template->set_var('DISPLAY_REMEMBER_ME', 'display: none;'); |
|
362 |
} else { |
|
363 |
$template->set_var('DISPLAY_REMEMBER_ME', ''); |
|
364 |
} |
|
365 |
$template->set_var(array( |
|
366 |
'ACTION_URL' => $this->login_url, |
|
367 |
'ATTEMPS' => $this->get_session('ATTEMPS'), |
|
368 |
'USERNAME' => $this->username, |
|
369 |
'USERNAME_FIELDNAME' => $this->username_fieldname, |
|
370 |
'PASSWORD_FIELDNAME' => $this->password_fieldname, |
|
371 |
'MESSAGE' => $this->message, |
|
372 |
'INTERFACE_DIR_URL' => ADMIN_URL.'/interface', |
|
373 |
'MAX_USERNAME_LEN' => $this->max_username_len, |
|
374 |
'MAX_PASSWORD_LEN' => $this->max_password_len, |
|
375 |
'WB_URL' => WB_URL, |
|
376 |
'THEME_URL' => THEME_URL, |
|
377 |
'VERSION' => VERSION, |
|
378 |
'REVISION' => REVISION, |
|
379 |
'LANGUAGE' => strtolower(LANGUAGE), |
|
380 |
'FORGOTTEN_DETAILS_APP' => $this->forgotten_details_app, |
|
381 |
'TEXT_FORGOTTEN_DETAILS' => $TEXT['FORGOTTEN_DETAILS'], |
|
382 |
'TEXT_USERNAME' => $TEXT['USERNAME'], |
|
383 |
'TEXT_PASSWORD' => $TEXT['PASSWORD'], |
|
384 |
'TEXT_REMEMBER_ME' => $TEXT['REMEMBER_ME'], |
|
385 |
'TEXT_LOGIN' => $TEXT['LOGIN'], |
|
386 |
'TEXT_HOME' => $TEXT['HOME'], |
|
387 |
'PAGES_DIRECTORY' => PAGES_DIRECTORY, |
|
388 |
'SECTION_LOGIN' => $MENU['LOGIN'] |
|
389 |
) |
|
390 |
); |
|
391 |
if(defined('DEFAULT_CHARSET')) { |
|
392 |
$charset=DEFAULT_CHARSET; |
|
393 |
} else { |
|
394 |
$charset='utf-8'; |
|
395 |
} |
|
396 |
|
|
397 |
$template->set_var('CHARSET', $charset); |
|
398 |
|
|
399 |
|
|
400 |
$template->parse('main', 'mainBlock', false); |
|
401 |
$template->pparse('output', 'page'); |
|
402 |
} |
|
403 |
} |
|
404 |
|
|
405 |
// sanities the REMEMBER_KEY cookie to avoid SQL injection |
|
406 |
function get_safe_remember_key() { |
|
407 |
if (!((strlen($_COOKIE['REMEMBER_KEY']) == 23) && (substr($_COOKIE['REMEMBER_KEY'], 11, 1) == '_'))) return ''; |
|
408 |
// create a clean cookie (XXXXXXXXXXX_YYYYYYYYYYY) where X:= numeric, Y:= hash |
|
409 |
$clean_cookie = sprintf('%011d', (int) substr($_COOKIE['REMEMBER_KEY'], 0, 11)) . substr($_COOKIE['REMEMBER_KEY'], 11); |
|
410 |
return ($clean_cookie == $_COOKIE['REMEMBER_KEY']) ? $this->add_slashes($clean_cookie) : ''; |
|
411 |
} |
|
412 |
|
|
413 |
// Warn user that they have had to many login attemps |
|
414 |
function warn() { |
|
415 |
header('Location: '.$this->warning_url); |
|
416 |
exit(0); |
|
417 |
} |
|
418 |
|
|
419 |
} |
|
420 |
|
|
431 | 421 |
?> |
432 | 422 |
Also available in: Unified diff
Branch 2.8.1 merged back into Trunk