#!/usr/bin/perl -wT

BEGIN {
 {
    if( $ENV{"SCRIPT_FILENAME"} && $ENV{"SCRIPT_FILENAME"} =~ /^(.+)\/[^\/]+$/ ) {
        chdir $1;
    }
    unshift @INC, '.';
    require 'setlib.cfg';
}

}
#
# TWiki Collaboration Platform, http://TWiki.org/
#
# Copyright (C) 2000-2003 Peter Thoeny, Peter@Thoeny.com
# Copyright (C) 2001 Klaus Wriessnegger, kw@sap.com
#
# For licensing info read license.txt file in the TWiki root.
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# 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 General Public License for more details, published at
# http://www.gnu.ai.mit.edu/copyleft/gpl.html
#

#NOTE: the reset code is in this script because it needs to be in an un-authenticated 
# script (see the .htaccess file)
# the matching InstallPassword code is protected, but still risky (anyone who has a valid 
# htpasswd entry can call it by hand 

#usage example:
#
#
#R e s e t
#
#<form name="passwd" action="/%SCRIPTURLPATH%/passwd%SCRIPTSUFFIX%/%WEB%/">
#Username     <input type="text" name="username" value="" size="16" /> <br />
#New password <input type="password" name="password" size="16" />
#retype New password <input type="password" name="passwordA" size="16" />
#<input type="hidden" name="installPasswd" value="requestReset" />
#<input type="submit" name="passwd" />
#
#

 
    # Set default current working directory
    # Set library paths in @INC at compile time

use CGI::Carp qw(fatalsToBrowser);
use CGI;
use TWiki;
use TWiki::User;
use TWiki::User::HtPasswdUser;
 

sub init_config { 
	my $config = shift;

$query= new CGI;

}# END of init_config
 
&main();
 
sub main
{  
   my %configHash; 
   my $config = \%configHash;
   my $twikiUri = $ENV{"SCRIPT_FILENAME"};
   $twikiUri =~ s!/+!/!g ; # remove multiple'/' 
   my @twikiUriItem = split ( '/' , $twikiUri ) ;
   $$config{'fileConfig'} = $ENV{"SERVER_NAME"} . "Port" . $ENV{"SERVER_PORT"} . $twikiUriItem[$#twikiUriItem - 2 ];

	 &TWiki::init_config($config) ;
	 &init_config($config) ;

    my $wikiName = $query->param( 'username' );

    #initialize
    my $topicName = $query->param( 'TopicName' );
    my $thePathInfo = $query->path_info();
    my $theUrl = $query->url;

    ( $topic, $webName ) =
        &TWiki::initialize($config, $thePathInfo, $wikiName, $topicName, $theUrl, $query );
 
    my $text = "";
    my $url = "";

    my $theRemoteUser = $query->remote_user();
    my ( $dummy1, $dummy2, $dummy3, $userName ) = 
	&TWiki::initialize($config, $thePathInfo, $theRemoteUser, $topicName, $theUrl, $query );

    my $action = $query->param("installPasswd");

if ( $action eq "requestReset" ) {
        showEncryptedPasswd ($config, $webName, $topic);
    } else {
       $url = &TWiki::getOopsUrl($config, $webName, $topic, "oopsmanage");
       TWiki::redirect($config, $query, $url );
    }
}

#==============================================
# ($webName, $topic)
sub showEncryptedPasswd
{ 
	my $config = shift;

    my  ($webName, $topic) = @_;

    # get all parameters from the form
    my $wikiName = $query->param( 'username' );
    my $passwordA = $query->param( 'password' );
    my $passwordB = $query->param( 'passwordA' );

    my $url = "";

    # check if required fields are filled in
    if( ! $wikiName || ! $passwordA ) {
        $url = &TWiki::getOopsUrl($config, $webName, $topic, "oopsregrequ", );
        TWiki::redirect($config, $query, $url );
        return;
    }
 
    # check if user entry exists
    if(  ( $wikiName )  && (! TWiki::User::UserPasswordExists($config, $wikiName ) ) ) {
        # PTh 20 Jun 2000: changed to getOopsUrl
        $url = &TWiki::getOopsUrl($config, $webName, $topic, "oopsnotwikiuser", $wikiName );
        TWiki::redirect($config, $query, $url );
        return;
    }

    # check if passwords are identical
    if( $passwordA ne $passwordB ) {
        $url = &TWiki::getOopsUrl($config, $webName, $topic, "oopsregpasswd" );
        TWiki::redirect($config, $query, $url );
        return;
    }

    my $theCryptPassword = &TWiki::User::HtPasswdUser::_htpasswdGeneratePasswd($config, $wikiName,  $passwordA );

    # and finally display the reset password page
    $url = &TWiki::getOopsUrl($config, $webName, $wikiName, "oopsresetpasswd", $wikiName.":".$theCryptPassword );
    TWiki::redirect($config, $query, $url );
}
