includes/auth/auth_apache.php
changeset 0 ceef6d64c21d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/includes/auth/auth_apache.php	Wed Mar 07 16:41:10 2012 +0400
     1.3 @@ -0,0 +1,249 @@
     1.4 +<?php
     1.5 +/**
     1.6 +* Apache auth plug-in for phpBB3
     1.7 +*
     1.8 +* Authentication plug-ins is largely down to Sergey Kanareykin, our thanks to him.
     1.9 +*
    1.10 +* @package login
    1.11 +* @version $Id$
    1.12 +* @copyright (c) 2005 phpBB Group
    1.13 +* @license http://opensource.org/licenses/gpl-license.php GNU Public License
    1.14 +*
    1.15 +*/
    1.16 +
    1.17 +/**
    1.18 +* @ignore
    1.19 +*/
    1.20 +if (!defined('IN_PHPBB'))
    1.21 +{
    1.22 +	exit;
    1.23 +}
    1.24 +
    1.25 +/**
    1.26 +* Checks whether the user is identified to apache
    1.27 +* Only allow changing authentication to apache if the user is identified
    1.28 +* Called in acp_board while setting authentication plugins
    1.29 +*
    1.30 +* @return boolean|string false if the user is identified and else an error message
    1.31 +*/
    1.32 +function init_apache()
    1.33 +{
    1.34 +	global $user;
    1.35 +
    1.36 +	if (!isset($_SERVER['PHP_AUTH_USER']) || $user->data['username'] !== $_SERVER['PHP_AUTH_USER'])
    1.37 +	{
    1.38 +		return $user->lang['APACHE_SETUP_BEFORE_USE'];
    1.39 +	}
    1.40 +	return false;
    1.41 +}
    1.42 +
    1.43 +/**
    1.44 +* Login function
    1.45 +*/
    1.46 +function login_apache(&$username, &$password)
    1.47 +{
    1.48 +	global $db;
    1.49 +
    1.50 +	// do not allow empty password
    1.51 +	if (!$password)
    1.52 +	{
    1.53 +		return array(
    1.54 +			'status'	=> LOGIN_ERROR_PASSWORD,
    1.55 +			'error_msg'	=> 'NO_PASSWORD_SUPPLIED',
    1.56 +			'user_row'	=> array('user_id' => ANONYMOUS),
    1.57 +		);
    1.58 +	}
    1.59 +
    1.60 +	if (!$username)
    1.61 +	{
    1.62 +		return array(
    1.63 +			'status'	=> LOGIN_ERROR_USERNAME,
    1.64 +			'error_msg'	=> 'LOGIN_ERROR_USERNAME',
    1.65 +			'user_row'	=> array('user_id' => ANONYMOUS),
    1.66 +		);
    1.67 +	}
    1.68 +
    1.69 +	if (!isset($_SERVER['PHP_AUTH_USER']))
    1.70 +	{
    1.71 +		return array(
    1.72 +			'status'		=> LOGIN_ERROR_EXTERNAL_AUTH,
    1.73 +			'error_msg'		=> 'LOGIN_ERROR_EXTERNAL_AUTH_APACHE',
    1.74 +			'user_row'		=> array('user_id' => ANONYMOUS),
    1.75 +		);
    1.76 +	}
    1.77 +
    1.78 +	$php_auth_user = $_SERVER['PHP_AUTH_USER'];
    1.79 +	$php_auth_pw = $_SERVER['PHP_AUTH_PW'];
    1.80 +
    1.81 +	if (!empty($php_auth_user) && !empty($php_auth_pw))
    1.82 +	{
    1.83 +		if ($php_auth_user !== $username)
    1.84 +		{
    1.85 +			return array(
    1.86 +				'status'	=> LOGIN_ERROR_USERNAME,
    1.87 +				'error_msg'	=> 'LOGIN_ERROR_USERNAME',
    1.88 +				'user_row'	=> array('user_id' => ANONYMOUS),
    1.89 +			);
    1.90 +		}
    1.91 +
    1.92 +		$sql = 'SELECT user_id, username, user_password, user_passchg, user_email, user_type
    1.93 +			FROM ' . USERS_TABLE . "
    1.94 +			WHERE username = '" . $db->sql_escape($php_auth_user) . "'";
    1.95 +		$result = $db->sql_query($sql);
    1.96 +		$row = $db->sql_fetchrow($result);
    1.97 +		$db->sql_freeresult($result);
    1.98 +
    1.99 +		if ($row)
   1.100 +		{
   1.101 +			// User inactive...
   1.102 +			if ($row['user_type'] == USER_INACTIVE || $row['user_type'] == USER_IGNORE)
   1.103 +			{
   1.104 +				return array(
   1.105 +					'status'		=> LOGIN_ERROR_ACTIVE,
   1.106 +					'error_msg'		=> 'ACTIVE_ERROR',
   1.107 +					'user_row'		=> $row,
   1.108 +				);
   1.109 +			}
   1.110 +
   1.111 +			// Successful login...
   1.112 +			return array(
   1.113 +				'status'		=> LOGIN_SUCCESS,
   1.114 +				'error_msg'		=> false,
   1.115 +				'user_row'		=> $row,
   1.116 +			);
   1.117 +		}
   1.118 +
   1.119 +		// this is the user's first login so create an empty profile
   1.120 +		return array(
   1.121 +			'status'		=> LOGIN_SUCCESS_CREATE_PROFILE,
   1.122 +			'error_msg'		=> false,
   1.123 +			'user_row'		=> user_row_apache($php_auth_user, $php_auth_pw),
   1.124 +		);
   1.125 +	}
   1.126 +
   1.127 +	// Not logged into apache
   1.128 +	return array(
   1.129 +		'status'		=> LOGIN_ERROR_EXTERNAL_AUTH,
   1.130 +		'error_msg'		=> 'LOGIN_ERROR_EXTERNAL_AUTH_APACHE',
   1.131 +		'user_row'		=> array('user_id' => ANONYMOUS),
   1.132 +	);
   1.133 +}
   1.134 +
   1.135 +/**
   1.136 +* Autologin function
   1.137 +*
   1.138 +* @return array containing the user row or empty if no auto login should take place
   1.139 +*/
   1.140 +function autologin_apache()
   1.141 +{
   1.142 +	global $db;
   1.143 +
   1.144 +	if (!isset($_SERVER['PHP_AUTH_USER']))
   1.145 +	{
   1.146 +		return array();
   1.147 +	}
   1.148 +
   1.149 +	$php_auth_user = $_SERVER['PHP_AUTH_USER'];
   1.150 +	$php_auth_pw = $_SERVER['PHP_AUTH_PW'];
   1.151 +
   1.152 +	if (!empty($php_auth_user) && !empty($php_auth_pw))
   1.153 +	{
   1.154 +		set_var($php_auth_user, $php_auth_user, 'string', true);
   1.155 +		set_var($php_auth_pw, $php_auth_pw, 'string', true);
   1.156 +
   1.157 +		$sql = 'SELECT *
   1.158 +			FROM ' . USERS_TABLE . "
   1.159 +			WHERE username = '" . $db->sql_escape($php_auth_user) . "'";
   1.160 +		$result = $db->sql_query($sql);
   1.161 +		$row = $db->sql_fetchrow($result);
   1.162 +		$db->sql_freeresult($result);
   1.163 +
   1.164 +		if ($row)
   1.165 +		{
   1.166 +			return ($row['user_type'] == USER_INACTIVE || $row['user_type'] == USER_IGNORE) ? array() : $row;
   1.167 +		}
   1.168 +
   1.169 +		if (!function_exists('user_add'))
   1.170 +		{
   1.171 +			global $phpbb_root_path, $phpEx;
   1.172 +
   1.173 +			include($phpbb_root_path . 'includes/functions_user.' . $phpEx);
   1.174 +		}
   1.175 +
   1.176 +		// create the user if he does not exist yet
   1.177 +		user_add(user_row_apache($php_auth_user, $php_auth_pw));
   1.178 +
   1.179 +		$sql = 'SELECT *
   1.180 +			FROM ' . USERS_TABLE . "
   1.181 +			WHERE username_clean = '" . $db->sql_escape(utf8_clean_string($php_auth_user)) . "'";
   1.182 +		$result = $db->sql_query($sql);
   1.183 +		$row = $db->sql_fetchrow($result);
   1.184 +		$db->sql_freeresult($result);
   1.185 +
   1.186 +		if ($row)
   1.187 +		{
   1.188 +			return $row;
   1.189 +		}
   1.190 +	}
   1.191 +
   1.192 +	return array();
   1.193 +}
   1.194 +
   1.195 +/**
   1.196 +* This function generates an array which can be passed to the user_add function in order to create a user
   1.197 +*/
   1.198 +function user_row_apache($username, $password)
   1.199 +{
   1.200 +	global $db, $config, $user;
   1.201 +	// first retrieve default group id
   1.202 +	$sql = 'SELECT group_id
   1.203 +		FROM ' . GROUPS_TABLE . "
   1.204 +		WHERE group_name = '" . $db->sql_escape('REGISTERED') . "'
   1.205 +			AND group_type = " . GROUP_SPECIAL;
   1.206 +	$result = $db->sql_query($sql);
   1.207 +	$row = $db->sql_fetchrow($result);
   1.208 +	$db->sql_freeresult($result);
   1.209 +
   1.210 +	if (!$row)
   1.211 +	{
   1.212 +		trigger_error('NO_GROUP');
   1.213 +	}
   1.214 +
   1.215 +	// generate user account data
   1.216 +	return array(
   1.217 +		'username'		=> $username,
   1.218 +		'user_password'	=> phpbb_hash($password),
   1.219 +		'user_email'	=> '',
   1.220 +		'group_id'		=> (int) $row['group_id'],
   1.221 +		'user_type'		=> USER_NORMAL,
   1.222 +		'user_ip'		=> $user->ip,
   1.223 +		'user_new'		=> ($config['new_member_post_limit']) ? 1 : 0,
   1.224 +	);
   1.225 +}
   1.226 +
   1.227 +/**
   1.228 +* The session validation function checks whether the user is still logged in
   1.229 +*
   1.230 +* @return boolean true if the given user is authenticated or false if the session should be closed
   1.231 +*/
   1.232 +function validate_session_apache(&$user)
   1.233 +{
   1.234 +	// Check if PHP_AUTH_USER is set and handle this case
   1.235 +	if (isset($_SERVER['PHP_AUTH_USER']))
   1.236 +	{
   1.237 +		$php_auth_user = '';
   1.238 +		set_var($php_auth_user, $_SERVER['PHP_AUTH_USER'], 'string', true);
   1.239 +
   1.240 +		return ($php_auth_user === $user['username']) ? true : false;
   1.241 +	}
   1.242 +
   1.243 +	// PHP_AUTH_USER is not set. A valid session is now determined by the user type (anonymous/bot or not)
   1.244 +	if ($user['user_type'] == USER_IGNORE)
   1.245 +	{
   1.246 +		return true;
   1.247 +	}
   1.248 +
   1.249 +	return false;
   1.250 +}
   1.251 +
   1.252 +?>
   1.253 \ No newline at end of file