templates.php 28.7 KB
Newer Older
haemmer's avatar
haemmer committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<?php
/*
******************************************************************************
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');
}

// Config validation
if(!isset($smallLogoURL) or empty($smallLogoURL)) $smallLogoURL = $logoURL;
if(!isset($federationName)) $federationName = '';
if(!isset($federationURL)) $federationURL = '';

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

function printHeader(){

	global $langStrings, $language, $imageURL, $logoURL;
	
	// 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>';
	if (isset($_GET['target']) && preg_match('|:/|', $_GET['target'])){
		$promptMessage = sprintf(getLocalString('access_target'), $_GET['target'], $_GET['target']);
	} 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']));
	}
	$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 = '';
	foreach ($IDProviders as $key => $value){
		
		// Get IdP Name
		if (isset($value[$language]['Name'])){
			$IdPName = $value[$language]['Name'];
		} else {
			$IdPName = $value['Name'];
		}
		
		// Figure out if entry is valid or a category
		if (!isset($value['SSO'])){
			
			// Check if entry is a category
			if (isset($value['Type']) && $value['Type'] == 'category'){
				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 = '';
		}
		echo '
		<option value="'.$key.'"'.$selected.'>'.$IdPName.'</option>';
		
		$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 = '';
	if (
			isset($_POST['user_idp']) 
			&& checkIDP($_POST['user_idp'])
		){
		$hiddenUserIdPInput = '<input type="hidden" name="user_idp" value="'.$_POST['user_idp'].'">';
	} elseif (
			isset($_COOKIE[$redirectCookieName]) 
			&& checkIDP($_COOKIE[$redirectCookieName])
		){
		$hiddenUserIdPInput = '<input type="hidden" name="user_idp" value="'.$_COOKIE[$redirectCookieName].'">';
	}
	
	$permanentUserIdPName = (isset($_COOKIE[$redirectCookieName])) ? 
			$IDProviders[$_COOKIE[$redirectCookieName]]['Name'] :
			$IDProviders[$_POST['user_idp']]['Name'];
	
	// Check if footer template exists
	if(file_exists('custom-notice.php')){
		include('custom-notice.php');
	} else {
		// Use default code
		include('default-notice.php');
	}
}

/******************************************************************************/

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();
}

/******************************************************************************/

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();
	$checkedBool = (isset($_COOKIE[$redirectCookieName]) && !empty($_COOKIE[$redirectCookieName])) ? 'checked="checked"' : '' ;
	$rememberSelectionText = addslashes(getLocalString('remember_selection'));
	$loginString = addslashes(getLocalString('login'));
	$selectIdPString = addslashes(getLocalString('select_idp'));
	$otherFederationString = addslashes(getLocalString('other_federation'));
226
	$mostUsedIdPsString = addslashes(getLocalString('most_used'));
haemmer's avatar
haemmer committed
227
	
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
	// Generate list of Identity Providers
	$JSONIdPArray = array();
	foreach ($IDProviders as $key => $value){
		
		// Get IdP Name
		if (isset($value[$language]['Name'])){
			$IdPName = addslashes($value[$language]['Name']);
		} else {
			$IdPName = addslashes($value['Name']);
		}
		
		// Set selected attribute
		$selected = ($selectedIDP == $key) ? ' selected="selected"' : '' ;
		$IdPType = isset($IDProviders[$key]['Type']) ? $IDProviders[$key]['Type'] : '';
		
		// Skip non-IdP entries
		if ($IdPType == '' || $IdPType == 'category'){
			continue;
		}
		
		$JSONIdPArray[] = <<<ENTRY

	"{$key}":{
		type:"{$IdPType}",
		name:"{$IdPName}",
		SAML1SSOurl:"{$value['SSO']}"
		}
ENTRY;
	}
	$JSONIdPList = join(',', $JSONIdPArray);
	
haemmer's avatar
haemmer committed
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
	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;
286
var wayf_most_used_idps;
haemmer's avatar
haemmer committed
287
288
289
290
291
292
293
294
295
var wayf_show_categories;
var wayf_hide_categories;
var wayf_hide_idps;
var wayf_unhide_idps;
var wayf_show_remember_checkbox;
var wayf_additional_idps;
var wayf_sp_samlDSURL;
var wayf_sp_samlACURL;
var wayf_html = "";
296
var wayf_idps = { {$JSONIdPList} };
haemmer's avatar
haemmer committed
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
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
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
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649

// Define functions
function submitForm(){
	
	if (document.IdPList.user_idp && document.IdPList.user_idp.selectedIndex == 0){
		alert('{$makeSelectionString}');
		return false;
	}
	
	// User chose non-federation IdP
	// TODO: FIX windows error
	// 4 >= (8 - 3/4)
	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){
			redirect_url = wayf_sp_samlDSURL + '?entityID=' 
			+ NonFedEntityID
			+ '&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;
			}
			
		} else {
			redirect_url = wayf_sp_handlerURL + '?providerId=' 
			+ NonFedEntityID
			+ '&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){
	
	for ( var i=0; i<=wayf_hide_idps.length; i++){
		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 = '';
	
	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 == 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;
}

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;
}

