Bug 28785: Adjust check_cookie_auth calls
[koha.git] / tags / review.pl
1 #!/usr/bin/perl
2
3 # This software is placed under the gnu General Public License, v2 (http://www.gnu.org/licenses/gpl.html)
4
5 # Copyright 2008 LibLime
6 #
7 # This file is part of Koha.
8 #
9 # Koha is free software; you can redistribute it and/or modify it
10 # under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3 of the License, or
12 # (at your option) any later version.
13 #
14 # Koha is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with Koha; if not, see <http://www.gnu.org/licenses>.
21
22 use Modern::Perl;
23 use POSIX qw( ceil exit );
24 use CGI qw ( -utf8 );
25 use CGI::Cookie;     # need to check cookies before having CGI parse the POST request
26 use URI::Escape qw( uri_escape_utf8 );
27 use C4::Auth qw( check_cookie_auth get_template_and_user );
28 use C4::Context;
29 use Koha::DateUtils qw( dt_from_string output_pref );
30 use C4::Output qw( output_with_http_headers is_ajax pagination_bar output_html_with_http_headers );
31 use C4::Tags qw(
32     approval_counts
33     blacklist
34     get_approval_rows
35     is_approved
36     whitelist
37 );
38
39 my $script_name = "/cgi-bin/koha/tags/review.pl";
40 my $needed_flags = { tools => 'moderate_tags' };    # FIXME: replace when more specific permission is created.
41
42 sub ajax_auth_cgi { # returns CGI object
43     my $needed_flags = shift;
44     my %cookies = CGI::Cookie->fetch;
45     my $input = CGI->new;
46     my $sessid = $cookies{'CGISESSID'}->value;
47     my ($auth_status) = check_cookie_auth($sessid, $needed_flags);
48     if ($auth_status ne "ok") {
49         output_with_http_headers $input, undef,
50             "window.alert('Your CGI session cookie ($sessid) is not current.  " .
51             "Please refresh the page and try again.');\n", 'js';
52         exit 0;
53     }
54     return $input;
55 }
56
57 if (is_ajax()) {
58     my $input = &ajax_auth_cgi($needed_flags);
59     my $operator = C4::Context->userenv->{'number'};  # must occur AFTER auth
60     my ($tag, $js_reply);
61     if ($tag = $input->param('test')) {
62         my $check = is_approved($tag);
63         $js_reply = ( $check >=  1 ? 'success' : $check <= -1 ? 'failure' : 'indeterminate' ) . "_test('".uri_escape_utf8($tag)."');\n";
64     }
65     if ($tag = $input->param('ok')) {
66         $js_reply = (   whitelist($operator,$tag) ? 'success' : 'failure') . "_approve('".uri_escape_utf8($tag)."');\n";
67     }
68     if ($tag = $input->param('rej')) {
69         $js_reply = (   blacklist($operator,$tag) ? 'success' : 'failure')  . "_reject('".uri_escape_utf8($tag)."');\n";
70     }
71     output_with_http_headers $input, undef, $js_reply, 'js';
72     exit;
73 }
74
75 ### Below is the sad, boring, necessary non-AJAX HTML code.
76
77 my $input = CGI->new;
78 my ($template, $borrowernumber, $cookie) = get_template_and_user(
79     {
80         template_name   => "tags/review.tt",
81         query           => $input,
82         type            => "intranet",
83         flagsrequired   => $needed_flags,
84     }
85 );
86
87 my ($op, @errors, @tags);
88
89 foreach (qw( approve reject test )) {
90     $op = $_ if ( $input->param("op-$_") );
91 }
92 $op ||= 'none';
93
94 @tags = $input->multi_param('tags');
95
96 $borrowernumber == 0 and push @errors, {op_zero=>1};
97 if ($op eq 'approve') {
98     foreach (@tags) {
99         whitelist($borrowernumber,$_) or push @errors, {failed_ok=>$_};
100     }
101 } elsif ($op eq 'reject' ) {
102     foreach (@tags) {
103          blacklist($borrowernumber,$_) or push @errors, {failed_rej=>$_};
104     }
105 } elsif ($op eq 'test'   ) {
106     my $tag = $input->param('test');
107     push @tags, $tag;
108     my $check = is_approved($tag);
109     $template->param(
110         test_term => $tag,
111         ( $check >=  1 ? 'verdict_ok' :
112           $check <= -1 ? 'verdict_rej' : 'verdict_indeterminate' ) => 1,
113      );
114 }
115
116 my $counts = &approval_counts;
117 foreach (keys %$counts) {
118     $template->param($_ => $counts->{$_});
119 }
120
121 sub pagination_calc {
122     my $query = shift or return;
123     my $hardlimit = (@_) ? shift : 100;     # hardcoded, could be another syspref
124     my $pagesize = $query->param('limit' ) || $hardlimit;
125     my $page     = $query->param('page'  ) || 1;
126     my $offset   = $query->param('offset') || 0;
127     ($pagesize <= $hardlimit) or $pagesize = $hardlimit;
128     if ($page > 1) {
129         $offset = ($page-1)*$pagesize;
130     } else {
131         $page = 1;
132     }
133     return ($pagesize,$page,$offset);
134 }
135
136 my ($pagesize,$page,$offset) = pagination_calc($input,100);
137
138 my %filters = (
139     limit => $offset ? "$offset,$pagesize" : $pagesize,
140      sort => 'approved,-weight_total,+term',
141 );
142 my ($filter,$date_from,$date_to);
143 if (defined $input->param('approved')) { # 0 is valid value, must check defined
144     $filter = $input->param('approved');
145 } else {
146     $filter = 0;
147 }
148 if ($filter eq 'all') {
149     $template->param(filter_approved_all => 1);
150 } elsif ($filter =~ /-?[01]/) {
151     $filters{approved} = $filter;
152     $template->param(
153         ($filter == 1  ? 'filter_approved_ok'      :
154          $filter == 0  ? 'filter_approved_pending' :
155          $filter == -1 ? 'filter_approved_rej'     :
156         'filter_approved') => 1
157     );
158 }
159
160 # my $q_count = get_approval_rows({limit=>$pagesize, sort=>'approved,-weight_total,+term', count=>1});
161 if ($filter = $input->param('tag')) {
162     $template->param(filter_tag=>$filter);
163     $filters{term} = $filter;
164 }
165 if ($filter = $input->param('from')) {
166         $date_from = eval { output_pref( { dt => dt_from_string( $filter ), dateonly => 1, dateformat => 'iso' } ); };
167         if ( $date_from ) {
168         $template->param(filter_date_approved_from=>$filter);
169         $filters{date_approved} = ">=$date_from";
170     } else {
171         push @errors, {date_from=>$filter};
172     }
173 }
174 if ($filter = $input->param('to')) {
175         $date_to = eval { output_pref( { dt => dt_from_string( $filter ), dateonly => 1, dateformat => 'iso' } ); };
176         if ( $date_to ) {
177         $template->param(filter_date_approved_to=>$filter);
178         $filters{date_approved} = "<=$date_to";
179     } else {
180         push @errors, {date_to=>$filter};
181     }
182 }
183 if ($filter = $input->param('approver')) {      # name (or borrowernumber) from input box
184     if ($filter =~ /^\d+$/ and $filter > 0) {
185         # $filter=get borrowernumber from name
186         # FIXME: get borrowernumber from name not implemented.
187         $template->param(filter_approver=>$filter);
188         $filters{approved_by} = $filter;
189     } else {
190         push @errors, {approver=>$filter};
191     }
192 }
193 if ($filter = $input->param('approved_by')) {   # borrowernumber from link
194     if ($filter =~ /^\d+$/ and $filter > 0) {
195         $template->param(filter_approver=>$filter);
196         $filters{approved_by} = $filter;
197     } else {
198         push @errors, {approved_by=>$filter};
199     }
200 }
201 my $tagloop = get_approval_rows(\%filters);
202 my $qstring = $input->query_string;
203 $qstring =~ s/([&;])*\blimit=\d+//;         # remove pagination var
204 $qstring =~ s/^;+//;                        # remove leading delims
205 $qstring = "limit=$pagesize" . ($qstring ? '&amp;' . $qstring : '');
206 (scalar @errors) and $template->param(message_loop=>\@errors);
207 $template->param(
208     offset => $offset,  # req'd for EXPR
209     op => $op,
210     op_count => scalar(@tags),
211     script_name => $script_name,
212     approved => 0,      # dummy value (also EXPR)
213     tagloop => $tagloop,
214     pagination_bar => pagination_bar(
215         "$script_name?$qstring\&amp;",
216         ceil($counts->{approved_total}/$pagesize),  # $page, 'page'
217     )
218 );
219
220 output_html_with_http_headers $input, $cookie, $template->output;
221 __END__
222
223 =head1 AUTHOR
224
225 Joe Atzberger
226 atz AT liblime.com
227
228 =cut