<?php
/*
 * PiAnoS (Picture Annotation System), A fingerprint examiner training tool
 * Copyright (C) 2011-2012 University of Lausanne, Switzerland
 * https://ips-labs.unil.ch/pianos
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/.
 */

/*
 * Note: a full example of the 'shibboleth_login' setting is the following:
 *
 * Application::set('shibboleth_login',array(
 * 'login_text' => 'Follow the link to use your UNIL crendentials',
 * 'login_prompt' => 'UNIL student login with Switch AAI',
 * 'login_image' => 'shibboleth_buttonflat.gif',
 * 'url' => 'shibboleth-simulate.php',
 * 'username' => 'Shib-SwissEP-UniqueID',
 * 'display_name' => array('Shib-InetOrgPerson-givenName','Shib-Person-surname'),
 * 'email' => 'Shib-InetOrgPerson-mail',
 * 'groups' => array(
 * //array('Shib-Application-ID', '/default/', 'ACO Automne 2011'),
 * array('Shib-EP-Entitlement', '/http:\/\/grps.unil.ch\/as\/ips/', 'Administrators'),
 * array('Shib-EP-Entitlement', '/http:\\/\\/grps.unil.ch\\/as\\/etu-ips-bac/', 'RensFo 2012 2eme BSc'),
 * //array('Shib-EP-Entitlement', '/http:\\/\\/grps.unil.ch\\/as\\/etu-ips-mas/', 'ACO Automne 2011'),
 * ),
 * 'return' => 'main.php',
 * ));
 *
 */
function getShibbolethValues()
{
    return array (
            'Shib-InetOrgPerson-givenName' => 'Thibault', 
            'Shib-InetOrgPerson-mail' => 'Thibault.Genessay@unil.ch', 
            'Shib-Person-surname' => 'Genessay', 
            'Shib-SwissEP-StudyBranch3' => '2600', 
            'Shib-SwissEP-StudyLevel' => '2600-40', 
            'Shib-SwissEP-UniqueID' => '128397203178@unil.ch', 
            'Shib-EP-Entitlement' => 'http://grps.unil.ch/as/personnes-phys;http://grps.unil.ch/as/pat-droit;http://grps.unil.ch/as/acces-soft;http://grps.unil.ch/as/eliot;http://grps.unil.ch/as/all-agendas;http://grps.unil.ch/as/tous-unil;http://grps.unil.ch/as/esc-anacrime;http://grps.unil.ch/as/bib-acces-portal;http://grps.unil.ch/as/enseignants-unil;http://grps.unil.ch/as/etudiants-esc;http://grps.unil.ch/as/acces-reseau;http://grps.unil.ch/as/all-etu-portail;http://grps.unil.ch/as/all-users-ips;http://grps.unil.ch/as/enseignants-assistants-unil;http://grps.unil.ch/as/esc;http://grps.unil.ch/as/portail-sap;http://grps.unil.ch/as/etu-sem-semanc;http://grps.unil.ch/as/all-ens-portail;http://grps.unil.ch/as/tous-droit;http://grps.unil.ch/as/ur_ips;http://grps.unil.ch/as/enseignants-droit;http://grps.unil.ch/as/enseignants-assistants-droit;http://grps.unil.ch/as/intranet;http://grps.unil.ch/as/membres-ur_unil;http://grps.unil.ch/as/unilogo;http://grps.unil.ch/as/etudiants-unil;http://grps.unil.ch/as/tous-unil-autres;http://grps.unil.ch/as/all-users-portail;http://grps.unil.ch/as/mailbox;http://grps.unil.ch/as/ips;http://grps.unil.ch/as/doctorants-unil;http://grps.unil.ch/as/personnel-unil;http://grps.unil.ch/as/pat-unil;http://grps.unil.ch/as/cnlv-wwwdcs;http://grps.unil.ch/as/jahia_redactors;urn:mace:dir:entitlement:common-lib-terms' 
    );
}

require_once ( "unil/Application.php" );
require_once ( "unil/SessionManager.php" );
class MainDriver extends Driver
{
    public function accept()
    {
        return true;
    }
    protected function doHandleRequest()
    {
        $shib = Application::get( 'shibboleth_login' );
        if( ! $shib )
            throw new Exception( "Could not find shibboleth settings" );
        
        if( isset( $shib[ 'return' ] ) && ! isset( $_GET[ 'dump' ] ) )
        {
            $ret_path = $shib[ 'return' ];
            
            if( ! isset( $_SESSION[ 'security' ] ) )
                $_SESSION[ 'security' ] = array ();
            
            $_SESSION[ 'security' ][ 'shibboleth' ] = $_SERVER;
            
            $localValues = getShibbolethValues();
            foreach( $localValues as $key => $value )
            {
                $_SESSION[ 'security' ][ 'shibboleth' ][ $key ] = $value;
            }
            
            header( "Location: " . $ret_path );
            exit();
        } else
        {
            header( "Content-Type: text/plain; charset=utf-8;" );
            echo "Shibboleth Authentication Succeeded\n";
            echo "===================================\n";
            echo "\n";
            echo "Your authentication was successful but I was unable to redirect you to the appropriate page.\n";
            echo "Please point your browser to the login page of the application and you should be fine.\n";
            echo "\n";
            echo "\n";
            echo "The following information will not be recorded but is shown here mainly for debugging purposes.\n";
            echo "If you do not now what it means, simply ignore it and return to the login page.\n";
            echo "\n";
            var_dump( $_SERVER );
        }
    }
}
class App extends Application
{
    function __construct()
    {
        parent::__construct( false );
        $this->setLocalSettingsPath( 'backend/settings-local.php' );
    }
    protected function createDefaultSettings()
    {
        $settings = parent::createDefaultSettings();
        if( ! isset( $settings[ 'session_lifetime' ] ) )
            $settings[ 'session_lifetime' ] = NULL;
        if( ! isset( $settings[ 'cookie_params' ] ) )
            $settings[ 'cookie_params' ] = NULL;
        return $settings;
    }
    public function init()
    {
        $this->setName( 'PiAnoS 4' );
        
        $this->registerDriver( new MainDriver() );
        $session = new SessionManager( md5( $this->getName() ), array (
                'action', 
                'logout' 
        ), Application::get( 'session_lifetime' ) );
        $sessionParams = Application::get( 'cookie_params' );
        if( $sessionParams )
            $session->setCookieParams( $sessionParams );
        $this->registerDriver( $session );
    }
}

$app = new App();
$app->run();

?>

