3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
21 use Digest::MD5 qw( md5_base64 md5_hex );
22 use String::Random qw( random_string );
23 use WWW::CSRF qw(generate_csrf_token check_csrf_token CSRF_OK);
28 use C4::Form::MessagingPreferences;
30 use Koha::Patron::Modifications;
31 use C4::Branch qw(GetBranchesLoop);
35 use Koha::Patron::Images;
38 my $dbh = C4::Context->dbh;
40 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
42 template_name => "opac-memberentry.tt",
49 unless ( C4::Context->preference('PatronSelfRegistration') || $borrowernumber )
51 print $cgi->redirect("/cgi-bin/koha/opac-main.pl");
55 my $action = $cgi->param('action') || q{};
56 if ( $action eq q{} ) {
57 if ($borrowernumber) {
65 my $mandatory = GetMandatoryFields($action);
69 hidden => GetHiddenFields( $mandatory, 'registration' ),
70 mandatory => $mandatory,
71 branches => GetBranchesLoop(),
72 OPACPatronDetails => C4::Context->preference('OPACPatronDetails'),
75 if ( $action eq 'create' ) {
77 my %borrower = ParseCgiForBorrower($cgi);
79 %borrower = DelEmptyFields(%borrower);
81 my @empty_mandatory_fields = CheckMandatoryFields( \%borrower, $action );
82 my $invalidformfields = CheckForInvalidFields(\%borrower);
83 delete $borrower{'password2'};
84 my $cardnumber_error_code;
85 if ( !grep { $_ eq 'cardnumber' } @empty_mandatory_fields ) {
86 # No point in checking the cardnumber if it's missing and mandatory, it'll just generate a
87 # spurious length warning.
88 $cardnumber_error_code = checkcardnumber( $borrower{cardnumber}, $borrower{borrowernumber} );
91 if ( @empty_mandatory_fields || @$invalidformfields || $cardnumber_error_code ) {
92 if ( $cardnumber_error_code == 1 ) {
93 $template->param( cardnumber_already_exists => 1 );
94 } elsif ( $cardnumber_error_code == 2 ) {
95 $template->param( cardnumber_wrong_length => 1 );
99 empty_mandatory_fields => \@empty_mandatory_fields,
100 invalid_form_fields => $invalidformfields,
101 borrower => \%borrower
105 md5_base64( uc( $cgi->param('captcha') ) ) ne $cgi->param('captcha_digest') )
109 borrower => \%borrower
114 C4::Context->boolean_preference(
115 'PatronSelfRegistrationVerifyByEmail')
118 ( $template, $borrowernumber, $cookie ) = get_template_and_user(
120 template_name => "opac-registration-email-sent.tt",
123 authnotrequired => 1,
126 $template->param( 'email' => $borrower{'email'} );
128 my $verification_token = md5_hex( \%borrower );
129 $borrower{'password'} = random_string("..........");
131 Koha::Patron::Modifications->new(
132 verification_token => $verification_token )
133 ->AddModifications(\%borrower);
135 #Send verification email
136 my $letter = C4::Letters::GetPreparedLetter(
138 letter_code => 'OPAC_REG_VERIFY',
140 borrower_modifications => $verification_token,
144 C4::Letters::EnqueueLetter(
147 message_transport_type => 'email',
148 to_address => $borrower{'email'},
150 C4::Context->preference('KohaAdminEmailAddress'),
155 ( $template, $borrowernumber, $cookie ) = get_template_and_user(
157 template_name => "opac-registration-confirmation.tt",
160 authnotrequired => 1,
164 $template->param( OpacPasswordChange =>
165 C4::Context->preference('OpacPasswordChange') );
167 my ( $borrowernumber, $password ) = AddMember_Opac(%borrower);
168 C4::Form::MessagingPreferences::handle_form_action($cgi, { borrowernumber => $borrowernumber }, $template, 1, C4::Context->preference('PatronSelfRegistrationDefaultCategory') ) if $borrowernumber && C4::Context->preference('EnhancedMessagingPreferences');
170 $template->param( password_cleartext => $password );
172 borrower => GetMember( borrowernumber => $borrowernumber ) );
174 PatronSelfRegistrationAdditionalInstructions =>
175 C4::Context->preference(
176 'PatronSelfRegistrationAdditionalInstructions')
181 elsif ( $action eq 'update' ) {
183 my $borrower = GetMember( borrowernumber => $borrowernumber );
184 my $csrf_status = check_csrf_token($borrower->{userid}, md5_base64(C4::Context->config('pass')), scalar $cgi->param('csrf_token'));
185 die "Wrong CSRF token" unless ($csrf_status == CSRF_OK);
187 my %borrower = ParseCgiForBorrower($cgi);
189 my %borrower_changes = DelEmptyFields(%borrower);
190 my @empty_mandatory_fields =
191 CheckMandatoryFields( \%borrower_changes, $action );
192 my $invalidformfields = CheckForInvalidFields(\%borrower);
194 if (@empty_mandatory_fields || @$invalidformfields) {
196 empty_mandatory_fields => \@empty_mandatory_fields,
197 invalid_form_fields => $invalidformfields,
198 borrower => \%borrower,
199 csrf_token => generate_csrf_token($borrower->{userid}, md5_base64(C4::Context->config('pass'))),
202 $template->param( action => 'edit' );
205 my %borrower_changes = DelUnchangedFields( $borrowernumber, %borrower );
206 if (%borrower_changes) {
207 ( $template, $borrowernumber, $cookie ) = get_template_and_user(
209 template_name => "opac-memberentry-update-submitted.tt",
212 authnotrequired => 1,
217 Koha::Patron::Modifications->new(
218 borrowernumber => $borrowernumber );
220 $m->DelModifications;
221 $m->AddModifications(\%borrower_changes);
223 borrower => GetMember( borrowernumber => $borrowernumber ),
230 borrower => GetMember( borrowernumber => $borrowernumber ),
231 csrf_token => generate_csrf_token($borrower->{userid}, md5_base64(C4::Context->config('pass')))
236 elsif ( $action eq 'edit' ) { #Display logged in borrower's data
237 my $borrower = GetMember( borrowernumber => $borrowernumber );
239 if (C4::Context->preference('ExtendedPatronAttributes')) {
240 my $attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber, 'opac');
241 if (scalar(@$attributes) > 0) {
242 $borrower->{ExtendedPatronAttributes} = 1;
243 $borrower->{patron_attributes} = $attributes;
248 borrower => $borrower,
249 guarantor => scalar Koha::Patrons->find($borrowernumber)->guarantor(),
250 hidden => GetHiddenFields( $mandatory, 'modification' ),
251 csrf_token => generate_csrf_token($borrower->{userid}, md5_base64(C4::Context->config('pass')))
254 if (C4::Context->preference('OPACpatronimages')) {
255 my $patron_image = Koha::Patron::Images->find($borrower->{borrowernumber});
256 $template->param( display_patron_image => 1 ) if $patron_image;
261 my $captcha = random_string("CCCCC");
265 captcha_digest => md5_base64($captcha)
268 output_html_with_http_headers $cgi, $cookie, $template->output, undef, { force_no_caching => 1 };
270 sub GetHiddenFields {
271 my ( $mandatory, $action ) = @_;
274 my $BorrowerUnwantedField = $action eq 'modification' ?
275 C4::Context->preference( "PatronSelfModificationBorrowerUnwantedField" ) :
276 C4::Context->preference( "PatronSelfRegistrationBorrowerUnwantedField" );
278 my @fields = split( /\|/, $BorrowerUnwantedField || q|| );
281 #Don't hide mandatory fields
282 next if $mandatory->{$_};
283 $hidden_fields{$_} = 1;
286 return \%hidden_fields;
289 sub GetMandatoryFields {
292 my %mandatory_fields;
294 my $BorrowerMandatoryField =
295 C4::Context->preference("PatronSelfRegistrationBorrowerMandatoryField");
297 my @fields = split( /\|/, $BorrowerMandatoryField );
300 $mandatory_fields{$_} = 1;
303 if ( $action eq 'create' || $action eq 'new' ) {
304 $mandatory_fields{'email'} = 1
305 if C4::Context->boolean_preference(
306 'PatronSelfRegistrationVerifyByEmail');
309 return \%mandatory_fields;
312 sub CheckMandatoryFields {
313 my ( $borrower, $action ) = @_;
315 my @empty_mandatory_fields;
317 my $mandatory_fields = GetMandatoryFields($action);
318 delete $mandatory_fields->{'cardnumber'};
320 foreach my $key ( keys %$mandatory_fields ) {
321 push( @empty_mandatory_fields, $key )
322 unless ( defined( $borrower->{$key} ) && $borrower->{$key} );
325 return @empty_mandatory_fields;
328 sub CheckForInvalidFields {
329 my $minpw = C4::Context->preference('minPasswordLength');
330 my $borrower = shift;
332 if ($borrower->{'email'}) {
333 push(@invalidFields, "email") if (!Email::Valid->address($borrower->{'email'}));
335 if ($borrower->{'emailpro'}) {
336 push(@invalidFields, "emailpro") if (!Email::Valid->address($borrower->{'emailpro'}));
338 if ($borrower->{'B_email'}) {
339 push(@invalidFields, "B_email") if (!Email::Valid->address($borrower->{'B_email'}));
341 if ( $borrower->{'password'} ne $borrower->{'password2'} ){
342 push(@invalidFields, "password_match");
344 if ( $borrower->{'password'} && $minpw && (length($borrower->{'password'}) < $minpw) ) {
345 push(@invalidFields, "password_invalid");
347 if ( $borrower->{'password'} ) {
348 push(@invalidFields, "password_spaces") if ($borrower->{'password'} =~ /^\s/ or $borrower->{'password'} =~ /\s$/);
351 return \@invalidFields;
354 sub ParseCgiForBorrower {
357 my $scrubber = C4::Scrubber->new();
360 foreach ( $cgi->param ) {
361 if ( $_ =~ '^borrower_' ) {
362 my ($key) = substr( $_, 9 );
363 $borrower{$key} = $scrubber->scrub( $cgi->param($_) );
368 $dob_dt = eval { dt_from_string( $borrower{'dateofbirth'} ); }
369 if ( $borrower{'dateofbirth'} );
372 $borrower{'dateofbirth'} = output_pref ( { dt => $dob_dt, dateonly => 1, dateformat => 'iso' } );
376 $borrower{'dateofbirth'} = undef;
382 sub DelUnchangedFields {
383 my ( $borrowernumber, %new_data ) = @_;
385 my $current_data = GetMember( borrowernumber => $borrowernumber );
387 foreach my $key ( keys %new_data ) {
388 if ( $current_data->{$key} eq $new_data{$key} ) {
389 delete $new_data{$key};
399 foreach my $key ( keys %borrower ) {
400 delete $borrower{$key} unless $borrower{$key};