Redirector support for T-pac
authorBill Erickson <berick@esilibrary.com>
Thu, 28 Jul 2011 15:36:23 +0000 (11:36 -0400)
committerBill Erickson <berick@esilibrary.com>
Thu, 28 Jul 2011 15:36:23 +0000 (11:36 -0400)
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 <berick@esilibrary.com>
Open-ILS/examples/apache/eg_vhost.conf
Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader.pm
Open-ILS/src/perlmods/lib/OpenILS/WWW/EGWeb.pm
Open-ILS/src/perlmods/lib/OpenILS/WWW/Redirect.pm

index e0571c8..48a259c 100644 (file)
@@ -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
 #</LocationMatch>
 
index fe5074e..e5743bc 100644 (file)
@@ -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)
index b6b4ed2..922f7e1 100644 (file)
@@ -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;
index c76cbf3..99667d0 100644 (file)
@@ -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";
-       <html>
-               <head>
-                       <meta HTTP-EQUIV='Refresh' CONTENT="0; URL=$url"/> 
-                       <style  TYPE="text/css">
-                               .loading_div {
-                                       text-align:center;
-                                       margin-top:30px;
-                               font-weight:bold;
-                                               background: lightgrey;
-                                       color:black;
-                                       width:100%;
-                               }
-                       </style>
-               </head>
-               <body>
-                       <br/><br/>
-                       <div class="loading_div">
-                               <h4>Loading...</h4>
-                       </div>
-                       <br/><br/>
-                       <center><img src='/opac/images/main_logo.jpg'/></center>
-               </body>
-       </html>
-       HTML
-
-       return Apache2::Const::OK;
-}
-
-
 1;