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