Apache2-ApacheMobileFilter
view release on metacpan or search on metacpan
lib/Apache2/AMF51DegreesFilter.pm view on Meta::CPAN
my $null3="";
my $val;
my $capability;
my $r_id;
my $dummy;
#The filter
$CommonLib->printLog("Start read configuration from httpd.conf");
if ($ENV{Degrees51NetDownload}) {
if ($ENV{Degrees51NetDownload} eq 'true' || $ENV{Degrees51NetDownload} eq 'false') {
$Degreesnetdownload=$ENV{Degrees51NetDownload};
$CommonLib->printLog("51DegreesNetDownload is: $Degreesnetdownload");
} else {
$CommonLib->printLog("Error 51DegreesNetDownload parmeter must set to true or false");
ModPerl::Util::exit();
}
}
if ($ENV{Download51DegreesURL}) {
$download51Degreesurl=$ENV{Download51DegreesURL};
$CommonLib->printLog("Download51DegreesURL is: $download51Degreesurl");
}
if ($Degreesnetdownload eq 'true') {
if ($ENV{Key51Degrees}) {
$download51Degreesurl=$download51Degreesurl."?LicenseKeys=".$ENV{Key51Degrees}."&Type=XML&Download=True";
$CommonLib->printLog("Key51Degrees is: $ENV{Key51Degrees}");
} else {
$CommonLib->printLog("Error Key51Degrees parmeter must be setted ");
ModPerl::Util::exit();
}
}
if ($ENV{CapabilityList}) {
my @dummycapability = split(/,/, $ENV{CapabilityList});
$capabilitylist=$ENV{CapabilityList};
foreach $dummy (@dummycapability) {
if ($dummy eq "all") {
$listall="true";
}
$Capability{$dummy}=$dummy;
$CommonLib->printLog("CapabilityList is: $dummy");
}
} else {
$listall="true";
$CommonLib->printLog('CapabilityList not setted so the default value is "all"');
}
if ($ENV{AMFMobileKeys}) {
my @dummyMobileKeys = split(/,/, $ENV{AMFMobileKeys});
foreach $dummy (@dummyMobileKeys) {
$MobileArray{$dummy}='mobile';
}
$CommonLib->printLog("AMFMobileKeys is: $ENV{AMFMobileKeys}");
}
if ($ENV{AMFProductionMode}) {
$cookiecachesystem=$ENV{AMFProductionMode};
$CommonLib->printLog("AMFProductionMode is: $cookiecachesystem");
} else {
$CommonLib->printLog("AMFProductionMode (the CookieCacheSystem is deprecated) is not setted the default value is $cookiecachesystem");
}
if ($ENV{Personal51DegreesFileName}) {
$personal51Degreesurl=$ENV{AMFMobileHome}."/".$ENV{Personal51DegreesFileName};
$CommonLib->printLog("Personal51DegreesFileName is: $ENV{Personal51DegreesFileName}");
}
if ($ENV{RestMode}) {
$restmode=$ENV{RestMode};
$CommonLib->printLog("RestMode is: $restmode");
}
if ($ENV{AMFDeepParse}) {
$deepSearch=$ENV{AMFDeepParse};
$CommonLib->printLog("AMFDeepParse is: $deepSearch");
} else {
$CommonLib->printLog("AMFDeepParse is not setted the default value is $deepSearch");
}
if ($ENV{FullBrowserMobileAccessKey}) {
$mobilenable="$ENV{FullBrowserMobileAccessKey}";
$CommonLib->printLog("FullBrowserMobileAccessKey is: $ENV{FullBrowserMobileAccessKey}");
$CommonLib->printLog("For access the device to fullbrowser set the link: <url>?$mobilenable=true");
}
$CommonLib->printLog("Finish loading parameter");
$CommonLib->printLog("---------------------------------------------------------------------------");
if ($Degreesnetdownload eq "true") {
$CommonLib->printLog("Start process downloading 51Degrees.xml from $download51Degreesurl");
$CommonLib->printLog ("Test the URL");
my ($content_type, $document_length, $modified_time, $expires, $server) = head($download51Degreesurl);
if ($content_type eq "") {
$CommonLib->printLog("Couldn't get $download51Degreesurl.");
$CommonLib->printLog("Take the last version downloaded.");
# ModPerl::Util::exit();
} else {
$CommonLib->printLog("The URL is correct");
$CommonLib->printLog("The size of document 51Degrees file: $document_length bytes");
}
if ($content_type eq 'application/octet-stream') {
$CommonLib->printLog("The file is a zip file.");
$CommonLib->printLog ("Start downloading");
my @dummypairs = split(/\//, $download51Degreesurl);
my ($ext_zip) = $download51Degreesurl =~ /\.(\w+)$/;
my $filezip=$dummypairs[-1];
my $tmp_dir=$ENV{AMFMobileHome};
$filezip="$tmp_dir/$filezip";
my $status = getstore ($download51Degreesurl,$filezip);
my $output="$tmp_dir/51Degrees.xml";
gunzip $filezip => $output
or die "gzip failed: $GunzipError\n";
$CommonLib->printLog("Finish downloading 51Degrees from $download51Degreesurl");
} else {
$CommonLib->printLog("Error download 51Degrees device repository, check the premium keys. AMF try to load the previous version.");
}
}
if (-e "$file51Degrees") {
$CommonLib->printLog("Start loading 51Degrees.xml");
if (open (IN,"$file51Degrees")) {
my $filesize= -s $file51Degrees;
read (IN,my $content,$filesize);
lib/Apache2/AMF51DegreesFilter.pm view on Meta::CPAN
return %ArrayCapFoundToPass;
}
sub IdentifyUAMethod {
my ($UserAgent) = @_;
my $ind=0;
my %ArrayPM;
my $pair;
my $pair2;
my $id_find="";
my $dummy;
my $uagent_toMatch;
my $near_toFind=100;
my $near_toMatch;
my %ArrayUAType=$CommonLib->GetMultipleUa(lc($UserAgent),$deepSearch);
foreach $pair (reverse sort { $a <=> $b } keys %ArrayUAType)
{
my $dummy=$ArrayUAType{$pair};
if ($Array_id{$dummy}) {
if (!$id_find) {
$id_find=$Array_id{$dummy};
}
}
}
return $id_find;
}
sub IdentifyPCUAMethod {
my ($UserAgent) = @_;
my $ind=0;
my $id_find="none";
my $pair;
my $length=0;
foreach $pair (sort keys %PCArray) {
if ($UserAgent =~ m/$pair/ && $id_find eq 'none') {
$id_find=$PCArray{$pair};
}
}
if ($id_find) {}else{$id_find="";};
return $id_find;
}
sub handler {
my $f = shift;
my $capability2;
my $variabile="";
my $user_agent=lc($f->headers_in->{'User-Agent'}|| '');
my $x_user_agent=$f->headers_in->{'X-Device-User-Agent'}|| '';
my $x_operamini_phone_ua=$f->headers_in->{'X-OperaMini-Phone-Ua'}|| '';
my $x_operamini_ua=$f->headers_in->{'X-OperaMini-Ua'}|| '';
my $query_string=$f->args;
my $docroot = $f->document_root();
my $id="";
my $location="none";
my $width_toSearch;
my $type_redirect="internal";
my $return_value;
my $dummy="";
my $variabile2="";
my %ArrayCapFound;
my $controlCookie;
my $query_img="";
$ArrayCapFound{is_transcoder}='false';
$ArrayCapFound{'IsMobile'}='true';
my %ArrayQuery;
my $var;
my $version="";
my $realPCbrowser='none';
if ($user_agent eq "") {
$user_agent="no useragent found";
}
if ($x_user_agent) {
$user_agent=lc($x_user_agent);
}
if ($x_operamini_phone_ua) {
$user_agent=lc($x_operamini_phone_ua);
}
my $cookie = $f->headers_in->{Cookie} || '';
$id=$CommonLib->readCookie($cookie);
my $amfFull=$CommonLib->readCookie_fullB($cookie);
if ($query_string) {
my @vars = split(/&/, $query_string);
foreach $var (sort @vars){
if ($var) {
my ($v,$i) = split(/=/, $var);
$v =~ tr/+/ /;
$v =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
if ($i) {
$i =~ tr/+/ /;
$i =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$i =~ s/<!--(.|\n)*-->//g;
$ArrayQuery{$v}=$i;
}
}
}
if (($ArrayQuery{amf}) && $restmode eq 'true') {
$user_agent=lc($ArrayQuery{amf});
}
if ($ArrayQuery{$mobilenable}) {
$f->err_headers_out->set('Set-Cookie' => "amfFull=false; path=/;");
$amfFull='ok';
}
}
$user_agent=lc($user_agent);
if ($user_agent =~ m/blackberry/i) {
$user_agent=substr($user_agent,index($user_agent,'blackberry'));
}
if ($user_agent =~ m/up.link/i ) {
$user_agent=substr($user_agent,0,index($user_agent,'up.link') - 1);
}
if ($user_agent =~ m/c4_acer_mozilla/i ) {
$user_agent=substr($user_agent,index($user_agent,'mozilla'));
}
($user_agent,$version)=$CommonLib->androidDetection($user_agent);
if ($cacheSystem->restore( '51Degrees-ua', $user_agent )) {
#
# cookie is not empty so I try to read in memory cache on my httpd cache
#
$id=$cacheSystem->restore( '51Degrees-ua', $user_agent );
if ($cacheSystem->restore( '51Degrees-id', $id )) {
#
# I'm here only for old device
#
my @pairs = split(/&/, $cacheSystem->restore( '51Degrees-id', $id ));
my $param_tofound;
my $string_tofound;
foreach $param_tofound (@pairs) {
($string_tofound,$dummy)=split(/=/, $param_tofound);
if ($dummy) {
$ArrayCapFound{$string_tofound}=$dummy;
my $upper2=uc($string_tofound);
$f->subprocess_env("AMF_$upper2" => $ArrayCapFound{$string_tofound});
$f->pnotes($string_tofound => $ArrayCapFound{$string_tofound});
}
}
$id=$ArrayCapFound{id};
if ($ArrayCapFound{realpcbrowser} ne 'none') {
$id=$realPCbrowser;
$ArrayCapFound{'IsMobile'}='false';
}
}
} else {
if ($id eq "") {
if ($user_agent) {
my $pair;
my $lcuser_agent=lc($user_agent);
if (!$id) {$id="";};
if ($id eq "") {
$id=IdentifyUAMethod($user_agent);
}
if ($id eq "") {
$id='generic_web_browser';
} else {
#this check the correct version of Android
if ($version) {
if ($version ne 'nc') {
my $lengthId=length($version);
my $count=0;
while($count<$lengthId) {
my $idToCheck=$id."_sub".substr($version,0,length($version)-$count);
if ($Array_fb{$idToCheck}) {
$id=$idToCheck;
$count=$lengthId;
}
$count++;
}
}
}
}
$cacheSystem->store( '51Degrees-ua', $user_agent, $id);
}
}
if ($id ne "") {
#
# device detected
#
if ($cacheSystem->restore( '51Degrees-id', $id )) {
#
# I'm here only for old device looking in cache
#
my @pairs = split(/&/, $cacheSystem->restore( '51Degrees-id', $id ));
my $param_tofound;
my $string_tofound;
foreach $param_tofound (@pairs) {
($string_tofound,$dummy)=split(/=/, $param_tofound);
$ArrayCapFound{$string_tofound}=$dummy;
my $upper2=uc($string_tofound);
$f->subprocess_env("AMF_$upper2" => $ArrayCapFound{$string_tofound});
$f->pnotes("$string_tofound" => $ArrayCapFound{$string_tofound});
}
$id=$ArrayCapFound{id};
} else {
%ArrayCapFound=FallBack($id);
foreach $capability2 (sort keys %ArrayCapFound) {
$variabile2="$variabile2$capability2=$ArrayCapFound{$capability2}&";
my $upper=uc($capability2);
$f->subprocess_env("AMF_$upper" => $ArrayCapFound{$capability2});
$f->pnotes("$capability2" => $ArrayCapFound{$capability2});
}
$variabile2="id=$id&$variabile2&realpcbrowser=$realPCbrowser";
$f->subprocess_env("AMF_ID" => $id);
$f->pnotes('id' => $id);
$cacheSystem->store( '51Degrees-id', $id, $variabile2 );
$cacheSystem->store( '51Degrees-ua', $user_agent, $id);
}
if ($cookiecachesystem eq "true") {
$f->err_headers_out->set('Set-Cookie' => "amf=$id; path=/;");
}
}
}
if ($ArrayCapFound{'ScreenPixelsWidth'} ne 'Unknown') {
$f->pnotes("max_image_width" => $ArrayCapFound{'ScreenPixelsWidth'});
$f->pnotes("max_image_height" => $ArrayCapFound{'ScreenPixelsHeight'});
}
if ($ArrayCapFound{'IsTablet'}) {
$f->pnotes("is_tablet" => lc($ArrayCapFound{'IsTablet'}));
$f->subprocess_env("AMF_DEVICE_IS_TABLET" => lc($ArrayCapFound{'IsTablet'}));
}
if ($amfFull ne "") {
$f->subprocess_env("AMF_FORCE_TO_DESKTOP" => 'true');
$f->pnotes("amf_force_to_desktop" => 'true');
}
$f->pnotes("amf_device_ismobile" => lc($ArrayCapFound{'IsMobile'}));
$f->subprocess_env("AMF_DEVICE_IS_MOBILE" => lc($ArrayCapFound{'IsMobile'}));
$f->subprocess_env("AMF_VER" => $VERSION);
$f->subprocess_env("AMF_DEGREES51_VER" => $DegreesVersion);
$f->headers_out->set("AMF-Ver"=> $VERSION);
if ($x_operamini_ua) {
$f->subprocess_env("AMF_MOBILE_BROWSER" => $x_operamini_ua);
$f->pnotes("mobile_browser" => $x_operamini_ua);
$f->subprocess_env("AMF_IS_TRANCODER" => 'true');
$f->pnotes("is_transcoder" => 'true');
}
return Apache2::Const::DECLINED;
}
1;
=head1 NAME
Apache2::AMF51DegreesFilter - The module detects the mobile device and passes the 51Degrees capabilities on to the other web application as environment variables
=head1 DESCRIPTION
Module for device detection, the cache is based on file system
h=head1 AMF PROJECT SITE
http://www.apachemobilefilter.org
=head1 DOCUMENTATION
http://wiki.apachemobilefilter.org
Perl Module Documentation: http://wiki.apachemobilefilter.org/index.php/AMF51DegreesFilter
=head1 AUTHOR
Idel Fuschini (idel.fuschini [at] gmail [dot] com)
=head1 COPYRIGHT
You may distribute under the terms of either the GNU General Public
License or the Artistic License, as specified in the Perl README file.
=cut
( run in 0.846 second using v1.01-cache-2.11-cpan-c966e8aa7e8 )