Project

General

Profile

1
<?php
2
/**
3
 *
4
 * @category        framework
5
 * @package         backend login
6
 * @author          Ryan Djurovich, WebsiteBaker Project
7
 * @copyright       2009-2012, Website Baker Org. e.V.
8
 * @link			http://www.websitebaker2.org/
9
 * @license         http://www.gnu.org/licenses/gpl.html
10
 * @platform        WebsiteBaker 2.8.x
11
 * @requirements    PHP 5.2.2 and higher
12
 * @version         $Id: class.login.php 1709 2012-08-29 11:37:46Z Luisehahne $
13
 * @filesource		$HeadURL: svn://isteam.dynxs.de/wb-archiv/branches/2.8.x/wb/framework/class.login.php $
14
 * @lastmodified    $Date: 2012-08-29 13:37:46 +0200 (Wed, 29 Aug 2012) $
15
 *
16
 */
17
/* -------------------------------------------------------- */
18
// Must include code to stop this file being accessed directly
19
if(!defined('WB_PATH')) {
20
	require_once(dirname(__FILE__).'/globalExceptionHandler.php');
21
	throw new IllegalFileException();
22
}
23
/* -------------------------------------------------------- */
24
define('LOGIN_CLASS_LOADED', true);
25

    
26
// Load the other required class files if they are not already loaded
27
require_once(WB_PATH."/framework/class.admin.php");
28
// Get WB version
29
require_once(ADMIN_PATH.'/interface/version.php');
30

    
31
class login extends admin {
32
	public function __construct($config_array) {
33
		// Get language vars
34
		global $MESSAGE, $database;
35
		parent::__construct();
36
		// Get configuration values
37
		while(list($key, $value) = each($config_array)) {
38
			$this->{(strtolower($key))} = $value;
39
		}
40
		if(!isset($this->redirect_url)) { $this->redirect_url = ''; }
41
		// Get the supplied username and password
42
		if ($this->get_post('username_fieldname') != ''){
43
			$username_fieldname = $this->get_post('username_fieldname');
44
			$password_fieldname = $this->get_post('password_fieldname');
45
		} else {
46
			$username_fieldname = 'username';
47
			$password_fieldname = 'password';
48
		}
49
		$this->username = htmlspecialchars (strtolower($this->get_post($username_fieldname)), ENT_QUOTES);
50

    
51
		$this->password = $this->get_post($password_fieldname);
52
		// Figure out if the "remember me" option has been checked
53
		if($this->get_post('remember') == 'true') {
54
			$this->remember = $this->get_post('remember');
55
		} else {
56
			$this->remember = false;
57
		}
58
		// Get the length of the supplied username and password
59
		if($this->get_post($username_fieldname) != '') {
60
			$this->username_len = strlen($this->username);
61
			$this->password_len = strlen($this->password);
62
		}
63
		// If the url is blank, set it to the default url
64
		$this->url = $this->get_post('url');
65
		if ($this->redirect_url!='') {
66
			$this->url = $this->redirect_url;
67
		}
68
		if(strlen($this->url) < 2) {
69
			$this->url = $config_array['DEFAULT_URL'];
70
		}
71
		if($this->is_authenticated() == true) {
72
			// User already logged-in, so redirect to default url
73
//				header("Location: ".$this->url);
74
//				exit(0);
75
				$this->send_header($this->url);
76
		} elseif($this->is_remembered() == true) {
77
			// User has been "remembered"
78
			// Get the users password
79
			// $database = new database();
80
			$sql  = 'SELECT * FROM `'.$this->users_table.'` ';
81
			$sql .= 'WHERE `user_id`=\''.$this->get_safe_remember_key().'\'';
82
			$query_details = $database->query($sql);
83
			$fetch_details = $query_details->fetchRow();
84
			$this->username = $fetch_details['username'];
85
			$this->password = $fetch_details['password'];
86
			// Check if the user exists (authenticate them)
87
			if($this->authenticate()) {
88
				// Authentication successful
89
//				header("Location: ".$this->url);
90
//				exit(0);
91
				$this->send_header($this->url);
92
			} else {
93
				$this->message = $MESSAGE['LOGIN_AUTHENTICATION_FAILED'];
94
				$this->increase_attemps();
95
			}
96
		} elseif($this->username == '' AND $this->password == '') {
97
			$this->message = $MESSAGE['LOGIN_BOTH_BLANK'];
98
			$this->display_login();
99
		} elseif($this->username == '') {
100
			$this->message = $MESSAGE['LOGIN_USERNAME_BLANK'];
101
			$this->increase_attemps();
102
		} elseif($this->password == '') {
103
			$this->message = $MESSAGE['LOGIN_PASSWORD_BLANK'];
104
			$this->increase_attemps();
105
		} elseif($this->username_len < $config_array['MIN_USERNAME_LEN']) {
106
			$this->message = $MESSAGE['LOGIN_USERNAME_TOO_SHORT'];
107
			$this->increase_attemps();
108
		} elseif($this->password_len < $config_array['MIN_PASSWORD_LEN']) {
109
			$this->message = $MESSAGE['LOGIN_PASSWORD_TOO_SHORT'];
110
			$this->increase_attemps();
111
		} elseif($this->username_len > $config_array['MAX_USERNAME_LEN']) {
112
			$this->message = $MESSAGE['LOGIN_USERNAME_TOO_LONG'];
113
			$this->increase_attemps();
114
		} elseif($this->password_len > $config_array['MAX_PASSWORD_LEN']) {
115
			$this->message = $MESSAGE['LOGIN_PASSWORD_TOO_LONG'];
116
			$this->increase_attemps();
117
		} else {
118
			// Check if the user exists (authenticate them)
119
			$this->password = md5($this->password);
120
			if($this->authenticate()) {
121
				// Authentication successful
122
// 				echo $this->url;exit();
123
// 				header("Location: ".$this->url);
124
// 				exit(0);
125
				$this->send_header($this->url);
126
			} else {
127
				$this->message = $MESSAGE['LOGIN_AUTHENTICATION_FAILED'];
128
				$this->increase_attemps();
129
			}
130
		}
131
	}
132

    
133
	// Authenticate the user (check if they exist in the database)
134
	function authenticate() {
135
		global $database;
136
		// Get user information
137
		// $database = new database();
138
		// $query = 'SELECT * FROM `'.$this->users_table.'` WHERE MD5(`username`) = "'.md5($this->username).'" AND `password` = "'.$this->password.'" AND `active` = 1';
139
 		$loginname = ( preg_match('/[\;\=\&\|\<\> ]/',$this->username) ? '' : $this->username );
140
		$sql  = 'SELECT * FROM `'.$this->users_table.'` ';
141
		$sql .= 'WHERE `username`=\''.$loginname.'\' AND `password`=\''.$this->password.'\' AND `active`=1';
142
		$results = $database->query($sql);
143
		$results_array = $results->fetchRow();
144
		$num_rows = $results->numRows();
145
		if($num_rows == 1) {
146
			$user_id = $results_array['user_id'];
147
			$this->user_id = $user_id;
148
			$_SESSION['USER_ID'] = $user_id;
149
			$_SESSION['GROUP_ID'] = $results_array['group_id'];
150
			$_SESSION['GROUPS_ID'] = $results_array['groups_id'];
151
			$_SESSION['USERNAME'] = $results_array['username'];
152
			$_SESSION['DISPLAY_NAME'] = $results_array['display_name'];
153
			$_SESSION['EMAIL'] = $results_array['email'];
154
			$_SESSION['HOME_FOLDER'] = $results_array['home_folder'];
155
			// Run remember function if needed
156
			if($this->remember == true) {
157
				$this->remember($this->user_id);
158
			}
159
			// Set language
160
			if($results_array['language'] != '') {
161
				$_SESSION['LANGUAGE'] = $results_array['language'];
162
			}
163
			// Set timezone
164
			if($results_array['timezone'] != '-72000') {
165
				$_SESSION['TIMEZONE'] = $results_array['timezone'];
166
			} else {
167
				// Set a session var so apps can tell user is using default tz
168
				$_SESSION['USE_DEFAULT_TIMEZONE'] = true;
169
			}
170
			// Set date format
171
			if($results_array['date_format'] != '') {
172
				$_SESSION['DATE_FORMAT'] = $results_array['date_format'];
173
			} else {
174
				// Set a session var so apps can tell user is using default date format
175
				$_SESSION['USE_DEFAULT_DATE_FORMAT'] = true;
176
			}
177
			// Set time format
178
			if($results_array['time_format'] != '') {
179
				$_SESSION['TIME_FORMAT'] = $results_array['time_format'];
180
			} else {
181
				// Set a session var so apps can tell user is using default time format
182
				$_SESSION['USE_DEFAULT_TIME_FORMAT'] = true;
183
			}
184

    
185
			// Get group information
186
			$_SESSION['SYSTEM_PERMISSIONS'] = array();
187
			$_SESSION['MODULE_PERMISSIONS'] = array();
188
			$_SESSION['TEMPLATE_PERMISSIONS'] = array();
189
			$_SESSION['GROUP_NAME'] = array();
190

    
191
			$first_group = true;
192
			foreach (explode(",", $this->get_session('GROUPS_ID')) as $cur_group_id)
193
            {
194
				$sql = 'SELECT * FROM `'.$this->groups_table.'` WHERE `group_id`=\''.$cur_group_id.'\'';
195
				$results = $database->query($sql);
196
				$results_array = $results->fetchRow();
197
				$_SESSION['GROUP_NAME'][$cur_group_id] = $results_array['name'];
198
				// Set system permissions
199
				if($results_array['system_permissions'] != '') {
200
					$_SESSION['SYSTEM_PERMISSIONS'] = array_merge($_SESSION['SYSTEM_PERMISSIONS'], explode(',', $results_array['system_permissions']));
201
				}
202
				// Set module permissions
203
				if($results_array['module_permissions'] != '') {
204
					if ($first_group) {
205
          	$_SESSION['MODULE_PERMISSIONS'] = explode(',', $results_array['module_permissions']);
206
          } else {
207
          	$_SESSION['MODULE_PERMISSIONS'] = array_intersect($_SESSION['MODULE_PERMISSIONS'], explode(',', $results_array['module_permissions']));
208
					}
209
				}
210
				// Set template permissions
211
				if($results_array['template_permissions'] != '') {
212
					if ($first_group) {
213
          	$_SESSION['TEMPLATE_PERMISSIONS'] = explode(',', $results_array['template_permissions']);
214
          } else {
215
          	$_SESSION['TEMPLATE_PERMISSIONS'] = array_intersect($_SESSION['TEMPLATE_PERMISSIONS'], explode(',', $results_array['template_permissions']));
216
					}
217
				}
218
				$first_group = false;
219
			}
220

    
221
			// Update the users table with current ip and timestamp
222
			$get_ts = time();
223
			$get_ip = $_SERVER['REMOTE_ADDR'];
224
			$sql  = 'UPDATE `'.$this->users_table.'` ';
225
			$sql .= 'SET `login_when`=\''.$get_ts.'\', `login_ip`=\''.$get_ip.'\' ';
226
			$sql .= 'WHERE `user_id`=\''.$user_id.'\'';
227
			$database->query($sql);
228
		}else {
229
		  $num_rows = 0;
230
		}
231
		// Return if the user exists or not
232
		return $num_rows;
233
	}
234

    
235
	// Increase the count for login attemps
236
	function increase_attemps() {
237
		if(!isset($_SESSION['ATTEMPS'])) {
238
			$_SESSION['ATTEMPS'] = 0;
239
		} else {
240
			$_SESSION['ATTEMPS'] = $this->get_session('ATTEMPS')+1;
241
		}
242
		$this->display_login();
243
	}
244

    
245
	// Function to set a "remembering" cookie for the user - removed
246
	function remember($user_id) {
247
		return true;
248
	}
249

    
250
	// Function to check if a user has been remembered - removed
251
	function is_remembered()
252
	{
253
		return false;
254
	}
255

    
256
	// Display the login screen
257
	function display_login() {
258
		// Get language vars
259
		global $MESSAGE, $MENU, $TEXT;
260
		// If attemps more than allowed, warn the user
261
		if($this->get_session('ATTEMPS') > $this->max_attemps) {
262
			$this->warn();
263
		}
264
		// Show the login form
265
		if($this->frontend != true) {
266
			//require_once(WB_PATH.'/include/phplib/template.inc');
267
			// $template = new Template($this->template_dir);
268
			// Setup template object, parse vars to it, then parse it
269
			$template = new Template(dirname($this->correct_theme_source($this->template_file)));
270
			$template->set_file('page', $this->template_file);
271
			$template->set_block('page', 'mainBlock', 'main');
272
			if($this->remember_me_option != true) {
273
				$template->set_var('DISPLAY_REMEMBER_ME', 'display: none;');
274
			} else {
275
				$template->set_var('DISPLAY_REMEMBER_ME', '');
276
			}
277
			$template->set_var(array(
278
				'ACTION_URL' => $this->login_url,
279
				'ATTEMPS' => $this->get_session('ATTEMPS'),
280
				'USERNAME' => $this->username,
281
				'USERNAME_FIELDNAME' => $this->username_fieldname,
282
				'PASSWORD_FIELDNAME' => $this->password_fieldname,
283
				'MESSAGE' => $this->message,
284
				'WEBSITE_TITLE' => WEBSITE_TITLE,
285
				'TEXT_ADMINISTRATION' => $TEXT['ADMINISTRATION'],
286
				'INTERFACE_DIR_URL' =>  ADMIN_URL.'/interface',
287
				'MAX_USERNAME_LEN' => $this->max_username_len,
288
				'MAX_PASSWORD_LEN' => $this->max_password_len,
289
				'ADMIN_URL' => ADMIN_URL,
290
				'WB_URL' => WB_URL,
291
				'URL_VIEW' => WB_URL,
292
				'THEME_URL' => THEME_URL,
293
				'VERSION' => VERSION,
294
				'SP' => (defined('SP') ? SP : ''),
295
				'REVISION' => REVISION,
296
				'LANGUAGE' => strtolower(LANGUAGE),
297
				'FORGOTTEN_DETAILS_APP' => $this->forgotten_details_app,
298
				'TEXT_FORGOTTEN_DETAILS' => $TEXT['FORGOTTEN_DETAILS'],
299
				'TEXT_USERNAME' => $TEXT['USERNAME'],
300
				'TEXT_PASSWORD' => $TEXT['PASSWORD'],
301
				'TEXT_REMEMBER_ME' => $TEXT['REMEMBER_ME'],
302
				'TEXT_LOGIN' => $TEXT['LOGIN'],
303
				'TITLE_LOGOUT' => $MENU['LOGIN'],
304
				'TEXT_RESET' => $TEXT['RESET'],
305
				'TEXT_HOME' => $TEXT['HOME'],
306
				'TITLE_VIEW' => $TEXT['WEBSITE'],
307
				'PAGES_DIRECTORY' => PAGES_DIRECTORY,
308
				'SECTION_NAME' => $MENU['LOGIN'],
309
				'SECTION_LOGIN' => $MENU['LOGIN'],
310
				'LOGIN_DISPLAY_HIDDEN' => !$this->is_authenticated() ? 'hidden' : '',
311
				'LOGIN_DISPLAY_NONE' => !$this->is_authenticated() ? 'none' : '',
312
				'LOGIN_LINK' => $_SERVER['SCRIPT_NAME'],
313
				'LOGIN_ICON' => 'login',
314
				'START_ICON' => 'blank',
315
				'URL_HELP' => 'http://www.websitebaker.org/',
316
				)
317
			);
318
			if(defined('DEFAULT_CHARSET')) {
319
				$charset=DEFAULT_CHARSET;
320
			} else {
321
				$charset='utf-8';
322
			}
323

    
324
			$template->set_var('CHARSET', $charset);
325

    
326
			$template->parse('main', 'mainBlock', false);
327
			$template->pparse('output', 'page');
328
		}
329
	}
330

    
331
	// sanities the REMEMBER_KEY cookie to avoid SQL injection
332
	function get_safe_remember_key() {
333
		if (!((strlen($_COOKIE['REMEMBER_KEY']) == 23) && (substr($_COOKIE['REMEMBER_KEY'], 11, 1) == '_'))) return '';
334
		// create a clean cookie (XXXXXXXXXXX_YYYYYYYYYYY) where X:= numeric, Y:= hash
335
		$clean_cookie = sprintf('%011d', (int) substr($_COOKIE['REMEMBER_KEY'], 0, 11)) . substr($_COOKIE['REMEMBER_KEY'], 11);
336
		return ($clean_cookie == $_COOKIE['REMEMBER_KEY']) ? $this->add_slashes($clean_cookie) : '';
337
	}
338

    
339
	// Warn user that they have had to many login attemps
340
	function warn() {
341
//		header('Location: '.$this->warning_url);
342
		$this->send_header($this->warning_url);
343
		exit(0);
344
	}
345

    
346
}
(14-14/25)