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
50 1833 Luisehahne
        if( is_array($this->get_post($username_fieldname) ) ) {
51
            $_POST[$username_fieldname]=implode(',',$this->get_post($username_fieldname));
52
        }
53
		$this->username = htmlspecialchars (strtolower( $this->get_post($username_fieldname) ), ENT_QUOTES);
54
55
        if( is_array($this->get_post($password_fieldname) ) ) {
56
            $_POST[$password_fieldname]=implode(',',$this->get_post($password_fieldname));
57
        }
58 1367 Luisehahne
		$this->password = $this->get_post($password_fieldname);
59 1833 Luisehahne
60 1367 Luisehahne
		// Figure out if the "remember me" option has been checked
61
		if($this->get_post('remember') == 'true') {
62
			$this->remember = $this->get_post('remember');
63
		} else {
64
			$this->remember = false;
65
		}
66
		// Get the length of the supplied username and password
67
		if($this->get_post($username_fieldname) != '') {
68
			$this->username_len = strlen($this->username);
69
			$this->password_len = strlen($this->password);
70
		}
71 1833 Luisehahne
72 1834 Luisehahne
		$aServerUrl = $this->mb_parse_url(WB_URL);
73
74 1833 Luisehahne
        $sServerUrl = $_SERVER['SERVER_NAME'];
75 1834 Luisehahne
        $sServerScheme = isset($_SERVER['REQUEST_SCHEME']) ? $_SERVER['REQUEST_SCHEME'] : isset($aServerUrl['scheme']) ? $aServerUrl['scheme'] : ' http';
76 1833 Luisehahne
        $sServerPath = $_SERVER['SCRIPT_NAME'];
77 1367 Luisehahne
		// If the url is blank, set it to the default url
78
		$this->url = $this->get_post('url');
79 1834 Luisehahne
        $aUrl = $this->mb_parse_url( $this->url );
80 1833 Luisehahne
        $this->url = isset($aRedirecthUrl['host']) &&($sServerUrl==$aUrl['host']) ? $this->url:ADMIN_URL.'/start/index.php';
81 1367 Luisehahne
		if ($this->redirect_url!='') {
82 1834 Luisehahne
            $aRedirecthUrl = $this->mb_parse_url( $this->redirect_url );
83 1833 Luisehahne
            $this->redirect_url = isset($aRedirecthUrl['host']) &&($sServerUrl==$aRedirecthUrl['host']) ? $this->redirect_url:$sServerScheme.'://'.$sServerUrl;
84 1367 Luisehahne
			$this->url = $this->redirect_url;
85 1529 Luisehahne
		}
86 1367 Luisehahne
		if(strlen($this->url) < 2) {
87 1834 Luisehahne
            $aDefaultUrl = $this->mb_parse_url( $this->default_url );
88 1833 Luisehahne
            $this->default_url = isset($aDefaultUrl['host']) &&($sServerUrl==$aDefaultUrl['host']) ? $this->default_url:$sServerScheme.'://'.$sServerUrl;
89
			$this->url = $this->default_url;
90 1367 Luisehahne
		}
