]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/WWW/EGWeb.pm
TPac: Default to en_us if no locales are configured
[working/Evergreen.git] / Open-ILS / src / perlmods / lib / OpenILS / WWW / EGWeb.pm
1 package OpenILS::WWW::EGWeb;
2 use strict; use warnings;
3 use Template;
4 use XML::Simple;
5 use XML::LibXML;
6 use File::stat;
7 use Encode;
8 use Apache2::Const -compile => qw(OK DECLINED HTTP_INTERNAL_SERVER_ERROR);
9 use Apache2::Log;
10 use OpenSRF::EX qw(:try);
11 use OpenILS::Utils::CStoreEditor;
12
13 use constant OILS_HTTP_COOKIE_SKIN => 'eg_skin';
14 use constant OILS_HTTP_COOKIE_THEME => 'eg_theme';
15 use constant OILS_HTTP_COOKIE_LOCALE => 'eg_locale';
16
17 # cache string bundles
18 my @registered_locales;
19
20 sub handler {
21     my $r = shift;
22     my $ctx = load_context($r);
23     my $base = $ctx->{base_path};
24
25     $r->content_type('text/html; encoding=utf8');
26
27     my($template, $page_args, $as_xml) = find_template($r, $base, $ctx);
28     $ctx->{page_args} = $page_args;
29
30     my $stat = run_context_loader($r, $ctx);
31
32     return $stat unless $stat == Apache2::Const::OK;
33     return Apache2::Const::DECLINED unless $template;
34
35     my $text_handler = set_text_handler($ctx, $r);
36
37     my $tt = Template->new({
38         OUTPUT => ($as_xml) ?  sub { parse_as_xml($r, $ctx, @_); } : $r,
39         INCLUDE_PATH => $ctx->{template_paths},
40         DEBUG => $ctx->{debug_template},
41         PLUGINS => {
42             EGI18N => 'OpenILS::WWW::EGWeb::I18NFilter',
43             CGI_utf8 => 'OpenILS::WWW::EGWeb::CGI_utf8'
44         },
45         FILTERS => {
46             # Register a dynamic filter factory for our locale::maketext generator
47             l => [
48                 sub {
49                     my($ctx, @args) = @_;
50                     return sub { $text_handler->(shift(), @args); }
51                 }, 1
52             ]
53         }
54     });
55
56     if (!$tt) {
57         $r->log->error("Error creating template processor: $@");
58         return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
59     }   
60
61     $ctx->{encode_utf8} = sub {return encode_utf8(shift())};
62
63     unless($tt->process($template, {ctx => $ctx, ENV => \%ENV, l => $text_handler})) {
64         $r->log->warn('egweb: template error: ' . $tt->error);
65         return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
66     }
67
68     return Apache2::Const::OK;
69 }
70
71 sub set_text_handler {
72     my $ctx = shift;
73     my $r = shift;
74
75     my $locale = $ctx->{locale};
76
77     $r->log->debug("egweb: messages locale = $locale");
78
79     return sub {
80         my $lh = OpenILS::WWW::EGWeb::I18N->get_handle($locale);
81         return $lh->maketext(@_);
82     };
83 }
84
85
86
87 sub run_context_loader {
88     my $r = shift;
89     my $ctx = shift;
90
91     my $stat = Apache2::Const::OK;
92
93     my $loader = $r->dir_config('OILSWebContextLoader');
94     return $stat unless $loader;
95
96     eval {
97         $loader->use;
98         $stat = $loader->new($r, $ctx)->load;
99     };
100
101     if($@) {
102         $r->log->error("egweb: Context Loader error: $@");
103         return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
104     }
105
106     $r->log->debug("egweb: context loader resulted in status $stat");
107     return $stat;
108 }
109
110 sub parse_as_xml {
111     my $r = shift;
112     my $ctx = shift;
113     my $data = shift;
114
115     my $success = 0;
116
117     try { 
118         my $doc = XML::LibXML->new->parse_string($data); 
119         $data = $doc->documentElement->toStringC14N;
120         $data = $ctx->{final_dtd} . "\n" . $data;
121         $success = 1;
122     } otherwise {
123             my $e = shift;
124         my $err = "Invalid XML: $e";
125         $r->log->error("egweb: $err");
126         $r->content_type('text/plain; encoding=utf8');
127         $r->print("\n$err\n\n$data");
128     };
129
130     $r->print($data) if ($success);
131 }
132
133 sub load_context {
134     my $r = shift;
135     my $cgi = CGI->new;
136     my $ctx = {}; # new context for each page load
137
138     $ctx->{base_path} = $r->dir_config('OILSWebBasePath');
139     $ctx->{web_dir} = $r->dir_config('OILSWebWebDir');
140     $ctx->{debug_template} = ($r->dir_config('OILSWebDebugTemplate') =~ /true/io);
141     $ctx->{media_prefix} = $r->dir_config('OILSWebMediaPrefix');
142     $ctx->{hostname} = $r->hostname;
143     $ctx->{base_url} = $cgi->url(-base => 1);
144     $ctx->{skin} = $cgi->cookie(OILS_HTTP_COOKIE_SKIN) || 'default';
145     $ctx->{theme} = $cgi->cookie(OILS_HTTP_COOKIE_THEME) || 'default';
146     $ctx->{proto} = $cgi->https ? 'https' : 'http';
147
148     my @template_paths = $r->dir_config->get('OILSWebTemplatePath');
149     $ctx->{template_paths} = [ reverse @template_paths ];
150
151     my %locales = $r->dir_config->get('OILSWebLocale');
152     load_locale_handlers($ctx, %locales);
153
154     $ctx->{locale} = 
155         $cgi->cookie(OILS_HTTP_COOKIE_LOCALE) || 
156         parse_accept_lang($r->headers_in->get('Accept-Language')) || 'en_us';
157
158     my $mprefix = $ctx->{media_prefix};
159     if($mprefix and $mprefix !~ /^http/ and $mprefix !~ /^\//) {
160         # if a hostname is provided /w no protocol, match the protocol to the current page
161         $ctx->{media_prefix} = ($cgi->https) ? "https://$mprefix" : "http://$mprefix";
162     }
163
164     return $ctx;
165 }
166
167 # turn Accept-Language into sometihng EG can understand
168 # TODO: try all langs, not just the first
169 sub parse_accept_lang {
170     my $al = shift;
171     return undef unless $al;
172     my ($locale) = split(/,/, $al);
173     ($locale) = split(/;/, $locale);
174     return undef unless $locale;
175     $locale =~ s/-/_/og;
176     return $locale;
177 }
178
179 # Given a URI, finds the configured template and any extra page 
180 # arguments (trailing path info).  Any extra data is returned
181 # as page arguments, in the form of an array, one item per 
182 # /-separated URI component
183 sub find_template {
184     my $r = shift;
185     my $base = shift;
186     my $ctx = shift;
187     my $path = $r->uri;
188     $path =~ s/$base\/?//og;
189     my $template = '';
190     my $page_args = [];
191     my $as_xml = $r->dir_config('OILSWebForceValidXML');
192     my $ext = $r->dir_config('OILSWebDefaultTemplateExtension');
193
194     my @parts = split('/', $path);
195     my $localpath = $path;
196     my @args;
197     while(@parts) {
198         last unless $localpath;
199         for my $tpath (@{$ctx->{template_paths}}) {
200             my $fpath = "$tpath/$localpath.$ext";
201             $r->log->debug("egweb: looking at possible template $fpath");
202             if(-r $fpath) {
203                 $template = "$localpath.$ext";
204                 last;
205             }
206         }
207         last if $template;
208         push(@args, pop @parts);
209         $localpath = join('/', @parts);
210     } 
211
212     $page_args = [@args];
213
214     # no template configured or found
215     unless($template) {
216         $r->log->debug("egweb: No template configured for path $path");
217         return ();
218     }
219
220     $r->log->debug("egweb: template = $template : page args = @$page_args");
221     return ($template, $page_args, $as_xml);
222 }
223
224 # Create an I18N sub-module for each supported locale
225 # Each module creates its own MakeText lexicon by parsing .po/.mo files
226 sub load_locale_handlers {
227     my $ctx = shift;
228     my %locales = @_;
229
230     my @locale_tags = sort { length($a) <=> length($b) } keys %locales;
231
232     # If no locales are defined, fall back to en_us so that at least 1 handler exists
233     push(@locale_tags, 'en_us') unless @registered_locales or @locale_tags;
234
235     for my $idx (0..$#locale_tags) {
236
237         my $tag = $locale_tags[$idx];
238         next if grep { $_ eq $tag } @registered_locales;
239
240         my $parent_tag = '';
241         my $sub_idx = $idx;
242
243         # find the parent locale if possible.  It will be 
244         # longest left-anchored substring of the current tag
245         while( --$sub_idx >= 0 ) {
246             my $ptag = $locale_tags[$sub_idx];
247             if( substr($tag, 0, length($ptag)) eq $ptag ) {
248                 $parent_tag = "::$ptag";
249                 last;
250             }
251         }
252
253         my $messages = $locales{$tag} || '';
254
255         # TODO Can we do this without eval?
256         my $eval = <<"        EVAL";
257             package OpenILS::WWW::EGWeb::I18N::$tag;
258             use base 'OpenILS::WWW::EGWeb::I18N$parent_tag';
259             if(\$messages) {
260                 use Locale::Maketext::Lexicon::Gettext;
261                 if(open F, '$messages') {
262                     our %Lexicon = (%Lexicon, %{ Locale::Maketext::Lexicon::Gettext->parse(<F>) });
263                     close F;
264                 } else {
265                     warn "EGWeb: unable to open messages file: $messages"; 
266                 }
267             }
268         EVAL
269         eval $eval;
270
271         if ($@) {
272             warn "$@\n" if $@;
273         } else {
274             push(@registered_locales, $tag);
275         }
276     }
277 }
278
279
280 # base class for all supported locales
281 package OpenILS::WWW::EGWeb::I18N;
282 use base 'Locale::Maketext';
283 our %Lexicon = (_AUTO => 1);
284
285 1;