Project

General

Profile

1 4 ryan
<?php
2
3
// $Id: class.login.php,v 1.7 2005/04/02 06:25:54 rdjurovich Exp $
4
5
/*
6
7
 Website Baker Project <http://www.websitebaker.org/>
8
 Copyright (C) 2004-2005, 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_PATH')) { exit('Direct access to this file is not allowed'); }
36
37
define('LOGIN_CLASS_LOADED', true);
38
39
// Load the other required class files if they are not already loaded
40
require_once(WB_PATH."/framework/class.admin.php");
41
42
class login extends admin {
43
	function login($config_array) {
44
		// Get language vars
45
		global $MESSAGE;
46 5 stefan
		$this->wb();
47 4 ryan
		// Get configuration values
48
		$this->USERS_TABLE = $config_array['USERS_TABLE'];
49
		$this->GROUPS_TABLE = $config_array['GROUPS_TABLE'];
50
		$this->username_fieldname = $config_array['USERNAME_FIELDNAME'];
51
		$this->password_fieldname = $config_array['PASSWORD_FIELDNAME'];
52
		$this->remember_me_option = $config_array['REMEMBER_ME_OPTION'];
53
		$this->max_attemps = $config_array['MAX_ATTEMPS'];
54
		$this->warning_url = $config_array['WARNING_URL'];
55
		$this->login_url = $config_array['LOGIN_URL'];
56
		$this->template_dir = $config_array['TEMPLATE_DIR'];
57
		$this->template_file = $config_array['TEMPLATE_FILE'];
58
		$this->frontend = $config_array['FRONTEND'];
59
		$this->forgotten_details_app = $config_array['FORGOTTEN_DETAILS_APP'];
60
		$this->max_username_len = $config_array['MAX_USERNAME_LEN'];
61
		$this->max_password_len = $config_array['MAX_PASSWORD_LEN'];
62
		// Get the supplied username and password
63
		if ($this->get_post('username_fieldname') != ''){
64
			$username_fieldname = $this->get_post('username_fieldname');
65
			$password_fieldname = $this->get_post('password_fieldname');
66
		} else {
67
			$username_fieldname = 'username';
68
			$password_fieldname = 'password';
69
		}
70
		$this->username = strtolower($this->get_post($username_fieldname));
71
		$this->password = $this->get_post($password_fieldname);
72
		// Figure out if the "remember me" option has been checked
73
		if($this->get_post('remember') == 'true') {
74
			$this->remember = $this->get_post('remember');
75
		} else {
76
			$this->remember = false;
77
		}
78
		// Get the length of the supplied username and password
79
		if($this->get_post($username_fieldname) != '') {
80
			$this->username_len = strlen($this->username);
81
			$this->password_len = strlen($this->password);
82
		}
83
		// If the url is blank, set it to the default url
84
		$this->url = $this->get_post('url');
85
		if(strlen($this->url) < 2) {
86
			$this->url = $config_array['DEFAULT_URL'];
87
		}
88
		// Login the user
89
		if($this->is_authenticated() == true) {
90
			// User already logged-in, so redirect to default url
91
			header('Location: '.$this->url);
92
			exit();
93
		} elseif(!isset($username_fieldname) AND $this->is_remembered() == true) {
94
			// User has been "remembered"
95
			// Get the users password
96
			$database = new database();
97
			$query_details = $database->query("SELECT * FROM ".$this->USERS_TABLE." WHERE user_id = '".substr($_COOKIE['REMEMBER_KEY'], 0, 11)."' LIMIT 1");
98
			$fetch_details = $query_details->fetchRow();
99
			$this->username = $fetch_details['username'];
100
			$this->password = $fetch_details['password'];
101
			// Check if the user exists (authenticate them)
102
			if($this->authenticate()) {
103
				// Authentication successful
104
				header("Location: ".$this->url);
105
			} else {
106
				$this->message = $MESSAGE['LOGIN']['AUTHENTICATION_FAILED'];
107
				$this->increase_attemps();
108
			}
109
		} elseif($this->username == '' AND $this->password == '') {
110
			$this->message = $MESSAGE['LOGIN']['BOTH_BLANK'];
111
			$this->increase_attemps();
112
		} elseif($this->username == '') {
113
			$this->message = $MESSAGE['LOGIN']['USERNAME_BLANK'];
114
			$this->increase_attemps();
115
		} elseif($this->password == '') {
116
			$this->message = $MESSAGE['LOGIN']['PASSWORD_BLANK'];
117
			$this->increase_attemps();
118
		} elseif($this->username_len < $config_array['MIN_USERNAME_LEN']) {
119
			$this->message = $MESSAGE['LOGIN']['USERNAME_TOO_SHORT'];
120
			$this->increase_attemps();
121
		} elseif($this->password_len < $config_array['MIN_PASSWORD_LEN']) {
122
			$this->message = $MESSAGE['LOGIN']['PASSWORD_TOO_SHORT'];
123
			$this->increase_attemps();
124
		} elseif($this->username_len > $config_array['MAX_USERNAME_LEN']) {
125
			$this->message = $MESSAGE['LOGIN']['USERNAME_TOO_LONG'];
126
			$this->increase_attemps();
127
		} elseif($this->password_len > $config_array['MAX_PASSWORD_LEN']) {
128
			$this->message = $MESSAGE['LOGIN']['PASSWORD_TOO_LONG'];
129
			$this->increase_attemps();
130
		} else {
131
			// Check if the user exists (authenticate them)
132
			$this->password = md5($this->password);
133
			if($this->authenticate()) {
134
				// Authentication successful
135
				header("Location: ".$this->url);
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['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
			// Update the users table with current ip and timestamp
213
			$get_ts = mktime();
214
			$get_ip = $_SERVER['REMOTE_ADDR'];
215
			$query = "UPDATE ".$this->USERS_TABLE." SET login_when = '$get_ts', login_ip = '$get_ip' WHERE user_id = '$user_id'";
216
			$database->query($query);
217
		}
218
		// Return if the user exists or not
219
		return $num_rows;
220
	}
221
222
	// Increase the count for login attemps
223
	function increase_attemps() {
224
		if(!isset($_SESSION['ATTEMPS'])) {
225
			$_SESSION['ATTEMPS'] = 0;
226
		} else {
227
			$_SESSION['ATTEMPS'] = $this->get_session('ATTEMPS')+1;
228
		}
229
		$this->display_login();
230
	}
231
232
	// Function to set a "remembering" cookie for the user
233
	function remember($user_id) {
234
		$remember_key = '';
235
		// Generate user id to append to the remember key
236
		$length = 11-strlen($user_id);
237
		if($length > 0) {
238
			for($i = 1; $i <= $length; $i++) {
239
				$remember_key .= '0';
240
			}
241
		}
242
		// Generate remember key
243
		$remember_key .= $user_id.'_';
244
		$salt = "abchefghjkmnpqrstuvwxyz0123456789";
245
		srand((double)microtime()*1000000);
246
		$i = 0;
247
		while ($i <= 10) {
248
			$num = rand() % 33;
249
			$tmp = substr($salt, $num, 1);
250
			$remember_key = $remember_key . $tmp;
251
			$i++;
252
		}
253
		$remember_key = $remember_key;
254
		// Update the remember key in the db
255
		$database = new database();
256
		$database->query("UPDATE ".$this->USERS_TABLE." SET remember_key = '$remember_key' WHERE user_id = '$user_id' LIMIT 1");
257
		if($database->is_error()) {
258
			return false;
259
		} else {
260
			// Workout options for the cookie
261
			$cookie_name = 'REMEMBER_KEY';
262
			$cookie_value = $remember_key;
263
			$cookie_expire = time()+60*60*24*30;
264
			// Set the cookie
265
			if(setcookie($cookie_name, $cookie_value, $cookie_expire, '/')) {
266
				return true;
267
			} else {
268
				return false;
269
			}
270
		}
271
	}
272
273
	// Function to check if a user has been remembered
274
	function is_remembered() {
275
		if(isset($_COOKIE['REMEMBER_KEY']) AND $_COOKIE['REMEMBER_KEY'] != '') {
276
			// Check if the remember key is correct
277
			$database = new database();
278
			$check_query = $database->query("SELECT user_id FROM ".$this->USERS_TABLE." WHERE remember_key = '".$_COOKIE['REMEMBER_KEY']."' LIMIT 1");
279
			if($check_query->numRows() > 0) {
280
				$check_fetch = $check_query->fetchRow();
281
				$user_id = $check_fetch['user_id'];
282
				// Check the remember key prefix
283
				$remember_key_prefix = '';
284
				$length = 11-strlen($user_id);
285
				if($length > 0) {
286
					for($i = 1; $i <= $length; $i++) {
287
						$remember_key_prefix .= '0';
288
					}
289
				}
290
				$remember_key_prefix .= $user_id.'_';
291
				$length = strlen($remember_key_prefix);
292
				if(substr($_COOKIE['REMEMBER_KEY'], 0, $length) == $remember_key_prefix) {
293
					return true;
294
				} else {
295
					return false;
296
				}
297
			} else {
298
				return false;
299
			}
300
		} else {
301
			return false;
302
		}
303
	}
304
305
	// Display the login screen
306
	function display_login() {
307
		// Get language vars
308
		global $MESSAGE;
309
		global $MENU;
310
		global $TEXT;
311
		// If attemps more than allowed, warn the user
312
		if($this->get_session('ATTEMPS') > $this->max_attemps) {
313
			$this->warn();
314
		}
315
		// Show the login form
316
		if($this->frontend != true) {
317
			require_once(WB_PATH.'/include/phplib/template.inc');
318
			$template = new Template($this->template_dir);
319
			$template->set_file('page', $this->template_file);
320
			$template->set_block('page', 'mainBlock', 'main');
321
			if($this->remember_me_option != true) {
322
				$template->set_var('DISPLAY_REMEMBER_ME', 'none');
323
			} else {
324
				$template->set_var('DISPLAY_REMEMBER_ME', '');
325
			}
326
			$template->set_var(array(
327
											'ACTION_URL' => $this->login_url,
328
											'ATTEMPS' => $this->get_session('ATTEMPS'),
329
											'USERNAME' => $this->username,
330
											'USERNAME_FIELDNAME' => $this->username_fieldname,
331
											'PASSWORD_FIELDNAME' => $this->password_fieldname,
332
											'MESSAGE' => $this->message,
333
											'INTERFACE_DIR_URL' =>  ADMIN_URL.'/interface',
334
											'MAX_USERNAME_LEN' => $this->max_username_len,
335
											'MAX_PASSWORD_LEN' => $this->max_password_len,
336
											'WB_URL' => WB_URL,
337
											'FORGOTTEN_DETAILS_APP' => $this->forgotten_details_app,
338
											'TEXT_FORGOTTEN_DETAILS' => $TEXT['FORGOTTEN_DETAILS'],
339
											'TEXT_USERNAME' => $TEXT['USERNAME'],
340
											'TEXT_PASSWORD' => $TEXT['PASSWORD'],
341
											'TEXT_REMEMBER_ME' => $TEXT['REMEMBER_ME'],
342
											'TEXT_LOGIN' => $TEXT['LOGIN'],
343
											'TEXT_HOME' => $TEXT['HOME'],
344
											'PAGES_DIRECTORY' => PAGES_DIRECTORY,
345
											'SECTION_LOGIN' => $MENU['LOGIN']
346
											)
347
									);
348
			$template->parse('main', 'mainBlock', false);
349
			$template->pparse('output', 'page');
350
		}
351
	}
352
353
	// Warn user that they have had to many login attemps
354
	function warn() {
355
		header('Location: '.$this->warning_url);
356
	}
357
358
}
359
360 5 stefan
?>