##############################################################
## MOD Title: Subdomain Redirection For Log In With a Board Integration
## MOD Author: John Degey < webmaster@johndegey.org > http://www.johndegey.org
## MOD Description: Guide that allows redirecting to a subdomain for log in.
## MOD Version: 1.0.1
##
## Installation Level: Easy
## Installation Time: 5 Minutes
##
## Files To Edit: 1
##  bridge/phpbb2018.inc.php
##
## Included Files: n/a
##
## License: http://opensource.org/licenses/gpl-license.php GNU General Public License v2 
##
############################################################## 
## Author Notes:
## 	Tested on 1.4.12
##
##  This file is intended to be a guide to redirecting users to a subdomain
##  when they log in into an integretad system with phpbb
##
##  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.
##
##
############################################################## 
## MOD History: 
##
##   2007-08-28 - Version 1.0.1
##      - Remove the redirecting_subdomain function from the bridge/udb_base.inc.php 
##        and put it in the bridge/phpbb2018.inc.php like suggested by the coppermine team
##   2007-08-28 - Version 1.0.0
##      - Initial Release
############################################################## 
## BEFORE ADDING THIS TO YOUR ALBUM, YOU SHOULD BACK UP ALL RELATED FILES.
############################################################## 


#
#-----[ OPEN ]------------------------------------------
#
bridge/phpbb2018.inc.php

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

	function login_page()


#
#-----[ BEFORE, ADD ]------------------------------------------
#

//---------------------------------------------------------------------------------
// Subdomain Redirection For Log In - Begin Code Addition
//
    function redirecting_subdomain() {
		global $CONFIG;

        $cpgHost = parse_url($CONFIG['site_url'],PHP_URL_HOST);
        $boardHost = parse_url($this->boardurl,PHP_URL_HOST);
        if($cpgHost == $boardHost) return FALSE;
        /*
          we check the subdomain a little bit
        */
        $cpgDomain = substr($cpgHost,strpos($cpgHost,'.')+1);
        $boardDomain = substr($boardHost,strpos($boardHost,'.')+1);
        
        if($cpgDomain != $boardDomain) return FALSE;

        $this->redirect("/login.php?redirect=${CONFIG['site_url']}&subdomain=1");
        return TRUE;
    }
//
// Subdomain Redirection For Log In - End Code Addition
//---------------------------------------------------------------------------------



#
#-----[ FIND ]------------------------------------------
#
	function login_page()
	{
		global $CONFIG;
		
        $cpg = parse_url($CONFIG['site_url']);
        $bb = parse_url($this->boardurl);
        $levels = count(explode('/', $bb['path'])) - 1;
		$redirect = str_repeat('../', $levels) . trim($cpg['path'], '/') . '/';
		$this->redirect("/login.php?redirect=$redirect");
	}

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

/*  Original Coppermine code - commented out for Subdomain Redirection For Log In
*/
/*
	function login_page()
	{
		global $CONFIG;
		
        $cpg = parse_url($CONFIG['site_url']);
        $bb = parse_url($this->boardurl);
        $levels = count(explode('/', $bb['path'])) - 1;
		$redirect = str_repeat('../', $levels) . trim($cpg['path'], '/') . '/';
		$this->redirect("/login.php?redirect=$redirect");
	}
*/

//---------------------------------------------------------------------------------
// Subdomain Redirection For Log In - Begin Code Addition
//
	function login_page()
	{
		global $CONFIG;
		
        if(!$this->redirecting_subdomain()) {
            $cpg = parse_url($CONFIG['site_url']);
		    $bb = parse_url($this->boardurl);
            $levels = count(explode('/', $bb['path'])) - 1;
		    $redirect = str_repeat('../', $levels) . trim($cpg['path'], '/') . '/';
		    $this->redirect("/login.php?redirect=$redirect");
        }
	}
//
// Subdomain Redirection For Log In - End Code Addition
//---------------------------------------------------------------------------------


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