(function() {
	
	var config_ok = true; 
	
	// 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;
		config_ok = false;
	}
	
	if(typeof(wayf_sp_entityID) == "undefined"){
		alert('The mandatory parameter \'wayf_sp_entityID\' is missing. Please add it as a javascript variable on this page.');
		config_ok = false;
	}
	
	if(typeof(wayf_URL) == "undefined"){
		alert('The mandatory parameter \'wayf_URL\' is missing. Please add it as a javascript variable on this page.');
		config_ok = false;
	}
	
	if(typeof(wayf_return_url) == "undefined"){
		alert('The mandatory parameter \'wayf_return_url\' is missing. Please add it as a javascript variable on this page.');
		config_ok = false;
	}
	
	if(wayf_use_discovery_service == false && typeof(wayf_sp_handlerURL) == "undefined"){
		alert('The mandatory parameter \'wayf_sp_handlerURL\' is missing. Please add it as a javascript variable on this page.');
		config_ok = false;
	}
	
	if(wayf_use_discovery_service == true && typeof(wayf_sp_samlDSURL) == "undefined"){
		// Set to default DS handler
		wayf_sp_samlDSURL = wayf_sp_handlerURL + "/DS";
	}
	
	if (typeof(wayf_sp_samlACURL) == "undefined"){
		wayf_sp_samlACURL = wayf_sp_handlerURL + '/SAML/POST';
	}
	
	if(typeof(wayf_font_color) == "undefined"){
		wayf_font_color = 'black';
	}
	
	if(
		typeof(wayf_font_size) == "undefined"
		|| typeof(wayf_font_size) != "number"
		){
		wayf_font_size = 12;
	}
	
	if(typeof(wayf_border_color) == "undefined"){
		wayf_border_color = '#00247D';
	}
	
	if(typeof(wayf_background_color) == "undefined"){
		wayf_background_color = '#F4F7F7';
	}
	
	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;
	}
	
	if(typeof(wayf_width) == "undefined"){
		wayf_width = "auto";
	} else if (typeof(wayf_width) == "number"){
		wayf_width += 'px';
	}
	
	if(typeof(wayf_height) == "undefined"){
		wayf_height = "auto";
	} else if (typeof(wayf_height) == "number"){
		wayf_height += "px";
	}
	
	if(
		typeof(wayf_show_remember_checkbox) == "undefined"
		|| typeof(wayf_show_remember_checkbox) != "boolean"
		){
		wayf_show_remember_checkbox = true;
	}
	
	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;
	}
	
	if(typeof(wayf_logged_in_messsage) == "undefined"){
		wayf_logged_in_messsage = "{$loggedInString}";
	}
	
	if(
650
651
		typeof(wayf_most_used_idps) == "undefined"
		|| typeof(wayf_most_used_idps) != "object"
haemmer's avatar
haemmer committed
652
		){
653
		wayf_most_used_idps = new Array();
haemmer's avatar
haemmer committed
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
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
	}
	
	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 = [];
	}
	
	// Exit without outputting html if config is not ok
	if (config_ok != true){
		return;
	}
	
	// Check if user is logged in already:
	var user_logged_in = false;
	if (typeof(wayf_check_login_state_function) == "undefined"
		|| typeof(wayf_check_login_state_function) != "function" ){
		// Use default Shibboleth Service Provider login check
		user_logged_in = isCookie('shibsession');
	} else {
		// Use custom function
		user_logged_in = wayf_check_login_state_function();
	}
	
	// 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
	){
		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;">');
	} else {
		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;">');
	}
	
	// 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
	if(
		wayf_hide_after_login 
		&& user_logged_in
	){
		// Search for login state cookie
		// If one exists, we stop drawing embedded WAYF
		
		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){
			var return_url = wayf_sp_samlDSURL + '?SAMLDS=1&target=' + encodeURIComponent(wayf_return_url);
			
			wayf_authReq_URL = wayf_URL 
			+ '?entityID=' + encodeURIComponent(wayf_sp_entityID)
			+ '&amp;return=' + encodeURIComponent(return_url);
			
			form_start = '<form id="IdPList" name="IdPList" method="post" target="_parent" action="' + wayf_authReq_URL + '">';
		} else {
			
			wayf_authReq_URL = wayf_URL 
			+ '?providerId=' + encodeURIComponent(wayf_sp_entityID)
			+ '&amp;shire=' + encodeURIComponent(wayf_sp_samlACURL)
			+ '&amp;target=' + encodeURIComponent(wayf_return_url);
			
			form_start = '<form id="IdPList" name="IdPList" method="post" target="_parent" action="' + wayf_authReq_URL + '&amp;time={$utcTime}'
			+ '">';
		}
		
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
		
		writeHTML(form_start);
		writeHTML('<input name="request_type" type="hidden" value="embedded" />');
		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
844
845
		if (wayf_most_used_idps.length > 0){
			writeHTML('<optgroup label="{$mostUsedIdPsString}">');
haemmer's avatar
haemmer committed
846
847
			
			// Show additional IdPs in the order they are defined
848
849
850
			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
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
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
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
				}
			}
			
			writeHTML('</optgroup>');
		}
		
SCRIPT;
	
	// Generate drop-down list
	$optgroup = '';
	foreach ($IDProviders as $key => $value){
		
		// Get IdP Name
		if (isset($value[$language]['Name'])){
			$IdPName = addslashes($value[$language]['Name']);
		} else {
			$IdPName = addslashes($value['Name']);
		}
		
		// Figure out if entry is valid or a category
		if (!isset($value['SSO'])){
			
			// Check if entry is a category
			if (isset($value['Type']) && $value['Type'] == 'category'){
			
				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
		if (wayf_additional_idps.length > 0){
			
			if (wayf_show_categories == true){
				writeHTML('<optgroup label="{$otherFederationString}">');
			}
			
			// Show additional IdPs in the order they are defined
			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){
			writeHTML('<input id="wayf_remember_checkbox" type="checkbox" name="session" value="true" {$checkedBool}>&nbsp;');
			
			// 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>');
			}
		}
		
		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>');
	
	// Now output HTML
	document.write(wayf_html);
})()

SCRIPT;
}

/******************************************************************************/

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');
	}
}

?>