From 553527a256d00c7914e21291634b214add46941d Mon Sep 17 00:00:00 2001 From: Bill Erickson Date: Thu, 28 Jul 2011 11:36:23 -0400 Subject: [PATCH] Redirector support for T-pac Includes new Apache configuration option: PerlSetVar OILSRedirectTpac "true" "Original location" is tracked initially as a URL param (orig_loc), but is ultimately absorbed into a cookie (eg_orig_loc) since the URL param would otherwise have to be propagated to every page. Signed-off-by: Bill Erickson --- Open-ILS/examples/apache/eg_vhost.conf | 2 + .../src/perlmods/lib/OpenILS/WWW/EGCatLoader.pm | 23 ++++ Open-ILS/src/perlmods/lib/OpenILS/WWW/EGWeb.pm | 6 +- Open-ILS/src/perlmods/lib/OpenILS/WWW/Redirect.pm | 131 +++++++++++---------- 4 files changed, 94 insertions(+), 68 deletions(-) diff --git a/Open-ILS/examples/apache/eg_vhost.conf b/Open-ILS/examples/apache/eg_vhost.conf index e0571c8d9d..48a259c61f 100644 --- a/Open-ILS/examples/apache/eg_vhost.conf +++ b/Open-ILS/examples/apache/eg_vhost.conf @@ -20,6 +20,8 @@ RedirectMatch 301 ^/$ /opac/en-US/skin/default/xml/index.xml # # OILSRedirectDepth defaults to the depth of the branch that the OPAC was directed to # #PerlSetVar OILSRedirectDepth "0" # #PerlSetVar OILSRedirectLocale "en-US" +# # Use the template-toolkit opac +# #PerlSetVar OILSRedirectTpac "true" # allow from all # diff --git a/Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader.pm b/Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader.pm index fe5074e865..e5743bca12 100644 --- a/Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader.pm +++ b/Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader.pm @@ -25,6 +25,7 @@ use OpenILS::WWW::EGCatLoader::Container; my $U = 'OpenILS::Application::AppUtils'; use constant COOKIE_SES => 'ses'; +use constant COOKIE_ORIG_LOC => 'eg_orig_loc'; sub new { my($class, $apache, $ctx) = @_; @@ -210,6 +211,7 @@ sub load_common { $ctx->{unparsed_uri} = $self->apache->unparsed_uri; $ctx->{opac_root} = $ctx->{base_path} . "/opac"; # absolute base url $ctx->{is_staff} = ($self->apache->headers_in->get('User-Agent') =~ /oils_xulrunner/); + $ctx->{orig_loc} = $self->get_orig_loc; # capture some commonly accessed pages $ctx->{home_page} = 'http://' . $self->apache->hostname . $self->ctx->{opac_root} . "/home"; @@ -240,6 +242,27 @@ sub load_common { return Apache2::Const::OK; } +# orig_loc (i.e. "original location") passed in as a URL +# param will replace any existing orig_loc stored as a cookie. +sub get_orig_loc { + my $self = shift; + + if(my $orig_loc = $self->cgi->param('orig_loc')) { + $self->apache->headers_out->add( + "Set-Cookie" => $self->cgi->cookie( + -name => COOKIE_ORIG_LOC, + -path => $self->ctx->{base_path}, + -value => $orig_loc, + -expires => undef + ) + ); + return $orig_loc; + } + + return $self->cgi->cookie('orig_loc'); +} + + # ----------------------------------------------------------------------------- # Log in and redirect to the redirect_to URL (or home) diff --git a/Open-ILS/src/perlmods/lib/OpenILS/WWW/EGWeb.pm b/Open-ILS/src/perlmods/lib/OpenILS/WWW/EGWeb.pm index b6b4ed2d9d..922f7e18c7 100644 --- a/Open-ILS/src/perlmods/lib/OpenILS/WWW/EGWeb.pm +++ b/Open-ILS/src/perlmods/lib/OpenILS/WWW/EGWeb.pm @@ -9,9 +9,9 @@ use Apache2::Log; use OpenSRF::EX qw(:try); use OpenILS::Utils::CStoreEditor; -use constant OILS_HTTP_COOKIE_SKIN => 'oils:skin'; -use constant OILS_HTTP_COOKIE_THEME => 'oils:theme'; -use constant OILS_HTTP_COOKIE_LOCALE => 'oils:locale'; +use constant OILS_HTTP_COOKIE_SKIN => 'eg_skin'; +use constant OILS_HTTP_COOKIE_THEME => 'eg_theme'; +use constant OILS_HTTP_COOKIE_LOCALE => 'eg_locale'; my $web_config; my $web_config_file; diff --git a/Open-ILS/src/perlmods/lib/OpenILS/WWW/Redirect.pm b/Open-ILS/src/perlmods/lib/OpenILS/WWW/Redirect.pm index c76cbf3706..99667d09bd 100644 --- a/Open-ILS/src/perlmods/lib/OpenILS/WWW/Redirect.pm +++ b/Open-ILS/src/perlmods/lib/OpenILS/WWW/Redirect.pm @@ -51,53 +51,90 @@ sub parse_ips_file { } +my %org_cache; sub handler { + my $apache = shift; + my $cgi = CGI->new( $apache ); + my $port = $cgi->server_port(); + my $hostname = $cgi->server_name(); + my $proto = ($cgi->https) ? 'https' : 'http'; my $user_ip = $ENV{REMOTE_ADDR}; - my $apache_obj = shift; - my $cgi = CGI->new( $apache_obj ); + # Apache config values + my $skin = $apache->dir_config('OILSRedirectSkin') || 'default'; + my $depth = $apache->dir_config('OILSRedirectDepth'); + my $locale = $apache->dir_config('OILSRedirectLocale') || 'en-US'; + my $use_tt = ($apache->dir_config('OILSRedirectTpac') || '') =~ /true/i; + my $orig_loc; - my $skin = $apache_obj->dir_config('OILSRedirectSkin') || 'default'; - my $depth = $apache_obj->dir_config('OILSRedirectDepth'); - my $locale = $apache_obj->dir_config('OILSRedirectLocale') || 'en-US'; + $apache->log->debug("Redirector sees client frim $user_ip"); - my $hostname = $cgi->server_name(); - my $port = $cgi->server_port(); + # parse the IP file + my ($shortname, $nskin, $nhostname) = redirect_libs($user_ip); - my $proto = "http"; - if($cgi->https) { $proto = "https"; } + if ($shortname) { # we have a config - my $url = "$proto://$hostname:$port/opac/$locale/skin/$skin/xml/index.xml"; - my $path = $apache_obj->path_info(); + # Read any override vars from the ips txt file + if ($nskin =~ m/[^\s]/) { $skin = $nskin; } + if ($nhostname =~ m/[^\s]/) { $hostname = $nhostname; } - $logger->debug("Apache client connecting from $user_ip"); + if($org_cache{$shortname}) { + $orig_loc = $org_cache{$shortname}; - my ($shortname, $nskin, $nhostname) = redirect_libs($user_ip); - if ($shortname) { + } else { - if ($nskin =~ m/[^\s]/) { $skin = $nskin; } - if ($nhostname =~ m/[^\s]/) { $hostname = $nhostname; } + my $session = OpenSRF::AppSession->create("open-ils.actor"); + my $org = $session->request( + 'open-ils.actor.org_unit.retrieve_by_shortname', + $shortname)->gather(1); + + $org_cache{$shortname} = $orig_loc = $org->id if $org; + } + } - $logger->info("Apache redirecting $user_ip to $shortname with skin $skin and host $hostname"); - my $session = OpenSRF::AppSession->create("open-ils.actor"); + my $url = "$proto://$hostname:$port"; - $url = "$proto://$hostname:$port/opac/$locale/skin/$skin/xml/index.xml"; + if($use_tt) { - my $org = $session->request( - 'open-ils.actor.org_unit.retrieve_by_shortname', - $shortname)->gather(1); + $url .= "/eg/opac/home"; + $url .= "?orig_loc=$orig_loc" if $orig_loc; - if($org) { - $url .= "?ol=" . $org->id; +=head potential locale/skin implementation + if($locale ne 'en-US') { + $apache->headers_out->add( + "Set-Cookie" => $cgi->cookie( + -name => "oils:locale", # see EGWeb.pm + -path => "/eg", + -value => $locale, + -expires => undef + ) + ); + } + + if($skin ne 'default') { + $apache->headers_out->add( + "Set-Cookie" => $cgi->cookie( + -name => "oils:skin", # see EGWeb.pm + -path => "/eg", + -value => $skin, + -expires => undef + ) + ); + } +=cut + + } else { + $url .= "/opac/$locale/skin/$skin/xml/index.xml"; + if($orig_loc) { + $url .= "?ol=" . $orig_loc; $url .= "&d=$depth" if defined $depth; } - } + } - print "Location: $url\n\n"; + $logger->info("Apache redirecting $user_ip to $url"); + $apache->headers_out->add('Location' => "$url"); return Apache2::Const::REDIRECT; - - return print_page($url); } sub redirect_libs { @@ -113,7 +150,7 @@ sub redirect_libs { my $range = new Net::IP( $block->[0] . ' - ' . $block->[1] ); if( $source_ip->overlaps($range)==$IP_A_IN_B_OVERLAP || $source_ip->overlaps($range)==$IP_IDENTICAL ) { - return ($shortname, $block->[2], $block->[3]); + return ($shortname, $block->[2] || '', $block->[3] || ''); } } } @@ -121,40 +158,4 @@ sub redirect_libs { return 0; } - -sub print_page { - - my $url = shift; - - print "Content-type: text/html; charset=utf-8\n\n"; - print <<" HTML"; - - - - - - -

-
-

Loading...

-
-

-
- - - HTML - - return Apache2::Const::OK; -} - - 1; -- 2.11.0