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");
56 my $user_ip = $ENV{REMOTE_ADDR};
57 my $apache_obj = shift;
58 my $cgi = CGI->new( $apache_obj );
61 my $skin = $apache_obj->dir_config('OILSRedirectSkin') || 'default';
62 my $depth = $apache_obj->dir_config('OILSRedirectDepth');
63 my $locale = $apache_obj->dir_config('OILSRedirectLocale') || 'en-US';
65 my $hostname = $cgi->server_name();
66 my $port = $cgi->server_port();
69 if($cgi->https) { $proto = "https"; }
71 my $url = "$proto://$hostname:$port/opac/$locale/skin/$skin/xml/index.xml";
72 my $path = $apache_obj->path_info();
74 $logger->debug("Apache client connecting from $user_ip");
76 my ($shortname, $nskin, $nhostname) = redirect_libs($user_ip);
79 if ($nskin =~ m/[^\s]/) { $skin = $nskin; }
80 if ($nhostname =~ m/[^\s]/) { $hostname = $nhostname; }
82 $logger->info("Apache redirecting $user_ip to $shortname with skin $skin and host $hostname");
83 my $session = OpenSRF::AppSession->create("open-ils.actor");
85 $url = "$proto://$hostname:$port/opac/$locale/skin/$skin/xml/index.xml";
87 my $org = $session->request(
88 'open-ils.actor.org_unit.retrieve_by_shortname',
89 $shortname)->gather(1);
92 $url .= "?ol=" . $org->id;
93 $url .= "&d=$depth" if defined $depth;
97 print "Location: $url\n\n";
98 return Apache2::Const::REDIRECT;
100 return print_page($url);
104 my $source_ip = new Net::IP (shift) or return 0;
106 # do this the linear way for now...
107 for my $shortname (keys %$lib_ips_hash) {
109 for my $block (@{$lib_ips_hash->{$shortname}}) {
111 $logger->debug("Checking whether " . $source_ip->ip() . " is in the range " . $block->[0] . " to " . $block->[1]);
112 if(defined($block->[0]) && defined($block->[1]) ) {
113 my $range = new Net::IP( $block->[0] . ' - ' . $block->[1] );
114 if( $source_ip->overlaps($range)==$IP_A_IN_B_OVERLAP ||
115 $source_ip->overlaps($range)==$IP_IDENTICAL ) {
116 return ($shortname, $block->[2], $block->[3]);
129 print "Content-type: text/html; charset=utf-8\n\n";
133 <meta HTTP-EQUIV='Refresh' CONTENT="0; URL=$url"/>
134 <style TYPE="text/css">
139 background: lightgrey;
147 <div class="loading_div">
151 <center><img src='/opac/images/main_logo.jpg'/></center>
156 return Apache2::Const::OK;