| Conditions | 88 |
| Paths | > 20000 |
| Total Lines | 329 |
| Code Lines | 150 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 81 | function Login2() |
||
| 82 | { |
||
| 83 | global $txt, $scripturl, $user_info, $user_settings, $smcFunc; |
||
| 84 | global $cookiename, $modSettings, $context, $sourcedir, $maintenance; |
||
| 85 | |||
| 86 | // Check to ensure we're forcing SSL for authentication |
||
| 87 | if (!empty($modSettings['force_ssl']) && empty($maintenance) && !httpsOn()) |
||
| 88 | fatal_lang_error('login_ssl_required', false); |
||
| 89 | |||
| 90 | // Load cookie authentication stuff. |
||
| 91 | require_once($sourcedir . '/Subs-Auth.php'); |
||
| 92 | |||
| 93 | if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') |
||
| 94 | { |
||
| 95 | $context['from_ajax'] = true; |
||
| 96 | $context['template_layers'] = array(); |
||
| 97 | } |
||
| 98 | |||
| 99 | if (isset($_GET['sa']) && $_GET['sa'] == 'salt' && !$user_info['is_guest']) |
||
| 100 | { |
||
| 101 | // First check for 2.1 json-format cookie in $_COOKIE |
||
| 102 | if (isset($_COOKIE[$cookiename]) && preg_match('~^{"0":\d+,"1":"[0-9a-f]*","2":\d+~', $_COOKIE[$cookiename]) === 1) |
||
| 103 | list (,, $timeout) = $smcFunc['json_decode']($_COOKIE[$cookiename], true); |
||
| 104 | |||
| 105 | // Try checking for 2.1 json-format cookie in $_SESSION |
||
| 106 | elseif (isset($_SESSION['login_' . $cookiename]) && preg_match('~^{"0":\d+,"1":"[0-9a-f]*","2":\d+~', $_SESSION['login_' . $cookiename]) === 1) |
||
| 107 | list (,, $timeout) = $smcFunc['json_decode']($_SESSION['login_' . $cookiename]); |
||
| 108 | |||
| 109 | // Next, try checking for 2.0 serialized string cookie in $_COOKIE |
||
| 110 | elseif (isset($_COOKIE[$cookiename]) && preg_match('~^a:[34]:\{i:0;i:\d+;i:1;s:(0|128):"([a-fA-F0-9]{128})?";i:2;[id]:\d+;~', $_COOKIE[$cookiename]) === 1) |
||
| 111 | list (,, $timeout) = safe_unserialize($_COOKIE[$cookiename]); |
||
| 112 | |||
| 113 | // Last, see if you need to fall back on checking for 2.0 serialized string cookie in $_SESSION |
||
| 114 | elseif (isset($_SESSION['login_' . $cookiename]) && preg_match('~^a:[34]:\{i:0;i:\d+;i:1;s:(0|128):"([a-fA-F0-9]{128})?";i:2;[id]:\d+;~', $_SESSION['login_' . $cookiename]) === 1) |
||
| 115 | list (,, $timeout) = safe_unserialize($_SESSION['login_' . $cookiename]); |
||
| 116 | |||
| 117 | else |
||
| 118 | trigger_error('Login2(): Cannot be logged in without a session or cookie', E_USER_ERROR); |
||
| 119 | |||
| 120 | $user_settings['password_salt'] = substr(md5($smcFunc['random_int']()), 0, 4); |
||
| 121 | updateMemberData($user_info['id'], array('password_salt' => $user_settings['password_salt'])); |
||
| 122 | |||
| 123 | // Preserve the 2FA cookie? |
||
| 124 | if (!empty($modSettings['tfa_mode']) && !empty($_COOKIE[$cookiename . '_tfa'])) |
||
| 125 | { |
||
| 126 | list (,, $exp) = $smcFunc['json_decode']($_COOKIE[$cookiename . '_tfa'], true); |
||
| 127 | setTFACookie((int) $exp - time(), $user_info['password_salt'], hash_salt($user_settings['tfa_backup'], $user_settings['password_salt'])); |
||
| 128 | } |
||
| 129 | |||
| 130 | setLoginCookie((int) $timeout - time(), $user_info['id'], hash_salt($user_settings['passwd'], $user_settings['password_salt'])); |
||
|
|
|||
| 131 | |||
| 132 | redirectexit('action=login2;sa=check;member=' . $user_info['id'], $context['server']['needs_login_fix']); |
||
| 133 | } |
||
| 134 | // Double check the cookie... |
||
| 135 | elseif (isset($_GET['sa']) && $_GET['sa'] == 'check') |
||
| 136 | { |
||
| 137 | // Strike! You're outta there! |
||
| 138 | if ($_GET['member'] != $user_info['id']) |
||
| 139 | fatal_lang_error('login_cookie_error', false); |
||
| 140 | |||
| 141 | $user_info['can_mod'] = allowedTo('access_mod_center') || (!$user_info['is_guest'] && ($user_info['mod_cache']['gq'] != '0=1' || $user_info['mod_cache']['bq'] != '0=1' || ($modSettings['postmod_active'] && !empty($user_info['mod_cache']['ap'])))); |
||
| 142 | |||
| 143 | // Some whitelisting for login_url... |
||
| 144 | if (empty($_SESSION['login_url'])) |
||
| 145 | redirectexit(empty($user_settings['tfa_secret']) ? '' : 'action=logintfa'); |
||
| 146 | elseif (!empty($_SESSION['login_url']) && (strpos($_SESSION['login_url'], 'http://') === false && strpos($_SESSION['login_url'], 'https://') === false)) |
||
| 147 | { |
||
| 148 | unset ($_SESSION['login_url']); |
||
| 149 | redirectexit(empty($user_settings['tfa_secret']) ? '' : 'action=logintfa'); |
||
| 150 | } |
||
| 151 | elseif (!empty($user_settings['tfa_secret'])) |
||
| 152 | { |
||
| 153 | redirectexit('action=logintfa'); |
||
| 154 | } |
||
| 155 | else |
||
| 156 | { |
||
| 157 | // Best not to clutter the session data too much... |
||
| 158 | $temp = $_SESSION['login_url']; |
||
| 159 | unset($_SESSION['login_url']); |
||
| 160 | |||
| 161 | redirectexit($temp); |
||
| 162 | } |
||
| 163 | } |
||
| 164 | |||
| 165 | // Beyond this point you are assumed to be a guest trying to login. |
||
| 166 | if (!$user_info['is_guest']) |
||
| 167 | redirectexit(); |
||
| 168 | |||
| 169 | // Are you guessing with a script? |
||
| 170 | checkSession(); |
||
| 171 | validateToken('login'); |
||
| 172 | spamProtection('login'); |
||
| 173 | |||
| 174 | // Set the login_url if it's not already set (but careful not to send us to an attachment). |
||
| 175 | if ((empty($_SESSION['login_url']) && isset($_SESSION['old_url']) && strpos($_SESSION['old_url'], 'dlattach') === false && preg_match('~(board|topic)[=,]~', $_SESSION['old_url']) != 0) || (isset($_GET['quicklogin']) && isset($_SESSION['old_url']) && strpos($_SESSION['old_url'], 'login') === false)) |
||
| 176 | $_SESSION['login_url'] = $_SESSION['old_url']; |
||
| 177 | |||
| 178 | // Been guessing a lot, haven't we? |
||
| 179 | if (isset($_SESSION['failed_login']) && $_SESSION['failed_login'] >= $modSettings['failed_login_threshold'] * 3) |
||
| 180 | fatal_lang_error('login_threshold_fail', 'login'); |
||
| 181 | |||
| 182 | // Set up the cookie length. (if it's invalid, just fall through and use the default.) |
||
| 183 | if (isset($_POST['cookieneverexp']) || (!empty($_POST['cookielength']) && $_POST['cookielength'] == -1)) |
||
| 184 | $modSettings['cookieTime'] = 3153600; |
||
| 185 | elseif (!empty($_POST['cookielength']) && ($_POST['cookielength'] >= 1 && $_POST['cookielength'] <= 3153600)) |
||
| 186 | $modSettings['cookieTime'] = (int) $_POST['cookielength']; |
||
| 187 | |||
| 188 | loadLanguage('Login'); |
||
| 189 | // Load the template stuff. |
||
| 190 | loadTemplate('Login'); |
||
| 191 | $context['sub_template'] = 'login'; |
||
| 192 | |||
| 193 | // Set up the default/fallback stuff. |
||
| 194 | $context['default_username'] = isset($_POST['user']) ? preg_replace('~&#(\\d{1,7}|x[0-9a-fA-F]{1,6});~', '&#\\1;', $smcFunc['htmlspecialchars']($_POST['user'])) : ''; |
||
| 195 | $context['default_password'] = ''; |
||
| 196 | $context['never_expire'] = $modSettings['cookieTime'] <= 525600; |
||
| 197 | $context['login_errors'] = array($txt['error_occured']); |
||
| 198 | $context['page_title'] = $txt['login']; |
||
| 199 | |||
| 200 | // Add the login chain to the link tree. |
||
| 201 | $context['linktree'][] = array( |
||
| 202 | 'url' => $scripturl . '?action=login', |
||
| 203 | 'name' => $txt['login'], |
||
| 204 | ); |
||
| 205 | |||
| 206 | // You forgot to type your username, dummy! |
||
| 207 | if (!isset($_POST['user']) || $_POST['user'] == '') |
||
| 208 | { |
||
| 209 | $context['login_errors'] = array($txt['need_username']); |
||
| 210 | return; |
||
| 211 | } |
||
| 212 | |||
| 213 | // Hmm... maybe 'admin' will login with no password. Uhh... NO! |
||
| 214 | if (!isset($_POST['passwrd']) || $_POST['passwrd'] == '') |
||
| 215 | { |
||
| 216 | $context['login_errors'] = array($txt['no_password']); |
||
| 217 | return; |
||
| 218 | } |
||
| 219 | |||
| 220 | // No funky symbols either. |
||
| 221 | if (preg_match('~[<>&"\'=\\\]~', preg_replace('~(&#(\\d{1,7}|x[0-9a-fA-F]{1,6});)~', '', $_POST['user'])) != 0) |
||
| 222 | { |
||
| 223 | $context['login_errors'] = array($txt['error_invalid_characters_username']); |
||
| 224 | return; |
||
| 225 | } |
||
| 226 | |||
| 227 | // And if it's too long, trim it back. |
||
| 228 | if ($smcFunc['strlen']($_POST['user']) > 80) |
||
| 229 | { |
||
| 230 | $_POST['user'] = $smcFunc['substr']($_POST['user'], 0, 79); |
||
| 231 | $context['default_username'] = preg_replace('~&#(\\d{1,7}|x[0-9a-fA-F]{1,6});~', '&#\\1;', $smcFunc['htmlspecialchars']($_POST['user'])); |
||
| 232 | } |
||
| 233 | |||
| 234 | // Are we using any sort of integration to validate the login? |
||
| 235 | if (in_array('retry', call_integration_hook('integrate_validate_login', array($_POST['user'], isset($_POST['passwrd']) ? $_POST['passwrd'] : null, $modSettings['cookieTime'])), true)) |
||
| 236 | { |
||
| 237 | $context['login_errors'] = array($txt['incorrect_password']); |
||
| 238 | return; |
||
| 239 | } |
||
| 240 | |||
| 241 | // Load the data up! |
||
| 242 | $request = $smcFunc['db_query']('', ' |
||
| 243 | SELECT passwd, id_member, id_group, lngfile, is_activated, email_address, additional_groups, member_name, password_salt, |
||
| 244 | passwd_flood, tfa_secret |
||
| 245 | FROM {db_prefix}members |
||
| 246 | WHERE ' . ($smcFunc['db_case_sensitive'] ? 'LOWER(member_name) = LOWER({string:user_name})' : 'member_name = {string:user_name}') . ' |
||
| 247 | LIMIT 1', |
||
| 248 | array( |
||
| 249 | 'user_name' => $smcFunc['db_case_sensitive'] ? strtolower($_POST['user']) : $_POST['user'], |
||
| 250 | ) |
||
| 251 | ); |
||
| 252 | // Probably mistyped or their email, try it as an email address. (member_name first, though!) |
||
| 253 | if ($smcFunc['db_num_rows']($request) == 0 && strpos($_POST['user'], '@') !== false) |
||
| 254 | { |
||
| 255 | $smcFunc['db_free_result']($request); |
||
| 256 | |||
| 257 | $request = $smcFunc['db_query']('', ' |
||
| 258 | SELECT passwd, id_member, id_group, lngfile, is_activated, email_address, additional_groups, member_name, password_salt, |
||
| 259 | passwd_flood, tfa_secret |
||
| 260 | FROM {db_prefix}members |
||
| 261 | WHERE email_address = {string:user_name} |
||
| 262 | LIMIT 1', |
||
| 263 | array( |
||
| 264 | 'user_name' => $_POST['user'], |
||
| 265 | ) |
||
| 266 | ); |
||
| 267 | } |
||
| 268 | |||
| 269 | // Let them try again, it didn't match anything... |
||
| 270 | if ($smcFunc['db_num_rows']($request) == 0) |
||
| 271 | { |
||
| 272 | $context['login_errors'] = array($txt['username_no_exist']); |
||
| 273 | return; |
||
| 274 | } |
||
| 275 | |||
| 276 | $user_settings = $smcFunc['db_fetch_assoc']($request); |
||
| 277 | $smcFunc['db_free_result']($request); |
||
| 278 | |||
| 279 | // Bad password! Thought you could fool the database?! |
||
| 280 | if (!hash_verify_password($user_settings['member_name'], un_htmlspecialchars($_POST['passwrd']), $user_settings['passwd'])) |
||
| 281 | { |
||
| 282 | // Let's be cautious, no hacking please. thanx. |
||
| 283 | validatePasswordFlood($user_settings['id_member'], $user_settings['member_name'], $user_settings['passwd_flood']); |
||
| 284 | |||
| 285 | // Maybe we were too hasty... let's try some other authentication methods. |
||
| 286 | $other_passwords = array(); |
||
| 287 | |||
| 288 | // None of the below cases will be used most of the time (because the salt is normally set.) |
||
| 289 | if (!empty($modSettings['enable_password_conversion']) && $user_settings['password_salt'] == '') |
||
| 290 | { |
||
| 291 | // YaBB SE, Discus, MD5 (used a lot), SHA-1 (used some), SMF 1.0.x, IkonBoard, and none at all. |
||
| 292 | $other_passwords[] = crypt($_POST['passwrd'], substr($_POST['passwrd'], 0, 2)); |
||
| 293 | $other_passwords[] = crypt($_POST['passwrd'], substr($user_settings['passwd'], 0, 2)); |
||
| 294 | $other_passwords[] = md5($_POST['passwrd']); |
||
| 295 | $other_passwords[] = sha1($_POST['passwrd']); |
||
| 296 | $other_passwords[] = md5_hmac($_POST['passwrd'], strtolower($user_settings['member_name'])); |
||
| 297 | $other_passwords[] = md5($_POST['passwrd'] . strtolower($user_settings['member_name'])); |
||
| 298 | $other_passwords[] = md5(md5($_POST['passwrd'])); |
||
| 299 | $other_passwords[] = $_POST['passwrd']; |
||
| 300 | |||
| 301 | // This one is a strange one... MyPHP, crypt() on the MD5 hash. |
||
| 302 | $other_passwords[] = crypt(md5($_POST['passwrd']), md5($_POST['passwrd'])); |
||
| 303 | |||
| 304 | // Snitz style - SHA-256. Technically, this is a downgrade, but most PHP configurations don't support sha256 anyway. |
||
| 305 | if (strlen($user_settings['passwd']) == 64 && function_exists('mhash') && defined('MHASH_SHA256')) |
||
| 306 | $other_passwords[] = bin2hex(mhash(MHASH_SHA256, $_POST['passwrd'])); |
||
| 307 | |||
| 308 | // phpBB3 users new hashing. We now support it as well ;). |
||
| 309 | $other_passwords[] = phpBB3_password_check($_POST['passwrd'], $user_settings['passwd']); |
||
| 310 | |||
| 311 | // APBoard 2 Login Method. |
||
| 312 | $other_passwords[] = md5(crypt($_POST['passwrd'], 'CRYPT_MD5')); |
||
| 313 | } |
||
| 314 | // The hash should be 40 if it's SHA-1, so we're safe with more here too. |
||
| 315 | elseif (!empty($modSettings['enable_password_conversion']) && strlen($user_settings['passwd']) == 32) |
||
| 316 | { |
||
| 317 | // vBulletin 3 style hashing? Let's welcome them with open arms \o/. |
||
| 318 | $other_passwords[] = md5(md5($_POST['passwrd']) . stripslashes($user_settings['password_salt'])); |
||
| 319 | |||
| 320 | // Hmm.. p'raps it's Invision 2 style? |
||
| 321 | $other_passwords[] = md5(md5($user_settings['password_salt']) . md5($_POST['passwrd'])); |
||
| 322 | |||
| 323 | // Some common md5 ones. |
||
| 324 | $other_passwords[] = md5($user_settings['password_salt'] . $_POST['passwrd']); |
||
| 325 | $other_passwords[] = md5($_POST['passwrd'] . $user_settings['password_salt']); |
||
| 326 | } |
||
| 327 | elseif (strlen($user_settings['passwd']) == 40) |
||
| 328 | { |
||
| 329 | // Maybe they are using a hash from before the password fix. |
||
| 330 | // This is also valid for SMF 1.1 to 2.0 style of hashing, changed to bcrypt in SMF 2.1 |
||
| 331 | $other_passwords[] = sha1(strtolower($user_settings['member_name']) . un_htmlspecialchars($_POST['passwrd'])); |
||
| 332 | |||
| 333 | // BurningBoard3 style of hashing. |
||
| 334 | if (!empty($modSettings['enable_password_conversion'])) |
||
| 335 | $other_passwords[] = sha1($user_settings['password_salt'] . sha1($user_settings['password_salt'] . sha1($_POST['passwrd']))); |
||
| 336 | |||
| 337 | // Perhaps we converted to UTF-8 and have a valid password being hashed differently. |
||
| 338 | if ($context['character_set'] == 'UTF-8' && !empty($modSettings['previousCharacterSet']) && $modSettings['previousCharacterSet'] != 'utf8') |
||
| 339 | { |
||
| 340 | // Try iconv first, for no particular reason. |
||
| 341 | if (function_exists('iconv')) |
||
| 342 | $other_passwords['iconv'] = sha1(strtolower(iconv('UTF-8', $modSettings['previousCharacterSet'], $user_settings['member_name'])) . un_htmlspecialchars(iconv('UTF-8', $modSettings['previousCharacterSet'], $_POST['passwrd']))); |
||
| 343 | |||
| 344 | // Say it aint so, iconv failed! |
||
| 345 | if (empty($other_passwords['iconv']) && function_exists('mb_convert_encoding')) |
||
| 346 | $other_passwords[] = sha1(strtolower(mb_convert_encoding($user_settings['member_name'], 'UTF-8', $modSettings['previousCharacterSet'])) . un_htmlspecialchars(mb_convert_encoding($_POST['passwrd'], 'UTF-8', $modSettings['previousCharacterSet']))); |
||
| 347 | } |
||
| 348 | } |
||
| 349 | |||
| 350 | // SMF's sha1 function can give a funny result on Linux (Not our fault!). If we've now got the real one let the old one be valid! |
||
| 351 | if (stripos(PHP_OS, 'win') !== 0 && strlen($user_settings['passwd']) < hash_length()) |
||
| 352 | { |
||
| 353 | require_once($sourcedir . '/Subs-Compat.php'); |
||
| 354 | $other_passwords[] = sha1_smf(strtolower($user_settings['member_name']) . un_htmlspecialchars($_POST['passwrd'])); |
||
| 355 | } |
||
| 356 | |||
| 357 | // Allows mods to easily extend the $other_passwords array |
||
| 358 | call_integration_hook('integrate_other_passwords', array(&$other_passwords)); |
||
| 359 | |||
| 360 | // Whichever encryption it was using, let's make it use SMF's now ;). |
||
| 361 | if (in_array($user_settings['passwd'], $other_passwords)) |
||
| 362 | { |
||
| 363 | $user_settings['passwd'] = hash_password($user_settings['member_name'], un_htmlspecialchars($_POST['passwrd'])); |
||
| 364 | $user_settings['password_salt'] = substr(md5($smcFunc['random_int']()), 0, 4); |
||
| 365 | |||
| 366 | // Update the password and set up the hash. |
||
| 367 | updateMemberData($user_settings['id_member'], array('passwd' => $user_settings['passwd'], 'password_salt' => $user_settings['password_salt'], 'passwd_flood' => '')); |
||
| 368 | } |
||
| 369 | // Okay, they for sure didn't enter the password! |
||
| 370 | else |
||
| 371 | { |
||
| 372 | // They've messed up again - keep a count to see if they need a hand. |
||
| 373 | $_SESSION['failed_login'] = isset($_SESSION['failed_login']) ? ($_SESSION['failed_login'] + 1) : 1; |
||
| 374 | |||
| 375 | // Hmm... don't remember it, do you? Here, try the password reminder ;). |
||
| 376 | if ($_SESSION['failed_login'] >= $modSettings['failed_login_threshold']) |
||
| 377 | redirectexit('action=reminder'); |
||
| 378 | // We'll give you another chance... |
||
| 379 | else |
||
| 380 | { |
||
| 381 | // Log an error so we know that it didn't go well in the error log. |
||
| 382 | log_error($txt['incorrect_password'] . ' - <span class="remove">' . $user_settings['member_name'] . '</span>', 'user'); |
||
| 383 | |||
| 384 | $context['login_errors'] = array($txt['incorrect_password']); |
||
| 385 | return; |
||
| 386 | } |
||
| 387 | } |
||
| 388 | } |
||
| 389 | elseif (!empty($user_settings['passwd_flood'])) |
||
| 390 | { |
||
| 391 | // Let's be sure they weren't a little hacker. |
||
| 392 | validatePasswordFlood($user_settings['id_member'], $user_settings['member_name'], $user_settings['passwd_flood'], true); |
||
| 393 | |||
| 394 | // If we got here then we can reset the flood counter. |
||
| 395 | updateMemberData($user_settings['id_member'], array('passwd_flood' => '')); |
||
| 396 | } |
||
| 397 | |||
| 398 | // Correct password, but they've got no salt; fix it! |
||
| 399 | if ($user_settings['password_salt'] == '') |
||
| 400 | { |
||
| 401 | $user_settings['password_salt'] = substr(md5($smcFunc['random_int']()), 0, 4); |
||
| 402 | updateMemberData($user_settings['id_member'], array('password_salt' => $user_settings['password_salt'])); |
||
| 403 | } |
||
| 404 | |||
| 405 | // Check their activation status. |
||
| 406 | if (!checkActivation()) |
||
| 407 | return; |
||
| 408 | |||
| 409 | DoLogin(); |
||
| 410 | } |
||
| 852 | ?> |