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