Bug 34623: Update jQuery-validate plugin to 1.20.0
[koha.git] / opac / opac-registration-verify.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
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.
9 #
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.
14 #
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>.
17
18 use Modern::Perl;
19
20 use CGI qw ( -utf8 );
21 use Try::Tiny;
22
23 use C4::Auth qw( get_template_and_user );
24 use C4::Context;
25 use C4::Output qw( output_html_with_http_headers );
26 use C4::Letters qw( GetPreparedLetter EnqueueLetter SendQueuedMessages );
27 use C4::Members;
28 use C4::Form::MessagingPreferences;
29 use Koha::AuthUtils;
30 use Koha::Patrons;
31 use Koha::Patron::Consent;
32 use Koha::Patron::Modifications;
33 use Koha::Patron::Categories;
34
35 my $cgi = CGI->new;
36 my $dbh = C4::Context->dbh;
37
38 unless ( C4::Context->preference('PatronSelfRegistration') ) {
39     print $cgi->redirect("/cgi-bin/koha/opac-main.pl");
40     exit;
41 }
42
43 my $token = $cgi->param('token');
44 my $op = $cgi->param('op');
45 my $confirmed;
46 if ( $op && $op eq 'confirmed' ) {
47     $confirmed = 1;
48 }
49 my $m = Koha::Patron::Modifications->find( { verification_token => $token } );
50
51 my ( $template, $borrowernumber, $cookie );
52 my ( $error_type, $error_info );
53
54 my $rego_found;
55 if (
56     $m    # The token exists and the email is unique if requested
57     and not(C4::Context->preference('PatronSelfRegistrationEmailMustBeUnique')
58         and Koha::Patrons->search( { email => $m->email } )->count )
59     )
60 {
61     $rego_found = 1;
62 }
63
64 if ( $rego_found
65     and !$confirmed )
66 {
67     ( $template, $borrowernumber, $cookie ) = get_template_and_user(
68         {
69             template_name   => "opac-registration-confirmation.tt",
70             type            => "opac",
71             query           => $cgi,
72             authnotrequired => C4::Context->preference("OpacPublic") ? 1 : 0,
73         }
74     );
75     $template->param( "token" => $token );
76 }
77 elsif ( $rego_found
78     and $confirmed )
79 {
80     my $patron_attrs = $m->unblessed;
81     $patron_attrs->{password} ||= Koha::AuthUtils::generate_password(Koha::Patron::Categories->find($patron_attrs->{categorycode}));
82     my $consent_dt = delete $patron_attrs->{gdpr_proc_consent};
83     $patron_attrs->{categorycode} ||= C4::Context->preference('PatronSelfRegistrationDefaultCategory');
84     delete $patron_attrs->{timestamp};
85     delete $patron_attrs->{verification_token};
86     delete $patron_attrs->{changed_fields};
87     delete $patron_attrs->{extended_attributes};
88
89     my $patron;
90     try {
91         $patron = Koha::Patron->new( $patron_attrs )->store;
92         Koha::Patron::Consent->new({ borrowernumber => $patron->borrowernumber, type => 'GDPR_PROCESSING', given_on => $consent_dt })->store if $patron && $consent_dt;
93     } catch {
94         $error_type = ref($_);
95         $error_info = "$_";
96     };
97
98     if ($patron) {
99         if( $m->extended_attributes ){
100             $m->borrowernumber( $patron->borrowernumber);
101             $m->changed_fields(['extended_attributes']);
102             $m->approve();
103         } else {
104             $m->delete();
105         }
106         ( $template, $borrowernumber, $cookie ) = get_template_and_user(
107             {
108                 template_name   => "opac-registration-confirmation.tt",
109                 type            => "opac",
110                 query           => $cgi,
111                 authnotrequired => C4::Context->preference("OpacPublic") ? 1 : 0,
112             }
113         );
114         $template->param( "confirmed" => 1 );
115         C4::Form::MessagingPreferences::handle_form_action($cgi, { borrowernumber => $patron->borrowernumber }, $template, 1, C4::Context->preference('PatronSelfRegistrationDefaultCategory') ) if C4::Context->preference('EnhancedMessagingPreferences');
116
117         $template->param( password_cleartext => $patron->plain_text_password );
118         $template->param( borrower => $patron );
119
120         # If 'AutoEmailNewUser' syspref is on, email user their account details from the 'notice' that matches the user's branchcode.
121         if ( C4::Context->preference("AutoEmailNewUser") ) {
122             # Look up correct email address taking EmailFieldPrimary into account
123             my $emailaddr = $patron->notice_email_address;
124             # if we manage to find a valid email address, send notice
125             if ($emailaddr) {
126                 eval {
127                     my $letter = GetPreparedLetter(
128                         module      => 'members',
129                         letter_code => 'WELCOME',
130                         branchcode  => $patron->branchcode,,
131                         lang        => $patron->lang || 'default',
132                         tables      => {
133                             'branches'  => $patron->branchcode,
134                             'borrowers' => $patron->borrowernumber,
135                         },
136                         want_librarian => 1,
137                     ) or return;
138
139                     my $message_id = EnqueueLetter(
140                         {
141                             letter                 => $letter,
142                             borrowernumber         => $patron->id,
143                             to_address             => $emailaddr,
144                             message_transport_type => 'email'
145                         }
146                     );
147                     SendQueuedMessages( { message_id => $message_id } ) if $message_id;
148                 };
149             }
150         }
151
152         # Notify library of new patron registration
153         my $notify_library = C4::Context->preference("EmailPatronRegistrations");
154         if ($notify_library) {
155             $patron->notify_library_of_registration($notify_library);
156         }
157
158         $template->param(
159             PatronSelfRegistrationAdditionalInstructions =>
160               C4::Context->preference(
161                 'PatronSelfRegistrationAdditionalInstructions')
162         );
163
164         my ($theme, $news_lang, $availablethemes) = C4::Templates::themelanguage(C4::Context->config('opachtdocs'),'opac-registration-confirmation.tt','opac',$cgi);
165         $template->param( news_lang => $news_lang );
166     }
167 }
168
169 if( !$template ) { # Missing token, patron exception, etc.
170     ( $template, $borrowernumber, $cookie ) = get_template_and_user({
171         template_name   => "opac-registration-invalid.tt",
172         type            => "opac",
173         query           => $cgi,
174         authnotrequired => C4::Context->preference("OpacPublic") ? 1 : 0,
175     });
176     $template->param( error_type => $error_type, error_info => $error_info );
177 }
178
179 output_html_with_http_headers $cgi, $cookie, $template->output;