]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/WWW/EGWeb.pm
50de099ac744d1c380fac086ce26387d48444e08
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / WWW / EGWeb.pm
1 package OpenILS::WWW::EGWeb;
2 use strict; use warnings;
3 use Template;
4 use XML::Simple;
5 use XML::LibXML;
6 use File::stat;
7 use Apache2::Const -compile => qw(OK DECLINED HTTP_INTERNAL_SERVER_ERROR);
8 use Apache2::Log;
9 use OpenSRF::EX qw(:try);
10 use OpenILS::Utils::CStoreEditor;
11
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';
15
16 my $web_config;
17 my $web_config_file;
18 my $web_config_edit_time;
19
20 sub import {
21     my $self = shift;
22     $web_config_file = shift;
23     unless(-r $web_config_file) {
24         warn "Invalid web config $web_config_file";
25         return;
26     }
27     check_web_config();
28 }
29
30
31 sub handler {
32     my $r = shift;
33     check_web_config($r); # option to disable this
34     my $ctx = load_context($r);
35     my $base = $ctx->{base_path};
36
37     $r->content_type('text/html; encoding=utf8');
38     my $stat = run_context_loader($r, $ctx);
39     return $stat unless $stat == Apache2::Const::OK;
40
41     my($template, $page_args, $as_xml) = find_template($r, $base, $ctx);
42     return Apache2::Const::DECLINED unless $template;
43
44     $template = $ctx->{skin} . "/$template";
45     $ctx->{page_args} = $page_args;
46
47     my $tt = Template->new({
48         OUTPUT => ($as_xml) ?  sub { parse_as_xml($r, $ctx, @_); } : $r,
49         INCLUDE_PATH => $ctx->{template_paths},
50         DEBUG => $ctx->{debug_template}
51     });
52
53     unless($tt->process($template, {ctx => $ctx})) {
54         $r->log->warn('Template error: ' . $tt->error);
55         return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
56     }
57
58     return Apache2::Const::OK;
59 }
60
61
62 sub run_context_loader {
63     my $r = shift;
64     my $ctx = shift;
65
66     my $stat = Apache2::Const::OK;
67
68     my $loader = $r->dir_config('OILSWebContextLoader');
69     return $stat unless $loader;
70
71     eval {
72         $loader->use;
73         $stat = $loader->new($r, $ctx)->load;
74     };
75
76     if($@) {
77         $r->log->error("Context Loader error: $@");
78         return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
79     }
80
81     $r->log->warn("context loader resulted in status $stat");
82     return $stat;
83 }
84
85 sub parse_as_xml {
86     my $r = shift;
87     my $ctx = shift;
88     my $data = shift;
89
90     my $success = 0;
91
92     try { 
93         my $doc = XML::LibXML->new->parse_string($data); 
94         $data = $doc->documentElement->toStringC14N;
95         $data = $ctx->{final_dtd} . "\n" . $data;
96         $success = 1;
97     } otherwise {
98             my $e = shift;
99         my $err = "Invalid XML: $e";
100         $r->log->error($err);
101         $r->content_type('text/plain; encoding=utf8');
102         $r->print("\n$err\n\n$data");
103     };
104
105     $r->print($data) if ($success);
106 }
107
108
109 sub load_context {
110     my $r = shift;
111     my $cgi = CGI->new;
112     my $ctx = {}; # new context for each page load
113     $ctx->{$_} = $web_config->{base_ctx}->{$_} for keys %{$web_config->{base_ctx}};
114     $ctx->{hostname} = $r->hostname;
115     $ctx->{base_url} = $cgi->url(-base => 1);
116     $ctx->{skin} = $cgi->cookie(OILS_HTTP_COOKIE_SKIN) || 'default';
117     $ctx->{theme} = $cgi->cookie(OILS_HTTP_COOKIE_THEME) || 'default';
118     $ctx->{locale} = 
119         $cgi->cookie(OILS_HTTP_COOKIE_LOCALE) || 
120         parse_accept_lang($r->headers_in->get('Accept-Language')) || 'en-US';
121     $r->log->debug('skin = ' . $ctx->{skin} . ' : theme = ' . 
122         $ctx->{theme} . ' : locale = ' . $ctx->{locale});
123     return $ctx;
124 }
125
126 # turn Accept-Language into sometihng EG can understand
127 sub parse_accept_lang {
128     my $al = shift;
129     return undef unless $al;
130     my ($locale) = split(/,/, $al);
131     ($locale) = split(/;/, $locale);
132     return undef unless $locale;
133     $locale =~ s/-(.*)/eval '-'.uc("$1")/e;
134     return $locale;
135 }
136
137 # Given a URI, finds the configured template and any extra page 
138 # arguments (trailing path info).  Any extra data is returned
139 # as page arguments, in the form of an array, one item per 
140 # /-separated URI component
141 sub find_template {
142     my $r = shift;
143     my $base = shift;
144     my $ctx = shift;
145     my $skin = $ctx->{skin};
146     my $path = $r->uri;
147     $path =~ s/$base//og;
148     my @parts = split('/', $path);
149     my $template = '';
150     my $page_args = [];
151     my $as_xml = $ctx->{force_valid_xml};
152     my $handler = $web_config->{handlers};
153
154     while(@parts) {
155         my $part = shift @parts;
156         next unless $part;
157         my $t = $handler->{$part};
158         if(ref($t) eq 'PathConfig') {
159             $template = $t->{template};
160             $as_xml = ($t->{as_xml} and $t->{as_xml} =~ /true/io) || $as_xml;
161             $page_args = [@parts];
162             last;
163         } else {
164             $handler = $t;
165         }
166     }
167
168     unless($template) { # no template configured
169
170         # see if we can magically find the template based on the path and default extension
171         my $ext = $ctx->{default_template_extension};
172
173         my @parts = split('/', $path);
174         my $localpath = $path;
175         my @args;
176         while(@parts) {
177             last unless $localpath;
178             for my $tpath (@{$ctx->{template_paths}}) {
179                 my $fpath = "$tpath/$skin/$localpath.$ext";
180                 $r->log->debug("looking at possible template $fpath");
181                 if(-r $fpath) {
182                     $template = "$localpath.$ext";
183                     last;
184                 }
185             }
186             last if $template;
187             push(@args, pop @parts);
188             $localpath = '/'.join('/', @parts);
189         } 
190
191         $page_args = [@args];
192
193         # no template configured or found
194         unless($template) {
195             $r->log->warn("No template configured for path $path");
196             return ();
197         }
198     }
199
200     $r->log->debug("template = $template : page args = @$page_args");
201     return ($template, $page_args, $as_xml);
202 }
203
204 # if the web configuration file has never been loaded or has
205 # changed since the last load, reload it
206 sub check_web_config {
207     my $r = shift;
208     my $epoch = stat($web_config_file)->mtime;
209     unless($web_config_edit_time and $web_config_edit_time == $epoch) {
210         $r->log->debug("Reloading web config after edit...") if $r;
211         $web_config_edit_time = $epoch;
212         $web_config = parse_config($web_config_file);
213     }
214 }
215
216 sub parse_config {
217     my $cfg_file = shift;
218     my $data = XML::Simple->new->XMLin($cfg_file);
219     my $ctx = {};
220     my $handlers = {};
221
222     $ctx->{media_prefix} = (ref $data->{media_prefix}) ? '' : $data->{media_prefix};
223     $ctx->{base_path} = (ref $data->{base_path}) ? '' : $data->{base_path};
224     $ctx->{template_paths} = [];
225     $ctx->{force_valid_xml} = ($data->{force_valid_xml} =~ /true/io) ? 1 : 0;
226     $ctx->{debug_template} = ($data->{debug_template} =~ /true/io) ? 1 : 0;
227     $ctx->{default_template_extension} = $data->{default_template_extension} || 'tt2';
228     $ctx->{web_dir} = $data->{web_dir};
229
230     my $tpaths = $data->{template_paths}->{path};
231     $tpaths = [$tpaths] unless ref $tpaths;
232     push(@{$ctx->{template_paths}}, $_) for @$tpaths;
233
234     for my $handler (@{$data->{handlers}->{handler}}) {
235         my @parts = split('/', $handler->{path});
236         my $h = $handlers;
237         my $pcount = scalar(@parts);
238         for(my $i = 0; $i < $pcount; $i++) {
239             my $p = $parts[$i];
240             unless(defined $h->{$p}) {
241                 if($i == $pcount - 1) {
242                     $h->{$p} = PathConfig->new(%$handler);
243                     last;
244                 } else {
245                     $h->{$p} = {};
246                 }
247             }
248             $h = $h->{$p};
249         }
250     }
251
252     return {base_ctx => $ctx, handlers => $handlers};
253 }
254
255 package PathConfig;
256 sub new {
257     my($class, %args) = @_;
258     return bless(\%args, $class);
259 }
260
261
262 1;