]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/WWW/Redirect.pm
Make Evergreen Perl modules installable via Module::Build to match OpenSRF
[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
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
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';
63
64         my $hostname = $cgi->server_name();
65         my $port                = $cgi->server_port();
66
67         my $proto = "http";
68         if($cgi->https) { $proto = "https"; }
69
70         my $url = "$proto://$hostname:$port/opac/$locale/skin/$skin/xml/index.xml";
71
72         my $path = $apache_obj->path_info();
73
74         $logger->debug("Apache client connecting from $user_ip");
75
76         if(my $shortname = redirect_libs($user_ip)) {
77
78                 $logger->info("Apache redirecting $user_ip to $shortname");
79                 my $session = OpenSRF::AppSession->create("open-ils.actor");
80
81                 my $org = $session->request(
82             'open-ils.actor.org_unit.retrieve_by_shortname',
83                          $shortname)->gather(1);
84
85                 if($org) { 
86             $url .= "?ol=" . $org->id; 
87             $url .= "&d=$depth" if defined $depth;
88         }
89         }
90
91         print "Location: $url\n\n"; 
92         return Apache2::Const::REDIRECT;
93
94         return print_page($url);
95 }
96
97 sub redirect_libs {
98         my $source_ip = shift;
99         my $aton_binary = inet_aton( $source_ip );
100
101     return 0 unless $aton_binary;
102
103         # do this the linear way for now...
104         for my $shortname (keys %$lib_ips_hash) {
105
106         for my $block (@{$lib_ips_hash->{$shortname}}) {
107
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 ) {
114                     return $shortname;
115                 }
116             }
117         }
118         }
119         return 0;
120 }
121
122
123 sub print_page {
124
125         my $url = shift;
126
127         print "Content-type: text/html; charset=utf-8\n\n";
128         print <<"       HTML";
129         <html>
130                 <head>
131                         <meta HTTP-EQUIV='Refresh' CONTENT="0; URL=$url"/> 
132                         <style  TYPE="text/css">
133                                 .loading_div {
134                                         text-align:center;
135                                         margin-top:30px;
136                                 font-weight:bold;
137                                                 background: lightgrey;
138                                         color:black;
139                                         width:100%;
140                                 }
141                         </style>
142                 </head>
143                 <body>
144                         <br/><br/>
145                         <div class="loading_div">
146                                 <h4>Loading...</h4>
147                         </div>
148                         <br/><br/>
149                         <center><img src='/opac/images/main_logo.jpg'/></center>
150                 </body>
151         </html>
152         HTML
153
154         return Apache2::Const::OK;
155 }
156
157
158 1;