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