| 1 | #file:Apache2/AMFCarrierDetection.pm; | ||
| 2 | #-------------------------------- | ||
| 3 | |||
| 4 | # | ||
| 5 | # Created by Idel Fuschini | ||
| 6 | # Date: 15/12/09 | ||
| 7 | # Site: http://www.idelfuschini.it | ||
| 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= "2.21"; | ||
| 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{MOBILE_HOME}) { | ||
| 40 | &loadConfigFile(); | ||
| 41 | } else { | ||
| 42 | $CommonLib->printLog("MOBILE_HOME not exist. Please set the variable MOBILE_HOME 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{MOBILE_HOME}/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 | =head1 NAME | ||
| 135 | |||
| 136 | Apache2::AMFCarrierDetection - This module has the scope to identify by ip address the carrier and the nation. | ||
| 137 | |||
| 138 | |||
| 139 | =head1 COREQUISITES | ||
| 140 | |||
| 141 | Apache2::RequestRec | ||
| 142 | |||
| 143 | Apache2::RequestUtil | ||
| 144 | |||
| 145 | Apache2::SubRequest | ||
| 146 | |||
| 147 | Apache2::Connection | ||
| 148 | |||
| 149 | Apache2::Log | ||
| 150 | |||
| 151 | Apache2::Filter | ||
| 152 | |||
| 153 | APR::Table | ||
| 154 | |||
| 155 | LWP::Simple | ||
| 156 | |||
| 157 | Apache2::Const | ||
| 158 | |||
| 159 | |||
| 160 | =head1 DESCRIPTION | ||
| 161 | |||
| 162 | This module has the scope to identify by ip address the carrier and the nation. | ||
| 163 | |||
| 164 | To work AMFSwitcher has need WURFLFilter configured. | ||
| 165 | |||
| 166 | For more details: http://www.idelfuschini.it/apache-mobile-filter-v2x.html | ||
| 167 | |||
| 168 | An example of how to set the httpd.conf is below: | ||
| 169 | |||
| 170 | =over 4 | ||
| 171 | |||
| 172 | =item C<PerlSetEnv MOBILE_HOME server_root/MobileFilter> | ||
| 173 | |||
| 174 | This indicate to the filter where you want to redirect the specific family of devices: | ||
| 175 | |||
| 176 | =item C<PerlSetEnv CarrierNetDownload true #optional> | ||
| 177 | |||
| 178 | =item C<PerlSetEnv Carrier http://www.andymoore.info/carrier-data.txt> | ||
| 179 | |||
| 180 | |||
| 181 | =back | ||
| 182 | |||
| 183 | 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. | ||
| 184 | |||
| 185 | =head1 SEE ALSO | ||
| 186 | |||
| 187 | For more details: http://www.idelfuschini.it/apache-mobile-filter-v2x.html | ||
| 188 | |||
| 189 | Mobile Demo page of the filter: http://apachemobilefilter.nogoogle.it (thanks Ivan alias sigmund) | ||
| 190 | |||
| 191 | Demo page of the filter: http://apachemobilefilter.nogoogle.it/php_test.php (thanks Ivan alias sigmund) | ||
| 192 | |||
| 193 | =head1 AUTHOR | ||
| 194 | |||
| 195 | Idel Fuschini (idel.fuschini [at] gmail [dot] com) | ||
| 196 | |||
| 197 | =cut |