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

templates.php 14.3 KB
Newer Older
Lukas Haemmerle's avatar
Lukas Haemmerle committed
1
<?php // Copyright (c) 2019, SWITCH
haemmer's avatar
haemmer committed
2

haemmer's avatar
haemmer committed
3 4 5 6 7 8
/*
******************************************************************************
This file contains the some functions that render HTML code.
******************************************************************************
*/

9 10
if (!isset($_SERVER['REMOTE_ADDR']) || basename($_SERVER['SCRIPT_NAME']) == 'templates.php') {
    exit('No direct script access allowed');
haemmer's avatar
haemmer committed
11 12 13 14 15
}

/*------------------------------------------------*/
// Functions containing HTML code
/*------------------------------------------------*/
16 17 18 19 20 21 22
// $bodyType states which screen WAYF is going to display:
// * settings
// * notice
// * WAYF
// * error
// This allows to adapt the content of the header
function printHeader($bodyType)
23 24 25 26 27
{
    global $langStrings, $language, $imageURL, $javascriptURL, $cssURL, $logoURL;
    global $useImprovedDropDownList, $disableRemoteLogos, $organizationLogoURL;
    global $federationURL, $organizationURL, $faqURL, $helpURL, $privacyURL;
    global $customStrings;
28 29 30 31 32
    global $developmentMode, $apiURL;
    global $permanentUserIdP, $permanentUserIdPName, $permanentUserIdPLogo;
    global $IDProviders;

    $permanentUserIdP = getPermanentUserIdp();
haemmer's avatar
haemmer committed
33

34
    include(get_template('header.php'));
haemmer's avatar
haemmer committed
35 36 37 38 39
}


/******************************************************************************/
// Presents the user the drop-down list with available IDPs
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
function printWAYF()
{
    global $selectedIDP, $language, $IDProviders, $SProviders, $redirectCookieName, $imageURL, $redirectStateCookieName, $showPermanentSetting;
    global $customStrings;

    if (!isset($showPermanentSetting)) {
        $showPermanentSetting = false;
    }

    $promptMessage =  getLocalString('make_selection');
    $serviceName = '';
    $entityID = '';

    // Check if entityID is available
    if (isset($_GET['entityID'])) {
        $entityID = $_GET['entityID'];
    } elseif (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'];
        }
    }

    // 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
    if (empty($serviceName)) {
        if (isset($_GET['return'])) {
            $serviceName = getHostNameFromURI($_GET['return']);
        } elseif (isset($_GET['shire'])) {
            $serviceName = getHostNameFromURI($_GET['shire']);
        } else {
            $serviceName = $entityID;
        }
        $serviceName = '<span class="hostName">'.$serviceName.'</span>';
    } else {
        $serviceName = '<span class="serviceName">'.$serviceName.'</span>';
    }

    // Compose strings
    $promptMessage =  sprintf(getLocalString('access_host'), $serviceName);
    $actionURL = $_SERVER['SCRIPT_NAME'].'?'.htmlentities($_SERVER['QUERY_STRING']);
    $defaultSelected = ($selectedIDP == '-') ? 'selected="selected"' : '';
    $rememberSelectionChecked = (isset($_COOKIE[$redirectStateCookieName])) ? 'checked="checked"' : '' ;

    include(get_template('body.php'));
haemmer's avatar
haemmer committed
95 96 97 98
}

/******************************************************************************/
// Presents the user a form to set a permanent cookie for their default IDP
99 100 101 102 103 104 105 106 107
function printSettings()
{
    global $selectedIDP, $language, $IDProviders, $redirectCookieName;
    global $customStrings;

    $actionURL = $_SERVER['SCRIPT_NAME'].'?'.htmlentities($_SERVER['QUERY_STRING']);
    $defaultSelected = ($selectedIDP == '-') ? 'selected="selected"' : '';

    include(get_template('settings.php'));
haemmer's avatar
haemmer committed
108 109 110 111
}

/******************************************************************************/
// Prints the HTML drop down list including categories etc
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
function printDropDownList($IDProviders, $selectedIDP = '')
{
    global $language;

    $previouslyUsedIdPsHTML = getPreviouslyUsedIdPsHTML();
    echo $previouslyUsedIdPsHTML;


    $counter = 0;
    $optgroup = '';
    foreach ($IDProviders as $key => $values) {

        // Get IdP Name
        $IdPName = (isset($values[$language]['Name'])) ? $values[$language]['Name'] : $IdPName = $values['Name'];

        // Figure out if entry is valid or a category
        if (!isset($values['SSO'])) {

            // Check if entry is a category
            if (isset($values['Type']) && $values['Type'] == 'category') {
                if (!empty($optgroup)) {
                    echo "\n".'</optgroup>';
                }

                // Skip adding a new category if first category is 'unknown'
                // and it is the (probably) only category
                if ($key == 'unknown' && empty($optgroup) && $previouslyUsedIdPsHTML == '') {
                    continue;
                }

                echo "\n".'<optgroup label="'.$IdPName.'">';
                $optgroup = $key;
            }
            continue;
        }

        echo "\n\t".printOptionElement($IDProviders, $key, $selectedIDP);

        $counter++;
    }

    // Add last optgroup if that was used
    if (!empty($optgroup)) {
        echo "\n".'</optgroup>';
    }
157 158 159 160
}

