]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader.pm
Ability to add records to permanent bookbags in TPAC.
[working/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 use Time::HiRes;
18
19 # EGCatLoader sub-modules 
20 use OpenILS::WWW::EGCatLoader::Util;
21 use OpenILS::WWW::EGCatLoader::Account;
22 use OpenILS::WWW::EGCatLoader::Search;
23 use OpenILS::WWW::EGCatLoader::Record;
24 use OpenILS::WWW::EGCatLoader::Container;
25 use OpenILS::WWW::EGCatLoader::SMS;
26
27 my $U = 'OpenILS::Application::AppUtils';
28
29 use constant COOKIE_SES => 'ses';
30 use constant COOKIE_PHYSICAL_LOC => 'eg_physical_loc';
31 use constant COOKIE_SSS_EXPAND => 'eg_sss_expand';
32
33 use constant COOKIE_ANON_CACHE => 'anoncache';
34 use constant ANON_CACHE_MYLIST => 'mylist';
35 use constant ANON_CACHE_STAFF_SEARCH => 'staffsearch';
36
37 use constant DEBUG_TIMING => 0;
38
39 sub new {
40     my($class, $apache, $ctx) = @_;
41
42     my $self = bless({}, ref($class) || $class);
43
44     $self->apache($apache);
45     $self->ctx($ctx);
46     $self->cgi(new CGI);
47     $self->timelog("New page");
48
49     OpenILS::Utils::CStoreEditor->init; # just in case
50     $self->editor(new_editor());
51
52     return $self;
53 }
54
55
56 # current Apache2::RequestRec;
57 sub apache {
58     my($self, $apache) = @_;
59     $self->{apache} = $apache if $apache;
60     return $self->{apache};
61 }
62
63 # runtime / template context
64 sub ctx {
65     my($self, $ctx) = @_;
66     $self->{ctx} = $ctx if $ctx;
67     return $self->{ctx};
68 }
69
70 # cstore editor
71 sub editor {
72     my($self, $editor) = @_;
73     $self->{editor} = $editor if $editor;
74     return $self->{editor};
75 }
76
77 # CGI handle
78 sub cgi {
79     my($self, $cgi) = @_;
80     $self->{cgi} = $cgi if $cgi;
81     return $self->{cgi};
82 }
83
84 sub timelog {
85     my($self, $description) = @_;
86
87     return unless DEBUG_TIMING;
88     return unless $description;
89     $self->ctx->{timing} ||= [];
90     
91     my $timer = [Time::HiRes::gettimeofday()];
92     $self->ctx->{time_begin} ||= $timer;
93
94     push @{$self->ctx->{timing}}, [
95         Time::HiRes::tv_interval($self->ctx->{time_begin}, $timer), $description
96     ];
97 }
98
99 # -----------------------------------------------------------------------------
100 # Perform initial setup, load common data, then load page data
101 # -----------------------------------------------------------------------------
102 sub load {
103     my $self = shift;
104
105     $self->init_ro_object_cache;
106     $self->timelog("Initial load");
107
108     my $stat = $self->load_common;
109     return $stat unless $stat == Apache2::Const::OK;
110
111     my $path = $self->apache->path_info;
112
113     (undef, $self->ctx->{mylist}) = $self->fetch_mylist unless
114         $path =~ /opac\/my(opac\/lists|list)/;
115
116     return $self->load_simple("home") if $path =~ m|opac/home|;
117     return $self->load_simple("advanced") if
118         $path =~ m:opac/(advanced|numeric|expert):;
119
120     return $self->load_rresults if $path =~ m|opac/results|;
121     return $self->load_print_record if $path =~ m|opac/record/print|;
122     return $self->load_record if $path =~ m|opac/record/\d|;
123     return $self->load_cnbrowse if $path =~ m|opac/cnbrowse|;
124
125     return $self->load_mylist_add if $path =~ m|opac/mylist/add|;
126     return $self->load_mylist_delete if $path =~ m|opac/mylist/delete|;
127     return $self->load_mylist_move if $path =~ m|opac/mylist/move|;
128     return $self->load_mylist if $path =~ m|opac/mylist|;
129     return $self->load_cache_clear if $path =~ m|opac/cache/clear|;
130     return $self->load_temp_warn_post if $path =~ m|opac/temp_warn/post|;
131     return $self->load_temp_warn if $path =~ m|opac/temp_warn|;
132
133     # ----------------------------------------------------------------
134     #  Everything below here requires SSL
135     # ----------------------------------------------------------------
136     return $self->redirect_ssl unless $self->cgi->https;
137     return $self->load_password_reset if $path =~ m|opac/password_reset|;
138     return $self->load_logout if $path =~ m|opac/logout|;
139
140     if($path =~ m|opac/login|) {
141         return $self->load_login unless $self->editor->requestor; # already logged in?
142
143         # This will be less confusing to users than to be shown a login form
144         # when they're already logged in.
145         return $self->generic_redirect(
146             sprintf(
147                 "https://%s%s/myopac/main",
148                 $self->apache->hostname, $self->ctx->{opac_root}
149             )
150         );
151     }
152
153     if ($path =~ m|opac/sms_cn| and !$self->editor->requestor) {
154         my $org_unit = $self->ctx->{physical_loc} || $self->cgi->param('loc') || $self->ctx->{aou_tree}->()->id;
155         my $skip_sms_auth = $self->ctx->{get_org_setting}->($org_unit, 'sms.disable_authentication_requirement.callnumbers');
156         return $self->load_sms_cn if $skip_sms_auth;
157     }
158
159     # ----------------------------------------------------------------
160     #  Everything below here requires authentication
161     # ----------------------------------------------------------------
162     return $self->redirect_auth unless $self->editor->requestor;
163
164     # Don't cache anything requiring auth for security reasons
165     $self->apache->headers_out->add("cache-control" => "no-store, no-cache, must-revalidate");
166     $self->apache->headers_out->add("expires" => "-1");
167
168     return $self->load_email_record if $path =~ m|opac/record/email|;
169
170     return $self->load_place_hold if $path =~ m|opac/place_hold|;
171     return $self->load_myopac_holds if $path =~ m|opac/myopac/holds|;
172     return $self->load_myopac_circs if $path =~ m|opac/myopac/circs|;
173     return $self->load_myopac_payment_form if $path =~ m|opac/myopac/main_payment_form|;
174     return $self->load_myopac_payments if $path =~ m|opac/myopac/main_payments|;
175     return $self->load_myopac_pay_init if $path =~ m|opac/myopac/main_pay_init|;
176     return $self->load_myopac_pay if $path =~ m|opac/myopac/main_pay|;
177     return $self->load_myopac_main if $path =~ m|opac/myopac/main|;
178     return $self->load_myopac_receipt_email if $path =~ m|opac/myopac/receipt_email|;
179     return $self->load_myopac_receipt_print if $path =~ m|opac/myopac/receipt_print|;
180     return $self->load_myopac_update_email if $path =~ m|opac/myopac/update_email|;
181     return $self->load_myopac_update_password if $path =~ m|opac/myopac/update_password|;
182     return $self->load_myopac_update_username if $path =~ m|opac/myopac/update_username|;
183     return $self->load_myopac_bookbags if $path =~ m|opac/myopac/lists|;
184     return $self->load_myopac_bookbag_print if $path =~ m|opac/myopac/list/print|;
185     return $self->load_myopac_bookbag_update if $path =~ m|opac/myopac/list/update|;
186     return $self->load_myopac_circ_history_export if $path =~ m|opac/myopac/circ_history/export|;
187     return $self->load_myopac_circ_history if $path =~ m|opac/myopac/circ_history|;
188     return $self->load_myopac_hold_history if $path =~ m|opac/myopac/hold_history|;
189     return $self->load_myopac_prefs_notify if $path =~ m|opac/myopac/prefs_notify|;
190     return $self->load_myopac_prefs_settings if $path =~ m|opac/myopac/prefs_settings|;
191     return $self->load_myopac_prefs if $path =~ m|opac/myopac/prefs|;
192     return $self->load_sms_cn if $path =~ m|opac/sms_cn|;
193
194     return Apache2::Const::OK;
195 }
196
197
198 # -----------------------------------------------------------------------------
199 # Redirect to SSL equivalent of a given page
200 # -----------------------------------------------------------------------------
201 sub redirect_ssl {
202     my $self = shift;
203     my $new_page = sprintf('https://%s%s', $self->apache->hostname, $self->apache->unparsed_uri);
204     return $self->generic_redirect($new_page);
205 }
206
207 # -----------------------------------------------------------------------------
208 # If an authnticated resource is requested w/o auth, redirect to the login page,
209 # then return to the originally requrested resource upon successful login.
210 # -----------------------------------------------------------------------------
211 sub redirect_auth {
212     my $self = shift;
213     my $login_page = sprintf('https://%s%s/login', $self->apache->hostname, $self->ctx->{opac_root});
214     my $redirect_to = uri_escape($self->apache->unparsed_uri);
215     return $self->generic_redirect("$login_page?redirect_to=$redirect_to");
216 }
217
218 # -----------------------------------------------------------------------------
219 # Fall-through for loading a basic page
220 # -----------------------------------------------------------------------------
221 sub load_simple {
222     my ($self, $page) = @_;
223     $self->ctx->{page} = $page;
224     $self->ctx->{search_ou} = $self->_get_search_lib();
225
226     return Apache2::Const::OK;
227 }
228
229 # -----------------------------------------------------------------------------
230 # Tests to see if the user is authenticated and sets some common context values
231 # -----------------------------------------------------------------------------
232 sub load_common {
233     my $self = shift;
234
235     my $e = $self->editor;
236     my $ctx = $self->ctx;
237
238     $ctx->{referer} = $self->cgi->referer;
239     $ctx->{path_info} = $self->cgi->path_info;
240     $ctx->{full_path} = $ctx->{base_path} . $self->cgi->path_info;
241     $ctx->{unparsed_uri} = $self->apache->unparsed_uri;
242     $ctx->{opac_root} = $ctx->{base_path} . "/opac"; # absolute base url
243     $ctx->{is_staff} = 0; # Assume false, check for workstation id later.  Was: ($self->apache->headers_in->get('User-Agent') =~ /oils_xulrunner/);
244     $ctx->{physical_loc} = $self->get_physical_loc;
245
246     # capture some commonly accessed pages
247     $ctx->{home_page} = 'http://' . $self->apache->hostname . $self->ctx->{opac_root} . "/home";
248     $ctx->{logout_page} = 'https://' . $self->apache->hostname . $self->ctx->{opac_root} . "/logout";
249
250     if($e->authtoken($self->cgi->cookie(COOKIE_SES))) {
251
252         if($e->checkauth) {
253
254             $ctx->{authtoken} = $e->authtoken;
255             $ctx->{authtime} = $e->authtime;
256             $ctx->{user} = $e->requestor;
257
258             $ctx->{user_stats} = $U->simplereq(
259                 'open-ils.actor', 
260                 'open-ils.actor.user.opac.vital_stats', 
261                 $e->authtoken, $e->requestor->id);
262             $ctx->{is_staff} = 1 if $e->requestor->wsid;
263
264         } else {
265
266             # if we encounter a stale authtoken, call load_logout 
267             # to clean up the cookie, then redirect the user to the
268             # originally requested page
269             return $self->load_logout($self->apache->unparsed_uri);
270         }
271     }
272
273     $self->extract_copy_location_group_info;
274     $ctx->{search_ou} = $self->_get_search_lib();
275     $self->staff_saved_searches_set_expansion_state if $ctx->{is_staff};
276     $self->load_eg_cache_hash;
277     $self->load_copy_location_groups;
278     $self->staff_saved_searches_set_expansion_state if $ctx->{is_staff};
279     $self->load_search_filter_groups($ctx->{search_ou});
280
281     return Apache2::Const::OK;
282 }
283
284 sub staff_saved_searches_set_expansion_state {
285     my $self = shift;
286
287     my $param = $self->cgi->param('sss_expand');
288     my $value;
289     
290     if (defined $param) {
291         $value = ($param ? 1 : 0);
292         $self->apache->headers_out->add(
293             "Set-Cookie" => $self->cgi->cookie(
294                 -name => COOKIE_SSS_EXPAND,
295                 -path => $self->ctx->{base_path},
296                 -secure => 1,   # not strictly necessary, but this feature is staff-only, so may as well
297                 -value => $value,
298                 -expires => undef
299             )
300         );
301     } else {
302         $value = $self->cgi->cookie(COOKIE_SSS_EXPAND);
303     }
304
305     $self->ctx->{saved_searches_expanded} = $value;
306 }
307
308 # physical_loc (i.e. "original location") passed in as a URL 
309 # param will replace any existing physical_loc stored as a cookie.
310 sub get_physical_loc {
311     my $self = shift;
312
313     if(my $physical_loc = $self->cgi->param('physical_loc')) {
314         $self->apache->headers_out->add(
315             "Set-Cookie" => $self->cgi->cookie(
316                 -name => COOKIE_PHYSICAL_LOC,
317                 -path => $self->ctx->{base_path},
318                 -value => $physical_loc,
319                 -expires => undef
320             )
321         );
322         return $physical_loc;
323     }
324
325     return $self->cgi->cookie(COOKIE_PHYSICAL_LOC);
326 }
327
328 # -----------------------------------------------------------------------------
329 # Log in and redirect to the redirect_to URL (or home)
330 # -----------------------------------------------------------------------------
331 sub load_login {
332     my $self = shift;
333     my $cgi = $self->cgi;
334     my $ctx = $self->ctx;
335
336     $ctx->{page} = 'login';
337
338     my $username = $cgi->param('username');
339     my $password = $cgi->param('password');
340     my $org_unit = $cgi->param('loc') || $ctx->{aou_tree}->()->id;
341     my $persist = $cgi->param('persist');
342
343     # initial log form only
344     return Apache2::Const::OK unless $username and $password;
345
346         my $seed = $U->simplereq(
347         'open-ils.auth', 
348                 'open-ils.auth.authenticate.init', $username);
349
350     my $args = {        
351         username => $username, 
352         password => md5_hex($seed . md5_hex($password)), 
353         type => ($persist) ? 'persist' : 'opac',
354         agent => 'opac'
355     };
356
357     my $bc_regex = $ctx->{get_org_setting}->($org_unit, 'opac.barcode_regex');
358
359     # To avoid surprises, default to "Barcodes start with digits"
360     $bc_regex = '^\d' unless $bc_regex;
361
362     $args->{barcode} = delete $args->{username} 
363         if $bc_regex and ($username =~ /$bc_regex/);
364
365         my $response = $U->simplereq(
366         'open-ils.auth', 'open-ils.auth.authenticate.complete', $args);
367
368     if($U->event_code($response)) { 
369         # login failed, report the reason to the template
370         $ctx->{login_failed_event} = $response;
371         return Apache2::Const::OK;
372     }
373
374     # login succeeded, redirect as necessary
375
376     my $acct = $self->apache->unparsed_uri;
377     $acct =~ s|/login|/myopac/main|;
378
379     return $self->generic_redirect(
380         $cgi->param('redirect_to') || $acct,
381         $cgi->cookie(
382             -name => COOKIE_SES,
383             -path => '/',
384             -secure => 1,
385             -value => $response->{payload}->{authtoken},
386             -expires => ($persist) ? CORE::time + $response->{payload}->{authtime} : undef
387         )
388     );
389 }
390
391 # -----------------------------------------------------------------------------
392 # Log out and redirect to the home page
393 # -----------------------------------------------------------------------------
394 sub load_logout {
395     my $self = shift;
396     my $redirect_to = shift;
397
398     # If the user was adding anyting to an anonymous cache 
399     # while logged in, go ahead and clear it out.
400     $self->clear_anon_cache;
401
402     return $self->generic_redirect(
403         $redirect_to || $self->ctx->{home_page},
404         $self->cgi->cookie(
405             -name => COOKIE_SES,
406             -path => '/',
407             -value => '',
408             -expires => '-1h'
409         )
410     );
411 }
412
413 1;
414