]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/WWW/Redirect.pm
Repaired merge conflicts resuling from ttopac-move-templates
[working/Evergreen.git] / Open-ILS / src / perlmods / lib / OpenILS / WWW / Redirect.pm
1 package OpenILS::WWW::Redirect;
2 use strict; use warnings;
3
4 use Socket;
5 use Apache2::Log;
6 use Apache2::Const -compile => qw(OK REDIRECT :log);
7 use APR::Const    -compile => qw(:error SUCCESS);
8 use Apache2::RequestRec ();
9 use Apache2::RequestIO ();
10 use CGI ();
11
12 use OpenSRF::AppSession;
13 use OpenSRF::System;
14 use OpenSRF::Utils::Logger qw/$logger/;
15 use Net::IP;
16
17 use vars '$lib_ips_hash';
18 my $lib_ips_hash;
19
20 my $bootstrap_config_file;
21 sub import {
22         my( $self, $config ) = @_;
23         $bootstrap_config_file = $config;
24 }
25
26 sub init {
27         OpenSRF::System->bootstrap_client( config_file => $bootstrap_config_file );
28 }
29
30 sub parse_ips_file {
31     my $class = shift;
32     my $ips_file = shift;
33
34     if( open(F, $ips_file) ) {
35
36        while( my $data = <F> ) {
37          chomp($data);
38
39          my ($shortname, $ip1, $ip2, $skin, $domain) = split(/\s+/, $data);
40          next unless ($shortname and $ip1 and $ip2);
41
42          $lib_ips_hash->{$shortname} = [] unless $lib_ips_hash->{$shortname};
43          push( @{$lib_ips_hash->{$shortname}}, [ $ip1, $ip2, $skin, $domain ] );
44        }
45
46        close(F);
47
48     } else {
49         $logger->error("Unable to open lib IP redirector file $ips_file");
50     }
51 }
52
53
54 my %org_cache;
55 sub handler {
56         my $apache = shift;
57
58         my $cgi = CGI->new( $apache );
59         my $port = $cgi->server_port();
60         my $hostname = $cgi->server_name();
61     my $proto = ($cgi->https) ? 'https' : 'http';
62         my $user_ip = $ENV{REMOTE_ADDR};
63
64     # Apache config values
65         my $skin = $apache->dir_config('OILSRedirectSkin') || 'default';
66         my $depth = $apache->dir_config('OILSRedirectDepth');
67         my $locale = $apache->dir_config('OILSRedirectLocale') || 'en-US';
68     my $use_tt = ($apache->dir_config('OILSRedirectTpac') || '') =~ /true/i;
69     my $orig_loc;
70
71     $apache->log->debug("Redirector sees client frim $user_ip");
72
73     # parse the IP file
74         my ($shortname, $nskin, $nhostname) = redirect_libs($user_ip);
75
76         if ($shortname) { # we have a config
77
78         # Read any override vars from the ips txt file
79                 if ($nskin =~ m/[^\s]/) { $skin = $nskin; }
80                 if ($nhostname =~ m/[^\s]/) { $hostname = $nhostname; }
81
82         if($org_cache{$shortname}) {
83             $orig_loc = $org_cache{$shortname};
84
85         } else {
86
87                     my $session = OpenSRF::AppSession->create("open-ils.actor");
88                     my $org = $session->request(
89                 'open-ils.actor.org_unit.retrieve_by_shortname',
90                             $shortname)->gather(1);
91
92             $org_cache{$shortname} = $orig_loc = $org->id if $org;
93         }
94         }
95
96     my $url = "$proto://$hostname:$port";
97
98     if($use_tt) {
99
100         $url .= "/eg/opac/home";
101         $url .= "?orig_loc=$orig_loc" if $orig_loc;
102
103 =head potential locale/skin implementation
104         if($locale ne 'en-US') {
105             $apache->headers_out->add(
106                 "Set-Cookie" => $cgi->cookie(
107                     -name => "oils:locale", # see EGWeb.pm
108                     -path => "/eg",
109                     -value => $locale,
110                     -expires => undef
111                 )
112             );
113         }
114
115         if($skin ne 'default') {
116             $apache->headers_out->add(
117                 "Set-Cookie" => $cgi->cookie(
118                     -name => "oils:skin", # see EGWeb.pm
119                     -path => "/eg",
120                     -value => $skin,
121                     -expires => undef
122                 )
123             );
124         }
125 =cut
126
127     } else {
128         $url .= "/opac/$locale/skin/$skin/xml/index.xml";
129         if($orig_loc) {
130             $url .= "?ol=" . $orig_loc;
131             $url .= "&d=$depth" if defined $depth;
132         }
133     }
134
135     $logger->info("Apache redirecting $user_ip to $url");
136     $apache->headers_out->add('Location' => "$url");
137         return Apache2::Const::REDIRECT;
138 }
139
140 sub redirect_libs {
141         my $source_ip = new Net::IP (shift) or return 0;
142
143         # do this the linear way for now...
144         for my $shortname (keys %$lib_ips_hash) {
145
146         for my $block (@{$lib_ips_hash->{$shortname}}) {
147
148             $logger->debug("Checking whether " . $source_ip->ip() . " is in the range " . $block->[0] . " to " . $block->[1]);
149             if(defined($block->[0]) && defined($block->[1]) ) {
150                 my $range = new Net::IP( $block->[0] . ' - ' . $block->[1] );
151                 if( $source_ip->overlaps($range)==$IP_A_IN_B_OVERLAP ||
152                     $source_ip->overlaps($range)==$IP_IDENTICAL ) {
153                     return ($shortname, $block->[2] || '', $block->[3] || '');
154                 }
155             }
156         }
157         }
158         return 0;
159 }
160
161 1;