Moved C4/Charset.pm to C4/Interface/CGI/Output.pm
[koha.git] / C4 / Interface / CGI / Output.pm
1 package C4::Interface::CGI::Output;
2
3 # $Id$
4
5 #package to work around problems in HTTP headers
6 # Note: This is just a utility module; it should not be instantiated.
7
8
9 # Copyright 2003 Katipo Communications
10 #
11 # This file is part of Koha.
12 #
13 # Koha is free software; you can redistribute it and/or modify it under the
14 # terms of the GNU General Public License as published by the Free Software
15 # Foundation; either version 2 of the License, or (at your option) any later
16 # version.
17 #
18 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
19 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
20 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
21 #
22 # You should have received a copy of the GNU General Public License along with
23 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
24 # Suite 330, Boston, MA  02111-1307 USA
25
26 use strict;
27 require Exporter;
28
29 use vars qw($VERSION @ISA @EXPORT);
30
31 # set the version for version checking
32 $VERSION = 0.01;
33
34 =head1 NAME
35
36 C4::CGI::Output - Convenience functions for handling outputting HTML pages
37
38 =head1 SYNOPSIS
39
40   use C4::CGI::Output;
41
42   print $query->header(-type => C4::CGI::Output::gettype($output)), $output;
43
44 =head1 DESCRIPTION
45
46 The functions in this module peek into a piece of HTML and return strings
47 related to the (guessed) charset.
48
49 =head1 FUNCTIONS
50
51 =over 2
52
53 =cut
54
55 @ISA = qw(Exporter);
56 @EXPORT = qw(
57                 &guesscharset
58                 &guesstype
59                 &output_html_with_http_headers
60              );
61
62 =item guesscharset
63
64    &guesscharset($output)
65
66 "Guesses" the charset from the some HTML that would be output.
67
68 C<$output> is the HTML page to be output. If it contains a META tag
69 with a Content-Type, the tag will be scanned for a language code.
70 This code is returned if it is found; undef is returned otherwise.
71
72 This function only does sloppy guessing; it will be confused by
73 unexpected things like SGML comments. What it basically does is to
74 grab something that looks like a META tag and scan it.
75
76 =cut
77
78 sub guesscharset ($) {
79     my($html) = @_;
80     my $charset = undef;
81     local($`, $&, $', $1, $2, $3);
82     # FIXME... These regular expressions will miss a lot of valid tags!
83     if ($html =~ /<meta\s+http-equiv=(["']?)Content-Type\1\s+content=(["'])text\/html\s*;\s*charset=([^\2\s\r\n]+)\2\s*(?:\/?)>/is) {
84         $charset = $3;
85     } elsif ($html =~ /<meta\s+content=(["'])text\/html\s*;\s*charset=([^\1\s\r\n]+)\1\s+http-equiv=(["']?)Content-Type\3\s*(?:\/?)>/is) {
86         $charset = $2;
87     }
88     return $charset;
89 } # guess
90
91 sub guesstype ($) {
92     my($html) = @_;
93     my $charset = guesscharset($html);
94     return defined $charset? "text/html; charset=$charset": "text/html";
95 }
96
97 =item output_html_with_http_headers
98
99    &output_html_with_http_headers($query, $cookie, $html)
100
101 Outputs the HTML page $html with the appropriate HTTP headers,
102 with the authentication cookie $cookie and a Content-Type that
103 corresponds to the HTML page $html.
104
105 =cut
106
107 sub output_html_with_http_headers ($$$) {
108     my($query, $cookie, $html) = @_;
109     print $query->header(
110         -type   => guesstype($html),
111         -cookie => $cookie,
112     ), $html;
113 }
114
115 #---------------------------------
116
117 END { }       # module clean-up code here (global destructor)
118
119 1;
120 __END__
121
122 =back
123
124 =head1 AUTHOR
125
126 Koha Developement team <info@koha.org>
127
128 =cut