templates.php 13.1 KB
Newer Older
Lukas Haemmerle's avatar
Lukas Haemmerle committed
1
<?php // Copyright (c) 2018, SWITCH
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(){

haemmer's avatar
haemmer committed
19
20
	global $langStrings, $language, $imageURL, $javascriptURL, $cssURL, $logoURL;
	global $useImprovedDropDownList, $disableRemoteLogos, $organizationLogoURL;
haemmer's avatar
haemmer committed
21
	global $federationURL, $organizationURL, $faqURL, $helpURL, $privacyURL;
haemmer's avatar
haemmer committed
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
	
	// 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(){
	
haemmer's avatar
haemmer committed
37
	global $selectedIDP, $language, $IDProviders, $SProviders, $redirectCookieName, $imageURL, $redirectStateCookieName, $showPermanentSetting;
haemmer's avatar
haemmer committed
38
39
40
41
	
	if (!isset($showPermanentSetting)){
		$showPermanentSetting = false;
	}
haemmer's avatar
haemmer committed
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
	
	$promptMessage =  getLocalString('make_selection');
	$serviceName = '';
	$entityID = '';
	
	// Check if entityID is available
	if (isset($_GET['entityID'])){
		$entityID = $_GET['entityID'];
	} else if (isset($_GET['providerId'])){
		$entityID = $_GET['providerId'];
	}
	
	// Set service name if entityID has a description
	if (!empty($entityID) && isset($SProviders[$entityID]) ){
		$SP = $SProviders[$entityID];
		$serviceName = $SP['Name'];
		if (isset($SP[$language]['Name'])){
			$serviceName = $SP[$language]['Name'];
		}
	}
	
63
64
65
66
67
68
	// Reset service name if it is the same as the entityID
	if ($serviceName == $entityID){
		$serviceName = '';
	}
	
	// Fallback to hostname of return URL if no service name was available
haemmer's avatar
haemmer committed
69
70
71
72
73
74
	if (empty($serviceName)){
		if (isset($_GET['return'])){
			$serviceName = getHostNameFromURI($_GET['return']);
		} else if (isset($_GET['shire'])){
			$serviceName = getHostNameFromURI($_GET['shire']);
		} else {
75
			$serviceName = $entityID;
haemmer's avatar
haemmer committed
76
77
		}
		$serviceName = '<span class="hostName">'.$serviceName.'</span>';
haemmer's avatar
haemmer committed
78
	} else {
haemmer's avatar
haemmer committed
79
		$serviceName = '<span class="serviceName">'.$serviceName.'</span>';
haemmer's avatar
haemmer committed
80
	}
haemmer's avatar
haemmer committed
81
82
83
	
	// Compose strings
	$promptMessage =  sprintf(getLocalString('access_host'), $serviceName);
haemmer's avatar
haemmer committed
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
	$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;
	
haemmer's avatar
haemmer committed
120
121
122
	$previouslyUsedIdPsHTML = getPreviouslyUsedIdPsHTML();
	echo $previouslyUsedIdPsHTML;
	
123
	
haemmer's avatar
haemmer committed
124
125
	$counter = 0;
	$optgroup = '';
126
	foreach ($IDProviders as $key => $values){
haemmer's avatar
haemmer committed
127
128
		
		// Get IdP Name
129
		$IdPName = (isset($values[$language]['Name'])) ? $values[$language]['Name'] : $IdPName = $values['Name'];
130
		
haemmer's avatar
haemmer committed
131
		// Figure out if entry is valid or a category
132
		if (!isset($values['SSO'])){
haemmer's avatar
haemmer committed
133
134
			
			// Check if entry is a category
135
			if (isset($values['Type']) && $values['Type'] == 'category'){
haemmer's avatar
haemmer committed
136
				if (!empty($optgroup)){
137
					echo "\n".'</optgroup>';
haemmer's avatar
haemmer committed
138
139
				}
				
haemmer's avatar
haemmer committed
140
141
142
143
				// Skip adding a new category if first category is 'unknown'
				// and it is the (probably) only category
				if ($key == 'unknown' && empty($optgroup) && $previouslyUsedIdPsHTML == ''){
					continue;
haemmer's avatar
haemmer committed
144
				}
haemmer's avatar
haemmer committed
145
146
147
148
				
				echo "\n".'<optgroup label="'.$IdPName.'">';
				$optgroup = $key;
				
haemmer's avatar
haemmer committed
149
150
151
152
			}
			continue;
		}
		
153
154
155
156
157
158
159
160
161
162
163
164
165
		echo "\n\t".printOptionElement($IDProviders, $key, $selectedIDP);
		
		$counter++;
	}
	
	// Add last optgroup if that was used
	if (!empty($optgroup)){
		echo "\n".'</optgroup>';
	}
}

