#!/usr/bin/perl -T
# $Id: changepwd.cgi,v 1.7 2014/03/20 13:51:22 sqko Exp $
############################################################
############################################################
###                                                      ###
###   Originally designed by Soeren Koch                 ###
###                                                      ###
###   Impelemented by Soeren Koch                        ###
###                                                      ###
###   Copyright by Soeren Koch January 2011              ###
###                                                      ###
############################################################
############################################################
use strict;
use warnings;
use RFC::Main;
use RFC::RFCCGI qw(:cgi :html);
use RFC::Header;
use CGI qw(:standard :netscape);

my $domain = &get_cv('servers','domain');
if (!defined(param))
{
    &print_header("Welcome to the $name database");
    print '<center>';
    RFC::RFCCGI::login();
    print '</center>';
    &print_end;
}
else
{
    if (&login_ok)
    { 
	$_ = param('action');
      CASE:
	{
	    /^change_pwd$/ and do {&change; last CASE;};
	    ### Default response ###
	    &print_header("$name user management center");
	    print '<center>';
	    &passwd_form;
	    print '</center>';
	    print p('');
	    print '<center>';
	    &ret;
	    print '</center>';
	}
	&print_end;
    }
    else
    {
	&not_auth;
    }
}

sub passwd_form
{
    print center(h3({-class=>'Bl'},"Passwords must be at least 6 characters and contain at least one letter and at least one number!"));
    print '<script>';
    print 'function checkpwd()',"\n";
    print "{\n";
    print "  var f = document.passform;\n";
    print '  var p1 = f.elements["pass1"].value;',"\n";
    print '  var p2 = f.elements["pass2"].value;',"\n";
    print '  if (p1 == p2)',"\n";
    print "  {\n";
    print "     return true;\n";
    print "  }\n";
    print '  alert("New passwords does not match!");',"\n";
    print "  return false;\n";
    print "}\n";
    print '</script>';
    print start_form(-name=>'passform',-onsubmit=>'return checkpwd()');
    &print_hidden;
    print '<center>';
    print '<table border=0 cellspacing=10 cellpadding=0>';
    print '<tr><th>';
    print p("New password");
    print '</th><th>';
    print password_field(-name=>'pass1',-value=>'',-force=>'1');
    print '</th></tr><tr><th>';
    print p("Retype new password");
    print '</th><th>';
    print password_field(-name=>'pass2',-value=>'',-force=>'1');
    print '</th></tr></table>';
    &action('change_pwd');
    print submit(-name=>'test',-value=>'change password',-class=>'s4',-style=>'width:200px;');
    print end_form;
    print '</center>';
}

sub change
{
    my $pass = &get_pass;
    my $new1;
    my $new2;
    $new1 = param('pass1') if defined(param('pass1'));
    $new2 = param('pass2') if defined(param('pass2'));
    if ($new1 ne $new2)
    {
	&print_header("$name user management center");
	print h3({-class=>'Alert'},"New passwords not equal!!");
	&passwd_form;
	print '<center>';
	&ret;
	print '</center>';
    }
    else
    {
	my $res = 0;
	$res = 1 if (length($new1) < 7);
	$res = 1 if ($new1 !~ /[0-9]/);
	$res = 1 if (($new1 !~ /[a-z]/) && ($new1 !~ /[A-Z]/));
	&print_header("$name user management center") if ($res);
	print center(h3({-class=>'Alert'},"New password not accepted!")) if ($res);
	print center(h3({-class=>'Bl'},"Passwords must be at least 6 characters and contain at least one letter and at least one number!")) if ($res);
	if (!$res)
	{
	    my $user = &get_user;
	    my $r = &RFC::Main::passwd_client('changepwd',$user,$pass,$new1);
	    if ($r && $r > 0)
	    {
		my $key = &RFC::Main::passwd_client('new_session',$user,$new1);
		&print_header("You are logged in to the $name");
		print center(h3({-class=>'Bl'},"Password changed."));
		print start_form(-action=>'main.cgi');
		print hidden(-name=>'name',-value=>$user,-force=>1);
		print hidden(-name=>'pass',-value=>$key,-force=>1);
		print center(submit(-class=>'s4',-name=>'test',-value=>'Ok',-style=>'width:100px;'));
		print end_form;
	    }
	    else
	    {
		&print_header("You are logged in to the $name");
		print center(h3({-class=>'Alert'},"Password could not be changed!")) if (!$r);
		&passwd_form;
		print '<center>';
		&ret;
		print '</center>';
	    }
	}
	else
	{
	    print '<center>';
	    &ret;
	    print '</center>';
	}
    }
}

sub ret
{
    print start_form(-action=>'main.cgi');
    &print_hidden;
    print center(submit(-name=>'test',-value=>'Return',-class=>'s0',-style=>'width:200px;'));
    print end_form;
}

