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);
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;
25 my $U = 'OpenILS::Application::AppUtils';
27 use constant COOKIE_SES => 'ses';
28 use constant COOKIE_PHYSICAL_LOC => 'eg_physical_loc';
29 use constant COOKIE_SSS_EXPAND => 'eg_sss_expand';
31 use constant COOKIE_ANON_CACHE => 'anoncache';
32 use constant ANON_CACHE_MYLIST => 'mylist';
33 use constant ANON_CACHE_STAFF_SEARCH => 'staffsearch';
36 my($class, $apache, $ctx) = @_;
38 my $self = bless({}, ref($class) || $class);
40 $self->apache($apache);
44 OpenILS::Utils::CStoreEditor->init; # just in case
45 $self->editor(new_editor());
51 # current Apache2::RequestRec;
53 my($self, $apache) = @_;
54 $self->{apache} = $apache if $apache;
55 return $self->{apache};
58 # runtime / template context
61 $self->{ctx} = $ctx if $ctx;
67 my($self, $editor) = @_;
68 $self->{editor} = $editor if $editor;
69 return $self->{editor};
75 $self->{cgi} = $cgi if $cgi;
80 # -----------------------------------------------------------------------------
81 # Perform initial setup, load common data, then load page data
82 # -----------------------------------------------------------------------------
86 $self->init_ro_object_cache;
88 my $stat = $self->load_common;
89 return $stat unless $stat == Apache2::Const::OK;
91 my $path = $self->apache->path_info;
93 (undef, $self->ctx->{mylist}) = $self->fetch_mylist unless
94 $path =~ /opac\/my(opac\/lists|list)/;
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):;
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|;
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|;
110 # ----------------------------------------------------------------
111 # Logout and login require SSL
112 # ----------------------------------------------------------------
113 if($path =~ m|opac/login|) {
114 return $self->redirect_ssl unless $self->cgi->https;
115 return $self->load_login unless $self->editor->requestor; # already logged in?
117 # This will be less confusing to users than to be shown a login form
118 # when they're already logged in.
119 return $self->generic_redirect(
121 "https://%s%s/myopac/main",
122 $self->apache->hostname, $self->ctx->{opac_root}
127 if($path =~ m|opac/logout|) {
128 #return Apache2::Const::FORBIDDEN unless $self->cgi->https;
129 $self->apache->log->warn("catloader: logout called in non-secure context from " .
130 ($self->ctx->{referer} || '<no referer>')) unless $self->cgi->https;
131 return $self->load_logout;
134 return $self->load_password_reset if $path =~ m|opac/password_reset|;
136 # ----------------------------------------------------------------
137 # Everything below here requires SSL + authentication
138 # ----------------------------------------------------------------
139 return $self->redirect_auth
140 unless $self->cgi->https and $self->editor->requestor;
142 return $self->load_place_hold if $path =~ m|opac/place_hold|;
143 return $self->load_myopac_holds if $path =~ m|opac/myopac/holds|;
144 return $self->load_myopac_circs if $path =~ m|opac/myopac/circs|;
145 return $self->load_myopac_payment_form if $path =~ m|opac/myopac/main_payment_form|;
146 return $self->load_myopac_payments if $path =~ m|opac/myopac/main_payments|;
147 return $self->load_myopac_pay if $path =~ m|opac/myopac/main_pay|;
148 return $self->load_myopac_main if $path =~ m|opac/myopac/main|;
149 return $self->load_myopac_receipt_email if $path =~ m|opac/myopac/receipt_email|;
150 return $self->load_myopac_receipt_print if $path =~ m|opac/myopac/receipt_print|;
151 return $self->load_myopac_update_email if $path =~ m|opac/myopac/update_email|;
152 return $self->load_myopac_update_password if $path =~ m|opac/myopac/update_password|;
153 return $self->load_myopac_update_username if $path =~ m|opac/myopac/update_username|;
154 return $self->load_myopac_bookbags if $path =~ m|opac/myopac/lists|;
155 return $self->load_myopac_bookbag_print if $path =~ m|opac/myopac/list/print|;
156 return $self->load_myopac_bookbag_update if $path =~ m|opac/myopac/list/update|;
157 return $self->load_myopac_circ_history if $path =~ m|opac/myopac/circ_history|;
158 return $self->load_myopac_hold_history if $path =~ m|opac/myopac/hold_history|;
159 return $self->load_myopac_prefs_notify if $path =~ m|opac/myopac/prefs_notify|;
160 return $self->load_myopac_prefs_settings if $path =~ m|opac/myopac/prefs_settings|;
161 return $self->load_myopac_prefs if $path =~ m|opac/myopac/prefs|;
163 return Apache2::Const::OK;
167 # -----------------------------------------------------------------------------
168 # Redirect to SSL equivalent of a given page
169 # -----------------------------------------------------------------------------
172 my $new_page = sprintf('https://%s%s', $self->apache->hostname, $self->apache->unparsed_uri);
173 return $self->generic_redirect($new_page);
176 # -----------------------------------------------------------------------------
177 # If an authnticated resource is requested w/o auth, redirect to the login page,
178 # then return to the originally requrested resource upon successful login.
179 # -----------------------------------------------------------------------------
182 my $login_page = sprintf('https://%s%s/login', $self->apache->hostname, $self->ctx->{opac_root});
183 my $redirect_to = uri_escape($self->apache->unparsed_uri);
184 return $self->generic_redirect("$login_page?redirect_to=$redirect_to");
187 # -----------------------------------------------------------------------------
188 # Fall-through for loading a basic page
189 # -----------------------------------------------------------------------------
191 my ($self, $page) = @_;
192 $self->ctx->{page} = $page;
194 if (my $patron_barcode = $self->cgi->param("patron_barcode")) {
195 # Special CGI variable from staff client; propagate henceforth as cookie
196 $self->apache->headers_out->add(
197 "Set-Cookie" => $self->cgi->cookie(
198 -name => "patron_barcode",
201 -value => $patron_barcode,
206 return Apache2::Const::OK;
209 # -----------------------------------------------------------------------------
210 # Tests to see if the user is authenticated and sets some common context values
211 # -----------------------------------------------------------------------------
215 my $e = $self->editor;
216 my $ctx = $self->ctx;
218 $ctx->{referer} = $self->cgi->referer;
219 $ctx->{path_info} = $self->cgi->path_info;
220 $ctx->{full_path} = $ctx->{base_path} . $self->cgi->path_info;
221 $ctx->{unparsed_uri} = $self->apache->unparsed_uri;
222 $ctx->{opac_root} = $ctx->{base_path} . "/opac"; # absolute base url
223 $ctx->{is_staff} = 0; # Assume false, check for workstation id later. Was: ($self->apache->headers_in->get('User-Agent') =~ /oils_xulrunner/);
224 $ctx->{physical_loc} = $self->get_physical_loc;
226 # capture some commonly accessed pages
227 $ctx->{home_page} = 'http://' . $self->apache->hostname . $self->ctx->{opac_root} . "/home";
228 $ctx->{logout_page} = 'https://' . $self->apache->hostname . $self->ctx->{opac_root} . "/logout";
230 if($e->authtoken($self->cgi->cookie(COOKIE_SES))) {
234 $ctx->{authtoken} = $e->authtoken;
235 $ctx->{authtime} = $e->authtime;
236 $ctx->{user} = $e->requestor;
238 $ctx->{user_stats} = $U->simplereq(
240 'open-ils.actor.user.opac.vital_stats',
241 $e->authtoken, $e->requestor->id);
242 $ctx->{is_staff} = 1 if $e->requestor->wsid;
246 # if we encounter a stale authtoken, call load_logout
247 # to clean up the cookie, then redirect the user to the
248 # originally requested page
249 return $self->load_logout($self->apache->unparsed_uri);
253 $self->staff_saved_searches_set_expansion_state if $ctx->{is_staff};
255 return Apache2::Const::OK;
258 sub staff_saved_searches_set_expansion_state {
261 my $param = $self->cgi->param('sss_expand');
264 if (defined $param) {
265 $value = ($param ? 1 : 0);
266 $self->apache->headers_out->add(
267 "Set-Cookie" => $self->cgi->cookie(
268 -name => COOKIE_SSS_EXPAND,
269 -path => $self->ctx->{base_path},
270 -secure => 1, # not strictly necessary, but this feature is staff-only, so may as well
276 $value = $self->cgi->cookie(COOKIE_SSS_EXPAND);
279 $self->ctx->{saved_searches_expanded} = $value;
282 # physical_loc (i.e. "original location") passed in as a URL
283 # param will replace any existing physical_loc stored as a cookie.
284 sub get_physical_loc {
287 if(my $physical_loc = $self->cgi->param('physical_loc')) {
288 $self->apache->headers_out->add(
289 "Set-Cookie" => $self->cgi->cookie(
290 -name => COOKIE_PHYSICAL_LOC,
291 -path => $self->ctx->{base_path},
292 -value => $physical_loc,
296 return $physical_loc;
299 return $self->cgi->cookie(COOKIE_PHYSICAL_LOC);
304 # -----------------------------------------------------------------------------
305 # Log in and redirect to the redirect_to URL (or home)
306 # -----------------------------------------------------------------------------
309 my $cgi = $self->cgi;
310 my $ctx = $self->ctx;
312 $ctx->{page} = 'login';
314 my $username = $cgi->param('username');
315 my $password = $cgi->param('password');
316 my $org_unit = $cgi->param('loc') || $ctx->{aou_tree}->()->id;
317 my $persist = $cgi->param('persist');
319 # initial log form only
320 return Apache2::Const::OK unless $username and $password;
322 my $seed = $U->simplereq(
324 'open-ils.auth.authenticate.init', $username);
327 username => $username,
328 password => md5_hex($seed . md5_hex($password)),
329 type => ($persist) ? 'persist' : 'opac'
332 my $bc_regex = $ctx->{get_org_setting}->($org_unit, 'opac.barcode_regex');
334 $args->{barcode} = delete $args->{username}
335 if $bc_regex and ($username =~ /$bc_regex/);
337 my $response = $U->simplereq(
338 'open-ils.auth', 'open-ils.auth.authenticate.complete', $args);
340 if($U->event_code($response)) {
341 # login failed, report the reason to the template
342 $ctx->{login_failed_event} = $response;
343 return Apache2::Const::OK;
346 # login succeeded, redirect as necessary
348 my $acct = $self->apache->unparsed_uri;
349 $acct =~ s|/login|/myopac/main|;
351 return $self->generic_redirect(
352 $cgi->param('redirect_to') || $acct,
357 -value => $response->{payload}->{authtoken},
358 -expires => ($persist) ? CORE::time + $response->{payload}->{authtime} : undef
363 # -----------------------------------------------------------------------------
364 # Log out and redirect to the home page
365 # -----------------------------------------------------------------------------
368 my $redirect_to = shift;
370 # If the user was adding anyting to an anonymous cache
371 # while logged in, go ahead and clear it out.
372 $self->clear_anon_cache;
374 return $self->generic_redirect(
375 $redirect_to || $self->ctx->{home_page},