geo.pl
114 lines of code
1
#!/usr/local/bin/perl
2
3
($<,$>) = (getpwnam('nobody'), getpwnam('nobody')) or die $!;
4
5
binmode(STDIN, ":utf8");
6
binmode(STDOUT, ":utf8");
7
8
# must have's!
9
use strict;
10
use warnings;
11
use CGI::Carp qw(fatalsToBrowser);
12
use DBI;
13
use URI::Escape;
14
use JSON;
15
16
use lib "/var/www/html/Pm";
17
18
use Html qw(pre_html_header header html_end);
19
use Html2 qw(tag);
20
use Bc_chef qw(cookie_get);
21
use Bc_misc qw(get_param referrer get_params_asHash);
22
use Bc_sql qw(
23
              get_constant
24
              sql_execute
25
              user_exists
26
              $QUERY_PAGE
27
              $QUERY_UID
28
              $LOGGEDIN
29
              $SITE_NAME
30
31
              $DB
32
             );
33
34
use Security qw(banned);
35
use Redir qw(error_redir notice_redir);
36
37
my $DEBUG = 0;
38
39
my $output;
40
41
if (not user_exists($LOGGEDIN) or banned($LOGGEDIN)) {
42
  my $msg =  "Access Denied";
43
  if ($DEBUG) { $msg .= " (TEMPLATE.pl)"; }
44
  $output = error_redir("/", $msg);
45
} else {
46
  ############################################################
47
  ### YOUR CONTENT HERE
48
  my $ip = get_param(get_constant("QUERY_IP"));
49
50
  if ($DEBUG) {
51
    $output = pre_html_header();
52
    $output .= header(
53
                     "Your Geo Location - " . $SITE_NAME,
54
                     "?nobg_img=1&nogrid=1",
55
                     0,
56
                     "setTimeout(removeMsg, " . get_constant("REMOVE_MSG_TIMEOUT") . ");",
57
                     "",
58
                     "style=\"padding: 2px;\""
59
                    );
60
  } else {
61
    $output = pre_html_header({type=>"text/plain"});
62
  }
63
64
  my $geoloc_url = "http://ip-api.com/json/$ip";
65
  my $geoloc_curl = `curl -H "Accept: application/json" $geoloc_url`;
66
  my $loc = decode_json($geoloc_curl);
67
68
  if (ref $loc eq "HASH") {
69
    if ($DEBUG) {
70
      foreach my $key (keys %$loc) {
71
        $output .= "\%\$loc{\"$key\"} = $loc->{$key}<br>\n";
72
      }
73
74
      $output .= "<hr>\n";
75
    }
76
77
    if ($loc->{status} ne "fail") {
78
      if ($DEBUG) {
79
        my %location; {
80
          $location{tag} = "div";
81
          $location{class} = "bordered";
82
          $location{innerHTML} = $loc->{city} . ", " . $loc->{country};
83
84
          $output .= tag(\%location);
85
        }
86
      } else {
87
        $output .= $loc->{city} . ", " . $loc->{country};
88
      }
89
    } else {
90
      if ($DEBUG) {
91
        $output .= "No location associated with given IP Address...<br>\n";
92
        $output .= "Perhaps the IP is in a LAN?\n";
93
      } else {
94
        print error_redir(referrer(), "No location associated with given IP Address...");
95
        exit 1;
96
      }
97
    }
98
  } else {
99
    if ($DEBUG) {
100
      $output .= "Error Fetching Geo-Location...\n";
101
    } else {
102
      print error_redir(referrer(), "Error Fetching Geo-Location");
103
      exit 1;
104
    }
105
  }
106
107
  ### END YOUR CONTENT
108
  if ($DEBUG) { $output .= html_end(); }
109
  ############################################################
110
}
111
112
print $output;
113
114
exit 1;