/******************************************************************************/
// Prints option group of previously used organisations
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
function getPreviouslyUsedIdPsHTML()
{
    global $IDProviders, $IDPArray, $selectedIDP, $showNumOfPreviouslyUsedIdPs;

    if (!isset($IDPArray) || count($IDPArray) < 1) {
        return '';
    }

    $content = '';
    $counter = (isset($showNumOfPreviouslyUsedIdPs)) ? $showNumOfPreviouslyUsedIdPs : 3;

    for ($n = count($IDPArray) - 1; $n >= 0; $n--) {
        if ($counter <= 0) {
            break;
        }

        $optionHTML = printOptionElement($IDProviders, $IDPArray[$n], $selectedIDP);

        if (empty($optionHTML)) {
            continue;
        }

        $content .= "\t".$optionHTML."\n";

        $counter--;
    }

    // Return if no previously used IdPs exist
    if (empty($content)) {
        return '';
    }

    // Print previously used IdPs
    $categoryName = getLocalString('last_used');
    $content = "\n".'<optgroup label="'.$categoryName.'">'."\n".$content;
    $content .= '</optgroup>';

    return $content;
199 200 201 202
}

/******************************************************************************/
// Print a single option element of the drop down list
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
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
222
    $data = buildIdpData($values, $key);
223 224 225 226 227

    // Add logo (which is assumed to be 16x16px) to extension string
    $logo =  (isset($values['Logo'])) ? 'logo="'.$values['Logo']['URL']. '"' : '' ;

    return '<option value="'.$key.'"'.$selected.' data="'.htmlspecialchars($data).'" '.$logo.'>'.$IdPName.'</option>';
haemmer's avatar
haemmer committed
228 229 230 231 232
}

/******************************************************************************/
// Prints the notice that tells the users their permanent IDP with an option
// to clear the permanent cookie.
233 234 235 236 237 238 239 240
function printNotice()
{
    global $redirectCookieName, $IDProviders;
    global $customStrings;

    $actionURL = $_SERVER['SCRIPT_NAME'].'?'.htmlentities($_SERVER['QUERY_STRING']);

    $hiddenUserIdPInput = '';
241
    $permanentUserIdP = getPermanentUserIdp();
242 243 244 245 246 247 248 249 250 251 252 253
    $permanentUserIdPName = '';
    $permanentUserIdPLogo = '';

    if ($permanentUserIdP != '') {
        $hiddenUserIdPInput = '<input type="hidden" name="user_idp" value="'.$permanentUserIdP.'">';
        $permanentUserIdPName = $IDProviders[$permanentUserIdP]['Name'];
        if (isset($IDProviders[$permanentUserIdP]['Logo']['URL'])) {
            $permanentUserIdPLogo = $IDProviders[$permanentUserIdP]['Logo']['URL'];
        }
    }

    include(get_template('notice.php'));
haemmer's avatar
haemmer committed
254 255
}

256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
function getPermanentUserIdp()
{
    global $redirectCookieName;
    $permanentUserIdP = '';

    if (
          isset($_POST['user_idp'])
          && checkIDPAndShowErrors($_POST['user_idp'])
      ) {
        $permanentUserIdP = $_POST['user_idp'];
    } elseif (
          isset($_COOKIE[$redirectCookieName])
          && checkIDPAndShowErrors($_COOKIE[$redirectCookieName])
      ) {
        $permanentUserIdP = $_COOKIE[$redirectCookieName];
    }

    return $permanentUserIdP;
}

haemmer's avatar
haemmer committed
276
/******************************************************************************/
haemmer's avatar
haemmer committed
277
// Prints end of HTML page
278 279 280
function printFooter()
{
    include(get_template('footer.php'));
haemmer's avatar
haemmer committed
281 282 283 284
}

/******************************************************************************/
// Prints an error message
285 286 287 288 289 290
function printError($message)
{
    global $langStrings, $language, $supportContactEmail;
    global $customStrings;

    // Show Header
291
    printHeader("error");
292 293 294 295 296

    include(get_template('error.php'));

    // Show footer
    printFooter();
haemmer's avatar
haemmer committed
297 298 299
}

