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