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