Upgrade to new Gitlab Version 13.9 on Saturday 19th April 20:00. Expect an interruption of about 30 to 60 minutes

templates.php 36 KB
Newer Older
haemmer's avatar
haemmer committed
1
<?php // Copyright (c) 2012, SWITCH - Serving Swiss Universities
haemmer's avatar
haemmer committed
2

haemmer's avatar
haemmer committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
******************************************************************************
This file contains the some functions that render HTML code.
******************************************************************************
*/

if(!isset($_SERVER['REMOTE_ADDR']) || basename($_SERVER['SCRIPT_NAME']) == 'templates.php'){
	exit('No direct script access allowed');
}

/*------------------------------------------------*/
// Functions containing HTML code
/*------------------------------------------------*/

function printHeader(){

19
	global $langStrings, $language, $imageURL, $javascriptURL, $cssURL, $logoURL, $useImprovedDropDownList;
haemmer's avatar
haemmer committed
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
	
	// Check if custom header template exists
	if(file_exists('custom-header.php')){
		include('custom-header.php');
	} else {
		// Use default code
		include('default-header.php');
	}
}


/******************************************************************************/
// Presents the user the drop-down list with available IDPs
function printWAYF(){
	
	global $selectedIDP, $language, $IDProviders, $redirectCookieName, $imageURL, $redirectStateCookieName, $showPermanentSetting;
	
	if (!isset($showPermanentSetting)){
		$showPermanentSetting = false;
	}
	$promptMessage =  '<strong>'.getLocalString('make_selection').'</strong>';
41
	if (isset($_GET['return'])){
haemmer's avatar
haemmer committed
42
		$promptMessage =  sprintf(getLocalString('access_host'), getHostNameFromURI($_GET['return']));
haemmer's avatar
haemmer committed
43 44 45 46
	} else if (isset($_GET['entityID'])){
		$promptMessage =  sprintf(getLocalString('access_host'), getHostNameFromURI($_GET['entityID']));
	} else if (isset($_GET['shire'])){
		$promptMessage =  sprintf(getLocalString('access_host'), getHostNameFromURI($_GET['shire']));
haemmer's avatar
haemmer committed
47 48
	} else {
		$promptMessage =  sprintf(getLocalString('access_host'), 'unknown');
haemmer's avatar
haemmer committed
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
	}
	$actionURL = $_SERVER['SCRIPT_NAME'].'?'.htmlentities($_SERVER['QUERY_STRING']);
	$defaultSelected = ($selectedIDP == '-') ? 'selected="selected"' : '';
	$rememberSelectionChecked = (isset($_COOKIE[$redirectStateCookieName])) ? 'checked="checked"' : '' ;
	
	
	// Check if custom header template exists
	if(file_exists('custom-body.php')){
		include('custom-body.php');
	} else {
		// Use default code
		include('default-body.php');
	}
}

/******************************************************************************/
// Presents the user a form to set a permanent cookie for their default IDP
function printSettings(){
	
	global $selectedIDP, $language, $IDProviders, $redirectCookieName;
	
	$actionURL = $_SERVER['SCRIPT_NAME'].'?'.htmlentities($_SERVER['QUERY_STRING']);
	$defaultSelected = ($selectedIDP == '-') ? 'selected="selected"' : '';
	
	// Check if custom header template exists
	if(file_exists('custom-settings.php')){
		include('custom-settings.php');
	} else {
		// Use default code
		include('default-settings.php');
	} 
}

/******************************************************************************/
// Prints the HTML drop down list including categories etc
function printDropDownList($IDProviders, $selectedIDP = ''){
	global $language;
	
	$counter = 0;
	$optgroup = '';
89
	foreach ($IDProviders as $key => $values){
haemmer's avatar
haemmer committed
90 91
		
		// Get IdP Name
92 93
		if (isset($values[$language]['Name'])){
			$IdPName = $values[$language]['Name'];
haemmer's avatar
haemmer committed
94
		} else {
95
			$IdPName = $values['Name'];
haemmer's avatar
haemmer committed
96 97
		}
		
98
		// Add additional information as title to the entry
99 100
		$title = getDomainNameFromURI($key);
		$title .= composeOptionTitle($values);
101
		
haemmer's avatar
haemmer committed
102
		// Figure out if entry is valid or a category
103
		if (!isset($values['SSO'])){
haemmer's avatar
haemmer committed
104 105
			
			// Check if entry is a category
106
			if (isset($values['Type']) && $values['Type'] == 'category'){
haemmer's avatar
haemmer committed
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
				if (!empty($optgroup)){
					echo '
	</optgroup>';
				}
				
				// Add another category
				echo '
	<optgroup label="'.$IdPName.'">';
				$optgroup = $key;
			}
			
			continue;
		}
		
		// Set selected attribute
		if ($selectedIDP == $key){
			$selected = ' selected="selected"';
		} else {
			$selected = '';
		}
127
		
haemmer's avatar
haemmer committed
128
		echo '
129
		<option title="'.$title.'" value="'.$key.'"'.$selected.'>'.$IdPName.'</option>';
haemmer's avatar
haemmer committed
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
		
		$counter++;
	}
	
	// Add last optgroup if that was used
	if (!empty($optgroup)){
		echo '
	</optgroup>';
	}
}

/******************************************************************************/
// Prints the notice that tells the users their permanent IDP with an option
// to clear the permanent cookie.
function printNotice(){
	
	global $redirectCookieName, $IDProviders;
	
	$actionURL = $_SERVER['SCRIPT_NAME'].'?'.htmlentities($_SERVER['QUERY_STRING']);
	
	$hiddenUserIdPInput = '';
haemmer's avatar
haemmer committed
151
	$permanentUserIdPName = '';
haemmer's avatar
haemmer committed
152 153
	if (
			isset($_POST['user_idp']) 
154
			&& checkIDPAndShowErrors($_POST['user_idp'])
haemmer's avatar
haemmer committed
155 156
		){
		$hiddenUserIdPInput = '<input type="hidden" name="user_idp" value="'.$_POST['user_idp'].'">';
haemmer's avatar
haemmer committed
157
		$permanentUserIdPName = $IDProviders[$_POST['user_idp']]['Name'];
haemmer's avatar
haemmer committed
158 159
	} elseif (
			isset($_COOKIE[$redirectCookieName]) 
160
			&& checkIDPAndShowErrors($_COOKIE[$redirectCookieName])
haemmer's avatar
haemmer committed
161 162
		){
		$hiddenUserIdPInput = '<input type="hidden" name="user_idp" value="'.$_COOKIE[$redirectCookieName].'">';
haemmer's avatar
haemmer committed
163
		$permanentUserIdPName = $IDProviders[$_COOKIE[$redirectCookieName]]['Name'];
haemmer's avatar
haemmer committed
164 165 166 167 168 169 170 171 172 173 174 175
	}
	
	// Check if footer template exists
	if(file_exists('custom-notice.php')){
		include('custom-notice.php');
	} else {
		// Use default code
		include('default-notice.php');
	}
}

