Blame Identity/Webenv/phpBB/3.0.4/includes/auth/auth_apache.php

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