HERE docs etc.
[MyWeb.git] / Web.pm
diff --git a/Web.pm b/Web.pm
index 60f5c72..b166e76 100644 (file)
--- a/Web.pm
+++ b/Web.pm
@@ -26,7 +26,7 @@ use warnings;
 use lib qw(/home/short/lib/perl5/site_perl/5.6.0/i386-linux /home/short/lib/perl5/site_perl/5.6.0 /home/short/lib/perl5/site_perl/i386-linux /home/short/lib/perl5/site_perl /home/short/lib/perl5/5.6.0/i386-linux /home/short/lib/perl5/5.6.0 /home/short/lib/perl5/i386-linux /home/short/lib/perl5);
 
 use Exporter;
-our @EXPORT=qw(&require &a_href &a_href_cz &vskip &img);
+our @EXPORT=qw(&require &a_href &a_href_cz &vskip &img $W);
 our @ISA=qw(Exporter);
 
 use WebConfig; # for %WebConfig
@@ -37,6 +37,7 @@ use Carp qw(cluck confess);
 use URI::Escape;
 require HTTP::BrowserDetect;
 require HTTP::Negotiate;
+require Geo::IP;
 
 
 # Undo 'www/engine/httpd-restart' as it may use obsolete Perl for 'mod_perl'
@@ -44,7 +45,7 @@ delete $ENV{"PERLLIB"};
 delete $ENV{"LD_LIBRARY_PATH"};
 
 
-my $W;
+our $W;
                # $W->{"title"}
                # $W->{"head"}
                # $W->{"head_css"}
@@ -260,7 +261,7 @@ my($url,$contents,%args)=@_;
        my $urlent=CGI::escapeHTML($url);
           if ($url eq $urlent)
                { $r.=$url; }
-       elsif ($url!~m#^[a-z]+://#)     # $url is our resource
+       elsif (url_is_local $url)
                { $r.=$urlent; }
        elsif (defined $W->{"have_ent"} && !$W->{"have_ent"})   # non-ent client
                { $r.=$url; }
@@ -279,9 +280,22 @@ my($url,$contents,%args)=@_;
        return $r;
 }
 
+sub remote_ip ()
+{
+       # Do not: PerlModule                 Apache::ForwardedFor
+       #         PerlPostReadRequestHandler Apache::ForwardedFor
+       # As 'Apache::ForwardedFor' takes the first of $ENV{"HTTP_X_FORWARDED_FOR"}
+       # while the contents is '127.0.0.1, 213.220.195.171' if client has its own proxy.
+       # We must take the last item ourselves.
+       my $r=$ENV{"HTTP_X_FORWARDED_FOR"} || $W->{"r"}->remote_host();
+       $r=~s/^.*,\s*//;
+       return $r;
+}
+
 sub is_cz ()
 {
-       return $W->{"r"}->get_remote_host()=~/[.]cz$/i;
+print STDERR "IP=".remote_ip()."\n";
+       return "CZ" eq Geo::IP->new()->country_code_by_addr(remote_ip());
 }
 
 sub a_href_cz ($$;%)