templates.php 13.3 KB
Newer Older
Lukas Haemmerle's avatar
Lukas Haemmerle committed
1
<?php // Copyright (c) 2018, SWITCH
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;
22
	global $customStrings;
haemmer's avatar
haemmer committed
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
	
	// 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
38
	global $selectedIDP, $language, $IDProviders, $SProviders, $redirectCookieName, $imageURL, $redirectStateCookieName, $showPermanentSetting;
39
	global $customStrings;
haemmer's avatar
haemmer committed
40 41 42 43
	
	if (!isset($showPermanentSetting)){
		$showPermanentSetting = false;
	}
haemmer's avatar
haemmer committed
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
	
	$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'];
		}
	}
	
65 66 67 68 69 70
	// 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
71 72 73 74 75 76
	if (empty($serviceName)){
		if (isset($_GET['return'])){
			$serviceName = getHostNameFromURI($_GET['return']);
		} else if (isset($_GET['shire'])){
			$serviceName = getHostNameFromURI($_GET['shire']);
		} else {
77
			$serviceName = $entityID;
haemmer's avatar
haemmer committed
78 79
		}
		$serviceName = '<span class="hostName">'.$serviceName.'</span>';
haemmer's avatar
haemmer committed
80
	} else {
haemmer's avatar
haemmer committed
81
		$serviceName = '<span class="serviceName">'.$serviceName.'</span>';
haemmer's avatar
haemmer committed
82
	}
haemmer's avatar
haemmer committed
83 84 85
	
	// Compose strings
	$promptMessage =  sprintf(getLocalString('access_host'), $serviceName);
haemmer's avatar
haemmer committed
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
	$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;
104
	global $customStrings;
haemmer's avatar
haemmer committed
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
	
	$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
123 124 125
	$previouslyUsedIdPsHTML = getPreviouslyUsedIdPsHTML();
	echo $previouslyUsedIdPsHTML;
	
126
	
haemmer's avatar
haemmer committed
127 128
	$counter = 0;
	$optgroup = '';
129
	foreach ($IDProviders as $key => $values){
haemmer's avatar
haemmer committed
130 131
		
		// Get IdP Name
132
		$IdPName = (isset($values[$language]['Name'])) ? $values[$language]['Name'] : $IdPName = $values['Name'];
133
		
haemmer's avatar
haemmer committed
134
		// Figure out if entry is valid or a category
135
		if (!isset($values['SSO'])){
haemmer's avatar
haemmer committed
136 137
			
			// Check if entry is a category
138
			if (isset($values['Type']) && $values['Type'] == 'category'){
haemmer's avatar
haemmer committed
139
				if (!empty($optgroup)){
140
					echo "\n".'</optgroup>';
haemmer's avatar
haemmer committed
141 142
				}
				
haemmer's avatar
haemmer committed
143 144 145 146
				// 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
147
				}
haemmer's avatar
haemmer committed
148 149 150 151
				
				echo "\n".'<optgroup label="'.$IdPName.'">';
				$optgroup = $key;
				
haemmer's avatar
haemmer committed
152 153 154 155
			}
			continue;
		}
		
156 157 158 159 160 161 162 163 164 165 166 167 168
		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
169
function getPreviouslyUsedIdPsHTML(){
170 171 172
	global $IDProviders, $IDPArray, $selectedIDP, $showNumOfPreviouslyUsedIdPs;
	
	if (!isset($IDPArray) || count($IDPArray) < 1){
haemmer's avatar
haemmer committed
173
		return '';
174 175 176 177
	}
	
	$content = '';
	$counter = (isset($showNumOfPreviouslyUsedIdPs)) ? $showNumOfPreviouslyUsedIdPs : 3;
haemmer's avatar
haemmer committed
178 179
	
	for($n = count($IDPArray) - 1; $n >= 0; $n--){
180 181 182
		
		if ($counter <= 0){
			break;
haemmer's avatar
haemmer committed
183
		}
184
		
haemmer's avatar
haemmer committed
185
		$optionHTML = printOptionElement($IDProviders, $IDPArray[$n], $selectedIDP);
186 187 188
		
		if (empty($optionHTML)){
			continue;
haemmer's avatar
haemmer committed
189 190
		}
		
haemmer's avatar
haemmer committed
191
		$content .= "\t".$optionHTML."\n";
haemmer's avatar
haemmer committed
192
		
193
		$counter--;
haemmer's avatar
haemmer committed
194 195
	}
	
196 197
	// Return if no previously used IdPs exist
	if (empty($content)){
haemmer's avatar
haemmer committed
198
		return '';
haemmer's avatar
haemmer committed
199
	}
200 201 202
	
	// Print previously used IdPs
	$categoryName = getLocalString('last_used');
haemmer's avatar
haemmer committed
203 204
	$content = "\n".'<optgroup label="'.$categoryName.'">'."\n".$content;
	$content .= '</optgroup>';
205
	
haemmer's avatar
haemmer committed
206
	return $content;
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 232 233 234
}

/******************************************************************************/
// 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']. '"' : '' ;
	
235
	return '<option value="'.$key.'"'.$selected.' data="'.htmlspecialchars($data).'" '.$logo.'>'.$IdPName.'</option>';
