]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/WWW/EGWeb.pm
move template finder in front of context loader, so that context loader has access...
[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
39     my($template, $page_args, $as_xml) = find_template($r, $base, $ctx);
40     $ctx->{page_args} = $page_args;
41
42     my $stat = run_context_loader($r, $ctx);
43
44     return $stat unless $stat == Apache2::Const::OK;
45     return Apache2::Const::DECLINED unless $template;
46
47     $template = $ctx->{skin} . "/$template";
48
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}
53     });
54
55     unless($tt->process($template, {ctx => $ctx})) {
56         $r->log->warn('Template error: ' . $tt->error);
57         return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
58     }
59
60     return Apache2::Const::OK;
61 }
62
63
64 sub run_context_loader {
65     my $r = shift;
66     my $ctx = shift;
67
68     my $stat = Apache2::Const::OK;
69
70     my $loader = $r->dir_config('OILSWebContextLoader');
71     return $stat unless $loader;
72
73     eval {
74         $loader->use;
75         $stat = $loader->new($r, $ctx)->load;
76     };
77
78     if($@) {
79         $r->log->error("Context Loader error: $@");
80         return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
81     }
82
83     $r->log->warn("context loader resulted in status $stat");
84     return $stat;
85 }
86
87 sub parse_as_xml {
88     my $r = shift;
89     my $ctx = shift;
90     my $data = shift;
91
92     my $success = 0;
93
94     try { 
95         my $doc = XML::LibXML->new->parse_string($data); 
96         $data = $doc->documentElement->toStringC14N;
97         $data = $ctx->{final_dtd} . "\n" . $data;
98         $success = 1;
99     } otherwise {
100             my $e = shift;
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");
105     };
106
107     $r->print($data) if ($success);
108 }
109
110
111 sub load_context {
112     my $r = shift;
113     my $cgi = CGI->new;
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';
120     $ctx->{locale} = 
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});
125
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";
130     }
131
132
133     return $ctx;
134 }
135
136 # turn Accept-Language into sometihng EG can understand
137 sub parse_accept_lang {
138     my $al = shift;
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;
144     return $locale;
145 }
146
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
151 sub find_template {
152     my $r = shift;
153     my $base = shift;
154     my $ctx = shift;
155     my $skin = $ctx->{skin};
156     my $path = $r->uri;
157     $path =~ s/$base//og;
158     my @parts = split('/', $path);
159     my $template = '';
160     my $page_args = [];
161     my $as_xml = $ctx->{force_valid_xml};
162     my $handler = $web_config->{handlers};
163
164     while(@parts) {
165         my $part = shift @parts;
166         next unless $part;
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];
172             last;
173         } else {
174             $handler = $t;
175         }
176     }
177
178     unless($template) { # no template configured
179
180         # see if we can magically find the template based on the path and default extension
181         my $ext = $ctx->{default_template_extension};
182
183         my @parts = split('/', $path);
184         my $localpath = $path;
185         my @args;
186         while(@parts) {
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");
191                 if(-r $fpath) {
192                     $template = "$localpath.$ext";
193                     last;
194                 }
195             }
196             last if $template;
197             push(@args, pop @parts);
198             $localpath = '/'.join('/', @parts);
199         } 
200
201         $page_args = [@args];
202
203         # no template configured or found
204         unless($template) {
205             $r->log->warn("No template configured for path $path");
206             return ();
207         }
208     }
209
210     $r->log->debug("template = $template : page args = @$page_args");
211     return ($template, $page_args, $as_xml);
212 }
213
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 {
217     my $r = shift;
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);
223     }
224 }
225
226 sub parse_config {
227     my $cfg_file = shift;
228     my $data = XML::Simple->new->XMLin($cfg_file);
229     my $ctx = {};
230     my $handlers = {};
231
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};
239
240     my $tpaths = $data->{template_paths}->{path};
241     $tpaths = [$tpaths] unless ref $tpaths;
242     push(@{$ctx->{template_paths}}, $_) for @$tpaths;
243
244     for my $handler (@{$data->{handlers}->{handler}}) {
245         my @parts = split('/', $handler->{path});
246         my $h = $handlers;
247         my $pcount = scalar(@parts);
248         for(my $i = 0; $i < $pcount; $i++) {
249             my $p = $parts[$i];
250             unless(defined $h->{$p}) {
251                 if($i == $pcount - 1) {
252                     $h->{$p} = PathConfig->new(%$handler);
253                     last;
254                 } else {
255                     $h->{$p} = {};
256                 }
257             }
258             $h = $h->{$p};
259         }
260     }
261
262     return {base_ctx => $ctx, handlers => $handlers};
263 }
264
265 package PathConfig;
266 sub new {
267     my($class, %args) = @_;
268     return bless(\%args, $class);
269 }
270
271
272 1;