Project

General

Profile

1 1367 Luisehahne
<?php
2
/**
3
 *
4 1529 Luisehahne
 * @category        framework
5
 * @package         backend login
6 1782 Luisehahne
 * @author          Ryan Djurovich (2004-2009), WebsiteBaker Project
7
 * @copyright       2009-2012, WebsiteBaker Org. e.V.
8 1367 Luisehahne
 * @link			http://www.websitebaker2.org/
9
 * @license         http://www.gnu.org/licenses/gpl.html
10
 * @platform        WebsiteBaker 2.8.x
11 1374 Luisehahne
 * @requirements    PHP 5.2.2 and higher
12 1367 Luisehahne
 * @version         $Id$
13
 * @filesource		$HeadURL$
14
 * @lastmodified    $Date$
15
 *
16
 */
17 1496 DarkViper
/* -------------------------------------------------------- */
18
// Must include code to stop this file being accessed directly
19 1499 DarkViper
if(!defined('WB_PATH')) {
20
	require_once(dirname(__FILE__).'/globalExceptionHandler.php');
21
	throw new IllegalFileException();
22
}
23 1496 DarkViper
/* -------------------------------------------------------- */
24 1367 Luisehahne
define('LOGIN_CLASS_LOADED', true);
25
26
// Load the other required class files if they are not already loaded
27 1782 Luisehahne
if(!class_exists('admin', false)){ include(WB_PATH.'/framework/class.admin.php'); }
28 1367 Luisehahne
// Get WB version
29
require_once(ADMIN_PATH.'/interface/version.php');
30
31
class login extends admin {
32 1378 Luisehahne
	public function __construct($config_array) {
33 1367 Luisehahne
		// Get language vars
34
		global $MESSAGE, $database;
35 1378 Luisehahne
		parent::__construct();
36 1367 Luisehahne
		// Get configuration values
37 1474 DarkViper
		while(list($key, $value) = each($config_array)) {
38
			$this->{(strtolower($key))} = $value;
39
		}
40
		if(!isset($this->redirect_url)) { $this->redirect_url = ''; }
41 1367 Luisehahne
		// 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 1529 Luisehahne
		}
68 1367 Luisehahne
		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 1709 Luisehahne
//				header("Location: ".$this->url);
74
//				exit(0);
75
				$this->send_header($this->url);
76 1367 Luisehahne
		} elseif($this->is_remembered() == true) {
77
			// User has been "remembered"
78
			// Get the users password
79
			// $database = new database();
80 1486 DarkViper
			$sql  = 'SELECT * FROM `'.$this->users_table.'` ';
81
			$sql .= 'WHERE `user_id`=\''.$this->get_safe_remember_key().'\'';
82
			$query_details = $database->query($sql);
83 1789 Luisehahne
			$fetch_details = $query_details->fetchRow(MYSQL_ASSOC);
84 1367 Luisehahne
			$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 1709 Luisehahne
//				header("Location: ".$this->url);
90
//				exit(0);
91
				$this->send_header($this->url);
92 1367 Luisehahne
			} else {
93 1709 Luisehahne
				$this->message = $MESSAGE['LOGIN_AUTHENTICATION_FAILED'];
94 1367 Luisehahne
				$this->increase_attemps();
95
			}
96
		} elseif($this->username == '' AND $this->password == '') {
97 1709 Luisehahne
			$this->message = $MESSAGE['LOGIN_BOTH_BLANK'];
98 1566 Luisehahne
			$this->display_login();
99 1367 Luisehahne
		} elseif($this->username == '') {
100 1709 Luisehahne
			$this->message = $MESSAGE['LOGIN_USERNAME_BLANK'];
101 1367 Luisehahne
			$this->increase_attemps();
102
		} elseif($this->password == '') {
103 1709 Luisehahne
			$this->message = $MESSAGE['LOGIN_PASSWORD_BLANK'];
104 1367 Luisehahne
			$this->increase_attemps();
105
		} elseif($this->username_len < $config_array['MIN_USERNAME_LEN']) {
106 1709 Luisehahne
			$this->message = $MESSAGE['LOGIN_USERNAME_TOO_SHORT'];
107 1367 Luisehahne
			$this->increase_attemps();
108
		} elseif($this->password_len < $config_array['MIN_PASSWORD_LEN']) {
109 1709 Luisehahne
			$this->message = $MESSAGE['LOGIN_PASSWORD_TOO_SHORT'];
110 1367 Luisehahne
			$this->increase_attemps();
111
		} elseif($this->username_len > $config_array['MAX_USERNAME_LEN']) {
112 1709 Luisehahne
			$this->message = $MESSAGE['LOGIN_USERNAME_TOO_LONG'];
113 1367 Luisehahne
			$this->increase_attemps();
114
		} elseif($this->password_len > $config_array['MAX_PASSWORD_LEN']) {
115 1709 Luisehahne
			$this->message = $MESSAGE['LOGIN_PASSWORD_TOO_LONG'];
116 1367 Luisehahne
			$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 1709 Luisehahne
// 				echo $this->url;exit();
123
// 				header("Location: ".$this->url);
124
// 				exit(0);
125
				$this->send_header($this->url);
126 1367 Luisehahne
			} else {
127 1709 Luisehahne
				$this->message = $MESSAGE['LOGIN_AUTHENTICATION_FAILED'];
128 1367 Luisehahne
				$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 1474 DarkViper
		// $query = 'SELECT * FROM `'.$this->users_table.'` WHERE MD5(`username`) = "'.md5($this->username).'" AND `password` = "'.$this->password.'" AND `active` = 1';
139 1367 Luisehahne
 		$loginname = ( preg_match('/[\;\=\&\|\<\> ]/',$this->username) ? '' : $this->username );
140 1486 DarkViper
		$sql  = 'SELECT * FROM `'.$this->users_table.'` ';
141
		$sql .= 'WHERE `username`=\''.$loginname.'\' AND `password`=\''.$this->password.'\' AND `active`=1';
142
		$results = $database->query($sql);
143 1782 Luisehahne
		$results_array = $results->fetchRow(MYSQL_ASSOC);
144 1367 Luisehahne
		$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 1789 Luisehahne
            $aGroupsIds = ((explode(',', $this->get_session('GROUPS_ID'))));
192
            $bOnlyAdminGroup = $this->ami_group_member('1') && (sizeof($aGroupsIds) == 1);
193
			$first_group = true;
194 1782 Luisehahne
195 1789 Luisehahne
			foreach ( $aGroupsIds  as $cur_group_id)
196 1367 Luisehahne
            {
197 1486 DarkViper
				$sql = 'SELECT * FROM `'.$this->groups_table.'` WHERE `group_id`=\''.$cur_group_id.'\'';
198 1789 Luisehahne
				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 1367 Luisehahne
				// Set system permissions
204 1789 Luisehahne
				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 1367 Luisehahne
				}
215 1789 Luisehahne
216 1367 Luisehahne
				// Set module permissions
217 1782 Luisehahne
				if( $results_array['module_permissions'] != '' ) {
218 1367 Luisehahne
					if ($first_group) {
219 1782 Luisehahne
                  	$_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 1367 Luisehahne
					}
223
				}
224
				// Set template permissions
225
				if($results_array['template_permissions'] != '') {
226
					if ($first_group) {
227 1782 Luisehahne
                      	$_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 1367 Luisehahne
					}
231
				}
232
				$first_group = false;
233 1709 Luisehahne
			}
