Bug 19752: offline_circ/service.pl - Return HTTP status 401 when authentication faile...
[koha.git] / C4 / Output.pm
blob39d9f9cd2189efa90af515f44fc43cb263d803e2
1 package C4::Output;
3 #package to deal with marking up output
4 #You will need to edit parts of this pm
5 #set the value of path to be where your html lives
7 # Copyright 2000-2002 Katipo Communications
9 # This file is part of Koha.
11 # Koha is free software; you can redistribute it and/or modify it
12 # under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 3 of the License, or
14 # (at your option) any later version.
16 # Koha is distributed in the hope that it will be useful, but
17 # WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU General Public License for more details.
21 # You should have received a copy of the GNU General Public License
22 # along with Koha; if not, see <http://www.gnu.org/licenses>.
25 # NOTE: I'm pretty sure this module is deprecated in favor of
26 # templates.
28 use strict;
29 #use warnings; FIXME - Bug 2505
31 use URI::Escape;
33 use C4::Context;
34 use C4::Templates;
36 use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
38 BEGIN {
39 require Exporter;
41 @ISA = qw(Exporter);
42 @EXPORT_OK = qw(&is_ajax ajax_fail); # More stuff should go here instead
43 %EXPORT_TAGS = ( all =>[qw(setlanguagecookie pagination_bar parametrized_url
44 &output_with_http_headers &output_ajax_with_http_headers &output_html_with_http_headers)],
45 ajax =>[qw(&output_with_http_headers &output_ajax_with_http_headers is_ajax)],
46 html =>[qw(&output_with_http_headers &output_html_with_http_headers)]
48 push @EXPORT, qw(
49 setlanguagecookie getlanguagecookie pagination_bar parametrized_url
51 push @EXPORT, qw(
52 &output_html_with_http_headers &output_ajax_with_http_headers &output_with_http_headers
53 &output_and_exit_if_error
58 =head1 NAME
60 C4::Output - Functions for managing output, is slowly being deprecated
62 =head1 FUNCTIONS
64 =over 2
65 =cut
67 =item pagination_bar
69 pagination_bar($base_url, $nb_pages, $current_page, $startfrom_name)
71 Build an HTML pagination bar based on the number of page to display, the
72 current page and the url to give to each page link.
74 C<$base_url> is the URL for each page link. The
75 C<$startfrom_name>=page_number is added at the end of the each URL.
77 C<$nb_pages> is the total number of pages available.
79 C<$current_page> is the current page number. This page number won't become a
80 link.
82 This function returns HTML, without any language dependency.
84 =cut
86 sub pagination_bar {
87 my $base_url = (@_ ? shift : return);
88 my $nb_pages = (@_) ? shift : 1;
89 my $current_page = (@_) ? shift : undef; # delay default until later
90 my $startfrom_name = (@_) ? shift : 'page';
91 my $additional_parameters = shift || {};
93 # how many pages to show before and after the current page?
94 my $pages_around = 2;
96 my $delim = qr/\&(?:amp;)?|;/; # "non memory" cluster: no backreference
97 $base_url =~ s/$delim*\b$startfrom_name=(\d+)//g; # remove previous pagination var
98 unless (defined $current_page and $current_page > 0 and $current_page <= $nb_pages) {
99 $current_page = ($1) ? $1 : 1; # pull current page from param in URL, else default to 1
100 # $debug and # FIXME: use C4::Debug;
101 # warn "with QUERY_STRING:" .$ENV{QUERY_STRING}. "\ncurrent_page:$current_page\n1:$1 2:$2 3:$3";
103 $base_url =~ s/($delim)+/$1/g; # compress duplicate delims
104 $base_url =~ s/$delim;//g; # remove empties
105 $base_url =~ s/$delim$//; # remove trailing delim
107 my $url = $base_url . (($base_url =~ m/$delim/ or $base_url =~ m/\?/) ? '&amp;' : '?' ) . $startfrom_name . '=';
108 my $url_suffix;
109 while ( my ( $k, $v ) = each %$additional_parameters ) {
110 $url_suffix .= '&amp;' . $k . '=' . $v;
112 my $pagination_bar = '';
114 # navigation bar useful only if more than one page to display !
115 if ( $nb_pages > 1 ) {
117 # link to first page?
118 if ( $current_page > 1 ) {
119 $pagination_bar .=
120 "\n" . '&nbsp;'
121 . '<a href="'
122 . $url
123 . '1'
124 . $url_suffix
125 . '"rel="start">'
126 . '&lt;&lt;' . '</a>';
128 else {
129 $pagination_bar .=
130 "\n" . '&nbsp;<span class="inactive">&lt;&lt;</span>';
133 # link on previous page ?
134 if ( $current_page > 1 ) {
135 my $previous = $current_page - 1;
137 $pagination_bar .=
138 "\n" . '&nbsp;'
139 . '<a href="'
140 . $url
141 . $previous
142 . $url_suffix
143 . '" rel="prev">' . '&lt;' . '</a>';
145 else {
146 $pagination_bar .=
147 "\n" . '&nbsp;<span class="inactive">&lt;</span>';
150 my $min_to_display = $current_page - $pages_around;
151 my $max_to_display = $current_page + $pages_around;
152 my $last_displayed_page = undef;
154 for my $page_number ( 1 .. $nb_pages ) {
155 if (
156 $page_number == 1
157 or $page_number == $nb_pages
158 or ( $page_number >= $min_to_display
159 and $page_number <= $max_to_display )
162 if ( defined $last_displayed_page
163 and $last_displayed_page != $page_number - 1 )
165 $pagination_bar .=
166 "\n" . '&nbsp;<span class="inactive">...</span>';
169 if ( $page_number == $current_page ) {
170 $pagination_bar .=
171 "\n" . '&nbsp;'
172 . '<span class="currentPage">'
173 . $page_number
174 . '</span>';
176 else {
177 $pagination_bar .=
178 "\n" . '&nbsp;'
179 . '<a href="'
180 . $url
181 . $page_number
182 . $url_suffix
183 . '">'
184 . $page_number . '</a>';
186 $last_displayed_page = $page_number;
190 # link on next page?
191 if ( $current_page < $nb_pages ) {
192 my $next = $current_page + 1;
194 $pagination_bar .= "\n"
195 . '&nbsp;<a href="'
196 . $url
197 . $next
198 . $url_suffix
199 . '" rel="next">' . '&gt;' . '</a>';
201 else {
202 $pagination_bar .=
203 "\n" . '&nbsp;<span class="inactive">&gt;</span>';
206 # link to last page?
207 if ( $current_page != $nb_pages ) {
208 $pagination_bar .= "\n"
209 . '&nbsp;<a href="'
210 . $url
211 . $nb_pages
212 . $url_suffix
213 . '" rel="last">'
214 . '&gt;&gt;' . '</a>';
216 else {
217 $pagination_bar .=
218 "\n" . '&nbsp;<span class="inactive">&gt;&gt;</span>';
222 return $pagination_bar;
225 =item output_with_http_headers
227 &output_with_http_headers($query, $cookie, $data, $content_type[, $status[, $extra_options]])
229 Outputs $data with the appropriate HTTP headers,
230 the authentication cookie $cookie and a Content-Type specified in
231 $content_type.
233 If applicable, $cookie can be undef, and it will not be sent.
235 $content_type is one of the following: 'html', 'js', 'json', 'xml', 'rss', or 'atom'.
237 $status is an HTTP status message, like '403 Authentication Required'. It defaults to '200 OK'.
239 $extra_options is hashref. If the key 'force_no_caching' is present and has
240 a true value, the HTTP headers include directives to force there to be no
241 caching whatsoever.
243 =cut
245 sub output_with_http_headers {
246 my ( $query, $cookie, $data, $content_type, $status, $extra_options ) = @_;
247 $status ||= '200 OK';
249 $extra_options //= {};
251 my %content_type_map = (
252 'html' => 'text/html',
253 'js' => 'text/javascript',
254 'json' => 'application/json',
255 'xml' => 'text/xml',
256 # NOTE: not using application/atom+xml or application/rss+xml because of
257 # Internet Explorer 6; see bug 2078.
258 'rss' => 'text/xml',
259 'atom' => 'text/xml'
262 die "Unknown content type '$content_type'" if ( !defined( $content_type_map{$content_type} ) );
263 my $cache_policy = 'no-cache';
264 $cache_policy .= ', no-store, max-age=0' if $extra_options->{force_no_caching};
265 my $options = {
266 type => $content_type_map{$content_type},
267 status => $status,
268 charset => 'UTF-8',
269 Pragma => 'no-cache',
270 'Cache-Control' => $cache_policy,
271 'X-Frame-Options' => 'SAMEORIGIN',
273 $options->{expires} = 'now' if $extra_options->{force_no_caching};
275 $options->{cookie} = $cookie if $cookie;
276 if ($content_type eq 'html') { # guaranteed to be one of the content_type_map keys, else we'd have died
277 $options->{'Content-Style-Type' } = 'text/css';
278 $options->{'Content-Script-Type'} = 'text/javascript';
281 # We can't encode here, that will double encode our templates, and xslt
282 # We need to fix the encoding as it comes out of the database, or when we pass the variables to templates
284 $data =~ s/\&amp\;amp\; /\&amp\; /g;
285 print $query->header($options), $data;
288 sub output_html_with_http_headers {
289 my ( $query, $cookie, $data, $status, $extra_options ) = @_;
290 output_with_http_headers( $query, $cookie, $data, 'html', $status, $extra_options );
294 sub output_ajax_with_http_headers {
295 my ( $query, $js ) = @_;
296 print $query->header(
297 -type => 'text/javascript',
298 -charset => 'UTF-8',
299 -Pragma => 'no-cache',
300 -'Cache-Control' => 'no-cache',
301 -expires => '-1d',
302 ), $js;
305 sub is_ajax {
306 my $x_req = $ENV{HTTP_X_REQUESTED_WITH};
307 return ( $x_req and $x_req =~ /XMLHttpRequest/i ) ? 1 : 0;
310 =item output_and_exit_if_error
312 output_and_exit_if_error( $query, $cookie, $template, $params );
314 To executed at the beginning of scripts to stop the script at this point if
315 some errors are found.
317 Tests for module 'members':
318 * patron is not defined (we are looking for a patron that does no longer exist/never existed)
319 * The logged in user cannot see patron's infos (feature 'cannot_see_patron_infos')
321 Others will be added here depending on the needs (for instance biblio does not exist will be useful).
323 =cut
325 sub output_and_exit_if_error {
326 my ( $query, $cookie, $template, $params ) = @_;
327 my $error;
328 if ( $params and exists $params->{module} ) {
329 if ( $params->{module} eq 'members' ) {
330 my $logged_in_user = $params->{logged_in_user};
331 my $current_patron = $params->{current_patron};
332 if ( not $current_patron ) {
333 $error = 'unknown_patron';
335 elsif( not $logged_in_user->can_see_patron_infos( $current_patron ) ) {
336 $error = 'cannot_see_patron_infos';
341 if ( $error ) {
342 $template->param( blocking_error => $error );
343 output_html_with_http_headers ( $query, $cookie, $template->output );
344 exit;
346 return;
349 sub parametrized_url {
350 my $url = shift || ''; # ie page.pl?ln={LANG}
351 my $vars = shift || {}; # ie { LANG => en }
352 my $ret = $url;
353 while ( my ($key,$val) = each %$vars) {
354 my $val_url = URI::Escape::uri_escape_utf8( $val // q{} );
355 $ret =~ s/\{$key\}/$val_url/g;
357 $ret =~ s/\{[^\{]*\}//g; # remove remaining vars
358 return $ret;
361 END { } # module clean-up code here (global destructor)
364 __END__
366 =back
368 =head1 AUTHOR
370 Koha Development Team <http://koha-community.org/>
372 =cut