Project

General

Profile

1 1189 Luisehahne
<?php
2 1235 Luisehahne
/**
3
 *
4 1269 Luisehahne
 * @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 1270 Luisehahne
 * @filesource		$HeadURL$
15
 * @lastmodified    $Date$
16 1235 Luisehahne
 *
17 1277 Luisehahne
 */
18 1189 Luisehahne
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 1277 Luisehahne
// Get WB version
30
require_once(ADMIN_PATH.'/interface/version.php');
31 1189 Luisehahne
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 1310 Luisehahne
65
		$this->username = htmlspecialchars (strtolower($this->get_post($username_fieldname)), ENT_QUOTES);
66 1189 Luisehahne
		$this->password = $this->get_post($password_fieldname);
67
		// Figure out if the "remember me" option has been checked
68
		if($this->get_post('remember') == 'true') {
69
			$this->remember = $this->get_post('remember');
70
		} else {
71
			$this->remember = false;
72
		}
73
		// Get the length of the supplied username and password
74
		if($this->get_post($username_fieldname) != '') {
75
			$this->username_len = strlen($this->username);
76
			$this->password_len = strlen($this->password);
77
		}
78
		// If the url is blank, set it to the default url
79
		$this->url = $this->get_post('url');
80
		if ($this->redirect_url!='') {
81
			$this->url = $this->redirect_url;
82
		}
83
		if(strlen($this->url) < 2) {
84
			$this->url = $config_array['DEFAULT_URL'];
85
		}
86
		if($this->is_authenticated() == true) {
87
			// User already logged-in, so redirect to default url
88
			header('Location: '.$this->url);
89
			exit();
90
		} elseif($this->is_remembered() == true) {
91
			// User has been "remembered"
92
			// Get the users password
93
			$database = new database();
94
			$query_details = $database->query("SELECT * FROM ".$this->USERS_TABLE." WHERE user_id = '".$this->get_safe_remember_key()."' LIMIT 1");
95
			$fetch_details = $query_details->fetchRow();
96
			$this->username = $fetch_details['username'];
97
			$this->password = $fetch_details['password'];
98
			// Check if the user exists (authenticate them)
99
			if($this->authenticate()) {
100
				// Authentication successful
101
				header("Location: ".$this->url);
102
				exit(0);
103
			} else {
104
				$this->message = $MESSAGE['LOGIN']['AUTHENTICATION_FAILED'];
105
				$this->increase_attemps();
106
			}
107
		} elseif($this->username == '' AND $this->password == '') {
108
			$this->message = $MESSAGE['LOGIN']['BOTH_BLANK'];
109
			$this->increase_attemps();
110
		} elseif($this->username == '') {
111
			$this->message = $MESSAGE['LOGIN']['USERNAME_BLANK'];
112
			$this->increase_attemps();
113
		} elseif($this->password == '') {
114
			$this->message = $MESSAGE['LOGIN']['PASSWORD_BLANK'];
115
			$this->increase_attemps();
116
		} elseif($this->username_len < $config_array['MIN_USERNAME_LEN']) {
117
			$this->message = $MESSAGE['LOGIN']['USERNAME_TOO_SHORT'];
118
			$this->increase_attemps();
119
		} elseif($this->password_len < $config_array['MIN_PASSWORD_LEN']) {
120
			$this->message = $MESSAGE['LOGIN']['PASSWORD_TOO_SHORT'];
121
			$this->increase_attemps();
122
		} elseif($this->username_len > $config_array['MAX_USERNAME_LEN']) {
123
			$this->message = $MESSAGE['LOGIN']['USERNAME_TOO_LONG'];
124
			$this->increase_attemps();
125
		} elseif($this->password_len > $config_array['MAX_PASSWORD_LEN']) {
126
			$this->message = $MESSAGE['LOGIN']['PASSWORD_TOO_LONG'];
127
			$this->increase_attemps();
128
		} else {
129
			// Check if the user exists (authenticate them)
130
			$this->password = md5($this->password);
131
			if($this->authenticate()) {
132
				// Authentication successful
133
				//echo $this->url;exit();
134
				header("Location: ".$this->url);
135
				exit(0);
136
			} else {
137
				$this->message = $MESSAGE['LOGIN']['AUTHENTICATION_FAILED'];
138
				$this->increase_attemps();
139
			}
140
		}
141
	}
142
143
	// Authenticate the user (check if they exist in the database)
