Bug 36792: Limit POSIX imports
[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;
24 use Carp qw( carp );
25 use Try::Tiny qw( catch try );
26
27 use C4::Biblio qw(
28   GetMarcSubjects
29 );
30 use C4::Auth qw( get_template_and_user );
31 use C4::Output qw( output_html_with_http_headers );
32 use C4::Templates;
33 use Koha::Biblios;
34 use Koha::Email;
35 use Koha::Patrons;
36 use Koha::Token;
37
38 my $query = CGI->new;
39
40 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
41     {
42         template_name => "opac-sendbasketform.tt",
43         query         => $query,
44         type          => "opac",
45     }
46 );
47
48 my $bib_list  = $query->param('bib_list') || '';
49 my $email_add = $query->param('email_add');
50
51 if ( $email_add ) {
52     die "Wrong CSRF token"
53       unless Koha::Token->new->check_csrf(
54         {
55             session_id => scalar $query->cookie('CGISESSID'),
56             token      => scalar $query->param('csrf_token'),
57         }
58       );
59
60     my $patron     = Koha::Patrons->find($borrowernumber);
61     my $user_email = $patron->notice_email_address;
62
63     my $comment = $query->param('comment');
64
65     my @bibs = split( /\//, $bib_list );
66     my $iso2709;
67     foreach my $bib (@bibs) {
68         my $biblio = Koha::Biblios->find($bib) or next;
69         $iso2709 .= $biblio->metadata->record->as_usmarc();
70     }
71
72     if ( !defined $iso2709 ) {
73         carp "Error sending mail: empty basket";
74         $template->param( error => 1 );
75     }
76     elsif ( !defined $user_email or $user_email eq '' ) {
77         carp "Error sending mail: sender's email address is invalid";
78         $template->param( error => 1 );
79     }
80     else {
81         my %loops = ( biblio => \@bibs, );
82
83         my %substitute = ( comment => $comment, );
84
85         my $letter = C4::Letters::GetPreparedLetter(
86             module      => 'catalogue',
87             letter_code => 'CART',
88             lang        => $patron->lang,
89             tables      => {
90                 borrowers => $borrowernumber,
91             },
92             message_transport_type => 'email',
93             loops                  => \%loops,
94             substitute             => \%substitute,
95         );
96
97         my $attachment = {
98             filename => 'basket.iso2709',
99             type     => 'application/octet-stream',
100             content  => Encode::encode( "UTF-8", $iso2709 ),
101         };
102
103         my $message_id = C4::Letters::EnqueueLetter(
104             {
105                 letter                 => $letter,
106                 message_transport_type => 'email',
107                 to_address             => $email_add,
108                 reply_address          => $user_email,
109                 attachments            => [$attachment],
110             }
111         );
112
113         C4::Letters::SendQueuedMessages( { message_id => $message_id } ) if $message_id;
114
115         $template->param( SENT => 1 );
116     }
117
118     $template->param( email_add => $email_add );
119     output_html_with_http_headers $query, $cookie, $template->output, undef,
120       { force_no_caching => 1 };
121 }
122 else {
123     my $new_session_id = $query->cookie('CGISESSID');
124     $template->param(
125         bib_list       => $bib_list,
126         url            => "/cgi-bin/koha/opac-sendbasket.pl",
127         suggestion     => C4::Context->preference("suggestion"),
128         virtualshelves => C4::Context->preference("virtualshelves"),
129         csrf_token =>
130           Koha::Token->new->generate_csrf( { session_id => $new_session_id, } ),
131     );
132     output_html_with_http_headers $query, $cookie, $template->output, undef,
133       { force_no_caching => 1 };
134 }