From e5769ff2747c5f2e07d1d1ace4a1f3fcd1108b41 Mon Sep 17 00:00:00 2001 From: Pedro Amorim Date: Fri, 11 Apr 2025 10:32:36 +0000 Subject: [PATCH] Bug 36197: (QA follow-up): Add public form captcha Feedback provided by David Cook + Matthias Meusburger at KohaCon24 --- Koha/ILL/Backend/Standard.pm | 6 ++++-- Koha/ILL/Backend/opac-includes/create.inc | 18 ++++++++++++++++++ Koha/ILL/Request.pm | 18 ++++++++++-------- opac/opac-illrequests.pl | 21 +++++++++++++++++++-- 4 files changed, 51 insertions(+), 12 deletions(-) diff --git a/Koha/ILL/Backend/Standard.pm b/Koha/ILL/Backend/Standard.pm index 36d4ea79408..47ae66f6809 100644 --- a/Koha/ILL/Backend/Standard.pm +++ b/Koha/ILL/Backend/Standard.pm @@ -296,8 +296,10 @@ sub create { C4::Context->preference("ILLOpacUnauthenticatedRequest") && !$other->{'cardnumber'}; if ($unauthenticated_request) { ( $failed, $result ) = _validate_form_params( $other, $result, $params ); - if ( !Koha::ILL::Request::unauth_request_data_check($other) ) { - $result->{status} = "missing_unauth_data"; + return $result if $failed; + my $unauth_request_error = Koha::ILL::Request::unauth_request_data_error($other); + if ($unauth_request_error) { + $result->{status} = $unauth_request_error; $result->{value} = $params; $failed = 1; } diff --git a/Koha/ILL/Backend/opac-includes/create.inc b/Koha/ILL/Backend/opac-includes/create.inc index fa2bad04236..b4d5e2c2e58 100644 --- a/Koha/ILL/Backend/opac-includes/create.inc +++ b/Koha/ILL/Backend/opac-includes/create.inc @@ -27,6 +27,8 @@
Please note: The library you chose is invalid.
+[% ELSIF whole.status == 'failed_captcha' %] +
You typed in the wrong characters in the box before submitting. Please try again.
[% ELSE %]

Unhandled error

[% END %] @@ -119,6 +121,22 @@ [% INCLUDE "${cwd}/shared-includes/forms/${type}.inc" %] [% END %] [% INCLUDE "${cwd}/shared-includes/custom_fields.inc" %] + [% IF whole.value.other.type && unauthenticated_ill && !logged_in_user%] +
+ Verification +
    +
  1. + + + +
    Required
    + + + Please type the following characters into the preceding box: [% captcha | html %] +
  2. +
+
+ [% END %]
Cancel diff --git a/Koha/ILL/Request.pm b/Koha/ILL/Request.pm index 70fbe9f49bf..265c2b8f876 100644 --- a/Koha/ILL/Request.pm +++ b/Koha/ILL/Request.pm @@ -1968,23 +1968,25 @@ sub add_unauthenticated_data { ); } -=head3 unauth_request_data_check +=head3 unauth_request_data_error - unauth_request_data_check($metadata); + unauth_request_data_error($metadata); -Checks if unauthenticated request data is present +Checks if unauthenticated request data errored, returns error_code if so, else 0 =cut -sub unauth_request_data_check { +sub unauth_request_data_error { my ($metadata) = @_; - return 1 unless C4::Context->preference("ILLOpacUnauthenticatedRequest"); - - return - $metadata->{unauthenticated_first_name} + return 0 unless C4::Context->preference("ILLOpacUnauthenticatedRequest"); + return 'missing_unauth_data' + unless $metadata->{unauthenticated_first_name} && $metadata->{unauthenticated_last_name} && $metadata->{unauthenticated_email}; + + return 'failed_captcha' if $metadata->{failed_captcha}; + return 0; } =head3 append_to_note diff --git a/opac/opac-illrequests.pl b/opac/opac-illrequests.pl index ad7ef34c8f3..6db3c3ab7d4 100755 --- a/opac/opac-illrequests.pl +++ b/opac/opac-illrequests.pl @@ -24,8 +24,10 @@ use JSON qw( encode_json ); use CGI qw ( -utf8 ); use C4::Auth qw( get_template_and_user ); use C4::Koha; -use C4::Output qw( output_html_with_http_headers ); -use POSIX qw( strftime ); +use C4::Output qw( output_html_with_http_headers ); +use Digest::MD5 qw( md5_base64 ); +use POSIX qw( strftime ); +use String::Random qw( random_string ); use Koha::ILL::Request::Config; use Koha::ILL::Requests; @@ -119,6 +121,15 @@ if ( $op eq 'list' ) { my $req = Koha::ILL::Request->new; $template->param( backends => $backends ); } else { + + if ( C4::Context->preference('ILLOpacUnauthenticatedRequest') && !$patron ) { + my $captcha = random_string("CCCCC"); + $template->param( + captcha => $captcha, + captcha_digest => md5_base64($captcha), + ); + } + $params->{backend} = 'Standard' if $params->{backend} eq 'FreeForm'; my $request = Koha::ILL::Request->new->load_backend( $params->{backend} ); @@ -155,6 +166,12 @@ if ( $op eq 'list' ) { exit; } + if ( $request->_backend_capability( 'can_create_request', $params ) ) { + if ( md5_base64( uc( $params->{captcha} ) ) ne $params->{captcha_digest} ) { + $params->{failed_captcha} = 1; + } + } + $params->{cardnumber} = $patron->cardnumber if $patron; $params->{branchcode} = $patron->branchcode if $patron; $params->{opac} = 1; -- 2.39.5