91 1833 Luisehahne
92 1367 Luisehahne
		if($this->is_authenticated() == true) {
93
			// User already logged-in, so redirect to default url
94 1709 Luisehahne
				$this->send_header($this->url);
95 1367 Luisehahne
		} elseif($this->is_remembered() == true) {
96
			// User has been "remembered"
97
			// Get the users password
98
			// $database = new database();
99 1486 DarkViper
			$sql  = 'SELECT * FROM `'.$this->users_table.'` ';
100
			$sql .= 'WHERE `user_id`=\''.$this->get_safe_remember_key().'\'';
101
			$query_details = $database->query($sql);
102 1789 Luisehahne
			$fetch_details = $query_details->fetchRow(MYSQL_ASSOC);
103 1367 Luisehahne
			$this->username = $fetch_details['username'];
104
			$this->password = $fetch_details['password'];
105
			// Check if the user exists (authenticate them)
106
			if($this->authenticate()) {
107
				// Authentication successful
108 1709 Luisehahne
				$this->send_header($this->url);
109 1367 Luisehahne
			} else {
110 1709 Luisehahne
				$this->message = $MESSAGE['LOGIN_AUTHENTICATION_FAILED'];
111 1367 Luisehahne
				$this->increase_attemps();
112
			}
113
		} elseif($this->username == '' AND $this->password == '') {
114 1709 Luisehahne
			$this->message = $MESSAGE['LOGIN_BOTH_BLANK'];
115 1566 Luisehahne
			$this->display_login();
116 1367 Luisehahne
		} elseif($this->username == '') {
117 1709 Luisehahne
			$this->message = $MESSAGE['LOGIN_USERNAME_BLANK'];
118 1367 Luisehahne
			$this->increase_attemps();
119
		} elseif($this->password == '') {
120 1709 Luisehahne
			$this->message = $MESSAGE['LOGIN_PASSWORD_BLANK'];
121 1367 Luisehahne
			$this->increase_attemps();
122
		} elseif($this->username_len < $config_array['MIN_USERNAME_LEN']) {
123 1709 Luisehahne
			$this->message = $MESSAGE['LOGIN_USERNAME_TOO_SHORT'];
124 1367 Luisehahne
			$this->increase_attemps();
125
		} elseif($this->password_len < $config_array['MIN_PASSWORD_LEN']) {
126 1709 Luisehahne
			$this->message = $MESSAGE['LOGIN_PASSWORD_TOO_SHORT'];
127 1367 Luisehahne
			$this->increase_attemps();
128
		} elseif($this->username_len > $config_array['MAX_USERNAME_LEN']) {
129 1709 Luisehahne
			$this->message = $MESSAGE['LOGIN_USERNAME_TOO_LONG'];
130 1367 Luisehahne
			$this->increase_attemps();
131
		} elseif($this->password_len > $config_array['MAX_PASSWORD_LEN']) {
132 1709 Luisehahne
			$this->message = $MESSAGE['LOGIN_PASSWORD_TOO_LONG'];
133 1367 Luisehahne
			$this->increase_attemps();
134
		} else {
135
			// Check if the user exists (authenticate them)
136
			$this->password = md5($this->password);
137
			if($this->authenticate()) {
138
				// Authentication successful
139 1709 Luisehahne
				$this->send_header($this->url);
140 1367 Luisehahne
			} else {
141 1709 Luisehahne
				$this->message = $MESSAGE['LOGIN_AUTHENTICATION_FAILED'];
142 1367 Luisehahne
				$this->increase_attemps();
143
			}
144
		}
145
	}
146
147
	// Authenticate the user (check if they exist in the database)
