Bug 36520: Sanitize input in opac-sendbasket.pl
[koha.git] / opac / opac-sendbasket.pl
1 #!/usr/bin/perl
2
3 # Copyright Doxulting 2004
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21
22 use CGI qw ( -utf8 );
23 use Encode;
24
25 use C4::Auth   qw( get_template_and_user );
26 use C4::Biblio qw(GetMarcSubjects);
27 use C4::Output qw( output_html_with_http_headers );
28 use C4::Templates;
29 use Koha::Biblios;
30 use Koha::Email;
31 use Koha::Patrons;
32 use Koha::Token;
33
34 my $query = CGI->new;
35
36 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
37     {
38         template_name => "opac-sendbasketform.tt",
39         query         => $query,
40         type          => "opac",
41     }
42 );
43 my $patron     = Koha::Patrons->find($borrowernumber);
44 my $user_email = $patron ? $patron->notice_email_address : undef;
45
46 my $bib_list  = $query->param('bib_list') || '';
47 my $email_add = $query->param('email_add');
48
49 if ( $email_add ) {
50     die "Wrong CSRF token"
51       unless Koha::Token->new->check_csrf(
52         {
53             session_id => scalar $query->cookie('CGISESSID'),
54             token      => scalar $query->param('csrf_token'),
55         }
56       );
57
58     my $comment = $query->param('comment');
59
60     my @bibs = split( /\//, $bib_list );
61     my $iso2709;
62     foreach my $bib (@bibs) {
63         $bib = int($bib);
64         my $biblio = Koha::Biblios->find($bib) or next;
65         $iso2709 .= $biblio->metadata->record->as_usmarc();
66     }
67
68     if ( !defined $iso2709 ) {
69         $template->param( error => 'NO_BODY' );
70     }
71     else {
72         my %loops = ( biblio => \@bibs, );
73
74         my %substitute = ( comment => $comment, );
75
76         my $letter = C4::Letters::GetPreparedLetter(
77             module      => 'catalogue',
78             letter_code => 'CART',
79             lang        => $patron->lang,
80             tables      => {
81                 borrowers => $borrowernumber,
82             },
83             message_transport_type => 'email',
84             loops                  => \%loops,
85             substitute             => \%substitute,
86         );
87
88         my $attachment = {
89             filename => 'basket.iso2709',
90             type     => 'application/octet-stream',
91             content  => Encode::encode( "UTF-8", $iso2709 ),
92         };
93
94         my $message_id = C4::Letters::EnqueueLetter(
95             {
96                 letter                 => $letter,
97                 message_transport_type => 'email',
98                 to_address             => $email_add,
99                 reply_address          => $user_email,
100                 attachments            => [$attachment],
101             }
102         );
103
104         C4::Letters::SendQueuedMessages( { message_id => $message_id } ) if $message_id;
105
106         $template->param( SENT => 1 );
107     }
108
109     $template->param( email_add => $email_add );
110     output_html_with_http_headers $query, $cookie, $template->output, undef,
111       { force_no_caching => 1 };
112
113 } elsif( !$user_email ) {
114     $template->param( email_add => 1, error => 'NO_REPLY_ADDRESS' );
115     output_html_with_http_headers $query, $cookie, $template->output;
116
117 } else {
118     my $new_session_id = $query->cookie('CGISESSID');
119     $template->param(
120         bib_list       => $bib_list,
121         url            => "/cgi-bin/koha/opac-sendbasket.pl",
122         suggestion     => C4::Context->preference("suggestion"),
123         virtualshelves => C4::Context->preference("virtualshelves"),
124         csrf_token =>
125           Koha::Token->new->generate_csrf( { session_id => $new_session_id, } ),
126     );
127     output_html_with_http_headers $query, $cookie, $template->output, undef,
128       { force_no_caching => 1 };
129 }