/******************************************************************************/
haemmer's avatar
haemmer committed
176
// Prints end of HTML page
haemmer's avatar
haemmer committed
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
function printFooter(){
	
	// Check if footer template exists
	if(file_exists('custom-footer.php')){
		include('custom-footer.php');
	} else {
		// Use default code
		include('default-footer.php');
	}
}

/******************************************************************************/
// Prints an error message
function printError($message){
	
	global $langStrings, $language;
	
	// Show Header
	printHeader();
	
	// Check if error template exists
	if(file_exists('custom-error.php')){
		include('custom-error.php');
	} else {
		// Use default code
		include('default-error.php');
	}
	
	// Show footer
	printFooter();
}

/******************************************************************************/
haemmer's avatar
haemmer committed
210
// Prints the JavaScript that renders the Embedded WAYF
haemmer's avatar
haemmer committed
211 212 213 214 215 216 217 218 219 220 221
function printEmbeddedWAYFScript(){

	global $langStrings, $language, $imageURL, $logoURL, $smallLogoURL, $federationURL;
	global $selectedIDP, $IDProviders, $redirectCookieName, $redirectStateCookieName, $federationName;
	
	// Get some values that are used in the script
	$loginWithString = getLocalString('login_with');
	$makeSelectionString = getLocalString('make_selection', 'js');
	$loggedInString =  getLocalString('logged_in');
	$configurationScriptUrl = preg_replace('/embedded-wayf.js/', 'embedded-wayf.js/snippet.html', 'https://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']);
	$utcTime = time();
222
	$checkedBool = (isset($_COOKIE[$redirectStateCookieName]) && !empty($_COOKIE[$redirectStateCookieName])) ? 'checked="checked"' : '' ;
haemmer's avatar
haemmer committed
223 224 225 226
	$rememberSelectionText = addslashes(getLocalString('remember_selection'));
	$loginString = addslashes(getLocalString('login'));
	$selectIdPString = addslashes(getLocalString('select_idp'));
	$otherFederationString = addslashes(getLocalString('other_federation'));
227
	$mostUsedIdPsString = addslashes(getLocalString('most_used'));
haemmer's avatar
haemmer committed
228
	
229 230
	// Generate list of Identity Providers
	$JSONIdPArray = array();
231
	foreach ($IDProviders as $key => $IDProvider){
232 233
		
		// Get IdP Name
234 235
		if (isset($IDProvider[$language]['Name'])){
			$IdPName = addslashes($IDProvider[$language]['Name']);
236
		} else {
237
			$IdPName = addslashes($IDProvider['Name']);
238 239 240 241 242 243
		}
		
		// Set selected attribute
		$selected = ($selectedIDP == $key) ? ' selected="selected"' : '' ;
		$IdPType = isset($IDProviders[$key]['Type']) ? $IDProviders[$key]['Type'] : '';
		
244 245 246 247 248 249 250
		// SSO
		if (isset($IDProvider['SSO'])){
			$IdPSSO = $IDProvider['SSO'];
		} else {
			$IdPSSO = '';
		}
		
251 252 253 254 255 256 257 258 259 260
		// Skip non-IdP entries
		if ($IdPType == '' || $IdPType == 'category'){
			continue;
		}
		
		$JSONIdPArray[] = <<<ENTRY

	"{$key}":{
		type:"{$IdPType}",
		name:"{$IdPName}",
261
		SAML1SSOurl:"{$IdPSSO}"
262 263 264 265 266
		}
ENTRY;
	}
	$JSONIdPList = join(',', $JSONIdPArray);
	
haemmer's avatar
haemmer committed
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
	echo <<<SCRIPT

// To use this JavaScript, please access:
// {$configurationScriptUrl}
// and copy/paste the resulting HTML snippet to an unprotected web page that 
// you want the embedded WAYF to be displayed

// ############################################################################

// Declare all variables
var wayf_sp_entityID;
var wayf_URL;
var wayf_return_url;
var wayf_sp_handlerURL;

var wayf_use_discovery_service;
var wayf_use_small_logo;
var wayf_width;
var wayf_height;
var wayf_background_color;
var wayf_border_color;
var wayf_font_color;
var wayf_font_size;
var wayf_hide_logo;
var wayf_auto_login;
var wayf_logged_in_messsage;
var wayf_hide_after_login;
294
var wayf_most_used_idps;
haemmer's avatar
haemmer committed
295 296 297 298 299
var wayf_show_categories;
var wayf_hide_categories;
var wayf_hide_idps;
var wayf_unhide_idps;
var wayf_show_remember_checkbox;
haemmer's avatar
haemmer committed
300
var wayf_force_remember_for_session;
haemmer's avatar
haemmer committed
301 302 303
var wayf_additional_idps;
var wayf_sp_samlDSURL;
var wayf_sp_samlACURL;
haemmer's avatar
haemmer committed
304 305
var wayf_use_disco_feed;
var wayf_discofeed_url;
haemmer's avatar
haemmer committed
306
var wayf_html = "";
307
var wayf_idps = { {$JSONIdPList} };
haemmer's avatar
haemmer committed
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331

