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';
30 my($class, $apache, $ctx) = @_;
32 my $self = bless({}, ref($class) || $class);
34 $self->apache($apache);
38 OpenILS::Utils::CStoreEditor->init; # just in case
39 $self->editor(new_editor());
45 # current Apache2::RequestRec;
47 my($self, $apache) = @_;
48 $self->{apache} = $apache if $apache;
49 return $self->{apache};
52 # runtime / template context
55 $self->{ctx} = $ctx if $ctx;
61 my($self, $editor) = @_;
62 $self->{editor} = $editor if $editor;
63 return $self->{editor};
69 $self->{cgi} = $cgi if $cgi;
74 # -----------------------------------------------------------------------------
75 # Perform initial setup, load common data, then load page data
76 # -----------------------------------------------------------------------------
80 $self->init_ro_object_cache;
82 my $stat = $self->load_common;
83 return $stat unless $stat == Apache2::Const::OK;
85 my $path = $self->apache->path_info;
87 (undef, $self->ctx->{mylist}) = $self->fetch_mylist unless
88 $path =~ /opac\/my(opac\/lists|list)/;
90 return $self->load_simple("home") if $path =~ m|opac/home|;
91 return $self->load_simple("advanced") if
92 $path =~ m:opac/(advanced|numeric|expert):;
94 return $self->load_rresults if $path =~ m|opac/results|;
95 return $self->load_record if $path =~ m|opac/record|;
97 return $self->load_mylist_add if $path =~ m|opac/mylist/add|;
98 return $self->load_mylist_move if $path =~ m|opac/mylist/move|;
99 return $self->load_mylist if $path =~ m|opac/mylist|;
100 return $self->load_cache_clear if $path =~ m|opac/cache/clear|;
102 # ----------------------------------------------------------------
103 # Logout and login require SSL
104 # ----------------------------------------------------------------
105 if($path =~ m|opac/login|) {
106 return $self->redirect_ssl unless $self->cgi->https;
107 return $self->load_login unless $self->editor->requestor; # already logged in?
109 # This will be less confusing to users than to be shown a login form
110 # when they're already logged in.
111 return $self->generic_redirect(
113 "https://%s%s/myopac/main",
114 $self->apache->hostname, $self->ctx->{opac_root}
119 if($path =~ m|opac/logout|) {
120 #return Apache2::Const::FORBIDDEN unless $self->cgi->https;
121 $self->apache->log->warn("catloader: logout called in non-secure context from " .
122 ($self->ctx->{referer} || '<no referer>')) unless $self->cgi->https;
123 return $self->load_logout;
126 # ----------------------------------------------------------------
127 # Everything below here requires SSL + authentication
128 # ----------------------------------------------------------------
129 return $self->redirect_auth
130 unless $self->cgi->https and $self->editor->requestor;
132 return $self->load_place_hold if $path =~ m|opac/place_hold|;
133 return $self->load_myopac_holds if $path =~ m|opac/myopac/holds|;
134 return $self->load_myopac_circs if $path =~ m|opac/myopac/circs|;
135 return $self->load_myopac_payment_form if $path =~ m|opac/myopac/main_payment_form|;
136 return $self->load_myopac_payments if $path =~ m|opac/myopac/main_payments|;
137 return $self->load_myopac_pay if $path =~ m|opac/myopac/main_pay|;
138 return $self->load_myopac_main if $path =~ m|opac/myopac/main|;
139 return $self->load_myopac_receipt_email if $path =~ m|opac/myopac/receipt_email|;
140 return $self->load_myopac_receipt_print if $path =~ m|opac/myopac/receipt_print|;
141 return $self->load_myopac_update_email if $path =~ m|opac/myopac/update_email|;
142 return $self->load_myopac_update_password if $path =~ m|opac/myopac/update_password|;
143 return $self->load_myopac_update_username if $path =~ m|opac/myopac/update_username|;
144 return $self->load_myopac_bookbags if $path =~ m|opac/myopac/lists|;
145 return $self->load_myopac_bookbag_update if $path =~ m|opac/myopac/list/update|;
146 return $self->load_myopac_circ_history if $path =~ m|opac/myopac/circ_history|;
147 return $self->load_myopac_hold_history if $path =~ m|opac/myopac/hold_history|;
148 return $self->load_myopac_prefs_notify if $path =~ m|opac/myopac/prefs_notify|;
149 return $self->load_myopac_prefs_settings if $path =~ m|opac/myopac/prefs_settings|;
150 return $self->load_myopac_prefs if $path =~ m|opac/myopac/prefs|;
152 return Apache2::Const::OK;
156 # -----------------------------------------------------------------------------
157 # Redirect to SSL equivalent of a given page
158 # -----------------------------------------------------------------------------
161 my $new_page = sprintf('https://%s%s', $self->apache->hostname, $self->apache->unparsed_uri);
162 return $self->generic_redirect($new_page);
165 # -----------------------------------------------------------------------------
166 # If an authnticated resource is requested w/o auth, redirect to the login page,
167 # then return to the originally requrested resource upon successful login.
168 # -----------------------------------------------------------------------------
171 my $login_page = sprintf('https://%s%s/login', $self->apache->hostname, $self->ctx->{opac_root});
172 my $redirect_to = uri_escape($self->apache->unparsed_uri);
173 return $self->generic_redirect("$login_page?redirect_to=$redirect_to");
176 # -----------------------------------------------------------------------------
177 # Fall-through for loading a basic page
178 # -----------------------------------------------------------------------------
180 my ($self, $page) = @_;
181 $self->ctx->{page} = $page;
183 if (my $patron_barcode = $self->cgi->param("patron_barcode")) {
184 # Special CGI variable from staff client; propagate henceforth as cookie
185 $self->apache->headers_out->add(
186 "Set-Cookie" => $self->cgi->cookie(
187 -name => "patron_barcode",
190 -value => $patron_barcode,
195 return Apache2::Const::OK;
198 # -----------------------------------------------------------------------------
199 # Tests to see if the user is authenticated and sets some common context values
200 # -----------------------------------------------------------------------------
204 my $e = $self->editor;
205 my $ctx = $self->ctx;
207 $ctx->{referer} = $self->cgi->referer;
208 $ctx->{path_info} = $self->cgi->path_info;
209 $ctx->{full_path} = $ctx->{base_path} . $self->cgi->path_info;
210 $ctx->{unparsed_uri} = $self->apache->unparsed_uri;
211 $ctx->{opac_root} = $ctx->{base_path} . "/opac"; # absolute base url
212 $ctx->{is_staff} = ($self->apache->headers_in->get('User-Agent') =~ /oils_xulrunner/);
214 # capture some commonly accessed pages
215 $ctx->{home_page} = 'http://' . $self->apache->hostname . $self->ctx->{opac_root} . "/home";
216 $ctx->{logout_page} = 'https://' . $self->apache->hostname . $self->ctx->{opac_root} . "/logout";
218 if($e->authtoken($self->cgi->cookie(COOKIE_SES))) {
222 $ctx->{authtoken} = $e->authtoken;
223 $ctx->{authtime} = $e->authtime;
224 $ctx->{user} = $e->requestor;
226 $ctx->{user_stats} = $U->simplereq(
228 'open-ils.actor.user.opac.vital_stats',
229 $e->authtoken, $e->requestor->id);
233 # if we encounter a stale authtoken, call load_logout
234 # to clean up the cookie, then redirect the user to the
235 # originally requested page
236 return $self->load_logout($self->apache->unparsed_uri);
240 return Apache2::Const::OK;
244 # -----------------------------------------------------------------------------
245 # Log in and redirect to the redirect_to URL (or home)
246 # -----------------------------------------------------------------------------
249 my $cgi = $self->cgi;
250 my $ctx = $self->ctx;
252 $ctx->{page} = 'login';
254 my $username = $cgi->param('username');
255 my $password = $cgi->param('password');
256 my $org_unit = $cgi->param('loc') || $ctx->{aou_tree}->()->id;
257 my $persist = $cgi->param('persist');
259 # initial log form only
260 return Apache2::Const::OK unless $username and $password;
262 my $seed = $U->simplereq(
264 'open-ils.auth.authenticate.init', $username);
267 barcode => $username,
268 password => md5_hex($seed . md5_hex($password)),
269 type => ($persist) ? 'persist' : 'opac'
272 my $bc_regex = $ctx->{get_org_setting}->($org_unit, 'opac.barcode_regex');
274 $args->{username} = delete $args->{barcode}
275 if $bc_regex and !($username =~ /$bc_regex/);
277 my $response = $U->simplereq(
278 'open-ils.auth', 'open-ils.auth.authenticate.complete', $args);
280 if($U->event_code($response)) {
281 # login failed, report the reason to the template
282 $ctx->{login_failed_event} = $response;
283 return Apache2::Const::OK;
286 # login succeeded, redirect as necessary
288 my $acct = $self->apache->unparsed_uri;
289 $acct =~ s|/login|/myopac/main|;
291 return $self->generic_redirect(
292 $cgi->param('redirect_to') || $acct,
297 -value => $response->{payload}->{authtoken},
298 -expires => ($persist) ? CORE::time + $response->{payload}->{authtime} : undef
303 # -----------------------------------------------------------------------------
304 # Log out and redirect to the home page
305 # -----------------------------------------------------------------------------
308 my $redirect_to = shift;
310 # If the user was adding anyting to an anonymous cache
311 # while logged in, go ahead and clear it out.
312 $self->clear_anon_cache;
314 return $self->generic_redirect(
315 $redirect_to || $self->ctx->{home_page},