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