######################################################################
#
#  EPrints Set Password (and Create Account)
#
######################################################################
#
#  This file is part of GNU EPrints 2.
#  
#  Copyright (c) 2000-2004 University of Southampton, UK. SO17 1BJ.
#  
#  EPrints 2 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.
#  
#  EPrints 2 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.
#  
#  You should have received a copy of the GNU General Public License
#  along with EPrints 2; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
######################################################################

use EPrints::Session;
use strict;

my $session = new EPrints::Session;
exit( 0 ) unless( defined $session );

exit( 0 ) unless( $session->auth_check( "change-email" ) );

my( $page, $title ) = make_page( $session );

$page->appendChild( $session->html_phrase( "general:userhome_link" ) );

$session->build_page( $title, $page, "change_email" );
$session->send_page();

$session->terminate();

sub make_page
{
	my( $session ) = @_;

	my $user = $session->current_user();

	my $page = $session->make_doc_fragment;
	my $title = $session->html_phrase( "cgi/users/change_email:title" );

	my $user_ds = $session->get_archive()->get_dataset( "user" );

	my $f_newemail = $user_ds->get_field( "newemail" );

	if( !$session->have_parameters() )
	{
		$page->appendChild( $session->html_phrase( "cgi/users/change_email:intro",
			email=>$user->render_value( "email" ) ) );

		my $fields = [ $f_newemail ];

		$page->appendChild( $session->render_input_form(
			fields=>$fields,
			values=>{},
			show_help=>1,
			default_action=>"submit",
			buttons=>{
				submit=>$session->phrase( "cgi/users/change_email:action_submit" )
			},
			dest=>"change_email" ) );

		return( $page, $title );
	}

	# Process the form.
	my $newemail = $f_newemail->form_value( $session );

	if( !EPrints::Utils::is_set( $newemail ) )
	{
		$page->appendChild( $session->html_phrase( 
			"cgi/users/change_email:no_email" ) );
		return( $page, $title );
	}

	$user->set_value( "newemail", $newemail );
	my $pin = sprintf( "%04X%04X%04X%04X",int rand 0xffff,int rand 0xffff,int rand 0xffff,int rand 0xffff );
	$user->set_value( "newpassword", undef );
	$user->set_value( "pin", $pin );
	$user->set_value( "pinsettime", time() );
	$user->commit();
	my $rc = $user->mail( 
		"cgi/users/change_email:changeemail",
		$session->html_phrase( "mail_email_pin", 
			confirmurl => $session->make_text( $session->get_archive()->get_conf( "perl_url" )."/confirm?userid=".$user->get_value( "userid" )."&pin=".$pin ),
			newemail => $session->make_text( $newemail ) ),
		undef,
		$newemail );

	if( !$rc )
	{
		$page->appendChild( $session->html_phrase( 
			"general:email_failed" ) );
		return( $page, $title );
	}

	$page->appendChild( $session->html_phrase( 
		"cgi/users/change_email:mail_sent",
		email=>$session->make_text( $newemail ) ) );
	return( $page, $title );
}