// Define functions
function submitForm(){
	
	if (document.IdPList.user_idp && document.IdPList.user_idp.selectedIndex == 0){
		alert('{$makeSelectionString}');
		return false;
	}
	
	// User chose non-federation IdP
	if (
		wayf_additional_idps.length > 0 
		&& document.IdPList.user_idp
		&& document.IdPList.user_idp.selectedIndex >= (document.IdPList.user_idp.options.length - wayf_additional_idps.length)){
		
		var NonFedEntityID = wayf_additional_idps[document.IdPList.user_idp[document.IdPList.user_idp.selectedIndex].value].entityID;
		var redirect_url;
		
		// Store SAML domain cookie for this foreign IdP
		setCookie('_saml_idp', encodeBase64(NonFedEntityID) , 100);
		
		// Redirect user to SP handler
		if (wayf_use_discovery_service){
			
haemmer's avatar
haemmer committed
332 333 334 335 336
			var entityIDGETParam = getGETArgument("entityID");
			var returnGETParam = getGETArgument("return");
			if (entityIDGETParam != "" && returnGETParam != ""){
				redirect_url = returnGETParam;
			} else {
haemmer's avatar
haemmer committed
337 338
				redirect_url = wayf_sp_samlDSURL;
				redirect_url += getGETArgumentSeparator(redirect_url) + 'target=' + encodeURIComponent(wayf_return_url);
haemmer's avatar
haemmer committed
339 340 341 342 343 344
			}
			
			// Append selected Identity Provider
			redirect_url += '&entityID=' + encodeURIComponent(NonFedEntityID);
			
			// Make sure the redirect always is being executed in parent window
haemmer's avatar
haemmer committed
345 346 347 348 349 350 351 352
			if (window.parent){
				window.parent.location = redirect_url;
			} else {
				window.location = redirect_url;
			}
			
		} else {
			redirect_url = wayf_sp_handlerURL + '?providerId=' 
haemmer's avatar
haemmer committed
353
			+ encodeURIComponent(NonFedEntityID)
haemmer's avatar
haemmer committed
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
			+ '&target=' + encodeURIComponent(wayf_return_url);
			
			// Make sure the redirect always is being done in parent window
			if (window.parent){
				window.parent.location = redirect_url;
			} else {
				window.location = redirect_url;
			}
			
		}
		
		// If input type button is used for submit, we must return false
		return false;
	} else {
		// User chose federation IdP entry
		document.IdPList.submit();
	}
	
	return false;
}

function writeHTML(a){
	wayf_html += a;
}

function isAllowedType(IdP, type){
	for ( var i=0; i<= wayf_hide_categories.length; i++){
		
		if (wayf_hide_categories[i] == type || wayf_hide_categories[i] == "all" ){
			
			for ( var i=0; i <= wayf_unhide_idps.length; i++){
				// Show IdP if it has to be unhidden
				if (wayf_unhide_idps[i] == IdP){
					return true;
				}
			}
			// If IdP is not unhidden, the default applies
			return false;
		}
	}
	
	// Category was not hidden
	return true;
}

function isAllowedCategory(category){
	
	if (!category || category == ''){
		return true;
	}
	
	for ( var i=0; i<= wayf_hide_categories.length; i++){
		
		if (wayf_hide_categories[i] == category || wayf_hide_categories[i] == "all" ){
			return false;
		}
	}
	
	// Category was not hidden
	return true;
}

function isAllowedIdP(IdP){
	
418
	for ( var i=0; i <= wayf_hide_idps.length; i++){
haemmer's avatar
haemmer committed
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
		if (wayf_hide_idps[i] == IdP){
			return false;
		}
	}
	// IdP was not hidden
	return true;
}

function setCookie(c_name, value, expiredays){
	var exdate = new Date();
	exdate.setDate(exdate.getDate() + expiredays);
	document.cookie=c_name + "=" + escape(value) +
	((expiredays==null) ? "" : "; expires=" + exdate.toGMTString());
}

function getCookie(check_name){
	// First we split the cookie up into name/value pairs
	// Note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	
haemmer's avatar
haemmer committed
442
	for ( var i = 0; i < a_all_cookies.length; i++ ){
haemmer's avatar
haemmer committed
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );
		
		
		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
	
		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			// We need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	
	return null;
}

// Checks if there exists a cookie containing check_name in its name
function isCookie(check_name){
	// First we split the cookie up into name/value pairs
	// Note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	
	for ( var i = 0; i < a_all_cookies.length; i++ ){
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );
		
		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
		
		// if the extracted name matches passed check_name
		
		if ( cookie_name.search(check_name) >= 0){
			return true;
		}
	}
	
	// Shibboleth session cookie has not been found
	return false;
}

