]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader.pm
Bookbag enhancements in TTOPAC
[Evergreen.git] / Open-ILS / src / perlmods / lib / OpenILS / WWW / EGCatLoader.pm
1 package OpenILS::WWW::EGCatLoader;
2 use strict; use warnings;
3 use XML::LibXML;
4 use URI::Escape;
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);
17
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
25 my $U = 'OpenILS::Application::AppUtils';
26
27 use constant COOKIE_SES => 'ses';
28 use constant COOKIE_ORIG_LOC => 'eg_orig_loc';
29
30 sub new {
31     my($class, $apache, $ctx) = @_;
32
33     my $self = bless({}, ref($class) || $class);
34
35     $self->apache($apache);
36     $self->ctx($ctx);
37     $self->cgi(new CGI);
38
39     OpenILS::Utils::CStoreEditor->init; # just in case
40     $self->editor(new_editor());
41
42     return $self;
43 }
44
45
46 # current Apache2::RequestRec;
47 sub apache {
48     my($self, $apache) = @_;
49     $self->{apache} = $apache if $apache;
50     return $self->{apache};
51 }
52
53 # runtime / template context
54 sub ctx {
55     my($self, $ctx) = @_;
56     $self->{ctx} = $ctx if $ctx;
57     return $self->{ctx};
58 }
59
60 # cstore editor
61 sub editor {
62     my($self, $editor) = @_;
63     $self->{editor} = $editor if $editor;
64     return $self->{editor};
65 }
66
67 # CGI handle
68 sub cgi {
69     my($self, $cgi) = @_;
70     $self->{cgi} = $cgi if $cgi;
71     return $self->{cgi};
72 }
73
74
75 # -----------------------------------------------------------------------------
76 # Perform initial setup, load common data, then load page data
77 # -----------------------------------------------------------------------------
78 sub load {
79     my $self = shift;
80
81     $self->init_ro_object_cache;
82
83     my $stat = $self->load_common;
84     return $stat unless $stat == Apache2::Const::OK;
85
86     my $path = $self->apache->path_info;
87
88     (undef, $self->ctx->{mylist}) = $self->fetch_mylist unless
89         $path =~ /opac\/my(opac\/lists|list)/;
90
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):;
94
95     return $self->load_rresults if $path =~ m|opac/results|;
96     return $self->load_record if $path =~ m|opac/record|;
97     return $self->load_cnbrowse if $path =~ m|opac/cnbrowse|;
98
99     return $self->load_mylist_add if $path =~ m|opac/mylist/add|;
100     return $self->load_mylist_delete if $path =~ m|opac/mylist/delete|;
101     return $self->load_mylist_move if $path =~ m|opac/mylist/move|;
102     return $self->load_mylist if $path =~ m|opac/mylist|;
103     return $self->load_cache_clear if $path =~ m|opac/cache/clear|;
104
105     # ----------------------------------------------------------------
106     # Logout and login require SSL
107     # ----------------------------------------------------------------
108     if($path =~ m|opac/login|) {
109         return $self->redirect_ssl unless $self->cgi->https;
110         return $self->load_login unless $self->editor->requestor; # already logged in?
111
112         # This will be less confusing to users than to be shown a login form
113         # when they're already logged in.
114         return $self->generic_redirect(
115             sprintf(
116                 "https://%s%s/myopac/main",
117                 $self->apache->hostname, $self->ctx->{opac_root}
118             )
119         );
120     }
121
122     if($path =~ m|opac/logout|) {
123         #return Apache2::Const::FORBIDDEN unless $self->cgi->https; 
124         $self->apache->log->warn("catloader: logout called in non-secure context from " . 
125             ($self->ctx->{referer} || '<no referer>')) unless $self->cgi->https;
126         return $self->load_logout;
127     }
128
129     # ----------------------------------------------------------------
130     #  Everything below here requires SSL + authentication
131     # ----------------------------------------------------------------
132     return $self->redirect_auth
133         unless $self->cgi->https and $self->editor->requestor;
134
135     return $self->load_place_hold if $path =~ m|opac/place_hold|;
136     return $self->load_myopac_holds if $path =~ m|opac/myopac/holds|;
137     return $self->load_myopac_circs if $path =~ m|opac/myopac/circs|;
138     return $self->load_myopac_payment_form if $path =~ m|opac/myopac/main_payment_form|;
139     return $self->load_myopac_payments if $path =~ m|opac/myopac/main_payments|;
140     return $self->load_myopac_pay if $path =~ m|opac/myopac/main_pay|;
141     return $self->load_myopac_main if $path =~ m|opac/myopac/main|;
142     return $self->load_myopac_receipt_email if $path =~ m|opac/myopac/receipt_email|;
143     return $self->load_myopac_receipt_print if $path =~ m|opac/myopac/receipt_print|;
144     return $self->load_myopac_update_email if $path =~ m|opac/myopac/update_email|;
145     return $self->load_myopac_update_password if $path =~ m|opac/myopac/update_password|;
146     return $self->load_myopac_update_username if $path =~ m|opac/myopac/update_username|;
147     return $self->load_myopac_bookbags if $path =~ m|opac/myopac/lists|;
148     return $self->load_myopac_bookbag_print if $path =~ m|opac/myopac/list/print|;
149     return $self->load_myopac_bookbag_update if $path =~ m|opac/myopac/list/update|;
150     return $self->load_myopac_circ_history if $path =~ m|opac/myopac/circ_history|;
151     return $self->load_myopac_hold_history if $path =~ m|opac/myopac/hold_history|;
152     return $self->load_myopac_prefs_notify if $path =~ m|opac/myopac/prefs_notify|;
153     return $self->load_myopac_prefs_settings if $path =~ m|opac/myopac/prefs_settings|;
154     return $self->load_myopac_prefs if $path =~ m|opac/myopac/prefs|;
155
156     return Apache2::Const::OK;
157 }
158
159
160 # -----------------------------------------------------------------------------
161 # Redirect to SSL equivalent of a given page
162 # -----------------------------------------------------------------------------
163 sub redirect_ssl {
164     my $self = shift;
165     my $new_page = sprintf('https://%s%s', $self->apache->hostname, $self->apache->unparsed_uri);
166     return $self->generic_redirect($new_page);
167 }
168
169 # -----------------------------------------------------------------------------
170 # If an authnticated resource is requested w/o auth, redirect to the login page,
171 # then return to the originally requrested resource upon successful login.
172 # -----------------------------------------------------------------------------
173 sub redirect_auth {
174     my $self = shift;
175     my $login_page = sprintf('https://%s%s/login', $self->apache->hostname, $self->ctx->{opac_root});
176     my $redirect_to = uri_escape($self->apache->unparsed_uri);
177     return $self->generic_redirect("$login_page?redirect_to=$redirect_to");
178 }
179
180 # -----------------------------------------------------------------------------
181 # Fall-through for loading a basic page
182 # -----------------------------------------------------------------------------
183 sub load_simple {
184     my ($self, $page) = @_;
185     $self->ctx->{page} = $page;
186
187     if (my $patron_barcode = $self->cgi->param("patron_barcode")) {
188         # Special CGI variable from staff client; propagate henceforth as cookie
189         $self->apache->headers_out->add(
190             "Set-Cookie" => $self->cgi->cookie(
191                 -name => "patron_barcode",
192                 -path => "/",
193                 -secure => 1,
194                 -value => $patron_barcode,
195                 -expires => undef
196             )
197         );
198     }
199     return Apache2::Const::OK;
200 }
201
202 # -----------------------------------------------------------------------------
203 # Tests to see if the user is authenticated and sets some common context values
204 # -----------------------------------------------------------------------------
205 sub load_common {
206     my $self = shift;
207
208     my $e = $self->editor;
209     my $ctx = $self->ctx;
210
211     $ctx->{referer} = $self->cgi->referer;
212     $ctx->{path_info} = $self->cgi->path_info;
213     $ctx->{full_path} = $ctx->{base_path} . $self->cgi->path_info;
214     $ctx->{unparsed_uri} = $self->apache->unparsed_uri;
215     $ctx->{opac_root} = $ctx->{base_path} . "/opac"; # absolute base url
216     $ctx->{is_staff} = ($self->apache->headers_in->get('User-Agent') =~ /oils_xulrunner/);
217     $ctx->{orig_loc} = $self->get_orig_loc;
218
219     # capture some commonly accessed pages
220     $ctx->{home_page} = 'http://' . $self->apache->hostname . $self->ctx->{opac_root} . "/home";
221     $ctx->{logout_page} = 'https://' . $self->apache->hostname . $self->ctx->{opac_root} . "/logout";
222
223     if($e->authtoken($self->cgi->cookie(COOKIE_SES))) {
224
225         if($e->checkauth) {
226
227             $ctx->{authtoken} = $e->authtoken;
228             $ctx->{authtime} = $e->authtime;
229             $ctx->{user} = $e->requestor;
230
231             $ctx->{user_stats} = $U->simplereq(
232                 'open-ils.actor', 
233                 'open-ils.actor.user.opac.vital_stats', 
234                 $e->authtoken, $e->requestor->id);
235
236         } else {
237
238             # if we encounter a stale authtoken, call load_logout 
239             # to clean up the cookie, then redirect the user to the
240             # originally requested page
241             return $self->load_logout($self->apache->unparsed_uri);
242         }
243     }
244
245     return Apache2::Const::OK;
246 }
247
248 # orig_loc (i.e. "original location") passed in as a URL 
249 # param will replace any existing orig_loc stored as a cookie.
250 sub get_orig_loc {
251     my $self = shift;
252
253     if(my $orig_loc = $self->cgi->param('orig_loc')) {
254         $self->apache->headers_out->add(
255             "Set-Cookie" => $self->cgi->cookie(
256                 -name => COOKIE_ORIG_LOC,
257                 -path => $self->ctx->{base_path},
258                 -value => $orig_loc,
259                 -expires => undef
260             )
261         );
262         return $orig_loc;
263     }
264
265     return $self->cgi->cookie(COOKIE_ORIG_LOC);
266 }
267
268
269
270 # -----------------------------------------------------------------------------
271 # Log in and redirect to the redirect_to URL (or home)
272 # -----------------------------------------------------------------------------
273 sub load_login {
274     my $self = shift;
275     my $cgi = $self->cgi;
276     my $ctx = $self->ctx;
277
278     $ctx->{page} = 'login';
279
280     my $username = $cgi->param('username');
281     my $password = $cgi->param('password');
282     my $org_unit = $cgi->param('loc') || $ctx->{aou_tree}->()->id;
283     my $persist = $cgi->param('persist');
284
285     # initial log form only
286     return Apache2::Const::OK unless $username and $password;
287
288         my $seed = $U->simplereq(
289         'open-ils.auth', 
290                 'open-ils.auth.authenticate.init', $username);
291
292     my $args = {        
293         username => $username, 
294         password => md5_hex($seed . md5_hex($password)), 
295         type => ($persist) ? 'persist' : 'opac' 
296     };
297
298     my $bc_regex = $ctx->{get_org_setting}->($org_unit, 'opac.barcode_regex');
299
300     $args->{barcode} = delete $args->{username} 
301         if $bc_regex and ($username =~ /$bc_regex/);
302
303         my $response = $U->simplereq(
304         'open-ils.auth', 'open-ils.auth.authenticate.complete', $args);
305
306     if($U->event_code($response)) { 
307         # login failed, report the reason to the template
308         $ctx->{login_failed_event} = $response;
309         return Apache2::Const::OK;
310     }
311
312     # login succeeded, redirect as necessary
313
314     my $acct = $self->apache->unparsed_uri;
315     $acct =~ s|/login|/myopac/main|;
316
317     return $self->generic_redirect(
318         $cgi->param('redirect_to') || $acct,
319         $cgi->cookie(
320             -name => COOKIE_SES,
321             -path => '/',
322             -secure => 1,
323             -value => $response->{payload}->{authtoken},
324             -expires => ($persist) ? CORE::time + $response->{payload}->{authtime} : undef
325         )
326     );
327 }
328
329 # -----------------------------------------------------------------------------
330 # Log out and redirect to the home page
331 # -----------------------------------------------------------------------------
332 sub load_logout {
333     my $self = shift;
334     my $redirect_to = shift;
335
336     # If the user was adding anyting to an anonymous cache 
337     # while logged in, go ahead and clear it out.
338     $self->clear_anon_cache;
339
340     return $self->generic_redirect(
341         $redirect_to || $self->ctx->{home_page},
342         $self->cgi->cookie(
343             -name => COOKIE_SES,
344             -path => '/',
345             -value => '',
346             -expires => '-1h'
347         )
348     );
349 }
350
351 1;
352