Project

General

Profile

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

    
359
?>
(3-3/7)