148
	function authenticate() {
149
		global $database;
150
		// Get user information
151
		// $database = new database();
152 1474 DarkViper
		// $query = 'SELECT * FROM `'.$this->users_table.'` WHERE MD5(`username`) = "'.md5($this->username).'" AND `password` = "'.$this->password.'" AND `active` = 1';
153 1367 Luisehahne
 		$loginname = ( preg_match('/[\;\=\&\|\<\> ]/',$this->username) ? '' : $this->username );
154 1486 DarkViper
		$sql  = 'SELECT * FROM `'.$this->users_table.'` ';
155
		$sql .= 'WHERE `username`=\''.$loginname.'\' AND `password`=\''.$this->password.'\' AND `active`=1';
156
		$results = $database->query($sql);
157 1782 Luisehahne
		$results_array = $results->fetchRow(MYSQL_ASSOC);
158 1367 Luisehahne
		$num_rows = $results->numRows();
159
		if($num_rows == 1) {
160
			$user_id = $results_array['user_id'];
161
			$this->user_id = $user_id;
162
			$_SESSION['USER_ID'] = $user_id;
163
			$_SESSION['GROUP_ID'] = $results_array['group_id'];
164
			$_SESSION['GROUPS_ID'] = $results_array['groups_id'];
165
			$_SESSION['USERNAME'] = $results_array['username'];
166
			$_SESSION['DISPLAY_NAME'] = $results_array['display_name'];
167
			$_SESSION['EMAIL'] = $results_array['email'];
168
			$_SESSION['HOME_FOLDER'] = $results_array['home_folder'];
169
			// Run remember function if needed
170
			if($this->remember == true) {
171
				$this->remember($this->user_id);
172
			}
173
			// Set language
174
			if($results_array['language'] != '') {
175
				$_SESSION['LANGUAGE'] = $results_array['language'];
176
			}
177
			// Set timezone
178
			if($results_array['timezone'] != '-72000') {
179
				$_SESSION['TIMEZONE'] = $results_array['timezone'];
180
			} else {
181
				// Set a session var so apps can tell user is using default tz
182
				$_SESSION['USE_DEFAULT_TIMEZONE'] = true;
183
			}
184
			// Set date format
185
			if($results_array['date_format'] != '') {
186
				$_SESSION['DATE_FORMAT'] = $results_array['date_format'];
187
			} else {
188
				// Set a session var so apps can tell user is using default date format
189
				$_SESSION['USE_DEFAULT_DATE_FORMAT'] = true;
190
			}
191
			// Set time format
192
			if($results_array['time_format'] != '') {
193
				$_SESSION['TIME_FORMAT'] = $results_array['time_format'];
194
			} else {
195
				// Set a session var so apps can tell user is using default time format
196
				$_SESSION['USE_DEFAULT_TIME_FORMAT'] = true;
197
			}
198
199
			// Get group information
200
			$_SESSION['SYSTEM_PERMISSIONS'] = array();
201
			$_SESSION['MODULE_PERMISSIONS'] = array();
202
			$_SESSION['TEMPLATE_PERMISSIONS'] = array();
203
			$_SESSION['GROUP_NAME'] = array();
204
205 1789 Luisehahne
            $aGroupsIds = ((explode(',', $this->get_session('GROUPS_ID'))));
206
            $bOnlyAdminGroup = $this->ami_group_member('1') && (sizeof($aGroupsIds) == 1);
207
			$first_group = true;
208 1782 Luisehahne
209 1789 Luisehahne
			foreach ( $aGroupsIds  as $cur_group_id)
210 1367 Luisehahne
            {
211 1486 DarkViper
				$sql = 'SELECT * FROM `'.$this->groups_table.'` WHERE `group_id`=\''.$cur_group_id.'\'';
212 1789 Luisehahne
				if($results = $database->query($sql)) {
213
    				$results_array = $results->fetchRow(MYSQL_ASSOC);
214
    				$_SESSION['GROUP_NAME'][$cur_group_id] = $results_array['name'];
215
				}
216
217 1367 Luisehahne
				// Set system permissions
218 1789 Luisehahne
				if( ($results_array['system_permissions'] != '') ) {
219
                    switch ($cur_group_id) :
220
                        case 1:
221 1790 Luisehahne
                            if( $this->user_id == 1) {
222
               					$_SESSION['SYSTEM_PERMISSIONS'] = array_merge($_SESSION['SYSTEM_PERMISSIONS'], explode(',', $results_array['system_permissions']));
223
                            } else {
224
                                $_SESSION['SYSTEM_PERMISSIONS'] = explode(',', $results_array['system_permissions']);
225 1789 Luisehahne
                            }
226 1790 Luisehahne
227 1789 Luisehahne
                            break;
228
                        default:
229
        					$_SESSION['SYSTEM_PERMISSIONS'] = array_merge($_SESSION['SYSTEM_PERMISSIONS'], explode(',', $results_array['system_permissions']));
230
                    endswitch;
231 1367 Luisehahne
				}
232 1789 Luisehahne
233 1367 Luisehahne
				// Set module permissions
234 1782 Luisehahne
				if( $results_array['module_permissions'] != '' ) {
235 1367 Luisehahne
					if ($first_group) {
236 1782 Luisehahne
                  	$_SESSION['MODULE_PERMISSIONS'] = explode(',', $results_array['module_permissions']);
237
                  } else {
238
                  	$_SESSION['MODULE_PERMISSIONS'] = array_intersect($_SESSION['MODULE_PERMISSIONS'], explode(',', $results_array['module_permissions']));
239 1367 Luisehahne
					}
240
				}
241
				// Set template permissions
242
				if($results_array['template_permissions'] != '') {
243
					if ($first_group) {
244 1782 Luisehahne
                      	$_SESSION['TEMPLATE_PERMISSIONS'] = explode(',', $results_array['template_permissions']);
245
                      } else {
246
                      	$_SESSION['TEMPLATE_PERMISSIONS'] = array_intersect($_SESSION['TEMPLATE_PERMISSIONS'], explode(',', $results_array['template_permissions']));
247 1367 Luisehahne
					}
248
				}
249
				$first_group = false;
250 1709 Luisehahne
			}
251 1367 Luisehahne
252 1790 Luisehahne
//            if( $$bOnlyAdminGroup ) {
253
//    			$_SESSION['MODULE_PERMISSIONS'] = array();
254
//    			$_SESSION['TEMPLATE_PERMISSIONS'] = array();
255
//            }
256 1782 Luisehahne
257 1367 Luisehahne
			// Update the users table with current ip and timestamp
258
			$get_ts = time();
259
			$get_ip = $_SERVER['REMOTE_ADDR'];
260 1486 DarkViper
			$sql  = 'UPDATE `'.$this->users_table.'` ';
261
			$sql .= 'SET `login_when`=\''.$get_ts.'\', `login_ip`=\''.$get_ip.'\' ';
262
			$sql .= 'WHERE `user_id`=\''.$user_id.'\'';
263
			$database->query($sql);
264 1367 Luisehahne
		}else {
265
		  $num_rows = 0;
266
		}
267
		// Return if the user exists or not
268
		return $num_rows;
269
	}
