AMFCarrierDetection.pm untested

filter: Statementstmt 0.0% 0 / 122 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 164 CC 9 SCAR 32.1
CC
9
Cov
0%
loadConfigFile
72.0
handler
6.0
BEGIN
2.0
CRAP 24.7
1 #file:Apache2/AMFCarrierDetection.pm;
2 #--------------------------------
3  
4 #
5 # Created by Idel Fuschini
6 # Date: 01/08/10
7 # Site: http://www.apachemobilefilter.org
8 # Mail: idel.fuschini@gmail.com
9  
10  
11  
12 package Apache2::AMFCarrierDetection;
13
14 use strict;
15 use warnings;
16 use Apache2::AMFCommonLib ();
17
18 use Apache2::RequestRec ();
19 use Apache2::RequestUtil ();
20 use Apache2::SubRequest ();
21 use Apache2::Log;
22 use Apache2::Filter ();
23 use Apache2::Connection ();
24 use APR::Table ();
25 use LWP::Simple;
26 use Apache2::Const -compile => qw(OK REDIRECT DECLINED);
27 use IO::Uncompress::Unzip qw(unzip $UnzipError) ;
28 use constant BUFF_LEN => 1024;
29 use vars qw($VERSION);
30 $VERSION= "4.33";;;
31 #
32 # Define the global environment
33 #
34 my $CommonLib = new Apache2::AMFCommonLib ();
35 my $filecarriernetdownload="none";
36 my %CarrierIP;
37 $CommonLib->printLog("---------------------------------------------------------------------------");
38 $CommonLib->printLog("AMFCarrierDetection Version $VERSION");
39 if ($ENV{AMFMobileHome}) {
40 &loadConfigFile();
41 } else {
42 $CommonLib->printLog("AMFMobileHome not exist. Please set the variable AMFMobileHome into httpd.conf");
43 $CommonLib->printLog("Pre-Requisite: WURFLFilter must be activated");
44 ModPerl::Util::exit();
45 }
46  
47 sub loadConfigFile {
48 my $dummy;
49 my $carrier;
50 my $nation;
51 my $ip;
52 my $row;
53 my @rows;
54 my $carriernetdownload="none";
55 my $carrierurl;
56 my $total_carrier_ip=0;
57 my $ip2;
58
59 $CommonLib->printLog("AMFCarrierDetection: Start read configuration from httpd.conf");
60 if ($ENV{CarrierNetDownload}) {
61 $carriernetdownload=$ENV{CarrierNetDownload};
62 $CommonLib->printLog("CarrierNetDownload is: $carriernetdownload");
63 }
64 if (($ENV{CarrierUrl}) && $carriernetdownload eq 'true') {
65 $carrierurl=$ENV{CarrierUrl};
66 $CommonLib->printLog("CarrierUrl is: $carrierurl");
67 }
68 if ($carriernetdownload eq "true") {
69 $CommonLib->printLog("Start downloading Carrier DB from $carrierurl");
70 my ($content_type, $document_length, $modified_time, $expires, $server) = head($carrierurl);
71 if ($content_type eq "") {
72 $CommonLib->printLog("Couldn't get $carrierurl.");
73 ModPerl::Util::exit();
74 } else {
75 $CommonLib->printLog("The URL for download Carrier DB is correct");
76 $CommonLib->printLog("The size of document is: $document_length bytes");
77 }
78 my $content = get ($carrierurl);
79 $CommonLib->printLog("Finish downloading Carrier DB");
80 if ($content eq "") {
81 $CommonLib->printLog("Couldn't get Data DB from $carrierurl.");
82 ModPerl::Util::exit();
83 }
84 @rows = split(/\n/, $content);
85 my $count=0;
86 foreach $row (@rows){
87 ($carrier,$nation,$ip)=split(/\|/, $row);
88 $CarrierIP{"$ip"}="$carrier|$nation";
89 $total_carrier_ip++;
90 }
91 } else {
92 my $fileCarrier="$ENV{AMFMobileHome}/carrier-data.txt";
93 if (-e "$fileCarrier") {
94 $CommonLib->printLog("Start loading carrier-data.txt");
95 if (open (IN,"$fileCarrier")) {
96 while (<IN>) {
97 #$ip=~s/\n/-/ ;
98 $ip=substr($_,0,10);
99 my $lunghezza= length($_) - 2;
100 my $string=substr($_, 0, $lunghezza);
101 ($carrier,$nation,$ip)=split(/\|/, $string);
102 $CarrierIP{$ip}="$carrier|$nation";
103 $total_carrier_ip++;
104 }
105 close IN;
106 } else {
107 $CommonLib->printLog("Error open file:$fileCarrier");
108 ModPerl::Util::exit();
109 }
110 } else {
111 $CommonLib->printLog("File $fileCarrier not found");
112 ModPerl::Util::exit();
113 }
114 }
115 $CommonLib->printLog("Total of Carrier IP: $total_carrier_ip");
116 $CommonLib->printLog("Finish loading parameter");
117 }
118 sub handler {
119 my $f = shift;
120 my $return_value=Apache2::Const::DECLINED;
121 my $c = $f->connection;
122 my $remote_ip=$c->remote_ip();
123 my $nation="none";
124 my $carrier="none";
125 if ($CarrierIP{"$remote_ip"}) {
126 ($nation, $carrier) = split(/\|/, $CarrierIP{"$remote_ip"});
127 }
128 $f->subprocess_env("AMF_CARRIER_NAME" => $carrier);
129 $f->subprocess_env("AMF_CARRIER_NATION" => $nation);
130 return $return_value;
131 }
132  
133 1;
134  
135
136 =head1 NAME
137  
138 Apache2::AMFCarrierDetection - This module has the scope to identify by ip address the carrier and the nation.
139  
140 =head1 DESCRIPTION
141  
142 This module has the scope to identify by ip address the carrier and the nation.
143
144 NOTE: this software need carrier-data.txt you can download it directly from this site: http://www.andymoore.info/carrier-data.txt or you can set the filter to download it directly.
145  
146 =head1 AMF PROJECT SITE
147  
148 http://www.apachemobilefilter.org
149  
150 =head1 DOCUMENTATION
151  
152 http://wiki.apachemobilefilter.org
153  
154 Perl Module Documentation: http://wiki.apachemobilefilter.org/index.php/AMFCarrierDetection
155  
156 =head1 AUTHOR
157  
158 Idel Fuschini (idel.fuschini [at] gmail [dot] com)
159  
160 =head1 COPYRIGHT
161  
162 You may distribute under the terms of either the GNU General Public
163 License or the Artistic License, as specified in the Perl README file.
164  
165 =cut