Blame Extras/phpBB/3.0.4/includes/auth/auth_db.php

4c79b5
4c79b5
/**
4c79b5
* Database auth plug-in for phpBB3
4c79b5
*
4c79b5
* Authentication plug-ins is largely down to Sergey Kanareykin, our thanks to him.
4c79b5
*
4c79b5
* This is for authentication via the integrated user table
4c79b5
*
4c79b5
* @package login
4c79b5
* @version $Id: auth_db.php 8479 2008-03-29 00:22:48Z naderman $
4c79b5
* @copyright (c) 2005 phpBB Group
4c79b5
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
4c79b5
*
4c79b5
*/
4c79b5
4c79b5
/**
4c79b5
* @ignore
4c79b5
*/
4c79b5
if (!defined('IN_PHPBB'))
4c79b5
{
4c79b5
	exit;
4c79b5
}
4c79b5
4c79b5
/**
4c79b5
* Login function
4c79b5
*/
4c79b5
function login_db(&$username, &$password)
4c79b5
{
4c79b5
	global $db, $config;
4c79b5
4c79b5
	// do not allow empty password
4c79b5
	if (!$password)
4c79b5
	{
4c79b5
		return array(
4c79b5
			'status'	=> LOGIN_ERROR_PASSWORD,
4c79b5
			'error_msg'	=> 'NO_PASSWORD_SUPPLIED',
4c79b5
			'user_row'	=> array('user_id' => ANONYMOUS),
4c79b5
		);
4c79b5
	}
4c79b5
4c79b5
	if (!$username)
4c79b5
	{
4c79b5
		return array(
4c79b5
			'status'	=> LOGIN_ERROR_USERNAME,
4c79b5
			'error_msg'	=> 'LOGIN_ERROR_USERNAME',
4c79b5
			'user_row'	=> array('user_id' => ANONYMOUS),
4c79b5
		);
4c79b5
	}
4c79b5
4c79b5
	$sql = 'SELECT user_id, username, user_password, user_passchg, user_pass_convert, user_email, user_type, user_login_attempts
4c79b5
		FROM ' . USERS_TABLE . "
4c79b5
		WHERE username_clean = '" . $db->sql_escape(utf8_clean_string($username)) . "'";
4c79b5
	$result = $db->sql_query($sql);
4c79b5
	$row = $db->sql_fetchrow($result);
4c79b5
	$db->sql_freeresult($result);
4c79b5
4c79b5
	if (!$row)
4c79b5
	{
4c79b5
		return array(
4c79b5
			'status'	=> LOGIN_ERROR_USERNAME,
4c79b5
			'error_msg'	=> 'LOGIN_ERROR_USERNAME',
4c79b5
			'user_row'	=> array('user_id' => ANONYMOUS),
4c79b5
		);
4c79b5
	}
4c79b5
4c79b5
	// If there are too much login attempts, we need to check for an confirm image
4c79b5
	// Every auth module is able to define what to do by itself...
4c79b5
	if ($config['max_login_attempts'] && $row['user_login_attempts'] >= $config['max_login_attempts'])
4c79b5
	{
4c79b5
		$confirm_id = request_var('confirm_id', '');
4c79b5
		$confirm_code = request_var('confirm_code', '');
4c79b5
4c79b5
		// Visual Confirmation handling
4c79b5
		if (!$confirm_id)
4c79b5
		{
4c79b5
			return array(
4c79b5
				'status'		=> LOGIN_ERROR_ATTEMPTS,
4c79b5
				'error_msg'		=> 'LOGIN_ERROR_ATTEMPTS',
4c79b5
				'user_row'		=> $row,
4c79b5
			);
4c79b5
		}
4c79b5
		else
4c79b5
		{
4c79b5
			global $user;
4c79b5
4c79b5
			$sql = 'SELECT code
4c79b5
				FROM ' . CONFIRM_TABLE . "
4c79b5
				WHERE confirm_id = '" . $db->sql_escape($confirm_id) . "'
4c79b5
					AND session_id = '" . $db->sql_escape($user->session_id) . "'
4c79b5
					AND confirm_type = " . CONFIRM_LOGIN;
4c79b5
			$result = $db->sql_query($sql);
4c79b5
			$confirm_row = $db->sql_fetchrow($result);
4c79b5
			$db->sql_freeresult($result);
4c79b5
4c79b5
			if ($confirm_row)
4c79b5
			{
4c79b5
				if (strcasecmp($confirm_row['code'], $confirm_code) === 0)
4c79b5
				{
4c79b5
					$sql = 'DELETE FROM ' . CONFIRM_TABLE . "
4c79b5
						WHERE confirm_id = '" . $db->sql_escape($confirm_id) . "'
4c79b5
							AND session_id = '" . $db->sql_escape($user->session_id) . "'
4c79b5
							AND confirm_type = " . CONFIRM_LOGIN;
4c79b5
					$db->sql_query($sql);
4c79b5
				}
4c79b5
				else
4c79b5
				{
4c79b5
					return array(
4c79b5
						'status'		=> LOGIN_ERROR_ATTEMPTS,
4c79b5
						'error_msg'		=> 'CONFIRM_CODE_WRONG',
4c79b5
						'user_row'		=> $row,
4c79b5
					);
4c79b5
				}
4c79b5
			}
4c79b5
			else
4c79b5
			{
4c79b5
				return array(
4c79b5
					'status'		=> LOGIN_ERROR_ATTEMPTS,
4c79b5
					'error_msg'		=> 'CONFIRM_CODE_WRONG',
4c79b5
					'user_row'		=> $row,
4c79b5
				);
4c79b5
			}
4c79b5
		}
4c79b5
	}
4c79b5
4c79b5
	// If the password convert flag is set we need to convert it
4c79b5
	if ($row['user_pass_convert'])
4c79b5
	{
4c79b5
		// in phpBB2 passwords were used exactly as they were sent, with addslashes applied
4c79b5
		$password_old_format = isset($_REQUEST['password']) ? (string) $_REQUEST['password'] : '';
4c79b5
		$password_old_format = (!STRIP) ? addslashes($password_old_format) : $password_old_format;
4c79b5
		$password_new_format = '';
4c79b5
4c79b5
		set_var($password_new_format, stripslashes($password_old_format), 'string');
4c79b5
4c79b5
		if ($password == $password_new_format)
4c79b5
		{
4c79b5
			if (!function_exists('utf8_to_cp1252'))
4c79b5
			{
4c79b5
				global $phpbb_root_path, $phpEx;
4c79b5
				include($phpbb_root_path . 'includes/utf/data/recode_basic.' . $phpEx);
4c79b5
			}
4c79b5
4c79b5
			// cp1252 is phpBB2's default encoding, characters outside ASCII range might work when converted into that encoding
4c79b5
			if (md5($password_old_format) == $row['user_password'] || md5(utf8_to_cp1252($password_old_format)) == $row['user_password'])
4c79b5
			{
4c79b5
				$hash = phpbb_hash($password_new_format);
4c79b5
4c79b5
				// Update the password in the users table to the new format and remove user_pass_convert flag
4c79b5
				$sql = 'UPDATE ' . USERS_TABLE . '
4c79b5
					SET user_password = \'' . $db->sql_escape($hash) . '\',
4c79b5
						user_pass_convert = 0
4c79b5
					WHERE user_id = ' . $row['user_id'];
4c79b5
				$db->sql_query($sql);
4c79b5
4c79b5
				$row['user_pass_convert'] = 0;
4c79b5
				$row['user_password'] = $hash;
4c79b5
			}
4c79b5
			else
4c79b5
			{
4c79b5
				// Although we weren't able to convert this password we have to
4c79b5
				// increase login attempt count to make sure this cannot be exploited
4c79b5
				$sql = 'UPDATE ' . USERS_TABLE . '
4c79b5
					SET user_login_attempts = user_login_attempts + 1
4c79b5
					WHERE user_id = ' . $row['user_id'];
4c79b5
				$db->sql_query($sql);
4c79b5
4c79b5
				return array(
4c79b5
					'status'		=> LOGIN_ERROR_PASSWORD_CONVERT,
4c79b5
					'error_msg'		=> 'LOGIN_ERROR_PASSWORD_CONVERT',
4c79b5
					'user_row'		=> $row,
4c79b5
				);
4c79b5
			}
4c79b5
		}
4c79b5
	}
4c79b5
4c79b5
	// Check password ...
4c79b5
	if (!$row['user_pass_convert'] && phpbb_check_hash($password, $row['user_password']))
4c79b5
	{
4c79b5
		// Check for old password hash...
4c79b5
		if (strlen($row['user_password']) == 32)
4c79b5
		{
4c79b5
			$hash = phpbb_hash($password);
4c79b5
4c79b5
			// Update the password in the users table to the new format
4c79b5
			$sql = 'UPDATE ' . USERS_TABLE . "
4c79b5
				SET user_password = '" . $db->sql_escape($hash) . "',
4c79b5
					user_pass_convert = 0
4c79b5
				WHERE user_id = {$row['user_id']}";
4c79b5
			$db->sql_query($sql);
4c79b5
4c79b5
			$row['user_password'] = $hash;
4c79b5
		}
4c79b5
4c79b5
		if ($row['user_login_attempts'] != 0)
4c79b5
		{
4c79b5
			// Successful, reset login attempts (the user passed all stages)
4c79b5
			$sql = 'UPDATE ' . USERS_TABLE . '
4c79b5
				SET user_login_attempts = 0
4c79b5
				WHERE user_id = ' . $row['user_id'];
4c79b5
			$db->sql_query($sql);
4c79b5
		}
4c79b5
4c79b5
		// User inactive...
4c79b5
		if ($row['user_type'] == USER_INACTIVE || $row['user_type'] == USER_IGNORE)
4c79b5
		{
4c79b5
			return array(
4c79b5
				'status'		=> LOGIN_ERROR_ACTIVE,
4c79b5
				'error_msg'		=> 'ACTIVE_ERROR',
4c79b5
				'user_row'		=> $row,
4c79b5
			);
4c79b5
		}
4c79b5
4c79b5
		// Successful login... set user_login_attempts to zero...
4c79b5
		return array(
4c79b5
			'status'		=> LOGIN_SUCCESS,
4c79b5
			'error_msg'		=> false,
4c79b5
			'user_row'		=> $row,
4c79b5
		);
4c79b5
	}
4c79b5
4c79b5
	// Password incorrect - increase login attempts
4c79b5
	$sql = 'UPDATE ' . USERS_TABLE . '
4c79b5
		SET user_login_attempts = user_login_attempts + 1
4c79b5
		WHERE user_id = ' . $row['user_id'];
4c79b5
	$db->sql_query($sql);
4c79b5
4c79b5
	// Give status about wrong password...
4c79b5
	return array(
4c79b5
		'status'		=> LOGIN_ERROR_PASSWORD,
4c79b5
		'error_msg'		=> 'LOGIN_ERROR_PASSWORD',
4c79b5
		'user_row'		=> $row,
4c79b5
	);
4c79b5
}
4c79b5
4c79b5
?>