fixing session timeout bug
[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 qw(: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
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/skin/default/xml/index.xml";
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 Apache2::Const::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
80                 for my $lib( keys %{$lib_ips_hash->{$reg}} ) {
81
82                         for my $block (@{$lib_ips_hash->{$reg}->{$lib}}) {
83
84                                 if(defined($block->[0]) && defined($block->[1]) ) {
85                                         my $start_binary        = inet_aton( $block->[0] );
86                                         my $end_binary          = inet_aton( $block->[1] );
87                                         unless( $start_binary and $end_binary ) { next; }
88                                         if( $start_binary le $aton_binary and
89                                                         $end_binary ge $aton_binary ) {
90                                                 return [ $reg, $lib ];
91                                         }
92                                 }
93                         }
94                 }
95         }
96         return 0;
97 }
98
99
100 sub print_page {
101
102         my $url = shift;
103
104         print "Content-type: text/html; charset=utf-8\n\n";
105         print <<"       HTML";
106         <html>
107                 <head>
108                         <meta HTTP-EQUIV='Refresh' CONTENT="0; URL=$url"/> 
109                         <style  TYPE="text/css">
110                                 .loading_div {
111                                         text-align:center;
112                                         margin-top:30px;
113                                 font-weight:bold;
114                                                 background: lightgrey;
115                                         color:black;
116                                         width:100%;
117                                 }
118                         </style>
119                 </head>
120                 <body>
121                         <br/><br/>
122                         <div class="loading_div">
123                                 <h4>Loading...</h4>
124                         </div>
125                         <br/><br/>
126                         <center><img src='/opac/images/main_logo.jpg'/></center>
127                 </body>
128         </html>
129         HTML
130
131         return Apache2::Const::OK;
132 }
133
134
135 1;