Bug 17109: Add CSRF token to [opac-]sendbasket
[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 qw(encode);
24 use Carp;
25 use Digest::MD5 qw(md5_base64);
26 use Mail::Sendmail;
27 use MIME::QuotedPrint;
28 use MIME::Base64;
29
30 use C4::Biblio;
31 use C4::Items;
32 use C4::Auth;
33 use C4::Output;
34 use C4::Members;
35 use C4::Templates ();
36 use Koha::Email;
37 use Koha::Token;
38
39 my $query = new CGI;
40
41 my ( $template, $borrowernumber, $cookie ) = get_template_and_user (
42     {
43         template_name   => "opac-sendbasketform.tt",
44         query           => $query,
45         type            => "opac",
46         authnotrequired => 0,
47     }
48 );
49
50 my $bib_list     = $query->param('bib_list') || '';
51 my $email_add    = $query->param('email_add');
52
53 my $dbh          = C4::Context->dbh;
54
55 my $csrf_err;
56 if ( $email_add ) {
57     $csrf_err = 1 unless Koha::Token->new->check_csrf({
58         id     => C4::Context->userenv->{id},
59         secret => md5_base64( C4::Context->config('pass') ),
60         token  => scalar $query->param('csrf_token'),
61     });
62 }
63
64 if( $csrf_err ) {
65     $template->param( csrf_error => 1, email_add => 1 );
66     output_html_with_http_headers $query, $cookie, $template->output;
67 } elsif ( $email_add ) {
68     my $email = Koha::Email->new();
69     my $user = GetMember(borrowernumber => $borrowernumber);
70     my $user_email = GetFirstValidEmailAddress($borrowernumber)
71     || C4::Context->preference('KohaAdminEmailAddress');
72
73     my $email_replyto = "$user->{firstname} $user->{surname} <$user_email>";
74     my $comment    = $query->param('comment');
75
76    # if you want to use the KohaAdmin address as from, that is the default no need to set it
77     my %mail = $email->create_message_headers({
78         to => $email_add,
79         replyto => $email_replyto,
80     });
81     $mail{'X-Abuse-Report'} = C4::Context->preference('KohaAdminEmailAddress');
82
83     # Since we are already logged in, no need to check credentials again
84     # We only need to add OPACBaseURL
85     my $template2 = C4::Templates::gettemplate(
86         'opac-sendbasket.tt', 'opac', $query,
87     );
88     $template2->param( OPACBaseURL => C4::Context->preference('OPACBaseURL') );
89
90     my @bibs = split( /\//, $bib_list );
91     my @results;
92     my $iso2709;
93     my $marcflavour = C4::Context->preference('marcflavour');
94     foreach my $biblionumber (@bibs) {
95         $template2->param( biblionumber => $biblionumber );
96
97         my $dat              = GetBiblioData($biblionumber);
98         next unless $dat;
99         my $record           = GetMarcBiblio($biblionumber, 1);
100         my $marcauthorsarray = GetMarcAuthors( $record, $marcflavour );
101         my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour );
102
103         my @items = GetItemsInfo( $biblionumber );
104
105         my $hasauthors = 0;
106         if($dat->{'author'} || @$marcauthorsarray) {
107           $hasauthors = 1;
108         }
109         
110
111         $dat->{MARCSUBJCTS}    = $marcsubjctsarray;
112         $dat->{MARCAUTHORS}    = $marcauthorsarray;
113         $dat->{HASAUTHORS}     = $hasauthors;
114         $dat->{'biblionumber'} = $biblionumber;
115         $dat->{ITEM_RESULTS}   = \@items;
116
117         $iso2709 .= $record->as_usmarc();
118
119         push( @results, $dat );
120     }
121
122     my $resultsarray = \@results;
123     
124     $template2->param(
125         BIBLIO_RESULTS => $resultsarray,
126         comment        => $comment,
127         firstname      => $user->{firstname},
128         surname        => $user->{surname},
129     );
130
131     # Getting template result
132     my $template_res = $template2->output();
133     my $body;
134
135     # Analysing information and getting mail properties
136
137     if ( $template_res =~ /<SUBJECT>(.*)<END_SUBJECT>/s ) {
138         $mail{subject} = $1;
139         $mail{subject} =~ s|\n?(.*)\n?|$1|;
140         $mail{subject} = Encode::encode("UTF-8", $mail{subject});
141     }
142     else { $mail{'subject'} = "no subject"; }
143
144     my $email_header = "";
145     if ( $template_res =~ /<HEADER>(.*)<END_HEADER>/s ) {
146         $email_header = $1;
147         $email_header =~ s|\n?(.*)\n?|$1|;
148         $email_header = encode_qp(Encode::encode("UTF-8", $email_header));
149     }
150
151     my $email_file = "basket.txt";
152     if ( $template_res =~ /<FILENAME>(.*)<END_FILENAME>/s ) {
153         $email_file = $1;
154         $email_file =~ s|\n?(.*)\n?|$1|;
155     }
156
157     if ( $template_res =~ /<MESSAGE>(.*)<END_MESSAGE>/s ) {
158         $body = $1;
159         $body =~ s|\n?(.*)\n?|$1|;
160         $body = encode_qp(Encode::encode("UTF-8", $body));
161     }
162
163     $mail{body} = $body;
164
165     my $boundary = "====" . time() . "====";
166
167     $mail{'content-type'} = "multipart/mixed; boundary=\"$boundary\"";
168     my $isofile = encode_base64(encode("UTF-8", $iso2709));
169     $boundary = '--' . $boundary;
170     $mail{body} = <<END_OF_BODY;
171 $boundary
172 MIME-Version: 1.0
173 Content-Type: text/plain; charset="UTF-8"
174 Content-Transfer-Encoding: quoted-printable
175
176 $email_header
177 $body
178 $boundary
179 Content-Type: application/octet-stream; name="basket.iso2709"
180 Content-Transfer-Encoding: base64
181 Content-Disposition: attachment; filename="basket.iso2709"
182
183 $isofile
184 $boundary--
185 END_OF_BODY
186
187     # Sending mail (if not empty basket)
188     if ( defined($iso2709) && sendmail %mail ) {
189     # do something if it works....
190         $template->param( SENT      => "1" );
191     }
192     else {
193         # do something if it doesnt work....
194     carp "Error sending mail: empty basket" if !defined($iso2709);
195         carp "Error sending mail: $Mail::Sendmail::error" if $Mail::Sendmail::error;
196         $template->param( error => 1 );
197     }
198     $template->param( email_add => $email_add );
199     output_html_with_http_headers $query, $cookie, $template->output;
200 }
201 else {
202     $template->param(
203         bib_list       => $bib_list,
204         url            => "/cgi-bin/koha/opac-sendbasket.pl",
205         suggestion     => C4::Context->preference("suggestion"),
206         virtualshelves => C4::Context->preference("virtualshelves"),
207         csrf_token     => Koha::Token->new->generate_csrf(
208             {   id     => C4::Context->userenv->{id},
209                 secret => md5_base64( C4::Context->config('pass') ),
210             }
211         ),
212     );
213     output_html_with_http_headers $query, $cookie, $template->output;
214 }