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

4c79b5
4c79b5
/**
4c79b5
* Apache auth plug-in for phpBB3
4c79b5
*
4c79b5
* Authentication plug-ins is largely down to Sergey Kanareykin, our thanks to him.
4c79b5
*
4c79b5
* @package login
4c79b5
* @version $Id: auth_apache.php 8602 2008-06-04 16:05:27Z 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
* Checks whether the user is identified to apache
4c79b5
* Only allow changing authentication to apache if the user is identified
4c79b5
* Called in acp_board while setting authentication plugins
4c79b5
*
4c79b5
* @return boolean|string false if the user is identified and else an error message
4c79b5
*/
4c79b5
function init_apache()
4c79b5
{
4c79b5
	global $user;
4c79b5
4c79b5
	if (!isset($_SERVER['PHP_AUTH_USER']) || $user->data['username'] !== $_SERVER['PHP_AUTH_USER'])
4c79b5
	{
4c79b5
		return $user->lang['APACHE_SETUP_BEFORE_USE'];
4c79b5
	}
4c79b5
	return false;
4c79b5
}
4c79b5
4c79b5
/**
4c79b5
* Login function
4c79b5
*/
4c79b5
function login_apache(&$username, &$password)
4c79b5
{
4c79b5
	global $db;
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
	if (!isset($_SERVER['PHP_AUTH_USER']))
4c79b5
	{
4c79b5
		return array(
4c79b5
			'status'		=> LOGIN_ERROR_EXTERNAL_AUTH,
4c79b5
			'error_msg'		=> 'LOGIN_ERROR_EXTERNAL_AUTH_APACHE',
4c79b5
			'user_row'		=> array('user_id' => ANONYMOUS),
4c79b5
		);
4c79b5
	}
4c79b5
4c79b5
	$php_auth_user = $_SERVER['PHP_AUTH_USER'];
4c79b5
	$php_auth_pw = $_SERVER['PHP_AUTH_PW'];
4c79b5
4c79b5
	if (!empty($php_auth_user) && !empty($php_auth_pw))
4c79b5
	{
4c79b5
		if ($php_auth_user !== $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_email, user_type
4c79b5
			FROM ' . USERS_TABLE . "
4c79b5
			WHERE username = '" . $db->sql_escape($php_auth_user) . "'";
4c79b5
		$result = $db->sql_query($sql);
4c79b5
		$row = $db->sql_fetchrow($result);
4c79b5
		$db->sql_freeresult($result);
4c79b5
4c79b5
		if ($row)
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...
4c79b5
			return array(
4c79b5
				'status'		=> LOGIN_SUCCESS,
4c79b5
				'error_msg'		=> false,
4c79b5
				'user_row'		=> $row,
4c79b5
			);
4c79b5
		}
4c79b5
4c79b5
		// this is the user's first login so create an empty profile
4c79b5
		return array(
4c79b5
			'status'		=> LOGIN_SUCCESS_CREATE_PROFILE,
4c79b5
			'error_msg'		=> false,
4c79b5
			'user_row'		=> user_row_apache($php_auth_user, $php_auth_pw),
4c79b5
		);
4c79b5
	}
4c79b5
4c79b5
	// Not logged into apache
4c79b5
	return array(
4c79b5
		'status'		=> LOGIN_ERROR_EXTERNAL_AUTH,
4c79b5
		'error_msg'		=> 'LOGIN_ERROR_EXTERNAL_AUTH_APACHE',
4c79b5
		'user_row'		=> array('user_id' => ANONYMOUS),
4c79b5
	);
4c79b5
}
4c79b5
4c79b5
/**
4c79b5
* Autologin function
4c79b5
*
4c79b5
* @return array containing the user row or empty if no auto login should take place
4c79b5
*/
4c79b5
function autologin_apache()
4c79b5
{
4c79b5
	global $db;
4c79b5
4c79b5
	if (!isset($_SERVER['PHP_AUTH_USER']))
4c79b5
	{
4c79b5
		return array();
4c79b5
	}
4c79b5
4c79b5
	$php_auth_user = $_SERVER['PHP_AUTH_USER'];
4c79b5
	$php_auth_pw = $_SERVER['PHP_AUTH_PW'];
4c79b5
4c79b5
	if (!empty($php_auth_user) && !empty($php_auth_pw))
4c79b5
	{
4c79b5
		set_var($php_auth_user, $php_auth_user, 'string', true);
4c79b5
		set_var($php_auth_pw, $php_auth_pw, 'string', true);
4c79b5
4c79b5
		$sql = 'SELECT *
4c79b5
			FROM ' . USERS_TABLE . "
4c79b5
			WHERE username = '" . $db->sql_escape($php_auth_user) . "'";
4c79b5
		$result = $db->sql_query($sql);
4c79b5
		$row = $db->sql_fetchrow($result);
4c79b5
		$db->sql_freeresult($result);
4c79b5
4c79b5
		if ($row)
4c79b5
		{
4c79b5
			return ($row['user_type'] == USER_INACTIVE || $row['user_type'] == USER_IGNORE) ? array() : $row;
4c79b5
		}
4c79b5
4c79b5
		if (!function_exists('user_add'))
4c79b5
		{
4c79b5
			global $phpbb_root_path, $phpEx;
4c79b5
4c79b5
			include($phpbb_root_path . 'includes/functions_user.' . $phpEx);
4c79b5
		}
4c79b5
4c79b5
		// create the user if he does not exist yet
4c79b5
		user_add(user_row_apache($php_auth_user, $php_auth_pw));
4c79b5
4c79b5
		$sql = 'SELECT *
4c79b5
			FROM ' . USERS_TABLE . "
4c79b5
			WHERE username_clean = '" . $db->sql_escape(utf8_clean_string($php_auth_user)) . "'";
4c79b5
		$result = $db->sql_query($sql);
4c79b5
		$row = $db->sql_fetchrow($result);
4c79b5
		$db->sql_freeresult($result);
4c79b5
4c79b5
		if ($row)
4c79b5
		{
4c79b5
			return $row;
4c79b5
		}
4c79b5
	}
4c79b5
4c79b5
	return array();
4c79b5
}
4c79b5
4c79b5
/**
4c79b5
* This function generates an array which can be passed to the user_add function in order to create a user
4c79b5
*/
4c79b5
function user_row_apache($username, $password)
4c79b5
{
4c79b5
	global $db, $config, $user;
4c79b5
	// first retrieve default group id
4c79b5
	$sql = 'SELECT group_id
4c79b5
		FROM ' . GROUPS_TABLE . "
4c79b5
		WHERE group_name = '" . $db->sql_escape('REGISTERED') . "'
4c79b5
			AND group_type = " . GROUP_SPECIAL;
4c79b5
	$result = $db->sql_query($sql);
4c79b5
	$row = $db->sql_fetchrow($result);
4c79b5
	$db->sql_freeresult($result);
4c79b5
4c79b5
	if (!$row)
4c79b5
	{
4c79b5
		trigger_error('NO_GROUP');
4c79b5
	}
4c79b5
4c79b5
	// generate user account data
4c79b5
	return array(
4c79b5
		'username'		=> $username,
4c79b5
		'user_password'	=> phpbb_hash($password),
4c79b5
		'user_email'	=> '',
4c79b5
		'group_id'		=> (int) $row['group_id'],
4c79b5
		'user_type'		=> USER_NORMAL,
4c79b5
		'user_ip'		=> $user->ip,
4c79b5
	);
4c79b5
}
4c79b5
4c79b5
/**
4c79b5
* The session validation function checks whether the user is still logged in
4c79b5
*
4c79b5
* @return boolean true if the given user is authenticated or false if the session should be closed
4c79b5
*/
4c79b5
function validate_session_apache(&$user)
4c79b5
{
4c79b5
	if (!isset($_SERVER['PHP_AUTH_USER']))
4c79b5
	{
4c79b5
		return false;
4c79b5
	}
4c79b5
4c79b5
	$php_auth_user = '';
4c79b5
	set_var($php_auth_user, $_SERVER['PHP_AUTH_USER'], 'string', true);
4c79b5
4c79b5
	return ($php_auth_user === $user['username']) ? true : false;
4c79b5
}
4c79b5
4c79b5
?>