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;
24 use OpenILS::WWW::EGCatLoader::SMS;
26 my $U = 'OpenILS::Application::AppUtils';
28 use constant COOKIE_SES => 'ses';
29 use constant COOKIE_PHYSICAL_LOC => 'eg_physical_loc';
30 use constant COOKIE_SSS_EXPAND => 'eg_sss_expand';
32 use constant COOKIE_ANON_CACHE => 'anoncache';
33 use constant ANON_CACHE_MYLIST => 'mylist';
34 use constant ANON_CACHE_STAFF_SEARCH => 'staffsearch';
37 my($class, $apache, $ctx) = @_;
39 my $self = bless({}, ref($class) || $class);
41 $self->apache($apache);
45 OpenILS::Utils::CStoreEditor->init; # just in case
46 $self->editor(new_editor());
52 # current Apache2::RequestRec;
54 my($self, $apache) = @_;
55 $self->{apache} = $apache if $apache;
56 return $self->{apache};
59 # runtime / template context
62 $self->{ctx} = $ctx if $ctx;
68 my($self, $editor) = @_;
69 $self->{editor} = $editor if $editor;
70 return $self->{editor};
76 $self->{cgi} = $cgi if $cgi;
81 # -----------------------------------------------------------------------------
82 # Perform initial setup, load common data, then load page data
83 # -----------------------------------------------------------------------------
87 $self->init_ro_object_cache;
89 my $stat = $self->load_common;
90 return $stat unless $stat == Apache2::Const::OK;
92 my $path = $self->apache->path_info;
94 (undef, $self->ctx->{mylist}) = $self->fetch_mylist unless
95 $path =~ /opac\/my(opac\/lists|list)/;
97 return $self->load_simple("home") if $path =~ m|opac/home|;
98 return $self->load_simple("advanced") if
99 $path =~ m:opac/(advanced|numeric|expert):;
101 return $self->load_rresults if $path =~ m|opac/results|;
102 return $self->load_print_record if $path =~ m|opac/record/print|;
103 return $self->load_record if $path =~ m|opac/record/\d|;
104 return $self->load_cnbrowse if $path =~ m|opac/cnbrowse|;
106 return $self->load_mylist_add if $path =~ m|opac/mylist/add|;
107 return $self->load_mylist_delete if $path =~ m|opac/mylist/delete|;
108 return $self->load_mylist_move if $path =~ m|opac/mylist/move|;
109 return $self->load_mylist if $path =~ m|opac/mylist|;
110 return $self->load_cache_clear if $path =~ m|opac/cache/clear|;
112 # ----------------------------------------------------------------
113 # Everything below here requires SSL
114 # ----------------------------------------------------------------
115 return $self->redirect_ssl unless $self->cgi->https;
116 return $self->load_password_reset if $path =~ m|opac/password_reset|;
117 return $self->load_logout if $path =~ m|opac/logout|;
119 if($path =~ m|opac/login|) {
120 return $self->load_login unless $self->editor->requestor; # already logged in?
122 # This will be less confusing to users than to be shown a login form
123 # when they're already logged in.
124 return $self->generic_redirect(
126 "https://%s%s/myopac/main",
127 $self->apache->hostname, $self->ctx->{opac_root}
132 if ($path =~ m|opac/sms_cn| and !$self->editor->requestor) {
133 my $org_unit = $self->ctx->{physical_loc} || $self->cgi->param('loc') || $self->ctx->{aou_tree}->()->id;
134 my $skip_sms_auth = $self->ctx->{get_org_setting}->($org_unit, 'sms.disable_authentication_requirement.callnumbers');
135 return $self->load_sms_cn if $skip_sms_auth;
138 # ----------------------------------------------------------------
139 # Everything below here requires authentication
140 # ----------------------------------------------------------------
141 return $self->redirect_auth unless $self->editor->requestor;
143 return $self->load_email_record if $path =~ m|opac/record/email|;
145 return $self->load_place_hold if $path =~ m|opac/place_hold|;
146 return $self->load_myopac_holds if $path =~ m|opac/myopac/holds|;
147 return $self->load_myopac_circs if $path =~ m|opac/myopac/circs|;
148 return $self->load_myopac_payment_form if $path =~ m|opac/myopac/main_payment_form|;
149 return $self->load_myopac_payments if $path =~ m|opac/myopac/main_payments|;
150 return $self->load_myopac_pay_init if $path =~ m|opac/myopac/main_pay_init|;
151 return $self->load_myopac_pay if $path =~ m|opac/myopac/main_pay|;
152 return $self->load_myopac_main if $path =~ m|opac/myopac/main|;
153 return $self->load_myopac_receipt_email if $path =~ m|opac/myopac/receipt_email|;
154 return $self->load_myopac_receipt_print if $path =~ m|opac/myopac/receipt_print|;
155 return $self->load_myopac_update_email if $path =~ m|opac/myopac/update_email|;
156 return $self->load_myopac_update_password if $path =~ m|opac/myopac/update_password|;
157 return $self->load_myopac_update_username if $path =~ m|opac/myopac/update_username|;
158 return $self->load_myopac_bookbags if $path =~ m|opac/myopac/lists|;
159 return $self->load_myopac_bookbag_print if $path =~ m|opac/myopac/list/print|;
160 return $self->load_myopac_bookbag_update if $path =~ m|opac/myopac/list/update|;
161 return $self->load_myopac_circ_history_export if $path =~ m|opac/myopac/circ_history/export|;
162 return $self->load_myopac_circ_history if $path =~ m|opac/myopac/circ_history|;
163 return $self->load_myopac_hold_history if $path =~ m|opac/myopac/hold_history|;
164 return $self->load_myopac_prefs_notify if $path =~ m|opac/myopac/prefs_notify|;
165 return $self->load_myopac_prefs_settings if $path =~ m|opac/myopac/prefs_settings|;
166 return $self->load_myopac_prefs if $path =~ m|opac/myopac/prefs|;
167 return $self->load_sms_cn if $path =~ m|opac/sms_cn|;
169 return Apache2::Const::OK;
173 # -----------------------------------------------------------------------------
174 # Redirect to SSL equivalent of a given page
175 # -----------------------------------------------------------------------------
178 my $new_page = sprintf('https://%s%s', $self->apache->hostname, $self->apache->unparsed_uri);
179 return $self->generic_redirect($new_page);
182 # -----------------------------------------------------------------------------
183 # If an authnticated resource is requested w/o auth, redirect to the login page,
184 # then return to the originally requrested resource upon successful login.
185 # -----------------------------------------------------------------------------
188 my $login_page = sprintf('https://%s%s/login', $self->apache->hostname, $self->ctx->{opac_root});
189 my $redirect_to = uri_escape($self->apache->unparsed_uri);
190 return $self->generic_redirect("$login_page?redirect_to=$redirect_to");
193 # -----------------------------------------------------------------------------
194 # Fall-through for loading a basic page
195 # -----------------------------------------------------------------------------
197 my ($self, $page) = @_;
198 $self->ctx->{page} = $page;
199 $self->ctx->{search_ou} = $self->_get_search_lib();
201 if (my $patron_barcode = $self->cgi->param("patron_barcode")) {
202 # Special CGI variable from staff client; propagate henceforth as cookie
203 $self->apache->headers_out->add(
204 "Set-Cookie" => $self->cgi->cookie(
205 -name => "patron_barcode",
208 -value => $patron_barcode,
213 return Apache2::Const::OK;
216 # -----------------------------------------------------------------------------
217 # Tests to see if the user is authenticated and sets some common context values
218 # -----------------------------------------------------------------------------
222 my $e = $self->editor;
223 my $ctx = $self->ctx;
225 $ctx->{referer} = $self->cgi->referer;
226 $ctx->{path_info} = $self->cgi->path_info;
227 $ctx->{full_path} = $ctx->{base_path} . $self->cgi->path_info;
228 $ctx->{unparsed_uri} = $self->apache->unparsed_uri;
229 $ctx->{opac_root} = $ctx->{base_path} . "/opac"; # absolute base url
230 $ctx->{is_staff} = 0; # Assume false, check for workstation id later. Was: ($self->apache->headers_in->get('User-Agent') =~ /oils_xulrunner/);
231 $ctx->{physical_loc} = $self->get_physical_loc;
233 # capture some commonly accessed pages
234 $ctx->{home_page} = 'http://' . $self->apache->hostname . $self->ctx->{opac_root} . "/home";
235 $ctx->{logout_page} = 'https://' . $self->apache->hostname . $self->ctx->{opac_root} . "/logout";
237 if($e->authtoken($self->cgi->cookie(COOKIE_SES))) {
241 $ctx->{authtoken} = $e->authtoken;
242 $ctx->{authtime} = $e->authtime;
243 $ctx->{user} = $e->requestor;
245 $ctx->{user_stats} = $U->simplereq(
247 'open-ils.actor.user.opac.vital_stats',
248 $e->authtoken, $e->requestor->id);
249 $ctx->{is_staff} = 1 if $e->requestor->wsid;
253 # if we encounter a stale authtoken, call load_logout
254 # to clean up the cookie, then redirect the user to the
255 # originally requested page
256 return $self->load_logout($self->apache->unparsed_uri);
260 $self->extract_copy_location_group_info;
261 $ctx->{search_ou} = $self->_get_search_lib();
262 $self->staff_saved_searches_set_expansion_state if $ctx->{is_staff};
263 $self->load_eg_cache_hash;
264 $self->load_copy_location_groups;
265 $self->staff_saved_searches_set_expansion_state if $ctx->{is_staff};
267 return Apache2::Const::OK;
270 sub staff_saved_searches_set_expansion_state {
273 my $param = $self->cgi->param('sss_expand');
276 if (defined $param) {
277 $value = ($param ? 1 : 0);
278 $self->apache->headers_out->add(
279 "Set-Cookie" => $self->cgi->cookie(
280 -name => COOKIE_SSS_EXPAND,
281 -path => $self->ctx->{base_path},
282 -secure => 1, # not strictly necessary, but this feature is staff-only, so may as well
288 $value = $self->cgi->cookie(COOKIE_SSS_EXPAND);
291 $self->ctx->{saved_searches_expanded} = $value;
294 # physical_loc (i.e. "original location") passed in as a URL
295 # param will replace any existing physical_loc stored as a cookie.
296 sub get_physical_loc {
299 if(my $physical_loc = $self->cgi->param('physical_loc')) {
300 $self->apache->headers_out->add(
301 "Set-Cookie" => $self->cgi->cookie(
302 -name => COOKIE_PHYSICAL_LOC,
303 -path => $self->ctx->{base_path},
304 -value => $physical_loc,
308 return $physical_loc;
311 return $self->cgi->cookie(COOKIE_PHYSICAL_LOC);
314 # -----------------------------------------------------------------------------
315 # Log in and redirect to the redirect_to URL (or home)
316 # -----------------------------------------------------------------------------
319 my $cgi = $self->cgi;
320 my $ctx = $self->ctx;
322 $ctx->{page} = 'login';
324 my $username = $cgi->param('username');
325 my $password = $cgi->param('password');
326 my $org_unit = $cgi->param('loc') || $ctx->{aou_tree}->()->id;
327 my $persist = $cgi->param('persist');
329 # initial log form only
330 return Apache2::Const::OK unless $username and $password;
332 my $seed = $U->simplereq(
334 'open-ils.auth.authenticate.init', $username);
337 username => $username,
338 password => md5_hex($seed . md5_hex($password)),
339 type => ($persist) ? 'persist' : 'opac',
343 my $bc_regex = $ctx->{get_org_setting}->($org_unit, 'opac.barcode_regex');
345 $args->{barcode} = delete $args->{username}
346 if $bc_regex and ($username =~ /$bc_regex/);
348 my $response = $U->simplereq(
349 'open-ils.auth', 'open-ils.auth.authenticate.complete', $args);
351 if($U->event_code($response)) {
352 # login failed, report the reason to the template
353 $ctx->{login_failed_event} = $response;
354 return Apache2::Const::OK;
357 # login succeeded, redirect as necessary
359 my $acct = $self->apache->unparsed_uri;
360 $acct =~ s|/login|/myopac/main|;
362 return $self->generic_redirect(
363 $cgi->param('redirect_to') || $acct,
368 -value => $response->{payload}->{authtoken},
369 -expires => ($persist) ? CORE::time + $response->{payload}->{authtime} : undef
374 # -----------------------------------------------------------------------------
375 # Log out and redirect to the home page
376 # -----------------------------------------------------------------------------
379 my $redirect_to = shift;
381 # If the user was adding anyting to an anonymous cache
382 # while logged in, go ahead and clear it out.
383 $self->clear_anon_cache;
385 return $self->generic_redirect(
386 $redirect_to || $self->ctx->{home_page},