templates.php 13.1 KB
Newer Older
Lukas Haemmerle's avatar
Lukas Haemmerle committed
1
<?php // Copyright (c) 2017, 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'];
haemmer's avatar
haemmer committed
265
		$permanentUserIdPLogo = $IDProviders[$permanentUserIdP]['Logo']['URL'];
haemmer's avatar
haemmer committed
266
267
268
269
270
271
272
273
274
275
276
277
	}
	
	// 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
278
// Prints end of HTML page
haemmer's avatar
haemmer committed
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
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){
	
294
	global $langStrings, $language, $supportContactEmail;
haemmer's avatar
haemmer committed
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
	
	// 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
312
// Prints the JavaScript that renders the Embedded WAYF
haemmer's avatar
haemmer committed
313
314
function printEmbeddedWAYFScript(){

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

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

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

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

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

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

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