<?php
/*
 * PiAnoS (Picture Annotation System), A fingerprint examiner training tool
 * Copyright (C) 2011-2021 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/.
 */
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;
            
            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();

?>
