2 /***************************************************************************
5 * begin : Saturday, Feb 13, 2001
6 * copyright : (C) 2001 The phpBB Group
7 * email : support@phpbb.com
9 * $Id: login.php,v 1.47.2.25 2006/12/16 13:11:24 acydburn Exp $
12 ***************************************************************************/
14 /***************************************************************************
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
21 ***************************************************************************/
24 // Allow people to reach login page if
27 define("IN_LOGIN", true);
29 define('IN_PHPBB', true);
30 $phpbb_root_path = './';
31 include($phpbb_root_path . 'extension.inc');
32 include($phpbb_root_path . 'common.'.$phpEx);
35 // Set page ID for session management
37 $userdata = session_pagestart($user_ip, PAGE_LOGIN);
38 init_userprefs($userdata);
40 // End session management
44 if (!empty($HTTP_POST_VARS['sid']) || !empty($HTTP_GET_VARS['sid']))
46 $sid = (!empty($HTTP_POST_VARS['sid'])) ? $HTTP_POST_VARS['sid'] : $HTTP_GET_VARS['sid'];
53 if( isset($HTTP_POST_VARS['login']) || isset($HTTP_GET_VARS['login']) || isset($HTTP_POST_VARS['logout']) || isset($HTTP_GET_VARS['logout']) )
55 if( ( isset($HTTP_POST_VARS['login']) || isset($HTTP_GET_VARS['login']) ) && (!$userdata['session_logged_in'] || isset($HTTP_POST_VARS['admin'])) )
57 $username = isset($HTTP_POST_VARS['username']) ? phpbb_clean_username($HTTP_POST_VARS['username']) : '';
58 $password = isset($HTTP_POST_VARS['password']) ? $HTTP_POST_VARS['password'] : '';
60 $sql = "SELECT user_id, username, user_password, user_active, user_level, user_login_tries, user_last_login_try
61 FROM " . USERS_TABLE . "
62 WHERE username = '" . str_replace("\\'", "''", $username) . "'";
63 if ( !($result = $db->sql_query($sql)) )
65 message_die(GENERAL_ERROR, 'Error in obtaining userdata', '', __LINE__, __FILE__, $sql);
68 if( $row = $db->sql_fetchrow($result) )
70 if( $row['user_level'] != ADMIN && $board_config['board_disable'] )
72 redirect(append_sid("index.$phpEx", true));
76 // If the last login is more than x minutes ago, then reset the login tries/time
77 if ($row['user_last_login_try'] && $board_config['login_reset_time'] && $row['user_last_login_try'] < (time() - ($board_config['login_reset_time'] * 60)))
79 $db->sql_query('UPDATE ' . USERS_TABLE . ' SET user_login_tries = 0, user_last_login_try = 0 WHERE user_id = ' . $row['user_id']);
80 $row['user_last_login_try'] = $row['user_login_tries'] = 0;
83 // Check to see if user is allowed to login again... if his tries are exceeded
84 if ($row['user_last_login_try'] && $board_config['login_reset_time'] && $board_config['max_login_attempts'] &&
85 $row['user_last_login_try'] >= (time() - ($board_config['login_reset_time'] * 60)) && $row['user_login_tries'] >= $board_config['max_login_attempts'] && $userdata['user_level'] != ADMIN)
87 message_die(GENERAL_MESSAGE, sprintf($lang['Login_attempts_exceeded'], $board_config['max_login_attempts'], $board_config['login_reset_time']));
90 if( md5($password) == $row['user_password'] && $row['user_active'] )
92 $autologin = ( isset($HTTP_POST_VARS['autologin']) ) ? TRUE : 0;
94 $admin = (isset($HTTP_POST_VARS['admin'])) ? 1 : 0;
95 $session_id = session_begin($row['user_id'], $user_ip, PAGE_INDEX, FALSE, $autologin, $admin);
98 $db->sql_query('UPDATE ' . USERS_TABLE . ' SET user_login_tries = 0, user_last_login_try = 0 WHERE user_id = ' . $row['user_id']);
102 $url = ( !empty($HTTP_POST_VARS['redirect']) ) ? str_replace('&', '&', htmlspecialchars($HTTP_POST_VARS['redirect'])) : "index.$phpEx";
103 redirect(append_sid($url, true));
107 message_die(CRITICAL_ERROR, "Couldn't start session : login", "", __LINE__, __FILE__);
110 // Only store a failed login attempt for an active user - inactive users can't login even with a correct password
111 elseif( $row['user_active'] )
113 // Save login tries and last login
114 if ($row['user_id'] != ANONYMOUS)
116 $sql = 'UPDATE ' . USERS_TABLE . '
117 SET user_login_tries = user_login_tries + 1, user_last_login_try = ' . time() . '
118 WHERE user_id = ' . $row['user_id'];
119 $db->sql_query($sql);
123 $redirect = ( !empty($HTTP_POST_VARS['redirect']) ) ? str_replace('&', '&', htmlspecialchars($HTTP_POST_VARS['redirect'])) : '';
124 $redirect = str_replace('?', '&', $redirect);
126 if (strstr(urldecode($redirect), "\n") || strstr(urldecode($redirect), "\r") || strstr(urldecode($redirect), ';url'))
128 message_die(GENERAL_ERROR, 'Tried to redirect to potentially insecure url.');
131 $template->assign_vars(array(
132 'META' => "<meta http-equiv=\"refresh\" content=\"3;url=login.$phpEx?redirect=$redirect\">")
135 $message = $lang['Error_login'] . '<br /><br />' . sprintf($lang['Click_return_login'], "<a href=\"login.$phpEx?redirect=$redirect\">", '</a>') . '<br /><br />' . sprintf($lang['Click_return_index'], '<a href="' . append_sid("index.$phpEx") . '">', '</a>');
137 message_die(GENERAL_MESSAGE, $message);
142 $redirect = ( !empty($HTTP_POST_VARS['redirect']) ) ? str_replace('&', '&', htmlspecialchars($HTTP_POST_VARS['redirect'])) : "";
143 $redirect = str_replace("?", "&", $redirect);
145 if (strstr(urldecode($redirect), "\n") || strstr(urldecode($redirect), "\r") || strstr(urldecode($redirect), ';url'))
147 message_die(GENERAL_ERROR, 'Tried to redirect to potentially insecure url.');
150 $template->assign_vars(array(
151 'META' => "<meta http-equiv=\"refresh\" content=\"3;url=login.$phpEx?redirect=$redirect\">")
154 $message = $lang['Error_login'] . '<br /><br />' . sprintf($lang['Click_return_login'], "<a href=\"login.$phpEx?redirect=$redirect\">", '</a>') . '<br /><br />' . sprintf($lang['Click_return_index'], '<a href="' . append_sid("index.$phpEx") . '">', '</a>');
156 message_die(GENERAL_MESSAGE, $message);
159 else if( ( isset($HTTP_GET_VARS['logout']) || isset($HTTP_POST_VARS['logout']) ) && $userdata['session_logged_in'] )
162 if ($sid == '' || $sid != $userdata['session_id'])
164 message_die(GENERAL_ERROR, 'Invalid_session');
167 if( $userdata['session_logged_in'] )
169 session_end($userdata['session_id'], $userdata['user_id']);
172 if (!empty($HTTP_POST_VARS['redirect']) || !empty($HTTP_GET_VARS['redirect']))
174 $url = (!empty($HTTP_POST_VARS['redirect'])) ? htmlspecialchars($HTTP_POST_VARS['redirect']) : htmlspecialchars($HTTP_GET_VARS['redirect']);
175 $url = str_replace('&', '&', $url);
176 redirect(append_sid($url, true));
180 redirect(append_sid("index.$phpEx", true));
185 $url = ( !empty($HTTP_POST_VARS['redirect']) ) ? str_replace('&', '&', htmlspecialchars($HTTP_POST_VARS['redirect'])) : "index.$phpEx";
186 redirect(append_sid($url, true));
192 // Do a full login page dohickey if
193 // user not already logged in
195 if( !$userdata['session_logged_in'] || (isset($HTTP_GET_VARS['admin']) && $userdata['session_logged_in'] && $userdata['user_level'] == ADMIN))
197 $page_title = $lang['Login'];
198 include($phpbb_root_path . 'includes/page_header.'.$phpEx);
200 $template->set_filenames(array(
201 'body' => 'login_body.tpl')
206 if( isset($HTTP_POST_VARS['redirect']) || isset($HTTP_GET_VARS['redirect']) )
208 $forward_to = $HTTP_SERVER_VARS['QUERY_STRING'];
210 if( preg_match("/^redirect=([a-z0-9\.#\/\?&=\+\-_]+)/si", $forward_to, $forward_matches) )
212 $forward_to = ( !empty($forward_matches[3]) ) ? $forward_matches[3] : $forward_matches[1];
213 $forward_match = explode('&', $forward_to);
215 if(count($forward_match) > 1)
217 for($i = 1; $i < count($forward_match); $i++)
219 if( !ereg("sid=", $forward_match[$i]) )
221 if( $forward_page != '' )
223 $forward_page .= '&';
225 $forward_page .= $forward_match[$i];
228 $forward_page = $forward_match[0] . '?' . $forward_page;
232 $forward_page = $forward_match[0];
237 $username = ( $userdata['user_id'] != ANONYMOUS ) ? $userdata['username'] : '';
239 $s_hidden_fields = '<input type="hidden" name="redirect" value="' . $forward_page . '" />';
240 $s_hidden_fields .= (isset($HTTP_GET_VARS['admin'])) ? '<input type="hidden" name="admin" value="1" />' : '';
242 make_jumpbox('viewforum.'.$phpEx);
243 $template->assign_vars(array(
244 'USERNAME' => $username,
246 'L_ENTER_PASSWORD' => (isset($HTTP_GET_VARS['admin'])) ? $lang['Admin_reauthenticate'] : $lang['Enter_password'],
247 'L_SEND_PASSWORD' => $lang['Forgotten_password'],
249 'U_SEND_PASSWORD' => append_sid("profile.$phpEx?mode=sendpassword"),
251 'S_HIDDEN_FIELDS' => $s_hidden_fields)
254 $template->pparse('body');
256 include($phpbb_root_path . 'includes/page_tail.'.$phpEx);
260 redirect(append_sid("index.$phpEx", true));