Project

General

Profile

1
<?php
2
/**
3
 *
4
 * @category        framework
5
 * @package         backend login
6
 * @author          Ryan Djurovich (2004-2009), WebsiteBaker Project
7
 * @copyright       2009-2012, WebsiteBaker 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 1782 2012-10-11 12:29:11Z Luisehahne $
13
 * @filesource		$HeadURL: svn://isteam.dynxs.de/wb-archiv/branches/2.8.x/wb/framework/class.login.php $
14
 * @lastmodified    $Date: 2012-10-11 14:29:11 +0200 (Thu, 11 Oct 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
if(!class_exists('admin', false)){ include(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(MYSQL_ASSOC);
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

    
192

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

    
223
            if( $this->ami_group_member('1')) {
224
    			$_SESSION['MODULE_PERMISSIONS'] = array();
225
    			$_SESSION['TEMPLATE_PERMISSIONS'] = array();
226
            }
227

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

    
242
	// Increase the count for login attemps
243
	function increase_attemps() {
244
		if(!isset($_SESSION['ATTEMPS'])) {
245
			$_SESSION['ATTEMPS'] = 0;
246
		} else {
247
			$_SESSION['ATTEMPS'] = $this->get_session('ATTEMPS')+1;
248
		}
249
		$this->display_login();
250
	}
251

    
252
	// Function to set a "remembering" cookie for the user - removed
253
	function remember($user_id) {
254
		return true;
255
	}
256

    
257
	// Function to check if a user has been remembered - removed
258
	function is_remembered()
259
	{
260
		return false;
261
	}
262

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

    
331
			$template->set_var('CHARSET', $charset);
332

    
333
			$template->parse('main', 'mainBlock', false);
334
			$template->pparse('output', 'page');
335
		}
336
	}
337

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

    
346
	// Warn user that they have had to many login attemps
347
	function warn() {
348
//		header('Location: '.$this->warning_url);
349
		$this->send_header($this->warning_url);
350
		exit(0);
351
	}
352

    
353
}
(14-14/25)