haemmer's avatar
haemmer committed
236 237 238 239 240 241 242 243
}

/******************************************************************************/
// Prints the notice that tells the users their permanent IDP with an option
// to clear the permanent cookie.
function printNotice(){
	
	global $redirectCookieName, $IDProviders;
244
	global $customStrings;
haemmer's avatar
haemmer committed
245 246 247 248
	
	$actionURL = $_SERVER['SCRIPT_NAME'].'?'.htmlentities($_SERVER['QUERY_STRING']);
	
	$hiddenUserIdPInput = '';
haemmer's avatar
haemmer committed
249
	$permanentUserIdP = '';
haemmer's avatar
haemmer committed
250
	$permanentUserIdPName = '';
haemmer's avatar
haemmer committed
251 252 253
	$permanentUserIdPLogo = '';
	
	
haemmer's avatar
haemmer committed
254 255
	if (
			isset($_POST['user_idp']) 
256
			&& checkIDPAndShowErrors($_POST['user_idp'])
haemmer's avatar
haemmer committed
257
		){
haemmer's avatar
haemmer committed
258
		$permanentUserIdP = $_POST['user_idp'];
haemmer's avatar
haemmer committed
259 260
	} elseif (
			isset($_COOKIE[$redirectCookieName]) 
261
			&& checkIDPAndShowErrors($_COOKIE[$redirectCookieName])
haemmer's avatar
haemmer committed
262
		){
haemmer's avatar
haemmer committed
263 264 265 266 267 268
		$permanentUserIdP = $_COOKIE[$redirectCookieName];
	}
	
	if ($permanentUserIdP != ''){
		$hiddenUserIdPInput = '<input type="hidden" name="user_idp" value="'.$permanentUserIdP.'">';
		$permanentUserIdPName = $IDProviders[$permanentUserIdP]['Name'];
269 270 271
		if (isset($IDProviders[$permanentUserIdP]['Logo']['URL'])){
			$permanentUserIdPLogo = $IDProviders[$permanentUserIdP]['Logo']['URL'];
		}
haemmer's avatar
haemmer committed
272 273 274 275 276 277 278 279 280 281 282 283
	}
	
	// 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
284
// Prints end of HTML page
haemmer's avatar
haemmer committed
285 286
function printFooter(){
	
287 288
	global $customStrings;

haemmer's avatar
haemmer committed
289 290 291 292 293 294 295 296 297 298 299 300 301
	// 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){
	
302
	global $langStrings, $language, $supportContactEmail;
303
	global $customStrings;
haemmer's avatar
haemmer committed
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
	
	// 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
321
// Prints the JavaScript that renders the Embedded WAYF
haemmer's avatar
haemmer committed
322 323
function printEmbeddedWAYFScript(){

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

haemmer's avatar
haemmer committed
387 388
"{$key}":{
	type:"{$IdPType}",
389
	name:"{$IdPName}"
haemmer's avatar
haemmer committed
390 391 392 393 394 395 396 397 398 399 400 401
}

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

"{$key}":{ {$selected}
	type:"{$IdPType}",
	name:"{$IdPName}",
	logoURL:"{$IdPLogoURL}",
	data:"{$IdPData}"
}
402
ENTRY;
haemmer's avatar
haemmer committed
403
		}
404 405
	}
	$JSONIdPList = join(',', $JSONIdPArray);
haemmer's avatar
haemmer committed
406
	$JSONCategoryList = join(',', $JSONCategoryArray);
407
	
408
	// Locales for javascript
haemmer's avatar
haemmer committed
409 410 411 412
	$searchText = getLocalString('search_idp', 'js');
	$noIdPFoundText =  getLocalString('no_idp_found', 'js');
	$noIdPAvailableText = getLocalString('no_idp_available', 'js');
	
413 414
	// Process script
	require_once('js/embeddedWAYF.js');
haemmer's avatar
haemmer committed
415 416 417
}

/******************************************************************************/
haemmer's avatar
haemmer committed
418
// Print sample configuration script used for Embedded WAYF
haemmer's avatar
haemmer committed
419 420
function printEmbeddedConfigurationScript(){
	global $IDProviders;
421
	global $customStrings;
haemmer's avatar
haemmer committed
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
	
	$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');
	}
}

444 445
/******************************************************************************/
// Print sample configuration script used for Embedded WAYF
haemmer's avatar
haemmer committed
446
function printCSS($file){
447 448 449
	
	global $imageURL;
	
haemmer's avatar
haemmer committed
450 451 452 453 454
	if ($file != 'ImprovedDropDown.css'){
		$file= 'styles.css';
	}
	
	$defaultCSSFile =  'css/default-'.$file;
455
	$cssContent = file_get_contents($defaultCSSFile);
456

457
	// Read custom CSS if available
haemmer's avatar
haemmer committed
458 459
	if (file_exists('css/custom-'.$file)){
		$customCSSFile =  'css/custom-'.$file;
460 461 462 463 464 465 466
		$cssContent .= file_get_contents($customCSSFile);
	}
	
	// Read CSS and substitute content
	$cssContent = preg_replace('/{?\$imageURL}?/',$imageURL, $cssContent);
	
	echo $cssContent;
467
}