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 );
28 use C4::Form::MessagingPreferences;
30 use Koha::Patron::Modification;
31 use Koha::Patron::Modifications;
32 use C4::Branch qw(GetBranchesLoop);
36 use Koha::Patron::Images;
40 my $dbh = C4::Context->dbh;
42 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
44 template_name => "opac-memberentry.tt",
51 unless ( C4::Context->preference('PatronSelfRegistration') || $borrowernumber )
53 print $cgi->redirect("/cgi-bin/koha/opac-main.pl");
57 my $action = $cgi->param('action') || q{};
58 if ( $action eq q{} ) {
59 if ($borrowernumber) {
67 my $mandatory = GetMandatoryFields($action);
71 hidden => GetHiddenFields( $mandatory, 'registration' ),
72 mandatory => $mandatory,
73 branches => GetBranchesLoop(),
74 OPACPatronDetails => C4::Context->preference('OPACPatronDetails'),
77 if ( $action eq 'create' ) {
79 my %borrower = ParseCgiForBorrower($cgi);
81 %borrower = DelEmptyFields(%borrower);
83 my @empty_mandatory_fields = CheckMandatoryFields( \%borrower, $action );
84 my $invalidformfields = CheckForInvalidFields(\%borrower);
85 delete $borrower{'password2'};
86 my $cardnumber_error_code;
87 if ( !grep { $_ eq 'cardnumber' } @empty_mandatory_fields ) {
88 # No point in checking the cardnumber if it's missing and mandatory, it'll just generate a
89 # spurious length warning.
90 $cardnumber_error_code = checkcardnumber( $borrower{cardnumber}, $borrower{borrowernumber} );
93 if ( @empty_mandatory_fields || @$invalidformfields || $cardnumber_error_code ) {
94 if ( $cardnumber_error_code == 1 ) {
95 $template->param( cardnumber_already_exists => 1 );
96 } elsif ( $cardnumber_error_code == 2 ) {
97 $template->param( cardnumber_wrong_length => 1 );
101 empty_mandatory_fields => \@empty_mandatory_fields,
102 invalid_form_fields => $invalidformfields,
103 borrower => \%borrower
107 md5_base64( uc( $cgi->param('captcha') ) ) ne $cgi->param('captcha_digest') )
111 borrower => \%borrower
116 C4::Context->boolean_preference(
117 'PatronSelfRegistrationVerifyByEmail')
120 ( $template, $borrowernumber, $cookie ) = get_template_and_user(
122 template_name => "opac-registration-email-sent.tt",
125 authnotrequired => 1,
128 $template->param( 'email' => $borrower{'email'} );
130 my $verification_token = md5_hex( \%borrower );
132 $borrower{password} = random_string("..........");
133 $borrower{verification_token} = $verification_token;
135 Koha::Patron::Modification->new( \%borrower )->store();
137 #Send verification email
138 my $letter = C4::Letters::GetPreparedLetter(
140 letter_code => 'OPAC_REG_VERIFY',
142 borrower_modifications => $verification_token,
146 C4::Letters::EnqueueLetter(
149 message_transport_type => 'email',
150 to_address => $borrower{'email'},
152 C4::Context->preference('KohaAdminEmailAddress'),
157 ( $template, $borrowernumber, $cookie ) = get_template_and_user(
159 template_name => "opac-registration-confirmation.tt",
162 authnotrequired => 1,
166 $template->param( OpacPasswordChange =>
167 C4::Context->preference('OpacPasswordChange') );
169 my ( $borrowernumber, $password ) = AddMember_Opac(%borrower);
170 C4::Form::MessagingPreferences::handle_form_action($cgi, { borrowernumber => $borrowernumber }, $template, 1, C4::Context->preference('PatronSelfRegistrationDefaultCategory') ) if $borrowernumber && C4::Context->preference('EnhancedMessagingPreferences');
172 $template->param( password_cleartext => $password );
174 borrower => GetMember( borrowernumber => $borrowernumber ) );
176 PatronSelfRegistrationAdditionalInstructions =>
177 C4::Context->preference(
178 'PatronSelfRegistrationAdditionalInstructions')
183 elsif ( $action eq 'update' ) {
185 my $borrower = GetMember( borrowernumber => $borrowernumber );
186 die "Wrong CSRF token"
187 unless Koha::Token->new->check_csrf({
188 id => $borrower->{userid},
189 secret => md5_base64( C4::Context->config('pass') ),
190 token => scalar $cgi->param('csrf_token'),
193 my %borrower = ParseCgiForBorrower($cgi);
195 my %borrower_changes = DelEmptyFields(%borrower);
196 my @empty_mandatory_fields =
197 CheckMandatoryFields( \%borrower_changes, $action );
198 my $invalidformfields = CheckForInvalidFields(\%borrower);
200 # Send back the data to the template
201 %borrower = ( %$borrower, %borrower );
203 if (@empty_mandatory_fields || @$invalidformfields) {
205 empty_mandatory_fields => \@empty_mandatory_fields,
206 invalid_form_fields => $invalidformfields,
207 borrower => \%borrower,
208 csrf_token => Koha::Token->new->generate_csrf({
209 id => $borrower->{userid},
210 secret => md5_base64( C4::Context->config('pass') ),
214 $template->param( action => 'edit' );
217 my %borrower_changes = DelUnchangedFields( $borrowernumber, %borrower );
218 if (%borrower_changes) {
219 ( $template, $borrowernumber, $cookie ) = get_template_and_user(
221 template_name => "opac-memberentry-update-submitted.tt",
224 authnotrequired => 1,
228 $borrower_changes{borrowernumber} = $borrowernumber;
230 my $m = Koha::Patron::Modification->new( \%borrower_changes )->store();
233 borrower => GetMember( borrowernumber => $borrowernumber ),
240 borrower => GetMember( borrowernumber => $borrowernumber ),
241 csrf_token => Koha::Token->new->generate_csrf({
242 id => $borrower->{userid},
243 secret => md5_base64( C4::Context->config('pass') ),
249 elsif ( $action eq 'edit' ) { #Display logged in borrower's data
250 my $borrower = GetMember( borrowernumber => $borrowernumber );
252 if (C4::Context->preference('ExtendedPatronAttributes')) {
253 my $attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber, 'opac');
254 if (scalar(@$attributes) > 0) {
255 $borrower->{ExtendedPatronAttributes} = 1;
256 $borrower->{patron_attributes} = $attributes;
261 borrower => $borrower,
262 guarantor => scalar Koha::Patrons->find($borrowernumber)->guarantor(),
263 hidden => GetHiddenFields( $mandatory, 'modification' ),
264 csrf_token => Koha::Token->new->generate_csrf({
265 id => $borrower->{userid},
266 secret => md5_base64( C4::Context->config('pass') ),
270 if (C4::Context->preference('OPACpatronimages')) {
271 my $patron_image = Koha::Patron::Images->find($borrower->{borrowernumber});
272 $template->param( display_patron_image => 1 ) if $patron_image;
277 my $captcha = random_string("CCCCC");
281 captcha_digest => md5_base64($captcha)
284 output_html_with_http_headers $cgi, $cookie, $template->output, undef, { force_no_caching => 1 };
286 sub GetHiddenFields {
287 my ( $mandatory, $action ) = @_;
290 my $BorrowerUnwantedField = $action eq 'modification' ?
291 C4::Context->preference( "PatronSelfModificationBorrowerUnwantedField" ) :
292 C4::Context->preference( "PatronSelfRegistrationBorrowerUnwantedField" );
294 my @fields = split( /\|/, $BorrowerUnwantedField || q|| );
297 #Don't hide mandatory fields
298 next if $mandatory->{$_};
299 $hidden_fields{$_} = 1;
302 return \%hidden_fields;
305 sub GetMandatoryFields {
308 my %mandatory_fields;
310 my $BorrowerMandatoryField =
311 C4::Context->preference("PatronSelfRegistrationBorrowerMandatoryField");
313 my @fields = split( /\|/, $BorrowerMandatoryField );
316 $mandatory_fields{$_} = 1;
319 if ( $action eq 'create' || $action eq 'new' ) {
320 $mandatory_fields{'email'} = 1
321 if C4::Context->boolean_preference(
322 'PatronSelfRegistrationVerifyByEmail');
325 return \%mandatory_fields;
328 sub CheckMandatoryFields {
329 my ( $borrower, $action ) = @_;
331 my @empty_mandatory_fields;
333 my $mandatory_fields = GetMandatoryFields($action);
334 delete $mandatory_fields->{'cardnumber'};
336 foreach my $key ( keys %$mandatory_fields ) {
337 push( @empty_mandatory_fields, $key )
338 unless ( defined( $borrower->{$key} ) && $borrower->{$key} );
341 return @empty_mandatory_fields;
344 sub CheckForInvalidFields {
345 my $minpw = C4::Context->preference('minPasswordLength');
346 my $borrower = shift;
348 if ($borrower->{'email'}) {
349 push(@invalidFields, "email") if (!Email::Valid->address($borrower->{'email'}));
351 if ($borrower->{'emailpro'}) {
352 push(@invalidFields, "emailpro") if (!Email::Valid->address($borrower->{'emailpro'}));
354 if ($borrower->{'B_email'}) {
355 push(@invalidFields, "B_email") if (!Email::Valid->address($borrower->{'B_email'}));
357 if ( $borrower->{'password'} ne $borrower->{'password2'} ){
358 push(@invalidFields, "password_match");
360 if ( $borrower->{'password'} && $minpw && (length($borrower->{'password'}) < $minpw) ) {
361 push(@invalidFields, "password_invalid");
363 if ( $borrower->{'password'} ) {
364 push(@invalidFields, "password_spaces") if ($borrower->{'password'} =~ /^\s/ or $borrower->{'password'} =~ /\s$/);
367 return \@invalidFields;
370 sub ParseCgiForBorrower {
373 my $scrubber = C4::Scrubber->new();
376 foreach ( $cgi->param ) {
377 if ( $_ =~ '^borrower_' ) {
378 my ($key) = substr( $_, 9 );
379 $borrower{$key} = HTML::Entities::encode( $scrubber->scrub( scalar $cgi->param($_) ) );
384 $dob_dt = eval { dt_from_string( $borrower{'dateofbirth'} ); }
385 if ( $borrower{'dateofbirth'} );
388 $borrower{'dateofbirth'} = output_pref ( { dt => $dob_dt, dateonly => 1, dateformat => 'iso' } );
392 $borrower{'dateofbirth'} = undef;
398 sub DelUnchangedFields {
399 my ( $borrowernumber, %new_data ) = @_;
401 my $current_data = GetMember( borrowernumber => $borrowernumber );
403 foreach my $key ( keys %new_data ) {
404 if ( $current_data->{$key} eq $new_data{$key} ) {
405 delete $new_data{$key};
415 foreach my $key ( keys %borrower ) {
416 delete $borrower{$key} unless $borrower{$key};