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