144
	function authenticate() {
145
		// Get user information
146
		$database = new database();
147
		$query = "SELECT * FROM ".$this->USERS_TABLE." WHERE username = '".$this->username."' AND password = '".$this->password."' AND active = '1'";
148
		$results = $database->query($query);
149
		$results_array = $results->fetchRow();
150
		$num_rows = $results->numRows();
151
		if($num_rows) {
152
			$user_id = $results_array['user_id'];
153
			$this->user_id = $user_id;
154
			$_SESSION['USER_ID'] = $user_id;
155
			$_SESSION['GROUP_ID'] = $results_array['group_id'];
156
			$_SESSION['GROUPS_ID'] = $results_array['groups_id'];
157
			$_SESSION['USERNAME'] = $results_array['username'];
158
			$_SESSION['DISPLAY_NAME'] = $results_array['display_name'];
159
			$_SESSION['EMAIL'] = $results_array['email'];
160
			$_SESSION['HOME_FOLDER'] = $results_array['home_folder'];
161
			// Run remember function if needed
162
			if($this->remember == true) {
163
				$this->remember($this->user_id);
164
			}
165
			// Set language
166
			if($results_array['language'] != '') {
167
				$_SESSION['LANGUAGE'] = $results_array['language'];
168
			}
169
			// Set timezone
170
			if($results_array['timezone'] != '-72000') {
171
				$_SESSION['TIMEZONE'] = $results_array['timezone'];
172
			} else {
173
				// Set a session var so apps can tell user is using default tz
174
				$_SESSION['USE_DEFAULT_TIMEZONE'] = true;
175
			}
176
			// Set date format
177
			if($results_array['date_format'] != '') {
178
				$_SESSION['DATE_FORMAT'] = $results_array['date_format'];
179
			} else {
180
				// Set a session var so apps can tell user is using default date format
181
				$_SESSION['USE_DEFAULT_DATE_FORMAT'] = true;
182
			}
183
			// Set time format
184
			if($results_array['time_format'] != '') {
185
				$_SESSION['TIME_FORMAT'] = $results_array['time_format'];
186
			} else {
187
				// Set a session var so apps can tell user is using default time format
188
				$_SESSION['USE_DEFAULT_TIME_FORMAT'] = true;
189
			}
190
			// Get group information
191
//			$query = "SELECT * FROM ".$this->GROUPS_TABLE." WHERE group_id = '".$this->get_session('GROUP_ID')."'";
192
//			$results = $database->query($query);
193
//			$results_array = $results->fetchRow();
194
//			$_SESSION['GROUP_NAME'] = $results_array['name'];
195
//			// Set system permissions
196
//			if($results_array['system_permissions'] != '') {
197
//				$_SESSION['SYSTEM_PERMISSIONS'] = explode(',', $results_array['system_permissions']);
198
//			} else {
199
//				$_SESSION['SYSTEM_PERMISSIONS'] = array();
200
//			}
201
//			// Set module permissions
202
//			if($results_array['module_permissions'] != '') {
203
//				$_SESSION['MODULE_PERMISSIONS'] = explode(',', $results_array['module_permissions']);
204
//			} else {
205
//				$_SESSION['MODULE_PERMISSIONS'] = array();
206
//			}
207
//			// Set template permissions
208
//			if($results_array['template_permissions'] != '') {
209
//				$_SESSION['TEMPLATE_PERMISSIONS'] = explode(',', $results_array['template_permissions']);
210
//			} else {
211
//				$_SESSION['TEMPLATE_PERMISSIONS'] = array();
212
//			}
213
214
			$_SESSION['SYSTEM_PERMISSIONS'] = array();
215
			$_SESSION['MODULE_PERMISSIONS'] = array();
216
			$_SESSION['TEMPLATE_PERMISSIONS'] = array();
217
			$_SESSION['GROUP_NAME'] = array();
218
219
			$first_group = true;
220 1192 Luisehahne
			foreach (explode(",", $this->get_session('GROUPS_ID')) as $cur_group_id)
221
            {
222 1189 Luisehahne
				$query = "SELECT * FROM ".$this->GROUPS_TABLE." WHERE group_id = '".$cur_group_id."'";
223
				$results = $database->query($query);
224
				$results_array = $results->fetchRow();
225
				$_SESSION['GROUP_NAME'][$cur_group_id] = $results_array['name'];
226
				// Set system permissions
227
				if($results_array['system_permissions'] != '') {
228
					$_SESSION['SYSTEM_PERMISSIONS'] = array_merge($_SESSION['SYSTEM_PERMISSIONS'], explode(',', $results_array['system_permissions']));
229
				}
230
				// Set module permissions
231
				if($results_array['module_permissions'] != '') {
232
					if ($first_group) {
233
          	$_SESSION['MODULE_PERMISSIONS'] = explode(',', $results_array['module_permissions']);
234
          } else {
235
          	$_SESSION['MODULE_PERMISSIONS'] = array_intersect($_SESSION['MODULE_PERMISSIONS'], explode(',', $results_array['module_permissions']));
236
					}
237
				}
238
				// Set template permissions
239
				if($results_array['template_permissions'] != '') {
240
					if ($first_group) {
241
          	$_SESSION['TEMPLATE_PERMISSIONS'] = explode(',', $results_array['template_permissions']);
242
          } else {
243
          	$_SESSION['TEMPLATE_PERMISSIONS'] = array_intersect($_SESSION['TEMPLATE_PERMISSIONS'], explode(',', $results_array['template_permissions']));
244
					}
245
				}
246
				$first_group = false;
247
			}
248
249
			// Update the users table with current ip and timestamp
250
			$get_ts = time();
251
			$get_ip = $_SERVER['REMOTE_ADDR'];
252
			$query = "UPDATE ".$this->USERS_TABLE." SET login_when = '$get_ts', login_ip = '$get_ip' WHERE user_id = '$user_id'";
253
			$database->query($query);
254
		}
255
		// Return if the user exists or not
256
		return $num_rows;
257
	}
