<?php
/*******************************************************************
Script: SSO-prod.php

        $Revision$
        $Id$


Doel:   SSO bewerkstelligen voor ASP providers - deel 1 van 2
        Voor Lingewaard - PRODUCTIE
Auteur: Peter Koerhuis (originele ASP-versie door Thorwald Beeris)
/*******************************************************************/
/* declare *********************************************************/
$strGUID; $strCTID; $strUserName; $strKey ;
$strEncryptedCode = ''; $strControlEncryptedCode = '';
/* variables *******************************************************/
$strASPUrl = "https://ling.facilitor.nl/cust/LING/sso.asp";   // HTTPS URL adres van ASP
$strSharedKey = "8122393053"; //Sharedkey - Should be the same at target side
/*******************************************************************/
?>

<HTML>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
function fnSubmit() {
  window.document.form.submit();
  return;
}
</SCRIPT>
</HEAD>
<BODY LANGUAGE="javascript" onload="return fnSubmit()">
Een moment aub.
<form action='<?php echo $strASPUrl; ?>' method="post" name="form" ID="Form1">
   <input type="hidden" name="returnurl" value="<?php echo 'http://'.$_SERVER["HTTP_HOST"].$_SERVER["SCRIPT_NAME"]; ?>" ID="Hidden1">
<?php
/* request action = requestid ***************************************/
$strGUID   = isset($_REQUEST["guid"])   ? $_REQUEST["guid"] : "";
$strCTID   = isset($_REQUEST["ctid"])   ? $_REQUEST["ctid"] : "";
$strJumpTo = isset($_REQUEST["jumpTo"]) ? $_REQUEST["jumpTo"] : "";

if ($strGUID == "" || $strCTID == "") {
	/* first flow: requestid ****************************************/
?>
	<input type="hidden" name="action" value="requestid" ID="Hidden2">
	<input type="hidden" name="jumpTo" value="<?php echo $strJumpTo; ?>" ID="jumpTo">
<?php
} else {
	/* second flow: create code *************************************/

   // TODO: Plak hier de code die de username achterhaalt (met of zonder domein)
   //       NB: Dit script wordt weliswaar twee keer aangeroepen, maar dat gebeurt
   //           beide keren vanaf de PC van de klant, dus beide keren moet op 
   //           basis van IP de user te achterhalen zijn.
   // Om de SSO te testen zet ik hier de user hard op APAAN.
   $strUserName = 'LINGEWAARD\APAAN';

	// Strip domain name
	while ( strpos($strUserName, "\\") > 0 ) {
	    $strUserName = substr( $strUserName, strpos($strUserName, "\\") + 1 );
	}
	/* crypt ********************************************************/
	//First coding phase
	$strKey = substr( $strSharedKey.$strGUID, 0, strlen($strUserName) );
	$strEncryptedCode = EnCrypt($strUserName);
	//Second coding phase
	$strKey = substr( $strGUID, 0, strlen($strEncryptedCode) );
	$strEncryptedCode = EnCrypt($strEncryptedCode);
	/* crypt Controlkey *********************************************/
	//First coding phase
	$strKey = substr( $strSharedKey.$strCTID, 0, strlen($strCTID) );
	$strControlEncryptedCode = EnCrypt("http://".$_SERVER["HTTP_HOST"].$_SERVER["SCRIPT_NAME"] );
	//Second coding phase
	$strKey = substr( $strCTID, 0, strlen($strControlEncryptedCode) );
	$strControlEncryptedCode = EnCrypt($strControlEncryptedCode);
?>
	<input type="hidden" name="action" value="processcode" ID="Hidden3">
	<input type="hidden" name="code" value="<?php echo ConvertToAsc($strEncryptedCode); ?>" ID="Hidden4">
	<input type="hidden" name="ctcode" value="<?php echo ConvertToAsc($strControlEncryptedCode); ?>" ID="Hidden5">
	<input type="hidden" name="ltcode" value="<?php echo strlen($strUserName); ?>" ID="Hidden6">
<?php
}
/* Functions ********************************************************/
function EnCrypt($strCryptThis) {
   global $strKey;
   $strChar; $iKeyChar; $iStringChar; $i;
   $strEncrypted = "";
   
   for ($i=0; $i < strlen($strCryptThis); $i++ ) {
      $iKeyChar = ord( substr( $strKey, $i, 1) );
      $iStringChar = ord( substr( $strCryptThis, $i, 1) );
      $iCryptChar = $iKeyChar ^ $iStringChar;
      $strEncrypted .= chr($iCryptChar);
   }
   return $strEncrypted;
}
function ConvertToAsc($strAsc) {
	$iCount;	$strTemp;
	$res = "";
	for ($iCount = 0; $iCount < strlen($strAsc); $iCount++ ) {
		$strTemp = ord( substr( $strAsc, $iCount, 1) );
		$res .= strlen($strTemp).$strTemp;
	}
	
	return $res;
}
/*********************************************************************/
?>
</form>
</BODY>
</HTML>