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::Search;
23 use OpenILS::WWW::EGCatLoader::Record;
24 use OpenILS::WWW::EGCatLoader::Container;
25 use OpenILS::WWW::EGCatLoader::SMS;
27 my $U = 'OpenILS::Application::AppUtils';
29 use constant COOKIE_SES => 'ses';
30 use constant COOKIE_PHYSICAL_LOC => 'eg_physical_loc';
31 use constant COOKIE_SSS_EXPAND => 'eg_sss_expand';
33 use constant COOKIE_ANON_CACHE => 'anoncache';
34 use constant ANON_CACHE_MYLIST => 'mylist';
35 use constant ANON_CACHE_STAFF_SEARCH => 'staffsearch';
37 use constant DEBUG_TIMING => 0;
40 my($class, $apache, $ctx) = @_;
42 my $self = bless({}, ref($class) || $class);
44 $self->apache($apache);
47 $self->timelog("New page");
49 OpenILS::Utils::CStoreEditor->init; # just in case
50 $self->editor(new_editor());
56 # current Apache2::RequestRec;
58 my($self, $apache) = @_;
59 $self->{apache} = $apache if $apache;
60 return $self->{apache};
63 # runtime / template context
66 $self->{ctx} = $ctx if $ctx;
72 my($self, $editor) = @_;
73 $self->{editor} = $editor if $editor;
74 return $self->{editor};
80 $self->{cgi} = $cgi if $cgi;
85 my($self, $description) = @_;
87 return unless DEBUG_TIMING;
88 return unless $description;
89 $self->ctx->{timing} ||= [];
91 my $timer = [Time::HiRes::gettimeofday()];
92 $self->ctx->{time_begin} ||= $timer;
94 push @{$self->ctx->{timing}}, [
95 Time::HiRes::tv_interval($self->ctx->{time_begin}, $timer), $description
99 # -----------------------------------------------------------------------------
100 # Perform initial setup, load common data, then load page data
101 # -----------------------------------------------------------------------------
105 $self->init_ro_object_cache;
106 $self->timelog("Initial load");
108 my $stat = $self->load_common;
109 return $stat unless $stat == Apache2::Const::OK;
111 my $path = $self->apache->path_info;
113 (undef, $self->ctx->{mylist}) = $self->fetch_mylist unless
114 $path =~ /opac\/my(opac\/lists|list)/;
116 return $self->load_simple("home") if $path =~ m|opac/home|;
117 return $self->load_simple("advanced") if
118 $path =~ m:opac/(advanced|numeric|expert):;
120 return $self->load_rresults if $path =~ m|opac/results|;
121 return $self->load_print_record if $path =~ m|opac/record/print|;
122 return $self->load_record if $path =~ m|opac/record/\d|;
123 return $self->load_cnbrowse if $path =~ m|opac/cnbrowse|;
125 return $self->load_mylist_add if $path =~ m|opac/mylist/add|;
126 return $self->load_mylist_delete if $path =~ m|opac/mylist/delete|;
127 return $self->load_mylist_move if $path =~ m|opac/mylist/move|;
128 return $self->load_mylist if $path =~ m|opac/mylist|;
129 return $self->load_cache_clear if $path =~ m|opac/cache/clear|;
131 # ----------------------------------------------------------------
132 # Everything below here requires SSL
133 # ----------------------------------------------------------------
134 return $self->redirect_ssl unless $self->cgi->https;
135 return $self->load_password_reset if $path =~ m|opac/password_reset|;
136 return $self->load_logout if $path =~ m|opac/logout|;
138 if($path =~ m|opac/login|) {
139 return $self->load_login unless $self->editor->requestor; # already logged in?
141 # This will be less confusing to users than to be shown a login form
142 # when they're already logged in.
143 return $self->generic_redirect(
145 "https://%s%s/myopac/main",
146 $self->apache->hostname, $self->ctx->{opac_root}
151 if ($path =~ m|opac/sms_cn| and !$self->editor->requestor) {
152 my $org_unit = $self->ctx->{physical_loc} || $self->cgi->param('loc') || $self->ctx->{aou_tree}->()->id;
153 my $skip_sms_auth = $self->ctx->{get_org_setting}->($org_unit, 'sms.disable_authentication_requirement.callnumbers');
154 return $self->load_sms_cn if $skip_sms_auth;
157 # ----------------------------------------------------------------
158 # Everything below here requires authentication
159 # ----------------------------------------------------------------
160 return $self->redirect_auth unless $self->editor->requestor;
162 return $self->load_email_record if $path =~ m|opac/record/email|;
164 return $self->load_place_hold if $path =~ m|opac/place_hold|;
165 return $self->load_myopac_holds if $path =~ m|opac/myopac/holds|;
166 return $self->load_myopac_circs if $path =~ m|opac/myopac/circs|;
167 return $self->load_myopac_payment_form if $path =~ m|opac/myopac/main_payment_form|;
168 return $self->load_myopac_payments if $path =~ m|opac/myopac/main_payments|;
169 return $self->load_myopac_pay_init if $path =~ m|opac/myopac/main_pay_init|;
170 return $self->load_myopac_pay if $path =~ m|opac/myopac/main_pay|;
171 return $self->load_myopac_main if $path =~ m|opac/myopac/main|;
172 return $self->load_myopac_receipt_email if $path =~ m|opac/myopac/receipt_email|;
173 return $self->load_myopac_receipt_print if $path =~ m|opac/myopac/receipt_print|;
174 return $self->load_myopac_update_email if $path =~ m|opac/myopac/update_email|;
175 return $self->load_myopac_update_password if $path =~ m|opac/myopac/update_password|;
176 return $self->load_myopac_update_username if $path =~ m|opac/myopac/update_username|;
177 return $self->load_myopac_bookbags if $path =~ m|opac/myopac/lists|;
178 return $self->load_myopac_bookbag_print if $path =~ m|opac/myopac/list/print|;
179 return $self->load_myopac_bookbag_update if $path =~ m|opac/myopac/list/update|;
180 return $self->load_myopac_circ_history_export if $path =~ m|opac/myopac/circ_history/export|;
181 return $self->load_myopac_circ_history if $path =~ m|opac/myopac/circ_history|;
182 return $self->load_myopac_hold_history if $path =~ m|opac/myopac/hold_history|;
183 return $self->load_myopac_prefs_notify if $path =~ m|opac/myopac/prefs_notify|;
184 return $self->load_myopac_prefs_settings if $path =~ m|opac/myopac/prefs_settings|;
185 return $self->load_myopac_prefs if $path =~ m|opac/myopac/prefs|;
186 return $self->load_sms_cn if $path =~ m|opac/sms_cn|;
188 return Apache2::Const::OK;
192 # -----------------------------------------------------------------------------
193 # Redirect to SSL equivalent of a given page
194 # -----------------------------------------------------------------------------
197 my $new_page = sprintf('https://%s%s', $self->apache->hostname, $self->apache->unparsed_uri);
198 return $self->generic_redirect($new_page);
201 # -----------------------------------------------------------------------------
202 # If an authnticated resource is requested w/o auth, redirect to the login page,
203 # then return to the originally requrested resource upon successful login.
204 # -----------------------------------------------------------------------------
207 my $login_page = sprintf('https://%s%s/login', $self->apache->hostname, $self->ctx->{opac_root});
208 my $redirect_to = uri_escape($self->apache->unparsed_uri);
209 return $self->generic_redirect("$login_page?redirect_to=$redirect_to");
212 # -----------------------------------------------------------------------------
213 # Fall-through for loading a basic page
214 # -----------------------------------------------------------------------------
216 my ($self, $page) = @_;
217 $self->ctx->{page} = $page;
218 $self->ctx->{search_ou} = $self->_get_search_lib();
220 if (my $patron_barcode = $self->cgi->param("patron_barcode")) {
221 # Special CGI variable from staff client; propagate henceforth as cookie
222 $self->apache->headers_out->add(
223 "Set-Cookie" => $self->cgi->cookie(
224 -name => "patron_barcode",
227 -value => $patron_barcode,
232 return Apache2::Const::OK;
235 # -----------------------------------------------------------------------------
236 # Tests to see if the user is authenticated and sets some common context values
237 # -----------------------------------------------------------------------------
241 my $e = $self->editor;
242 my $ctx = $self->ctx;
244 $ctx->{referer} = $self->cgi->referer;
245 $ctx->{path_info} = $self->cgi->path_info;
246 $ctx->{full_path} = $ctx->{base_path} . $self->cgi->path_info;
247 $ctx->{unparsed_uri} = $self->apache->unparsed_uri;
248 $ctx->{opac_root} = $ctx->{base_path} . "/opac"; # absolute base url
249 $ctx->{is_staff} = 0; # Assume false, check for workstation id later. Was: ($self->apache->headers_in->get('User-Agent') =~ /oils_xulrunner/);
250 $ctx->{physical_loc} = $self->get_physical_loc;
252 # capture some commonly accessed pages
253 $ctx->{home_page} = 'http://' . $self->apache->hostname . $self->ctx->{opac_root} . "/home";
254 $ctx->{logout_page} = 'https://' . $self->apache->hostname . $self->ctx->{opac_root} . "/logout";
256 if($e->authtoken($self->cgi->cookie(COOKIE_SES))) {
260 $ctx->{authtoken} = $e->authtoken;
261 $ctx->{authtime} = $e->authtime;
262 $ctx->{user} = $e->requestor;
264 $ctx->{user_stats} = $U->simplereq(
266 'open-ils.actor.user.opac.vital_stats',
267 $e->authtoken, $e->requestor->id);
268 $ctx->{is_staff} = 1 if $e->requestor->wsid;
272 # if we encounter a stale authtoken, call load_logout
273 # to clean up the cookie, then redirect the user to the
274 # originally requested page
275 return $self->load_logout($self->apache->unparsed_uri);
279 $self->extract_copy_location_group_info;
280 $ctx->{search_ou} = $self->_get_search_lib();
281 $self->staff_saved_searches_set_expansion_state if $ctx->{is_staff};
282 $self->load_eg_cache_hash;
283 $self->load_copy_location_groups;
284 $self->staff_saved_searches_set_expansion_state if $ctx->{is_staff};
286 return Apache2::Const::OK;
289 sub staff_saved_searches_set_expansion_state {
292 my $param = $self->cgi->param('sss_expand');
295 if (defined $param) {
296 $value = ($param ? 1 : 0);
297 $self->apache->headers_out->add(
298 "Set-Cookie" => $self->cgi->cookie(
299 -name => COOKIE_SSS_EXPAND,
300 -path => $self->ctx->{base_path},
301 -secure => 1, # not strictly necessary, but this feature is staff-only, so may as well
307 $value = $self->cgi->cookie(COOKIE_SSS_EXPAND);
310 $self->ctx->{saved_searches_expanded} = $value;
313 # physical_loc (i.e. "original location") passed in as a URL
314 # param will replace any existing physical_loc stored as a cookie.
315 sub get_physical_loc {
318 if(my $physical_loc = $self->cgi->param('physical_loc')) {
319 $self->apache->headers_out->add(
320 "Set-Cookie" => $self->cgi->cookie(
321 -name => COOKIE_PHYSICAL_LOC,
322 -path => $self->ctx->{base_path},
323 -value => $physical_loc,
327 return $physical_loc;
330 return $self->cgi->cookie(COOKIE_PHYSICAL_LOC);
333 # -----------------------------------------------------------------------------
334 # Log in and redirect to the redirect_to URL (or home)
335 # -----------------------------------------------------------------------------
338 my $cgi = $self->cgi;
339 my $ctx = $self->ctx;
341 $ctx->{page} = 'login';
343 my $username = $cgi->param('username');
344 my $password = $cgi->param('password');
345 my $org_unit = $cgi->param('loc') || $ctx->{aou_tree}->()->id;
346 my $persist = $cgi->param('persist');
348 # initial log form only
349 return Apache2::Const::OK unless $username and $password;
351 my $seed = $U->simplereq(
353 'open-ils.auth.authenticate.init', $username);
356 username => $username,
357 password => md5_hex($seed . md5_hex($password)),
358 type => ($persist) ? 'persist' : 'opac',
362 my $bc_regex = $ctx->{get_org_setting}->($org_unit, 'opac.barcode_regex');
364 $args->{barcode} = delete $args->{username}
365 if $bc_regex and ($username =~ /$bc_regex/);
367 my $response = $U->simplereq(
368 'open-ils.auth', 'open-ils.auth.authenticate.complete', $args);
370 if($U->event_code($response)) {
371 # login failed, report the reason to the template
372 $ctx->{login_failed_event} = $response;
373 return Apache2::Const::OK;
376 # login succeeded, redirect as necessary
378 my $acct = $self->apache->unparsed_uri;
379 $acct =~ s|/login|/myopac/main|;
381 return $self->generic_redirect(
382 $cgi->param('redirect_to') || $acct,
387 -value => $response->{payload}->{authtoken},
388 -expires => ($persist) ? CORE::time + $response->{payload}->{authtime} : undef
393 # -----------------------------------------------------------------------------
394 # Log out and redirect to the home page
395 # -----------------------------------------------------------------------------
398 my $redirect_to = shift;
400 # If the user was adding anyting to an anonymous cache
401 # while logged in, go ahead and clear it out.
402 $self->clear_anon_cache;
404 return $self->generic_redirect(
405 $redirect_to || $self->ctx->{home_page},