Project

General

Profile

1
<?php
2
/*
3
*
4
*                       About WebsiteBaker
5
*
6
* Website Baker is a PHP-based Content Management System (CMS)
7
* designed with one goal in mind: to enable its users to produce websites
8
* with ease.
9
*
10
*                       LICENSE INFORMATION
11
*
12
* WebsiteBaker is free software; you can redistribute it and/or
13
* modify it under the terms of the GNU General Public License
14
* as published by the Free Software Foundation; either version 2
15
* of the License, or (at your option) any later version.
16
*
17
* WebsiteBaker is distributed in the hope that it will be useful,
18
* but WITHOUT ANY WARRANTY; without even the implied warranty of
19
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
20
* See the GNU General Public License for more details.
21
*
22
* You should have received a copy of the GNU General Public License
23
* along with this program; if not, write to the Free Software
24
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25
*
26
*                   WebsiteBaker Extra Information
27
*
28
*
29
*/
30
/**
31
 *
32
 * @category        backend
33
 * @package         login
34
 * @author          WebsiteBaker Project
35
 * @copyright       2004-2009, Ryan Djurovich
36
 * @copyright       2009-2010, Website Baker Org. e.V.
37
 * @link			http://www.websitebaker2.org/
38
 * @license         http://www.gnu.org/licenses/gpl.html
39
 * @platform        WebsiteBaker 2.8.x
40
 * @requirements    PHP 4.3.4 and higher
41
 * @version         $Id: class.login.php 1270 2010-01-22 22:56:36Z Luisehahne $
42
 * @filesource		$HeadURL: svn://isteam.dynxs.de/wb-archiv/branches/2.8.x/wb/framework/class.login.php $
43
 * @lastmodified    $Date: 2010-01-22 23:56:36 +0100 (Fri, 22 Jan 2010) $
44
 *
45
*/
46

    
47
// Stop this file from being accessed directly
48
if(!defined('WB_URL')) {
49
	header('Location: ../index.php');
50
	exit(0);
51
}
52

    
53
define('LOGIN_CLASS_LOADED', true);
54

    
55
// Load the other required class files if they are not already loaded
56
require_once(WB_PATH."/framework/class.admin.php");
57

    
58
class login extends admin {
59
	function login($config_array) {
60
		// Get language vars
61
		global $MESSAGE;
62
		$this->wb();
63
		// Get configuration values
64
		$this->USERS_TABLE = $config_array['USERS_TABLE'];
65
		$this->GROUPS_TABLE = $config_array['GROUPS_TABLE'];
66
		$this->username_fieldname = $config_array['USERNAME_FIELDNAME'];
67
		$this->password_fieldname = $config_array['PASSWORD_FIELDNAME'];
68
		$this->remember_me_option = $config_array['REMEMBER_ME_OPTION'];
69
		$this->max_attemps = $config_array['MAX_ATTEMPS'];
70
		$this->warning_url = $config_array['WARNING_URL'];
71
		$this->login_url = $config_array['LOGIN_URL'];
72
		$this->template_dir = $config_array['TEMPLATE_DIR'];
73
		$this->template_file = $config_array['TEMPLATE_FILE'];
74
		$this->frontend = $config_array['FRONTEND'];
75
		$this->forgotten_details_app = $config_array['FORGOTTEN_DETAILS_APP'];
76
		$this->max_username_len = $config_array['MAX_USERNAME_LEN'];
77
		$this->max_password_len = $config_array['MAX_PASSWORD_LEN'];
78
		if (array_key_exists('REDIRECT_URL',$config_array))
79
			$this->redirect_url = $config_array['REDIRECT_URL'];
80
		else
81
			$this->redirect_url = '';
82
		// Get the supplied username and password
83
		if ($this->get_post('username_fieldname') != ''){
84
			$username_fieldname = $this->get_post('username_fieldname');
85
			$password_fieldname = $this->get_post('password_fieldname');
86
		} else {
87
			$username_fieldname = 'username';
88
			$password_fieldname = 'password';
89
		}
90
		$this->username = $this->add_slashes(strtolower($this->get_post($username_fieldname)));
91
		$this->password = $this->get_post($password_fieldname);
92
		// Figure out if the "remember me" option has been checked
93
		if($this->get_post('remember') == 'true') {
94
			$this->remember = $this->get_post('remember');
95
		} else {
96
			$this->remember = false;
97
		}
98
		// Get the length of the supplied username and password
99
		if($this->get_post($username_fieldname) != '') {
100
			$this->username_len = strlen($this->username);
101
			$this->password_len = strlen($this->password);
102
		}
103
		// If the url is blank, set it to the default url
104
		$this->url = $this->get_post('url');
105
		if ($this->redirect_url!='') {
106
			$this->url = $this->redirect_url;
107
		}		
108
		if(strlen($this->url) < 2) {
109
			$this->url = $config_array['DEFAULT_URL'];
110
		}
111
		if($this->is_authenticated() == true) {
112
			// User already logged-in, so redirect to default url
113
			header('Location: '.$this->url);
114
			exit();
115
		} elseif($this->is_remembered() == true) {
116
			// User has been "remembered"
117
			// Get the users password
118
			$database = new database();
119
			$query_details = $database->query("SELECT * FROM ".$this->USERS_TABLE." WHERE user_id = '".$this->get_safe_remember_key()."' LIMIT 1");
120
			$fetch_details = $query_details->fetchRow();
121
			$this->username = $fetch_details['username'];
122
			$this->password = $fetch_details['password'];
123
			// Check if the user exists (authenticate them)
124
			if($this->authenticate()) {
125
				// Authentication successful
126
				header("Location: ".$this->url);
127
				exit(0);
128
			} else {
129
				$this->message = $MESSAGE['LOGIN']['AUTHENTICATION_FAILED'];
130
				$this->increase_attemps();
131
			}
132
		} elseif($this->username == '' AND $this->password == '') {
133
			$this->message = $MESSAGE['LOGIN']['BOTH_BLANK'];
134
			$this->increase_attemps();
135
		} elseif($this->username == '') {
136
			$this->message = $MESSAGE['LOGIN']['USERNAME_BLANK'];
137
			$this->increase_attemps();
138
		} elseif($this->password == '') {
139
			$this->message = $MESSAGE['LOGIN']['PASSWORD_BLANK'];
140
			$this->increase_attemps();
141
		} elseif($this->username_len < $config_array['MIN_USERNAME_LEN']) {
142
			$this->message = $MESSAGE['LOGIN']['USERNAME_TOO_SHORT'];
143
			$this->increase_attemps();
144
		} elseif($this->password_len < $config_array['MIN_PASSWORD_LEN']) {
145
			$this->message = $MESSAGE['LOGIN']['PASSWORD_TOO_SHORT'];
146
			$this->increase_attemps();
147
		} elseif($this->username_len > $config_array['MAX_USERNAME_LEN']) {
148
			$this->message = $MESSAGE['LOGIN']['USERNAME_TOO_LONG'];
149
			$this->increase_attemps();
150
		} elseif($this->password_len > $config_array['MAX_PASSWORD_LEN']) {
151
			$this->message = $MESSAGE['LOGIN']['PASSWORD_TOO_LONG'];
152
			$this->increase_attemps();
153
		} else {
154
			// Check if the user exists (authenticate them)
155
			$this->password = md5($this->password);
156
			if($this->authenticate()) {
157
				// Authentication successful
158
				//echo $this->url;exit();
159
				header("Location: ".$this->url);
160
				exit(0);
161
			} else {
162
				$this->message = $MESSAGE['LOGIN']['AUTHENTICATION_FAILED'];
163
				$this->increase_attemps();
164
			}
165
		}
166
	}
167
	
168
	// Authenticate the user (check if they exist in the database)
169
	function authenticate() {
170
		// Get user information
171
		$database = new database();
172
		$query = "SELECT * FROM ".$this->USERS_TABLE." WHERE username = '".$this->username."' AND password = '".$this->password."' AND active = '1'";
173
		$results = $database->query($query);
174
		$results_array = $results->fetchRow();
175
		$num_rows = $results->numRows();
176
		if($num_rows) {
177
			$user_id = $results_array['user_id'];
178
			$this->user_id = $user_id;
179
			$_SESSION['USER_ID'] = $user_id;
180
			$_SESSION['GROUP_ID'] = $results_array['group_id'];
181
			$_SESSION['GROUPS_ID'] = $results_array['groups_id'];
182
			$_SESSION['USERNAME'] = $results_array['username'];
183
			$_SESSION['DISPLAY_NAME'] = $results_array['display_name'];
184
			$_SESSION['EMAIL'] = $results_array['email'];
185
			$_SESSION['HOME_FOLDER'] = $results_array['home_folder'];
186
			// Run remember function if needed
187
			if($this->remember == true) {
188
				$this->remember($this->user_id);
189
			}
190
			// Set language
191
			if($results_array['language'] != '') {
192
				$_SESSION['LANGUAGE'] = $results_array['language'];
193
			}
194
			// Set timezone
195
			if($results_array['timezone'] != '-72000') {
196
				$_SESSION['TIMEZONE'] = $results_array['timezone'];
197
			} else {
198
				// Set a session var so apps can tell user is using default tz
199
				$_SESSION['USE_DEFAULT_TIMEZONE'] = true;
200
			}
201
			// Set date format
202
			if($results_array['date_format'] != '') {
203
				$_SESSION['DATE_FORMAT'] = $results_array['date_format'];
204
			} else {
205
				// Set a session var so apps can tell user is using default date format
206
				$_SESSION['USE_DEFAULT_DATE_FORMAT'] = true;
207
			}
208
			// Set time format
209
			if($results_array['time_format'] != '') {
210
				$_SESSION['TIME_FORMAT'] = $results_array['time_format'];
211
			} else {
212
				// Set a session var so apps can tell user is using default time format
213
				$_SESSION['USE_DEFAULT_TIME_FORMAT'] = true;
214
			}
215
			// Get group information
216
//			$query = "SELECT * FROM ".$this->GROUPS_TABLE." WHERE group_id = '".$this->get_session('GROUP_ID')."'";
217
//			$results = $database->query($query);
218
//			$results_array = $results->fetchRow();
219
//			$_SESSION['GROUP_NAME'] = $results_array['name'];
220
//			// Set system permissions
221
//			if($results_array['system_permissions'] != '') {
222
//				$_SESSION['SYSTEM_PERMISSIONS'] = explode(',', $results_array['system_permissions']);
223
//			} else {
224
//				$_SESSION['SYSTEM_PERMISSIONS'] = array();
225
//			}
226
//			// Set module permissions
227
//			if($results_array['module_permissions'] != '') {
228
//				$_SESSION['MODULE_PERMISSIONS'] = explode(',', $results_array['module_permissions']);
229
//			} else {
230
//				$_SESSION['MODULE_PERMISSIONS'] = array();
231
//			}
232
//			// Set template permissions
233
//			if($results_array['template_permissions'] != '') {
234
//				$_SESSION['TEMPLATE_PERMISSIONS'] = explode(',', $results_array['template_permissions']);
235
//			} else {
236
//				$_SESSION['TEMPLATE_PERMISSIONS'] = array();
237
//			}
238

    
239
			$_SESSION['SYSTEM_PERMISSIONS'] = array();
240
			$_SESSION['MODULE_PERMISSIONS'] = array();
241
			$_SESSION['TEMPLATE_PERMISSIONS'] = array();
242
			$_SESSION['GROUP_NAME'] = array();
243

    
244
			$first_group = true;
245
			foreach (explode(",", $this->get_session('GROUPS_ID')) as $cur_group_id)
246
            {
247
				$query = "SELECT * FROM ".$this->GROUPS_TABLE." WHERE group_id = '".$cur_group_id."'";
248
				$results = $database->query($query);
249
				$results_array = $results->fetchRow();
250
				$_SESSION['GROUP_NAME'][$cur_group_id] = $results_array['name'];
251
				// Set system permissions
252
				if($results_array['system_permissions'] != '') {
253
					$_SESSION['SYSTEM_PERMISSIONS'] = array_merge($_SESSION['SYSTEM_PERMISSIONS'], explode(',', $results_array['system_permissions']));
254
				}
255
				// Set module permissions
256
				if($results_array['module_permissions'] != '') {
257
					if ($first_group) {
258
          	$_SESSION['MODULE_PERMISSIONS'] = explode(',', $results_array['module_permissions']);
259
          } else {
260
          	$_SESSION['MODULE_PERMISSIONS'] = array_intersect($_SESSION['MODULE_PERMISSIONS'], explode(',', $results_array['module_permissions']));
261
					}
262
				}
263
				// Set template permissions
264
				if($results_array['template_permissions'] != '') {
265
					if ($first_group) {
266
          	$_SESSION['TEMPLATE_PERMISSIONS'] = explode(',', $results_array['template_permissions']);
267
          } else {
268
          	$_SESSION['TEMPLATE_PERMISSIONS'] = array_intersect($_SESSION['TEMPLATE_PERMISSIONS'], explode(',', $results_array['template_permissions']));
269
					}
270
				}
271
				$first_group = false;
272
			}	
273

    
274
			// Update the users table with current ip and timestamp
275
			$get_ts = time();
276
			$get_ip = $_SERVER['REMOTE_ADDR'];
277
			$query = "UPDATE ".$this->USERS_TABLE." SET login_when = '$get_ts', login_ip = '$get_ip' WHERE user_id = '$user_id'";
278
			$database->query($query);
279
		}
280
		// Return if the user exists or not
281
		return $num_rows;
282
	}
283
	
284
	// Increase the count for login attemps
285
	function increase_attemps() {
286
		if(!isset($_SESSION['ATTEMPS'])) {
287
			$_SESSION['ATTEMPS'] = 0;
288
		} else {
289
			$_SESSION['ATTEMPS'] = $this->get_session('ATTEMPS')+1;
290
		}
291
		$this->display_login();
292
	}
293
	
294
	// Function to set a "remembering" cookie for the user
295
	function remember($user_id) {
296
		$remember_key = '';
297
		// Generate user id to append to the remember key
298
		$length = 11-strlen($user_id);
299
		if($length > 0) {
300
			for($i = 1; $i <= $length; $i++) {
301
				$remember_key .= '0';
302
			}
303
		}
304
		// Generate remember key
305
		$remember_key .= $user_id.'_';
306
		$salt = "abchefghjkmnpqrstuvwxyz0123456789";
307
		srand((double)microtime()*1000000);
308
		$i = 0;
309
		while ($i <= 10) {
310
			$num = rand() % 33;
311
			$tmp = substr($salt, $num, 1);
312
			$remember_key = $remember_key . $tmp;
313
			$i++;
314
		}
315
		$remember_key = $remember_key;
316
		// Update the remember key in the db
317
		$database = new database();
318
		$database->query("UPDATE ".$this->USERS_TABLE." SET remember_key = '$remember_key' WHERE user_id = '$user_id' LIMIT 1");
319
		if($database->is_error()) {
320
			return false;
321
		} else {
322
			// Workout options for the cookie
323
			$cookie_name = 'REMEMBER_KEY';
324
			$cookie_value = $remember_key;
325
			$cookie_expire = time()+60*60*24*30;
326
			// Set the cookie
327
			if(setcookie($cookie_name, $cookie_value, $cookie_expire, '/')) {
328
				return true;
329
			} else {
330
				return false;
331
			}
332
		}
333
	}
334
	
335
	// Function to check if a user has been remembered
336
	function is_remembered() {
337
		if(isset($_COOKIE['REMEMBER_KEY']) AND $_COOKIE['REMEMBER_KEY'] != '') {
338
			// Check if the remember key is correct
339
			$database = new database();
340
			$sql = "SELECT `user_id` FROM `" . $this->USERS_TABLE . "` WHERE `remember_key` = '";
341
			$sql .= $this->get_safe_remember_key() . "' LIMIT 1";
342
			$check_query = $database->query($sql);
343

    
344
			if($check_query->numRows() > 0) {
345
				$check_fetch = $check_query->fetchRow();
346
				$user_id = $check_fetch['user_id'];
347
				// Check the remember key prefix
348
				$remember_key_prefix = '';
349
				$length = 11-strlen($user_id);
350
				if($length > 0) {
351
					for($i = 1; $i <= $length; $i++) {
352
						$remember_key_prefix .= '0';
353
					}
354
				}
355
				$remember_key_prefix .= $user_id.'_';
356
				$length = strlen($remember_key_prefix);
357
				if(substr($_COOKIE['REMEMBER_KEY'], 0, $length) == $remember_key_prefix) {
358
					return true;
359
				} else {
360
					return false;
361
				}
362
			} else {
363
				return false;
364
			}
365
		} else {
366
			return false;
367
		}
368
	}
369
	
370
	// Display the login screen
371
	function display_login() {
372
		// Get language vars
373
		global $MESSAGE;
374
		global $MENU;
375
		global $TEXT;
376
		// If attemps more than allowed, warn the user
377
		if($this->get_session('ATTEMPS') > $this->max_attemps) {
378
			$this->warn();
379
		}
380
		// Show the login form
381
		if($this->frontend != true) {
382
			require_once(WB_PATH.'/include/phplib/template.inc');
383
			$template = new Template($this->template_dir);
384
			$template->set_file('page', $this->template_file);
385
			$template->set_block('page', 'mainBlock', 'main');
386
			if($this->remember_me_option != true) {
387
				$template->set_var('DISPLAY_REMEMBER_ME', 'display: none;');
388
			} else {
389
				$template->set_var('DISPLAY_REMEMBER_ME', '');
390
			}
391
			$template->set_var(array(
392
											'ACTION_URL' => $this->login_url,
393
											'ATTEMPS' => $this->get_session('ATTEMPS'),
394
											'USERNAME' => $this->username,
395
											'USERNAME_FIELDNAME' => $this->username_fieldname,
396
											'PASSWORD_FIELDNAME' => $this->password_fieldname,
397
											'MESSAGE' => $this->message,
398
											'INTERFACE_DIR_URL' =>  ADMIN_URL.'/interface',
399
											'MAX_USERNAME_LEN' => $this->max_username_len,
400
											'MAX_PASSWORD_LEN' => $this->max_password_len,
401
											'WB_URL' => WB_URL,
402
											'THEME_URL' => THEME_URL,
403
											'LANGUAGE' => strtolower(LANGUAGE),
404
											'FORGOTTEN_DETAILS_APP' => $this->forgotten_details_app,
405
											'TEXT_FORGOTTEN_DETAILS' => $TEXT['FORGOTTEN_DETAILS'],
406
											'TEXT_USERNAME' => $TEXT['USERNAME'],
407
											'TEXT_PASSWORD' => $TEXT['PASSWORD'],
408
											'TEXT_REMEMBER_ME' => $TEXT['REMEMBER_ME'],
409
											'TEXT_LOGIN' => $TEXT['LOGIN'],
410
											'TEXT_HOME' => $TEXT['HOME'],
411
											'PAGES_DIRECTORY' => PAGES_DIRECTORY,
412
											'SECTION_LOGIN' => $MENU['LOGIN']
413
											)
414
									);
415
			if(defined('DEFAULT_CHARSET')) {
416
				$charset=DEFAULT_CHARSET;
417
			} else {
418
				$charset='utf-8';
419
			}
420
			
421
			$template->set_var('CHARSET', $charset);	
422
									
423
									
424
			$template->parse('main', 'mainBlock', false);
425
			$template->pparse('output', 'page');
426
		}
427
	}
428

    
429
	// sanities the REMEMBER_KEY cookie to avoid SQL injection
430
	function get_safe_remember_key() {
431
		if (!((strlen($_COOKIE['REMEMBER_KEY']) == 23) && (substr($_COOKIE['REMEMBER_KEY'], 11, 1) == '_'))) return '';
432
		// create a clean cookie (XXXXXXXXXXX_YYYYYYYYYYY) where X:= numeric, Y:= hash
433
		$clean_cookie = sprintf('%011d', (int) substr($_COOKIE['REMEMBER_KEY'], 0, 11)) . substr($_COOKIE['REMEMBER_KEY'], 11);
434
		return ($clean_cookie == $_COOKIE['REMEMBER_KEY']) ? $this->add_slashes($clean_cookie) : '';
435
	}
436
	
437
	// Warn user that they have had to many login attemps
438
	function warn() {
439
		header('Location: '.$this->warning_url);
440
		exit(0);
441
	}
442
	
443
}
444

    
445
?>
(6-6/15)