Bug 26019: Koha should set SameSite attribute on cookies
[koha.git] / opac / opac-ratings-ajax.pl
1 #!/usr/bin/perl
2
3 # Copyright 2011 KohaAloha, NZ
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 =head1 DESCRIPTION
21
22 A script that takes an ajax json query, and then inserts or modifies a star-rating.
23
24 =cut
25
26 use Modern::Perl;
27
28 use CGI qw ( -utf8 );
29 use CGI::Cookie;  # need to check cookies before having CGI parse the POST request
30
31 use C4::Auth qw( get_template_and_user check_cookie_auth );
32 use C4::Context;
33 use C4::Output qw( is_ajax output_ajax_with_http_headers );
34
35 use Koha::Ratings;
36
37 use JSON;
38
39 my $is_ajax = is_ajax();
40
41 my ( $query, $auth_status );
42 if ($is_ajax) {
43     ( $query, $auth_status ) = &ajax_auth_cgi( {} );
44 }
45 else {
46     $query = CGI->new();
47 }
48
49 my $biblionumber     = $query->param('biblionumber');
50 my $rating_value     = $query->param('rating_value');
51 my $rating_old_value = $query->param('rating_old_value');
52
53 my ( $template, $loggedinuser, $cookie );
54 if ($is_ajax) {
55     $loggedinuser = C4::Context->userenv->{'number'};
56 }
57 else {
58     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
59         {
60             template_name   => "opac-detail.tt",
61             query           => $query,
62             type            => "opac",
63             authnotrequired => 0,                    # auth required to add tags
64         }
65     );
66 }
67
68 my $rating;
69 $rating_value //= '';
70
71 if ( $rating_value eq '' ) {
72     my $rating = Koha::Ratings->find( { biblionumber => $biblionumber, borrowernumber => $loggedinuser } );
73     $rating->delete if $rating;
74 }
75
76 elsif ( $rating_value and !$rating_old_value ) {
77     Koha::Rating->new( { biblionumber => $biblionumber, borrowernumber => $loggedinuser, rating_value => $rating_value, })->store;
78 }
79
80 elsif ( $rating_value ne $rating_old_value ) {
81     my $rating = Koha::Ratings->find( { biblionumber => $biblionumber, borrowernumber => $loggedinuser });
82     $rating->rating_value($rating_value)->store if $rating
83 }
84
85 my $ratings = Koha::Ratings->search({ biblionumber => $biblionumber });
86 my $my_rating = $ratings->search({ borrowernumber => $loggedinuser })->next;
87 my $avg = $ratings->get_avg_rating;
88
89 my %js_reply = (
90     rating_total   => $ratings->count,
91     rating_avg     => $avg,
92     rating_avg_int => sprintf("%.0f", $avg),
93     rating_value   => $my_rating ? $my_rating->rating_value : undef,
94     auth_status    => $auth_status,
95
96 );
97
98 my $json_reply = JSON->new->encode( \%js_reply );
99
100 #### $rating
101 #### %js_reply
102 #### $json_reply
103
104 output_ajax_with_http_headers( $query, $json_reply );
105 exit;
106
107 # a ratings specific ajax return sub, returns CGI object, and an 'auth_success' value
108 sub ajax_auth_cgi {
109     my $needed_flags = shift;
110     my %cookies      = CGI::Cookie->fetch;
111     my $input        = CGI->new;
112     my $sessid = $cookies{'CGISESSID'}->value || $input->param('CGISESSID');
113     my ( $auth_status ) =
114       check_cookie_auth( $sessid, $needed_flags );
115     return $input, $auth_status;
116 }