1 package OpenILS::WWW::EGWeb;
2 use strict; use warnings;
7 use Apache2::Const -compile => qw(OK DECLINED HTTP_INTERNAL_SERVER_ERROR);
9 use OpenSRF::EX qw(:try);
10 use OpenILS::Utils::CStoreEditor;
12 use constant OILS_HTTP_COOKIE_SKIN => 'oils:skin';
13 use constant OILS_HTTP_COOKIE_THEME => 'oils:theme';
14 use constant OILS_HTTP_COOKIE_LOCALE => 'oils:locale';
18 my $web_config_edit_time;
22 $web_config_file = shift;
23 unless(-r $web_config_file) {
24 warn "Invalid web config $web_config_file\n";
33 check_web_config($r); # option to disable this
34 my $ctx = load_context($r);
35 my $base = $ctx->{base_path};
37 $r->content_type('text/html; encoding=utf8');
39 my($template, $page_args, $as_xml) = find_template($r, $base, $ctx);
40 $ctx->{page_args} = $page_args;
42 my $stat = run_context_loader($r, $ctx);
44 return $stat unless $stat == Apache2::Const::OK;
45 return Apache2::Const::DECLINED unless $template;
47 $template = $ctx->{skin} . "/$template";
49 my $tt = Template->new({
50 OUTPUT => ($as_xml) ? sub { parse_as_xml($r, $ctx, @_); } : $r,
51 INCLUDE_PATH => $ctx->{template_paths},
52 DEBUG => $ctx->{debug_template}
55 unless($tt->process($template, {ctx => $ctx})) {
56 $r->log->warn('Template error: ' . $tt->error);
57 return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
60 return Apache2::Const::OK;
64 sub run_context_loader {
68 my $stat = Apache2::Const::OK;
70 my $loader = $r->dir_config('OILSWebContextLoader');
71 return $stat unless $loader;
75 $stat = $loader->new($r, $ctx)->load;
79 $r->log->error("Context Loader error: $@");
80 return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
83 $r->log->info("context loader resulted in status $stat");
95 my $doc = XML::LibXML->new->parse_string($data);
96 $data = $doc->documentElement->toStringC14N;
97 $data = $ctx->{final_dtd} . "\n" . $data;
101 my $err = "Invalid XML: $e";
102 $r->log->error($err);
103 $r->content_type('text/plain; encoding=utf8');
104 $r->print("\n$err\n\n$data");
107 $r->print($data) if ($success);
114 my $ctx = {}; # new context for each page load
115 $ctx->{$_} = $web_config->{base_ctx}->{$_} for keys %{$web_config->{base_ctx}};
116 $ctx->{hostname} = $r->hostname;
117 $ctx->{base_url} = $cgi->url(-base => 1);
118 $ctx->{skin} = $cgi->cookie(OILS_HTTP_COOKIE_SKIN) || 'default';
119 $ctx->{theme} = $cgi->cookie(OILS_HTTP_COOKIE_THEME) || 'default';
121 $cgi->cookie(OILS_HTTP_COOKIE_LOCALE) ||
122 parse_accept_lang($r->headers_in->get('Accept-Language')) || 'en-US';
123 $r->log->debug('skin = ' . $ctx->{skin} . ' : theme = ' .
124 $ctx->{theme} . ' : locale = ' . $ctx->{locale});
126 my $mprefix = $ctx->{media_prefix};
127 if($mprefix and $mprefix !~ /^http/ and $mprefix !~ /^\//) {
128 # if a hostname is provided /w no protocol, match the protocol to the current page
129 $ctx->{media_prefix} = ($cgi->https) ? "https://$mprefix" : "http://$mprefix";
136 # turn Accept-Language into sometihng EG can understand
137 sub parse_accept_lang {
139 return undef unless $al;
140 my ($locale) = split(/,/, $al);
141 ($locale) = split(/;/, $locale);
142 return undef unless $locale;
143 $locale =~ s/-(.*)/eval '-'.uc("$1")/e;
147 # Given a URI, finds the configured template and any extra page
148 # arguments (trailing path info). Any extra data is returned
149 # as page arguments, in the form of an array, one item per
150 # /-separated URI component
155 my $skin = $ctx->{skin};
157 $path =~ s/$base//og;
158 my @parts = split('/', $path);
161 my $as_xml = $ctx->{force_valid_xml};
162 my $handler = $web_config->{handlers};
165 my $part = shift @parts;
167 my $t = $handler->{$part};
168 if(ref($t) eq 'PathConfig') {
169 $template = $t->{template};
170 $as_xml = ($t->{as_xml} and $t->{as_xml} =~ /true/io) || $as_xml;
171 $page_args = [@parts];
178 unless($template) { # no template configured
180 # see if we can magically find the template based on the path and default extension
181 my $ext = $ctx->{default_template_extension};
183 my @parts = split('/', $path);
184 my $localpath = $path;
187 last unless $localpath;
188 for my $tpath (@{$ctx->{template_paths}}) {
189 my $fpath = "$tpath/$skin/$localpath.$ext";
190 $r->log->debug("looking at possible template $fpath");
192 $template = "$localpath.$ext";
197 push(@args, pop @parts);
198 $localpath = '/'.join('/', @parts);
201 $page_args = [@args];
203 # no template configured or found
205 $r->log->warn("No template configured for path $path");
210 $r->log->debug("template = $template : page args = @$page_args");
211 return ($template, $page_args, $as_xml);
214 # if the web configuration file has never been loaded or has
215 # changed since the last load, reload it
216 sub check_web_config {
218 my $epoch = stat($web_config_file)->mtime;
219 unless($web_config_edit_time and $web_config_edit_time == $epoch) {
220 $r->log->debug("Reloading web config after edit...") if $r;
221 $web_config_edit_time = $epoch;
222 $web_config = parse_config($web_config_file);
227 my $cfg_file = shift;
228 my $data = XML::Simple->new->XMLin($cfg_file);
232 $ctx->{media_prefix} = (ref $data->{media_prefix}) ? '' : $data->{media_prefix};
233 $ctx->{base_path} = (ref $data->{base_path}) ? '' : $data->{base_path};
234 $ctx->{template_paths} = [];
235 $ctx->{force_valid_xml} = ($data->{force_valid_xml} =~ /true/io) ? 1 : 0;
236 $ctx->{debug_template} = ($data->{debug_template} =~ /true/io) ? 1 : 0;
237 $ctx->{default_template_extension} = $data->{default_template_extension} || 'tt2';
238 $ctx->{web_dir} = $data->{web_dir};
240 my $tpaths = $data->{template_paths}->{path};
241 $tpaths = [$tpaths] unless ref $tpaths;
242 push(@{$ctx->{template_paths}}, $_) for @$tpaths;
244 for my $handler (@{$data->{handlers}->{handler}}) {
245 my @parts = split('/', $handler->{path});
247 my $pcount = scalar(@parts);
248 for(my $i = 0; $i < $pcount; $i++) {
250 unless(defined $h->{$p}) {
251 if($i == $pcount - 1) {
252 $h->{$p} = PathConfig->new(%$handler);
262 return {base_ctx => $ctx, handlers => $handlers};
267 my($class, %args) = @_;
268 return bless(\%args, $class);