<?

// Seed the damn random number generator
mt_srand(make_seed());

// Create 12-byte ID
function CreateId12()
{
	// Create the prefix
	$prefix = "000".dechex(mt_rand());
	$prefix = substr($prefix, strlen($prefix) - 3);

	// Create the prefix
	$suffix = "00000000".dechex(mt_rand());
	$suffix = substr($suffix, strlen($suffix) - 8);

	// Return it
	return strtoupper($prefix."-".$suffix);
}

// Make a seed for the random number generator
function make_seed()
{
	list($usec, $sec) = explode(' ', microtime());
	return (float) $sec + ((float) $usec * 100000);
}

// Load scheme

$gblScheme = array();

// Connect
$db = mysql_connect("localhost", $gblMySqlUser, $gblMySqlPass);
mysql_select_db($gblMySqlDb, $db);

$query = "SELECT name, value FROM scheme WHERE site = '" . $gblSiteName . "'";
$result = mysql_query($query, $db);

// Loop results
for( $RowIndex = 0; $a = mysql_fetch_array($result); $RowIndex++ )
{
	$Record = new PhpSqlRow;
	$Record->SetAsArray($a);
	
	$gblScheme[$Record->GetValue("name")] = $Record->GetValue("value");
}


// ======================================================================

// Check core escapes
function CheckCoreEscapes( &$tpCurrent )
{

	global $gblSiteName;
	global $gblDemo;
	global $gblScheme;
	global $pagetitle;
	global $gblSiteNameDisplay;
	global $gblContactEmail;
	global $gblPayPalEmail;

	if( substr($tpCurrent->GetEscapeName(), 0, strlen("CORE-REQUEST")) == "CORE-REQUEST" )
		echo request(substr($tpCurrent->GetEscapeName(), strlen("CORE-REQUEST") + 1));
	else if( substr($tpCurrent->GetEscapeName(), 0, strlen("CORE-IF-VALUE-EXISTS")) == "CORE-IF-VALUE-EXISTS" )
	{
		if( request(substr($tpCurrent->GetEscapeName(), strlen("CORE-IF-VALUE-EXISTS") + 1)) != "" )
			$tpCurrent->PushArgumentToStream();
	}
	else if( substr($tpCurrent->GetEscapeName(), 0, strlen("CORE-IF-VALUE-NOT-EXISTS")) == "CORE-IF-VALUE-NOT-EXISTS" )
	{
		if( request(substr($tpCurrent->GetEscapeName(), strlen("CORE-IF-VALUE-NOT-EXISTS") + 1)) == "" )
			$tpCurrent->PushArgumentToStream();
	}
	else if( substr($tpCurrent->GetEscapeName(), 0, strlen("CORE-IF-VALUE-IS")) == "CORE-IF-VALUE-IS" )
	{
		$NameExtension = split("\-", substr($tpCurrent->GetEscapeName(), strlen("CORE-IF-VALUE-IS") + 1));
		$RequestName = $NameExtension[0];
		$RequestValue = $NameExtension[1];

		if( strtoupper(request($RequestName)) == strtoupper($RequestValue) )
			$tpCurrent->PushArgumentToStream();
	}
	else if( substr($tpCurrent->GetEscapeName(), 0, strlen("CORE-IF-VALUE-NOT")) == "CORE-IF-VALUE-NOT" )
	{
		$NameExtension = split("\-", substr($tpCurrent->GetEscapeName(), strlen("CORE-IF-VALUE-NOT") + 1));
		$RequestName = $NameExtension[0];
		$RequestValue = $NameExtension[1];

		if( strtoupper(request($RequestName)) != strtoupper($RequestValue) )
			$tpCurrent->PushArgumentToStream();
	}
	else if( substr($tpCurrent->GetEscapeName(), 0, strlen("CORE-INCLUDE")) == "CORE-INCLUDE" )
	{
		include "includes/" . substr($tpCurrent->GetEscapeName(), strlen("CORE-INCLUDE") + 1) . ".php";
	}
	else if( $tpCurrent->IsEscape("CORE-SITE-NAME") )
		echo $gblSiteName;
	else if( $tpCurrent->IsEscape("CORE-PAYPAL-EMAIL") )
		echo $gblPayPalEmail;
	else if( $tpCurrent->IsEscape("CORE-CONTACT-EMAIL") )
		echo $gblContactEmail;
	else if( $tpCurrent->IsEscape("CORE-IF-DEMO") )
	{
		if( $gblDemo )
			$tpCurrent->PushArgumentToStream();
	}
	else if( $tpCurrent->IsEscape("CORE-IF-NOT-DEMO") )
	{
		if( !$gblDemo )
			$tpCurrent->PushArgumentToStream();
	}
	else if( $tpCurrent->IsEscape("CORE-SCHEME") )
	{
		echo $gblScheme[$tpCurrent->GetParameter()];
	}
	else if( $tpCurrent->IsEscape("CORE-SITE-NAME-DISPLAY") )
	{
		echo $gblSiteNameDisplay;
	}
	
}	

?>