File Coverage

blib/lib/Labyrinth/IPAddr.pm
Criterion Covered Total %
statement 16 18 88.8
branch n/a
condition n/a
subroutine 6 6 100.0
pod n/a
total 22 24 91.6


line stmt bran cond sub pod time code
1             package Labyrinth::IPAddr;
2              
3 3     3   3903 use warnings;
  3         3  
  3         94  
4 3     3   12 use strict;
  3         4  
  3         78  
5              
6 3     3   10 use vars qw($VERSION @ISA %EXPORT_TAGS @EXPORT @EXPORT_OK);
  3         3  
  3         226  
7             $VERSION = '5.31';
8              
9             =head1 NAME
10              
11             Labyrinth::IPAddr - IP Address Functions for Labyrinth
12              
13             =head1 SYNOPSIS
14              
15             use Labyrinth::IPAddr;
16              
17             CheckIP();
18             BlockIP($who,$ipaddr);
19             AllowIP($who,$ipaddr);
20              
21             =head1 DESCRIPTION
22              
23             The IPAddr package contains generic functions used for verifying known IP
24             addresses. Used to allow known safe address to use the site without hindrance
25             and to refuse access to spammers.
26              
27             Eventually this may be rewritten as a memcached stand-alone application, to be
28             used across multiple sites.
29              
30             =head1 EXPORT
31              
32             CheckIP
33             BlockIP
34             AllowIP
35              
36             =cut
37              
38             # -------------------------------------
39             # Constants
40              
41 3     3   15 use constant BLOCK => 1;
  3         4  
  3         187  
42 3     3   11 use constant ALLOW => 2;
  3         3  
  3         157  
43              
44             # -------------------------------------
45             # Export Details
46              
47             require Exporter;
48             @ISA = qw(Exporter);
49             @EXPORT = ( qw( CheckIP BlockIP AllowIP BLOCK ALLOW) );
50              
51             # -------------------------------------
52             # Library Modules
53              
54 3     3   414 use Labyrinth::Globals;
  0            
  0            
55             use Labyrinth::DBUtils;
56             use Labyrinth::Variables;
57              
58             use JSON::XS;
59             use URI::Escape;
60             use WWW::Mechanize;
61              
62             # -------------------------------------
63             # The Subs
64              
65             =head1 FUNCTIONS
66              
67             =over 4
68              
69             =item CheckIP
70              
71             Checks whether the current request sender IP address is know, and if so returns
72             the classification. Return codes are:
73              
74             0 - Unknown
75             1 - Blocked
76             2 - Allowed
77              
78             =cut
79              
80             sub CheckIP {
81             if($settings{blockurl}) {
82             my $res = _request($settings{blockurl},'check',$settings{ipaddr});
83             return $res && $res->{success} ? $res->{result} : 0;
84             }
85              
86             my @rows = $dbi->GetQuery('hash','FindIPAddress',$settings{ipaddr});
87             return @rows ? $rows[0]->{type} : 0;
88             }
89              
90              
91             =item BlockIP
92              
93             Block current request sender IP address.
94              
95             =cut
96              
97             sub BlockIP {
98             my $who = shift || 'UNKNOWN';
99             my $ipaddr = shift || return;
100              
101             if($settings{blockurl}) {
102             my $res = _request($settings{blockurl},'block',$ipaddr,$who);
103             return $res && $res->{success} ? $res->{result} : 0;
104             }
105              
106             if(my @rows = $dbi->GetQuery('array','FindIPAddress',$ipaddr)) {
107             $dbi->DoQuery('SaveIPAddress',$who,1,$ipaddr);
108             } else {
109             $dbi->DoQuery('AddIPAddress',$who,1,$ipaddr);
110             }
111              
112             return 1;
113             }
114              
115             =item AllowIP
116              
117             Allow current request sender IP address.
118              
119             =cut
120              
121             sub AllowIP {
122             my $who = shift || 'UNKNOWN';
123             my $ipaddr = shift || return;
124              
125             if($settings{blockurl}) {
126             my $res = _request($settings{blockurl},'allow',$ipaddr,$who);
127             return $res && $res->{success} ? $res->{result} : 0;
128             }
129              
130             if(my @rows = $dbi->GetQuery('array','FindIPAddress',$ipaddr)) {
131             $dbi->DoQuery('SaveIPAddress',$who,2,$ipaddr);
132             } else {
133             $dbi->DoQuery('AddIPAddress',$who,2,$ipaddr);
134             }
135              
136             return 1;
137             }
138              
139             sub _request {
140             my $url = shift;
141             $url .= '/' . join('/', map { uri_escape_utf8($_) } @_);
142              
143             my $mech = WWW::Mechanize->new();
144             $mech->get($url);
145             if($mech->success()) {
146             my $json = $mech->content();
147             my $data = decode_json($json);
148             return $data;
149             }
150              
151             return;
152             }
153              
154             1;
155              
156             __END__