############################################################## 
## MOD Title: Account Switch Mod
## MOD Author: Wildfire < wildfire@macrossgenerations.com > (Sara Miller) www.winternightex.com 
## MOD Description: This Mod lets users switch between any number of linked accounts
## MOD Version: 1.1.0 
## 
## Installation Level: Advanced
## Installation Time: 15 Minutes 
## Files To Edit: login.php, includes/page_header.php, includes/usercp_register.php, templates/subSilver/profile_add_body.tpl, templates/subSilver/overall_header.tpl 
## Included Files: pid.php, templates/subSilver/pid.tpl, primer.txt 
############################################################## 
## For Security Purposes, Please Check: http://www.phpbb.com/mods/downloads/ for the 
## latest version of this MOD. Downloading this MOD from other sites could cause malicious code 
## to enter into your phpBB Forum. As such, phpBB will not offer support for MOD's not offered 
## in our MOD-Database, located at: http://www.phpbb.com/mods/downloads/ 
############################################################## 
## Author Notes: 
##  This Mod requires modification of the phpBB database.  If you are incapable of or 
##  are not comfortable with doing so, please do not attempt installation of this MOD.
##  Primer.txt is for distrubution to users after installation to instruct them on usage.
############################################################## 
## Before Adding This MOD To Your Forum, You Should Back Up All Files Related To This MOD 
############################################################## 

# 
#-----[ OPEN ]------------------------------------------ 
# 
login.php

