use EPrints; use EPrints::Apache::AnApache; use strict; my $json = undef; if( $ENV{CONTENT_LENGTH} ) { use JSON; use Encode; my $buffer = ''; read(STDIN,$buffer,$ENV{CONTENT_LENGTH}); my $json_decoder = JSON->new; $buffer = decode_utf8( $buffer ); $json = $json_decoder->decode( $buffer ); } my $session = EPrints::Session->new(); # If there is no current user, then attempt to login via auth_basic details if( !defined( $session->current_user ) ) { my $auth_name = "/cgi/archivematica/set_uuid"; $session->get_request->auth_name( $auth_name ); my( $res, $passwd_sent ) = $session->get_request->get_basic_auth_pw; my( $user_sent ) = $session->get_request->user; $session->{current_user} = $session->_current_user_auth_basic; my $valid_username = $session->valid_login( $user_sent, $passwd_sent ); # Check the that the user is logged in, otherwise throw up a basic auth if( !$valid_username || !defined( $session->current_user ) ) { my $request = $session->get_request; EPrints::Apache::AnApache::header_out( $session->get_request, "WWW-Authenticate" => "Basic realm=\"$auth_name\"" ); EPrints::Apache::AnApache::send_status_line( $session->get_request, AUTH_REQUIRED, "Requires authentication" ); EPrints::Apache::AnApache::send_http_header( $session->get_request ); $session->terminate; exit 0; } } $session->send_http_header( content_type => "text/json" ); my $amid = $session->param( "amid" ); $amid = $json->{"amid"} if $json && $json->{"amid"}; if( !$amid ) { print '[{ error: "No amid parameter specified in the JSON" }]'; $session->terminate; exit( 0 ); } #ignore instances when Archivematica assigns a version number to the amid using the underscore, for example: 1_4, the amid is still 1 $amid =~ s/_.*$//g; #ignore underscore version #extract prefix if one is set, search for string before - my $prefix_received = $amid; $prefix_received =~ s/-.*$//g; #ignore all after - $amid =~ s/[^0-9]//g; # digits only #check for prefix mismatch #if there is a non-empty prefix defined in the settings, it should match what we received if ((defined $session->config( 'DPExport', 'transfer_prefix' ) ) && ($session->config( 'DPExport', 'transfer_prefix' ) ne "")){ if( $session->config( 'DPExport', 'transfer_prefix' ) ne $prefix_received ) { print '[{ error: "Prefix received '.$prefix_received.' does not match prefix configured. Ignore call." }]'; $session->terminate; exit( 0 ); } } #there is either empty or not-defined prefix - check to make sure that we didn't receive one, if we did, it's a mismatch else{ if ($prefix_received ne $amid){ print '[{ error: "Prefix received, but no prefix configured. Ignore call." }]'; $session->terminate; exit( 0 ); } } my $uuid = $session->param( "uuid" ); $uuid = $json->{"uuid"} if $json && $json->{"uuid"}; $uuid =~ s/[^0-9\-a-z]//g; # digits, letters and dashes only if( ! $session->current_user->has_role( "archivematica/export" ) ) { print '[{ error: "You must be logged in and have the archivematica/export role to access this page." }]'; $session->terminate; exit( 0 ); } if( !$amid ) { print '[{ error: "No amid number could be extracted." }]'; $session->terminate; exit( 0 ); } if( !$uuid ) { print '[{ error: "No uuid specified" }]'; $session->terminate; exit( 0 ); } my $ds = $session->dataset( "archivematica" ); my $searchexp = new EPrints::Search( session=>$session, dataset=>$ds ); $searchexp->add_field( $ds->get_field( "amid" ), $amid, "EQ" ); my $list = $searchexp->perform_search; if( $list->count() ) { # just the first one my $obj = $list->item(0); $obj->set_value( "uuid", $uuid ); $obj->add_to_record_log( "set_uuid", "UUID:[".$uuid."]", "success" ); $obj->commit(); } my $plugin = $session->plugin( "Export::JSON" ); $plugin->initialise_fh( \*STDOUT ); $plugin->output_list( list=>$list, fh=>*STDOUT ); $session->terminate();