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/;
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';
17 # cache string bundles
18 my %registered_locales;
22 my $ctx = load_context($r);
23 my $base = $ctx->{base_path};
25 $r->content_type('text/html; encoding=utf8');
27 my($template, $page_args, $as_xml) = find_template($r, $base, $ctx);
28 $ctx->{page_args} = $page_args;
30 my $stat = run_context_loader($r, $ctx);
32 return $stat unless $stat == Apache2::Const::OK;
33 return Apache2::Const::DECLINED unless $template;
35 my $text_handler = set_text_handler($ctx, $r);
37 my $tt = Template->new({
39 OUTPUT => ($as_xml) ? sub { parse_as_xml($r, $ctx, @_); } : $r,
40 INCLUDE_PATH => $ctx->{template_paths},
41 DEBUG => $ctx->{debug_template},
43 EGI18N => 'OpenILS::WWW::EGWeb::I18NFilter',
44 CGI_utf8 => 'OpenILS::WWW::EGWeb::CGI_utf8'
47 # Register a dynamic filter factory for our locale::maketext generator
51 return sub { $text_handler->(shift(), @args); }
58 $r->log->error("Error creating template processor: $@");
59 return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
62 $ctx->{encode_utf8} = sub {return encode_utf8(shift())};
64 unless($tt->process($template, {ctx => $ctx, ENV => \%ENV, l => $text_handler})) {
65 $r->log->warn('egweb: template error: ' . $tt->error);
66 return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
69 return Apache2::Const::OK;
72 sub set_text_handler {
76 my $locale = $ctx->{locale};
78 $r->log->debug("egweb: messages locale = $locale");
81 my $lh = OpenILS::WWW::EGWeb::I18N->get_handle($locale);
82 return $lh->maketext(@_);
88 sub run_context_loader {
92 my $stat = Apache2::Const::OK;
94 my $loader = $r->dir_config('OILSWebContextLoader');
95 return $stat unless $loader;
99 $stat = $loader->new($r, $ctx)->load;
103 $r->log->error("egweb: Context Loader error: $@");
104 return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
107 $r->log->debug("egweb: context loader resulted in status $stat");
119 my $doc = XML::LibXML->new->parse_string($data);
120 $data = $doc->documentElement->toStringC14N;
121 $data = $ctx->{final_dtd} . "\n" . $data;
125 my $err = "Invalid XML: $e";
126 $r->log->error("egweb: $err");
127 $r->content_type('text/plain; encoding=utf8');
128 $r->print("\n$err\n\n$data");
131 $r->print($data) if ($success);
137 my $ctx = {}; # new context for each page load
139 $ctx->{base_path} = $r->dir_config('OILSWebBasePath');
140 $ctx->{web_dir} = $r->dir_config('OILSWebWebDir');
141 $ctx->{debug_template} = ($r->dir_config('OILSWebDebugTemplate') =~ /true/io);
142 $ctx->{media_prefix} = $r->dir_config('OILSWebMediaPrefix');
143 $ctx->{hostname} = $r->hostname;
144 $ctx->{base_url} = $cgi->url(-base => 1);
145 $ctx->{skin} = $cgi->cookie(OILS_HTTP_COOKIE_SKIN) || 'default';
146 $ctx->{theme} = $cgi->cookie(OILS_HTTP_COOKIE_THEME) || 'default';
147 $ctx->{proto} = $cgi->https ? 'https' : 'http';
149 my @template_paths = $r->dir_config->get('OILSWebTemplatePath');
150 $ctx->{template_paths} = [ reverse @template_paths ];
152 my %locales = $r->dir_config->get('OILSWebLocale');
153 load_locale_handlers($ctx, %locales);
155 $ctx->{locales} = \%registered_locales;
157 # Set a locale cookie if the requested locale is valid
158 my $set_locale = $cgi->param('set_eg_locale');
159 if (!(grep {$_ eq $set_locale} keys %registered_locales)) {
162 my $slc = $cgi->cookie({
163 '-name' => OILS_HTTP_COOKIE_LOCALE,
164 '-value' => $set_locale,
167 $r->headers_out->add('Set-Cookie' => $slc);
170 $ctx->{locale} = $set_locale ||
171 $cgi->cookie(OILS_HTTP_COOKIE_LOCALE) ||
172 parse_accept_lang($r->headers_in->get('Accept-Language')) || 'en_us';
174 my $mprefix = $ctx->{media_prefix};
175 if($mprefix and $mprefix !~ /^http/ and $mprefix !~ /^\//) {
176 # if a hostname is provided /w no protocol, match the protocol to the current page
177 $ctx->{media_prefix} = ($cgi->https) ? "https://$mprefix" : "http://$mprefix";
183 # turn Accept-Language into sometihng EG can understand
184 # TODO: try all langs, not just the first
185 sub parse_accept_lang {
187 return undef unless $al;
188 my ($locale) = split(/,/, $al);
189 ($locale) = split(/;/, $locale);
190 return undef unless $locale;
195 # Given a URI, finds the configured template and any extra page
196 # arguments (trailing path info). Any extra data is returned
197 # as page arguments, in the form of an array, one item per
198 # /-separated URI component
204 $path =~ s/$base\/?//og;
207 my $as_xml = $r->dir_config('OILSWebForceValidXML');
208 my $ext = $r->dir_config('OILSWebDefaultTemplateExtension');
210 my @parts = split('/', $path);
211 my $localpath = $path;
214 last unless $localpath;
215 for my $tpath (@{$ctx->{template_paths}}) {
216 my $fpath = "$tpath/$localpath.$ext";
217 $r->log->debug("egweb: looking at possible template $fpath");
219 $template = "$localpath.$ext";
224 push(@args, pop @parts);
225 $localpath = join('/', @parts);
228 $page_args = [@args];
230 # no template configured or found
232 $r->log->debug("egweb: No template configured for path $path");
236 $r->log->debug("egweb: template = $template : page args = @$page_args");
237 return ($template, $page_args, $as_xml);
240 # Create an I18N sub-module for each supported locale
241 # Each module creates its own MakeText lexicon by parsing .po/.mo files
242 sub load_locale_handlers {
246 my $editor = new_editor();
247 my @locale_tags = sort { length($a) <=> length($b) } keys %locales;
249 # always fall back to en_us, the assumed template language
250 push(@locale_tags, 'en_us');
252 for my $idx (0..$#locale_tags) {
254 my $tag = $locale_tags[$idx];
255 next if grep { $_ eq $tag } keys %registered_locales;
257 my $res = $editor->json_query({
259 "evergreen.get_locale_name",
264 my $locale_name = $res->[0]->{"name"} if exists $res->[0]->{"name"};
265 next unless $locale_name;
270 # find the parent locale if possible. It will be
271 # longest left-anchored substring of the current tag
272 while( --$sub_idx >= 0 ) {
273 my $ptag = $locale_tags[$sub_idx];
274 if( substr($tag, 0, length($ptag)) eq $ptag ) {
275 $parent_tag = "::$ptag";
280 my $messages = $locales{$tag} || '';
282 # TODO Can we do this without eval?
283 my $eval = <<" EVAL";
284 package OpenILS::WWW::EGWeb::I18N::$tag;
285 use base 'OpenILS::WWW::EGWeb::I18N$parent_tag';
287 use Locale::Maketext::Lexicon {
290 use Locale::Maketext::Lexicon::Gettext;
291 if(open F, '$messages') {
292 our %Lexicon = (%Lexicon, %{ Locale::Maketext::Lexicon::Gettext->parse(<F>) });
295 warn "EGWeb: unable to open messages file: $messages";
304 $registered_locales{"$tag"} = $locale_name;
310 # base class for all supported locales
311 package OpenILS::WWW::EGWeb::I18N;
312 use base 'Locale::Maketext';
313 our %Lexicon = (_AUTO => 1);