]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/WWW/Redirect.pm
add status as a public note in exporter
[Evergreen.git] / Open-ILS / src / perlmods / 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
16 use vars '$lib_ips_hash';
17 my $lib_ips_hash;
18
19 my $bootstrap_config_file;
20 sub import {
21         my( $self, $config ) = @_;
22         $bootstrap_config_file = $config;
23 }
24
25 sub init {
26         OpenSRF::System->bootstrap_client( config_file => $bootstrap_config_file );
27 }
28
29 sub parse_ips_file {
30     my $class = shift;
31     my $ips_file = shift;
32
33     if( open(F, $ips_file) ) {
34
35        while( my $data = <F> ) {
36          chomp($data);
37
38          my( $shortname, $ip1, $ip2 ) = split(/\s+/, $data);
39          next unless ($shortname and $ip1 and $ip2);
40
41          $lib_ips_hash->{$shortname} = [] unless $lib_ips_hash->{$shortname};
42          push( @{$lib_ips_hash->{$shortname}}, [ $ip1, $ip2 ] );
43        }
44
45        close(F);
46
47     } else {
48         $logger->error("Unable to open lib IP redirector file $ips_file");
49     }
50 }
51
52
53 sub handler {
54
55         my $user_ip = $ENV{REMOTE_ADDR};
56         my $apache_obj = shift;
57         my $cgi = CGI->new( $apache_obj );
58
59         my $hostname = $cgi->server_name();
60         my $port                = $cgi->server_port();
61
62         my $proto = "http";
63         if($cgi->https) { $proto = "https"; }
64
65         my $url = "$proto://$hostname:$port/opac/en-US/skin/default/xml/index.xml";
66
67         my $path = $apache_obj->path_info();
68
69         $logger->debug("Apache client connecting from $user_ip");
70
71         if(my $shortname = redirect_libs($user_ip)) {
72
73                 $logger->info("Apache redirecting $user_ip to $shortname");
74                 my $session = OpenSRF::AppSession->create("open-ils.storage");
75
76                 my $org = $session->request(
77                         "open-ils.storage.direct.actor.org_unit.search.shortname",
78                          $shortname)->gather(1);
79
80                 if($org) { $url .= "?ol=" . $org->id; }
81         }
82
83         print "Location: $url\n\n"; 
84         return Apache2::Const::REDIRECT;
85
86         return print_page($url);
87 }
88
89 sub redirect_libs {
90         my $source_ip = shift;
91         my $aton_binary = inet_aton( $source_ip );
92
93     return 0 unless $aton_binary;
94
95         # do this the linear way for now...
96         for my $shortname (keys %$lib_ips_hash) {
97
98         for my $block (@{$lib_ips_hash->{$shortname}}) {
99
100             if(defined($block->[0]) && defined($block->[1]) ) {
101                 my $start_binary        = inet_aton( $block->[0] );
102                 my $end_binary          = inet_aton( $block->[1] );
103                 next unless( $start_binary and $end_binary );
104                 if( $start_binary le $aton_binary and
105                         $end_binary ge $aton_binary ) {
106                     return $shortname;
107                 }
108             }
109         }
110         }
111         return 0;
112 }
113
114
115 sub print_page {
116
117         my $url = shift;
118
119         print "Content-type: text/html; charset=utf-8\n\n";
120         print <<"       HTML";
121         <html>
122                 <head>
123                         <meta HTTP-EQUIV='Refresh' CONTENT="0; URL=$url"/> 
124                         <style  TYPE="text/css">
125                                 .loading_div {
126                                         text-align:center;
127                                         margin-top:30px;
128                                 font-weight:bold;
129                                                 background: lightgrey;
130                                         color:black;
131                                         width:100%;
132                                 }
133                         </style>
134                 </head>
135                 <body>
136                         <br/><br/>
137                         <div class="loading_div">
138                                 <h4>Loading...</h4>
139                         </div>
140                         <br/><br/>
141                         <center><img src='/opac/images/main_logo.jpg'/></center>
142                 </body>
143         </html>
144         HTML
145
146         return Apache2::Const::OK;
147 }
148
149
150 1;