replace constants with class constants.
[phpbb.git] / phpBB / includes / auth / auth_db.php
blob670b16c4e36daea98fd23eaf2c512ad2e2b89431
1 <?php
2 /**
3 * Database auth plug-in for phpBB3
5 * Authentication plug-ins is largely down to Sergey Kanareykin, our thanks to him.
7 * This is for authentication via the integrated user table
9 * @package login
10 * @version $Id$
11 * @copyright (c) 2005 phpBB Group
12 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
16 /**
17 * @ignore
19 if (!defined('IN_PHPBB'))
21 exit;
24 /**
25 * Login function
27 function login_db(&$username, &$password)
29 global $db, $config;
31 // do not allow empty password
32 if (!$password)
34 return array(
35 'status' => LOGIN_ERROR_PASSWORD,
36 'error_msg' => 'NO_PASSWORD_SUPPLIED',
37 'user_row' => array('user_id' => ANONYMOUS),
41 if (!$username)
43 return array(
44 'status' => LOGIN_ERROR_USERNAME,
45 'error_msg' => 'LOGIN_ERROR_USERNAME',
46 'user_row' => array('user_id' => ANONYMOUS),
50 $sql = 'SELECT user_id, username, user_password, user_passchg, user_pass_convert, user_email, user_type, user_login_attempts
51 FROM ' . USERS_TABLE . "
52 WHERE username_clean = '" . $db->sql_escape(utf8_clean_string($username)) . "'";
53 $result = $db->sql_query($sql);
54 $row = $db->sql_fetchrow($result);
55 $db->sql_freeresult($result);
57 if (!$row)
59 return array(
60 'status' => LOGIN_ERROR_USERNAME,
61 'error_msg' => 'LOGIN_ERROR_USERNAME',
62 'user_row' => array('user_id' => ANONYMOUS),
66 // If there are too much login attempts, we need to check for an confirm image
67 // Every auth module is able to define what to do by itself...
68 if ($config['max_login_attempts'] && $row['user_login_attempts'] >= $config['max_login_attempts'])
70 $confirm_id = request_var('confirm_id', '');
71 $confirm_code = request_var('confirm_code', '');
73 // Visual Confirmation handling
74 if (!$confirm_id)
76 return array(
77 'status' => LOGIN_ERROR_ATTEMPTS,
78 'error_msg' => 'LOGIN_ERROR_ATTEMPTS',
79 'user_row' => $row,
82 else
84 $captcha = phpbb_captcha_factory::get_instance($config['captcha_plugin']);
85 $captcha->init(CONFIRM_LOGIN);
86 $vc_response = $captcha->validate();
87 if ($vc_response)
89 return array(
90 'status' => LOGIN_ERROR_ATTEMPTS,
91 'error_msg' => 'LOGIN_ERROR_ATTEMPTS',
92 'user_row' => $row,
98 // @todo: safe to remove?
99 // If the password convert flag is set we need to convert it
100 /*if ($row['user_pass_convert'])
102 // in phpBB2 passwords were used exactly as they were sent, with addslashes applied
103 $disabled = phpbb_request::super_globals_disabled();
104 phpbb_request::enable_super_globals();
105 $password_old_format = isset($_REQUEST['password']) ? (string) $_REQUEST['password'] : '';
106 $password_old_format = (!STRIP) ? addslashes($password_old_format) : $password_old_format;
107 $password_new_format = '';
108 if ($disabled)
110 phpbb_request::disable_super_globals();
113 set_var($password_new_format, stripslashes($password_old_format), 'string');
115 if ($password == $password_new_format)
117 if (!function_exists('utf8_to_cp1252'))
119 include(PHPBB_ROOT_PATH . 'includes/utf/data/recode_basic.' . PHP_EXT);
122 // cp1252 is phpBB2's default encoding, characters outside ASCII range might work when converted into that encoding
123 if (md5($password_old_format) == $row['user_password'] || md5(utf8_to_cp1252($password_old_format)) == $row['user_password'])
125 $hash = phpbb_hash($password_new_format);
127 // Update the password in the users table to the new format and remove user_pass_convert flag
128 $sql = 'UPDATE ' . USERS_TABLE . '
129 SET user_password = \'' . $db->sql_escape($hash) . '\',
130 user_pass_convert = 0
131 WHERE user_id = ' . $row['user_id'];
132 $db->sql_query($sql);
134 $row['user_pass_convert'] = 0;
135 $row['user_password'] = $hash;
137 else
139 // Although we weren't able to convert this password we have to
140 // increase login attempt count to make sure this cannot be exploited
141 $sql = 'UPDATE ' . USERS_TABLE . '
142 SET user_login_attempts = user_login_attempts + 1
143 WHERE user_id = ' . $row['user_id'];
144 $db->sql_query($sql);
146 return array(
147 'status' => LOGIN_ERROR_PASSWORD_CONVERT,
148 'error_msg' => 'LOGIN_ERROR_PASSWORD_CONVERT',
149 'user_row' => $row,
155 // Check password ...
156 if (!$row['user_pass_convert'] && phpbb_check_hash($password, $row['user_password']))
158 // Check for old password hash...
159 if (strlen($row['user_password']) == 32)
161 $hash = phpbb_hash($password);
163 // Update the password in the users table to the new format
164 $sql = 'UPDATE ' . USERS_TABLE . "
165 SET user_password = '" . $db->sql_escape($hash) . "',
166 user_pass_convert = 0
167 WHERE user_id = {$row['user_id']}";
168 $db->sql_query($sql);
170 $row['user_password'] = $hash;
173 if ($row['user_login_attempts'] != 0)
175 // Successful, reset login attempts (the user passed all stages)
176 $sql = 'UPDATE ' . USERS_TABLE . '
177 SET user_login_attempts = 0
178 WHERE user_id = ' . $row['user_id'];
179 $db->sql_query($sql);
182 // User inactive...
183 if ($row['user_type'] == phpbb::USER_INACTIVE || $row['user_type'] == phpbb::USER_IGNORE)
185 return array(
186 'status' => LOGIN_ERROR_ACTIVE,
187 'error_msg' => 'ACTIVE_ERROR',
188 'user_row' => $row,
192 // Successful login... set user_login_attempts to zero...
193 return array(
194 'status' => LOGIN_SUCCESS,
195 'error_msg' => false,
196 'user_row' => $row,
200 // Password incorrect - increase login attempts
201 $sql = 'UPDATE ' . USERS_TABLE . '
202 SET user_login_attempts = user_login_attempts + 1
203 WHERE user_id = ' . $row['user_id'];
204 $db->sql_query($sql);
206 // Give status about wrong password...
207 return array(
208 'status' => LOGIN_ERROR_PASSWORD,
209 'error_msg' => 'LOGIN_ERROR_PASSWORD',
210 'user_row' => $row,