Bug 19319: Reflected XSS Vulnerability in opac-MARCdetail.pl
[koha.git] / opac / opac-ISBDdetail.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 # parts copyright 2010 BibLibre
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20
21
22 =head1 NAME
23
24 opac-ISBDdetail.pl - script to show a biblio in ISBD format
25
26 =head1 DESCRIPTION
27
28 This script needs a biblionumber as parameter 
29
30 It shows the biblio
31
32 The template is in <templates_dir>/catalogue/ISBDdetail.tt.
33 this template must be divided into 11 "tabs".
34
35 The first 10 tabs present the biblio, the 11th one presents
36 the items attached to the biblio
37
38 =head1 FUNCTIONS
39
40 =cut
41
42 use Modern::Perl;
43
44 use C4::Auth;
45 use C4::Context;
46 use C4::Output;
47 use CGI qw ( -utf8 );
48 use MARC::Record;
49 use C4::Biblio;
50 use C4::Items;
51 use C4::Reserves;
52 use C4::Acquisition;
53 use C4::Serials;    # uses getsubscriptionfrom biblionumber
54 use C4::Koha;
55 use Koha::ItemTypes;
56 use Koha::Patrons;
57 use Koha::RecordProcessor;
58 use Koha::Biblios;
59
60 my $query = CGI->new();
61 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
62     {
63         template_name   => "opac-ISBDdetail.tt",
64         query           => $query,
65         type            => "opac",
66         authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
67         debug           => 1,
68     }
69 );
70
71 my $biblionumber = $query->param('biblionumber');
72 $biblionumber = int($biblionumber);
73
74 my $biblio = Koha::Biblios->find( $biblionumber );
75
76 # get biblionumbers stored in the cart
77 if(my $cart_list = $query->cookie("bib_list")){
78     my @cart_list = split(/\//, $cart_list);
79     if ( grep {$_ eq $biblionumber} @cart_list) {
80         $template->param( incart => 1 );
81     }
82 }
83
84 $template->param( 'ItemsIssued' => CountItemsIssued( $biblionumber ) );
85
86 my $marcflavour      = C4::Context->preference("marcflavour");
87
88 my @items = GetItemsInfo($biblionumber);
89 if (scalar @items >= 1) {
90     my @hiddenitems = GetHiddenItemnumbers(@items);
91
92     if (scalar @hiddenitems == scalar @items ) {
93         print $query->redirect("/cgi-bin/koha/errors/404.pl"); # escape early
94         exit;
95     }
96 }
97
98 my $record = GetMarcBiblio({
99     biblionumber => $biblionumber,
100     embed_items  => 1 });
101 if ( ! $record ) {
102     print $query->redirect("/cgi-bin/koha/errors/404.pl");
103     exit;
104 }
105 my $framework = GetFrameworkCode( $biblionumber );
106 my $record_processor = Koha::RecordProcessor->new({
107     filters => 'ViewPolicy',
108     options => {
109         interface => 'opac',
110         frameworkcode => $framework
111     }
112 });
113 $record_processor->process($record);
114
115 # some useful variables for enhanced content;
116 # in each case, we're grabbing the first value we find in
117 # the record and normalizing it
118 my $upc = GetNormalizedUPC($record,$marcflavour);
119 my $ean = GetNormalizedEAN($record,$marcflavour);
120 my $oclc = GetNormalizedOCLCNumber($record,$marcflavour);
121 my $isbn = GetNormalizedISBN(undef,$record,$marcflavour);
122 my $content_identifier_exists;
123 if ( $isbn or $ean or $oclc or $upc ) {
124     $content_identifier_exists = 1;
125 }
126 $template->param(
127     normalized_upc => $upc,
128     normalized_ean => $ean,
129     normalized_oclc => $oclc,
130     normalized_isbn => $isbn,
131     content_identifier_exists => $content_identifier_exists,
132 );
133
134 #coping with subscriptions
135 my $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber);
136 my $dbh = C4::Context->dbh;
137 my $dat                 = TransformMarcToKoha( $record );
138
139 my @subscriptions       = SearchSubscriptions({ biblionumber => $biblionumber, orderby => 'title' });
140 my @subs;
141 foreach my $subscription (@subscriptions) {
142     my %cell;
143         my $serials_to_display;
144     $cell{subscriptionid}    = $subscription->{subscriptionid};
145     $cell{subscriptionnotes} = $subscription->{notes};
146     $cell{branchcode}        = $subscription->{branchcode};
147
148     #get the three latest serials.
149         $serials_to_display = $subscription->{opacdisplaycount};
150         $serials_to_display = C4::Context->preference('OPACSerialIssueDisplayCount') unless $serials_to_display;
151         $cell{opacdisplaycount} = $serials_to_display;
152     $cell{latestserials} =
153       GetLatestSerials( $subscription->{subscriptionid}, $serials_to_display );
154     push @subs, \%cell;
155 }
156
157 $template->param(
158     subscriptions       => \@subs,
159     subscriptionsnumber => $subscriptionsnumber,
160 );
161
162 my $norequests = 1;
163 my $allow_onshelf_holds;
164 my $res = GetISBDView({
165     'record'    => $record,
166     'template'  => 'opac',
167     'framework' => $framework
168 });
169
170 my $itemtypes = { map { $_->{itemtype} => $_ } @{ Koha::ItemTypes->search_with_localization->unblessed } };
171 my $patron = Koha::Patrons->find( $loggedinuser );
172 for my $itm (@items) {
173     $norequests = 0
174       if $norequests
175         && !$itm->{'withdrawn'}
176         && !$itm->{'itemlost'}
177         && ($itm->{'itemnotforloan'}<0 || not $itm->{'itemnotforloan'})
178         && !$itemtypes->{$itm->{'itype'}}->{notforloan}
179         && $itm->{'itemnumber'};
180
181     $allow_onshelf_holds = C4::Reserves::OnShelfHoldsAllowed( $itm, ( $patron ? $patron->unblessed : {} ) )
182       unless $allow_onshelf_holds;
183 }
184
185 $template->param(
186     RequestOnOpac       => C4::Context->preference("RequestOnOpac"),
187     AllowOnShelfHolds   => $allow_onshelf_holds,
188     norequests   => $norequests,
189     ISBD         => $res,
190     biblio       => $biblio,
191 );
192
193 #Search for title in links
194 my $marccontrolnumber   = GetMarcControlnumber ($record, $marcflavour);
195 my $marcissns = GetMarcISSN ( $record, $marcflavour );
196 my $issn = $marcissns->[0] || '';
197
198 if (my $search_for_title = C4::Context->preference('OPACSearchForTitleIn')){
199     $dat->{title} =~ s/\/+$//; # remove trailing slash
200     $dat->{title} =~ s/\s+$//; # remove trailing space
201     $search_for_title = parametrized_url(
202         $search_for_title,
203         {
204             TITLE         => $dat->{title},
205             AUTHOR        => $dat->{author},
206             ISBN          => $isbn,
207             ISSN          => $issn,
208             CONTROLNUMBER => $marccontrolnumber,
209             BIBLIONUMBER  => $biblionumber,
210         }
211     );
212     $template->param('OPACSearchForTitleIn' => $search_for_title);
213 }
214
215 output_html_with_http_headers $query, $cookie, $template->output;