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/;
16 use vars '$lib_ips_hash';
19 my $bootstrap_config_file;
21 my( $self, $config ) = @_;
22 $bootstrap_config_file = $config;
26 OpenSRF::System->bootstrap_client( config_file => $bootstrap_config_file );
33 if( open(F, $ips_file) ) {
35 while( my $data = <F> ) {
38 my( $shortname, $ip1, $ip2 ) = split(/\s+/, $data);
39 next unless ($shortname and $ip1 and $ip2);
41 $lib_ips_hash->{$shortname} = [] unless $lib_ips_hash->{$shortname};
42 push( @{$lib_ips_hash->{$shortname}}, [ $ip1, $ip2 ] );
48 $logger->error("Unable to open lib IP redirector file $ips_file");
55 my $user_ip = $ENV{REMOTE_ADDR};
56 my $apache_obj = shift;
57 my $cgi = CGI->new( $apache_obj );
60 my $skin = $apache_obj->dir_config('OILSRedirectSkin') || 'default';
61 my $depth = $apache_obj->dir_config('OILSRedirectDepth');
62 my $locale = $apache_obj->dir_config('OILSRedirectLocale') || 'en-US';
64 my $hostname = $cgi->server_name();
65 my $port = $cgi->server_port();
68 if($cgi->https) { $proto = "https"; }
70 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 if(my $shortname = redirect_libs($user_ip)) {
78 $logger->info("Apache redirecting $user_ip to $shortname");
79 my $session = OpenSRF::AppSession->create("open-ils.actor");
81 my $org = $session->request(
82 'open-ils.actor.org_unit.retrieve_by_shortname',
83 $shortname)->gather(1);
86 $url .= "?ol=" . $org->id;
87 $url .= "&d=$depth" if defined $depth;
91 print "Location: $url\n\n";
92 return Apache2::Const::REDIRECT;
94 return print_page($url);
98 my $source_ip = shift;
99 my $aton_binary = inet_aton( $source_ip );
101 return 0 unless $aton_binary;
103 # do this the linear way for now...
104 for my $shortname (keys %$lib_ips_hash) {
106 for my $block (@{$lib_ips_hash->{$shortname}}) {
108 if(defined($block->[0]) && defined($block->[1]) ) {
109 my $start_binary = inet_aton( $block->[0] );
110 my $end_binary = inet_aton( $block->[1] );
111 next unless( $start_binary and $end_binary );
112 if( $start_binary le $aton_binary and
113 $end_binary ge $aton_binary ) {
127 print "Content-type: text/html; charset=utf-8\n\n";
131 <meta HTTP-EQUIV='Refresh' CONTENT="0; URL=$url"/>
132 <style TYPE="text/css">
137 background: lightgrey;
145 <div class="loading_div">
149 <center><img src='/opac/images/main_logo.jpg'/></center>
154 return Apache2::Const::OK;