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