270
271
	// Increase the count for login attemps
272
	function increase_attemps() {
273
		if(!isset($_SESSION['ATTEMPS'])) {
274
			$_SESSION['ATTEMPS'] = 0;
275
		} else {
276
			$_SESSION['ATTEMPS'] = $this->get_session('ATTEMPS')+1;
277
		}
278
		$this->display_login();
279
	}
280 1709 Luisehahne
281
	// Function to set a "remembering" cookie for the user - removed
282 1367 Luisehahne
	function remember($user_id) {
283 1474 DarkViper
		return true;
284 1367 Luisehahne
	}
285 1709 Luisehahne
286
	// Function to check if a user has been remembered - removed
287 1367 Luisehahne
	function is_remembered()
288
	{
289 1474 DarkViper
		return false;
290 1367 Luisehahne
	}
291
292
	// Display the login screen
293
	function display_login() {
294
		// Get language vars
295 1709 Luisehahne
		global $MESSAGE, $MENU, $TEXT;
296 1367 Luisehahne
		// If attemps more than allowed, warn the user
297
		if($this->get_session('ATTEMPS') > $this->max_attemps) {
298
			$this->warn();
299
		}
300
		// Show the login form
301
		if($this->frontend != true) {
302 1709 Luisehahne
			//require_once(WB_PATH.'/include/phplib/template.inc');
303 1529 Luisehahne
			// $template = new Template($this->template_dir);
304
			// Setup template object, parse vars to it, then parse it
305 1625 Luisehahne
			$template = new Template(dirname($this->correct_theme_source($this->template_file)));
306 1367 Luisehahne
			$template->set_file('page', $this->template_file);
307
			$template->set_block('page', 'mainBlock', 'main');
308
			if($this->remember_me_option != true) {
309
				$template->set_var('DISPLAY_REMEMBER_ME', 'display: none;');
310
			} else {
311
				$template->set_var('DISPLAY_REMEMBER_ME', '');
312
			}
313
			$template->set_var(array(
314 1474 DarkViper
				'ACTION_URL' => $this->login_url,
315 1833 Luisehahne
				'URL' => $this->default_url,
316 1474 DarkViper
				'ATTEMPS' => $this->get_session('ATTEMPS'),
317
				'USERNAME' => $this->username,
318
				'USERNAME_FIELDNAME' => $this->username_fieldname,
319
				'PASSWORD_FIELDNAME' => $this->password_fieldname,
320
				'MESSAGE' => $this->message,
321 1709 Luisehahne
				'WEBSITE_TITLE' => WEBSITE_TITLE,
322
				'TEXT_ADMINISTRATION' => $TEXT['ADMINISTRATION'],
323 1474 DarkViper
				'INTERFACE_DIR_URL' =>  ADMIN_URL.'/interface',
324
				'MAX_USERNAME_LEN' => $this->max_username_len,
325
				'MAX_PASSWORD_LEN' => $this->max_password_len,
326 1709 Luisehahne
				'ADMIN_URL' => ADMIN_URL,
327 1474 DarkViper
				'WB_URL' => WB_URL,
328 1709 Luisehahne
				'URL_VIEW' => WB_URL,
329 1474 DarkViper
				'THEME_URL' => THEME_URL,
330
				'VERSION' => VERSION,
331 1709 Luisehahne
				'SP' => (defined('SP') ? SP : ''),
332 1474 DarkViper
				'REVISION' => REVISION,
333
				'LANGUAGE' => strtolower(LANGUAGE),
334
				'FORGOTTEN_DETAILS_APP' => $this->forgotten_details_app,
335
				'TEXT_FORGOTTEN_DETAILS' => $TEXT['FORGOTTEN_DETAILS'],
336
				'TEXT_USERNAME' => $TEXT['USERNAME'],
337
				'TEXT_PASSWORD' => $TEXT['PASSWORD'],
338
				'TEXT_REMEMBER_ME' => $TEXT['REMEMBER_ME'],
339
				'TEXT_LOGIN' => $TEXT['LOGIN'],
340 1709 Luisehahne
				'TITLE_LOGOUT' => $MENU['LOGIN'],
341
				'TEXT_RESET' => $TEXT['RESET'],
342 1474 DarkViper
				'TEXT_HOME' => $TEXT['HOME'],
343 1709 Luisehahne
				'TITLE_VIEW' => $TEXT['WEBSITE'],
344 1474 DarkViper
				'PAGES_DIRECTORY' => PAGES_DIRECTORY,
345 1709 Luisehahne
				'SECTION_NAME' => $MENU['LOGIN'],
346
				'SECTION_LOGIN' => $MENU['LOGIN'],
347
				'LOGIN_DISPLAY_HIDDEN' => !$this->is_authenticated() ? 'hidden' : '',
348
				'LOGIN_DISPLAY_NONE' => !$this->is_authenticated() ? 'none' : '',
349
				'LOGIN_LINK' => $_SERVER['SCRIPT_NAME'],
350
				'LOGIN_ICON' => 'login',
351
				'START_ICON' => 'blank',
352
				'URL_HELP' => 'http://www.websitebaker.org/',
353 1474 DarkViper
				)
354
			);
355 1367 Luisehahne
			if(defined('DEFAULT_CHARSET')) {
356
				$charset=DEFAULT_CHARSET;
357
			} else {
358
				$charset='utf-8';
359
			}
360 1529 Luisehahne
361 1709 Luisehahne
			$template->set_var('CHARSET', $charset);
362
363 1367 Luisehahne
			$template->parse('main', 'mainBlock', false);
364
			$template->pparse('output', 'page');
365
		}
366
	}
367
368
	// sanities the REMEMBER_KEY cookie to avoid SQL injection
369
	function get_safe_remember_key() {
370
		if (!((strlen($_COOKIE['REMEMBER_KEY']) == 23) && (substr($_COOKIE['REMEMBER_KEY'], 11, 1) == '_'))) return '';
371
		// create a clean cookie (XXXXXXXXXXX_YYYYYYYYYYY) where X:= numeric, Y:= hash
372
		$clean_cookie = sprintf('%011d', (int) substr($_COOKIE['REMEMBER_KEY'], 0, 11)) . substr($_COOKIE['REMEMBER_KEY'], 11);
373
		return ($clean_cookie == $_COOKIE['REMEMBER_KEY']) ? $this->add_slashes($clean_cookie) : '';
374
	}
375 1709 Luisehahne
376 1367 Luisehahne
	// Warn user that they have had to many login attemps
377
	function warn() {
378 1709 Luisehahne
//		header('Location: '.$this->warning_url);
379
		$this->send_header($this->warning_url);
380 1367 Luisehahne
		exit(0);
381
	}
382 1709 Luisehahne
383 1367 Luisehahne
}