1 package OpenILS::WWW::EGWeb;
2 use strict; use warnings;
7 use Apache2::Const -compile => qw(OK DECLINED HTTP_INTERNAL_SERVER_ERROR);
10 use constant OILS_HTTP_COOKIE_SKIN => 'oils:skin';
11 use constant OILS_HTTP_COOKIE_THEME => 'oils:theme';
12 use constant OILS_HTTP_COOKIE_LOCALE => 'oils:locale';
16 my $web_config_edit_time;
20 $web_config_file = shift;
21 unless(-r $web_config_file) {
22 warn "Invalid web config $web_config_file";
31 check_web_config($r); # option to disable this
32 my $ctx = load_context($r);
33 my $base = $ctx->{base_path};
34 my($template, $page_args, $as_xml) = find_template($r, $base, $ctx);
35 return Apache2::Const::DECLINED unless $template;
37 $template = $ctx->{skin} . "/$template";
38 $ctx->{page_args} = $page_args;
39 $r->content_type('text/html; encoding=utf8');
41 my $tt = Template->new({
42 OUTPUT => ($as_xml) ? sub { parse_as_xml($r, $ctx, @_); } : $r,
43 INCLUDE_PATH => $ctx->{template_paths},
46 unless($tt->process($template, {ctx => $ctx})) {
47 $r->log->warn('Template error: ' . $tt->error);
48 return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
51 return Apache2::Const::OK;
60 my $doc = XML::LibXML->new->parse_string($data);
61 $data = $doc->documentElement->toStringC14N;
62 $data = $ctx->{final_dtd} . "\n" . $data;
66 my $err = "Invalid XML: $@";
68 $r->content_type('text/plain; encoding=utf8');
69 $r->print("\n$err\n\n$data");
79 my $ctx = $web_config->{ctx};
80 $ctx->{hostname} = $r->hostname;
81 $ctx->{base_url} = $cgi->url(-base => 1);
82 $ctx->{skin} = $cgi->cookie(OILS_HTTP_COOKIE_SKIN) || 'default';
83 $ctx->{theme} = $cgi->cookie(OILS_HTTP_COOKIE_THEME) || 'default';
85 $cgi->cookie(OILS_HTTP_COOKIE_LOCALE) ||
86 parse_accept_lang($r->headers_in->get('Accept-Language')) || 'en-US'; # this will need some trimming
87 $r->log->debug('skin = ' . $ctx->{skin} . ' : theme = ' .
88 $ctx->{theme} . ' : locale = ' . $ctx->{locale});
92 # turn Accept-Language into sometihng EG can understand
93 sub parse_accept_lang {
95 return undef unless $al;
96 my ($locale) = split(/,/, $al);
97 ($locale) = split(/;/, $locale);
98 return undef unless $locale;
99 $locale =~ s/-(.*)/eval '-'.uc("$1")/e;
103 # Given a URI, finds the configured template and any extra page
104 # arguments (trailing path info). Any extra data is returned
105 # as page arguments, in the form of an array, one item per
106 # /-separated URI component
111 my $skin = $ctx->{skin};
113 $path =~ s/$base//og;
114 my @parts = split('/', $path);
117 my $as_xml = $ctx->{force_valid_xml};
118 my $handler = $web_config->{handlers};
121 my $part = shift @parts;
123 my $t = $handler->{$part};
124 if(ref($t) eq 'PathConfig') {
125 $template = $t->{template};
126 $as_xml = ($t->{as_xml} and $t->{as_xml} =~ /true/io) || $as_xml;
127 $page_args = [@parts];
134 unless($template) { # no template configured
136 # see if we can magically find the template based on the path and default extension
137 my $ext = $ctx->{default_template_extension};
139 my @parts = split('/', $path);
140 my $localpath = $path;
143 last unless $localpath;
144 for my $tpath (@{$ctx->{template_paths}}) {
145 my $fpath = "$tpath/$skin/$localpath.$ext";
146 $r->log->debug("looking at possible template $fpath");
148 $template = "$localpath.$ext";
153 push(@args, pop @parts);
154 $localpath = '/'.join('/', @parts);
157 $page_args = [@args];
159 # no template configured or found
161 $r->log->warn("No template configured for path $path");
166 $r->log->debug("template = $template : page args = @$page_args");
167 return ($template, $page_args, $as_xml);
170 # if the web configuration file has never been loaded or has
171 # changed since the last load, reload it
172 sub check_web_config {
174 my $epoch = stat($web_config_file)->mtime;
175 unless($web_config_edit_time and $web_config_edit_time == $epoch) {
176 $r->log->debug("Reloading web config after edit...") if $r;
177 $web_config_edit_time = $epoch;
178 $web_config = parse_config($web_config_file);
183 my $cfg_file = shift;
184 my $data = XML::Simple->new->XMLin($cfg_file);
188 $ctx->{media_prefix} = (ref $data->{media_prefix}) ? '' : $data->{media_prefix};
189 $ctx->{base_path} = (ref $data->{base_path}) ? '' : $data->{base_path};
190 $ctx->{template_paths} = [];
191 $ctx->{force_valid_xml} = ($data->{force_valid_xml} =~ /true/io) ? 1 : 0;
192 $ctx->{default_template_extension} = $data->{default_template_extension} || 'tt2';
193 $ctx->{web_dir} = $data->{web_dir};
195 my $tpaths = $data->{template_paths}->{path};
196 $tpaths = [$tpaths] unless ref $tpaths;
197 push(@{$ctx->{template_paths}}, $_) for @$tpaths;
199 for my $handler (@{$data->{handlers}->{handler}}) {
200 my @parts = split('/', $handler->{path});
202 my $pcount = scalar(@parts);
203 for(my $i = 0; $i < $pcount; $i++) {
205 unless(defined $h->{$p}) {
206 if($i == $pcount - 1) {
207 $h->{$p} = PathConfig->new(%$handler);
217 return {ctx => $ctx, handlers => $handlers};
222 my($class, %args) = @_;
223 return bless(\%args, $class);