Bug 13321 - DBRev 16.06.00.042
[koha.git] / opac / opac-sendbasket.pl
blob77fc370e31d27c0dd70c5a9ede9af30230e72395
1 #!/usr/bin/perl
3 # Copyright Doxulting 2004
5 # This file is part of Koha.
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.
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.
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>.
20 use Modern::Perl;
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;
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;
39 my $query = new CGI;
41 my ( $template, $borrowernumber, $cookie ) = get_template_and_user (
43 template_name => "opac-sendbasketform.tt",
44 query => $query,
45 type => "opac",
46 authnotrequired => 0,
50 my $bib_list = $query->param('bib_list') || '';
51 my $email_add = $query->param('email_add');
53 my $dbh = C4::Context->dbh;
55 if ( $email_add ) {
56 die "Wrong CSRF token" unless Koha::Token->new->check_csrf({
57 id => C4::Context->userenv->{id},
58 secret => md5_base64( C4::Context->config('pass') ),
59 token => scalar $query->param('csrf_token'),
60 });
61 my $email = Koha::Email->new();
62 my $user = GetMember(borrowernumber => $borrowernumber);
63 my $user_email = GetFirstValidEmailAddress($borrowernumber)
64 || C4::Context->preference('KohaAdminEmailAddress');
66 my $email_replyto = "$user->{firstname} $user->{surname} <$user_email>";
67 my $comment = $query->param('comment');
69 # if you want to use the KohaAdmin address as from, that is the default no need to set it
70 my %mail = $email->create_message_headers({
71 to => $email_add,
72 replyto => $email_replyto,
73 });
74 $mail{'X-Abuse-Report'} = C4::Context->preference('KohaAdminEmailAddress');
76 # Since we are already logged in, no need to check credentials again
77 # when loading a second template.
78 my $template2 = C4::Templates::gettemplate(
79 'opac-sendbasket.tt', 'opac', $query,
82 my @bibs = split( /\//, $bib_list );
83 my @results;
84 my $iso2709;
85 my $marcflavour = C4::Context->preference('marcflavour');
86 foreach my $biblionumber (@bibs) {
87 $template2->param( biblionumber => $biblionumber );
89 my $dat = GetBiblioData($biblionumber);
90 next unless $dat;
91 my $record = GetMarcBiblio($biblionumber, 1);
92 my $marcauthorsarray = GetMarcAuthors( $record, $marcflavour );
93 my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour );
95 my @items = GetItemsInfo( $biblionumber );
97 my $hasauthors = 0;
98 if($dat->{'author'} || @$marcauthorsarray) {
99 $hasauthors = 1;
103 $dat->{MARCSUBJCTS} = $marcsubjctsarray;
104 $dat->{MARCAUTHORS} = $marcauthorsarray;
105 $dat->{HASAUTHORS} = $hasauthors;
106 $dat->{'biblionumber'} = $biblionumber;
107 $dat->{ITEM_RESULTS} = \@items;
109 $iso2709 .= $record->as_usmarc();
111 push( @results, $dat );
114 my $resultsarray = \@results;
116 $template2->param(
117 BIBLIO_RESULTS => $resultsarray,
118 comment => $comment,
119 firstname => $user->{firstname},
120 surname => $user->{surname},
123 # Getting template result
124 my $template_res = $template2->output();
125 my $body;
127 # Analysing information and getting mail properties
129 if ( $template_res =~ /<SUBJECT>(.*)<END_SUBJECT>/s ) {
130 $mail{subject} = $1;
131 $mail{subject} =~ s|\n?(.*)\n?|$1|;
132 $mail{subject} = Encode::encode("UTF-8", $mail{subject});
134 else { $mail{'subject'} = "no subject"; }
136 my $email_header = "";
137 if ( $template_res =~ /<HEADER>(.*)<END_HEADER>/s ) {
138 $email_header = $1;
139 $email_header =~ s|\n?(.*)\n?|$1|;
140 $email_header = encode_qp(Encode::encode("UTF-8", $email_header));
143 my $email_file = "basket.txt";
144 if ( $template_res =~ /<FILENAME>(.*)<END_FILENAME>/s ) {
145 $email_file = $1;
146 $email_file =~ s|\n?(.*)\n?|$1|;
149 if ( $template_res =~ /<MESSAGE>(.*)<END_MESSAGE>/s ) {
150 $body = $1;
151 $body =~ s|\n?(.*)\n?|$1|;
152 $body = encode_qp(Encode::encode("UTF-8", $body));
155 $mail{body} = $body;
157 my $boundary = "====" . time() . "====";
159 $mail{'content-type'} = "multipart/mixed; boundary=\"$boundary\"";
160 my $isofile = encode_base64(encode("UTF-8", $iso2709));
161 $boundary = '--' . $boundary;
162 $mail{body} = <<END_OF_BODY;
163 $boundary
164 MIME-Version: 1.0
165 Content-Type: text/plain; charset="UTF-8"
166 Content-Transfer-Encoding: quoted-printable
168 $email_header
169 $body
170 $boundary
171 Content-Type: application/octet-stream; name="basket.iso2709"
172 Content-Transfer-Encoding: base64
173 Content-Disposition: attachment; filename="basket.iso2709"
175 $isofile
176 $boundary--
177 END_OF_BODY
179 # Sending mail (if not empty basket)
180 if ( defined($iso2709) && sendmail %mail ) {
181 # do something if it works....
182 $template->param( SENT => "1" );
184 else {
185 # do something if it doesnt work....
186 carp "Error sending mail: empty basket" if !defined($iso2709);
187 carp "Error sending mail: $Mail::Sendmail::error" if $Mail::Sendmail::error;
188 $template->param( error => 1 );
190 $template->param( email_add => $email_add );
191 output_html_with_http_headers $query, $cookie, $template->output;
193 else {
194 $template->param(
195 bib_list => $bib_list,
196 url => "/cgi-bin/koha/opac-sendbasket.pl",
197 suggestion => C4::Context->preference("suggestion"),
198 virtualshelves => C4::Context->preference("virtualshelves"),
199 csrf_token => Koha::Token->new->generate_csrf(
200 { id => C4::Context->userenv->{id},
201 secret => md5_base64( C4::Context->config('pass') ),
205 output_html_with_http_headers $query, $cookie, $template->output;