/******************************************************************************/
haemmer's avatar
haemmer committed
300
// Prints the JavaScript that renders the Embedded WAYF
301 302 303 304 305
function printEmbeddedWAYFScript()
{
    global $langStrings, $language, $imageURL, $javascriptURL, $cssURL, $logoURL, $smallLogoURL, $federationURL;
    global $selectedIDP, $IDProviders, $SAMLDomainCookieName, $redirectCookieName, $redirectStateCookieName;
    global $customStrings;
306
    global $apiURL, $developmentMode;
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

    // Set values that are used in the java 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[$redirectStateCookieName]) && !empty($_COOKIE[$redirectStateCookieName])) ? 'checked="checked"' : '' ;
    $rememberSelectionText = addslashes(getLocalString('remember_selection'));
    $loginString = addslashes(getLocalString('login'));
    $selectIdPString = addslashes(getLocalString('select_idp'));
    $otherFederationString = addslashes(getLocalString('other_federation'));
    $mostUsedIdPsString = addslashes(getLocalString('most_used'));
    $lastUsedIdPsString = addslashes(getLocalString('last_used'));
    $redirectCookie = (isset($_COOKIE[$redirectCookieName]) && !empty($_COOKIE[$redirectCookieName])) ?  $_COOKIE[$redirectCookieName] : '';

    // Generate list of Identity Providers
    $JSONIdPArray = array();
    $JSONCategoryArray = array();
    foreach ($IDProviders as $key => $IDProvider) {

        // Get IdP Name
        if (isset($IDProvider[$language]['Name'])) {
            $IdPName = addslashes($IDProvider[$language]['Name']);
        } else {
            $IdPName = addslashes($IDProvider['Name']);
        }

        // Set selected attribute
        $selected = ($selectedIDP == $key) ? ' selected:"true",' : '' ;
        $IdPType = isset($IDProviders[$key]['Type']) ? $IDProviders[$key]['Type'] : '';

        // SSO
        if (isset($IDProvider['SSO'])) {
            $IdPSSO = $IDProvider['SSO'];
        } else {
            $IdPSSO = '';
        }

        // Logo URL
        if (isset($IDProvider['Logo']['URL'])) {
            $IdPLogoURL = $IDProvider['Logo']['URL'];
        } else {
            $IdPLogoURL = '';
        }

        // Add other information to find IdP
354 355 356
        // $IdPData = getDomainNameFromURI($key);
        // $IdPData .= composeOptionData($IDProvider);
        $IdPData= buildIdpData($IDProvider, $key);
357 358 359 360 361 362 363 364 365 366
        $IdPData = addslashes($IdPData);

        // Skip non-IdP entries
        if ($IdPType == '') {
            continue;
        }

        // Fill category and IdP buckets
        if ($IdPType == 'category') {
            $JSONCategoryArray[] = <<<ENTRY
367

haemmer's avatar
haemmer committed
368 369
"{$key}":{
	type:"{$IdPType}",
haemmer's avatar
haemmer committed
370
	name:"{$IdPName}"
haemmer's avatar
haemmer committed
371 372 373
}

ENTRY;
374 375
        } else {
            $JSONIdPArray[] = <<<ENTRY
haemmer's avatar
haemmer committed
376 377 378 379 380 381 382

"{$key}":{ {$selected}
	type:"{$IdPType}",
	name:"{$IdPName}",
	logoURL:"{$IdPLogoURL}",
	data:"{$IdPData}"
}
383
ENTRY;
384 385 386 387 388 389 390 391 392 393 394 395
        }
    }
    $JSONIdPList = join(',', $JSONIdPArray);
    $JSONCategoryList = join(',', $JSONCategoryArray);

    // Locales for javascript
    $searchText = getLocalString('search_idp', 'js');
    $noIdPFoundText =  getLocalString('no_idp_found', 'js');
    $noIdPAvailableText = getLocalString('no_idp_available', 'js');

    // Process script
    require_once('js/embeddedWAYF.js');
haemmer's avatar
haemmer committed
396 397 398
}

/******************************************************************************/
haemmer's avatar
haemmer committed
399
// Print sample configuration script used for Embedded WAYF
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
function printEmbeddedConfigurationScript()
{
    global $IDProviders;
    global $customStrings;

    $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"');

    include(get_template('embedded-wayf.php'));
haemmer's avatar
haemmer committed
419 420
}

421 422
/******************************************************************************/
// Print sample configuration script used for Embedded WAYF
423 424 425 426
function printCSS($file)
{
    global $imageURL;

Geoffroy ARNOUD's avatar
Geoffroy ARNOUD committed
427
    if ($file != 'ImprovedDropDown.css' && $file != 'select2.css') {
428 429
        $file= 'styles.css';
    }
430

431 432 433 434 435 436 437 438 439 440 441 442 443 444
    $defaultCSSFile =  'css/default-'.$file;
    $cssContent = file_get_contents($defaultCSSFile);

    // Read custom CSS if available
    if (file_exists('css/custom-'.$file)) {
        $customCSSFile =  'css/custom-'.$file;
        $cssContent .= file_get_contents($customCSSFile);
    }

    // Read CSS and substitute content
    $cssContent = preg_replace('/{?\$imageURL}?/', $imageURL, $cssContent);

    echo $cssContent;
}
445

446 447 448
function get_template($name)
{
    global $topLevelDir;
449

450 451
    $custom_template = $topLevelDir . '/lib/custom-' . $name;
    $default_template = $topLevelDir . '/lib/default-' . $name;
452

453 454
    return (file_exists($custom_template)) ?
               $custom_template : $default_template;
455
}