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
;
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);
70 hidden
=> GetHiddenFields
( $mandatory, 'registration' ),
71 mandatory
=> $mandatory,
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 );
130 $borrower{password
} = random_string
("..........");
131 $borrower{verification_token
} = $verification_token;
133 Koha
::Patron
::Modification
->new( \
%borrower )->store();
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 die "Wrong CSRF token"
185 unless Koha
::Token
->new->check_csrf({
186 id
=> $borrower->{userid
},
187 secret
=> md5_base64
( C4
::Context
->config('pass') ),
188 token
=> scalar $cgi->param('csrf_token'),
191 my %borrower = ParseCgiForBorrower
($cgi);
193 my %borrower_changes = DelEmptyFields
(%borrower);
194 my @empty_mandatory_fields =
195 CheckMandatoryFields
( \
%borrower_changes, $action );
196 my $invalidformfields = CheckForInvalidFields
(\
%borrower);
198 # Send back the data to the template
199 %borrower = ( %$borrower, %borrower );
201 if (@empty_mandatory_fields || @
$invalidformfields) {
203 empty_mandatory_fields
=> \
@empty_mandatory_fields,
204 invalid_form_fields
=> $invalidformfields,
205 borrower
=> \
%borrower,
206 csrf_token
=> Koha
::Token
->new->generate_csrf({
207 id
=> $borrower->{userid
},
208 secret
=> md5_base64
( C4
::Context
->config('pass') ),
212 $template->param( action
=> 'edit' );
215 my %borrower_changes = DelUnchangedFields
( $borrowernumber, %borrower );
216 if (%borrower_changes) {
217 ( $template, $borrowernumber, $cookie ) = get_template_and_user
(
219 template_name
=> "opac-memberentry-update-submitted.tt",
222 authnotrequired
=> 1,
226 $borrower_changes{borrowernumber
} = $borrowernumber;
228 # FIXME update the following with
229 # Koha::Patron::Modifications->search({ borrowernumber => $borrowernumber })->delete;
230 # when bug 17091 will be pushed
231 my $patron_modifications = Koha
::Patron
::Modifications
->search({ borrowernumber
=> $borrowernumber });
232 while ( my $patron_modification = $patron_modifications->next ) {
233 $patron_modification->delete;
236 my $m = Koha
::Patron
::Modification
->new( \
%borrower_changes )->store();
239 borrower
=> GetMember
( borrowernumber
=> $borrowernumber ),
246 borrower
=> GetMember
( borrowernumber
=> $borrowernumber ),
247 csrf_token
=> Koha
::Token
->new->generate_csrf({
248 id
=> $borrower->{userid
},
249 secret
=> md5_base64
( C4
::Context
->config('pass') ),
255 elsif ( $action eq 'edit' ) { #Display logged in borrower's data
256 my $borrower = GetMember
( borrowernumber
=> $borrowernumber );
258 if (C4
::Context
->preference('ExtendedPatronAttributes')) {
259 my $attributes = C4
::Members
::Attributes
::GetBorrowerAttributes
($borrowernumber, 'opac');
260 if (scalar(@
$attributes) > 0) {
261 $borrower->{ExtendedPatronAttributes
} = 1;
262 $borrower->{patron_attributes
} = $attributes;
267 borrower
=> $borrower,
268 guarantor
=> scalar Koha
::Patrons
->find($borrowernumber)->guarantor(),
269 hidden
=> GetHiddenFields
( $mandatory, 'modification' ),
270 csrf_token
=> Koha
::Token
->new->generate_csrf({
271 id
=> $borrower->{userid
},
272 secret
=> md5_base64
( C4
::Context
->config('pass') ),
276 if (C4
::Context
->preference('OPACpatronimages')) {
277 my $patron_image = Koha
::Patron
::Images
->find($borrower->{borrowernumber
});
278 $template->param( display_patron_image
=> 1 ) if $patron_image;
283 my $captcha = random_string
("CCCCC");
287 captcha_digest
=> md5_base64
($captcha)
290 output_html_with_http_headers
$cgi, $cookie, $template->output, undef, { force_no_caching
=> 1 };
292 sub GetHiddenFields
{
293 my ( $mandatory, $action ) = @_;
296 my $BorrowerUnwantedField = $action eq 'modification' ?
297 C4
::Context
->preference( "PatronSelfModificationBorrowerUnwantedField" ) :
298 C4
::Context
->preference( "PatronSelfRegistrationBorrowerUnwantedField" );
300 my @fields = split( /\|/, $BorrowerUnwantedField || q
|| );
303 #Don't hide mandatory fields
304 next if $mandatory->{$_};
305 $hidden_fields{$_} = 1;
308 return \
%hidden_fields;
311 sub GetMandatoryFields
{
314 my %mandatory_fields;
316 my $BorrowerMandatoryField =
317 C4
::Context
->preference("PatronSelfRegistrationBorrowerMandatoryField");
319 my @fields = split( /\|/, $BorrowerMandatoryField );
322 $mandatory_fields{$_} = 1;
325 if ( $action eq 'create' || $action eq 'new' ) {
326 $mandatory_fields{'email'} = 1
327 if C4
::Context
->boolean_preference(
328 'PatronSelfRegistrationVerifyByEmail');
331 return \
%mandatory_fields;
334 sub CheckMandatoryFields
{
335 my ( $borrower, $action ) = @_;
337 my @empty_mandatory_fields;
339 my $mandatory_fields = GetMandatoryFields
($action);
340 delete $mandatory_fields->{'cardnumber'};
342 foreach my $key ( keys %$mandatory_fields ) {
343 push( @empty_mandatory_fields, $key )
344 unless ( defined( $borrower->{$key} ) && $borrower->{$key} );
347 return @empty_mandatory_fields;
350 sub CheckForInvalidFields
{
351 my $minpw = C4
::Context
->preference('minPasswordLength');
352 my $borrower = shift;
354 if ($borrower->{'email'}) {
355 unless ( Email
::Valid
->address($borrower->{'email'}) ) {
356 push(@invalidFields, "email");
357 } elsif ( C4
::Context
->preference("PatronSelfRegistrationEmailMustBeUnique") ) {
358 my $patrons_with_same_email = Koha
::Patrons
->search( { email
=> $borrower->{email
} })->count;
359 if ( $patrons_with_same_email ) {
360 push @invalidFields, "duplicate_email";
364 if ($borrower->{'emailpro'}) {
365 push(@invalidFields, "emailpro") if (!Email
::Valid
->address($borrower->{'emailpro'}));
367 if ($borrower->{'B_email'}) {
368 push(@invalidFields, "B_email") if (!Email
::Valid
->address($borrower->{'B_email'}));
370 if ( $borrower->{'password'} ne $borrower->{'password2'} ){
371 push(@invalidFields, "password_match");
373 if ( $borrower->{'password'} && $minpw && (length($borrower->{'password'}) < $minpw) ) {
374 push(@invalidFields, "password_invalid");
376 if ( $borrower->{'password'} ) {
377 push(@invalidFields, "password_spaces") if ($borrower->{'password'} =~ /^\s/ or $borrower->{'password'} =~ /\s$/);
380 return \
@invalidFields;
383 sub ParseCgiForBorrower
{
386 my $scrubber = C4
::Scrubber
->new();
389 foreach ( $cgi->param ) {
390 if ( $_ =~ '^borrower_' ) {
391 my ($key) = substr( $_, 9 );
392 $borrower{$key} = HTML
::Entities
::encode
( $scrubber->scrub( scalar $cgi->param($_) ) );
397 $dob_dt = eval { dt_from_string
( $borrower{'dateofbirth'} ); }
398 if ( $borrower{'dateofbirth'} );
401 $borrower{'dateofbirth'} = output_pref
( { dt
=> $dob_dt, dateonly
=> 1, dateformat
=> 'iso' } );
405 $borrower{'dateofbirth'} = undef;
411 sub DelUnchangedFields
{
412 my ( $borrowernumber, %new_data ) = @_;
414 my $current_data = GetMember
( borrowernumber
=> $borrowernumber );
416 foreach my $key ( keys %new_data ) {
417 if ( $current_data->{$key} eq $new_data{$key} ) {
418 delete $new_data{$key};
428 foreach my $key ( keys %borrower ) {
429 delete $borrower{$key} unless $borrower{$key};