#!%PERL% -w
# Copyright (c) vhffs project and its contributors
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without 
# modification, are permitted provided that the following conditions 
# are met:
#
# 1. Redistributions of source code must retain the above copyright 
#   notice, this list of conditions and the following disclaimer.
#2. Redistributions in binary form must reproduce the above copyright
#   notice, this list of conditions and the following disclaimer in 
#   the documentation and/or other materials provided with the 
#   distribution.
#3. Neither the name of vhffs nor the names of its contributors 
#   may be used to endorse or promote products derived from this 
#   software without specific prior written permission.
#
#THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
#"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
#LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
#FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
#COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
#INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
#BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
#LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
#CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
# POSSIBILITY OF SUCH DAMAGE.


require 5.004;
use utf8;
use POSIX;
use strict;
use locale;
use Locale::gettext;
use HTML::Template;
use CGI;
use lib '%VHFFS_LIB_DIR%';
use Vhffs::Main;
use Vhffs::Panel::Main;
use Vhffs::Panel::Commons;
use Vhffs::Constants;

my $panel = new Vhffs::Panel::Main();
exit 0 unless $panel;

my $vhffs = $panel->{'vhffs'};
my $templatedir = $panel->{'templatedir'};
my $cgi = $panel->{cgi};
my $loginsubmitted = defined( $cgi->param('login_submit') );
my $logoutsubmitted = defined( $cgi->param('logout') );

if( $loginsubmitted ) {
    # User tried to log in
    # we try to clean the previous session
    my $oldsid = CGI->cookie( CGI::Session::name() );
    if( defined $oldsid ) {
        my $oldsession = new CGI::Session(undef, $oldsid, {Directory=>'/tmp'});
        $oldsession->delete();
    }

    my $username = $cgi->param('username');
    my $password = $cgi->param('password');
    my $user;
    $user = Vhffs::User::get_by_username($vhffs, $username) if(defined $username);

    #Incomplete input
    unless( defined $username  &&  defined $password  &&  defined $user  &&  $user->check_password( $password ) ) {
        $panel->add_error( gettext('Login failed !') );
    } elsif($user->get_status != Vhffs::Constants::ACTIVATED) {
        $panel->add_error( gettext('User is not active yet') );
    } else {
        # Creates the new session
        my $session = new CGI::Session("driver:File", undef, {Directory=>'/tmp'});
        $session->expires("+1h");
        $session->param("username", $user->get_username);
        $session->param("uid", $user->get_uid);

        my $sessioncookie = new CGI::Cookie(-name => $session->name, -value => $session->id);

        # Refresh cookies (avoid theme and language loss when user
        # deletes cookies).
        my $themecookie = new CGI::Cookie( -name=> 'theme', -value=>$user->get_theme, -expires=>'+10y' );
        my $langcookie = new CGI::Cookie( -name=>'language', -value=>$user->get_lang, -expires=>'+10y' );

	# Set last login panel to current time
	$user->update_lastloginpanel;
	$user->commit;

        $panel->redirect('/panel.pl', [$sessioncookie, $themecookie, $langcookie]);
    }
}

if( $logoutsubmitted )  {
	#clean session
	my $oldsid = CGI->cookie( CGI::Session::name() );
	if( defined $oldsid )  {
		my $oldsession = new CGI::Session(undef, $oldsid, {Directory=>'/tmp'});
		$oldsession->delete();
	}

	$panel->add_info( gettext('You left your VHFFS session!') );

	Vhffs::Panel::Commons::display_login($vhffs, $panel);
}

if( ( ! $loginsubmitted  &&  ! $logoutsubmitted )  ||  $panel->has_errors ) {
	Vhffs::Panel::Commons::display_login($vhffs, $panel);
}
