<?

/*
+--------------------------------------------------------------------------
|   Invision Power Board v1.1
|   ========================================
|   by Matthew Mecham
|   (c) 2001,2002 Invision Power Services, Inc
|   http://www.ibforums.com
|   ========================================
|   Web: http://www.ibforums.com
|   Email: phpboards@ibforums.com
|   Licence Info: phpib-licence@ibforums.com
+---------------------------------------------------------------------------
|
|   > IP Chat => IPB Bridge Script
|   > Script written by Matt Mecham
|   > Date started: 17th February 2003
|
+--------------------------------------------------------------------------
*/

$root_path = './';

//----------------------------------------------
// END OF USER EDITABLE COMPONENTS
//---------------------------------------------

require $root_path."conf_global.php";

define( 'DENIED', 0 );
define( 'ACCESS', 1 );
define( 'ADMIN' , 2 );


$db_info['host']       = $INFO['sql_host'];
$db_info['user']       = $INFO['sql_user'];
$db_info['pass']       = $INFO['sql_pass'];
$db_info['database']   = $INFO['sql_database'];
$db_info['tbl_prefix'] = $INFO['sql_tbl_prefix'];

$allowed_groups        = $INFO['chat_admin_groups'];

$allow_guest_access    = $INFO['chat_allow_guest'] == 1 ? ACCESS : DENIED;

// Stupid PHP changing it's mind on HTTP args

$username = $_GET['username'] != "" ? $_GET['username'] : $HTTP_GET_VARS['username'];
$password = $_GET['password'] != "" ? $_GET['password'] : $HTTP_GET_VARS['password'];
$ip       = $_GET['ip']       != "" ? $_GET['ip']       : $HTTP_GET_VARS['ip'];

// Remove URL encoding (%20, etc)

$username = urldecode(trim($username));
$password = urldecode(trim($password));
$ip       = urldecode(trim($ip));

//----------------------------------------------
// Main code
//----------------------------------------------

// Start off with the lowest accessibility

$output_int  = $allow_guest_access;
$output_name = "";



$DB = @mysql_connect( $db_info['host'], $db_info['user'], $db_info['pass'] );

if ( ! @mysql_select_db( $db_info['database'] ) )
{
	die_nice();
	
	//-- script exits --//
}

//------------------------------
// Attempt to find the user
//------------------------------

$md5_password = md5($password);

$query_id = @mysql_query("SELECT m.mgroup, m.password, m.name, m.id  FROM {$db_info['tbl_prefix']}members m
						  WHERE m.name='$username' LIMIT 1"
						 , $DB );
						
if ( ! $query_id )
{
	die_nice();
	
	//-- script exits --//
}


if ( ! $member = @mysql_fetch_array( $query_id, MYSQL_ASSOC ) )
{
	// No member found - allow guest access?
	
	die_nice($allow_guest_access);
	
	//-- script exits --//
}


@mysql_close();


//------------------------------
// Check password - member exists
//------------------------------

if ( $password != "" )
{
	// Password was entered..
	
	if ( $md5_password != $member['password'] )
	{
		// Password incorrect..
		
		die_nice();
		
		//-- script exits --//
	}
}
else
{
	// No password entered - die!
	// Do not allow guest access on reg. name
	
	die_nice();
		
	//-- script exits --//
}


//------------------------------
// Do we have admin access?
//------------------------------

$output_int = ACCESS;

if ( preg_match( "/(^|,)".$member['mgroup']."(,|$)/", $allowed_groups ) )
{
	$output_int = ADMIN;
}


//------------------------------
// Spill the beans
//------------------------------

print $output_int;

exit();
	 
	 
function die_nice( $access=0 )
{
	// Simply error out silently, showing guest access only for the user
	@mysql_close();
	print $access;
	exit();
}
	 
?>