Project

General

Profile

1
<?php
2

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

    
369
?>
(4-4/11)