/******************************************************************************/
// Prints option group of previously used organisations
haemmer's avatar
haemmer committed
166
function getPreviouslyUsedIdPsHTML(){
167
168
169
	global $IDProviders, $IDPArray, $selectedIDP, $showNumOfPreviouslyUsedIdPs;
	
	if (!isset($IDPArray) || count($IDPArray) < 1){
haemmer's avatar
haemmer committed
170
		return '';
171
172
173
174
	}
	
	$content = '';
	$counter = (isset($showNumOfPreviouslyUsedIdPs)) ? $showNumOfPreviouslyUsedIdPs : 3;
haemmer's avatar
haemmer committed
175
176
	
	for($n = count($IDPArray) - 1; $n >= 0; $n--){
177
178
179
		
		if ($counter <= 0){
			break;
haemmer's avatar
haemmer committed
180
		}
181
		
haemmer's avatar
haemmer committed
182
		$optionHTML = printOptionElement($IDProviders, $IDPArray[$n], $selectedIDP);
183
184
185
		
		if (empty($optionHTML)){
			continue;
haemmer's avatar
haemmer committed
186
187
		}
		
haemmer's avatar
haemmer committed
188
		$content .= "\t".$optionHTML."\n";
haemmer's avatar
haemmer committed
189
		
190
		$counter--;
haemmer's avatar
haemmer committed
191
192
	}
	
193
194
	// Return if no previously used IdPs exist
	if (empty($content)){
haemmer's avatar
haemmer committed
195
		return '';
haemmer's avatar
haemmer committed
196
	}
197
198
199
	
	// Print previously used IdPs
	$categoryName = getLocalString('last_used');
haemmer's avatar
haemmer committed
200
201
	$content = "\n".'<optgroup label="'.$categoryName.'">'."\n".$content;
	$content .= '</optgroup>';
202
	
haemmer's avatar
haemmer committed
203
	return $content;
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
}

/******************************************************************************/
// Print a single option element of the drop down list
function printOptionElement($IDProviders, $key, $selectedIDP){
	global $language;
	
	// Return if IdP does not exit
	if (!isset($IDProviders[$key])){
		return '';
	}
	
	// Get values
	$values = $IDProviders[$key];
	
	// Get IdP Name
	$IdPName = (isset($values[$language]['Name'])) ? $values[$language]['Name'] : $IdPName = $values['Name'];
	
	// Set selected attribute
	$selected = ($selectedIDP == $key) ? ' selected="selected"' : $selected = '';
	
	// Add additional information as data attribute to the entry
	$data = getDomainNameFromURI($key);
	$data .= composeOptionData($values);
	
	// Add logo (which is assumed to be 16x16px) to extension string
	$logo =  (isset($values['Logo'])) ? 'logo="'.$values['Logo']['URL']. '"' : '' ;
	
232
	return '<option value="'.$key.'"'.$selected.' data="'.htmlspecialchars($data).'" '.$logo.'>'.$IdPName.'</option>';
haemmer's avatar
haemmer committed
233
234
235
236
237
238
239
240
241
242
243
244
}

