Project

General

Profile

1
<?php
2

    
3
// $Id: class.login.php 19 2005-09-04 23:18:58Z stefan $
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_URL')) {
36
	header('Location: ../index.php');
37
}
38

    
39
define('LOGIN_CLASS_LOADED', true);
40

    
41
// Load the other required class files if they are not already loaded
42
require_once(WB_PATH."/framework/class.admin.php");
43

    
44
class login extends admin {
45
	function login($config_array) {
46
		// Get language vars
47
		global $MESSAGE;
48
		$this->wb();
49
		// Get configuration values
50
		$this->USERS_TABLE = $config_array['USERS_TABLE'];
51
		$this->GROUPS_TABLE = $config_array['GROUPS_TABLE'];
52
		$this->username_fieldname = $config_array['USERNAME_FIELDNAME'];
53
		$this->password_fieldname = $config_array['PASSWORD_FIELDNAME'];
54
		$this->remember_me_option = $config_array['REMEMBER_ME_OPTION'];
55
		$this->max_attemps = $config_array['MAX_ATTEMPS'];
56
		$this->warning_url = $config_array['WARNING_URL'];
57
		$this->login_url = $config_array['LOGIN_URL'];
58
		$this->template_dir = $config_array['TEMPLATE_DIR'];
59
		$this->template_file = $config_array['TEMPLATE_FILE'];
60
		$this->frontend = $config_array['FRONTEND'];
61
		$this->forgotten_details_app = $config_array['FORGOTTEN_DETAILS_APP'];
62
		$this->max_username_len = $config_array['MAX_USERNAME_LEN'];
63
		$this->max_password_len = $config_array['MAX_PASSWORD_LEN'];
64
		// Get the supplied username and password
65
		if ($this->get_post('username_fieldname') != ''){
66
			$username_fieldname = $this->get_post('username_fieldname');
67
			$password_fieldname = $this->get_post('password_fieldname');
68
		} else {
69
			$username_fieldname = 'username';
70
			$password_fieldname = 'password';
71
		}
72
		$this->username = strtolower($this->get_post($username_fieldname));
73
		$this->password = $this->get_post($password_fieldname);
74
		// Figure out if the "remember me" option has been checked
75
		if($this->get_post('remember') == 'true') {
76
			$this->remember = $this->get_post('remember');
77
		} else {
78
			$this->remember = false;
79
		}
80
		// Get the length of the supplied username and password
81
		if($this->get_post($username_fieldname) != '') {
82
			$this->username_len = strlen($this->username);
83
			$this->password_len = strlen($this->password);
84
		}
85
		// If the url is blank, set it to the default url
86
		$this->url = $this->get_post('url');
87
		if(strlen($this->url) < 2) {
88
			$this->url = $config_array['DEFAULT_URL'];
89
		}
90
		// Login the user
91
		if($this->is_authenticated() == true) {
92
			// User already logged-in, so redirect to default url
93
			header('Location: '.$this->url);
94
			exit();
95
		} elseif(!isset($username_fieldname) AND $this->is_remembered() == true) {
96
			// User has been "remembered"
97
			// Get the users password
98
			$database = new database();
99
			$query_details = $database->query("SELECT * FROM ".$this->USERS_TABLE." WHERE user_id = '".substr($_COOKIE['REMEMBER_KEY'], 0, 11)."' LIMIT 1");
100
			$fetch_details = $query_details->fetchRow();
101
			$this->username = $fetch_details['username'];
102
			$this->password = $fetch_details['password'];
103
			// Check if the user exists (authenticate them)
104
			if($this->authenticate()) {
105
				// Authentication successful
106
				header("Location: ".$this->url);
107
			} else {
108
				$this->message = $MESSAGE['LOGIN']['AUTHENTICATION_FAILED'];
109
				$this->increase_attemps();
110
			}
111
		} elseif($this->username == '' AND $this->password == '') {
112
			$this->message = $MESSAGE['LOGIN']['BOTH_BLANK'];
113
			$this->increase_attemps();
114
		} elseif($this->username == '') {
115
			$this->message = $MESSAGE['LOGIN']['USERNAME_BLANK'];
116
			$this->increase_attemps();
117
		} elseif($this->password == '') {
118
			$this->message = $MESSAGE['LOGIN']['PASSWORD_BLANK'];
119
			$this->increase_attemps();
120
		} elseif($this->username_len < $config_array['MIN_USERNAME_LEN']) {
121
			$this->message = $MESSAGE['LOGIN']['USERNAME_TOO_SHORT'];
122
			$this->increase_attemps();
123
		} elseif($this->password_len < $config_array['MIN_PASSWORD_LEN']) {
124
			$this->message = $MESSAGE['LOGIN']['PASSWORD_TOO_SHORT'];
125
			$this->increase_attemps();
126
		} elseif($this->username_len > $config_array['MAX_USERNAME_LEN']) {
127
			$this->message = $MESSAGE['LOGIN']['USERNAME_TOO_LONG'];
128
			$this->increase_attemps();
129
		} elseif($this->password_len > $config_array['MAX_PASSWORD_LEN']) {
130
			$this->message = $MESSAGE['LOGIN']['PASSWORD_TOO_LONG'];
131
			$this->increase_attemps();
132
		} else {
133
			// Check if the user exists (authenticate them)
134
			$this->password = md5($this->password);
135
			if($this->authenticate()) {
136
				// Authentication successful
137
				header("Location: ".$this->url);
138
			} else {
139
				$this->message = $MESSAGE['LOGIN']['AUTHENTICATION_FAILED'];
140
				$this->increase_attemps();
141
			}
142
		}
143
	}
144
	
145
	// Authenticate the user (check if they exist in the database)
146
	function authenticate() {
147
		// Get user information
148
		$database = new database();
149
		$query = "SELECT * FROM ".$this->USERS_TABLE." WHERE username = '".$this->username."' AND password = '".$this->password."' AND active = '1'";
150
		$results = $database->query($query);
151
		$results_array = $results->fetchRow();
152
		$num_rows = $results->numRows();
153
		if($num_rows) {
154
			$user_id = $results_array['user_id'];
155
			$this->user_id = $user_id;
156
			$_SESSION['USER_ID'] = $user_id;
157
			$_SESSION['GROUP_ID'] = $results_array['group_id'];
158
			$_SESSION['USERNAME'] = $results_array['username'];
159
			$_SESSION['DISPLAY_NAME'] = $results_array['display_name'];
160
			$_SESSION['EMAIL'] = $results_array['email'];
161
			$_SESSION['HOME_FOLDER'] = $results_array['home_folder'];
162
			// Run remember function if needed
163
			if($this->remember == true) {
164
				$this->remember($this->user_id);
165
			}
166
			// Set language
167
			if($results_array['language'] != '') {
168
				$_SESSION['LANGUAGE'] = $results_array['language'];
169
			}
170
			// Set timezone
171
			if($results_array['timezone'] != '-72000') {
172
				$_SESSION['TIMEZONE'] = $results_array['timezone'];
173
			} else {
174
				// Set a session var so apps can tell user is using default tz
175
				$_SESSION['USE_DEFAULT_TIMEZONE'] = true;
176
			}
177
			// Set date format
178
			if($results_array['date_format'] != '') {
179
				$_SESSION['DATE_FORMAT'] = $results_array['date_format'];
180
			} else {
181
				// Set a session var so apps can tell user is using default date format
182
				$_SESSION['USE_DEFAULT_DATE_FORMAT'] = true;
183
			}
184
			// Set time format
185
			if($results_array['time_format'] != '') {
186
				$_SESSION['TIME_FORMAT'] = $results_array['time_format'];
187
			} else {
188
				// Set a session var so apps can tell user is using default time format
189
				$_SESSION['USE_DEFAULT_TIME_FORMAT'] = true;
190
			}
191
			// Get group information
192
			$query = "SELECT * FROM ".$this->GROUPS_TABLE." WHERE group_id = '".$this->get_session('GROUP_ID')."'";
193
			$results = $database->query($query);
194
			$results_array = $results->fetchRow();
195
			$_SESSION['GROUP_NAME'] = $results_array['name'];
196
			// Set system permissions
197
			if($results_array['system_permissions'] != '') {
198
				$_SESSION['SYSTEM_PERMISSIONS'] = explode(',', $results_array['system_permissions']);
199
			} else {
200
				$_SESSION['SYSTEM_PERMISSIONS'] = array();
201
			}
202
			// Set module permissions
203
			if($results_array['module_permissions'] != '') {
204
				$_SESSION['MODULE_PERMISSIONS'] = explode(',', $results_array['module_permissions']);
205
			} else {
206
				$_SESSION['MODULE_PERMISSIONS'] = array();
207
			}
208
			// Set template permissions
209
			if($results_array['template_permissions'] != '') {
210
				$_SESSION['TEMPLATE_PERMISSIONS'] = explode(',', $results_array['template_permissions']);
211
			} else {
212
				$_SESSION['TEMPLATE_PERMISSIONS'] = array();
213
			}
214
			// Update the users table with current ip and timestamp
215
			$get_ts = mktime();
216
			$get_ip = $_SERVER['REMOTE_ADDR'];
217
			$query = "UPDATE ".$this->USERS_TABLE." SET login_when = '$get_ts', login_ip = '$get_ip' WHERE user_id = '$user_id'";
218
			$database->query($query);
219
		}
220
		// Return if the user exists or not
221
		return $num_rows;
222
	}
223
	
224
	// Increase the count for login attemps
225
	function increase_attemps() {
226
		if(!isset($_SESSION['ATTEMPS'])) {
227
			$_SESSION['ATTEMPS'] = 0;
228
		} else {
229
			$_SESSION['ATTEMPS'] = $this->get_session('ATTEMPS')+1;
230
		}
231
		$this->display_login();
232
	}
233
	
234
	// Function to set a "remembering" cookie for the user
235
	function remember($user_id) {
236
		$remember_key = '';
237
		// Generate user id to append to the remember key
238
		$length = 11-strlen($user_id);
239
		if($length > 0) {
240
			for($i = 1; $i <= $length; $i++) {
241
				$remember_key .= '0';
242
			}
243
		}
244
		// Generate remember key
245
		$remember_key .= $user_id.'_';
246
		$salt = "abchefghjkmnpqrstuvwxyz0123456789";
247
		srand((double)microtime()*1000000);
248
		$i = 0;
249
		while ($i <= 10) {
250
			$num = rand() % 33;
251
			$tmp = substr($salt, $num, 1);
252
			$remember_key = $remember_key . $tmp;
253
			$i++;
254
		}
255
		$remember_key = $remember_key;
256
		// Update the remember key in the db
257
		$database = new database();
258
		$database->query("UPDATE ".$this->USERS_TABLE." SET remember_key = '$remember_key' WHERE user_id = '$user_id' LIMIT 1");
259
		if($database->is_error()) {
260
			return false;
261
		} else {
262
			// Workout options for the cookie
263
			$cookie_name = 'REMEMBER_KEY';
264
			$cookie_value = $remember_key;
265
			$cookie_expire = time()+60*60*24*30;
266
			// Set the cookie
267
			if(setcookie($cookie_name, $cookie_value, $cookie_expire, '/')) {
268
				return true;
269
			} else {
270
				return false;
271
			}
272
		}
273
	}
274
	
275
	// Function to check if a user has been remembered
276
	function is_remembered() {
277
		if(isset($_COOKIE['REMEMBER_KEY']) AND $_COOKIE['REMEMBER_KEY'] != '') {
278
			// Check if the remember key is correct
279
			$database = new database();
280
			$check_query = $database->query("SELECT user_id FROM ".$this->USERS_TABLE." WHERE remember_key = '".$_COOKIE['REMEMBER_KEY']."' LIMIT 1");
281
			if($check_query->numRows() > 0) {
282
				$check_fetch = $check_query->fetchRow();
283
				$user_id = $check_fetch['user_id'];
284
				// Check the remember key prefix
285
				$remember_key_prefix = '';
286
				$length = 11-strlen($user_id);
287
				if($length > 0) {
288
					for($i = 1; $i <= $length; $i++) {
289
						$remember_key_prefix .= '0';
290
					}
291
				}
292
				$remember_key_prefix .= $user_id.'_';
293
				$length = strlen($remember_key_prefix);
294
				if(substr($_COOKIE['REMEMBER_KEY'], 0, $length) == $remember_key_prefix) {
295
					return true;
296
				} else {
297
					return false;
298
				}
299
			} else {
300
				return false;
301
			}
302
		} else {
303
			return false;
304
		}
305
	}
306
	
307
	// Display the login screen
308
	function display_login() {
309
		// Get language vars
310
		global $MESSAGE;
311
		global $MENU;
312
		global $TEXT;
313
		// If attemps more than allowed, warn the user
314
		if($this->get_session('ATTEMPS') > $this->max_attemps) {
315
			$this->warn();
316
		}
317
		// Show the login form
318
		if($this->frontend != true) {
319
			require_once(WB_PATH.'/include/phplib/template.inc');
320
			$template = new Template($this->template_dir);
321
			$template->set_file('page', $this->template_file);
322
			$template->set_block('page', 'mainBlock', 'main');
323
			if($this->remember_me_option != true) {
324
				$template->set_var('DISPLAY_REMEMBER_ME', 'none');
325
			} else {
326
				$template->set_var('DISPLAY_REMEMBER_ME', '');
327
			}
328
			$template->set_var(array(
329
											'ACTION_URL' => $this->login_url,
330
											'ATTEMPS' => $this->get_session('ATTEMPS'),
331
											'USERNAME' => $this->username,
332
											'USERNAME_FIELDNAME' => $this->username_fieldname,
333
											'PASSWORD_FIELDNAME' => $this->password_fieldname,
334
											'MESSAGE' => $this->message,
335
											'INTERFACE_DIR_URL' =>  ADMIN_URL.'/interface',
336
											'MAX_USERNAME_LEN' => $this->max_username_len,
337
											'MAX_PASSWORD_LEN' => $this->max_password_len,
338
											'WB_URL' => WB_URL,
339
											'FORGOTTEN_DETAILS_APP' => $this->forgotten_details_app,
340
											'TEXT_FORGOTTEN_DETAILS' => $TEXT['FORGOTTEN_DETAILS'],
341
											'TEXT_USERNAME' => $TEXT['USERNAME'],
342
											'TEXT_PASSWORD' => $TEXT['PASSWORD'],
343
											'TEXT_REMEMBER_ME' => $TEXT['REMEMBER_ME'],
344
											'TEXT_LOGIN' => $TEXT['LOGIN'],
345
											'TEXT_HOME' => $TEXT['HOME'],
346
											'PAGES_DIRECTORY' => PAGES_DIRECTORY,
347
											'SECTION_LOGIN' => $MENU['LOGIN']
348
											)
349
									);
350
			$template->parse('main', 'mainBlock', false);
351
			$template->pparse('output', 'page');
352
		}
353
	}
354
	
355
	// Warn user that they have had to many login attemps
356
	function warn() {
357
		header('Location: '.$this->warning_url);
358
	}
359
	
360
}
361

    
362
?>
(4-4/11)