1 package OpenILS::WWW::Redirect;
2 use strict; use warnings;
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 ();
12 use OpenSRF::AppSession;
14 use OpenSRF::Utils::Logger qw/$logger/;
17 use vars '$lib_ips_hash';
20 my $bootstrap_config_file;
22 my( $self, $config ) = @_;
23 $bootstrap_config_file = $config;
27 OpenSRF::System->bootstrap_client( config_file => $bootstrap_config_file );
34 if( open(F, $ips_file) ) {
36 while( my $data = <F> ) {
39 my ($shortname, $ip1, $ip2, $skin, $domain) = split(/\s+/, $data);
40 next unless ($shortname and $ip1 and $ip2);
42 $lib_ips_hash->{$shortname} = [] unless $lib_ips_hash->{$shortname};
43 push( @{$lib_ips_hash->{$shortname}}, [ $ip1, $ip2, $skin, $domain ] );
49 $logger->error("Unable to open lib IP redirector file $ips_file");
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};
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;
71 $apache->log->debug("Redirector sees client frim $user_ip");
74 my ($shortname, $nskin, $nhostname) = redirect_libs($user_ip);
76 if ($shortname) { # we have a config
78 # Read any override vars from the ips txt file
79 if ($nskin =~ m/[^\s]/) { $skin = $nskin; }
80 if ($nhostname =~ m/[^\s]/) { $hostname = $nhostname; }
82 if($org_cache{$shortname}) {
83 $orig_loc = $org_cache{$shortname};
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);
92 $org_cache{$shortname} = $orig_loc = $org->id if $org;
96 my $url = "$proto://$hostname:$port";
100 $url .= "/eg/opac/home";
101 $url .= "?orig_loc=$orig_loc" if $orig_loc;
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
115 if($skin ne 'default') {
116 $apache->headers_out->add(
117 "Set-Cookie" => $cgi->cookie(
118 -name => "oils:skin", # see EGWeb.pm
128 $url .= "/opac/$locale/skin/$skin/xml/index.xml";
130 $url .= "?ol=" . $orig_loc;
131 $url .= "&d=$depth" if defined $depth;
135 $logger->info("Apache redirecting $user_ip to $url");
136 $apache->headers_out->add('Location' => "$url");
137 return Apache2::Const::REDIRECT;
141 my $source_ip = new Net::IP (shift) or return 0;
143 # do this the linear way for now...
144 for my $shortname (keys %$lib_ips_hash) {
146 for my $block (@{$lib_ips_hash->{$shortname}}) {
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] || '');