258
259
	// Increase the count for login attemps
260
	function increase_attemps() {
261
		if(!isset($_SESSION['ATTEMPS'])) {
262
			$_SESSION['ATTEMPS'] = 0;
263
		} else {
264
			$_SESSION['ATTEMPS'] = $this->get_session('ATTEMPS')+1;
265
		}
266
		$this->display_login();
267
	}
268
269
	// Function to set a "remembering" cookie for the user
270
	function remember($user_id) {
271
		$remember_key = '';
272
		// Generate user id to append to the remember key
273
		$length = 11-strlen($user_id);
274
		if($length > 0) {
275
			for($i = 1; $i <= $length; $i++) {
276
				$remember_key .= '0';
277
			}
278
		}
279
		// Generate remember key
280
		$remember_key .= $user_id.'_';
281
		$salt = "abchefghjkmnpqrstuvwxyz0123456789";
282
		srand((double)microtime()*1000000);
283
		$i = 0;
284
		while ($i <= 10) {
285
			$num = rand() % 33;
286
			$tmp = substr($salt, $num, 1);
287
			$remember_key = $remember_key . $tmp;
288
			$i++;
289
		}
290
		$remember_key = $remember_key;
291
		// Update the remember key in the db
292
		$database = new database();
293
		$database->query("UPDATE ".$this->USERS_TABLE." SET remember_key = '$remember_key' WHERE user_id = '$user_id' LIMIT 1");
294
		if($database->is_error()) {
295
			return false;
296
		} else {
297
			// Workout options for the cookie
298
			$cookie_name = 'REMEMBER_KEY';
299
			$cookie_value = $remember_key;
300
			$cookie_expire = time()+60*60*24*30;
301
			// Set the cookie
302
			if(setcookie($cookie_name, $cookie_value, $cookie_expire, '/')) {
303
				return true;
304
			} else {
305
				return false;
306
			}
307
		}
308
	}
309
310
	// Function to check if a user has been remembered
311
	function is_remembered() {
312
		if(isset($_COOKIE['REMEMBER_KEY']) AND $_COOKIE['REMEMBER_KEY'] != '') {
313
			// Check if the remember key is correct
314
			$database = new database();
315
			$sql = "SELECT `user_id` FROM `" . $this->USERS_TABLE . "` WHERE `remember_key` = '";
316
			$sql .= $this->get_safe_remember_key() . "' LIMIT 1";
317
			$check_query = $database->query($sql);
318
319
			if($check_query->numRows() > 0) {
320
				$check_fetch = $check_query->fetchRow();
321
				$user_id = $check_fetch['user_id'];
322
				// Check the remember key prefix
323
				$remember_key_prefix = '';
324
				$length = 11-strlen($user_id);
325
				if($length > 0) {
326
					for($i = 1; $i <= $length; $i++) {
327
						$remember_key_prefix .= '0';
328
					}
329
				}
330
				$remember_key_prefix .= $user_id.'_';
331
				$length = strlen($remember_key_prefix);
332
				if(substr($_COOKIE['REMEMBER_KEY'], 0, $length) == $remember_key_prefix) {
333
					return true;
334
				} else {
335
					return false;
336
				}
337
			} else {
338
				return false;
339
			}
340
		} else {
341
			return false;
342
		}
343
	}
