1 package OpenILS::WWW::EGCatLoader;
2 use strict; use warnings;
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 OpenILS::Utils::DateTime 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);
19 # EGCatLoader sub-modules
20 use OpenILS::WWW::EGCatLoader::Util;
21 use OpenILS::WWW::EGCatLoader::Account;
22 use OpenILS::WWW::EGCatLoader::Browse;
23 use OpenILS::WWW::EGCatLoader::Library;
24 use OpenILS::WWW::EGCatLoader::Search;
25 use OpenILS::WWW::EGCatLoader::Record;
26 use OpenILS::WWW::EGCatLoader::Container;
27 use OpenILS::WWW::EGCatLoader::SMS;
28 use OpenILS::WWW::EGCatLoader::Register;
30 my $U = 'OpenILS::Application::AppUtils';
32 use constant COOKIE_SES => 'ses';
33 use constant COOKIE_LOGGEDIN => 'eg_loggedin';
34 use constant COOKIE_TZ => 'client_tz';
35 use constant COOKIE_PHYSICAL_LOC => 'eg_physical_loc';
36 use constant COOKIE_SSS_EXPAND => 'eg_sss_expand';
38 use constant COOKIE_ANON_CACHE => 'anoncache';
39 use constant COOKIE_CART_CACHE => 'cartcache';
40 use constant CART_CACHE_MYLIST => 'mylist';
41 use constant ANON_CACHE_STAFF_SEARCH => 'staffsearch';
43 use constant DEBUG_TIMING => 0;
46 my($class, $apache, $ctx) = @_;
48 my $self = bless({}, ref($class) || $class);
50 $self->apache($apache);
53 $self->timelog("New page");
55 # Add a timelog helper to the context
56 $self->ctx->{timelog} = sub { return $self->timelog(@_) };
58 OpenILS::Utils::CStoreEditor->init; # just in case
59 $self->editor(new_editor());
66 $ENV{TZ} = $self->ctx->{original_tz}
67 if ($self->ctx && exists $self->ctx->{original_tz});
70 # current Apache2::RequestRec;
72 my($self, $apache) = @_;
73 $self->{apache} = $apache if $apache;
74 return $self->{apache};
77 # runtime / template context
80 $self->{ctx} = $ctx if $ctx;
86 my($self, $editor) = @_;
87 $self->{editor} = $editor if $editor;
88 return $self->{editor};
94 $self->{cgi} = $cgi if $cgi;
99 my($self, $description) = @_;
101 return unless DEBUG_TIMING;
102 return unless $description;
103 $self->ctx->{timing} ||= [];
105 my $timer = [Time::HiRes::gettimeofday()];
106 $self->ctx->{time_begin} ||= $timer;
108 push @{$self->ctx->{timing}}, [
109 Time::HiRes::tv_interval($self->ctx->{time_begin}, $timer), $description
113 # -----------------------------------------------------------------------------
114 # Perform initial setup, load common data, then load page data
115 # -----------------------------------------------------------------------------
119 $self->init_ro_object_cache;
120 $self->timelog("Initial load");
122 my $stat = $self->load_common;
123 return $stat unless $stat == Apache2::Const::OK;
125 my $path = $self->apache->path_info;
127 if ($path =~ m|opac/?$|) {
128 # nowhere specified, just go home
129 return $self->generic_redirect($self->ctx->{home_page});
132 (undef, $self->ctx->{mylist}) = $self->fetch_mylist unless
133 $path =~ /opac\/my(opac\/lists|list)/ ||
134 $path =~ m!opac/api/mylist!;
136 return $self->load_api_mylist_retrieve if $path =~ m|opac/api/mylist/retrieve|;
137 return $self->load_api_mylist_add if $path =~ m|opac/api/mylist/add|;
138 return $self->load_api_mylist_delete if $path =~ m|opac/api/mylist/delete|;
139 return $self->load_api_mylist_clear if $path =~ m|opac/api/mylist/clear|;
141 return $self->load_simple("home") if $path =~ m|opac/home|;
142 return $self->load_simple("css") if $path =~ m|opac/css|;
143 return $self->load_simple("advanced") if
144 $path =~ m:opac/(advanced|numeric|expert):;
146 return $self->load_library if $path =~ m|opac/library|;
147 return $self->load_rresults if $path =~ m|opac/results|;
148 return $self->load_print_record if $path =~ m|opac/record/print|;
149 return $self->load_record if $path =~ m|opac/record/\d|;
150 return $self->load_cnbrowse if $path =~ m|opac/cnbrowse|;
151 return $self->load_browse if $path =~ m|opac/browse|;
153 return $self->load_mylist_add if $path =~ m|opac/mylist/add|;
154 return $self->load_mylist_delete if $path =~ m|opac/mylist/delete|;
155 return $self->load_mylist_move if $path =~ m|opac/mylist/move|;
156 return $self->load_mylist_print if $path =~ m|opac/mylist/doprint|;
157 return $self->load_mylist if $path =~ m|opac/mylist| && $path !~ m|opac/mylist/email| && $path !~ m|opac/mylist/doemail|;
158 return $self->load_cache_clear if $path =~ m|opac/cache/clear|;
159 return $self->load_temp_warn_post if $path =~ m|opac/temp_warn/post|;
160 return $self->load_temp_warn if $path =~ m|opac/temp_warn|;
162 # ----------------------------------------------------------------
163 # Everything below here requires SSL
164 # ----------------------------------------------------------------
165 return $self->redirect_ssl unless $self->cgi->https;
166 return $self->load_password_reset if $path =~ m|opac/password_reset|;
167 return $self->load_logout if $path =~ m|opac/logout|;
168 return $self->load_patron_reg if $path =~ m|opac/register|;
170 $self->load_simple("myopac") if $path =~ m:opac/myopac:; # A default page for myopac parts
172 if($path =~ m|opac/login|) {
173 return $self->load_login unless $self->editor->requestor; # already logged in?
175 # This will be less confusing to users than to be shown a login form
176 # when they're already logged in.
177 return $self->generic_redirect(
179 "%s://%s%s/myopac/main",
181 $self->ctx->{hostname}, $self->ctx->{opac_root}
186 if ($path =~ m|opac/sms_cn| and !$self->editor->requestor) {
187 my $org_unit = $self->ctx->{physical_loc} || $self->cgi->param('loc') || $self->ctx->{aou_tree}->()->id;
188 my $skip_sms_auth = $self->ctx->{get_org_setting}->($org_unit, 'sms.disable_authentication_requirement.callnumbers');
189 return $self->load_sms_cn if $skip_sms_auth;
192 # ----------------------------------------------------------------
193 # Everything below here requires authentication
194 # ----------------------------------------------------------------
195 return $self->redirect_auth unless $self->editor->requestor;
197 # Don't cache anything requiring auth for security reasons
198 $self->apache->headers_out->add("cache-control" => "no-store, no-cache, must-revalidate");
199 $self->apache->headers_out->add("expires" => "-1");
201 if ($path =~ m|opac/mylist/email|) {
202 (undef, $self->ctx->{mylist}) = $self->fetch_mylist;
204 $self->load_simple("mylist/email") if $path =~ m|opac/mylist/email|;
205 return $self->load_mylist_email if $path =~ m|opac/mylist/doemail|;
206 return $self->load_email_record if $path =~ m|opac/record/email|;
208 return $self->load_place_hold if $path =~ m|opac/place_hold|;
209 return $self->load_myopac_holds if $path =~ m|opac/myopac/holds|;
210 return $self->load_myopac_circs if $path =~ m|opac/myopac/circs|;
211 return $self->load_myopac_messages if $path =~ m|opac/myopac/messages|;
212 return $self->load_myopac_payment_form if $path =~ m|opac/myopac/main_payment_form|;
213 return $self->load_myopac_payments if $path =~ m|opac/myopac/main_payments|;
214 return $self->load_myopac_pay_init if $path =~ m|opac/myopac/main_pay_init|;
215 return $self->load_myopac_pay if $path =~ m|opac/myopac/main_pay|;
216 return $self->load_myopac_main if $path =~ m|opac/myopac/main|;
217 return $self->load_myopac_receipt_email if $path =~ m|opac/myopac/receipt_email|;
218 return $self->load_myopac_receipt_print if $path =~ m|opac/myopac/receipt_print|;
219 return $self->load_myopac_update_email if $path =~ m|opac/myopac/update_email|;
220 return $self->load_myopac_update_password if $path =~ m|opac/myopac/update_password|;
221 return $self->load_myopac_update_username if $path =~ m|opac/myopac/update_username|;
222 return $self->load_myopac_bookbags if $path =~ m|opac/myopac/lists|;
223 return $self->load_myopac_bookbag_print if $path =~ m|opac/myopac/list/print|;
224 return $self->load_myopac_bookbag_update if $path =~ m|opac/myopac/list/update|;
225 return $self->load_myopac_circ_history_export if $path =~ m|opac/myopac/circ_history/export|;
226 return $self->load_myopac_circ_history if $path =~ m|opac/myopac/circ_history|;
227 return $self->load_myopac_hold_history if $path =~ m|opac/myopac/hold_history|;
228 return $self->load_myopac_prefs_notify if $path =~ m|opac/myopac/prefs_notify|;
229 return $self->load_myopac_prefs_settings if $path =~ m|opac/myopac/prefs_settings|;
230 return $self->load_myopac_prefs_my_lists if $path =~ m|opac/myopac/prefs_my_lists|;
231 return $self->load_myopac_prefs if $path =~ m|opac/myopac/prefs|;
232 return $self->load_sms_cn if $path =~ m|opac/sms_cn|;
234 return Apache2::Const::OK;
238 # -----------------------------------------------------------------------------
239 # Redirect to SSL equivalent of a given page
240 # -----------------------------------------------------------------------------
243 my $new_page = sprintf('%s://%s%s', ($self->ctx->{is_staff} ? 'oils' : 'https'), $self->ctx->{hostname}, $self->apache->unparsed_uri);
244 return $self->generic_redirect($new_page);
247 # -----------------------------------------------------------------------------
248 # If an authnticated resource is requested w/o auth, redirect to the login page,
249 # then return to the originally requrested resource upon successful login.
250 # -----------------------------------------------------------------------------
253 my $login_page = sprintf('%s://%s%s/login',($self->ctx->{is_staff} ? 'oils' : 'https'), $self->ctx->{hostname}, $self->ctx->{opac_root});
254 my $redirect_to = uri_escape_utf8($self->apache->unparsed_uri);
255 return $self->generic_redirect("$login_page?redirect_to=$redirect_to");
258 # -----------------------------------------------------------------------------
259 # Fall-through for loading a basic page
260 # -----------------------------------------------------------------------------
262 my ($self, $page) = @_;
263 $self->ctx->{page} = $page;
264 $self->ctx->{search_ou} = $self->_get_search_lib();
266 return Apache2::Const::OK;
269 # -----------------------------------------------------------------------------
270 # Tests to see if the user is authenticated and sets some common context values
271 # -----------------------------------------------------------------------------
275 my $e = $self->editor;
276 my $ctx = $self->ctx;
278 # redirect non-https to https if we think we are already logged in
279 if ($self->cgi->cookie(COOKIE_LOGGEDIN)) {
280 return $self->redirect_ssl unless $self->cgi->https;
283 # XXX Cache this? Makes testing difficult as apache needs a restart.
284 my $default_sort = $e->retrieve_config_global_flag('opac.default_sort');
285 $ctx->{default_sort} =
286 ($default_sort && $U->is_true($default_sort->enabled)) ? $default_sort->value : '';
288 $ctx->{client_tz} = $self->cgi->cookie(COOKIE_TZ) || $ENV{TZ};
289 $ctx->{referer} = $self->cgi->referer;
290 $ctx->{path_info} = $self->cgi->path_info;
291 $ctx->{full_path} = $ctx->{base_path} . $self->cgi->path_info;
292 $ctx->{unparsed_uri} = $self->apache->unparsed_uri;
293 $ctx->{opac_root} = $ctx->{base_path} . "/opac"; # absolute base url
295 $ctx->{original_tz} = $ENV{TZ};
296 $ENV{TZ} = $ctx->{client_tz};
299 ($self->apache->headers_in->get('OILS-Wrapper') || '') =~ /true/;
303 $ctx->{is_staff} = 1;
304 $ctx->{proto} = 'oils';
305 $ctx->{hostname} = 'remote';
308 $ctx->{physical_loc} = $self->get_physical_loc;
310 # capture some commonly accessed pages
311 $ctx->{home_page} = $ctx->{proto} . '://' . $ctx->{hostname} . $self->ctx->{opac_root} . "/home";
312 $ctx->{logout_page} = ($ctx->{proto} eq 'http' ? 'https' : $ctx->{proto} ) . '://' . $ctx->{hostname} . $self->ctx->{opac_root} . "/logout";
314 if($e->authtoken($self->cgi->cookie(COOKIE_SES))) {
318 $ctx->{authtoken} = $e->authtoken;
319 $ctx->{authtime} = $e->authtime;
320 $ctx->{user} = $e->requestor;
321 my $card = $self->editor->retrieve_actor_card($ctx->{user}->card);
322 $ctx->{active_card} = (ref $card) ? $card->barcode : undef;
323 $ctx->{place_unfillable} = 1 if $e->requestor->wsid && $e->allowed('PLACE_UNFILLABLE_HOLD', $e->requestor->ws_ou);
325 # The browser client does not set an OILS-Wrapper header (above).
326 # The presence of a workstation and no header indicates staff mode.
327 # FIXME: this approach leaves un-wrapped TPAC's within the same
328 # browser (and hence same ses cookie) in an unnatural is_staff
329 # state. Consider alternatives for determining is_staff /
330 # is_browser_staff when $xul_wrapper is false.
331 if (!$xul_wrapper and $e->requestor->wsid) {
332 $ctx->{is_staff} = 1;
333 $ctx->{is_browser_staff} = 1;
336 $self->update_dashboard_stats();
340 # if we encounter a stale authtoken, call load_logout
341 # to clean up the cookie, then redirect the user to the
342 # originally requested page
343 return $self->load_logout($self->apache->unparsed_uri);
347 # List of <meta> and <link> elements to populate
348 $ctx->{metalinks} = [];
350 $self->extract_copy_location_group_info;
351 $ctx->{search_ou} = $self->_get_search_lib();
352 $self->staff_saved_searches_set_expansion_state if $ctx->{is_staff};
353 $self->load_eg_cache_hash;
354 $self->load_copy_location_groups;
355 $self->staff_saved_searches_set_expansion_state if $ctx->{is_staff};
356 $self->load_search_filter_groups($ctx->{search_ou});
357 $self->load_org_util_funcs;
358 $self->load_perm_funcs;
360 $ctx->{fetch_display_fields} = sub {
364 return $ctx->{_hl_data}{''.$$id[0]}
365 if ($ctx->{_hl_data}{''.$$id[0]});
368 $self->timelog("HL data not cached, fetching from server.");
370 my $rows = $U->simplereq(
372 'open-ils.search.fetch.metabib.display_field.highlight',
373 $ctx->{query_struct}{additional_data}{highlight_map},
377 $ctx->{_hl_data}{''.$$id[0]} = $rows if (@$id == 1);
382 return Apache2::Const::OK;
385 sub update_dashboard_stats {
388 my $e = $self->editor;
389 my $ctx = $self->ctx;
391 $ctx->{user_stats} = $U->simplereq(
393 'open-ils.actor.user.opac.vital_stats',
394 $e->authtoken, $e->requestor->id);
397 sub staff_saved_searches_set_expansion_state {
400 my $param = $self->cgi->param('sss_expand');
403 if (defined $param) {
404 $value = ($param ? 1 : 0);
405 $self->apache->headers_out->add(
406 "Set-Cookie" => $self->cgi->cookie(
407 -name => COOKIE_SSS_EXPAND,
408 -path => $self->ctx->{base_path},
409 -secure => 1, # not strictly necessary, but this feature is staff-only, so may as well
415 $value = $self->cgi->cookie(COOKIE_SSS_EXPAND);
418 $self->ctx->{saved_searches_expanded} = $value;
421 # physical_loc (i.e. "original location") passed in as a URL
422 # param will replace any existing physical_loc stored as a cookie.
423 # If specified via ENV that rules over all and we don't set cookies.
424 sub get_physical_loc {
427 return $ENV{physical_loc} if($ENV{physical_loc});
429 if(my $physical_loc = $self->cgi->param('physical_loc')) {
430 $self->apache->headers_out->add(
431 "Set-Cookie" => $self->cgi->cookie(
432 -name => COOKIE_PHYSICAL_LOC,
433 -path => $self->ctx->{base_path},
434 -value => $physical_loc,
438 return $physical_loc;
441 return $self->cgi->cookie(COOKIE_PHYSICAL_LOC);
444 # -----------------------------------------------------------------------------
445 # Log in and redirect to the redirect_to URL (or home)
446 # -----------------------------------------------------------------------------
449 my $cgi = $self->cgi;
450 my $ctx = $self->ctx;
452 $self->timelog("Load login begins");
454 $ctx->{page} = 'login';
456 my $username = $cgi->param('username') || '';
457 $username =~ s/\s//g; # Remove blanks
458 my $password = $cgi->param('password');
459 my $org_unit = $ctx->{physical_loc} || $ctx->{aou_tree}->()->id;
460 my $persist = $cgi->param('persist');
461 my $client_tz = $cgi->param('client_tz');
463 # initial log form only
464 return Apache2::Const::OK unless $username and $password;
466 my $auth_proxy_enabled = 0; # default false
467 try { # if the service is not running, just let this fail silently
468 $auth_proxy_enabled = $U->simplereq(
469 'open-ils.auth_proxy',
470 'open-ils.auth_proxy.enabled');
471 } catch Error with {};
473 $self->timelog("Checked for auth proxy: $auth_proxy_enabled; org = $org_unit; username = $username");
476 type => ($persist) ? 'persist' : 'opac',
481 my $bc_regex = $ctx->{get_org_setting}->($org_unit, 'opac.barcode_regex');
483 # To avoid surprises, default to "Barcodes start with digits"
484 $bc_regex = '^\d' unless $bc_regex;
486 if ($bc_regex and ($username =~ /$bc_regex/)) {
487 $args->{barcode} = $username;
489 $args->{username} = $username;
493 if (!$auth_proxy_enabled) {
494 my $seed = $U->simplereq(
496 'open-ils.auth.authenticate.init', $username);
497 $args->{password} = md5_hex($seed . md5_hex($password));
498 $response = $U->simplereq(
499 'open-ils.auth', 'open-ils.auth.authenticate.complete', $args);
501 $args->{password} = $password;
502 $response = $U->simplereq(
503 'open-ils.auth_proxy',
504 'open-ils.auth_proxy.login', $args);
506 $self->timelog("Checked password");
508 if($U->event_code($response)) {
509 # login failed, report the reason to the template
510 $ctx->{login_failed_event} = $response;
511 return Apache2::Const::OK;
514 # login succeeded, redirect as necessary
516 my $acct = $self->apache->unparsed_uri;
517 $acct =~ s|/login|/myopac/main|;
519 # both login-related cookies should expire at the same time
520 my $login_cookie_expires = ($persist) ? CORE::time + $response->{payload}->{authtime} : undef;
523 # contains the actual auth token and should be sent only over https
528 -value => $response->{payload}->{authtoken},
529 -expires => $login_cookie_expires
531 # contains only a hint that we are logged in, and is used to
532 # trigger a redirect to https
534 -name => COOKIE_LOGGEDIN,
538 -expires => $login_cookie_expires
543 # contains the client's tz, as passed by the client
544 # trigger a redirect to https
545 push @$cookie_list, $cgi->cookie(
549 -value => $client_tz,
550 -expires => $login_cookie_expires
554 return $self->generic_redirect(
555 $cgi->param('redirect_to') || $acct,
560 # -----------------------------------------------------------------------------
561 # Log out and redirect to the home page
562 # -----------------------------------------------------------------------------
565 my $redirect_to = shift || $self->cgi->param('redirect_to');
567 # If the user was adding anyting to an anonymous cache
568 # while logged in, go ahead and clear it out.
569 $self->clear_anon_cache;
571 try { # a missing auth token will cause an ugly explosion
574 'open-ils.auth.session.delete',
575 $self->cgi->cookie(COOKIE_SES)
577 } catch Error with {};
579 return $self->generic_redirect(
580 $redirect_to || $self->ctx->{home_page},
582 # clear value of and expire both of these login-related cookies
590 -name => COOKIE_LOGGEDIN,