##############################################################
## MOD Title: Subdomain Redirection After Log In
## MOD Author: John Degey < webmaster@johndegey.org > (N/A) http://www.johndegey.org
## MOD Description: Guide that allows redirecting to a subdomain after log in.
## MOD Version: 1.0.0
##
## Installation Level: Easy
## Installation Time: 5 Minutes
## Files To Edit: 1
##  login.php
## Included Files: n/a
## License: http://opensource.org/licenses/gpl-license.php GNU General Public License v2
##############################################################

## For security purposes, please check: http://www.phpbb.com/mods/
############################################################## 
## Author Notes:
## 	Tested on 2.0.22.
##
##  This file is intended to be a guide to redirecting users to a subdomain
##  when they log in on a case-by-case basis (that is to
##  say, so that you can have some log ins redirect like this and have some
##  not). This file deals only with login.php, logging in.
##
##  This come from my use of coppermine and phpbb
##  as a bridge to authenticate coppermine users. Everything was fine until a
##  change my configuration to use subdomain instead of one like usual.
##
##  Once all these modifications have been applied, you can use log in forms to
##  redirect users to a subdomain by adding one hidden form inputs
##  like these:
##  <input type="hidden" name="subdomain" value="1" />
##  <input type="hidden" name="redirect" value="http://myother.sub.domain" />
##  You can also create log in links like this one:
##  <a href="login.php?redirect=http://myother.sub.domain&subdomain=1">Log In</a>
##
##
############################################################## 
## MOD History: 
##
##   2007-08-28 - Version 1.0.0
##      - Initial Release
##############################################################
 
## Before Adding This MOD To Your Forum, You Should Back Up All Files Related To
##############################################################
#
#-----[ OPEN ]------------------------------------------
#
login.php 

#
#-----[ FIND ]------------------------------------------
#

					if( $session_id )
					{
						$url = ( !empty($HTTP_POST_VARS['redirect']) ) ? str_replace('&amp;', '&', htmlspecialchars($HTTP_POST_VARS['redirect'])) : "index.$phpEx";
						redirect(append_sid($url, true));
					}


#
#-----[ REPLACE WITH ]------------------------------------------
#

/*  Original phpBB code - commented out for Subdomain Redirection After Log In
					if( $session_id )
					{
						$url = ( !empty($HTTP_POST_VARS['redirect']) ) ? str_replace('&amp;', '&', htmlspecialchars($HTTP_POST_VARS['redirect'])) : "index.$phpEx";
						redirect(append_sid($url, true));
					}
*/
//---------------------------------------------------------------------------------
// Subdomain Redirection After Log In - Begin Code Addition
//
if( $session_id )
{
	if ( isset($HTTP_POST_VARS['redirect']) && isset($HTTP_POST_VARS['subdomain']) && $HTTP_POST_VARS['subdomain']==1 )
	{
		$board_domain = substr($board_config['sitename'],strpos($board_config['sitename'],'.')+1);
		$pattern_sub_domain = "/^http:\/\/([a-z0-9_\-]+\.".$board_domain.")/i";
		if( preg_match($pattern_sub_domain,$HTTP_POST_VARS['redirect'],$forward_matches) )
		{
			$redirect_to = $HTTP_POST_VARS['redirect'].append_sid($url, true);
			header('Location: ' . $redirect_to);
			exit;
		}
	}
	$url = ( !empty($HTTP_POST_VARS['redirect']) ) ? str_replace('&amp;', '&', htmlspecialchars($HTTP_POST_VARS['redirect'])) : "index.$phpEx";
	redirect(append_sid($url, true));
}
//
// Subdomain Redirection After Log In - End Code Addition
//---------------------------------------------------------------------------------

