1 package OpenILS::WWW::EGWeb;
2 use strict; use warnings;
8 use Apache2::Const -compile => qw(OK DECLINED HTTP_INTERNAL_SERVER_ERROR);
10 use OpenSRF::EX qw(:try);
11 use OpenILS::Utils::CStoreEditor q/:funcs/;
12 use List::MoreUtils qw/uniq/;
14 use constant OILS_HTTP_COOKIE_SKIN => 'eg_skin';
15 use constant OILS_HTTP_COOKIE_THEME => 'eg_theme';
16 use constant OILS_HTTP_COOKIE_LOCALE => 'eg_locale';
18 # cache string bundles
19 my %registered_locales;
23 my $ctx = load_context($r);
24 my $base = $ctx->{base_path};
26 my($template, $page_args, $as_xml) = find_template($r, $base, $ctx);
27 $ctx->{page_args} = $page_args;
29 my $stat = run_context_loader($r, $ctx);
31 return $stat unless $stat == Apache2::Const::OK;
32 return Apache2::Const::DECLINED unless $template;
34 my $text_handler = set_text_handler($ctx, $r);
36 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},
42 EGI18N => 'OpenILS::WWW::EGWeb::I18NFilter',
43 CGI_utf8 => 'OpenILS::WWW::EGWeb::CGI_utf8'
46 # Register a dynamic filter factory for our locale::maketext generator
50 return sub { $text_handler->(shift(), @args); }
57 $r->log->error("Error creating template processor: $@");
58 return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
61 $ctx->{encode_utf8} = sub {return encode_utf8(shift())};
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;
68 return Apache2::Const::OK;
71 sub set_text_handler {
75 my $locale = $ctx->{locale};
77 $r->log->debug("egweb: messages locale = $locale");
80 my $lh = OpenILS::WWW::EGWeb::I18N->get_handle($locale);
81 return $lh->maketext(@_);
87 sub run_context_loader {
91 my $stat = Apache2::Const::OK;
93 my $loader = $r->dir_config('OILSWebContextLoader');
94 return $stat unless $loader;
98 $stat = $loader->new($r, $ctx)->load;
102 $r->log->error("egweb: Context Loader error: $@");
103 return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
106 $r->log->debug("egweb: context loader resulted in status $stat");
118 my $doc = XML::LibXML->new->parse_string($data);
119 $data = $doc->documentElement->toStringC14N;
120 $data = $ctx->{final_dtd} . "\n" . $data;
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");
130 $r->print($data) if ($success);
136 my $ctx = {}; # new context for each page load
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 $ctx->{ext_proto} = $ctx->{proto};
148 my $default_locale = $r->dir_config('OILSWebDefaultLocale') || 'en_us';
150 my @template_paths = uniq $r->dir_config->get('OILSWebTemplatePath');
151 $ctx->{template_paths} = [ reverse @template_paths ];
153 my %locales = $r->dir_config->get('OILSWebLocale');
154 load_locale_handlers($ctx, %locales);
156 $ctx->{locales} = \%registered_locales;
158 # Set a locale cookie if the requested locale is valid
159 my $set_locale = $cgi->param('set_eg_locale') || '';
160 if (!(grep {$_ eq $set_locale} keys %registered_locales)) {
163 my $slc = $cgi->cookie({
164 '-name' => OILS_HTTP_COOKIE_LOCALE,
165 '-value' => $set_locale,
168 $r->headers_out->add('Set-Cookie' => $slc);
171 $ctx->{locale} = $set_locale ||
172 $cgi->cookie(OILS_HTTP_COOKIE_LOCALE) || $default_locale ||
173 parse_accept_lang($r->headers_in->get('Accept-Language'));
175 # set the editor default locale for each page load
176 $OpenILS::Utils::CStoreEditor::default_locale = parse_eg_locale($ctx->{locale});
178 my $mprefix = $ctx->{media_prefix};
179 if($mprefix and $mprefix !~ /^http/ and $mprefix !~ /^\//) {
180 # if a hostname is provided /w no protocol, match the protocol to the current page
181 $ctx->{media_prefix} = ($cgi->https) ? "https://$mprefix" : "http://$mprefix";
187 # turn Accept-Language into something EG can understand
188 # TODO: try all langs, not just the first
189 sub parse_accept_lang {
191 return undef unless $al;
192 my ($locale) = split(/,/, $al);
193 ($locale) = split(/;/, $locale);
194 return undef unless $locale;
199 # Accept-Language uses locales like 'en', 'fr', 'fr_fr', while Evergreen
200 # internally uses 'en-US', 'fr-CA', 'fr-FR' (always with the 2 lowercase,
201 # hyphen, 2 uppercase convention)
202 sub parse_eg_locale {
203 my $ua_locale = shift || 'en_us';
205 $ua_locale =~ m/^(..).?(..)?$/;
206 my $lang_code = lc($1);
207 my $region_code = $2 ? uc($2) : uc($1);
208 return "$lang_code-$region_code";
211 # Given a URI, finds the configured template and any extra page
212 # arguments (trailing path info). Any extra data is returned
213 # as page arguments, in the form of an array, one item per
214 # /-separated URI component
220 $path =~ s/$base\/?//og;
223 my $as_xml = $r->dir_config('OILSWebForceValidXML');
224 my $ext = $r->dir_config('OILSWebDefaultTemplateExtension');
226 my @parts = split('/', $path);
227 my $localpath = $path;
229 if ($localpath =~ m|opac/css|) {
230 $r->content_type('text/css; encoding=utf8');
232 $r->content_type('text/html; encoding=utf8');
236 last unless $localpath;
237 for my $tpath (@{$ctx->{template_paths}}) {
238 my $fpath = "$tpath/$localpath.$ext";
239 $r->log->debug("egweb: looking at possible template $fpath");
241 $template = "$localpath.$ext";
246 push(@args, pop @parts);
247 $localpath = join('/', @parts);
250 $page_args = [@args];
252 # no template configured or found
254 $r->log->debug("egweb: No template configured for path $path");
258 $r->log->debug("egweb: template = $template : page args = @$page_args");
259 return ($template, $page_args, $as_xml);
262 # Create an I18N sub-module for each supported locale
263 # Each module creates its own MakeText lexicon by parsing .po/.mo files
264 sub load_locale_handlers {
268 my $editor = new_editor();
269 my @locale_tags = sort { length($a) <=> length($b) } keys %locales;
271 # always fall back to en_us, the assumed template language
272 push(@locale_tags, 'en_us');
274 for my $idx (0..$#locale_tags) {
276 my $tag = $locale_tags[$idx];
277 next if grep { $_ eq $tag } keys %registered_locales;
279 my $res = $editor->json_query({
281 "evergreen.get_locale_name",
286 my $locale_name = $res->[0]->{"name"} if exists $res->[0]->{"name"};
287 next unless $locale_name;
292 # find the parent locale if possible. It will be
293 # longest left-anchored substring of the current tag
294 while( --$sub_idx >= 0 ) {
295 my $ptag = $locale_tags[$sub_idx];
296 if( substr($tag, 0, length($ptag)) eq $ptag ) {
297 $parent_tag = "::$ptag";
302 my $messages = $locales{$tag} || '';
304 # TODO Can we do this without eval?
305 my $eval = <<" EVAL";
306 package OpenILS::WWW::EGWeb::I18N::$tag;
307 use base 'OpenILS::WWW::EGWeb::I18N$parent_tag';
309 use Locale::Maketext::Lexicon {
312 use Locale::Maketext::Lexicon::Gettext;
313 if(open F, '$messages') {
314 our %Lexicon = (%Lexicon, %{ Locale::Maketext::Lexicon::Gettext->parse(<F>) });
317 warn "EGWeb: unable to open messages file: $messages";
326 $registered_locales{"$tag"} = $locale_name;
332 # base class for all supported locales
333 package OpenILS::WWW::EGWeb::I18N;
334 use base 'Locale::Maketext';
335 our %Lexicon = (_AUTO => 1);