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 );
27 use C4::Form::MessagingPreferences;
29 use Koha::Patron::Modification;
30 use Koha::Patron::Modifications;
35 use Koha::Patron::Images;
39 my $dbh = C4::Context->dbh;
41 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
43 template_name => "opac-memberentry.tt",
50 unless ( C4::Context->preference('PatronSelfRegistration') || $borrowernumber )
52 print $cgi->redirect("/cgi-bin/koha/opac-main.pl");
56 my $action = $cgi->param('action') || q{};
57 if ( $action eq q{} ) {
58 if ($borrowernumber) {
66 my $mandatory = GetMandatoryFields($action);
68 my @libraries = Koha::Libraries->search;
69 if ( my @libraries_to_display = split '\|', C4::Context->preference('PatronSelfRegistrationLibraryList') ) {
70 @libraries = map { my $b = $_; my $branchcode = $_->branchcode; grep( /^$branchcode$/, @libraries_to_display ) ? $b : () } @libraries;
72 my ( $min, $max ) = C4::Members::get_cardnumber_length();
75 minlength_cardnumber => $min,
76 maxlength_cardnumber => $max
82 hidden => GetHiddenFields( $mandatory, 'registration' ),
83 mandatory => $mandatory,
84 libraries => \@libraries,
85 OPACPatronDetails => C4::Context->preference('OPACPatronDetails'),
88 if ( $action eq 'create' ) {
90 my %borrower = ParseCgiForBorrower($cgi);
92 %borrower = DelEmptyFields(%borrower);
94 my @empty_mandatory_fields = CheckMandatoryFields( \%borrower, $action );
95 my $invalidformfields = CheckForInvalidFields(\%borrower);
96 delete $borrower{'password2'};
97 my $cardnumber_error_code;
98 if ( !grep { $_ eq 'cardnumber' } @empty_mandatory_fields ) {
99 # No point in checking the cardnumber if it's missing and mandatory, it'll just generate a
100 # spurious length warning.
101 $cardnumber_error_code = checkcardnumber( $borrower{cardnumber}, $borrower{borrowernumber} );
104 if ( @empty_mandatory_fields || @$invalidformfields || $cardnumber_error_code ) {
105 if ( $cardnumber_error_code == 1 ) {
106 $template->param( cardnumber_already_exists => 1 );
107 } elsif ( $cardnumber_error_code == 2 ) {
108 $template->param( cardnumber_wrong_length => 1 );
112 empty_mandatory_fields => \@empty_mandatory_fields,
113 invalid_form_fields => $invalidformfields,
114 borrower => \%borrower
118 md5_base64( uc( $cgi->param('captcha') ) ) ne $cgi->param('captcha_digest') )
122 borrower => \%borrower
127 C4::Context->boolean_preference(
128 'PatronSelfRegistrationVerifyByEmail')
131 ( $template, $borrowernumber, $cookie ) = get_template_and_user(
133 template_name => "opac-registration-email-sent.tt",
136 authnotrequired => 1,
139 $template->param( 'email' => $borrower{'email'} );
141 my $verification_token = md5_hex( time().{}.rand().{}.$$ );
142 while ( Koha::Patron::Modifications->search( { verification_token => $verification_token } )->count() ) {
143 $verification_token = md5_hex( time().{}.rand().{}.$$ );
146 $borrower{password} = random_string("..........");
147 $borrower{verification_token} = $verification_token;
149 Koha::Patron::Modification->new( \%borrower )->store();
151 #Send verification email
152 my $letter = C4::Letters::GetPreparedLetter(
154 letter_code => 'OPAC_REG_VERIFY',
156 borrower_modifications => $verification_token,
160 C4::Letters::EnqueueLetter(
163 message_transport_type => 'email',
164 to_address => $borrower{'email'},
166 C4::Context->preference('KohaAdminEmailAddress'),
171 ( $template, $borrowernumber, $cookie ) = get_template_and_user(
173 template_name => "opac-registration-confirmation.tt",
176 authnotrequired => 1,
180 $template->param( OpacPasswordChange =>
181 C4::Context->preference('OpacPasswordChange') );
183 my ( $borrowernumber, $password ) = AddMember_Opac(%borrower);
184 C4::Form::MessagingPreferences::handle_form_action($cgi, { borrowernumber => $borrowernumber }, $template, 1, C4::Context->preference('PatronSelfRegistrationDefaultCategory') ) if $borrowernumber && C4::Context->preference('EnhancedMessagingPreferences');
186 $template->param( password_cleartext => $password );
188 borrower => GetMember( borrowernumber => $borrowernumber ) );
190 PatronSelfRegistrationAdditionalInstructions =>
191 C4::Context->preference(
192 'PatronSelfRegistrationAdditionalInstructions')
197 elsif ( $action eq 'update' ) {
199 my $borrower = GetMember( borrowernumber => $borrowernumber );
200 die "Wrong CSRF token"
201 unless Koha::Token->new->check_csrf({
202 id => $borrower->{userid},
203 secret => md5_base64( C4::Context->config('pass') ),
204 token => scalar $cgi->param('csrf_token'),
207 my %borrower = ParseCgiForBorrower($cgi);
209 my %borrower_changes = DelEmptyFields(%borrower);
210 my @empty_mandatory_fields =
211 CheckMandatoryFields( \%borrower_changes, $action );
212 my $invalidformfields = CheckForInvalidFields(\%borrower);
214 # Send back the data to the template
215 %borrower = ( %$borrower, %borrower );
217 if (@empty_mandatory_fields || @$invalidformfields) {
219 empty_mandatory_fields => \@empty_mandatory_fields,
220 invalid_form_fields => $invalidformfields,
221 borrower => \%borrower,
222 csrf_token => Koha::Token->new->generate_csrf({
223 id => $borrower->{userid},
224 secret => md5_base64( C4::Context->config('pass') ),
228 $template->param( action => 'edit' );
231 my %borrower_changes = DelUnchangedFields( $borrowernumber, %borrower );
232 if (%borrower_changes) {
233 ( $template, $borrowernumber, $cookie ) = get_template_and_user(
235 template_name => "opac-memberentry-update-submitted.tt",
238 authnotrequired => 1,
242 $borrower_changes{borrowernumber} = $borrowernumber;
244 # FIXME update the following with
245 # Koha::Patron::Modifications->search({ borrowernumber => $borrowernumber })->delete;
246 # when bug 17091 will be pushed
247 my $patron_modifications = Koha::Patron::Modifications->search({ borrowernumber => $borrowernumber });
248 while ( my $patron_modification = $patron_modifications->next ) {
249 $patron_modification->delete;
252 my $m = Koha::Patron::Modification->new( \%borrower_changes )->store();
255 borrower => GetMember( borrowernumber => $borrowernumber ),
262 borrower => GetMember( borrowernumber => $borrowernumber ),
263 csrf_token => Koha::Token->new->generate_csrf({
264 id => $borrower->{userid},
265 secret => md5_base64( C4::Context->config('pass') ),
271 elsif ( $action eq 'edit' ) { #Display logged in borrower's data
272 my $borrower = GetMember( borrowernumber => $borrowernumber );
274 if (C4::Context->preference('ExtendedPatronAttributes')) {
275 my $attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber, 'opac');
276 if (scalar(@$attributes) > 0) {
277 $borrower->{ExtendedPatronAttributes} = 1;
278 $borrower->{patron_attributes} = $attributes;
283 borrower => $borrower,
284 guarantor => scalar Koha::Patrons->find($borrowernumber)->guarantor(),
285 hidden => GetHiddenFields( $mandatory, 'modification' ),
286 csrf_token => Koha::Token->new->generate_csrf({
287 id => $borrower->{userid},
288 secret => md5_base64( C4::Context->config('pass') ),
292 if (C4::Context->preference('OPACpatronimages')) {
293 my $patron_image = Koha::Patron::Images->find($borrower->{borrowernumber});
294 $template->param( display_patron_image => 1 ) if $patron_image;
299 my $captcha = random_string("CCCCC");
303 captcha_digest => md5_base64($captcha)
306 output_html_with_http_headers $cgi, $cookie, $template->output, undef, { force_no_caching => 1 };
308 sub GetHiddenFields {
309 my ( $mandatory, $action ) = @_;
312 my $BorrowerUnwantedField = $action eq 'modification' ?
313 C4::Context->preference( "PatronSelfModificationBorrowerUnwantedField" ) :
314 C4::Context->preference( "PatronSelfRegistrationBorrowerUnwantedField" );
316 my @fields = split( /\|/, $BorrowerUnwantedField || q|| );
319 #Don't hide mandatory fields
320 next if $mandatory->{$_};
321 $hidden_fields{$_} = 1;
324 return \%hidden_fields;
327 sub GetMandatoryFields {
330 my %mandatory_fields;
332 my $BorrowerMandatoryField =
333 C4::Context->preference("PatronSelfRegistrationBorrowerMandatoryField");
335 my @fields = split( /\|/, $BorrowerMandatoryField );
338 $mandatory_fields{$_} = 1;
341 if ( $action eq 'create' || $action eq 'new' ) {
342 $mandatory_fields{'email'} = 1
343 if C4::Context->boolean_preference(
344 'PatronSelfRegistrationVerifyByEmail');
347 return \%mandatory_fields;
350 sub CheckMandatoryFields {
351 my ( $borrower, $action ) = @_;
353 my @empty_mandatory_fields;
355 my $mandatory_fields = GetMandatoryFields($action);
356 delete $mandatory_fields->{'cardnumber'};
358 foreach my $key ( keys %$mandatory_fields ) {
359 push( @empty_mandatory_fields, $key )
360 unless ( defined( $borrower->{$key} ) && $borrower->{$key} );
363 return @empty_mandatory_fields;
366 sub CheckForInvalidFields {
367 my $minpw = C4::Context->preference('minPasswordLength');
368 my $borrower = shift;
370 if ($borrower->{'email'}) {
371 unless ( Email::Valid->address($borrower->{'email'}) ) {
372 push(@invalidFields, "email");
373 } elsif ( C4::Context->preference("PatronSelfRegistrationEmailMustBeUnique") ) {
374 my $patrons_with_same_email = Koha::Patrons->search( { email => $borrower->{email} })->count;
375 if ( $patrons_with_same_email ) {
376 push @invalidFields, "duplicate_email";
380 if ($borrower->{'emailpro'}) {
381 push(@invalidFields, "emailpro") if (!Email::Valid->address($borrower->{'emailpro'}));
383 if ($borrower->{'B_email'}) {
384 push(@invalidFields, "B_email") if (!Email::Valid->address($borrower->{'B_email'}));
386 if ( $borrower->{'password'} ne $borrower->{'password2'} ){
387 push(@invalidFields, "password_match");
389 if ( $borrower->{'password'} && $minpw && (length($borrower->{'password'}) < $minpw) ) {
390 push(@invalidFields, "password_invalid");
392 if ( $borrower->{'password'} ) {
393 push(@invalidFields, "password_spaces") if ($borrower->{'password'} =~ /^\s/ or $borrower->{'password'} =~ /\s$/);
396 return \@invalidFields;
399 sub ParseCgiForBorrower {
402 my $scrubber = C4::Scrubber->new();
405 foreach ( $cgi->param ) {
406 if ( $_ =~ '^borrower_' ) {
407 my ($key) = substr( $_, 9 );
408 $borrower{$key} = $scrubber->scrub( scalar $cgi->param($_) );
413 $dob_dt = eval { dt_from_string( $borrower{'dateofbirth'} ); }
414 if ( $borrower{'dateofbirth'} );
417 $borrower{'dateofbirth'} = output_pref ( { dt => $dob_dt, dateonly => 1, dateformat => 'iso' } );
421 $borrower{'dateofbirth'} = undef;
427 sub DelUnchangedFields {
428 my ( $borrowernumber, %new_data ) = @_;
430 my $current_data = GetMember( borrowernumber => $borrowernumber );
432 foreach my $key ( keys %new_data ) {
433 if ( $current_data->{$key} eq $new_data{$key} ) {
434 delete $new_data{$key};
444 foreach my $key ( keys %borrower ) {
445 delete $borrower{$key} unless $borrower{$key};