]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/WWW/Redirect.pm
module for redirecting from / to /opac/?location=x based on source ip of lib
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / WWW / Redirect.pm
1 package OpenILS::WWW::Redirect;
2 use strict; use warnings;
3 use Socket;
4
5 use Apache2 ();
6 use Apache::Log;
7 use Apache::Const -compile => qw(OK REDIRECT :log);
8 use APR::Const    -compile => qw(:error SUCCESS);
9 use Template qw(:template);
10 use Apache::RequestRec ();
11 use Apache::RequestIO ();
12 use CGI ();
13
14 use OpenSRF::AppSession;
15 use OpenSRF::System;
16 use OpenILS::Utils::Fieldmapper;
17
18 use vars '$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
31 sub handler {
32
33         my $user_ip = $ENV{REMOTE_ADDR};
34         my $apache_obj = shift;
35         my $cgi = CGI->new( $apache_obj );
36
37         my $hostname = $cgi->server_name();
38         my $port                = $cgi->server_port();
39
40         my $proto = "http";
41         if($cgi->https) { $proto = "https"; }
42
43         my $url = "$proto://$hostname:$port/opac/";
44
45         my $path = $apache_obj->path_info();
46
47         warn "Client connecting from $user_ip\n";
48
49         if( my $lib_data = redirect_libs( $user_ip ) ) {
50                 my $region = $lib_data->[0];
51                 my $library = $lib_data->[1];
52
53                 warn "Will redirect to $region / $library\n";
54                 my $session = OpenSRF::AppSession->create("open-ils.storage");
55                 my $shortname = "$region-$library";
56
57                 my $org = $session->request(
58                         "open-ils.storage.direct.actor.org_unit.search.shortname",
59                          $shortname)->gather(1);
60
61                 if($org) { $url .= "?location=" . $org->id; }
62
63         }
64
65 #       print "Location: $url\n\n"; 
66 #       return Apache::REDIRECT;
67
68         return print_page($url);
69 }
70
71 sub redirect_libs {
72         my $source_ip = shift;
73         my $aton_binary = inet_aton( $source_ip );
74
75         if( ! $aton_binary ) { return 0; }
76
77         # do this the linear way for now...
78         for my $reg (keys %$lib_ips_hash) {
79                 for my $lib( keys %{$lib_ips_hash->{$reg}} ) {
80                         for my $ip_block (@{$lib_ips_hash->{$reg}->{$lib}}) {
81
82                                 if(defined($ip_block->[0]) && defined($ip_block->[1]) ) {
83                                         my $start_binary        = inet_aton( $ip_block->[0] );
84                                         my $end_binary          = inet_aton( $ip_block->[1] );
85                                         unless( $start_binary and $end_binary ) { next; }
86                                         if( $start_binary le $aton_binary and
87                                                         $end_binary ge $aton_binary ) {
88                                                 return [ $reg, $lib ];
89                                         }
90                                 }
91
92                         }
93                 }
94         }
95         return 0;
96 }
97
98
99 sub print_page {
100
101         my $url = shift;
102
103         print "Content-type: text/html; charset=utf-8\n\n";
104         print <<"       HTML";
105         <html>
106                 <head>
107                         <meta HTTP-EQUIV='Refresh' CONTENT="0; URL=$url"/> 
108                         <style  TYPE="text/css">
109                                 .loading_div {
110                                         text-align:center;
111                                         margin-top:30px;
112                                 font-weight:bold;
113                                                 background: lightgrey;
114                                         color:black;
115                                         width:100%;
116                                 }
117                         </style>
118                 </head>
119                 <body>
120                         <br/><br/>
121                         <div class="loading_div">
122                                 <h4>Loading...</h4>
123                         </div>
124                         <br/><br/>
125                         <center><img src='/images/main_logo.jpg'/></center>
126                 </body>
127         </html>
128         HTML
129
130         return Apache::OK;
131 }
132
133
134 1;