# 
#-----[ FIND ]------------------------------------------ 
# 
if( isset($HTTP_POST_VARS['login']) || isset($HTTP_GET_VARS['login']) || isset($HTTP_POST_VARS['logout']) || isset($HTTP_GET_VARS['logout']) )
{
	//
	// This appears to work for IIS5 CGI under Win2K. Uses getenv
	// since this doesn't exist for ISAPI mode and therefore the 
	// normal Location redirector is used in preference
	//
if( ( isset($HTTP_POST_VARS['login']) || isset($HTTP_GET_VARS['login']) ) && !$userdata['session_logged_in'] )
	{
		$username = isset($HTTP_POST_VARS['username']) ? $HTTP_POST_VARS['username'] : '';
		$password = isset($HTTP_POST_VARS['password']) ? $HTTP_POST_VARS['password'] : '';

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

if( isset($HTTP_POST_VARS['login']) || isset($HTTP_GET_VARS['login']) || isset($HTTP_POST_VARS['logout']) || isset($HTTP_GET_VARS['logout']) || isset($HTTP_POST_VARS['switch']) || isset($HTTP_GET_VARS['switch']))
{
	//
	// This appears to work for IIS5 CGI under Win2K. Uses getenv
	// since this doesn't exist for ISAPI mode and therefore the 
	// normal Location redirector is used in preference
	//
if( isset($HTTP_POST_VARS['switch']) || isset($HTTP_GET_VARS['switch']) || isset($HTTP_POST_VARS['login']) || isset($HTTP_GET_VARS['login']))
	{
		if( (isset($HTTP_POST_VARS['switch']) || isset($HTTP_GET_VARS['switch']))  && $userdata['session_logged_in'])
		{
			$sql = "SELECT username, user_password 
				FROM ".USERS_TABLE." 
				WHERE username = '" . str_replace("\'", "''", $userdata['username']) . "'";
			$currentresult = $db->sql_query($sql);
			$currentarray = $db->sql_fetchrow($currentresult);
	
			$sql = "SELECT user_id, username, user_password 
				FROM ".USERS_TABLE." 
				WHERE username = '" . str_replace("\'", "''", $jump) . "'"; 
			$boxresult = $db->sql_query($sql);
			$boxarray = $db->sql_fetchrow($boxresult);

			if ($currentarray['PID'] == $boxarray['PID'])
			{
				session_end($userdata['session_id'], $userdata['user_id']);	
				$username = $boxarray['username'];
				$password = $boxarray['user_password'];
			}

		}
		else if( ( isset($HTTP_POST_VARS['login']) || isset($HTTP_GET_VARS['login']) ) && !$userdata['session_logged_in'] )
		{
			$username = isset($HTTP_POST_VARS['username']) ? $HTTP_POST_VARS['username'] : "";
			$passwordnocrypt = isset($HTTP_POST_VARS['password']) ? $HTTP_POST_VARS['password'] : "";
			$password = md5($passwordnocrypt);
		}

# 
#-----[ FIND ]------------------------------------------ 
# 
if( $row = $db->sql_fetchrow($result) )
		{
			if( $row['user_level'] != ADMIN && $board_config['board_disable'] )
			{
				header($header_location . append_sid("index.$phpEx", true));
			}
			else
			{
				if( md5($password) == $row['user_password'] && $row['user_active'] )

# 
#-----[ REPLACE WITH ]------------------------------------------ 
# 
		if( $row = $db->sql_fetchrow($result) )
		{
			if( $row['user_level'] != ADMIN && $board_config['board_disable'] )
			{
				header($header_location . append_sid("index.$phpEx", true));
			}
			else
			{
				if( $password == $row['user_password'] && $row['user_active'] )

# 
#-----[ OPEN ]------------------------------------------ 
# 
includes/page_header.php

# 
#-----[ FIND ]------------------------------------------ 
#
//
// Generate HTML required for Mozilla Navigation bar
//
$nav_links_html = '';
$nav_link_proto = '<link rel="%s" href="%s" title="%s" />' . "\n";
while( list($nav_item, $nav_array) = @each($nav_links) )
{
	if ( !empty($nav_array['url']) )
	{
		$nav_links_html .= sprintf($nav_link_proto, $nav_item, $nav_array['url'], $nav_array['title']);
	}
	else
	{
		// We have a nested array, used for items like <link rel='chapter'> that can occur more than once.
		while( list(,$nested_array) = each($nav_array) )
		{  
			$nav_links_html .= sprintf($nav_link_proto, $nav_item, $nested_array['url'], $nested_array['title']);
		}
	}
}
# 
#-----[ AFTER, ADD ]------------------------------------------ 
# 
//******************************ACCOUNT SWITCH MOD BEGIN**********
$switchcount = 0;	
$sql = "SELECT * FROM " . USERS_TABLE . " WHERE PID = " . $userdata['PID'];
if (!($accounts = $db->sql_query($sql)))
	$switchcontext = "<option>No Results</option>";
else
{
	while ($accountrow = $db->sql_fetchrow($accounts))
	{
		$switchcount = $switchcount + 1;
		$name = $accountrow['username'];
		$switchcontext = $switchcontext . "<option value=\"$name\">$name</option>";
	}
}
//*****************************ACCOUNT SWITCH MOD END*************

# 
#-----[ FIND ]------------------------------------------ 
# 
	'NAV_LINKS' => $nav_links_html)

# 
#-----[ BEFORE, ADD ]------------------------------------------ 
# 
	'SWITCH' => $switchcontext,

# 
#-----[ OPEN ]------------------------------------------ 
# 
includes/usercp_register.php

# 
#-----[ FIND ]------------------------------------------ 
# 
		if ( $mode == 'editprofile' )
		{
			if ( $email != $userdata['user_email'] && $board_config['require_activation'] != USER_ACTIVATION_NONE && $userdata['user_level'] != ADMIN )
			{
				$user_active = 0;

				$user_actkey = gen_rand_string(true);
				$key_len = 54 - ( strlen($server_url) );
				$key_len = ( $key_len > 6 ) ? $key_len : 6;
				$user_actkey = substr($user_actkey, 0, $key_len);

				if ( $userdata['session_logged_in'] )
				{
					session_end($userdata['session_id'], $userdata['user_id']);
				}
			}
			else
			{
				$user_active = 1;
				$user_actkey = '';
			}

# 
#-----[ BEFORE, ADD ]------------------------------------------ 
# 
		if ( $mode == 'editprofile' && $linkname)
		{

			//***************************ACCOUNT SWITCH MOD START*****************
			$linkpass = md5($linkpass);
			$sql = "SELECT * FROM " . USERS_TABLE . " WHERE username = '$linkname'";
			if ( !($pidresult = $db->sql_query($sql)) )
				message_die(GENERAL_ERROR, "No result");				
			$resultarray = $db->sql_fetchrow($pidresult);
			$pidcheck = $resultarray['PID'];			
			$passcheck = $resultarray['user_password'];
			if($passcheck != $linkpass)
			{
				message_die(GENERAL_ERROR, "Wrong password for link account.");
			}
			
			if (($linkpid != $pidcheck) && ($pidcheck != NULL))
				{
					message_die(GENERAL_ERROR, "Wrong Personnel ID for link account.");
				}
		
			else
			{
				$pid = $linkpid;
				$linksql = "UPDATE " . USERS_TABLE . " SET PID = $pid WHERE username = '$username'";
				$linkresult = $db->sql_query($linksql);
			}
			//****************************ACCOUNT SWITCH MOD END********************
		}

# 
#-----[ FIND ]------------------------------------------ 
# 
else if ( $mode == 'editprofile' && !isset($HTTP_POST_VARS['avatargallery']) && !isset($HTTP_POST_VARS['submitavatar']) && !isset($HTTP_POST_VARS['cancelavatar']) )
{
	$user_id = $userdata['user_id'];
	$username = htmlspecialchars($userdata['username']);
	$email = $userdata['user_email'];
	$password = '';
	$password_confirm = '';

	$icq = $userdata['user_icq'];
	$aim = htmlspecialchars(str_replace('+', ' ', $userdata['user_aim']));
	$msn = htmlspecialchars($userdata['user_msnm']);
	$yim = htmlspecialchars($userdata['user_yim']);

# 
#-----[ AFTER, ADD ]------------------------------------------ 
# 
	//*************************ACCOUNT SWITCH MOD START*********************
	$pid = htmlspecialchars($userdata['PID']);
	//*************************ACCOUNT SWITCH MOD END **********************

# 
#-----[ FIND ]------------------------------------------ 
# 
	$template->assign_vars(array(
		'USERNAME' => $username,
		'EMAIL' => $email,
		'YIM' => $yim,
		'ICQ' => $icq,
		'MSN' => $msn,
		'AIM' => $aim,

# 
#-----[ AFTER, ADD ]------------------------------------------ 
#
		'PID' => $pid,

# 
#-----[ OPEN ]------------------------------------------ 
#
templates/subSilver/profile_add_body.tpl

# 
#-----[ FIND ]------------------------------------------ 
#
<!-- BEGIN switch_avatar_block -->

# 
#-----[ BEFORE, ADD ]------------------------------------------ 
#
<!-- ACCOUNT SWITCH MOD START -->
	<tr>
	<th class="thSides" colspan="2" height="12" valign="middle">Account Switch Settings</th>
	</tr>
	<tr align="center">
	  <td class="row1" colspan="2">
		<table width=90%" cellspacing="2" cellpadding="0" border="0">
		<tr>
			<td width="90%" aligh="left"><span class="gensmall">Enter a Username and password pair or another account you would like to link to this one, and your PID below.  If your PID appear below, this account is already linked.</span></td>
			<td align="left"><span class="gensmall">Don't have a PID yet?<br/><a href="pid.php">Click here</a>
		</tr>
		</table>
	  </td>
	<tr>
	<tr align="left">
	<td>Username:</td>
	<td><input type="text" class="post"style="width: 150px"  name="linkname" size="20" maxlength="255" value="{LINKNAME}" /></td>
	</tr><tr>
	<td>Password:</td>
	<td><input type="password" class="post"style="width: 150px"  name="linkpass" size="20" maxlength="255" value="{LINKPASS}" /></td>
	</tr><tr>
	<td>PID:</td>
	<td><input type="password" class="post"style="width: 150px"  name="linkpid" size="20" maxlength="255" value="{PID}" /></td>
	</tr>
	<!-- ACCOUNT SWITCH MOD END -->

# 
#-----[ OPEN ]------------------------------------------ 
#
templates/subSilver/overall_header.tpl

# 
#-----[ FIND ]------------------------------------------ 
#
<body bgcolor="{T_BODY_BGCOLOR}" text="{T_BODY_TEXT}" link="{T_BODY_LINK}" vlink="{T_BODY_VLINK}">

# 
#-----[ AFTER, ADD ]------------------------------------------ 
#
<!-- BEGIN switch_user_logged_out -->
<form method="post" action="{S_LOGIN_ACTION}">
  <table width="100%" cellpadding="3" cellspacing="1" border="0" class="forumline">
	<tr> 
	  <td class="row1" align="right" valign="middle" height="28"><span class="gensmall">{L_USERNAME}: 
		<input class="post" type="text" name="username" size="10" />
		&nbsp;&nbsp;&nbsp;{L_PASSWORD}: 
		<input class="post" type="password" name="password" size="10" />
		<br/>
		&nbsp;&nbsp; &nbsp;&nbsp;Autologin? 
		<input class="text" type="checkbox" name="autologin" />
		&nbsp;&nbsp;&nbsp; 
		<input type="submit" class="mainoption" name="login" value="{L_LOGIN}" />
		</span> </td>
	</tr>
  </table>
</form>
<!-- END switch_user_logged_out -->

<!-- BEGIN switch_user_logged_in -->
<form method="post" action="{S_LOGIN_ACTION}">
  <table width="100%" cellpadding="3" cellspacing="1" border="0" class="forumline">
	<tr> 
	  <td class="row1" align="right" valign="middle" height="28"><span class="gensmall"> 
		&nbsp;&nbsp;&nbsp;Account Switch:
		<select name="jump">
		{SWITCH}
		</select>
		<input type="submit" class="mainoption" name="switch" value="Switch" />
		</span> </td>
	</tr>
  </table>
</form>
<!-- END switch_user_logged_in -->

# 
#-----[ SQL ]------------------------------------------ 
# 
ALTER TABLE prefix_users ADD PID VARCHAR(5) DEFAULT NULL;

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