1 package OpenILS::WWW::EGWeb;
2 use strict; use warnings;
6 use Apache2::Const -compile => qw(OK DECLINED HTTP_INTERNAL_SERVER_ERROR);
9 use constant OILS_HTTP_COOKIE_SKIN => 'oils:skin';
10 use constant OILS_HTTP_COOKIE_THEME => 'oils:theme';
11 use constant OILS_HTTP_COOKIE_LOCALE => 'oils:locale';
15 my $web_config_edit_time;
19 $web_config_file = shift;
20 unless(-r $web_config_file) {
21 warn "Invalid web config $web_config_file";
30 check_web_config($r); # option to disable this
31 my $ctx = load_context($r);
32 my $base = $ctx->{base_uri};
33 my($template, $page_args) = find_template($r, $base);
34 return Apache2::Const::DECLINED unless $template;
36 $template = $ctx->{skin} . "/$template";
37 $ctx->{page_args} = $page_args;
38 $r->content_type('text/html; encoding=utf8');
40 my $tt = Template->new({
42 INCLUDE_PATH => $ctx->{template_paths},
45 unless($tt->process($template, {ctx => $ctx})) {
46 $r->log->warn('Template error: ' . $tt->error);
47 return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
50 return Apache2::Const::OK;
56 my $ctx = $web_config->{ctx};
57 $ctx->{skin} = $cgi->cookie(OILS_HTTP_COOKIE_SKIN) || 'default';
58 $ctx->{theme} = $cgi->cookie(OILS_HTTP_COOKIE_THEME) || 'default';
60 $r->headers_in->get('Accept-Language') || # this will need some trimming
61 $cgi->cookie(OILS_HTTP_COOKIE_LOCALE) || 'en-US';
62 $r->log->debug('skin = ' . $ctx->{skin} . ' : theme = ' .
63 $ctx->{theme} . ' : locale = ' . $ctx->{locale});
67 # Given a URI, finds the configured template and any extra page
68 # arguments (trailing path info). Any extra data is returned
69 # as page arguments, in the form of an array, one item per
70 # /-separated URI component
76 my @parts = split('/', $path);
79 my $handler = $web_config->{handlers};
81 my $part = shift @parts;
83 my $t = $handler->{$part};
88 $page_args = [@parts];
94 $r->log->warn("No template configured for path $path");
98 $r->log->debug("template = $template : page args = @$page_args");
99 return ($template, $page_args);
102 # if the web configuration file has never been loaded or has
103 # changed since the last load, reload it
104 sub check_web_config {
106 my $epoch = stat($web_config_file)->mtime;
107 unless($web_config_edit_time and $web_config_edit_time == $epoch) {
108 $r->log->debug("Reloading web config after edit...") if $r;
109 $web_config_edit_time = $epoch;
110 $web_config = parse_config($web_config_file);
115 my $cfg_file = shift;
116 my $data = XML::Simple->new->XMLin($cfg_file);
120 $ctx->{media_prefix} = (ref $data->{media_prefix}) ? '' : $data->{media_prefix};
121 $ctx->{base_uri} = (ref $data->{base_uri}) ? '' : $data->{base_uri};
122 $ctx->{template_paths} = [];
124 my $tpaths = $data->{template_paths}->{path};
125 $tpaths = [$tpaths] unless ref $tpaths;
126 push(@{$ctx->{template_paths}}, $_) for @$tpaths;
128 for my $handler (@{$data->{handlers}->{handler}}) {
129 my @parts = split('/', $handler->{path});
131 my $pcount = scalar(@parts);
132 for(my $i = 0; $i < $pcount; $i++) {
134 unless(defined $h->{$p}) {
135 if($i == $pcount - 1) {
136 $h->{$p} = $handler->{template};
146 return {ctx => $ctx, handlers => $handlers};