/******************************************************************************/
// 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
245
	$permanentUserIdP = '';
haemmer's avatar
haemmer committed
246
	$permanentUserIdPName = '';
haemmer's avatar
haemmer committed
247
248
249
	$permanentUserIdPLogo = '';
	
	
haemmer's avatar
haemmer committed
250
251
	if (
			isset($_POST['user_idp']) 
252
			&& checkIDPAndShowErrors($_POST['user_idp'])
haemmer's avatar
haemmer committed
253
		){
haemmer's avatar
haemmer committed
254
		$permanentUserIdP = $_POST['user_idp'];
haemmer's avatar
haemmer committed
255
256
	} elseif (
			isset($_COOKIE[$redirectCookieName]) 
257
			&& checkIDPAndShowErrors($_COOKIE[$redirectCookieName])
haemmer's avatar
haemmer committed
258
		){
haemmer's avatar
haemmer committed
259
260
261
262
263
264
		$permanentUserIdP = $_COOKIE[$redirectCookieName];
	}
	
	if ($permanentUserIdP != ''){
		$hiddenUserIdPInput = '<input type="hidden" name="user_idp" value="'.$permanentUserIdP.'">';
		$permanentUserIdPName = $IDProviders[$permanentUserIdP]['Name'];
265
266
267
		if (isset($IDProviders[$permanentUserIdP]['Logo']['URL'])){
			$permanentUserIdPLogo = $IDProviders[$permanentUserIdP]['Logo']['URL'];
		}
haemmer's avatar
haemmer committed
268
269
270
271
272
273
274
275
276
277
278
279
	}
	
	// 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
