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_ORIG_LOC => 'eg_orig_loc';
30 use constant COOKIE_ANON_CACHE => 'anoncache';
31 use constant ANON_CACHE_MYLIST => 'mylist';
32 use constant ANON_CACHE_STAFF_SEARCH => 'staffsearch';
35 my($class, $apache, $ctx) = @_;
37 my $self = bless({}, ref($class) || $class);
39 $self->apache($apache);
43 OpenILS::Utils::CStoreEditor->init; # just in case
44 $self->editor(new_editor());
50 # current Apache2::RequestRec;
52 my($self, $apache) = @_;
53 $self->{apache} = $apache if $apache;
54 return $self->{apache};
57 # runtime / template context
60 $self->{ctx} = $ctx if $ctx;
66 my($self, $editor) = @_;
67 $self->{editor} = $editor if $editor;
68 return $self->{editor};
74 $self->{cgi} = $cgi if $cgi;
79 # -----------------------------------------------------------------------------
80 # Perform initial setup, load common data, then load page data
81 # -----------------------------------------------------------------------------
85 $self->init_ro_object_cache;
87 my $stat = $self->load_common;
88 return $stat unless $stat == Apache2::Const::OK;
90 my $path = $self->apache->path_info;
92 (undef, $self->ctx->{mylist}) = $self->fetch_mylist unless
93 $path =~ /opac\/my(opac\/lists|list)/;
95 return $self->load_simple("home") if $path =~ m|opac/home|;
96 return $self->load_simple("advanced") if
97 $path =~ m:opac/(advanced|numeric|expert):;
99 return $self->load_rresults if $path =~ m|opac/results|;
100 return $self->load_record if $path =~ m|opac/record|;
101 return $self->load_cnbrowse if $path =~ m|opac/cnbrowse|;
103 return $self->load_mylist_add if $path =~ m|opac/mylist/add|;
104 return $self->load_mylist_delete if $path =~ m|opac/mylist/delete|;
105 return $self->load_mylist_move if $path =~ m|opac/mylist/move|;
106 return $self->load_mylist if $path =~ m|opac/mylist|;
107 return $self->load_cache_clear if $path =~ m|opac/cache/clear|;
109 # ----------------------------------------------------------------
110 # Logout and login require SSL
111 # ----------------------------------------------------------------
112 if($path =~ m|opac/login|) {
113 return $self->redirect_ssl unless $self->cgi->https;
114 return $self->load_login unless $self->editor->requestor; # already logged in?
116 # This will be less confusing to users than to be shown a login form
117 # when they're already logged in.
118 return $self->generic_redirect(
120 "https://%s%s/myopac/main",
121 $self->apache->hostname, $self->ctx->{opac_root}
126 if($path =~ m|opac/logout|) {
127 #return Apache2::Const::FORBIDDEN unless $self->cgi->https;
128 $self->apache->log->warn("catloader: logout called in non-secure context from " .
129 ($self->ctx->{referer} || '<no referer>')) unless $self->cgi->https;
130 return $self->load_logout;
133 return $self->load_password_reset if $path =~ m|opac/password_reset|;
135 # ----------------------------------------------------------------
136 # Everything below here requires SSL + authentication
137 # ----------------------------------------------------------------
138 return $self->redirect_auth
139 unless $self->cgi->https and $self->editor->requestor;
141 return $self->load_place_hold if $path =~ m|opac/place_hold|;
142 return $self->load_myopac_holds if $path =~ m|opac/myopac/holds|;
143 return $self->load_myopac_circs if $path =~ m|opac/myopac/circs|;
144 return $self->load_myopac_payment_form if $path =~ m|opac/myopac/main_payment_form|;
145 return $self->load_myopac_payments if $path =~ m|opac/myopac/main_payments|;
146 return $self->load_myopac_pay if $path =~ m|opac/myopac/main_pay|;
147 return $self->load_myopac_main if $path =~ m|opac/myopac/main|;
148 return $self->load_myopac_receipt_email if $path =~ m|opac/myopac/receipt_email|;
149 return $self->load_myopac_receipt_print if $path =~ m|opac/myopac/receipt_print|;
150 return $self->load_myopac_update_email if $path =~ m|opac/myopac/update_email|;
151 return $self->load_myopac_update_password if $path =~ m|opac/myopac/update_password|;
152 return $self->load_myopac_update_username if $path =~ m|opac/myopac/update_username|;
153 return $self->load_myopac_bookbags if $path =~ m|opac/myopac/lists|;
154 return $self->load_myopac_bookbag_print if $path =~ m|opac/myopac/list/print|;
155 return $self->load_myopac_bookbag_update if $path =~ m|opac/myopac/list/update|;
156 return $self->load_myopac_circ_history if $path =~ m|opac/myopac/circ_history|;
157 return $self->load_myopac_hold_history if $path =~ m|opac/myopac/hold_history|;
158 return $self->load_myopac_prefs_notify if $path =~ m|opac/myopac/prefs_notify|;
159 return $self->load_myopac_prefs_settings if $path =~ m|opac/myopac/prefs_settings|;
160 return $self->load_myopac_prefs if $path =~ m|opac/myopac/prefs|;
162 return Apache2::Const::OK;
166 # -----------------------------------------------------------------------------
167 # Redirect to SSL equivalent of a given page
168 # -----------------------------------------------------------------------------
171 my $new_page = sprintf('https://%s%s', $self->apache->hostname, $self->apache->unparsed_uri);
172 return $self->generic_redirect($new_page);
175 # -----------------------------------------------------------------------------
176 # If an authnticated resource is requested w/o auth, redirect to the login page,
177 # then return to the originally requrested resource upon successful login.
178 # -----------------------------------------------------------------------------
181 my $login_page = sprintf('https://%s%s/login', $self->apache->hostname, $self->ctx->{opac_root});
182 my $redirect_to = uri_escape($self->apache->unparsed_uri);
183 return $self->generic_redirect("$login_page?redirect_to=$redirect_to");
186 # -----------------------------------------------------------------------------
187 # Fall-through for loading a basic page
188 # -----------------------------------------------------------------------------
190 my ($self, $page) = @_;
191 $self->ctx->{page} = $page;
193 if (my $patron_barcode = $self->cgi->param("patron_barcode")) {
194 # Special CGI variable from staff client; propagate henceforth as cookie
195 $self->apache->headers_out->add(
196 "Set-Cookie" => $self->cgi->cookie(
197 -name => "patron_barcode",
200 -value => $patron_barcode,
205 return Apache2::Const::OK;
208 # -----------------------------------------------------------------------------
209 # Tests to see if the user is authenticated and sets some common context values
210 # -----------------------------------------------------------------------------
214 my $e = $self->editor;
215 my $ctx = $self->ctx;
217 $ctx->{referer} = $self->cgi->referer;
218 $ctx->{path_info} = $self->cgi->path_info;
219 $ctx->{full_path} = $ctx->{base_path} . $self->cgi->path_info;
220 $ctx->{unparsed_uri} = $self->apache->unparsed_uri;
221 $ctx->{opac_root} = $ctx->{base_path} . "/opac"; # absolute base url
222 $ctx->{is_staff} = 0; # Assume false, check for workstation id later. Was: ($self->apache->headers_in->get('User-Agent') =~ /oils_xulrunner/);
223 $ctx->{orig_loc} = $self->get_orig_loc;
225 # capture some commonly accessed pages
226 $ctx->{home_page} = 'http://' . $self->apache->hostname . $self->ctx->{opac_root} . "/home";
227 $ctx->{logout_page} = 'https://' . $self->apache->hostname . $self->ctx->{opac_root} . "/logout";
229 if($e->authtoken($self->cgi->cookie(COOKIE_SES))) {
233 $ctx->{authtoken} = $e->authtoken;
234 $ctx->{authtime} = $e->authtime;
235 $ctx->{user} = $e->requestor;
237 $ctx->{user_stats} = $U->simplereq(
239 'open-ils.actor.user.opac.vital_stats',
240 $e->authtoken, $e->requestor->id);
241 $ctx->{is_staff} = 1 if $e->requestor->wsid;
245 # if we encounter a stale authtoken, call load_logout
246 # to clean up the cookie, then redirect the user to the
247 # originally requested page
248 return $self->load_logout($self->apache->unparsed_uri);
252 return Apache2::Const::OK;
255 # orig_loc (i.e. "original location") passed in as a URL
256 # param will replace any existing orig_loc stored as a cookie.
260 if(my $orig_loc = $self->cgi->param('orig_loc')) {
261 $self->apache->headers_out->add(
262 "Set-Cookie" => $self->cgi->cookie(
263 -name => COOKIE_ORIG_LOC,
264 -path => $self->ctx->{base_path},
272 return $self->cgi->cookie(COOKIE_ORIG_LOC);
277 # -----------------------------------------------------------------------------
278 # Log in and redirect to the redirect_to URL (or home)
279 # -----------------------------------------------------------------------------
282 my $cgi = $self->cgi;
283 my $ctx = $self->ctx;
285 $ctx->{page} = 'login';
287 my $username = $cgi->param('username');
288 my $password = $cgi->param('password');
289 my $org_unit = $cgi->param('loc') || $ctx->{aou_tree}->()->id;
290 my $persist = $cgi->param('persist');
292 # initial log form only
293 return Apache2::Const::OK unless $username and $password;
295 my $seed = $U->simplereq(
297 'open-ils.auth.authenticate.init', $username);
300 username => $username,
301 password => md5_hex($seed . md5_hex($password)),
302 type => ($persist) ? 'persist' : 'opac'
305 my $bc_regex = $ctx->{get_org_setting}->($org_unit, 'opac.barcode_regex');
307 $args->{barcode} = delete $args->{username}
308 if $bc_regex and ($username =~ /$bc_regex/);
310 my $response = $U->simplereq(
311 'open-ils.auth', 'open-ils.auth.authenticate.complete', $args);
313 if($U->event_code($response)) {
314 # login failed, report the reason to the template
315 $ctx->{login_failed_event} = $response;
316 return Apache2::Const::OK;
319 # login succeeded, redirect as necessary
321 my $acct = $self->apache->unparsed_uri;
322 $acct =~ s|/login|/myopac/main|;
324 return $self->generic_redirect(
325 $cgi->param('redirect_to') || $acct,
330 -value => $response->{payload}->{authtoken},
331 -expires => ($persist) ? CORE::time + $response->{payload}->{authtime} : undef
336 # -----------------------------------------------------------------------------
337 # Log out and redirect to the home page
338 # -----------------------------------------------------------------------------
341 my $redirect_to = shift;
343 # If the user was adding anyting to an anonymous cache
344 # while logged in, go ahead and clear it out.
345 $self->clear_anon_cache;
347 return $self->generic_redirect(
348 $redirect_to || $self->ctx->{home_page},