344 1310 Luisehahne
345 1189 Luisehahne
	// Display the login screen
346
	function display_login() {
347
		// Get language vars
348
		global $MESSAGE;
349
		global $MENU;
350
		global $TEXT;
351
		// If attemps more than allowed, warn the user
352
		if($this->get_session('ATTEMPS') > $this->max_attemps) {
353
			$this->warn();
354
		}
355
		// Show the login form
356
		if($this->frontend != true) {
357
			require_once(WB_PATH.'/include/phplib/template.inc');
358
			$template = new Template($this->template_dir);
359
			$template->set_file('page', $this->template_file);
360
			$template->set_block('page', 'mainBlock', 'main');
361
			if($this->remember_me_option != true) {
362 1235 Luisehahne
				$template->set_var('DISPLAY_REMEMBER_ME', 'display: none;');
363 1189 Luisehahne
			} else {
364
				$template->set_var('DISPLAY_REMEMBER_ME', '');
365
			}
366
			$template->set_var(array(
367
											'ACTION_URL' => $this->login_url,
368
											'ATTEMPS' => $this->get_session('ATTEMPS'),
369
											'USERNAME' => $this->username,
370
											'USERNAME_FIELDNAME' => $this->username_fieldname,
371
											'PASSWORD_FIELDNAME' => $this->password_fieldname,
372
											'MESSAGE' => $this->message,
373
											'INTERFACE_DIR_URL' =>  ADMIN_URL.'/interface',
374
											'MAX_USERNAME_LEN' => $this->max_username_len,
375
											'MAX_PASSWORD_LEN' => $this->max_password_len,
376
											'WB_URL' => WB_URL,
377
											'THEME_URL' => THEME_URL,
378 1277 Luisehahne
                                            'VERSION' => VERSION,
379
                                            'REVISION' => REVISION,
380 1189 Luisehahne
											'LANGUAGE' => strtolower(LANGUAGE),
381
											'FORGOTTEN_DETAILS_APP' => $this->forgotten_details_app,
382
											'TEXT_FORGOTTEN_DETAILS' => $TEXT['FORGOTTEN_DETAILS'],
383
											'TEXT_USERNAME' => $TEXT['USERNAME'],
384
											'TEXT_PASSWORD' => $TEXT['PASSWORD'],
385
											'TEXT_REMEMBER_ME' => $TEXT['REMEMBER_ME'],
386
											'TEXT_LOGIN' => $TEXT['LOGIN'],
387
											'TEXT_HOME' => $TEXT['HOME'],
388
											'PAGES_DIRECTORY' => PAGES_DIRECTORY,
389
											'SECTION_LOGIN' => $MENU['LOGIN']
390
											)
391
									);
392
			if(defined('DEFAULT_CHARSET')) {
393
				$charset=DEFAULT_CHARSET;
394
			} else {
395
				$charset='utf-8';
396
			}
397
398
			$template->set_var('CHARSET', $charset);
399
400
401
			$template->parse('main', 'mainBlock', false);
402
			$template->pparse('output', 'page');
403
		}
404
	}
405
406
	// sanities the REMEMBER_KEY cookie to avoid SQL injection
407
	function get_safe_remember_key() {
408
		if (!((strlen($_COOKIE['REMEMBER_KEY']) == 23) && (substr($_COOKIE['REMEMBER_KEY'], 11, 1) == '_'))) return '';
409
		// create a clean cookie (XXXXXXXXXXX_YYYYYYYYYYY) where X:= numeric, Y:= hash
410
		$clean_cookie = sprintf('%011d', (int) substr($_COOKIE['REMEMBER_KEY'], 0, 11)) . substr($_COOKIE['REMEMBER_KEY'], 11);
411
		return ($clean_cookie == $_COOKIE['REMEMBER_KEY']) ? $this->add_slashes($clean_cookie) : '';
412
	}
413
414
	// Warn user that they have had to many login attemps
415
	function warn() {
416
		header('Location: '.$this->warning_url);
417
		exit(0);
418
	}
419
420
}
421
422 239 stefan
?>