#!/usr/bin/perl -T
# $Id: login.cgi,v 1.7 2016/06/20 08:45:06 sqko Exp $
############################################################
############################################################
###                                                      ###
###   Originally designed by Soeren Koch                 ###
###                                                      ###
###   Impelemented by Soeren Koch                        ###
###                                                      ###
###   Copyright (C) by Soeren Koch 2010                  ###
###                                                      ###
############################################################
############################################################

use strict;
use warnings;
use RFC::Header;
use RFC::RFCCGI qw(:html :cgi);
use RFC::Main;
use CGI qw(:standard :netscape);

my $domain = &get_cv('servers','domain');
if (!defined(param('action')))
{
    &print_header("Welcome to the $name");
    print '<center>';
    RFC::RFCCGI::login();
    print '</center>';
    &print_end;
}
else
{
    my $c = 0;
    my ($user,$pass) = &RFC::RFCCGI::get_id();
    my $md5 = RFC::Main::passwd_client('new_session',$user,$pass);
    my $uid = RFC::Main::passwd_client('checkuser',$user,$md5);
    if ($uid && $uid !~ /error/i && $md5 !~ /error/i)
    {
	&RFC::RFCCGI::print_header("You are logged in to the $name",-onload=>'enterdb()');
	### Javascript for proceeding to main.cgi ###
	print "<script lanuage=\"javascript\">\n";
	print "function enterdb() {\n";
	print "   document.enterdatabase.submit();\n";
	print "}\n";
	print "</script>\n\n";
	print start_form(-action=>'main.cgi',-name=>'enterdatabase');
	print hidden(-name=>'name',-value=>$user,-force=>1);
	print hidden(-name=>'pass',-value=>$md5,-force=>1);
	print end_form;
	&print_end;
    }
    else
    {
	&print_header("Access for $name denied!");
	if (($md5 && $md5 =~ /error/i) || ($uid && $uid =~ /error/i))
	{
	    print h2({-class=>'C'},"Error: Password server could not be reached!");
	}
	else
	{
	    print h2({-class=>'C'},"Possible reason wrong username or password.");
	}
        print '<center>';
        RFC::RFCCGI::login();
        print '</center>';
	&print_end;
    }
}

