package EPrints::Plugin::MePrints::ENovaMePrintsHandler; use strict; use warnings; our @ISA = qw/ EPrints::Plugin /; use EPrints::Apache::AnApache; sub handler { my( $r ) = @_; my $uri = $r->uri; my $repository_id = $r->dir_config( "EPrints_ArchiveID" ); return DECLINED unless( defined $repository_id ); if( $uri =~ m! ^\/profile\/([0-9a-zA-Z]+)(.*)$ !x ) { my $session = new EPrints::Session( 0, $repository_id ); return DECLINED unless( defined $session ); return DECLINED unless( $session->get_conf( "meprints_enabled" ) ); my $user; my $user_identifier = $1; if( $session->get_repository->get_conf( "meprints_profile_with_username" ) ) { if( $user_identifier =~ m! ^([a-zA-Z0-9]+)$ !x ) { # Handle a username. $user = EPrints::DataObj::User::user_with_username( $session, $1 ); } } else { if( $user_identifier =~ m! ^([0-9]+)$ !x ) { # Handle the userid. $user = EPrints::DataObj::User->new( $session, $1 ); } } if( defined $user ) { if( $user->is_profile_visible() ) { EPrints::Update::MePrints::update( $session->get_repository, $user ); &_send_user_profile_page( $session, $user ); $session->terminate; return OK; } else { $session->terminate; return FORBIDDEN } } $session->terminate; # Since the user has not been found they do not exist. # As a user profile was requested it is not appropriate # to continue to attempt parsing. The lack of any return # raises a 404 error which is handled in the standard # way. } return DECLINED; } sub _send_user_profile_page { my ( $session, $user ) = @_; my $page = $user->localpath."/index.page"; return unless( -e $page ); my $fh; open( $fh, "$page" ); my $html = ""; if( defined $fh ) { my $buffer; while( read( $fh, $buffer, 4096 ) ) { $html .= $buffer; } close( $fh ); } if(length $html) { my $xml = undef; eval{ my $tmp_xml = EPrints::XML::parse_xml_string( "".$html."" ); my @frags = $tmp_xml->getElementsByTagName( "fragment" ); if( scalar(@frags) ) { my $frag = $frags[0]; $xml = $session->make_doc_fragment; foreach my $node ($frag->childNodes) { $xml->appendChild( $node ); } } }; unless( defined $xml ) { $session->get_repository->log( "MePrints error: failed to parse the static user profile." ); return; } # uses text/html by default: $session->get_repository->send_http_header(); # EPrints Services/pjw 2011-01-31 http://servicesjira.eprints.org:8080/browse/EDGE-58 $session->prepare_page({ title => $session->html_phrase( "meprints:profile:title" ), page => $xml }); #$session->prepare_page({ title => $session->make_text( "User Profile" ), page => $session->make_doc_fragment }); $session->send_page(); } return; } 1;