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 1789 2012-10-14 10:17:38Z Luisehahne $
13
 * @filesource		$HeadURL: svn://isteam.dynxs.de/wb-archiv/branches/2.8.x/wb/framework/class.login.php $
14
 * @lastmodified    $Date: 2012-10-14 12:17:38 +0200 (Sun, 14 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(MYSQL_ASSOC);
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
            $aGroupsIds = ((explode(',', $this->get_session('GROUPS_ID'))));
192
            $bOnlyAdminGroup = $this->ami_group_member('1') && (sizeof($aGroupsIds) == 1);
193
			$first_group = true;
194

    
195
			foreach ( $aGroupsIds  as $cur_group_id)
196
            {
197
				$sql = 'SELECT * FROM `'.$this->groups_table.'` WHERE `group_id`=\''.$cur_group_id.'\'';
198
				if($results = $database->query($sql)) {
199
    				$results_array = $results->fetchRow(MYSQL_ASSOC);
200
    				$_SESSION['GROUP_NAME'][$cur_group_id] = $results_array['name'];
201
				}
202

    
203
				// Set system permissions
204
				if( ($results_array['system_permissions'] != '') ) {
205
                    switch ($cur_group_id) :
206
                        case 1:
207
                            if($bOnlyAdminGroup) {
208
                                $_SESSION['SYSTEM_PERMISSIONS'] = array_merge($_SESSION['SYSTEM_PERMISSIONS'], explode(',', $results_array['system_permissions']));
209
                            }
210
                            break;
211
                        default:
212
        					$_SESSION['SYSTEM_PERMISSIONS'] = array_merge($_SESSION['SYSTEM_PERMISSIONS'], explode(',', $results_array['system_permissions']));
213
                    endswitch;
214
				}
215

    
216
				// Set module permissions
217
				if( $results_array['module_permissions'] != '' ) {
218
					if ($first_group) {
219
                  	$_SESSION['MODULE_PERMISSIONS'] = explode(',', $results_array['module_permissions']);
220
                  } else {
221
                  	$_SESSION['MODULE_PERMISSIONS'] = array_intersect($_SESSION['MODULE_PERMISSIONS'], explode(',', $results_array['module_permissions']));
222
					}
223
				}
224
				// Set template permissions
225
				if($results_array['template_permissions'] != '') {
226
					if ($first_group) {
227
                      	$_SESSION['TEMPLATE_PERMISSIONS'] = explode(',', $results_array['template_permissions']);
228
                      } else {
229
                      	$_SESSION['TEMPLATE_PERMISSIONS'] = array_intersect($_SESSION['TEMPLATE_PERMISSIONS'], explode(',', $results_array['template_permissions']));
230
					}
231
				}
232
				$first_group = false;
233
			}
234

    
235
            if( $$bOnlyAdminGroup ) {
236
    			$_SESSION['MODULE_PERMISSIONS'] = array();
237
    			$_SESSION['TEMPLATE_PERMISSIONS'] = array();
238
            }
239

    
240
			// Update the users table with current ip and timestamp
241
			$get_ts = time();
242
			$get_ip = $_SERVER['REMOTE_ADDR'];
243
			$sql  = 'UPDATE `'.$this->users_table.'` ';
244
			$sql .= 'SET `login_when`=\''.$get_ts.'\', `login_ip`=\''.$get_ip.'\' ';
245
			$sql .= 'WHERE `user_id`=\''.$user_id.'\'';
246
			$database->query($sql);
247
		}else {
248
		  $num_rows = 0;
249
		}
250
		// Return if the user exists or not
251
		return $num_rows;
252
	}
253

    
254
	// Increase the count for login attemps
255
	function increase_attemps() {
256
		if(!isset($_SESSION['ATTEMPS'])) {
257
			$_SESSION['ATTEMPS'] = 0;
258
		} else {
259
			$_SESSION['ATTEMPS'] = $this->get_session('ATTEMPS')+1;
260
		}
261
		$this->display_login();
262
	}
263

    
264
	// Function to set a "remembering" cookie for the user - removed
265
	function remember($user_id) {
266
		return true;
267
	}
268

    
269
	// Function to check if a user has been remembered - removed
270
	function is_remembered()
271
	{
272
		return false;
273
	}
274

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

    
343
			$template->set_var('CHARSET', $charset);
344

    
345
			$template->parse('main', 'mainBlock', false);
346
			$template->pparse('output', 'page');
347
		}
348
	}
349

    
350
	// sanities the REMEMBER_KEY cookie to avoid SQL injection
351
	function get_safe_remember_key() {
352
		if (!((strlen($_COOKIE['REMEMBER_KEY']) == 23) && (substr($_COOKIE['REMEMBER_KEY'], 11, 1) == '_'))) return '';
353
		// create a clean cookie (XXXXXXXXXXX_YYYYYYYYYYY) where X:= numeric, Y:= hash
354
		$clean_cookie = sprintf('%011d', (int) substr($_COOKIE['REMEMBER_KEY'], 0, 11)) . substr($_COOKIE['REMEMBER_KEY'], 11);
355
		return ($clean_cookie == $_COOKIE['REMEMBER_KEY']) ? $this->add_slashes($clean_cookie) : '';
356
	}
357

    
358
	// Warn user that they have had to many login attemps
359
	function warn() {
360
//		header('Location: '.$this->warning_url);
361
		$this->send_header($this->warning_url);
362
		exit(0);
363
	}
364

    
365
}
(14-14/25)