280
// Prints end of HTML page
haemmer's avatar
haemmer committed
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
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){
	
296
	global $langStrings, $language, $supportContactEmail;
haemmer's avatar
haemmer committed
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
	
	// 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
314
// Prints the JavaScript that renders the Embedded WAYF
haemmer's avatar
haemmer committed
315
316
function printEmbeddedWAYFScript(){

haemmer's avatar
haemmer committed
317
	global $langStrings, $language, $imageURL, $javascriptURL, $cssURL, $logoURL, $smallLogoURL, $federationURL;
haemmer's avatar
haemmer committed
318
	global $selectedIDP, $IDProviders, $SAMLDomainCookieName, $redirectCookieName, $redirectStateCookieName, $federationName;
haemmer's avatar
haemmer committed
319
	
320
	// Set values that are used in the java script
haemmer's avatar
haemmer committed
321
322
323
324
325
	$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();
326
	$checkedBool = (isset($_COOKIE[$redirectStateCookieName]) && !empty($_COOKIE[$redirectStateCookieName])) ? 'checked="checked"' : '' ;
haemmer's avatar
haemmer committed
327
328
329
330
	$rememberSelectionText = addslashes(getLocalString('remember_selection'));
	$loginString = addslashes(getLocalString('login'));
	$selectIdPString = addslashes(getLocalString('select_idp'));
	$otherFederationString = addslashes(getLocalString('other_federation'));
331
	$mostUsedIdPsString = addslashes(getLocalString('most_used'));
332
333
	$lastUsedIdPsString = addslashes(getLocalString('last_used'));
	$redirectCookie = (isset($_COOKIE[$redirectCookieName]) && !empty($_COOKIE[$redirectCookieName])) ?  $_COOKIE[$redirectCookieName] : '';
haemmer's avatar
haemmer committed
334
	
335
336
	// Generate list of Identity Providers
	$JSONIdPArray = array();
haemmer's avatar
haemmer committed
337
	$JSONCategoryArray = array();
338
	foreach ($IDProviders as $key => $IDProvider){
339
340
		
		// Get IdP Name
341
342
		if (isset($IDProvider[$language]['Name'])){
			$IdPName = addslashes($IDProvider[$language]['Name']);
343
		} else {
344
			$IdPName = addslashes($IDProvider['Name']);
345
346
347
		}
		
		// Set selected attribute
haemmer's avatar
haemmer committed
348
		$selected = ($selectedIDP == $key) ? ' selected:"true",' : '' ;
349
350
		$IdPType = isset($IDProviders[$key]['Type']) ? $IDProviders[$key]['Type'] : '';
		
351
352
353
354
355
356
357
		// SSO
		if (isset($IDProvider['SSO'])){
			$IdPSSO = $IDProvider['SSO'];
		} else {
			$IdPSSO = '';
		}
		
haemmer's avatar
haemmer committed
358
		// Logo URL
haemmer's avatar
haemmer committed
359
360
		if (isset($IDProvider['Logo']['URL'])){
			$IdPLogoURL = $IDProvider['Logo']['URL'];
haemmer's avatar
haemmer committed
361
362
363
364
365
366
367
		} else {
			$IdPLogoURL = '';
		}
		
		// Add other information to find IdP
		$IdPData = getDomainNameFromURI($key);
		$IdPData .= composeOptionData($IDProvider);
haemmer's avatar
haemmer committed
368
		$IdPData = addslashes( $IdPData);
haemmer's avatar
haemmer committed
369
		
370
		// Skip non-IdP entries
haemmer's avatar
haemmer committed
371
		if ($IdPType == ''){
372
373
374
			continue;
		}
		
haemmer's avatar
haemmer committed
375
376
377
		// Fill category and IdP buckets
		if ($IdPType == 'category'){
			$JSONCategoryArray[] = <<<ENTRY
378

haemmer's avatar
haemmer committed
379
380
"{$key}":{
	type:"{$IdPType}",
haemmer's avatar
haemmer committed
381
	name:"{$IdPName}"
haemmer's avatar
haemmer committed
382
383
384
385
386
387
388
389
390
391
392
393
}

ENTRY;
		} else {
			$JSONIdPArray[] = <<<ENTRY

"{$key}":{ {$selected}
	type:"{$IdPType}",
	name:"{$IdPName}",
	logoURL:"{$IdPLogoURL}",
	data:"{$IdPData}"
}
394
ENTRY;
haemmer's avatar
haemmer committed
395
		}
396
397
	}
	$JSONIdPList = join(',', $JSONIdPArray);
haemmer's avatar
haemmer committed
398
	$JSONCategoryList = join(',', $JSONCategoryArray);
399
	
400
	// Locales for javascript
haemmer's avatar
haemmer committed
401
402
403
404
	$searchText = getLocalString('search_idp', 'js');
	$noIdPFoundText =  getLocalString('no_idp_found', 'js');
	$noIdPAvailableText = getLocalString('no_idp_available', 'js');
	
405
406
	// Process script
	require_once('js/embeddedWAYF.js');
haemmer's avatar
haemmer committed
407
408
409
}

/******************************************************************************/
haemmer's avatar
haemmer committed
410
// Print sample configuration script used for Embedded WAYF
haemmer's avatar
haemmer committed
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
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');
	}
}

435
436
/******************************************************************************/
// Print sample configuration script used for Embedded WAYF
haemmer's avatar
haemmer committed
437
function printCSS($file){
438
439
440
	
	global $imageURL;
	
haemmer's avatar
haemmer committed
441
442
443
444
445
	if ($file != 'ImprovedDropDown.css'){
		$file= 'styles.css';
	}
	
	$defaultCSSFile =  'css/default-'.$file;
446
	$cssContent = file_get_contents($defaultCSSFile);
447

448
	// Read custom CSS if available
haemmer's avatar
haemmer committed
449
450
	if (file_exists('css/custom-'.$file)){
		$customCSSFile =  'css/custom-'.$file;
451
452
453
454
455
456
457
		$cssContent .= file_get_contents($customCSSFile);
	}
	
	// Read CSS and substitute content
	$cssContent = preg_replace('/{?\$imageURL}?/',$imageURL, $cssContent);
	
	echo $cssContent;
458
}
haemmer's avatar
haemmer committed
459
?>