#
#-----[ FIND ]------------------------------------------
#

	if( preg_match("/^redirect=([a-z0-9\.#\/\?&=\+\-_]+)/si", $forward_to, $forward_matches) )
	{
		$forward_to = ( !empty($forward_matches[3]) ) ? $forward_matches[3] : $forward_matches[1];
		$forward_match = explode('&', $forward_to);
 
		if(count($forward_match) > 1)
		{
			for($i = 1; $i < count($forward_match); $i++)
			{
				if( !ereg("sid=", $forward_match[$i]) )
				{
					if( $forward_page != '' )
					{
						$forward_page .= '&';
					}
					$forward_page .= $forward_match[$i];
				}
		    }
		    $forward_page = $forward_match[0] . '?' . $forward_page;
		}
		else
		{
		    $forward_page = $forward_match[0];
		}
	}

#
#-----[ REPLACE WITH ]------------------------------------------
#

/*  Original phpBB code - commented out for Subdomain Redirection After Log In
	if( preg_match("/^redirect=([a-z0-9\.#\/\?&=\+\-_]+)/si", $forward_to, $forward_matches) )
	{
	    $forward_to = ( !empty($forward_matches[3]) ) ? $forward_matches[3] : $forward_matches[1];
		$forward_match = explode('&', $forward_to);
 
		if(count($forward_match) > 1)
		{
		    for($i = 1; $i < count($forward_match); $i++)
		    {
			    if( !ereg("sid=", $forward_match[$i]) )
			    {
				    if( $forward_page != '' )
				    {
					    $forward_page .= '&';
				    }
				    $forward_page .= $forward_match[$i];
			    }
		    }
		    $forward_page = $forward_match[0] . '?' . $forward_page;
		}
		else
		{
		    $forward_page = $forward_match[0];
		}
	}
*/
//---------------------------------------------------------------------------------
// Subdomain Redirection After Log In - Begin Code Addition
//
$do_as_usual = true;
if ( (isset($HTTP_POST_VARS['subdomain']) && $HTTP_POST_VARS['subdomain'] == 1) || (isset($HTTP_GET_VARS['subdomain']) && $HTTP_GET_VARS['subdomain'] == 1) )
{
	$board_domain = substr($board_config['sitename'],strpos($board_config['sitename'],'.')+1);
	$pattern_sub_domain = "/^redirect=http:\/\/([a-z0-9_\-]+\.".$board_domain.")/i";
	if ( preg_match($pattern_sub_domain,$forward_to,$forward_matches) )
	{
		$forward_page = 'http://'.$forward_matches[1];
		$do_as_usual = false;
	}
}
if ( $do_as_usual )
{
	if ( preg_match("/^redirect=([a-z0-9\.#\/\?&=\+\-_]+)/si", $forward_to, $forward_matches) )
	{
		$forward_to = ( !empty($forward_matches[3]) ) ? $forward_matches[3] : $forward_matches[1];
		$forward_match = explode('&', $forward_to);

		if ( count($forward_match) > 1)
		{
			for($i = 1; $i < count($forward_match); $i++)
			{
				if( !ereg("sid=", $forward_match[$i]) )
				{
					if ( $forward_page != '' )
					{
						$forward_page .= '&';
					}
					$forward_page .= $forward_match[$i];
				}
			}
			$forward_page = $forward_match[0] . '?' . $forward_page;
		}
		else
		{
			$forward_page = $forward_match[0];
		}
	}
}
//
// Subdomain Redirection After Log In - End Code Addition
//---------------------------------------------------------------------------------


#
#-----[ FIND ]------------------------------------------
#
		$s_hidden_fields = '<input type="hidden" name="redirect" value="' . $forward_page . '" />';


#
#-----[ AFTER, ADD ]------------------------------------------
#

//---------------------------------------------------------------------------------
// Subdomain Redirection After Log In - Begin Code Addition
//
if ( (isset($HTTP_POST_VARS['subdomain']) && $HTTP_POST_VARS['subdomain'] == 1) || (isset($HTTP_GET_VARS['subdomain']) && $HTTP_GET_VARS['subdomain'] == 1) )
{
	$s_hidden_fields .= '<input type="hidden" name="subdomain" value="1" />';
}
//
// Subdomain Redirection After Log In - End Code Addition
//---------------------------------------------------------------------------------

#
#-----[ SAVE/CLOSE ALL FILES ]------------------------------------------
#
# EoM
