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 1790 2012-10-14 18:24:34Z 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 20:24:34 +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( $this->user_id == 1) {
208
               					$_SESSION['SYSTEM_PERMISSIONS'] = array_merge($_SESSION['SYSTEM_PERMISSIONS'], explode(',', $results_array['system_permissions']));
209
                            } else {
210
                                $_SESSION['SYSTEM_PERMISSIONS'] = explode(',', $results_array['system_permissions']);
211
                            }
212

    
213
                            break;
214
                        default:
215
        					$_SESSION['SYSTEM_PERMISSIONS'] = array_merge($_SESSION['SYSTEM_PERMISSIONS'], explode(',', $results_array['system_permissions']));
216
                    endswitch;
217
				}
218

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

    
238
//            if( $$bOnlyAdminGroup ) {
239
//    			$_SESSION['MODULE_PERMISSIONS'] = array();
240
//    			$_SESSION['TEMPLATE_PERMISSIONS'] = array();
241
//            }
242

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

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

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

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

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

    
346
			$template->set_var('CHARSET', $charset);
347

    
348
			$template->parse('main', 'mainBlock', false);
349
			$template->pparse('output', 'page');
350
		}
351
	}
352

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

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

    
368
}
(14-14/25)