| 1 | #file:Apache2/AMFLiteDetectionFilter.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 | package Apache2::AMFLiteDetectionFilter; | ||
| 12 | |||
| 13 | use strict; | ||
| 14 | use warnings; | ||
| 15 | use MIME::Base64 qw(encode_base64); | ||
| 16 | use Apache2::AMFCommonLib (); | ||
| 17 | use Apache2::RequestRec (); | ||
| 18 | use Apache2::RequestUtil (); | ||
| 19 | use Apache2::SubRequest (); | ||
| 20 | use Apache2::Log; | ||
| 21 | use Apache2::Filter (); | ||
| 22 | use APR::Table (); | ||
| 23 | use LWP::Simple (); | ||
| 24 | use LWP::UserAgent; | ||
| 25 | use Apache2::Const -compile => qw(OK REDIRECT DECLINED); | ||
| 26 | use IO::Uncompress::Unzip qw(unzip $UnzipError) ; | ||
| 27 | use constant BUFF_LEN => 1024; | ||
| 28 | use Cache::FileBackend; | ||
| 29 | |||
| 30 | |||
| 31 | # | ||
| 32 | # Define the global environment | ||
| 33 | # | ||
| 34 | |||
| 35 | use vars qw($VERSION); | ||
| 36 | $VERSION= "4.33"; | ||
| 37 | my $CommonLib = new Apache2::AMFCommonLib (); | ||
| 38 | my %MobileArray;#=$CommonLib->getMobileArray; | ||
| 39 | my %MobileTabletArray; | ||
| 40 | my %MobileTouchArray; | ||
| 41 | my %MobileTVArray; | ||
| 42 | my $cookiecachesystem="false"; | ||
| 43 | my $restmode='false'; | ||
| 44 | my $downloadparam='true'; | ||
| 45 | my $configMobileFile; | ||
| 46 | my $forcetablet='true'; | ||
| 47 | my $configTabletFile; | ||
| 48 | my $configTouchFile; | ||
| 49 | my $configTVFile; | ||
| 50 | my $checkVersion='true'; | ||
| 51 | my $mobilenable="false"; | ||
| 52 | my $hostNames = "github.com"; | ||
| 53 | my @hostName = split(/,/, $hostNames); | ||
| 54 | my $numberOfAvaiableHosts=@hostName; | ||
| 55 | my $correctHost=0; | ||
| 56 | my $correctHostName=$hostName[$correctHost]; | ||
| 57 | my $hostServer="https://".$correctHostName."/ifuschini/amf/blob/master/repository/"; | ||
| 58 | my $urlmobile=$hostServer."litemobiledetection.config"; | ||
| 59 | my $urlTablet=$hostServer."litetabletdetection.config"; | ||
| 60 | my $urlTouch=$hostServer."litetouchdetection.config"; | ||
| 61 | my $urlTv=$hostServer."litetvdetection.config"; | ||
| 62 | my $urlBot=$hostServer."litebotdetection.config"; | ||
| 63 | my $forceBlockDownload="false"; | ||
| 64 | my $ua=LWP::UserAgent->new; | ||
| 65 | $CommonLib->printLog("---------------------------------------------------------------------------"); | ||
| 66 | $CommonLib->printLog("------- APACHE MOBILE FILTER V$VERSION -------"); | ||
| 67 | $CommonLib->printLog("------- support http://groups.google.com/group/amf-device-detection -------"); | ||
| 68 | $CommonLib->printLog("---------------------------------------------------------------------------"); | ||
| 69 | $CommonLib->printLog("----------------- AMF Lite Detection (not DR required) -------------------"); | ||
| 70 | $CommonLib->printLog("---------------------------------------------------------------------------"); | ||
| 71 | $CommonLib->printLog("AMFLiteDetectionFilter module Version $VERSION"); | ||
| 72 | |||
| 73 | if (($ENV{AMFSetProxy}) && $ENV{AMFSetProxy} eq 'true' ) { | ||
| 74 | $CommonLib->printLog("AMFSetHttpProxy is ".$ENV{AMFSetHttpProxy}); | ||
| 75 | my $port=3128; | ||
| 76 | my $default=""; | ||
| 77 | if ($ENV{AMFSetHttpProxyPort}) { | ||
| 78 | $port=$ENV{AMFSetHttpProxyPort}; | ||
| 79 | } else { | ||
| 80 | $default=' (default)'; | ||
| 81 | } | ||
| 82 | $CommonLib->printLog("AMFSetHttpProxyPort is ".$port.$default); | ||
| 83 | $ua->proxy('http','http://'.$ENV{AMFSetHttpProxy}.':'.$port); | ||
| 84 | $ua->proxy('https','http://'.$ENV{AMFSetHttpProxy}.':'.$port); | ||
| 85 | } | ||
| 86 | my $timeoutinsecond=5; | ||
| 87 | my $default=""; | ||
| 88 | if ($ENV{AMFSetGetParameterTimeoOut}) { | ||
| 89 | $timeoutinsecond=$ENV{AMFSetGetParameterTimeoOut}; | ||
| 90 | } else { | ||
| 91 | $default=' (default)'; | ||
| 92 | } | ||
| 93 | $CommonLib->printLog("AMFSetGetParameterTimeoOut is ".$timeoutinsecond.'s'.$default); | ||
| 94 | $ua->timeout($timeoutinsecond); | ||
| 95 | if ($ENV{AMFCheckVersion}) { | ||
| 96 | $checkVersion=$ENV{AMFCheckVersion}; | ||
| 97 | } | ||
| 98 | if ($ENV{AMFMobileHome}) { | ||
| 99 | $configMobileFile="$ENV{AMFMobileHome}/amflitedetection.config"; | ||
| 100 | $configTabletFile="$ENV{AMFMobileHome}/amflitedetection_tablet.config"; | ||
| 101 | $configTouchFile="$ENV{AMFMobileHome}/amflitedetection_touch.config"; | ||
| 102 | $configTVFile="$ENV{AMFMobileHome}/amflitedetection_tv.config"; | ||
| 103 | } else { | ||
| 104 | $CommonLib->printLog("AMFMobileHome not exist. Please set the variable AMFMobileHome into httpd.conf"); | ||
| 105 | ModPerl::Util::exit(); | ||
| 106 | } | ||
| 107 | if ($ENV{AMFProductionMode}) { | ||
| 108 | $cookiecachesystem=$ENV{AMFProductionMode}; | ||
| 109 | $CommonLib->printLog("AMFProductionMode is: $cookiecachesystem"); | ||
| 110 | } else { | ||
| 111 | $CommonLib->printLog("AMFProductionMode is not setted the default value is $cookiecachesystem"); | ||
| 112 | } | ||
| 113 | if ($ENV{AMFMobileKeys}) { | ||
| 114 | my @dummyMobileKeys = split(/,/, $ENV{AMFMobileKeys}); | ||
| 115 | foreach my $dummy (@dummyMobileKeys) { | ||
| 116 | $MobileArray{$dummy}='mobile'; | ||
| 117 | } | ||
| 118 | $CommonLib->printLog("AMFMobileKeys is: $ENV{AMFMobileKeys}"); | ||
| 119 | } | ||
| 120 | if ($ENV{RestMode}) { | ||
| 121 | $restmode=$ENV{RestMode}; | ||
| 122 | $CommonLib->printLog("RestMode is: $restmode"); | ||
| 123 | } | ||
| 124 | if ($ENV{AMFDownloadParam}) { | ||
| 125 | $downloadparam=$ENV{AMFDownloadParam}; | ||
| 126 | $CommonLib->printLog("DownloadAMFParam is: $downloadparam"); | ||
| 127 | } | ||
| 128 | if ($downloadparam eq 'true' && $forceBlockDownload eq "false") { | ||
| 129 | &readMobileParamFromUrl; | ||
| 130 | &readTabletParamFromUrl; | ||
| 131 | &readTouchParamFromUrl; | ||
| 132 | &readTVParamFromUrl; | ||
| 133 | } else { | ||
| 134 | &readMobileParamFromFile; | ||
| 135 | &readTabletParamFromFile; | ||
| 136 | &readTouchParamFromFile; | ||
| 137 | &readTVParamFromFile; | ||
| 138 | } | ||
| 139 | if ($ENV{ForceTabletAsFullBrowser}) { | ||
| 140 | if ($ENV{ForceTabletAsFullBrowser} eq 'true') { | ||
| 141 | $CommonLib->printLog("AMFMobileHome not exist. Please set the variable AMFMobileHome into httpd.conf"); | ||
| 142 | $forcetablet="true"; | ||
| 143 | } else { | ||
| 144 | $forcetablet="false"; | ||
| 145 | } | ||
| 146 | } | ||
| 147 | if ($ENV{FullBrowserMobileAccessKey}) { | ||
| 148 | $mobilenable="$ENV{FullBrowserMobileAccessKey}"; | ||
| 149 | $CommonLib->printLog("FullBrowserMobileAccessKey is: $ENV{FullBrowserMobileAccessKey}"); | ||
| 150 | $CommonLib->printLog("For access the device to fullbrowser set the link: <url>?$mobilenable=true"); | ||
| 151 | } | ||
| 152 | sub readMobileParamFromUrl { | ||
| 153 | $CommonLib->printLog("Read data from ".$urlmobile); | ||
| 154 | |||
| 155 | my $req = HTTP::Request->new(HEAD => $urlmobile); | ||
| 156 | $req->header('Accept' => 'text/html'); | ||
| 157 | |||
| 158 | my $res = $ua->request($req); | ||
| 159 | |||
| 160 | if ($res->is_success) { | ||
| 161 | $CommonLib->printLog("Redirect to:" . $res->request()->uri()); | ||
| 162 | $urlmobile=$res->request()->uri(); | ||
| 163 | my $request = $ua->get ($urlmobile); | ||
| 164 | my $content=$request->content; | ||
| 165 | if ($content) { | ||
| 166 | $CommonLib->printLog("Download OK"); | ||
| 167 | $content =~ s/\n//g; | ||
| 168 | my @dummyMobileKeys = split(/,/, lc($content)); | ||
| 169 | foreach my $dummy (@dummyMobileKeys) { | ||
| 170 | $MobileArray{$dummy}='mobile'; | ||
| 171 | } | ||
| 172 | open (MYFILE, ">$configMobileFile") || die ("Cannot Open File: $configMobileFile"); | ||
| 173 | print MYFILE $content; | ||
| 174 | close (MYFILE); | ||
| 175 | } else { | ||
| 176 | $CommonLib->printLog("Download error ".$correctHostName); | ||
| 177 | $CommonLib->printLog("Try download previews version"); | ||
| 178 | &readMobileParamFromFile; | ||
| 179 | } | ||
| 180 | } else { | ||
| 181 | $CommonLib->printLog("Download error ".$correctHostName); | ||
| 182 | $CommonLib->printLog("Try download previews version"); | ||
| 183 | &readMobileParamFromFile; | ||
| 184 | } | ||
| 185 | } | ||
| 186 | sub readMobileParamFromFile { | ||
| 187 | $CommonLib->printLog("Read for mobile data from $configMobileFile"); | ||
| 188 | my $content=""; | ||
| 189 | if (open (IN,$configMobileFile)) { | ||
| 190 | while (<IN>) { | ||
| 191 | $content=$content.$_; | ||
| 192 | } | ||
| 193 | close IN; | ||
| 194 | } else { | ||
| 195 | $CommonLib->printLog("Error open file:$configMobileFile"); | ||
| 196 | ModPerl::Util::exit(); | ||
| 197 | } | ||
| 198 | $content =~ s/\n//g; | ||
| 199 | my @dummyMobileKeys = split(/,/, lc($content)); | ||
| 200 | foreach my $dummy (@dummyMobileKeys) { | ||
| 201 | $MobileArray{$dummy}='mobile'; | ||
| 202 | } | ||
| 203 | } | ||
| 204 | sub readTabletParamFromUrl { | ||
| 205 | $CommonLib->printLog("Read data for tablet detection from ".$urlTablet); | ||
| 206 | my $req = HTTP::Request->new(HEAD => $urlTablet); | ||
| 207 | $req->header('Accept' => 'text/html'); | ||
| 208 | |||
| 209 | my $res = $ua->request($req); | ||
| 210 | |||
| 211 | if ($res->is_success) { | ||
| 212 | $CommonLib->printLog("Redirect to:" . $res->request()->uri()); | ||
| 213 | $urlTablet=$res->request()->uri(); | ||
| 214 | my $request = $ua->get ($urlTablet); | ||
| 215 | my $content=$request->content; | ||
| 216 | if ($content) { | ||
| 217 | $CommonLib->printLog("Download OK"); | ||
| 218 | $content =~ s/\n//g; | ||
| 219 | my @dummyMobileKeys = split(/,/, lc($content)); | ||
| 220 | foreach my $dummy (@dummyMobileKeys) { | ||
| 221 | $MobileTabletArray{$dummy}='mobile'; | ||
| 222 | } | ||
| 223 | open (MYFILE, ">$configTabletFile") || die ("Cannot Open File: $configMobileFile"); | ||
| 224 | print MYFILE $content; | ||
| 225 | close (MYFILE); | ||
| 226 | } else { | ||
| 227 | $CommonLib->printLog("Download error from ".$correctHostName); | ||
| 228 | $CommonLib->printLog("Try download previews version"); | ||
| 229 | &readTabletParamFromFile; | ||
| 230 | } | ||
| 231 | } else { | ||
| 232 | $CommonLib->printLog("Error: " . $res->status_line); | ||
| 233 | $CommonLib->printLog("Download error from ".$correctHostName); | ||
| 234 | $CommonLib->printLog("Try download previews version"); | ||
| 235 | &readTabletParamFromFile; | ||
| 236 | } | ||
| 237 | |||
| 238 | } | ||
| 239 | sub readTabletParamFromFile { | ||
| 240 | $CommonLib->printLog("Read for tablet data from $configTabletFile"); | ||
| 241 | my $content=""; | ||
| 242 | if (open (IN,$configTabletFile)) { | ||
| 243 | while (<IN>) { | ||
| 244 | $content=$content.$_; | ||
| 245 | } | ||
| 246 | close IN; | ||
| 247 | } else { | ||
| 248 | $CommonLib->printLog("Error open file:$configTabletFile"); | ||
| 249 | ModPerl::Util::exit(); | ||
| 250 | } | ||
| 251 | $content =~ s/\n//g; | ||
| 252 | my @dummyMobileKeys = split(/,/, lc($content)); | ||
| 253 | foreach my $dummy (@dummyMobileKeys) { | ||
| 254 | $MobileTabletArray{$dummy}='mobile'; | ||
| 255 | } | ||
| 256 | } | ||
| 257 | sub readTouchParamFromUrl { | ||
| 258 | $CommonLib->printLog("Read for touch data for touch detection from ".$urlTouch); | ||
| 259 | my $req = HTTP::Request->new(HEAD => $urlTouch); | ||
| 260 | $req->header('Accept' => 'text/html'); | ||
| 261 | |||
| 262 | my $res = $ua->request($req); | ||
| 263 | |||
| 264 | if ($res->is_success) { | ||
| 265 | $CommonLib->printLog("Redirect to:" . $res->request()->uri()); | ||
| 266 | $urlmobile=$urlmobile; | ||
| 267 | my $request = $ua->get ($urlTouch); | ||
| 268 | my $content=$request->content; | ||
| 269 | |||
| 270 | if ($content) { | ||
| 271 | $CommonLib->printLog("Download OK"); | ||
| 272 | $content =~ s/\n//g; | ||
| 273 | my @dummyMobileKeys = split(/,/, lc($content)); | ||
| 274 | foreach my $dummy (@dummyMobileKeys) { | ||
| 275 | $MobileTouchArray{$dummy}='mobile'; | ||
| 276 | } | ||
| 277 | open (MYFILE, ">$configTouchFile") || die ("Cannot Open File: $configMobileFile"); | ||
| 278 | print MYFILE $content; | ||
| 279 | close (MYFILE); | ||
| 280 | } else { | ||
| 281 | $CommonLib->printLog("Download error from ".$correctHostName); | ||
| 282 | $CommonLib->printLog("Try download previews version"); | ||
| 283 | &readTouchParamFromFile; | ||
| 284 | } | ||
| 285 | } else { | ||
| 286 | $CommonLib->printLog("Error: " . $res->status_line); | ||
| 287 | $CommonLib->printLog("Download error from ".$correctHostName); | ||
| 288 | $CommonLib->printLog("Try download previews version"); | ||
| 289 | &readTouchParamFromFile; | ||
| 290 | } | ||
| 291 | } | ||
| 292 | sub readTouchParamFromFile { | ||
| 293 | $CommonLib->printLog("Read data from $configTouchFile"); | ||
| 294 | my $content=""; | ||
| 295 | if (open (IN,$configTouchFile)) { | ||
| 296 | while (<IN>) { | ||
| 297 | $content=$content.$_; | ||
| 298 | } | ||
| 299 | close IN; | ||
| 300 | } else { | ||
| 301 | $CommonLib->printLog("Error open file:$configTouchFile"); | ||
| 302 | ModPerl::Util::exit(); | ||
| 303 | } | ||
| 304 | $content =~ s/\n//g; | ||
| 305 | my @dummyMobileKeys = split(/,/, lc($content)); | ||
| 306 | foreach my $dummy (@dummyMobileKeys) { | ||
| 307 | $MobileTouchArray{$dummy}='mobile'; | ||
| 308 | } | ||
| 309 | } | ||
| 310 | sub readTVParamFromUrl { | ||
| 311 | $CommonLib->printLog("Read data for TV detection from ".$urlTv); | ||
| 312 | my $req = HTTP::Request->new(HEAD => $urlTv); | ||
| 313 | $req->header('Accept' => 'text/html'); | ||
| 314 | |||
| 315 | my $res = $ua->request($req); | ||
| 316 | |||
| 317 | if ($res->is_success) { | ||
| 318 | $CommonLib->printLog("Redirect to:" . $res->request()->uri()); | ||
| 319 | $urlmobile= $res->request()->uri(); | ||
| 320 | my $request = $ua->get ($urlTv); | ||
| 321 | my $content=$request->content; | ||
| 322 | |||
| 323 | if ($content) { | ||
| 324 | $CommonLib->printLog("Download OK"); | ||
| 325 | $content =~ s/\n//g; | ||
| 326 | my @dummyMobileKeys = split(/,/, lc($content)); | ||
| 327 | foreach my $dummy (@dummyMobileKeys) { | ||
| 328 | $MobileTVArray{$dummy}='mobile'; | ||
| 329 | } | ||
| 330 | open (MYFILE, ">$configTVFile") || die ("Cannot Open File: $configTVFile"); | ||
| 331 | print MYFILE $content; | ||
| 332 | close (MYFILE); | ||
| 333 | } else { | ||
| 334 | $CommonLib->printLog("Download error from ".$correctHostName); | ||
| 335 | $CommonLib->printLog("Try download previews version"); | ||
| 336 | &readTVParamFromFile; | ||
| 337 | } | ||
| 338 | } else { | ||
| 339 | $CommonLib->printLog("Error: " . $res->status_line); | ||
| 340 | $CommonLib->printLog("Download error from ".$correctHostName); | ||
| 341 | $CommonLib->printLog("Try download previews version"); | ||
| 342 | &readTVParamFromFile; | ||
| 343 | } | ||
| 344 | } | ||
| 345 | sub readTVParamFromFile { | ||
| 346 | $CommonLib->printLog("Read for tv data from $configTVFile"); | ||
| 347 | my $content=""; | ||
| 348 | if (open (IN,$configTVFile)) { | ||
| 349 | while (<IN>) { | ||
| 350 | $content=$content.$_; | ||
| 351 | } | ||
| 352 | close IN; | ||
| 353 | } else { | ||
| 354 | $CommonLib->printLog("Error open file:$configTVFile"); | ||
| 355 | ModPerl::Util::exit(); | ||
| 356 | } | ||
| 357 | $content =~ s/\n//g; | ||
| 358 | my @dummyMobileKeys = split(/,/, lc($content)); | ||
| 359 | foreach my $dummy (@dummyMobileKeys) { | ||
| 360 | $MobileTVArray{$dummy}='mobile'; | ||
| 361 | } | ||
| 362 | } | ||
| 363 | sub isMobile { | ||
| 364 | my ($UserAgent) = @_; | ||
| 365 | my $ind=0; | ||
| 366 | my $isMobileValue='false'; | ||
| 367 | my $pair; | ||
| 368 | my $length=0; | ||
| 369 | foreach $pair (sort keys %MobileArray) { | ||
| 370 | if ($UserAgent =~ m/\Q$pair\E/) { | ||
| 371 | $isMobileValue='true'; | ||
| 372 | } | ||
| 373 | } | ||
| 374 | return $isMobileValue; | ||
| 375 | } | ||
| 376 | sub isTablet { | ||
| 377 | my ($UserAgent) = @_; | ||
| 378 | my $ind=0; | ||
| 379 | my $isTabletValue='false'; | ||
| 380 | my $pair; | ||
| 381 | my $length=0; | ||
| 382 | foreach $pair (sort keys %MobileTabletArray) { | ||
| 383 | if ($UserAgent =~ m/\Q$pair\E/) { | ||
| 384 | $isTabletValue='true'; | ||
| 385 | } | ||
| 386 | } | ||
| 387 | return $isTabletValue; | ||
| 388 | } | ||
| 389 | sub isTouch { | ||
| 390 | my ($UserAgent) = @_; | ||
| 391 | my $ind=0; | ||
| 392 | my $isTouchValue='false'; | ||
| 393 | my $pair; | ||
| 394 | my $length=0; | ||
| 395 | foreach $pair (sort keys %MobileTouchArray) { | ||
| 396 | if ($UserAgent =~ m/\Q$pair\E/) { | ||
| 397 | $isTouchValue='true'; | ||
| 398 | } | ||
| 399 | } | ||
| 400 | return $isTouchValue; | ||
| 401 | } | ||
| 402 | sub isTV { | ||
| 403 | my ($UserAgent) = @_; | ||
| 404 | my $ind=0; | ||
| 405 | my $isTVValue='false'; | ||
| 406 | my $pair; | ||
| 407 | my $length=0; | ||
| 408 | foreach $pair (sort keys %MobileTVArray) { | ||
| 409 | if ($UserAgent =~ m/\Q$pair\E/) { | ||
| 410 | $isTVValue='true'; | ||
| 411 | } | ||
| 412 | } | ||
| 413 | return $isTVValue; | ||
| 414 | } | ||
| 415 | sub cleanUA { | ||
| 416 | my ($UserAgent) = @_; | ||
| 417 | $UserAgent =~ s/google favicon//g; | ||
| 418 | return $UserAgent; | ||
| 419 | } | ||
| 420 | sub getOperativeSystem { | ||
| 421 | my ($UserAgent) = @_; | ||
| 422 | my $returnValue="nc"; | ||
| 423 | my @osTypesArray = split(/,/, "android,iphone|ipad|ipod,windows phone,symbianos,blackberry,kindle"); | ||
| 424 | my $osNumber=0; | ||
| 425 | foreach my $os (@osTypesArray){ | ||
| 426 | if ($UserAgent =~ /$os/) { | ||
| 427 | if ($osNumber == 0 ) { | ||
| 428 | $returnValue="android"; | ||
| 429 | } elsif ($osNumber == 1 ) { | ||
| 430 | $returnValue="ios"; | ||
| 431 | } elsif ($osNumber == 2 ) { | ||
| 432 | $returnValue="windows phone"; | ||
| 433 | } elsif ($osNumber == 3 ) { | ||
| 434 | $returnValue="symbian"; | ||
| 435 | } elsif ($osNumber == 4 ) { | ||
| 436 | $returnValue="kindle"; | ||
| 437 | } | ||
| 438 | return $returnValue; | ||
| 439 | } | ||
| 440 | $osNumber++; | ||
| 441 | } | ||
| 442 | return $returnValue; | ||
| 443 | } | ||
| 444 | sub getOperativeSystemVersion { | ||
| 445 | my ($UserAgent, $os) = (@_); | ||
| 446 | my $matchOS=0; | ||
| 447 | my $regex_param=''; | ||
| 448 | my $return_value="nc"; | ||
| 449 | if ($os eq 'android') { | ||
| 450 | $regex_param="android ([0-9]\\.[0-9](\\.[0-9])?)"; | ||
| 451 | $matchOS=1; | ||
| 452 | } elsif ($os eq 'ios') { | ||
| 453 | $regex_param="os ((\\d+_?){2,3})\\s"; | ||
| 454 | $matchOS=1; | ||
| 455 | } elsif ($os eq 'windows phone') { | ||
| 456 | $regex_param="( phone| phone os) ([0-9]\\.[0-9](\\.[0-9])?)"; | ||
| 457 | $matchOS=1; | ||
| 458 | } elsif ($os eq 'symbian') { | ||
| 459 | $regex_param="symbianos/([0-9]\\.[0-9](\\.[0-9])?)"; | ||
| 460 | $matchOS=1; | ||
| 461 | } elsif ($os eq 'mac') { | ||
| 462 | $regex_param="os x ([0-9]([0-9]?)(_|.)([0-9]?)([0-9]?)(_?|.?)([0-9]?)([0-9]?))"; | ||
| 463 | $matchOS=1; | ||
| 464 | } | ||
| 465 | if ($matchOS == 1) { | ||
| 466 | ($return_value) = ($UserAgent =~ /$regex_param/i); | ||
| 467 | } | ||
| 468 | |||
| 469 | return $return_value; | ||
| 470 | |||
| 471 | } | ||
| 472 | sub getOperativeDesktopSystem { | ||
| 473 | my ($UserAgent, $os) = (@_); | ||
| 474 | my $returnValue="nc"; | ||
| 475 | my @osTypesArray = split(/,/, "windows,mac,linux"); | ||
| 476 | my $osNumber=0; | ||
| 477 | foreach my $os (@osTypesArray){ | ||
| 478 | if ($UserAgent =~ /$os/) { | ||
| 479 | if ($osNumber == 0 ) { | ||
| 480 | $returnValue="windows"; | ||
| 481 | } elsif ($osNumber == 1 ) { | ||
| 482 | $returnValue="mac"; | ||
| 483 | } elsif ($osNumber == 2 ) { | ||
| 484 | $returnValue="windows"; | ||
| 485 | } | ||
| 486 | return $returnValue; | ||
| 487 | } | ||
| 488 | $osNumber++; | ||
| 489 | } | ||
| 490 | return $returnValue; | ||
| 491 | } | ||
| 492 | sub getBrowserVersion { | ||
| 493 | my ($UserAgent, $os) = (@_); | ||
| 494 | my $regex="(firefox|msie|chrome|chromium|safari|edge|seamonkey|opera)\\/(([0-9]?)([0-9]?)([0-9]?)(.?)([0-9]?)([0-9]?)([0-9]?)(.?)([0-9]?)([0-9]?)([0-9]?)(.?)([0-9]?)([0-9]?)([0-9]?)(.?)([0-9]?)([0-9]?)([0-9]?))"; | ||
| 495 | my $regex2='(msie|(?!gecko.+)firefox|(?!applewebkit.+chrome.+)safari|(?!applewebkit.+)chrome|applewebkit(?!.+chrome|.+safari)|gecko(?!.+firefox))(?: |\/)([\d\.apre]+)'; | ||
| 496 | my $type="nc"; | ||
| 497 | my $version="nc"; | ||
| 498 | my ($mtype,$mversion)=($UserAgent =~ /$regex/i); | ||
| 499 | if (!$mtype) { | ||
| 500 | my ($mtype2,$mversion2)=($UserAgent =~ /$regex2/i); | ||
| 501 | if ($mtype2) { | ||
| 502 | $type=$mtype2; | ||
| 503 | $version=$mversion2; | ||
| 504 | } | ||
| 505 | } else { | ||
| 506 | $type=$mtype; | ||
| 507 | $version=$mversion; | ||
| 508 | } | ||
| 509 | print $type."\n"; | ||
| 510 | print $version."\n"; | ||
| 511 | return ($type,$version); | ||
| 512 | |||
| 513 | } | ||
| 514 | |||
| 515 | sub handler { | ||
| 516 | my $f = shift; | ||
| 517 | my $capability2; | ||
| 518 | my $variabile=""; | ||
| 519 | my $user_agent=lc($f->headers_in->{'User-Agent'}|| ''); | ||
| 520 | my $x_user_agent=$f->headers_in->{'X-Device-User-Agent'}|| ''; | ||
| 521 | my $x_operamini_phone_ua=$f->headers_in->{'X-OperaMini-Phone-Ua'}|| ''; | ||
| 522 | my $x_operamini_ua=$f->headers_in->{'X-OperaMini-Ua'}|| ''; | ||
| 523 | my $query_string=$f->args; | ||
| 524 | my $docroot = $f->document_root(); | ||
| 525 | my $id=""; | ||
| 526 | my $location="none"; | ||
| 527 | my $isTablet="false"; | ||
| 528 | my $width_toSearch; | ||
| 529 | my $type_redirect="internal"; | ||
| 530 | my $return_value; | ||
| 531 | my $dummy=""; | ||
| 532 | my $variabile2=""; | ||
| 533 | my %ArrayCapFound; | ||
| 534 | my $controlCookie; | ||
| 535 | my $query_img=""; | ||
| 536 | $ArrayCapFound{is_transcoder}='false'; | ||
| 537 | my %ArrayQuery; | ||
| 538 | my $var; | ||
| 539 | my $mobile=0; | ||
| 540 | my $amf_device_istablet='false'; | ||
| 541 | my $amf_device_istouch='false'; | ||
| 542 | my $amf_device_ismobile=''; | ||
| 543 | my $amf_device_istv=''; | ||
| 544 | my $version=""; | ||
| 545 | my $amf_device_os='nc'; | ||
| 546 | my $amf_device_os_version='nc'; | ||
| 547 | my $amf_browser_type='nc'; | ||
| 548 | my $amf_browser_version='nc'; | ||
| 549 | if ($user_agent eq "") { | ||
| 550 | $user_agent="no useragent found"; | ||
| 551 | } | ||
| 552 | if ($x_user_agent) { | ||
| 553 | $user_agent=lc($x_user_agent); | ||
| 554 | } | ||
| 555 | if ($x_operamini_phone_ua) { | ||
| 556 | $user_agent=lc($x_operamini_phone_ua); | ||
| 557 | } | ||
| 558 | my $cookie = $f->headers_in->{Cookie} || ''; | ||
| 559 | if ($CommonLib->readCookie($cookie) eq 'true' || $CommonLib->readCookie($cookie) eq 'false') { | ||
| 560 | $amf_device_ismobile=$CommonLib->readCookie($cookie); | ||
| 561 | } | ||
| 562 | my $amfFull=$CommonLib->readCookie_fullB($cookie); | ||
| 563 | if ($query_string) { | ||
| 564 | my @vars = split(/&/, $query_string); | ||
| 565 | foreach $var (sort @vars){ | ||
| 566 | if ($var) { | ||
| 567 | my ($v,$i) = split(/=/, $var); | ||
| 568 | $v =~ tr/+/ /; | ||
| 569 | $v =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; | ||
| 570 | if ($i) { | ||
| 571 | $i =~ tr/+/ /; | ||
| 572 | $i =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; | ||
| 573 | $i =~ s/<!--(.|\n)*-->//g; | ||
| 574 | $ArrayQuery{$v}=$i; | ||
| 575 | } | ||
| 576 | } | ||
| 577 | } | ||
| 578 | if (($ArrayQuery{amf}) && $restmode eq 'true') { | ||
| 579 | $user_agent=lc($ArrayQuery{amf}); | ||
| 580 | } | ||
| 581 | if ($ArrayQuery{$mobilenable}) { | ||
| 582 | $f->err_headers_out->set('Set-Cookie' => "amfFull=false; path=/;"); | ||
| 583 | $amfFull='ok'; | ||
| 584 | } | ||
| 585 | |||
| 586 | } | ||
| 587 | $user_agent=&cleanUA($user_agent); | ||
| 588 | if ($amf_device_ismobile eq "") { | ||
| 589 | $amf_device_ismobile = &isMobile($user_agent); | ||
| 590 | if ($amf_device_ismobile eq 'true') { | ||
| 591 | $amf_device_istouch = &isTouch($user_agent); | ||
| 592 | $amf_device_istablet=&isTablet($user_agent); | ||
| 593 | $amf_device_os=&getOperativeSystem($user_agent); | ||
| 594 | } else { | ||
| 595 | $amf_device_os=&getOperativeDesktopSystem($user_agent); | ||
| 596 | } | ||
| 597 | $amf_device_os_version=&getOperativeSystemVersion($user_agent,$amf_device_os); | ||
| 598 | $amf_device_istv = &isTV($user_agent); | ||
| 599 | if ($cookiecachesystem eq "true") { | ||
| 600 | $f->err_headers_out->set('Set-Cookie' => "amfID=$id; path=/;"); | ||
| 601 | } | ||
| 602 | } | ||
| 603 | ($amf_browser_type,$amf_browser_version)=&getBrowserVersion($user_agent); | ||
| 604 | if ($amfFull ne "") { | ||
| 605 | $f->subprocess_env("AMF_FORCE_TO_DESKTOP" => 'true'); | ||
| 606 | $f->pnotes("amf_force_to_desktop" => 'true'); | ||
| 607 | } | ||
| 608 | $f->pnotes('is_tablet' => $amf_device_istablet); | ||
| 609 | $f->pnotes("amf_device_ismobile" => $amf_device_ismobile); | ||
| 610 | $f->pnotes("is_touch" => $amf_device_istouch); | ||
| 611 | $f->subprocess_env("AMF_ID" => "amf_lite_detection"); | ||
| 612 | $f->subprocess_env("AMF_DEVICE_IS_MOBILE" => $amf_device_ismobile); | ||
| 613 | $f->subprocess_env("AMF_DEVICE_IS_TABLET" => $amf_device_istablet); | ||
| 614 | $f->subprocess_env("AMF_DEVICE_IS_TOUCH" => $amf_device_istouch); | ||
| 615 | $f->subprocess_env("AMF_DEVICE_IS_TV" => $amf_device_istv); | ||
| 616 | $f->subprocess_env("AMF_DEVICE_OS" => $amf_device_os); | ||
| 617 | $f->subprocess_env("AMF_DEVICE_OS_VERSION" => $amf_device_os_version); | ||
| 618 | $f->subprocess_env("AMF_BROWSER_TYPE" => $amf_browser_type); | ||
| 619 | $f->subprocess_env("AMF_BROWSER_VERSION" => $amf_browser_version); | ||
| 620 | |||
| 621 | $f->subprocess_env("AMF_VER" => $VERSION); | ||
| 622 | $f->headers_out->set("AMF-Ver"=> $VERSION); | ||
| 623 | if ($x_operamini_ua) { | ||
| 624 | $f->subprocess_env("AMF_MOBILE_BROWSER" => $x_operamini_ua); | ||
| 625 | $f->pnotes("mobile_browser" => $x_operamini_ua); | ||
| 626 | $f->subprocess_env("AMF_IS_TRANCODER" => 'true'); | ||
| 627 | $f->pnotes("is_transcoder" => 'true'); | ||
| 628 | } else { | ||
| 629 | $f->pnotes("is_transcoder" => 'true'); | ||
| 630 | } | ||
| 631 | return Apache2::Const::DECLINED; | ||
| 632 | } | ||
| 633 | 1; | ||
| 634 | |||
| 635 | |||
| 636 | =head1 NAME | ||
| 637 | |||
| 638 | Apache2::AMFLiteDetectionFilter - The module detects in lite mode the mobile device and passes few capabilities on to the other web application as environment variables | ||
| 639 | |||
| 640 | =head1 DESCRIPTION | ||
| 641 | |||
| 642 | Module for device detection, parse the user agent and decide if the device is mobile, touch or tablet. | ||
| 643 | |||
| 644 | =head1 AMF PROJECT SITE | ||
| 645 | |||
| 646 | http://www.apachemobilefilter.org | ||
| 647 | |||
| 648 | =head1 DOCUMENTATION | ||
| 649 | |||
| 650 | http://wiki.apachemobilefilter.org | ||
| 651 | |||
| 652 | Perl Module Documentation: http://wiki.apachemobilefilter.org/index.php/AMFLiteDetectionFilter | ||
| 653 | |||
| 654 | =head1 AUTHOR | ||
| 655 | |||
| 656 | Idel Fuschini (idel.fuschini [at] gmail [dot] com) | ||
| 657 | |||
| 658 | =head1 COPYRIGHT | ||
| 659 | |||
| 660 | You may distribute under the terms of either the GNU General Public | ||
| 661 | License or the Artistic License, as specified in the Perl README file. | ||
| 662 | |||
| 663 | =cut | ||
| 664 |