]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader.pm
Tpac: Rename orig_loc to physical_loc, for clarity's sake.
[working/Evergreen.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(:all -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 use constant COOKIE_PHYSICAL_LOC => 'eg_physical_loc';
29
30 use constant COOKIE_ANON_CACHE => 'anoncache';
31 use constant ANON_CACHE_MYLIST => 'mylist';
32 use constant ANON_CACHE_STAFF_SEARCH => 'staffsearch';
33
34 sub new {
35     my($class, $apache, $ctx) = @_;
36
37     my $self = bless({}, ref($class) || $class);
38
39     $self->apache($apache);
40     $self->ctx($ctx);
41     $self->cgi(new CGI);
42
43     OpenILS::Utils::CStoreEditor->init; # just in case
44     $self->editor(new_editor());
45
46     return $self;
47 }
48
49
50 # current Apache2::RequestRec;
51 sub apache {
52     my($self, $apache) = @_;
53     $self->{apache} = $apache if $apache;
54     return $self->{apache};
55 }
56
57 # runtime / template context
58 sub ctx {
59     my($self, $ctx) = @_;
60     $self->{ctx} = $ctx if $ctx;
61     return $self->{ctx};
62 }
63
64 # cstore editor
65 sub editor {
66     my($self, $editor) = @_;
67     $self->{editor} = $editor if $editor;
68     return $self->{editor};
69 }
70
71 # CGI handle
72 sub cgi {
73     my($self, $cgi) = @_;
74     $self->{cgi} = $cgi if $cgi;
75     return $self->{cgi};
76 }
77
78
79 # -----------------------------------------------------------------------------
80 # Perform initial setup, load common data, then load page data
81 # -----------------------------------------------------------------------------
82 sub load {
83     my $self = shift;
84
85     $self->init_ro_object_cache;
86
87     my $stat = $self->load_common;
88     return $stat unless $stat == Apache2::Const::OK;
89
90     my $path = $self->apache->path_info;
91
92     (undef, $self->ctx->{mylist}) = $self->fetch_mylist unless
93         $path =~ /opac\/my(opac\/lists|list)/;
94
95     return $self->load_simple("home") if $path =~ m|opac/home|;
96     return $self->load_simple("advanced") if
97         $path =~ m:opac/(advanced|numeric|expert):;
98
99     return $self->load_rresults if $path =~ m|opac/results|;
100     return $self->load_record if $path =~ m|opac/record|;
101     return $self->load_cnbrowse if $path =~ m|opac/cnbrowse|;
102
103     return $self->load_mylist_add if $path =~ m|opac/mylist/add|;
104     return $self->load_mylist_delete if $path =~ m|opac/mylist/delete|;
105     return $self->load_mylist_move if $path =~ m|opac/mylist/move|;
106     return $self->load_mylist if $path =~ m|opac/mylist|;
107     return $self->load_cache_clear if $path =~ m|opac/cache/clear|;
108
109     # ----------------------------------------------------------------
110     # Logout and login require SSL
111     # ----------------------------------------------------------------
112     if($path =~ m|opac/login|) {
113         return $self->redirect_ssl unless $self->cgi->https;
114         return $self->load_login unless $self->editor->requestor; # already logged in?
115
116         # This will be less confusing to users than to be shown a login form
117         # when they're already logged in.
118         return $self->generic_redirect(
119             sprintf(
120                 "https://%s%s/myopac/main",
121                 $self->apache->hostname, $self->ctx->{opac_root}
122             )
123         );
124     }
125
126     if($path =~ m|opac/logout|) {
127         #return Apache2::Const::FORBIDDEN unless $self->cgi->https; 
128         $self->apache->log->warn("catloader: logout called in non-secure context from " . 
129             ($self->ctx->{referer} || '<no referer>')) unless $self->cgi->https;
130         return $self->load_logout;
131     }
132
133     return $self->load_password_reset if $path =~ m|opac/password_reset|;
134
135     # ----------------------------------------------------------------
136     #  Everything below here requires SSL + authentication
137     # ----------------------------------------------------------------
138     return $self->redirect_auth
139         unless $self->cgi->https and $self->editor->requestor;
140
141     return $self->load_place_hold if $path =~ m|opac/place_hold|;
142     return $self->load_myopac_holds if $path =~ m|opac/myopac/holds|;
143     return $self->load_myopac_circs if $path =~ m|opac/myopac/circs|;
144     return $self->load_myopac_payment_form if $path =~ m|opac/myopac/main_payment_form|;
145     return $self->load_myopac_payments if $path =~ m|opac/myopac/main_payments|;
146     return $self->load_myopac_pay if $path =~ m|opac/myopac/main_pay|;
147     return $self->load_myopac_main if $path =~ m|opac/myopac/main|;
148     return $self->load_myopac_receipt_email if $path =~ m|opac/myopac/receipt_email|;
149     return $self->load_myopac_receipt_print if $path =~ m|opac/myopac/receipt_print|;
150     return $self->load_myopac_update_email if $path =~ m|opac/myopac/update_email|;
151     return $self->load_myopac_update_password if $path =~ m|opac/myopac/update_password|;
152     return $self->load_myopac_update_username if $path =~ m|opac/myopac/update_username|;
153     return $self->load_myopac_bookbags if $path =~ m|opac/myopac/lists|;
154     return $self->load_myopac_bookbag_print if $path =~ m|opac/myopac/list/print|;
155     return $self->load_myopac_bookbag_update if $path =~ m|opac/myopac/list/update|;
156     return $self->load_myopac_circ_history if $path =~ m|opac/myopac/circ_history|;
157     return $self->load_myopac_hold_history if $path =~ m|opac/myopac/hold_history|;
158     return $self->load_myopac_prefs_notify if $path =~ m|opac/myopac/prefs_notify|;
159     return $self->load_myopac_prefs_settings if $path =~ m|opac/myopac/prefs_settings|;
160     return $self->load_myopac_prefs if $path =~ m|opac/myopac/prefs|;
161
162     return Apache2::Const::OK;
163 }
164
165
166 # -----------------------------------------------------------------------------
167 # Redirect to SSL equivalent of a given page
168 # -----------------------------------------------------------------------------
169 sub redirect_ssl {
170     my $self = shift;
171     my $new_page = sprintf('https://%s%s', $self->apache->hostname, $self->apache->unparsed_uri);
172     return $self->generic_redirect($new_page);
173 }
174
175 # -----------------------------------------------------------------------------
176 # If an authnticated resource is requested w/o auth, redirect to the login page,
177 # then return to the originally requrested resource upon successful login.
178 # -----------------------------------------------------------------------------
179 sub redirect_auth {
180     my $self = shift;
181     my $login_page = sprintf('https://%s%s/login', $self->apache->hostname, $self->ctx->{opac_root});
182     my $redirect_to = uri_escape($self->apache->unparsed_uri);
183     return $self->generic_redirect("$login_page?redirect_to=$redirect_to");
184 }
185
186 # -----------------------------------------------------------------------------
187 # Fall-through for loading a basic page
188 # -----------------------------------------------------------------------------
189 sub load_simple {
190     my ($self, $page) = @_;
191     $self->ctx->{page} = $page;
192
193     if (my $patron_barcode = $self->cgi->param("patron_barcode")) {
194         # Special CGI variable from staff client; propagate henceforth as cookie
195         $self->apache->headers_out->add(
196             "Set-Cookie" => $self->cgi->cookie(
197                 -name => "patron_barcode",
198                 -path => "/",
199                 -secure => 1,
200                 -value => $patron_barcode,
201                 -expires => undef
202             )
203         );
204     }
205     return Apache2::Const::OK;
206 }
207
208 # -----------------------------------------------------------------------------
209 # Tests to see if the user is authenticated and sets some common context values
210 # -----------------------------------------------------------------------------
211 sub load_common {
212     my $self = shift;
213
214     my $e = $self->editor;
215     my $ctx = $self->ctx;
216
217     $ctx->{referer} = $self->cgi->referer;
218     $ctx->{path_info} = $self->cgi->path_info;
219     $ctx->{full_path} = $ctx->{base_path} . $self->cgi->path_info;
220     $ctx->{unparsed_uri} = $self->apache->unparsed_uri;
221     $ctx->{opac_root} = $ctx->{base_path} . "/opac"; # absolute base url
222     $ctx->{is_staff} = 0; # Assume false, check for workstation id later.  Was: ($self->apache->headers_in->get('User-Agent') =~ /oils_xulrunner/);
223     $ctx->{physical_loc} = $self->get_physical_loc;
224
225     # capture some commonly accessed pages
226     $ctx->{home_page} = 'http://' . $self->apache->hostname . $self->ctx->{opac_root} . "/home";
227     $ctx->{logout_page} = 'https://' . $self->apache->hostname . $self->ctx->{opac_root} . "/logout";
228
229     if($e->authtoken($self->cgi->cookie(COOKIE_SES))) {
230
231         if($e->checkauth) {
232
233             $ctx->{authtoken} = $e->authtoken;
234             $ctx->{authtime} = $e->authtime;
235             $ctx->{user} = $e->requestor;
236
237             $ctx->{user_stats} = $U->simplereq(
238                 'open-ils.actor', 
239                 'open-ils.actor.user.opac.vital_stats', 
240                 $e->authtoken, $e->requestor->id);
241             $ctx->{is_staff} = 1 if $e->requestor->wsid;
242
243         } else {
244
245             # if we encounter a stale authtoken, call load_logout 
246             # to clean up the cookie, then redirect the user to the
247             # originally requested page
248             return $self->load_logout($self->apache->unparsed_uri);
249         }
250     }
251
252     return Apache2::Const::OK;
253 }
254
255 # physical_loc (i.e. "original location") passed in as a URL 
256 # param will replace any existing physical_loc stored as a cookie.
257 sub get_physical_loc {
258     my $self = shift;
259
260     if(my $physical_loc = $self->cgi->param('physical_loc')) {
261         $self->apache->headers_out->add(
262             "Set-Cookie" => $self->cgi->cookie(
263                 -name => COOKIE_PHYSICAL_LOC,
264                 -path => $self->ctx->{base_path},
265                 -value => $physical_loc,
266                 -expires => undef
267             )
268         );
269         return $physical_loc;
270     }
271
272     return $self->cgi->cookie(COOKIE_PHYSICAL_LOC);
273 }
274
275
276
277 # -----------------------------------------------------------------------------
278 # Log in and redirect to the redirect_to URL (or home)
279 # -----------------------------------------------------------------------------
280 sub load_login {
281     my $self = shift;
282     my $cgi = $self->cgi;
283     my $ctx = $self->ctx;
284
285     $ctx->{page} = 'login';
286
287     my $username = $cgi->param('username');
288     my $password = $cgi->param('password');
289     my $org_unit = $cgi->param('loc') || $ctx->{aou_tree}->()->id;
290     my $persist = $cgi->param('persist');
291
292     # initial log form only
293     return Apache2::Const::OK unless $username and $password;
294
295         my $seed = $U->simplereq(
296         'open-ils.auth', 
297                 'open-ils.auth.authenticate.init', $username);
298
299     my $args = {        
300         username => $username, 
301         password => md5_hex($seed . md5_hex($password)), 
302         type => ($persist) ? 'persist' : 'opac' 
303     };
304
305     my $bc_regex = $ctx->{get_org_setting}->($org_unit, 'opac.barcode_regex');
306
307     $args->{barcode} = delete $args->{username} 
308         if $bc_regex and ($username =~ /$bc_regex/);
309
310         my $response = $U->simplereq(
311         'open-ils.auth', 'open-ils.auth.authenticate.complete', $args);
312
313     if($U->event_code($response)) { 
314         # login failed, report the reason to the template
315         $ctx->{login_failed_event} = $response;
316         return Apache2::Const::OK;
317     }
318
319     # login succeeded, redirect as necessary
320
321     my $acct = $self->apache->unparsed_uri;
322     $acct =~ s|/login|/myopac/main|;
323
324     return $self->generic_redirect(
325         $cgi->param('redirect_to') || $acct,
326         $cgi->cookie(
327             -name => COOKIE_SES,
328             -path => '/',
329             -secure => 1,
330             -value => $response->{payload}->{authtoken},
331             -expires => ($persist) ? CORE::time + $response->{payload}->{authtime} : undef
332         )
333     );
334 }
335
336 # -----------------------------------------------------------------------------
337 # Log out and redirect to the home page
338 # -----------------------------------------------------------------------------
339 sub load_logout {
340     my $self = shift;
341     my $redirect_to = shift;
342
343     # If the user was adding anyting to an anonymous cache 
344     # while logged in, go ahead and clear it out.
345     $self->clear_anon_cache;
346
347     return $self->generic_redirect(
348         $redirect_to || $self->ctx->{home_page},
349         $self->cgi->cookie(
350             -name => COOKIE_SES,
351             -path => '/',
352             -value => '',
353             -expires => '-1h'
354         )
355     );
356 }
357
358 1;
359