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