Bug 23058: Prevent XSS vulnerabiliies when 'tag' is passed to opac-search
[koha.git] / opac / opac-search-history.pl
1 #!/usr/bin/perl
2
3 # Copyright 2013 BibLibre SARL
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 C4::Auth qw(:DEFAULT get_session);
23 use CGI qw ( -utf8 );
24 use C4::Context;
25 use C4::Output;
26 use C4::Log;
27 use C4::Items;
28 use C4::Debug;
29 use C4::Search::History;
30
31 use URI::Escape;
32 use POSIX qw(strftime);
33
34
35 my $cgi = new CGI;
36
37 # Getting the template and auth
38 my ($template, $loggedinuser, $cookie) = get_template_and_user(
39     {
40         template_name => "opac-search-history.tt",
41         query => $cgi,
42         type => "opac",
43         authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
44         debug => 1,
45     }
46 );
47
48 unless ( C4::Context->preference("EnableOpacSearchHistory") ) {
49     print $cgi->redirect("/cgi-bin/koha/errors/404.pl"); # escape early
50     exit;
51 }
52
53 my $type = $cgi->param('type');
54 my $action = $cgi->param('action') || q{};
55 my $previous = $cgi->param('previous');
56
57 # If the user is not logged in, we deal with the session
58 unless ( $loggedinuser ) {
59     # Deleting search history
60     if ( $action eq 'delete') {
61         # Deleting session's search history
62         my @id = $cgi->multi_param('id');
63         my $all = not scalar( @id );
64
65         my $type = $cgi->param('type');
66         my @searches = ();
67         unless ( $all ) {
68             @searches = C4::Search::History::get_from_session({ cgi => $cgi });
69             if ( $type ) {
70                 @searches = map { $_->{type} ne $type ? $_ : () } @searches;
71             }
72             if ( @id ) {
73                 @searches = map { my $search = $_; ( grep {/^$search->{id}$/} @id ) ? () : $_ } @searches;
74             }
75         }
76         C4::Search::History::set_to_session({ cgi => $cgi, search_history => \@searches });
77
78         # Redirecting to this same url so the user won't see the search history link in the header
79         print $cgi->redirect(-uri => '/cgi-bin/koha/opac-search-history.pl');
80     # Showing search history
81     } else {
82         # Getting the searches from session
83         my @current_searches = C4::Search::History::get_from_session({
84             cgi => $cgi,
85         });
86
87         my @current_biblio_searches = map {
88             $_->{type} eq 'biblio' ? $_ : ()
89         } @current_searches;
90
91         my @current_authority_searches = map {
92             $_->{type} eq 'authority' ? $_ : ()
93         } @current_searches;
94
95         $template->param(
96             current_biblio_searches => \@current_biblio_searches,
97             current_authority_searches => \@current_authority_searches,
98         );
99     }
100 } else {
101     # And if the user is logged in, we deal with the database
102     my $dbh = C4::Context->dbh;
103
104     # Deleting search history
105     if ( $action eq 'delete' ) {
106         my @id = $cgi->multi_param('id');
107         if ( @id ) {
108             C4::Search::History::delete(
109                 {
110                     userid => $loggedinuser,
111                     id     => [ $cgi->param('id') ],
112                 }
113             );
114         } else {
115             C4::Search::History::delete(
116                 {
117                    userid => $loggedinuser,
118                 }
119             );
120         }
121         # Redirecting to this same url so the user won't see the search history link in the header
122         print $cgi->redirect(-uri => '/cgi-bin/koha/opac-search-history.pl');
123
124     # Showing search history
125     } else {
126         my $current_searches = C4::Search::History::get({
127             userid => $loggedinuser,
128             sessionid => $cgi->cookie("CGISESSID")
129         });
130         my @current_biblio_searches = map {
131             $_->{type} eq 'biblio' ? $_ : ()
132         } @$current_searches;
133
134         my @current_authority_searches = map {
135             $_->{type} eq 'authority' ? $_ : ()
136         } @$current_searches;
137
138         my $previous_searches = C4::Search::History::get({
139             userid => $loggedinuser,
140             sessionid => $cgi->cookie("CGISESSID"),
141             previous => 1
142         });
143
144         my @previous_biblio_searches = map {
145             $_->{type} eq 'biblio' ? $_ : ()
146         } @$previous_searches;
147
148         my @previous_authority_searches = map {
149             $_->{type} eq 'authority' ? $_ : ()
150         } @$previous_searches;
151
152         $template->param(
153             current_biblio_searches => \@current_biblio_searches,
154             current_authority_searches => \@current_authority_searches,
155             previous_biblio_searches => \@previous_biblio_searches,
156             previous_authority_searches => \@previous_authority_searches,
157
158         );
159     }
160 }
161
162 $template->param(searchhistoryview => 1);
163
164 output_html_with_http_headers $cgi, $cookie, $template->output, undef, { force_no_caching => 1 };