CGIUTF8 was a horrible, subtle, dark failure
[evergreen-equinox.git] / Open-ILS / src / perlmods / lib / OpenILS / WWW / EGCatLoader.pm
1 package OpenILS::WWW::EGCatLoader;
2 use strict; use warnings;
3 use XML::LibXML;
4 use URI::Escape;
5 use Digest::MD5 qw(md5_hex);
6 use Apache2::Const -compile => qw(OK DECLINED FORBIDDEN HTTP_INTERNAL_SERVER_ERROR REDIRECT HTTP_BAD_REQUEST);
7 use OpenSRF::AppSession;
8 use OpenSRF::EX qw/:try/;
9 use OpenSRF::Utils qw/:datetime/;
10 use OpenSRF::Utils::JSON;
11 use OpenSRF::Utils::Logger qw/$logger/;
12 use OpenILS::Application::AppUtils;
13 use OpenILS::Utils::CStoreEditor qw/:funcs/;
14 use OpenILS::Utils::Fieldmapper;
15 use DateTime::Format::ISO8601;
16 use CGI qw(-utf8);
17
18 # EGCatLoader sub-modules 
19 use OpenILS::WWW::EGCatLoader::Util;
20 use OpenILS::WWW::EGCatLoader::Account;
21 use OpenILS::WWW::EGCatLoader::Search;
22 use OpenILS::WWW::EGCatLoader::Record;
23 use OpenILS::WWW::EGCatLoader::Container;
24
25 my $U = 'OpenILS::Application::AppUtils';
26
27 use constant COOKIE_SES => 'ses';
28
29 sub new {
30     my($class, $apache, $ctx) = @_;
31
32     my $self = bless({}, ref($class) || $class);
33
34     $self->apache($apache);
35     $self->ctx($ctx);
36     $self->cgi(new CGI);
37
38     my $msg1 = "LFW XXX: param('query') is " . $self->cgi->param('query');
39     $logger->info("LFW XXX: msg1 is " . $msg1);
40     $logger->info("LFW XXX: query_string is " . $self->cgi->query_string());
41
42     OpenILS::Utils::CStoreEditor->init; # just in case
43     $self->editor(new_editor());
44
45     return $self;
46 }
47
48
49 # current Apache2::RequestRec;
50 sub apache {
51     my($self, $apache) = @_;
52     $self->{apache} = $apache if $apache;
53     return $self->{apache};
54 }
55
56 # runtime / template context
57 sub ctx {
58     my($self, $ctx) = @_;
59     $self->{ctx} = $ctx if $ctx;
60     return $self->{ctx};
61 }
62
63 # cstore editor
64 sub editor {
65     my($self, $editor) = @_;
66     $self->{editor} = $editor if $editor;
67     return $self->{editor};
68 }
69
70 # CGI handle
71 sub cgi {
72     my($self, $cgi) = @_;
73     $self->{cgi} = $cgi if $cgi;
74     return $self->{cgi};
75 }
76
77
78 # -----------------------------------------------------------------------------
79 # Perform initial setup, load common data, then load page data
80 # -----------------------------------------------------------------------------
81 sub load {
82     my $self = shift;
83
84     $self->init_ro_object_cache;
85
86     my $stat = $self->load_common;
87     return $stat unless $stat == Apache2::Const::OK;
88
89     my $path = $self->apache->path_info;
90
91     (undef, $self->ctx->{mylist}) = $self->fetch_mylist unless
92         $path =~ /opac\/my(opac\/lists|list)/;
93
94     return $self->load_simple("home") if $path =~ /opac\/home/;
95     return $self->load_simple("advanced") if $path =~ /opac\/advanced/;
96     return $self->load_rresults if $path =~ /opac\/results/;
97     return $self->load_record if $path =~ /opac\/record/;
98
99     return $self->load_mylist_add if $path =~ /opac\/mylist\/add/;
100     return $self->load_mylist_del if $path =~ /opac\/mylist\/del/;
101     return $self->load_mylist if $path =~ /opac\/mylist/;
102     return $self->load_cache_clear if $path =~ /opac\/cache\/clear/;
103
104     # ----------------------------------------------------------------
105     # Logout and login require SSL
106     # ----------------------------------------------------------------
107     if($path =~ /opac\/login/) {
108         return $self->redirect_ssl unless $self->cgi->https;
109         return $self->load_login;
110     }
111
112     if($path =~ /opac\/logout/) {
113         #return Apache2::Const::FORBIDDEN unless $self->cgi->https; 
114         $self->apache->log->warn("catloader: logout called in non-secure context from " . 
115             ($self->ctx->{referer} || '<no referer>')) unless $self->cgi->https;
116         return $self->load_logout;
117     }
118
119     # ----------------------------------------------------------------
120     #  Everything below here requires SSL + authentication
121     # ----------------------------------------------------------------
122     return $self->redirect_auth
123         unless $self->cgi->https and $self->editor->requestor;
124
125     return $self->load_place_hold if $path =~ /opac\/place_hold/;
126     return $self->load_myopac_holds if $path =~ /opac\/myopac\/holds/;
127     return $self->load_myopac_circs if $path =~ /opac\/myopac\/circs/;
128     return $self->load_myopac_fines if $path =~ /opac\/myopac\/main/;
129     return $self->load_myopac_update_email if $path =~ /opac\/myopac\/update_email/;
130     return $self->load_myopac_bookbags if $path =~ /opac\/myopac\/lists/;
131     return $self->load_myopac_bookbag_update if $path =~ /opac\/myopac\/list\/update/;
132     return $self->load_myopac if $path =~ /opac\/myopac/;
133
134     return Apache2::Const::OK;
135 }
136
137
138 # -----------------------------------------------------------------------------
139 # Redirect to SSL equivalent of a given page
140 # -----------------------------------------------------------------------------
141 sub redirect_ssl {
142     my $self = shift;
143     my $new_page = sprintf('https://%s%s', $self->apache->hostname, $self->apache->unparsed_uri);
144     return $self->generic_redirect($new_page);
145 }
146
147 # -----------------------------------------------------------------------------
148 # If an authnticated resource is requested w/o auth, redirect to the login page,
149 # then return to the originally requrested resource upon successful login.
150 # -----------------------------------------------------------------------------
151 sub redirect_auth {
152     my $self = shift;
153     my $login_page = sprintf('https://%s%s/login', $self->apache->hostname, $self->ctx->{opac_root});
154     my $redirect_to = uri_escape($self->apache->unparsed_uri);
155     return $self->generic_redirect("$login_page?redirect_to=$redirect_to");
156 }
157
158 # -----------------------------------------------------------------------------
159 # Fall-through for loading a basic page
160 # -----------------------------------------------------------------------------
161 sub load_simple {
162     my ($self, $page) = @_;
163     $self->ctx->{page} = $page;
164     return Apache2::Const::OK;
165 }
166
167 # -----------------------------------------------------------------------------
168 # Tests to see if the user is authenticated and sets some common context values
169 # -----------------------------------------------------------------------------
170 sub load_common {
171     my $self = shift;
172
173     my $e = $self->editor;
174     my $ctx = $self->ctx;
175
176     $ctx->{referer} = $self->cgi->referer;
177     $ctx->{path_info} = $self->cgi->path_info;
178     $ctx->{opac_root} = $ctx->{base_path} . "/opac"; # absolute base url
179     $ctx->{is_staff} = ($self->apache->headers_in->get('User-Agent') =~ 'oils_xulrunner');
180
181     # capture some commonly accessed pages
182     $ctx->{home_page} = 'http://' . $self->apache->hostname . $self->ctx->{opac_root} . "/home";
183     $ctx->{logout_page} = 'https://' . $self->apache->hostname . $self->ctx->{opac_root} . "/logout";
184
185     if($e->authtoken($self->cgi->cookie(COOKIE_SES))) {
186
187         if($e->checkauth) {
188
189             $ctx->{authtoken} = $e->authtoken;
190             $ctx->{authtime} = $e->authtime;
191             $ctx->{user} = $e->requestor;
192
193             $ctx->{user_stats} = $U->simplereq(
194                 'open-ils.actor', 
195                 'open-ils.actor.user.opac.vital_stats', 
196                 $e->authtoken, $e->requestor->id);
197
198         } else {
199
200             # For now, keep an eye out for any pages being unceremoniously redirected to logout...
201             $self->apache->log->info("catloader: loading " . $ctx->{path_info} . 
202                 "; auth session " .  $e->authtoken . " no longer valid; redirecting to logout");
203
204             return $self->load_logout;
205         }
206     }
207
208     return Apache2::Const::OK;
209 }
210
211
212 # -----------------------------------------------------------------------------
213 # Log in and redirect to the redirect_to URL (or home)
214 # -----------------------------------------------------------------------------
215 sub load_login {
216     my $self = shift;
217     my $cgi = $self->cgi;
218     my $ctx = $self->ctx;
219
220     $ctx->{page} = 'login';
221
222     my $username = $cgi->param('username');
223     my $password = $cgi->param('password');
224     my $org_unit = $cgi->param('loc') || $ctx->{aou_tree}->()->id;
225     my $persist = $cgi->param('persist');
226
227     # initial log form only
228     return Apache2::Const::OK unless $username and $password;
229
230         my $seed = $U->simplereq(
231         'open-ils.auth', 
232                 'open-ils.auth.authenticate.init', $username);
233
234     my $args = {        
235         username => $username, 
236         password => md5_hex($seed . md5_hex($password)), 
237         type => ($persist) ? 'persist' : 'opac' 
238     };
239
240     my $bc_regex = $ctx->{get_org_setting}->($org_unit, 'opac.barcode_regex');
241
242     $args->{barcode} = delete $args->{username} 
243         if $bc_regex and $username =~ /$bc_regex/;
244
245         my $response = $U->simplereq(
246         'open-ils.auth', 'open-ils.auth.authenticate.complete', $args);
247
248     if($U->event_code($response)) { 
249         # login failed, report the reason to the template
250         $ctx->{login_failed_event} = $response;
251         return Apache2::Const::OK;
252     }
253
254     # login succeeded, redirect as necessary
255
256     my $acct = $self->apache->unparsed_uri;
257     $acct =~ s#/login#/myopac/main#;
258
259     return $self->generic_redirect(
260         $cgi->param('redirect_to') || $acct,
261         $cgi->cookie(
262             -name => COOKIE_SES,
263             -path => '/',
264             -secure => 1,
265             -value => $response->{payload}->{authtoken},
266             -expires => ($persist) ? CORE::time + $response->{payload}->{authtime} : undef
267         )
268     );
269 }
270
271 # -----------------------------------------------------------------------------
272 # Log out and redirect to the home page
273 # -----------------------------------------------------------------------------
274 sub load_logout {
275     my $self = shift;
276
277     # If the user was adding anyting to an anonymous cache 
278     # while logged in, go ahead and clear it out.
279     $self->clear_anon_cache;
280
281     return $self->generic_redirect(
282         $self->ctx->{home_page},
283         $self->cgi->cookie(
284             -name => COOKIE_SES,
285             -path => '/',
286             -value => '',
287             -expires => '-1h'
288         )
289     );
290 }
291
292 1;
293