<?php
if (isset($_REQUEST['token'])) {
	$token = $_REQUEST['token'];
	$url = $_REQUEST['url'];
} else {
	$token = '';
	$url = '';
}

$f_do_autologin = false;

$user = isset($_REQUEST['user']) ? $_REQUEST['user'] : 'ssouser' ;
$name = isset($_REQUEST['name']) ? $_REQUEST['name'] : 'Single Sign-On user' ;
$groups = isset($_REQUEST['groups']) ? $_REQUEST['groups'] : 'ssogroup1|ssogroup2' ;
$email = isset($_REQUEST['email']) ? $_REQUEST['email'] : 'ssouser@mycompany.com' ;
$telephone = isset($_REQUEST['telephone']) ? $_REQUEST['telephone'] : '+358 123 2345' ;
$admin = isset($_REQUEST['admin']) ? $_REQUEST['admin'] : '0';

$secret = 'secret';

if ( @$_REQUEST['continue'] || $f_do_autologin )
{
	if ($f_do_autologin) {
		
	}
	// User's email is optional
	if (isset($_REQUEST['noemailchange'])) {
		$email = '';
	}
	else {
		// User's email can be erased by setting blank email address
		$email = isset($_REQUEST['email']) ? $_REQUEST['email'] : '' ;
	}
	// User's telephone is optional
	if (isset($_REQUEST['notelephonechange'])) {
		$email = '';
	}
	else {
		// User's telephone can be erased by setting blank telephone address
		$email = isset($_REQUEST['telephone']) ? $_REQUEST['telephone'] : '' ;
	}
	// Group is optional
	if (isset($_REQUEST['nogroupchange'])) {
		$groups = '';
	}

	$hash = sha1( $user . $name . $groups . $email . $telephone . $admin . $token . $secret );

	$url .= '?user=' . urlencode($user) . '&name=' . urlencode($name) . '&hash=' . $hash;

	if (!isset($_REQUEST['nogroupchange'])) {
		$url .= '&groups=' . urlencode($groups);
	}

	if (!isset($_REQUEST['noemailchange'])) {
		$url .= '&email=' . urlencode($email);
	}
	
	if (!isset($_REQUEST['notelephonechange'])) {
		$url .= '&telephone=' . urlencode($telephone);
	}
	
	
	$url .= '&admin=' . $admin;
	header('Location:' . $url);
	die;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<title>SSO example</title>
	<style type="text/css">
		body { background-color: #EEEEEE;}
		fieldset{border:none;}
		fieldset ul{margin 0;padding:0;}
		fieldset li{list-style:none;}
		fieldset li label{display:inline-block;vertical-align:top;width:100px;padding:5px 0px;}
		fieldset li label.right {width:auto;}
		fieldset li label.normal{display:static;width:inherit;}
		fieldset li div{display: inline-table;padding:5px 0px;}
		input.wide { width: 350px;}
		input { margin: 2px;}
	</style>
	</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF'];?>">
	<fieldset>
	<ul>
		<li><label class="right" style="font-weight:bold;">myDBR SSO login example</label></li>
		<li><label for="u">User: </label><input id="u" type="text" name="user" value="<?php echo $user ?>" />
			<input id="ad" type="checkbox" name="admin" value="1"  /><label class="right" for="ad">Admin</label></li>
		<li><label for="n">Name: </label><input id="n" type="text" name="name" value="<?php echo $name ?>" /></li>
		<li><label for="e">Email: </label><input id="e" type="text" name="email" value="<?php echo $email ?>" />
			<input id="ne" type="checkbox" name="noemailchange" value="No"  /><label class="right" for="ne">No changes to user's email</label></li>
		<li><label for="e">Telephone: </label><input id="e" type="text" name="telephone" value="<?php echo $telephone ?>" />
				<input id="ne" type="checkbox" name="notelephonechange" value="No"  /><label class="right" for="ne">No changes to user's telephone number</label></li>
		<li><label for="g">Group(s): </label><input id="g" type="text" name="groups" value="<?php echo $groups ?>" />
			<input id="ng" type="checkbox" name="nogroupchange" value="No"  /><label class="right" for="ng">No changes to user's groups</label></li>
		<li><label for="t">Token: </label><input id="t" class="wide" type="text" name="token" value="<?php echo $token ?>" /></li>
		<li><label for="url">Url: </label><input id="url" class="wide" type="text" name="url" value="<?php echo $url ?>" /></li>
		<li><input type="submit" name="continue"/></li>
	</ul>
	</fieldset>
</form>
</body>
</html>