######################################################################
#
#  EPrints Process Request
#
######################################################################
#
#  __COPYRIGHT__
#
# Copyright 2023 University of Southampton.
# EPrints 3.4 is supplied by EPrints Services.
#
# http://www.eprints.org/eprints-3.4/
#
#  __LICENSE__
#
# This file is part of EPrints 3.4 L<http://www.eprints.org/>.
#
# EPrints 3.4 and this file are released under the terms of the
# GNU Lesser General Public License version 3 as published by
# the Free Software Foundation unless otherwise stated.
#
# EPrints 3.4 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with EPrints 3.4.
# If not, see L<http://www.gnu.org/licenses/>.
#
######################################################################

use EPrints;
use strict;

my $session = EPrints->new->current_repository;
exit( 0 ) unless( defined $session );

my $code = $session->param( "code" );
unless( defined $code )
{
	&_render_error( $session, $session->html_phrase( "cgi/process_request:bad_params" ) );
	$session->terminate;
	return;
}

my $request = EPrints::DataObj::Request->new_from_code( $session, $code );

if( !defined $request )
{
	&_render_error( $session, $session->html_phrase( "cgi/process_request:bad_params" ) );
	$session->terminate;
	return;
}

if( $request->has_expired() )
{
	&_render_error( $session, $session->html_phrase( "cgi/process_request:request_expired" ) );
	$session->terminate;
	return;
}

# is the document still available?
my $document = $session->dataset( 'document' )->dataobj( $request->get_value( "docid" ) );
unless( defined $document )
{
	&_render_error( $session, $session->html_phrase( "cgi/process_request:doc_deleted" ) );
	$session->terminate;
	return;
}

# is the document now public? in which case redirect to document url
my $doc_secu = $document->get_value( "security" );
if( defined $doc_secu && $doc_secu eq 'public' )
{
	$session->redirect( $document->get_url );
	$session->terminate;
	return;
}

# don't set expiry date -> will expire at the end of the current session
my $cookie;
my $document_url = $document->get_url;
if ( EPrints::Utils::is_set( $session->config( "securehost" ) ) )
{
	$document_url =~ s/^http:/https:/;
	$cookie = $session->{query}->cookie(
		-name => "eprints_doc_request",
		-path => "/",
		-value => $code,
		-secure => 1,
		-samesite => 'Strict',
		-domain => $session->config( "cookie_domain" ),
	);
}
else
{
	$cookie = $session->{query}->cookie(
		-name => "eprints_doc_request",
		-path => "/",
		-value => $code,
		-samesite => 'Strict'
		-domain => $session->config( "cookie_domain" ),
	);
}

unless( defined $cookie )
{
	&_render_error( $session, $session->html_phrase( "cgi/process_request:cookie_error" ) );
	$session->terminate;
	return;
}

my $r = $session->get_request;
$r->err_headers_out->{"Set-Cookie"} = $cookie;

$session->send_http_header( "content-type" => "text/html" );  
print '<html><head><meta http-equiv="refresh" content="0;url='.$document_url.'"/></head><body></body></html>';  

$session->terminate;
return;

sub _render_error
{
	my( $session, $error_msg ) = @_;

	my $title = $session->html_phrase( "cgi/process_request:title" );
	my $page = $session->html_phrase( "cgi/process_request:page", error => $error_msg );
	$session->build_page( $title, $page, "process_request" );
	$session->send_page;
	return;
}
