<?php
// Het principe van deze index.php is dat van geen enkele pagina er een .html bestand bestaat. 
// Het gevolg hiervan is dat alle requests een 404 (page not found) geven.
// in de .htaccess wordt die error 404 afgevangen en doorgestuurd naar deze index.php
// In deze index.php wordt checkaccess.php aangeroepen wat uitzoeket of de pagina wel bekeken mag worden,
// als de gebruiker toegang krijgt wordt de pagina on the fly gegenereerd met xsltproc


// -------------------------------------------------
// Include checkaccess.php, 
// Hierin wordt de parameter $AccessArray[Access] op 1 gezet als de gebruiker toegang heeft
// -------------------------------------------------
require 'authenticate.php';

$url=$_SERVER['REQUEST_URI'];                                      // url opvragen
$user=$_SERVER['REMOTE_USER'];                                     // ingelogde user (vanuit htaccess)
$FullFileName=$_SERVER["DOCUMENT_ROOT"]. $_SERVER['REQUEST_URI'];  // volledige URL 

// -------------------------------------------------
// log controle, als URL eindigd op /pleaselog dan logging aan zetten (komt boven in het schrem)
// -------------------------------------------------
$EndUrl = substr($url, strrpos($url, '/') + 1);
if ($EndUrl == "pleaselog" ) { 
    $LogLevel = 1 ; 
    echo "Originele URL is  $url <br>";
} else {
    $LogLevel =  0  ; 
}

// -------------------------------------------------
// Include siteconfig.php, Configuratie parameters laden
// -------------------------------------------------
require 'siteconfig.php';
if ($LogLevel == 1 ) { $start = microtime(true); }

// -------------------------------------------------
// Usernaam ophalen, is ingevoerd via .htaccess
// -------------------------------------------------
$UserName=$_SERVER['REMOTE_USER'];

// -------------------------------------------------
// Extra informatie boven in het scherm voor debug doeleinde,
// -------------------------------------------------
if ($LogLevel == 1 ) { echo "Url: $url <br>"; }
if ($LogLevel == 1 ) { echo "Volledige filenaam: $FullFileName <br>"; }
if ($LogLevel == 1 ) { echo "TitleIndexFile: $TitleIndexFile <br>"; }
if ($LogLevel == 1 ) { echo "Ingelogd met LDAP gebruiker: $UserName <br>"; }

// -------------------------------------------------
// heeft de gebruiker wel toegang ?
// -------------------------------------------------
$AccessArray = CheckAccess($UserName, $url) ;
if ($LogLevel == 1 ) { echo "Total Access is  $AccessArray[Access] <br>" ; }

if ( $AccessArray[Access] == 404 )  {
    // -------------------------------------------------
    // Opgevraagde pagina bestaat niet, een foutpagina wordt getoond.
    // -------------------------------------------------
    echo '<img src="/images/404-error.png" alt="image" /><br>';
    echo "De pagina bestaat NIET $url<br>";
    echo " de pagina bestaat niet, u wordt door gestuurd over 5 seconden.<br>";
    header( "refresh:5;url=" . "$SitePrefix" . "index/index.html" );
} elseif ( $AccessArray[Access] == 1   )  {
    // -------------------------------------------------
    // Als de gebruiker toegang krijgt tot de pagina dan een nieuwe pagina opbouwen met xslt
    // -------------------------------------------------
    $xmlDoc = new DOMDocument();
    $xslDoc = new DOMDocument();
    $xslDoc->load( $SiteStyleSheet );

    $xmlDoc->load($AccessArray[bronbestand]);
    $xsltProcessor = new XSLTProcessor();
    $xsltProcessor->registerPHPFunctions();
    $xsltProcessor->importStyleSheet($xslDoc);

        // the site-description file to use, no default:
    $xsltProcessor->setParameter('','sitefilename', $SDFFile);

        // site-local references not mentioned in the sdf are prefixed with the following (not a configuration variable):
    $xsltProcessor->setParameter('','jit201688sitelocalprefix', "$SitePrefix" . "object.php?file=");

        // lri's come from the local site, from a directory called lris/, not a configuration variable
    $xsltProcessor->setParameter('','lriprefix', "$SitePrefix" . "object.php?file=lris/");

        // the logo always sits locally in . ; not a configuration variable:
    $xsltProcessor->setParameter('','logoprefix', "$SitePrefix" . "object.php?file=");

        // auxiliary files may come from a remote URL or be just local objects; this is a configuration item
    $xsltProcessor->setParameter('','auxfileprefix', isset( $AuxFilePrefix ) ? $AuxFilePrefix : "$SitePrefix" . "object.php?file=");

        // some sites require /<some name>/ for addressing site-local urls.
    $xsltProcessor->setParameter('','siteprefix', "$SitePrefix");

        // in order to correctly select random lri's, the max amount is needed
    $xsltProcessor->setParameter('','numOfLRIs', '10');

        // there's a lot of the above parameters, that is because the defaults in
        // sitec are in favour of static pages, not jit. We need to be able
        // to re-use the given sitec source-files for jit-purposes without any change.

    // -------------------------------------------------
    // Pagina presenteren
    // -------------------------------------------------
    echo $xsltProcessor->transformToXML($xmlDoc);
} else {
    echo '<img src="/images/access-denied.gif" alt="image" /><br>';
    echo "$AccessArray[Access] Access Geen toegang, druk op de back button in uw browser <br> ";
}

if ($LogLevel == 1 ) { echo $time_elapsed_secs = microtime(true) - $start; }

?>
