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';
31 my($class, $apache, $ctx) = @_;
33 my $self = bless({}, ref($class) || $class);
35 $self->apache($apache);
39 OpenILS::Utils::CStoreEditor->init; # just in case
40 $self->editor(new_editor());
46 # current Apache2::RequestRec;
48 my($self, $apache) = @_;
49 $self->{apache} = $apache if $apache;
50 return $self->{apache};
53 # runtime / template context
56 $self->{ctx} = $ctx if $ctx;
62 my($self, $editor) = @_;
63 $self->{editor} = $editor if $editor;
64 return $self->{editor};
70 $self->{cgi} = $cgi if $cgi;
75 # -----------------------------------------------------------------------------
76 # Perform initial setup, load common data, then load page data
77 # -----------------------------------------------------------------------------
81 $self->init_ro_object_cache;
83 my $stat = $self->load_common;
84 return $stat unless $stat == Apache2::Const::OK;
86 my $path = $self->apache->path_info;
88 (undef, $self->ctx->{mylist}) = $self->fetch_mylist unless
89 $path =~ /opac\/my(opac\/lists|list)/;
91 return $self->load_simple("home") if $path =~ m|opac/home|;
92 return $self->load_simple("advanced") if
93 $path =~ m:opac/(advanced|numeric|expert):;
95 return $self->load_rresults if $path =~ m|opac/results|;
96 return $self->load_record if $path =~ m|opac/record|;
98 return $self->load_mylist_add if $path =~ m|opac/mylist/add|;
99 return $self->load_mylist_move if $path =~ m|opac/mylist/move|;
100 return $self->load_mylist if $path =~ m|opac/mylist|;
101 return $self->load_cache_clear if $path =~ m|opac/cache/clear|;
103 # ----------------------------------------------------------------
104 # Logout and login require SSL
105 # ----------------------------------------------------------------
106 if($path =~ m|opac/login|) {
107 return $self->redirect_ssl unless $self->cgi->https;
108 return $self->load_login unless $self->editor->requestor; # already logged in?
110 # This will be less confusing to users than to be shown a login form
111 # when they're already logged in.
112 return $self->generic_redirect(
114 "https://%s%s/myopac/main",
115 $self->apache->hostname, $self->ctx->{opac_root}
120 if($path =~ m|opac/logout|) {
121 #return Apache2::Const::FORBIDDEN unless $self->cgi->https;
122 $self->apache->log->warn("catloader: logout called in non-secure context from " .
123 ($self->ctx->{referer} || '<no referer>')) unless $self->cgi->https;
124 return $self->load_logout;
127 # ----------------------------------------------------------------
128 # Everything below here requires SSL + authentication
129 # ----------------------------------------------------------------
130 return $self->redirect_auth
131 unless $self->cgi->https and $self->editor->requestor;
133 return $self->load_place_hold if $path =~ m|opac/place_hold|;
134 return $self->load_myopac_holds if $path =~ m|opac/myopac/holds|;
135 return $self->load_myopac_circs if $path =~ m|opac/myopac/circs|;
136 return $self->load_myopac_payment_form if $path =~ m|opac/myopac/main_payment_form|;
137 return $self->load_myopac_payments if $path =~ m|opac/myopac/main_payments|;
138 return $self->load_myopac_pay if $path =~ m|opac/myopac/main_pay|;
139 return $self->load_myopac_main if $path =~ m|opac/myopac/main|;
140 return $self->load_myopac_receipt_email if $path =~ m|opac/myopac/receipt_email|;
141 return $self->load_myopac_receipt_print if $path =~ m|opac/myopac/receipt_print|;
142 return $self->load_myopac_update_email if $path =~ m|opac/myopac/update_email|;
143 return $self->load_myopac_update_password if $path =~ m|opac/myopac/update_password|;
144 return $self->load_myopac_update_username if $path =~ m|opac/myopac/update_username|;
145 return $self->load_myopac_bookbags if $path =~ m|opac/myopac/lists|;
146 return $self->load_myopac_bookbag_update if $path =~ m|opac/myopac/list/update|;
147 return $self->load_myopac_circ_history if $path =~ m|opac/myopac/circ_history|;
148 return $self->load_myopac_hold_history if $path =~ m|opac/myopac/hold_history|;
149 return $self->load_myopac_prefs_notify if $path =~ m|opac/myopac/prefs_notify|;
150 return $self->load_myopac_prefs_settings if $path =~ m|opac/myopac/prefs_settings|;
151 return $self->load_myopac_prefs if $path =~ m|opac/myopac/prefs|;
153 return Apache2::Const::OK;
157 # -----------------------------------------------------------------------------
158 # Redirect to SSL equivalent of a given page
159 # -----------------------------------------------------------------------------
162 my $new_page = sprintf('https://%s%s', $self->apache->hostname, $self->apache->unparsed_uri);
163 return $self->generic_redirect($new_page);
166 # -----------------------------------------------------------------------------
167 # If an authnticated resource is requested w/o auth, redirect to the login page,
168 # then return to the originally requrested resource upon successful login.
169 # -----------------------------------------------------------------------------
172 my $login_page = sprintf('https://%s%s/login', $self->apache->hostname, $self->ctx->{opac_root});
173 my $redirect_to = uri_escape($self->apache->unparsed_uri);
174 return $self->generic_redirect("$login_page?redirect_to=$redirect_to");
177 # -----------------------------------------------------------------------------
178 # Fall-through for loading a basic page
179 # -----------------------------------------------------------------------------
181 my ($self, $page) = @_;
182 $self->ctx->{page} = $page;
184 if (my $patron_barcode = $self->cgi->param("patron_barcode")) {
185 # Special CGI variable from staff client; propagate henceforth as cookie
186 $self->apache->headers_out->add(
187 "Set-Cookie" => $self->cgi->cookie(
188 -name => "patron_barcode",
191 -value => $patron_barcode,
196 return Apache2::Const::OK;
199 # -----------------------------------------------------------------------------
200 # Tests to see if the user is authenticated and sets some common context values
201 # -----------------------------------------------------------------------------
205 my $e = $self->editor;
206 my $ctx = $self->ctx;
208 $ctx->{referer} = $self->cgi->referer;
209 $ctx->{path_info} = $self->cgi->path_info;
210 $ctx->{full_path} = $ctx->{base_path} . $self->cgi->path_info;
211 $ctx->{unparsed_uri} = $self->apache->unparsed_uri;
212 $ctx->{opac_root} = $ctx->{base_path} . "/opac"; # absolute base url
213 $ctx->{is_staff} = ($self->apache->headers_in->get('User-Agent') =~ /oils_xulrunner/);
214 $ctx->{orig_loc} = $self->get_orig_loc;
216 # capture some commonly accessed pages
217 $ctx->{home_page} = 'http://' . $self->apache->hostname . $self->ctx->{opac_root} . "/home";
218 $ctx->{logout_page} = 'https://' . $self->apache->hostname . $self->ctx->{opac_root} . "/logout";
220 if($e->authtoken($self->cgi->cookie(COOKIE_SES))) {
224 $ctx->{authtoken} = $e->authtoken;
225 $ctx->{authtime} = $e->authtime;
226 $ctx->{user} = $e->requestor;
228 $ctx->{user_stats} = $U->simplereq(
230 'open-ils.actor.user.opac.vital_stats',
231 $e->authtoken, $e->requestor->id);
235 # if we encounter a stale authtoken, call load_logout
236 # to clean up the cookie, then redirect the user to the
237 # originally requested page
238 return $self->load_logout($self->apache->unparsed_uri);
242 return Apache2::Const::OK;
245 # orig_loc (i.e. "original location") passed in as a URL
246 # param will replace any existing orig_loc stored as a cookie.
250 if(my $orig_loc = $self->cgi->param('orig_loc')) {
251 $self->apache->headers_out->add(
252 "Set-Cookie" => $self->cgi->cookie(
253 -name => COOKIE_ORIG_LOC,
254 -path => $self->ctx->{base_path},
262 return $self->cgi->cookie('orig_loc');
267 # -----------------------------------------------------------------------------
268 # Log in and redirect to the redirect_to URL (or home)
269 # -----------------------------------------------------------------------------
272 my $cgi = $self->cgi;
273 my $ctx = $self->ctx;
275 $ctx->{page} = 'login';
277 my $username = $cgi->param('username');
278 my $password = $cgi->param('password');
279 my $org_unit = $cgi->param('loc') || $ctx->{aou_tree}->()->id;
280 my $persist = $cgi->param('persist');
282 # initial log form only
283 return Apache2::Const::OK unless $username and $password;
285 my $seed = $U->simplereq(
287 'open-ils.auth.authenticate.init', $username);
290 barcode => $username,
291 password => md5_hex($seed . md5_hex($password)),
292 type => ($persist) ? 'persist' : 'opac'
295 my $bc_regex = $ctx->{get_org_setting}->($org_unit, 'opac.barcode_regex');
297 $args->{username} = delete $args->{barcode}
298 if $bc_regex and !($username =~ /$bc_regex/);
300 my $response = $U->simplereq(
301 'open-ils.auth', 'open-ils.auth.authenticate.complete', $args);
303 if($U->event_code($response)) {
304 # login failed, report the reason to the template
305 $ctx->{login_failed_event} = $response;
306 return Apache2::Const::OK;
309 # login succeeded, redirect as necessary
311 my $acct = $self->apache->unparsed_uri;
312 $acct =~ s|/login|/myopac/main|;
314 return $self->generic_redirect(
315 $cgi->param('redirect_to') || $acct,
320 -value => $response->{payload}->{authtoken},
321 -expires => ($persist) ? CORE::time + $response->{payload}->{authtime} : undef
326 # -----------------------------------------------------------------------------
327 # Log out and redirect to the home page
328 # -----------------------------------------------------------------------------
331 my $redirect_to = shift;
333 # If the user was adding anyting to an anonymous cache
334 # while logged in, go ahead and clear it out.
335 $self->clear_anon_cache;
337 return $self->generic_redirect(
338 $redirect_to || $self->ctx->{home_page},