234 1367 Luisehahne
235 1789 Luisehahne
            if( $$bOnlyAdminGroup ) {
236 1782 Luisehahne
    			$_SESSION['MODULE_PERMISSIONS'] = array();
237
    			$_SESSION['TEMPLATE_PERMISSIONS'] = array();
238
            }
239
240 1367 Luisehahne
			// Update the users table with current ip and timestamp
241
			$get_ts = time();
242
			$get_ip = $_SERVER['REMOTE_ADDR'];
243 1486 DarkViper
			$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 1367 Luisehahne
		}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 1709 Luisehahne
264
	// Function to set a "remembering" cookie for the user - removed
265 1367 Luisehahne
	function remember($user_id) {
266 1474 DarkViper
		return true;
267 1367 Luisehahne
	}
268 1709 Luisehahne
269
	// Function to check if a user has been remembered - removed
270 1367 Luisehahne
	function is_remembered()
271
	{
272 1474 DarkViper
		return false;
273 1367 Luisehahne
	}
274
275
	// Display the login screen
276
	function display_login() {
277
		// Get language vars
278 1709 Luisehahne
		global $MESSAGE, $MENU, $TEXT;
279 1367 Luisehahne
		// 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 1709 Luisehahne
			//require_once(WB_PATH.'/include/phplib/template.inc');
286 1529 Luisehahne
			// $template = new Template($this->template_dir);
287
			// Setup template object, parse vars to it, then parse it
288 1625 Luisehahne
			$template = new Template(dirname($this->correct_theme_source($this->template_file)));
289 1367 Luisehahne
			$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 1474 DarkViper
				'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 1709 Luisehahne
				'WEBSITE_TITLE' => WEBSITE_TITLE,
304
				'TEXT_ADMINISTRATION' => $TEXT['ADMINISTRATION'],
305 1474 DarkViper
				'INTERFACE_DIR_URL' =>  ADMIN_URL.'/interface',
306
				'MAX_USERNAME_LEN' => $this->max_username_len,
307
				'MAX_PASSWORD_LEN' => $this->max_password_len,
308 1709 Luisehahne
				'ADMIN_URL' => ADMIN_URL,
309 1474 DarkViper
				'WB_URL' => WB_URL,
310 1709 Luisehahne
				'URL_VIEW' => WB_URL,
311 1474 DarkViper
				'THEME_URL' => THEME_URL,
312
				'VERSION' => VERSION,
313 1709 Luisehahne
				'SP' => (defined('SP') ? SP : ''),
314 1474 DarkViper
				'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 1709 Luisehahne
				'TITLE_LOGOUT' => $MENU['LOGIN'],
323
				'TEXT_RESET' => $TEXT['RESET'],
324 1474 DarkViper
				'TEXT_HOME' => $TEXT['HOME'],
325 1709 Luisehahne
				'TITLE_VIEW' => $TEXT['WEBSITE'],
326 1474 DarkViper
				'PAGES_DIRECTORY' => PAGES_DIRECTORY,
327 1709 Luisehahne
				'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 1474 DarkViper
				)
336
			);
337 1367 Luisehahne
			if(defined('DEFAULT_CHARSET')) {
338
				$charset=DEFAULT_CHARSET;
339
			} else {
340
				$charset='utf-8';
341
			}
342 1529 Luisehahne
343 1709 Luisehahne
			$template->set_var('CHARSET', $charset);
344
345 1367 Luisehahne
			$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 1709 Luisehahne
358 1367 Luisehahne
	// Warn user that they have had to many login attemps
359
	function warn() {
360 1709 Luisehahne
//		header('Location: '.$this->warning_url);
361
		$this->send_header($this->warning_url);
362 1367 Luisehahne
		exit(0);
363
	}
364 1709 Luisehahne
365 1367 Luisehahne
}