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);
11 use constant OILS_HTTP_COOKIE_SKIN => 'oils:skin';
12 use constant OILS_HTTP_COOKIE_THEME => 'oils:theme';
13 use constant OILS_HTTP_COOKIE_LOCALE => 'oils:locale';
17 my $web_config_edit_time;
21 $web_config_file = shift || '';
22 unless(-r $web_config_file) {
23 warn "Invalid web config $web_config_file";
32 check_web_config($r); # option to disable this
33 my $ctx = load_context($r);
34 my $base = $ctx->{base_path};
35 my($template, $page_args, $as_xml) = find_template($r, $base, $ctx);
36 return Apache2::Const::DECLINED unless $template;
38 $template = $ctx->{skin} . "/$template";
39 $ctx->{page_args} = $page_args;
40 $r->content_type('text/html; encoding=utf8');
42 my $tt = Template->new({
43 OUTPUT => ($as_xml) ? sub { parse_as_xml($r, $ctx, @_); } : $r,
44 INCLUDE_PATH => $ctx->{template_paths},
47 unless($tt->process($template, {ctx => $ctx})) {
48 $r->log->warn('Template error: ' . $tt->error);
49 return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
52 return Apache2::Const::OK;
63 my $doc = XML::LibXML->new->parse_string($data);
64 $data = $doc->documentElement->toStringC14N;
65 $data = $ctx->{final_dtd} . "\n" . $data;
69 my $err = "Invalid XML: $e";
71 $r->content_type('text/plain; encoding=utf8');
72 $r->print("\n$err\n\n$data");
75 $r->print($data) if ($success);
82 my $ctx = $web_config->{ctx};
83 $ctx->{hostname} = $r->hostname;
84 $ctx->{base_url} = $cgi->url(-base => 1);
85 $ctx->{skin} = $cgi->cookie(OILS_HTTP_COOKIE_SKIN) || 'default';
86 $ctx->{theme} = $cgi->cookie(OILS_HTTP_COOKIE_THEME) || 'default';
88 $cgi->cookie(OILS_HTTP_COOKIE_LOCALE) ||
89 parse_accept_lang($r->headers_in->get('Accept-Language')) || 'en-US';
90 $r->log->debug('skin = ' . $ctx->{skin} . ' : theme = ' .
91 $ctx->{theme} . ' : locale = ' . $ctx->{locale});
95 # turn Accept-Language into sometihng EG can understand
96 sub parse_accept_lang {
98 return undef unless $al;
99 my ($locale) = split(/,/, $al);
100 ($locale) = split(/;/, $locale);
101 return undef unless $locale;
102 $locale =~ s/-(.*)/eval '-'.uc("$1")/e;
106 # Given a URI, finds the configured template and any extra page
107 # arguments (trailing path info). Any extra data is returned
108 # as page arguments, in the form of an array, one item per
109 # /-separated URI component
114 my $skin = $ctx->{skin};
116 $path =~ s/$base//og;
117 my @parts = split('/', $path);
120 my $as_xml = $ctx->{force_valid_xml};
121 my $handler = $web_config->{handlers};
124 my $part = shift @parts;
126 my $t = $handler->{$part};
127 if(ref($t) eq 'PathConfig') {
128 $template = $t->{template};
129 $as_xml = ($t->{as_xml} and $t->{as_xml} =~ /true/io) || $as_xml;
130 $page_args = [@parts];
137 unless($template) { # no template configured
139 # see if we can magically find the template based on the path and default extension
140 my $ext = $ctx->{default_template_extension};
142 my @parts = split('/', $path);
143 my $localpath = $path;
146 last unless $localpath;
147 for my $tpath (@{$ctx->{template_paths}}) {
148 my $fpath = "$tpath/$skin/$localpath.$ext";
149 $r->log->debug("looking at possible template $fpath");
151 $template = "$localpath.$ext";
156 push(@args, pop @parts);
157 $localpath = '/'.join('/', @parts);
160 $page_args = [@args];
162 # no template configured or found
164 $r->log->warn("No template configured for path $path");
169 $r->log->debug("template = $template : page args = @$page_args");
170 return ($template, $page_args, $as_xml);
173 # if the web configuration file has never been loaded or has
174 # changed since the last load, reload it
175 sub check_web_config {
177 my $epoch = stat($web_config_file)->mtime;
178 unless($web_config_edit_time and $web_config_edit_time == $epoch) {
179 $r->log->debug("Reloading web config after edit...") if $r;
180 $web_config_edit_time = $epoch;
181 $web_config = parse_config($web_config_file);
186 my $cfg_file = shift;
187 my $data = XML::Simple->new->XMLin($cfg_file);
191 $ctx->{media_prefix} = (ref $data->{media_prefix}) ? '' : $data->{media_prefix};
192 $ctx->{base_path} = (ref $data->{base_path}) ? '' : $data->{base_path};
193 $ctx->{template_paths} = [];
194 $ctx->{force_valid_xml} = ($data->{force_valid_xml} =~ /true/io) ? 1 : 0;
195 $ctx->{default_template_extension} = $data->{default_template_extension} || 'tt2';
196 $ctx->{web_dir} = $data->{web_dir};
198 my $tpaths = $data->{template_paths}->{path};
199 $tpaths = [$tpaths] unless ref $tpaths;
200 push(@{$ctx->{template_paths}}, $_) for @$tpaths;
202 for my $handler (@{$data->{handlers}->{handler}}) {
203 my @parts = split('/', $handler->{path});
205 my $pcount = scalar(@parts);
206 for(my $i = 0; $i < $pcount; $i++) {
208 unless(defined $h->{$p}) {
209 if($i == $pcount - 1) {
210 $h->{$p} = PathConfig->new(%$handler);
220 return {ctx => $ctx, handlers => $handlers};
225 my($class, %args) = @_;
226 return bless(\%args, $class);