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;
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';
19 my $web_config_edit_time;
20 my %lh_cache; # locale handlers
24 $web_config_file = shift || '';
25 unless(-r $web_config_file) {
26 warn "Invalid web config $web_config_file\n";
35 check_web_config($r); # option to disable this
36 my $ctx = load_context($r);
37 my $base = $ctx->{base_path};
39 $r->content_type('text/html; encoding=utf8');
41 my($template, $page_args, $as_xml) = find_template($r, $base, $ctx);
42 $ctx->{page_args} = $page_args;
44 my $stat = run_context_loader($r, $ctx);
46 return $stat unless $stat == Apache2::Const::OK;
47 return Apache2::Const::DECLINED unless $template;
49 $template = $ctx->{skin} . "/$template";
51 my $text_handler = set_text_handler($ctx, $r);
53 my $tt = Template->new({
54 OUTPUT => ($as_xml) ? sub { parse_as_xml($r, $ctx, @_); } : $r,
55 INCLUDE_PATH => $ctx->{template_paths},
56 DEBUG => $ctx->{debug_template},
58 EGI18N => 'OpenILS::WWW::EGWeb::I18NFilter',
59 CGI_utf8 => 'OpenILS::WWW::EGWeb::CGI_utf8'
62 # Register a dynamic filter factory for our locale::maketext generator
66 return sub { $text_handler->(shift(), @args); }
72 $ctx->{encode_utf8} = sub {return encode_utf8(shift())};
74 unless($tt->process($template, {ctx => $ctx, ENV => \%ENV, l => $text_handler})) {
75 $r->log->warn('egweb: template error: ' . $tt->error);
76 return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
79 return Apache2::Const::OK;
82 sub set_text_handler {
86 my $locale = $ctx->{locale};
89 $r->log->debug("egweb: messages locale = $locale");
91 unless($lh_cache{$locale}) {
92 $r->log->info("egweb: Unsupported locale: $locale");
93 $lh_cache{$locale} = $lh_cache{'en_US'};
96 return sub { return $lh_cache{$locale}->maketext(@_); };
101 sub run_context_loader {
105 my $stat = Apache2::Const::OK;
107 my $loader = $r->dir_config('OILSWebContextLoader');
108 return $stat unless $loader;
112 $stat = $loader->new($r, $ctx)->load;
116 $r->log->error("egweb: Context Loader error: $@");
117 return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
120 $r->log->debug("egweb: context loader resulted in status $stat");
132 my $doc = XML::LibXML->new->parse_string($data);
133 $data = $doc->documentElement->toStringC14N;
134 $data = $ctx->{final_dtd} . "\n" . $data;
138 my $err = "Invalid XML: $e";
139 $r->log->error("egweb: $err");
140 $r->content_type('text/plain; encoding=utf8');
141 $r->print("\n$err\n\n$data");
144 $r->print($data) if ($success);
151 my $ctx = {}; # new context for each page load
152 $ctx->{$_} = $web_config->{base_ctx}->{$_} for keys %{$web_config->{base_ctx}};
153 $ctx->{hostname} = $r->hostname;
154 $ctx->{base_url} = $cgi->url(-base => 1);
155 $ctx->{skin} = $cgi->cookie(OILS_HTTP_COOKIE_SKIN) || 'default';
156 $ctx->{theme} = $cgi->cookie(OILS_HTTP_COOKIE_THEME) || 'default';
159 $cgi->cookie(OILS_HTTP_COOKIE_LOCALE) ||
160 parse_accept_lang($r->headers_in->get('Accept-Language')) || 'en-US';
162 my $mprefix = $ctx->{media_prefix};
163 if($mprefix and $mprefix !~ /^http/ and $mprefix !~ /^\//) {
164 # if a hostname is provided /w no protocol, match the protocol to the current page
165 $ctx->{media_prefix} = ($cgi->https) ? "https://$mprefix" : "http://$mprefix";
171 # turn Accept-Language into sometihng EG can understand
172 sub parse_accept_lang {
174 return undef unless $al;
175 my ($locale) = split(/,/, $al);
176 ($locale) = split(/;/, $locale);
177 return undef unless $locale;
178 $locale =~ s/-(.*)/eval '-'.uc("$1")/e;
182 # Given a URI, finds the configured template and any extra page
183 # arguments (trailing path info). Any extra data is returned
184 # as page arguments, in the form of an array, one item per
185 # /-separated URI component
190 my $skin = $ctx->{skin};
192 $path =~ s/$base//og;
193 my @parts = split('/', $path);
196 my $as_xml = $ctx->{force_valid_xml};
197 my $handler = $web_config->{handlers};
200 my $part = shift @parts;
202 my $t = $handler->{$part};
203 if(ref($t) eq 'PathConfig') {
204 $template = $t->{template};
205 $as_xml = ($t->{as_xml} and $t->{as_xml} =~ /true/io) || $as_xml;
206 $page_args = [@parts];
213 unless($template) { # no template configured
215 # see if we can magically find the template based on the path and default extension
216 my $ext = $ctx->{default_template_extension};
218 my @parts = split('/', $path);
219 my $localpath = $path;
222 last unless $localpath;
223 for my $tpath (@{$ctx->{template_paths}}) {
224 my $fpath = "$tpath/$skin/$localpath.$ext";
225 $r->log->debug("egweb: looking at possible template $fpath");
227 $template = "$localpath.$ext";
232 push(@args, pop @parts);
233 $localpath = '/'.join('/', @parts);
236 $page_args = [@args];
238 # no template configured or found
240 $r->log->debug("egweb: No template configured for path $path");
245 $r->log->debug("egweb: template = $template : page args = @$page_args");
246 return ($template, $page_args, $as_xml);
249 # if the web configuration file has never been loaded or has
250 # changed since the last load, reload it
251 sub check_web_config {
253 my $epoch = stat($web_config_file)->mtime;
254 unless($web_config_edit_time and $web_config_edit_time == $epoch) {
255 $r->log->debug("egweb: Reloading web config after edit...") if $r;
256 $web_config_edit_time = $epoch;
257 $web_config = parse_config($web_config_file);
261 # Create an I18N sub-module for each supported locale
262 # Each module creates its own MakeText lexicon by parsing .po/.mo files
263 sub load_locale_handlers {
265 my $locales = $ctx->{locales};
267 $locales->{en_US} = {} unless exists $locales->{en_US};
269 for my $lang (keys %$locales) {
270 my $messages = $locales->{$lang};
271 $messages = '' if ref $messages; # empty {}
273 # TODO Can we do this without eval?
275 package OpenILS::WWW::EGWeb::I18N::$lang;
276 use base 'OpenILS::WWW::EGWeb::I18N';
278 use Locale::Maketext::Lexicon::Gettext;
279 if(open F, '$messages') {
280 our %Lexicon = (%Lexicon, %{ Locale::Maketext::Lexicon::Gettext->parse(<F>) });
283 warn "EGWeb: unable to open messages file: $messages";
288 warn "$@\n" if $@; # TODO better logging
289 $lh_cache{$lang} = "OpenILS::WWW::EGWeb::I18N::$lang"->new;
296 my $cfg_file = shift;
297 my $data = XML::Simple->new->XMLin($cfg_file);
301 $ctx->{media_prefix} = (ref $data->{media_prefix}) ? '' : $data->{media_prefix};
302 $ctx->{base_path} = (ref $data->{base_path}) ? '' : $data->{base_path};
303 $ctx->{template_paths} = [];
304 $ctx->{force_valid_xml} = ( ($data->{force_valid_xml}||'') =~ /true/io) ? 1 : 0;
305 $ctx->{debug_template} = ( ($data->{debug_template}||'') =~ /true/io) ? 1 : 0;
306 $ctx->{default_template_extension} = $data->{default_template_extension} || 'tt2';
307 $ctx->{web_dir} = $data->{web_dir};
308 $ctx->{locales} = $data->{locales};
309 load_locale_handlers($ctx);
311 my $tpaths = $data->{template_paths}->{path};
312 $tpaths = [$tpaths] unless ref $tpaths;
313 push(@{$ctx->{template_paths}}, $_) for @$tpaths;
315 for my $handler (@{$data->{handlers}->{handler}}) {
316 my @parts = split('/', $handler->{path});
318 my $pcount = scalar(@parts);
319 for(my $i = 0; $i < $pcount; $i++) {
321 unless(defined $h->{$p}) {
322 if($i == $pcount - 1) {
323 $h->{$p} = PathConfig->new(%$handler);
333 return {base_ctx => $ctx, handlers => $handlers};
338 my($class, %args) = @_;
339 return bless(\%args, $class);
342 # base class for all supported locales
343 package OpenILS::WWW::EGWeb::I18N;
344 use base 'Locale::Maketext';
345 our %Lexicon = (_AUTO => 1);