Project

General

Profile

1
<?php
2

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

    
366
?>
(4-4/11)