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 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);
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_PHYSICAL_LOC => 'eg_physical_loc';
35 use constant COOKIE_SSS_EXPAND => 'eg_sss_expand';
37 use constant COOKIE_ANON_CACHE => 'anoncache';
38 use constant ANON_CACHE_MYLIST => 'mylist';
39 use constant ANON_CACHE_STAFF_SEARCH => 'staffsearch';
41 use constant DEBUG_TIMING => 0;
44 my($class, $apache, $ctx) = @_;
46 my $self = bless({}, ref($class) || $class);
48 $self->apache($apache);
51 $self->timelog("New page");
53 OpenILS::Utils::CStoreEditor->init; # just in case
54 $self->editor(new_editor());
60 # current Apache2::RequestRec;
62 my($self, $apache) = @_;
63 $self->{apache} = $apache if $apache;
64 return $self->{apache};
67 # runtime / template context
70 $self->{ctx} = $ctx if $ctx;
76 my($self, $editor) = @_;
77 $self->{editor} = $editor if $editor;
78 return $self->{editor};
84 $self->{cgi} = $cgi if $cgi;
89 my($self, $description) = @_;
91 return unless DEBUG_TIMING;
92 return unless $description;
93 $self->ctx->{timing} ||= [];
95 my $timer = [Time::HiRes::gettimeofday()];
96 $self->ctx->{time_begin} ||= $timer;
98 push @{$self->ctx->{timing}}, [
99 Time::HiRes::tv_interval($self->ctx->{time_begin}, $timer), $description
103 # -----------------------------------------------------------------------------
104 # Perform initial setup, load common data, then load page data
105 # -----------------------------------------------------------------------------
109 $self->init_ro_object_cache;
110 $self->timelog("Initial load");
112 my $stat = $self->load_common;
113 return $stat unless $stat == Apache2::Const::OK;
115 my $path = $self->apache->path_info;
117 if ($path =~ m|opac/?$|) {
118 # nowhere specified, just go home
119 return $self->generic_redirect($self->ctx->{home_page});
122 (undef, $self->ctx->{mylist}) = $self->fetch_mylist unless
123 $path =~ /opac\/my(opac\/lists|list)/;
125 return $self->load_simple("home") if $path =~ m|opac/home|;
126 return $self->load_simple("css") if $path =~ m|opac/css|;
127 return $self->load_simple("advanced") if
128 $path =~ m:opac/(advanced|numeric|expert):;
130 return $self->load_library if $path =~ m|opac/library|;
131 return $self->load_rresults if $path =~ m|opac/results|;
132 return $self->load_print_record if $path =~ m|opac/record/print|;
133 return $self->load_record if $path =~ m|opac/record/\d|;
134 return $self->load_cnbrowse if $path =~ m|opac/cnbrowse|;
135 return $self->load_browse if $path =~ m|opac/browse|;
137 return $self->load_mylist_add if $path =~ m|opac/mylist/add|;
138 return $self->load_mylist_delete if $path =~ m|opac/mylist/delete|;
139 return $self->load_mylist_move if $path =~ m|opac/mylist/move|;
140 return $self->load_mylist if $path =~ m|opac/mylist|;
141 return $self->load_cache_clear if $path =~ m|opac/cache/clear|;
142 return $self->load_temp_warn_post if $path =~ m|opac/temp_warn/post|;
143 return $self->load_temp_warn if $path =~ m|opac/temp_warn|;
145 # ----------------------------------------------------------------
146 # Everything below here requires SSL
147 # ----------------------------------------------------------------
148 return $self->redirect_ssl unless $self->cgi->https;
149 return $self->load_password_reset if $path =~ m|opac/password_reset|;
150 return $self->load_logout if $path =~ m|opac/logout|;
151 return $self->load_patron_reg if $path =~ m|opac/register|;
153 if($path =~ m|opac/login|) {
154 return $self->load_login unless $self->editor->requestor; # already logged in?
156 # This will be less confusing to users than to be shown a login form
157 # when they're already logged in.
158 return $self->generic_redirect(
160 "%s://%s%s/myopac/main",
162 $self->ctx->{hostname}, $self->ctx->{opac_root}
167 if ($path =~ m|opac/sms_cn| and !$self->editor->requestor) {
168 my $org_unit = $self->ctx->{physical_loc} || $self->cgi->param('loc') || $self->ctx->{aou_tree}->()->id;
169 my $skip_sms_auth = $self->ctx->{get_org_setting}->($org_unit, 'sms.disable_authentication_requirement.callnumbers');
170 return $self->load_sms_cn if $skip_sms_auth;
173 # ----------------------------------------------------------------
174 # Everything below here requires authentication
175 # ----------------------------------------------------------------
176 return $self->redirect_auth unless $self->editor->requestor;
178 # Don't cache anything requiring auth for security reasons
179 $self->apache->headers_out->add("cache-control" => "no-store, no-cache, must-revalidate");
180 $self->apache->headers_out->add("expires" => "-1");
182 return $self->load_email_record if $path =~ m|opac/record/email|;
184 return $self->load_place_hold if $path =~ m|opac/place_hold|;
185 return $self->load_myopac_holds if $path =~ m|opac/myopac/holds|;
186 return $self->load_myopac_circs if $path =~ m|opac/myopac/circs|;
187 return $self->load_myopac_payment_form if $path =~ m|opac/myopac/main_payment_form|;
188 return $self->load_myopac_payments if $path =~ m|opac/myopac/main_payments|;
189 return $self->load_myopac_pay_init if $path =~ m|opac/myopac/main_pay_init|;
190 return $self->load_myopac_pay if $path =~ m|opac/myopac/main_pay|;
191 return $self->load_myopac_main if $path =~ m|opac/myopac/main|;
192 return $self->load_myopac_receipt_email if $path =~ m|opac/myopac/receipt_email|;
193 return $self->load_myopac_receipt_print if $path =~ m|opac/myopac/receipt_print|;
194 return $self->load_myopac_update_email if $path =~ m|opac/myopac/update_email|;
195 return $self->load_myopac_update_password if $path =~ m|opac/myopac/update_password|;
196 return $self->load_myopac_update_username if $path =~ m|opac/myopac/update_username|;
197 return $self->load_myopac_bookbags if $path =~ m|opac/myopac/lists|;
198 return $self->load_myopac_bookbag_print if $path =~ m|opac/myopac/list/print|;
199 return $self->load_myopac_bookbag_update if $path =~ m|opac/myopac/list/update|;
200 return $self->load_myopac_circ_history_export if $path =~ m|opac/myopac/circ_history/export|;
201 return $self->load_myopac_circ_history if $path =~ m|opac/myopac/circ_history|;
202 return $self->load_myopac_hold_history if $path =~ m|opac/myopac/hold_history|;
203 return $self->load_myopac_prefs_notify if $path =~ m|opac/myopac/prefs_notify|;
204 return $self->load_myopac_prefs_settings if $path =~ m|opac/myopac/prefs_settings|;
205 return $self->load_myopac_prefs_my_lists if $path =~ m|opac/myopac/prefs_my_lists|;
206 return $self->load_myopac_prefs if $path =~ m|opac/myopac/prefs|;
207 return $self->load_sms_cn if $path =~ m|opac/sms_cn|;
209 return Apache2::Const::OK;
213 # -----------------------------------------------------------------------------
214 # Redirect to SSL equivalent of a given page
215 # -----------------------------------------------------------------------------
218 my $new_page = sprintf('%s://%s%s', ($self->ctx->{is_staff} ? 'oils' : 'https'), $self->ctx->{hostname}, $self->apache->unparsed_uri);
219 return $self->generic_redirect($new_page);
222 # -----------------------------------------------------------------------------
223 # If an authnticated resource is requested w/o auth, redirect to the login page,
224 # then return to the originally requrested resource upon successful login.
225 # -----------------------------------------------------------------------------
228 my $login_page = sprintf('%s://%s%s/login',($self->ctx->{is_staff} ? 'oils' : 'https'), $self->ctx->{hostname}, $self->ctx->{opac_root});
229 my $redirect_to = uri_escape_utf8($self->apache->unparsed_uri);
230 return $self->generic_redirect("$login_page?redirect_to=$redirect_to");
233 # -----------------------------------------------------------------------------
234 # Fall-through for loading a basic page
235 # -----------------------------------------------------------------------------
237 my ($self, $page) = @_;
238 $self->ctx->{page} = $page;
239 $self->ctx->{search_ou} = $self->_get_search_lib();
241 return Apache2::Const::OK;
244 # -----------------------------------------------------------------------------
245 # Tests to see if the user is authenticated and sets some common context values
246 # -----------------------------------------------------------------------------
250 my $e = $self->editor;
251 my $ctx = $self->ctx;
253 # redirect non-https to https if we think we are already logged in
254 if ($self->cgi->cookie(COOKIE_LOGGEDIN)) {
255 return $self->redirect_ssl unless $self->cgi->https;
258 $ctx->{referer} = $self->cgi->referer;
259 $ctx->{path_info} = $self->cgi->path_info;
260 $ctx->{full_path} = $ctx->{base_path} . $self->cgi->path_info;
261 $ctx->{unparsed_uri} = $self->apache->unparsed_uri;
262 $ctx->{opac_root} = $ctx->{base_path} . "/opac"; # absolute base url
263 my $oils_wrapper = $self->apache->headers_in->get('OILS-Wrapper') || '';
264 $ctx->{is_staff} = ($oils_wrapper =~ /true/);
265 $ctx->{proto} = 'oils' if $ctx->{is_staff};
266 $ctx->{hostname} = 'remote' if $ctx->{is_staff};
267 $ctx->{physical_loc} = $self->get_physical_loc;
269 # capture some commonly accessed pages
270 $ctx->{home_page} = $ctx->{proto} . '://' . $ctx->{hostname} . $self->ctx->{opac_root} . "/home";
271 $ctx->{logout_page} = ($ctx->{proto} eq 'http' ? 'https' : $ctx->{proto} ) . '://' . $ctx->{hostname} . $self->ctx->{opac_root} . "/logout";
273 if($e->authtoken($self->cgi->cookie(COOKIE_SES))) {
277 $ctx->{authtoken} = $e->authtoken;
278 $ctx->{authtime} = $e->authtime;
279 $ctx->{user} = $e->requestor;
280 $ctx->{place_unfillable} = 1 if $e->requestor->wsid && $e->allowed('PLACE_UNFILLABLE_HOLD', $e->requestor->ws_ou);
282 $ctx->{user_stats} = $U->simplereq(
284 'open-ils.actor.user.opac.vital_stats',
285 $e->authtoken, $e->requestor->id);
289 # if we encounter a stale authtoken, call load_logout
290 # to clean up the cookie, then redirect the user to the
291 # originally requested page
292 return $self->load_logout($self->apache->unparsed_uri);
296 $self->extract_copy_location_group_info;
297 $ctx->{search_ou} = $self->_get_search_lib();
298 $self->staff_saved_searches_set_expansion_state if $ctx->{is_staff};
299 $self->load_eg_cache_hash;
300 $self->load_copy_location_groups;
301 $self->staff_saved_searches_set_expansion_state if $ctx->{is_staff};
302 $self->load_search_filter_groups($ctx->{search_ou});
303 $self->load_org_util_funcs;
304 $self->load_perm_funcs;
306 return Apache2::Const::OK;
309 sub staff_saved_searches_set_expansion_state {
312 my $param = $self->cgi->param('sss_expand');
315 if (defined $param) {
316 $value = ($param ? 1 : 0);
317 $self->apache->headers_out->add(
318 "Set-Cookie" => $self->cgi->cookie(
319 -name => COOKIE_SSS_EXPAND,
320 -path => $self->ctx->{base_path},
321 -secure => 1, # not strictly necessary, but this feature is staff-only, so may as well
327 $value = $self->cgi->cookie(COOKIE_SSS_EXPAND);
330 $self->ctx->{saved_searches_expanded} = $value;
333 # physical_loc (i.e. "original location") passed in as a URL
334 # param will replace any existing physical_loc stored as a cookie.
335 # If specified via ENV that rules over all and we don't set cookies.
336 sub get_physical_loc {
339 return $ENV{physical_loc} if($ENV{physical_loc});
341 if(my $physical_loc = $self->cgi->param('physical_loc')) {
342 $self->apache->headers_out->add(
343 "Set-Cookie" => $self->cgi->cookie(
344 -name => COOKIE_PHYSICAL_LOC,
345 -path => $self->ctx->{base_path},
346 -value => $physical_loc,
350 return $physical_loc;
353 return $self->cgi->cookie(COOKIE_PHYSICAL_LOC);
356 # -----------------------------------------------------------------------------
357 # Log in and redirect to the redirect_to URL (or home)
358 # -----------------------------------------------------------------------------
361 my $cgi = $self->cgi;
362 my $ctx = $self->ctx;
364 $self->timelog("Load login begins");
366 $ctx->{page} = 'login';
368 my $username = $cgi->param('username');
369 $username =~ s/\s//g; # Remove blanks
370 my $password = $cgi->param('password');
371 my $org_unit = $ctx->{physical_loc} || $ctx->{aou_tree}->()->id;
372 my $persist = $cgi->param('persist');
374 # initial log form only
375 return Apache2::Const::OK unless $username and $password;
377 my $auth_proxy_enabled = 0; # default false
378 try { # if the service is not running, just let this fail silently
379 $auth_proxy_enabled = $U->simplereq(
380 'open-ils.auth_proxy',
381 'open-ils.auth_proxy.enabled');
382 } catch Error with {};
384 $self->timelog("Checked for auth proxy: $auth_proxy_enabled; org = $org_unit; username = $username");
387 type => ($persist) ? 'persist' : 'opac',
392 my $bc_regex = $ctx->{get_org_setting}->($org_unit, 'opac.barcode_regex');
394 # To avoid surprises, default to "Barcodes start with digits"
395 $bc_regex = '^\d' unless $bc_regex;
397 if ($bc_regex and ($username =~ /$bc_regex/)) {
398 $args->{barcode} = $username;
400 $args->{username} = $username;
404 if (!$auth_proxy_enabled) {
405 my $seed = $U->simplereq(
407 'open-ils.auth.authenticate.init', $username);
408 $args->{password} = md5_hex($seed . md5_hex($password));
409 $response = $U->simplereq(
410 'open-ils.auth', 'open-ils.auth.authenticate.complete', $args);
412 $args->{password} = $password;
413 $response = $U->simplereq(
414 'open-ils.auth_proxy',
415 'open-ils.auth_proxy.login', $args);
417 $self->timelog("Checked password");
419 if($U->event_code($response)) {
420 # login failed, report the reason to the template
421 $ctx->{login_failed_event} = $response;
422 return Apache2::Const::OK;
425 # login succeeded, redirect as necessary
427 my $acct = $self->apache->unparsed_uri;
428 $acct =~ s|/login|/myopac/main|;
430 # both login-related cookies should expire at the same time
431 my $login_cookie_expires = ($persist) ? CORE::time + $response->{payload}->{authtime} : undef;
433 return $self->generic_redirect(
434 $cgi->param('redirect_to') || $acct,
436 # contains the actual auth token and should be sent only over https
441 -value => $response->{payload}->{authtoken},
442 -expires => $login_cookie_expires
444 # contains only a hint that we are logged in, and is used to
445 # trigger a redirect to https
447 -name => COOKIE_LOGGEDIN,
451 -expires => $login_cookie_expires
457 # -----------------------------------------------------------------------------
458 # Log out and redirect to the home page
459 # -----------------------------------------------------------------------------
462 my $redirect_to = shift || $self->cgi->param('redirect_to');
464 # If the user was adding anyting to an anonymous cache
465 # while logged in, go ahead and clear it out.
466 $self->clear_anon_cache;
468 return $self->generic_redirect(
469 $redirect_to || $self->ctx->{home_page},
471 # clear value of and expire both of these login-related cookies
479 -name => COOKIE_LOGGEDIN,