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