495
// Query Shibboleth Session handler and process response afterwards
496 497
// This method has to be used because HttpOnly prevents reading 
// the shib session cookies via JavaScript
498
function isShibbolethSession(url){
499 500 501 502 503 504 505 506
	var xmlhttp;
	if (window.XMLHttpRequest){
		xmlhttp = new XMLHttpRequest();
	}  else {
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	// Send request
507 508 509 510 511 512 513
	try {
		xmlhttp.open("GET", url, false);
		xmlhttp.send();
	} catch (e) {
		// Something went wrong, send back false
		return false;
	} 
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
	
	// Check response code
	if (xmlhttp.readyState != 4 || xmlhttp.status != 200 ){
		return false;
	}
	
	// Return true if session handler shows valid session
	if (
		xmlhttp.responseText.search(/Authentication Time/i) > 0){
		return true;
	}
	
	return false;
}

haemmer's avatar
haemmer committed
529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
// Loads Identity Provider from DiscoFeed and adds them to additional IdPs
function loadDiscoFeedIdPs(){
	var xmlhttp;
	
	if (window.XMLHttpRequest){
		xmlhttp = new XMLHttpRequest();
	}  else {
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	// Send request
	try {
		xmlhttp.open("GET", wayf_discofeed_url, false);
		xmlhttp.send();
	} catch (e) {
		// Something went wrong, send back false
		return;
	} 
	
	// Check response code
	if (xmlhttp.readyState != 4 || xmlhttp.status != 200 ){
		return;
	}
	
	// Load JSON
	var IdPs = eval("(" + xmlhttp.responseText + ")");
	
556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
	return IdPs;

} 

// Adds unknown IdPs to wayf_additional_idps and hides IdPs that are not
// contained in the Discovery Feed
function processDiscoFeedIdPs(IdPs){
	
	if (typeof(IdPs) == "undefined"){
		return;
	}
	
	// Hide IdPs that are not in the Discovery Feed
	for (var entityID in wayf_idps){
		var foundIdP = false;
		for ( var i = 0; i < IdPs.length; i++) {
			if (IdPs[i].entityID == entityID){
				foundIdP = true;
			}
		}
		
		if (foundIdP == false){
			wayf_hide_idps.push(entityID);
		}
	}
	
	
	// Add unkown IdPs to wayf_additional_idps
haemmer's avatar
haemmer committed
584 585 586 587 588 589
	for ( var i = 0; i < IdPs.length; i++) {
		// Skip IdPs that are in same federation
		if (wayf_idps[IdPs[i].entityID]){
			continue;
		}
		
590 591 592 593 594
		// Skip hidden IdPs
		if (!isAllowedIdP(IdPs[i].entityID)){
			continue;
		}
		
haemmer's avatar
haemmer committed
595 596 597 598 599 600 601 602 603 604
		var newIdP;
		if (IdPs[i].DisplayNames){
			newIdP = {"name": IdPs[i].DisplayNames[0].value, "entityID":IdPs[i].entityID, "SAML1SSOurl":"https://www.example.org/test"};
		} else {
			newIdP = {"name":IdPs[i].entityID, "entityID":IdPs[i].entityID, "SAML1SSOurl":"https://www.example.org/test"};
		}
		wayf_additional_idps.push(newIdP);
	}
}

605
// Sorts Discovery feed entries 
606 607 608 609 610 611 612 613 614 615 616 617 618 619
function sortEntities(a, b){
	var nameA = a.name.toLowerCase();
	var nameB = b.name.toLowerCase();
	
	if (nameA < nameB){
		return -1;
	}
	
	if (nameA > nameB){
		return 1;
	}
	
	return 0;
}
haemmer's avatar
haemmer committed
620

621 622 623 624 625 626 627 628 629 630 631
// Returns true if user is logged in
function isUserLoggedIn(){
	
	if (
		   typeof(wayf_check_login_state_function) != "undefined"
		&& typeof(wayf_check_login_state_function) == "function" ){
		
		// Use custom function
		return wayf_check_login_state_function();
	
	} else {
haemmer's avatar
haemmer committed
632 633
		// Check Shibboleth session handler
		return isShibbolethSession(wayf_sp_handlerURL + '/Session');
634 635 636
	}
}

haemmer's avatar
haemmer committed
637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695
function encodeBase64(input) {
	var base64chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
	var output = "", c1, c2, c3, e1, e2, e3, e4;
	
	for ( var i = 0; i < input.length; ) {
		c1 = input.charCodeAt(i++);
		c2 = input.charCodeAt(i++);
		c3 = input.charCodeAt(i++);
		e1 = c1 >> 2;
		e2 = ((c1 & 3) << 4) + (c2 >> 4);
		e3 = ((c2 & 15) << 2) + (c3 >> 6);
		e4 = c3 & 63;
		if (isNaN(c2)){
			e3 = e4 = 64;
		} else if (isNaN(c3)){
			e4 = 64;
		}
		output += base64chars.charAt(e1) + base64chars.charAt(e2) + base64chars.charAt(e3) + base64chars.charAt(e4);
	}
	
	return output;
}

function decodeBase64(input) {
	var base64chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
	var output = "", chr1, chr2, chr3, enc1, enc2, enc3, enc4;
	var i = 0;

	// Remove all characters that are not A-Z, a-z, 0-9, +, /, or =
	var base64test = /[^A-Za-z0-9\+\/\=]/g;
	input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
	
	do {
		enc1 = base64chars.indexOf(input.charAt(i++));
		enc2 = base64chars.indexOf(input.charAt(i++));
		enc3 = base64chars.indexOf(input.charAt(i++));
		enc4 = base64chars.indexOf(input.charAt(i++));

		chr1 = (enc1 << 2) | (enc2 >> 4);
		chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
		chr3 = ((enc3 & 3) << 6) | enc4;

		output = output + String.fromCharCode(chr1);

		if (enc3 != 64) {
			output = output + String.fromCharCode(chr2);
		}
		if (enc4 != 64) {
			output = output + String.fromCharCode(chr3);
		}
		
		chr1 = chr2 = chr3 = "";
		enc1 = enc2 = enc3 = enc4 = "";
		
	} while (i < input.length);
	
	return output;
}

haemmer's avatar
haemmer committed
696 697 698 699 700 701 702 703 704 705 706 707 708
function getGETArgument(name){
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexString = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp(regexString);
	var results = regex.exec(window.location.href);
	
	if( results == null ){
		return "";
	} else {
		return decodeURIComponent(results[1]);
	}
}

haemmer's avatar
haemmer committed
709 710 711 712 713 714 715 716
function getGETArgumentSeparator(url){
	if (url.indexOf('?') >=0 ){
		return '&';
	} else {
		return '?';
	}
}

haemmer's avatar
haemmer committed
717 718 719 720
(function() {
	
	var config_ok = true; 
	
haemmer's avatar
haemmer committed
721 722 723 724
	// Get GET parameters that maybe are set by Shibboleth
	var returnGETParam = getGETArgument("return");
	var entityIDGETParam = getGETArgument("entityID");
	
haemmer's avatar
haemmer committed
725 726 727 728 729 730 731 732
	// First lets make sure properties are available
	if(
		typeof(wayf_use_discovery_service)  == "undefined"  
		|| typeof(wayf_use_discovery_service) != "boolean"
	){
		wayf_use_discovery_service = true;
	}
	
haemmer's avatar
haemmer committed
733 734 735 736 737 738
	// Overwrite entityID with GET argument if present
	var entityIDGETParam = getGETArgument("entityID");
	if (entityIDGETParam != ""){
		wayf_sp_entityID = entityIDGETParam;
	}
	
haemmer's avatar
haemmer committed
739 740 741 742
	if(
		typeof(wayf_sp_entityID) == "undefined"
		|| typeof(wayf_sp_entityID) != "string"
		){
haemmer's avatar
haemmer committed
743 744 745 746
		alert('The mandatory parameter \'wayf_sp_entityID\' is missing. Please add it as a javascript variable on this page.');
		config_ok = false;
	}
	
haemmer's avatar
haemmer committed
747 748 749 750
	if(
		typeof(wayf_URL) == "undefined"
		|| typeof(wayf_URL) != "string"
		){
haemmer's avatar
haemmer committed
751 752 753 754
		alert('The mandatory parameter \'wayf_URL\' is missing. Please add it as a javascript variable on this page.');
		config_ok = false;
	}
	
haemmer's avatar
haemmer committed
755 756 757 758
	if(
		typeof(wayf_return_url) == "undefined"
		|| typeof(wayf_return_url) != "string"
		){
haemmer's avatar
haemmer committed
759 760 761 762
		alert('The mandatory parameter \'wayf_return_url\' is missing. Please add it as a javascript variable on this page.');
		config_ok = false;
	}
	
haemmer's avatar
haemmer committed
763 764 765 766
	if(
		wayf_use_discovery_service == false 
		&& typeof(wayf_sp_handlerURL) == "undefined"
		){
haemmer's avatar
haemmer committed
767 768 769 770
		alert('The mandatory parameter \'wayf_sp_handlerURL\' is missing. Please add it as a javascript variable on this page.');
		config_ok = false;
	}
	
haemmer's avatar
haemmer committed
771 772 773 774
	if(
		wayf_use_discovery_service == true 
		&& typeof(wayf_sp_samlDSURL) == "undefined"
		){
haemmer's avatar
haemmer committed
775
		// Set to default DS handler
776
		wayf_sp_samlDSURL = wayf_sp_handlerURL + "/Login";
haemmer's avatar
haemmer committed
777 778
	}
	
haemmer's avatar
haemmer committed
779 780 781 782
	if (
		typeof(wayf_sp_samlACURL) == "undefined"
		|| typeof(wayf_sp_samlACURL) != "string"
		){
haemmer's avatar
haemmer committed
783 784 785
		wayf_sp_samlACURL = wayf_sp_handlerURL + '/SAML/POST';
	}
	
haemmer's avatar
haemmer committed
786 787 788 789
	if(
		typeof(wayf_font_color) == "undefined"
		|| typeof(wayf_font_color) != "string"
		){
haemmer's avatar
haemmer committed
790 791 792 793 794 795 796 797 798 799
		wayf_font_color = 'black';
	}
	
	if(
		typeof(wayf_font_size) == "undefined"
		|| typeof(wayf_font_size) != "number"
		){
		wayf_font_size = 12;
	}
	
haemmer's avatar
haemmer committed
800 801 802 803
	if(
		typeof(wayf_border_color) == "undefined"
		|| typeof(wayf_border_color) != "string"
		){
804
		wayf_border_color = '#848484';
haemmer's avatar
haemmer committed
805 806
	}
	
haemmer's avatar
haemmer committed
807 808 809 810
	if(
		typeof(wayf_background_color) == "undefined"
		|| typeof(wayf_background_color) != "string"
		){
811
		wayf_background_color = '#F0F0F0';
haemmer's avatar
haemmer committed
812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827
	}
	
	if(
		typeof(wayf_use_small_logo) == "undefined" 
		|| typeof(wayf_use_small_logo) != "boolean"
		){
		wayf_use_small_logo = false;
	}
	
	if(
		typeof(wayf_hide_logo) == "undefined" 
		|| typeof(wayf_use_small_logo) != "boolean"
		){
		wayf_hide_logo = false;
	}
	
haemmer's avatar
haemmer committed
828 829 830 831
	if(
		typeof(wayf_width) == "undefined" 
		|| typeof(wayf_width) != "number"
	){
haemmer's avatar
haemmer committed
832
		wayf_width = "auto";
833
	} else {
haemmer's avatar
haemmer committed
834 835 836
		wayf_width += 'px';
	}
	
haemmer's avatar
haemmer committed
837 838 839 840
	if(
		typeof(wayf_height) == "undefined" 
		|| typeof(wayf_height) != "number"
		){
haemmer's avatar
haemmer committed
841
		wayf_height = "auto";
842
	} else {
haemmer's avatar
haemmer committed
843 844 845 846 847 848 849 850 851 852
		wayf_height += "px";
	}
	
	if(
		typeof(wayf_show_remember_checkbox) == "undefined"
		|| typeof(wayf_show_remember_checkbox) != "boolean"
		){
		wayf_show_remember_checkbox = true;
	}
	
haemmer's avatar
haemmer committed
853 854 855 856 857 858 859
	if(
		typeof(wayf_force_remember_for_session) == "undefined"
		|| typeof(wayf_force_remember_for_session) != "boolean"
		){
		wayf_force_remember_for_session = false;
	}
	
haemmer's avatar
haemmer committed
860 861 862 863 864 865 866 867 868 869 870 871 872 873
	if(
		typeof(wayf_auto_login) == "undefined"
		|| typeof(wayf_auto_login) != "boolean"
		){
		wayf_auto_login = true;
	}
	
	if(
		typeof(wayf_hide_after_login) == "undefined"
		|| typeof(wayf_hide_after_login) != "boolean"
		){
		wayf_hide_after_login = true;
	}
	
haemmer's avatar
haemmer committed
874 875 876 877
	if(
		typeof(wayf_logged_in_messsage) == "undefined"
		|| typeof(wayf_logged_in_messsage) != "string"
		){
haemmer's avatar
haemmer committed
878
		wayf_logged_in_messsage = "{$loggedInString}".replace(/%s/, wayf_return_url);
haemmer's avatar
haemmer committed
879 880 881
	}
	
	if(
882 883
		typeof(wayf_most_used_idps) == "undefined"
		|| typeof(wayf_most_used_idps) != "object"
haemmer's avatar
haemmer committed
884
		){
885
		wayf_most_used_idps = new Array();
haemmer's avatar
haemmer committed
886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927
	}
	
	if(
		typeof(wayf_show_categories) == "undefined"
		|| typeof(wayf_show_categories) != "boolean"
		){
		wayf_show_categories = true;
	}
	
	if(
		typeof(wayf_hide_categories) == "undefined"
		|| typeof(wayf_hide_categories) != "object"
		){
		wayf_hide_categories = new Array();
	}
	
	if(
		typeof(wayf_unhide_idps) == "undefined"
		||  typeof(wayf_unhide_idps) != "object"
	){
		wayf_unhide_idps = new Array();
	}
	
	// Disable categories if IdPs are unhidden from hidden categories
	if (wayf_unhide_idps.length > 0){
		wayf_show_categories = false;
	}
	
	if(
		typeof(wayf_hide_idps) == "undefined"
		|| typeof(wayf_hide_idps) != "object"
		){
		wayf_hide_idps = new Array();
	}
	
	if(
		typeof(wayf_additional_idps) == "undefined"
		|| typeof(wayf_additional_idps) != "object"
		){
		wayf_additional_idps = [];
	}
	
haemmer's avatar
haemmer committed
928 929 930 931 932 933 934 935 936 937 938 939 940 941
	if(
		typeof(wayf_use_disco_feed) == "undefined"
		|| typeof(wayf_use_disco_feed) != "boolean"
		){
		wayf_use_disco_feed = false;
	}
	
	if(
		typeof(wayf_discofeed_url) == "undefined"
		|| typeof(wayf_discofeed_url) != "string"
		){
		wayf_discofeed_url = "/Shibboleth.sso/DiscoFeed";
	}
	
haemmer's avatar
haemmer committed
942 943 944 945 946 947
	// Exit without outputting html if config is not ok
	if (config_ok != true){
		return;
	}
	
	// Check if user is logged in already:
948
	var user_logged_in = isUserLoggedIn();
haemmer's avatar
haemmer committed
949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966
	
	// Check if user is authenticated already and 
	// whether something has to be drawn
	if (
		wayf_hide_after_login 
		&& user_logged_in 
		&& wayf_logged_in_messsage == ''
	){
		
		// Exit script without drawing
		return;
	}
	
	// Now start generating the HTML for outer box
	if(
		wayf_hide_after_login 
		&& user_logged_in
	){
967
		writeHTML('<div id="wayf_div" style="background:' + wayf_background_color + ';border-style: solid;border-color: ' + wayf_border_color + ';border-width: 1px;padding: 10px; height: auto;width: ' + wayf_width + ';text-align: left;overflow: hidden;">');
haemmer's avatar
haemmer committed
968
	} else {
969
		writeHTML('<div id="wayf_div" style="background:' + wayf_background_color + ';border-style: solid;border-color: ' + wayf_border_color + ';border-width: 1px;padding: 10px; height: ' + wayf_height + ';width: ' + wayf_width + ';text-align: left;overflow: hidden;">');
haemmer's avatar
haemmer committed
970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989
	}
	
	// Shall we display the logo
	if (wayf_hide_logo != true){
		
		// Write header of logo div
		writeHTML('<div id="wayf_logo_div" style="float: right;"><a href="$federationURL" target="_blank" style="border:0px">');
		
		// Which size of the logo shall we display
		if (wayf_use_small_logo){
			writeHTML('<img id="wayf_logo" src="{$smallLogoURL}" alt="Federation Logo" style="border:0px">')
		} else {
			writeHTML('<img id="wayf_logo" src="{$logoURL}" alt="Federation Logo" style="border:0px">')
		}
		
		// Write footer of logo div
		writeHTML('</a></div>');
	}
	
	// Start login check
990 991
	// Search for login state cookie
	// If one exists, we only draw the logged_in_message
haemmer's avatar
haemmer committed
992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
	if(
		wayf_hide_after_login 
		&& user_logged_in
	){
		writeHTML('<p id="wayf_intro_div" style="float:left;font-size:' + wayf_font_size + 'px;color:' + wayf_font_color + ';">' + wayf_logged_in_messsage + '</p>');
		
	} else {
	// Else draw embedded WAYF
		
		//Do we have to draw custom text? or any text at all?
		if(typeof(wayf_overwrite_intro_text) == "undefined"){
			writeHTML('<label for="user_idp" id="wayf_intro_label" style="float:left; min-width:80px; font-size:' + wayf_font_size + 'px;color:' + wayf_font_color + ';">{$loginWithString}</label>');
		} else if (wayf_overwrite_intro_text != "") {
			writeHTML('<label for="user_idp" id="wayf_intro_label" style="float:left; min-width:80px; font-size:' + wayf_font_size + 'px;color:' + wayf_font_color + ';">' + wayf_overwrite_intro_text + '</label>');
		}
		
		var wayf_authReq_URL = '';
		var form_start = '';
		
		if (wayf_use_discovery_service == true){
haemmer's avatar
haemmer committed
1012
			// New SAML Discovery Service protocol
haemmer's avatar
haemmer committed
1013
			
haemmer's avatar
haemmer committed
1014
			wayf_authReq_URL = wayf_URL;
haemmer's avatar
haemmer committed
1015
			
haemmer's avatar
haemmer committed
1016 1017 1018 1019 1020
			// Use GET arguments or use configuration parameters
			if (entityIDGETParam != "" && returnGETParam != ""){
				wayf_authReq_URL += '?entityID=' + encodeURIComponent(entityIDGETParam);
				wayf_authReq_URL += '&amp;return=' + encodeURIComponent(returnGETParam);
			} else {
haemmer's avatar
haemmer committed
1021 1022
				var return_url = wayf_sp_samlDSURL + getGETArgumentSeparator(wayf_sp_samlDSURL);
				return_url += 'SAMLDS=1&target=' + encodeURIComponent(wayf_return_url);
haemmer's avatar
haemmer committed
1023 1024 1025
				wayf_authReq_URL += '?entityID=' + encodeURIComponent(wayf_sp_entityID);
				wayf_authReq_URL += '&amp;return=' + encodeURIComponent(return_url);
			}
haemmer's avatar
haemmer committed
1026
		} else {
haemmer's avatar
haemmer committed
1027 1028 1029
			// Old Shibboleth WAYF protocol
			wayf_authReq_URL = wayf_URL;
			wayf_authReq_URL += '?providerId=' + encodeURIComponent(wayf_sp_entityID);
haemmer's avatar
haemmer committed
1030
			wayf_authReq_URL += '&amp;target=' + encodeURIComponent(wayf_return_url);
haemmer's avatar
haemmer committed
1031 1032
			wayf_authReq_URL += '&amp;shire=' + encodeURIComponent(wayf_sp_samlACURL);
			wayf_authReq_URL += '&amp;time={$utcTime}';
haemmer's avatar
haemmer committed
1033 1034
		}
		
haemmer's avatar
haemmer committed
1035 1036 1037
		// Add form element
		form_start = '<form id="IdPList" name="IdPList" method="post" target="_parent" action="' + wayf_authReq_URL + '">';
		
haemmer's avatar
haemmer committed
1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067
SCRIPT;
	
	// Create redirect links in case the checkbox is checked
	if (isset($_COOKIE[$redirectCookieName]) && !empty($_COOKIE[$redirectCookieName])){
		// Redirect user to WAYF automatically
		echo <<<SCRIPT
		
		// Do auto login in case this option is set
		if (wayf_auto_login){
		
			// Redirect user automatically to WAYF
			var redirect_url = wayf_authReq_URL.replace(/&amp;/g, '&');
			
			// Make sure the redirect is always being done in the parent window
			if (window.parent){
				window.parent.location = redirect_url;
			} else {
				window.location = redirect_url;
			}
			
			// Return here and stop writing HTML
			return;
		}
		
SCRIPT;
		
	}
	
	echo <<<SCRIPT
		
1068 1069 1070 1071 1072 1073 1074 1075
		// Load additional IdPs from DiscoFeed if feature is enabled
		if (wayf_use_disco_feed){
			discoFeedIdPs = loadDiscoFeedIdPs();
			
			// Hide IdPs for which SP doesnt have metadata and add unknown IdPs 
			processDiscoFeedIdPs(discoFeedIdPs);
		}
		
haemmer's avatar
haemmer committed
1076
		writeHTML(form_start);
haemmer's avatar
haemmer committed
1077
		writeHTML('<input name="request_type" type="hidden" value="embedded">');
haemmer's avatar
haemmer committed
1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095
		writeHTML('<select id="user_idp" name="user_idp" style="margin-top: 15px;margin-bottom: 10px; width: 100%;">');
		
		// Get local cookie
		var saml_idp = getCookie('_saml_idp');
		var last_idp = '';
		var last_idps = new Array();
		
		if (saml_idp && saml_idp.length > 0){
			last_idps = saml_idp.split('+')
			if (last_idps[0] && last_idps[0].length > 0){
				last_idp = decodeBase64(last_idps[0]);
			}
		}
		
		// Add first entry: "Select your IdP..."
		writeHTML('<option value="-">{$selectIdPString} ...</option>');
		
		// Favourites
1096
		if (wayf_most_used_idps.length > 0){
1097 1098 1099 1100 1101
			if(typeof(wayf_overwrite_most_used_idps_text) == "undefined"){
				writeHTML('<optgroup label="{$mostUsedIdPsString}">');
			} else {
				writeHTML('<optgroup label="' + wayf_overwrite_most_used_idps_text + '">');
			}
haemmer's avatar
haemmer committed
1102 1103
			
			// Show additional IdPs in the order they are defined
1104 1105 1106
			for ( var i=0; i < wayf_most_used_idps.length; i++){
				if (wayf_idps[wayf_most_used_idps[i]]){
					writeHTML('<option value="' + wayf_most_used_idps[i] + '">' + wayf_idps[wayf_most_used_idps[i]].name + '</option>');
haemmer's avatar
haemmer committed
1107 1108 1109 1110 1111 1112 1113 1114 1115 1116
				}
			}
			
			writeHTML('</optgroup>');
		}
		
SCRIPT;
	
	// Generate drop-down list
	$optgroup = '';
1117
	foreach ($IDProviders as $key => $IDProvider){
haemmer's avatar
haemmer committed
1118 1119
		
		// Get IdP Name
1120 1121
		if (isset($IDProvider[$language]['Name'])){
			$IdPName = addslashes($IDProvider[$language]['Name']);
haemmer's avatar
haemmer committed
1122
		} else {
1123
			$IdPName = addslashes($IDProvider['Name']);
haemmer's avatar
haemmer committed
1124 1125 1126
		}
		
		// Figure out if entry is valid or a category
1127
		if (!isset($IDProvider['SSO'])){
haemmer's avatar
haemmer committed
1128 1129
			
			// Check if entry is a category
1130
			if (isset($IDProvider['Type']) && $IDProvider['Type'] == 'category'){
haemmer's avatar
haemmer committed
1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191
			
				echo <<<SCRIPT

		if (isAllowedCategory('{$key}')){
SCRIPT;
			
				if (!empty($optgroup)){
					echo <<<SCRIPT

			if (wayf_show_categories == true){
				writeHTML('</optgroup>');
			}
SCRIPT;
				}
				
				// Add another category
				echo <<<SCRIPT

			if (wayf_show_categories == true){
				writeHTML('<optgroup label="{$IdPName}">');
			}
		}
SCRIPT;
				$optgroup = $key;
			}
			
			continue;
		}
		
		// Set selected attribute
		$selected = ($selectedIDP == $key) ? ' selected="selected"' : '' ;
		$IdPType = isset($IDProviders[$key]['Type']) ? $IDProviders[$key]['Type'] : '';
		
		echo <<<SCRIPT

		if (isAllowedType('{$key}','{$IdPType}') && isAllowedIdP('{$key}')){
			if (
				"{$selectedIDP}" == "-" 
				&& typeof(wayf_default_idp) != "undefined"
				&& wayf_default_idp == "{$key}"
				){
				writeHTML('<option value="{$key}" selected="selected">{$IdPName}</option>');
			} else {
				// Let central WAYF choose
				writeHTML('<option value="{$key}" {$selected}>{$IdPName}</option>');
			}
		}
SCRIPT;
	}
	
	// Add last optgroup if that was used
	if (!empty($optgroup)){
		echo <<<SCRIPT

		if (wayf_show_categories == true){
			writeHTML('</optgroup>');
		}
SCRIPT;
	}
	
	echo <<<SCRIPT
1192
		
haemmer's avatar
haemmer committed
1193 1194 1195 1196 1197 1198
		if (wayf_additional_idps.length > 0){
			
			if (wayf_show_categories == true){
				writeHTML('<optgroup label="{$otherFederationString}">');
			}
			
1199 1200 1201 1202
			// Sort Array
			wayf_additional_idps.sort(sortEntities)
			
			// Show additional IdPs
haemmer's avatar
haemmer committed
1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236
			for ( var i=0; i < wayf_additional_idps.length ; i++){
				if (wayf_additional_idps[i]){
					// Last used IdP is known because of local _saml_idp cookie
					if (
						wayf_additional_idps[i].name
						&& wayf_additional_idps[i].entityID == last_idp
						){
						writeHTML('<option value="' + i + '" selected="selected">' + wayf_additional_idps[i].name  + '</option>');
					} 
					// If no IdP is known but the default IdP matches, use this entry
					else if (
						wayf_additional_idps[i].name
						&& typeof(wayf_default_idp) != "undefined" 
						&& wayf_additional_idps[i].entityID == wayf_default_idp
						){
						writeHTML('<option value="' + i + '" selected="selected">' + wayf_additional_idps[i].name  + '</option>');
					} else if (wayf_additional_idps[i].name) {
						writeHTML('<option value="' + i + '">' + wayf_additional_idps[i].name  + '</option>');
					}
				}
			}
			
			if (wayf_show_categories == true){
				writeHTML('</optgroup>');
			}
		}
		
		writeHTML('</select>');
		
		// Draw checkbox
		writeHTML('<div id="wayf_remember_checkbox_div" style="float: left;margin-top: 0px;margin-bottom:10px;">');
		
		// Do we have to show the remember settings checkbox?
		if (wayf_show_remember_checkbox){
haemmer's avatar
haemmer committed
1237 1238 1239 1240 1241 1242 1243 1244 1245
			// Is the checkbox forced to be checked
			if (wayf_force_remember_for_session){
				// First draw the dummy checkbox ...
				writeHTML('<input id="wayf_remember_checkbox" type="checkbox" name="session_dummy" value="true" checked="checked" disabled="disabled" >&nbsp;');
				// ... and now the real but hidden checkbox
				writeHTML('<input type="hidden" name="session" value="true">&nbsp;');
			} else {
				writeHTML('<input id="wayf_remember_checkbox" type="checkbox" name="session" value="true" {$checkedBool}>&nbsp;');
			}
haemmer's avatar
haemmer committed
1246 1247 1248 1249 1250 1251 1252 1253
			
			// Do we have to display custom text?
			if(typeof(wayf_overwrite_checkbox_label_text) == "undefined"){
				writeHTML('<label for="wayf_remember_checkbox" id="wayf_remember_checkbox_label" style="min-width:80px; font-size:' + wayf_font_size + 'px;color:' + wayf_font_color + ';">{$rememberSelectionText}</label>');
				
			} else if (wayf_overwrite_checkbox_label_text != "")  {
				writeHTML('<label for="wayf_remember_checkbox" id="wayf_remember_checkbox_label" style="min-width:80px; font-size:' + wayf_font_size + 'px;color:' + wayf_font_color + ';">' + wayf_overwrite_checkbox_label_text + '</label>');
			}
haemmer's avatar
haemmer committed
1254 1255 1256
		} else if (wayf_force_remember_for_session){
			// Is the checkbox forced to be checked but hidden
			writeHTML('<input id="wayf_remember_checkbox" type="hidden" name="session" value="true">&nbsp;');
haemmer's avatar
haemmer committed
1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275
		}
		
		writeHTML('</div>');
		
		// Do we have to display custom text?
		if(typeof(wayf_overwrite_submit_button_text) == "undefined"){
			writeHTML('<input id="wayf_submit_button" type="submit" name="Login" accesskey="s" value="{$loginString}" style="float: right;" onClick="javascript:return submitForm();">');
		} else {
			writeHTML('<input id="wayf_submit_button" type="submit" name="Login" accesskey="s" value="' + wayf_overwrite_submit_button_text + '" style="float: right;" onClick="javascript:return submitForm();">');
		}
		
		// Close form
		writeHTML('</form>');
		
	}  // End login check
	
	// Close box
	writeHTML('</div>');
	
1276
	// Now output HTML all at once
haemmer's avatar
haemmer committed
1277 1278 1279 1280 1281 1282 1283
	document.write(wayf_html);
})()

SCRIPT;
}

/******************************************************************************/
haemmer's avatar
haemmer committed
1284
// Print sample configuration script used for Embedded WAYF
haemmer's avatar
haemmer committed
1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308
function printEmbeddedConfigurationScript(){
	global $IDProviders;
	
	$types = array();
	foreach ($IDProviders as $IDProvider){
		if (isset($IDProvider['Type']) && $IDProvider['Type'] != 'category'){
			$types[$IDProvider['Type']] = $IDProvider['Type'];
		}
	}
	
	$host = $_SERVER['SERVER_NAME'];
	$path = $_SERVER['SCRIPT_NAME'];
	$types = '"'.implode('","',$types).'"';
	
	header('Content-type: text/plain;charset="utf-8"');
	
	if(file_exists('custom-embedded-wayf.php')){
		include('custom-embedded-wayf.php');
	} else {
		// Use default code
		include('default-embedded-wayf.php');
	}
}

1309 1310 1311
/******************************************************************************/
// Print sample configuration script used for Embedded WAYF
function printCSS(){
1312 1313 1314 1315 1316
	
	global $imageURL;
	
	$defaultCSSFile =  'css/default-styles.css';
	$cssContent = file_get_contents($defaultCSSFile);
1317

1318 1319 1320 1321 1322 1323 1324 1325 1326 1327
	// Read custom CSS if available
	if (file_exists('css/custom-styles.css')){
		$customCSSFile =  'css/custom-styles.css';
		$cssContent .= file_get_contents($customCSSFile);
	}
	
	// Read CSS and substitute content
	$cssContent = preg_replace('/{?\$imageURL}?/',$imageURL, $cssContent);
	
	echo $cssContent;
1328
}
haemmer's avatar
haemmer committed
1329
?>