| 1 | #file:Apache2/AMFWURFLFilter.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::AMFWURFLFilter; | ||
| 12 | |||
| 13 | use strict; | ||
| 14 | use warnings; | ||
| 15 | use Apache2::AMFCommonLib (); | ||
| 16 | |||
| 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 qw($ua get); | ||
| 24 | use Apache2::Const -compile => qw(OK REDIRECT DECLINED); | ||
| 25 | use IO::Uncompress::Unzip qw(unzip $UnzipError) ; | ||
| 26 | use constant BUFF_LEN => 1024; | ||
| 27 | use Cache::FileBackend; | ||
| 28 | |||
| 29 | |||
| 30 | # | ||
| 31 | # Define the global environment | ||
| 32 | # | ||
| 33 | |||
| 34 | use vars qw($VERSION); | ||
| 35 | $VERSION= "4.33"; | ||
| 36 | my $CommonLib = new Apache2::AMFCommonLib (); | ||
| 37 | |||
| 38 | my %Capability; | ||
| 39 | my %Array_fb; | ||
| 40 | my %Array_id; | ||
| 41 | my %Array_fullua_id; | ||
| 42 | my %Array_DDRcapability; | ||
| 43 | |||
| 44 | my %MobileArray=$CommonLib->getMobileArray; | ||
| 45 | my %PCArray=$CommonLib->getPCArray; | ||
| 46 | my $mobileversionurl="none"; | ||
| 47 | my $fullbrowserurl="none"; | ||
| 48 | my $redirecttranscoder="true"; | ||
| 49 | my $redirecttranscoderurl="none"; | ||
| 50 | my $resizeimagedirectory="none"; | ||
| 51 | my $wurflnetdownload="false"; | ||
| 52 | my $downloadwurflurl="false"; | ||
| 53 | my $listall="false"; | ||
| 54 | my $cookiecachesystem="false"; | ||
| 55 | my $WURFLVersion="unknown"; | ||
| 56 | my $personalwurflurl='unknown'; | ||
| 57 | my $cachedirectorystore="/tmp"; | ||
| 58 | my $capabilitylist="none"; | ||
| 59 | my $restmode='false'; | ||
| 60 | my $deepSearch=0; | ||
| 61 | my $checkVersion='false'; | ||
| 62 | my $mobilenable="false"; | ||
| 63 | |||
| 64 | |||
| 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("--- The license of the wurfl.xml file is now changed. ---"); | ||
| 70 | $CommonLib->printLog("--- The WURFL file is the Copyright of ScientiaMobile, read the license ---"); | ||
| 71 | $CommonLib->printLog("--- For more info: http://www.scientiamobile.com. ---"); | ||
| 72 | $CommonLib->printLog("---------------------------------------------------------------------------"); | ||
| 73 | $CommonLib->printLog("AMFWURFLFilter module Version $VERSION"); | ||
| 74 | if (($ENV{AMFSetProxy}) && $ENV{AMFSetProxy} eq 'true' ) { | ||
| 75 | $CommonLib->printLog("AMFSetHttpProxy is ".$ENV{AMFSetHttpProxy}); | ||
| 76 | my $port=3128; | ||
| 77 | my $default=""; | ||
| 78 | if ($ENV{AMFSetHttpProxyPort}) { | ||
| 79 | $port=$ENV{AMFSetHttpProxyPort}; | ||
| 80 | } else { | ||
| 81 | $default=' (default)'; | ||
| 82 | } | ||
| 83 | $CommonLib->printLog("AMFSetHttpProxyPort is ".$port.$default); | ||
| 84 | $ua->proxy('http','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{AMFCheckVersion}) { | ||
| 99 | $checkVersion=$ENV{AMFCheckVersion}; | ||
| 100 | } | ||
| 101 | if ($checkVersion eq 'true') { | ||
| 102 | $CommonLib->printLog("Check on apchemobilefilter.org if the installed AMF is the last version"); | ||
| 103 | $CommonLib->printLog("Try to download http://www.apachemobilefilter.org/param/amf.config"); | ||
| 104 | my $url="http://www.apachemobilefilter.org/param/amf.config"; | ||
| 105 | my $content = get ($url); | ||
| 106 | $content =~ s/\n//g; | ||
| 107 | my $check_version=0; | ||
| 108 | if ($content) { | ||
| 109 | $check_version=$content; | ||
| 110 | } | ||
| 111 | if ($check_version > $VERSION && $check_version ne 0) { | ||
| 112 | $CommonLib->printLog("---------------------------------------------------------------------------"); | ||
| 113 | $CommonLib->printLog("----- There is a new version of AMF V$check_version online ----"); | ||
| 114 | $CommonLib->printLog("---------------------------------------------------------------------------"); | ||
| 115 | } else { | ||
| 116 | $CommonLib->printLog("AMF installed is the last version"); | ||
| 117 | } | ||
| 118 | } else { | ||
| 119 | $CommonLib->printLog("AMFCheckVersion is false, AMF don't check the last version."); | ||
| 120 | } | ||
| 121 | if ($ENV{ResizeImageDirectory}) { | ||
| 122 | $Capability{'max_image_width'}="max_image_width"; | ||
| 123 | $Capability{'max_image_height'}="max_image_width"; | ||
| 124 | $resizeimagedirectory=$ENV{ResizeImageDirectory}; | ||
| 125 | } | ||
| 126 | if (($ENV{FullBrowserUrl}) || ($ENV{MobileVersionUrl})) { | ||
| 127 | $Capability{'device_claims_web_support'}="device_claims_web_support"; | ||
| 128 | $Capability{'is_wireless_device'}="is_wireless_device"; | ||
| 129 | $fullbrowserurl=$ENV{FullBrowserUrl} | ||
| 130 | } | ||
| 131 | if ($ENV{RedirectTranscoderUrl}) { | ||
| 132 | $Capability{'is_transcoder'}="is_transcoder"; | ||
| 133 | $redirecttranscoderurl=$ENV{RedirectTranscoderUrl}; | ||
| 134 | } | ||
| 135 | |||
| 136 | # | ||
| 137 | # Check if AMFMobileHome and CacheDirectoryStore is setting in apache httpd.conf file for example: | ||
| 138 | # PerlSetEnv AMFMobileHome <apache_directory>/MobileFilter | ||
| 139 | # | ||
| 140 | if ($ENV{CacheDirectoryStore}) { | ||
| 141 | $cachedirectorystore=$ENV{CacheDirectoryStore}; | ||
| 142 | $CommonLib->printLog("CacheDirectoryStore is: $cachedirectorystore"); | ||
| 143 | } else { | ||
| 144 | $CommonLib->printLog("CacheDirectoryStore not exist. Please set the variable CacheDirectoryStore into httpd.conf, (the directory must be writeable)"); | ||
| 145 | ModPerl::Util::exit(); | ||
| 146 | } | ||
| 147 | # | ||
| 148 | # Define the cache system directory | ||
| 149 | # | ||
| 150 | my $cacheSystem = new Cache::FileBackend( $cachedirectorystore, 3, 000 ); | ||
| 151 | $cacheSystem->store( 'wurfl-id', 'device_not_found', "id=device_not_found&device=false&device_claims_web_support=true&is_wireless_device=false"); | ||
| 152 | if ($cacheSystem->restore('wurfl-conf','ver')) { | ||
| 153 | } else { | ||
| 154 | $CommonLib->printLog('Create new wurf-con store'); | ||
| 155 | $cacheSystem->store('wurfl-conf', 'ver', 'null'); | ||
| 156 | $cacheSystem->store('wurfl-conf', 'caplist', 'null'); | ||
| 157 | $cacheSystem->store('wurfl-conf', 'listall', 'null'); | ||
| 158 | $cacheSystem->store('wurfl-conf', 'RedirectTranscoderUrl','null'); | ||
| 159 | $cacheSystem->store('wurfl-conf', 'MobileVersionUrl','null'); | ||
| 160 | $cacheSystem->store('wurfl-conf', 'FullBrowserUrl','null'); | ||
| 161 | $cacheSystem->store('wurfl-conf', 'ResizeImageDirectory','null'); | ||
| 162 | } | ||
| 163 | if ($ENV{AMFMobileHome}) { | ||
| 164 | &loadConfigFile("$ENV{AMFMobileHome}/wurfl.xml"); | ||
| 165 | } else { | ||
| 166 | $CommonLib->printLog("AMFMobileHome not exist. Please set the variable AMFMobileHome into httpd.conf"); | ||
| 167 | ModPerl::Util::exit(); | ||
| 168 | } | ||
| 169 | |||
| 170 | sub loadConfigFile { | ||
| 171 | my ($fileWurfl) = @_; | ||
| 172 | my $null=""; | ||
| 173 | my $null2=""; | ||
| 174 | my $null3=""; | ||
| 175 | my $val; | ||
| 176 | my $capability; | ||
| 177 | my $r_id; | ||
| 178 | my $dummy; | ||
| 179 | #The filter | ||
| 180 | $CommonLib->printLog("Start read configuration from httpd.conf"); | ||
| 181 | |||
| 182 | if ($ENV{WurflNetDownload}) { | ||
| 183 | if ($ENV{WurflNetDownload} eq 'true' || $ENV{WurflNetDownload} eq 'false') { | ||
| 184 | $wurflnetdownload=$ENV{WurflNetDownload}; | ||
| 185 | $CommonLib->printLog("WurflNetDownload is: $wurflnetdownload"); | ||
| 186 | } else { | ||
| 187 | $CommonLib->printLog("Error WurflNetDownload parmeter must set to true or false"); | ||
| 188 | ModPerl::Util::exit(); | ||
| 189 | } | ||
| 190 | } | ||
| 191 | |||
| 192 | if ($ENV{DownloadWurflURL}) { | ||
| 193 | $downloadwurflurl=$ENV{DownloadWurflURL}; | ||
| 194 | $CommonLib->printLog("DownloadWurflURL is: $downloadwurflurl"); | ||
| 195 | } | ||
| 196 | if ($ENV{CapabilityList}) { | ||
| 197 | my @dummycapability = split(/,/, $ENV{CapabilityList}); | ||
| 198 | $capabilitylist=$ENV{CapabilityList}; | ||
| 199 | foreach $dummy (@dummycapability) { | ||
| 200 | if ($dummy eq "all") { | ||
| 201 | $listall="true"; | ||
| 202 | } | ||
| 203 | $Capability{$dummy}=$dummy; | ||
| 204 | $CommonLib->printLog("CapabilityList is: $dummy"); | ||
| 205 | } | ||
| 206 | } else { | ||
| 207 | $listall="true"; | ||
| 208 | $CommonLib->printLog('CapabilityList not setted so the default value is "all"'); | ||
| 209 | } | ||
| 210 | if ($ENV{AMFMobileKeys}) { | ||
| 211 | my @dummyMobileKeys = split(/,/, $ENV{AMFMobileKeys}); | ||
| 212 | foreach $dummy (@dummyMobileKeys) { | ||
| 213 | $MobileArray{$dummy}='mobile'; | ||
| 214 | } | ||
| 215 | $CommonLib->printLog("AMFMobileKeys is: $ENV{AMFMobileKeys}"); | ||
| 216 | } | ||
| 217 | |||
| 218 | |||
| 219 | if ($ENV{AMFProductionMode}) { | ||
| 220 | $cookiecachesystem=$ENV{AMFProductionMode}; | ||
| 221 | $CommonLib->printLog("AMFProductionMode is: $cookiecachesystem"); | ||
| 222 | } else { | ||
| 223 | $CommonLib->printLog("AMFProductionMode (the CookieCacheSystem is deprecated) is not setted the default value is $cookiecachesystem"); | ||
| 224 | } | ||
| 225 | if ($ENV{PersonalWurflFileName}) { | ||
| 226 | $personalwurflurl=$ENV{AMFMobileHome}."/".$ENV{PersonalWurflFileName}; | ||
| 227 | $CommonLib->printLog("PersonalWurflFileName is: $ENV{PersonalWurflFileName}"); | ||
| 228 | } | ||
| 229 | if ($ENV{RestMode}) { | ||
| 230 | $restmode=$ENV{RestMode}; | ||
| 231 | $CommonLib->printLog("RestMode is: $restmode"); | ||
| 232 | } | ||
| 233 | if ($ENV{AMFDeepParse}) { | ||
| 234 | $deepSearch=$ENV{AMFDeepParse}; | ||
| 235 | $CommonLib->printLog("AMFDeepParse is: $deepSearch"); | ||
| 236 | } else { | ||
| 237 | $CommonLib->printLog("AMFDeepParse is not setted the default value is 0"); | ||
| 238 | } | ||
| 239 | if ($ENV{FullBrowserMobileAccessKey}) { | ||
| 240 | $mobilenable="$ENV{FullBrowserMobileAccessKey}"; | ||
| 241 | $CommonLib->printLog("FullBrowserMobileAccessKey is: $ENV{FullBrowserMobileAccessKey}"); | ||
| 242 | $CommonLib->printLog("For access the device to fullbrowser set the link: <url>?$mobilenable=true"); | ||
| 243 | } | ||
| 244 | |||
| 245 | $CommonLib->printLog("Finish loading parameter"); | ||
| 246 | $CommonLib->printLog("---------------------------------------------------------------------------"); | ||
| 247 | if ($wurflnetdownload eq "true") { | ||
| 248 | |||
| 249 | $CommonLib->printLog("Start process downloading WURFL.xml from $downloadwurflurl"); | ||
| 250 | $CommonLib->printLog ("Test the URL"); | ||
| 251 | my ($content_type, $document_length, $modified_time, $expires, $server) = head($downloadwurflurl); | ||
| 252 | if ($content_type eq "") { | ||
| 253 | $CommonLib->printLog("Couldn't get $downloadwurflurl."); | ||
| 254 | ModPerl::Util::exit(); | ||
| 255 | } else { | ||
| 256 | $CommonLib->printLog("The URL is correct"); | ||
| 257 | $CommonLib->printLog("The size of document wurf file: $document_length bytes"); | ||
| 258 | } | ||
| 259 | |||
| 260 | if ($content_type eq 'application/zip') { | ||
| 261 | $CommonLib->printLog("The file is a zip file."); | ||
| 262 | $CommonLib->printLog ("Start downloading"); | ||
| 263 | my @dummypairs = split(/\//, $downloadwurflurl); | ||
| 264 | my ($ext_zip) = $downloadwurflurl =~ /\.(\w+)$/; | ||
| 265 | my $filezip=$dummypairs[-1]; | ||
| 266 | my $tmp_dir=$ENV{AMFMobileHome}; | ||
| 267 | $filezip="$tmp_dir/$filezip"; | ||
| 268 | my $status = getstore ($downloadwurflurl,$filezip); | ||
| 269 | my $output="$tmp_dir/tmp_wurfl.xml"; | ||
| 270 | unzip $filezip => $output | ||
| 271 | or die "unzip failed: $UnzipError\n"; | ||
| 272 | # | ||
| 273 | # call parseWURFLFile | ||
| 274 | # | ||
| 275 | callparseWURFLFile($output); | ||
| 276 | |||
| 277 | } else { | ||
| 278 | $CommonLib->printLog("The file is a xml file."); | ||
| 279 | my $content = get ($downloadwurflurl); | ||
| 280 | $content =~ s/\n//g; | ||
| 281 | $content =~ s/>/>\n/g; | ||
| 282 | |||
| 283 | my @rows = split(/\n/, $content); | ||
| 284 | my $row; | ||
| 285 | my $count=0; | ||
| 286 | my $numberRow=scalar(@rows); | ||
| 287 | my $progress=0; | ||
| 288 | |||
| 289 | foreach $row (@rows){ | ||
| 290 | $r_id=parseWURFLFile($row,$r_id); | ||
| 291 | my $perc=int(($progress/$numberRow)*100); | ||
| 292 | print "Percent loaded: ".$perc."%\r"; | ||
| 293 | $progress++; | ||
| 294 | } | ||
| 295 | } | ||
| 296 | $CommonLib->printLog("Finish downloading WURFL from $downloadwurflurl"); | ||
| 297 | |||
| 298 | } else { | ||
| 299 | if (-e "$fileWurfl") { | ||
| 300 | $CommonLib->printLog("Start loading WURFL.xml"); | ||
| 301 | if (open (IN,"$fileWurfl")) { | ||
| 302 | my $filesize= -s $fileWurfl; | ||
| 303 | my $string_file; | ||
| 304 | read (IN,$string_file,$filesize); | ||
| 305 | close IN; | ||
| 306 | $string_file =~ s/\n//g; | ||
| 307 | $string_file =~ s/>/>\n/g; | ||
| 308 | my @arrayFile=split(/\n/, $string_file); | ||
| 309 | my $numberRow=scalar(@arrayFile); | ||
| 310 | my $progress=0; | ||
| 311 | foreach my $line (@arrayFile) { | ||
| 312 | $r_id=parseWURFLFile($line,$r_id); | ||
| 313 | my $perc=int(($progress/$numberRow)*100); | ||
| 314 | print "Percent loaded: ".$perc."%\r"; | ||
| 315 | $progress++; | ||
| 316 | } | ||
| 317 | } else { | ||
| 318 | $CommonLib->printLog("Error open file:$fileWurfl"); | ||
| 319 | ModPerl::Util::exit(); | ||
| 320 | } | ||
| 321 | } else { | ||
| 322 | $CommonLib->printLog("File $fileWurfl not found"); | ||
| 323 | ModPerl::Util::exit(); | ||
| 324 | } | ||
| 325 | } | ||
| 326 | close IN; | ||
| 327 | my $arrLen = scalar %Array_fb; | ||
| 328 | ($arrLen,$dummy)= split(/\//, $arrLen); | ||
| 329 | if ($arrLen == 0) { | ||
| 330 | $CommonLib->printLog("Error the file probably is not a wurfl file, control the url or path"); | ||
| 331 | $CommonLib->printLog("Control also if the file is compress file, and DownloadZipFile parameter is seted false"); | ||
| 332 | ModPerl::Util::exit(); | ||
| 333 | } | ||
| 334 | $CommonLib->printLog("WURFL version: $WURFLVersion"); | ||
| 335 | if ($cacheSystem->restore('wurfl-conf', 'amfver') ne $VERSION||$cacheSystem->restore('wurfl-conf', 'ResizeImageDirectory') ne $resizeimagedirectory||$cacheSystem->restore('wurfl-conf', 'DownloadWurflURL') ne $downloadwurflurl||$cacheSystem->restore('wurfl-conf', 'FullBrowserUrl') ne $fullbrowserurl||$cacheSystem->restore('wurfl-conf', 'RedirectTranscoderUrl') ne $redirecttranscoderurl || $cacheSystem->restore('wurfl-conf', 'ver') ne $WURFLVersion || $cacheSystem->restore('wurfl-conf', 'caplist') ne $capabilitylist||$cacheSystem->restore('wurfl-conf', 'listall') ne $listall) { | ||
| 336 | $CommonLib->printLog("********************************************************************************************************"); | ||
| 337 | $CommonLib->printLog("* This is a new version of WURFL or you change some parameter value or it's a new version of AMF, now the old cache must be deleted *"); | ||
| 338 | $CommonLib->printLog("********************************************************************************************************"); | ||
| 339 | $cacheSystem->store('wurfl-conf', 'ver', $WURFLVersion); | ||
| 340 | $cacheSystem->store('wurfl-conf', 'amfver', $VERSION); | ||
| 341 | $cacheSystem->store('wurfl-conf', 'caplist', $capabilitylist); | ||
| 342 | $cacheSystem->store('wurfl-conf', 'listall', $listall); | ||
| 343 | $cacheSystem->store('wurfl-conf', 'RedirectTranscoderUrl', $redirecttranscoderurl); | ||
| 344 | $cacheSystem->store('wurfl-conf', 'FullBrowserUrl', $fullbrowserurl); | ||
| 345 | $cacheSystem->store('wurfl-conf', 'DownloadWurflURL', $downloadwurflurl); | ||
| 346 | $cacheSystem->store('wurfl-conf', 'ResizeImageDirectory', $resizeimagedirectory); | ||
| 347 | |||
| 348 | $cacheSystem->delete_namespace( 'WURFL-id' ); | ||
| 349 | $cacheSystem->delete_namespace( 'WURFL-ua' ); | ||
| 350 | } | ||
| 351 | $CommonLib->printLog("This version of WURFL has $arrLen UserAgent"); | ||
| 352 | $CommonLib->printLog("End loading WURFL.xml"); | ||
| 353 | if ($personalwurflurl ne 'unknown') { | ||
| 354 | $CommonLib->printLog("---------------------------------------------------------------------------"); | ||
| 355 | if (-e "$personalwurflurl") { | ||
| 356 | $CommonLib->printLog("Start loading $ENV{PersonalWurflFileName}"); | ||
| 357 | if (open (IN,"$personalwurflurl")) { | ||
| 358 | my $filesize= -s $personalwurflurl; | ||
| 359 | my $string_file; | ||
| 360 | read (IN,$string_file,$filesize); | ||
| 361 | close IN; | ||
| 362 | $string_file =~ s/\n//g; | ||
| 363 | $string_file =~ s/>/>\n/g; | ||
| 364 | my @arrayFile=split(/\n/, $string_file); | ||
| 365 | foreach my $line (@arrayFile) { | ||
| 366 | $r_id=parseWURFLFile($line,$r_id); | ||
| 367 | } | ||
| 368 | } else { | ||
| 369 | $CommonLib->printLog("Error open file:$personalwurflurl"); | ||
| 370 | ModPerl::Util::exit(); | ||
| 371 | } | ||
| 372 | $CommonLib->printLog("END loading $ENV{PersonalWurflFileName}"); | ||
| 373 | close IN; | ||
| 374 | |||
| 375 | } else { | ||
| 376 | $CommonLib->printLog("File $personalwurflurl not found"); | ||
| 377 | ModPerl::Util::exit(); | ||
| 378 | } | ||
| 379 | } | ||
| 380 | |||
| 381 | |||
| 382 | } | ||
| 383 | sub callparseWURFLFile { | ||
| 384 | my ($output) = @_; | ||
| 385 | my $r_id; | ||
| 386 | if (open (IN,"$output")) { | ||
| 387 | my $filesize= -s $output; | ||
| 388 | my $string_file; | ||
| 389 | read (IN,$string_file,$filesize); | ||
| 390 | close IN; | ||
| 391 | $string_file =~ s/\n//g; | ||
| 392 | $string_file =~ s/>/>\n/g; | ||
| 393 | my @arrayFile=split(/\n/, $string_file); | ||
| 394 | foreach my $line (@arrayFile) { | ||
| 395 | $r_id=parseWURFLFile($line,$r_id); | ||
| 396 | } | ||
| 397 | } else { | ||
| 398 | $CommonLib->printLog("Error open file:$output"); | ||
| 399 | ModPerl::Util::exit(); | ||
| 400 | } | ||
| 401 | } | ||
| 402 | sub parseWURFLFile { | ||
| 403 | my ($record,$val) = @_; | ||
| 404 | my $null=""; | ||
| 405 | my $null2=""; | ||
| 406 | my $null3=""; | ||
| 407 | my $uagent=""; | ||
| 408 | my $fb=""; | ||
| 409 | my $value=""; | ||
| 410 | my $id; | ||
| 411 | my $name=""; | ||
| 412 | my $version=""; | ||
| 413 | if ($val) { | ||
| 414 | $id="$val"; | ||
| 415 | } | ||
| 416 | if ($record =~ /\<device/o) { | ||
| 417 | if (index($record,'user_agent') > -1 ) { | ||
| 418 | $uagent=lc(substr($record,index($record,'user_agent') + 12,index($record,'"',index($record,'user_agent')+ 13)- index($record,'user_agent') - 12)); | ||
| 419 | |||
| 420 | if (index($uagent,'blackberry') >-1 ) { | ||
| 421 | $uagent=substr($uagent,index($uagent,'blackberry')); | ||
| 422 | } | ||
| 423 | ($uagent,$version)=$CommonLib->androidDetection($uagent); | ||
| 424 | } | ||
| 425 | if (index($record,'id') > -1 ) { | ||
| 426 | $id=substr($record,index($record,'id') + 4,index($record,'"',index($record,'id')+ 5)- index($record,'id') - 4); | ||
| 427 | } | ||
| 428 | if (index($record,'fall_back') > -1 ) { | ||
| 429 | $fb=substr($record,index($record,'fall_back') + 11,index($record,'"',index($record,'fall_back')+ 12)- index($record,'fall_back') - 11); | ||
| 430 | } | ||
| 431 | if (($fb) && ($id)) { | ||
| 432 | $Array_fb{"$id"}=$fb; | ||
| 433 | } | ||
| 434 | if (($uagent) && ($id)) { | ||
| 435 | my %ParseUA=$CommonLib->GetMultipleUa($uagent,$deepSearch); | ||
| 436 | my $pair; | ||
| 437 | my $arrUaLen = scalar %ParseUA; | ||
| 438 | my $contaUA=0; | ||
| 439 | my $Array_fullua_id=$uagent; | ||
| 440 | foreach $pair (reverse sort { $a <=> $b } keys %ParseUA) { | ||
| 441 | my $dummy=$ParseUA{$pair}; | ||
| 442 | if ($Array_id{$dummy}) {} else { | ||
| 443 | $Array_id{$dummy}=$id; | ||
| 444 | } | ||
| 445 | $contaUA=$contaUA-1; | ||
| 446 | } | ||
| 447 | } | ||
| 448 | |||
| 449 | } | ||
| 450 | if ($record =~ /\<capability/o) { | ||
| 451 | ($null,$name,$null2,$value,$null3,$fb)=split(/\"/, $record); | ||
| 452 | if ($listall eq "true") { | ||
| 453 | $Capability{$name}=$name; | ||
| 454 | } | ||
| 455 | if (($id) && ($Capability{$name}) && ($name) && ($value)) { | ||
| 456 | $Array_DDRcapability{"$val|$name"}=$value; | ||
| 457 | } | ||
| 458 | } | ||
| 459 | if ($record =~ /\/ver>/o) { | ||
| 460 | $WURFLVersion=substr($record,index($record,'<ver>') + 1,index($record,'</ver>') - 9); | ||
| 461 | } | ||
| 462 | return $id; | ||
| 463 | |||
| 464 | } | ||
| 465 | |||
| 466 | sub FallBack { | ||
| 467 | my ($idToFind) = @_; | ||
| 468 | my $dummy_id; | ||
| 469 | my $dummy; | ||
| 470 | my $dummy2; | ||
| 471 | my $LOOP; | ||
| 472 | my %ArrayCapFoundToPass; | ||
| 473 | my $capability; | ||
| 474 | foreach $capability (sort keys %Capability) { | ||
| 475 | $dummy_id=$idToFind; | ||
| 476 | $LOOP=0; | ||
| 477 | while ($LOOP<2) { | ||
| 478 | $dummy="$dummy_id|$capability"; | ||
| 479 | if ($Array_DDRcapability{$dummy}) { | ||
| 480 | $LOOP=2; | ||
| 481 | $dummy2="$dummy_id|$capability"; | ||
| 482 | $ArrayCapFoundToPass{$capability}=$Array_DDRcapability{$dummy2}; | ||
| 483 | } else { | ||
| 484 | if ($Array_fb{$dummy_id}) { | ||
| 485 | $dummy_id=$Array_fb{$dummy_id}; | ||
| 486 | } else { | ||
| 487 | $dummy_id="root"; | ||
| 488 | } | ||
| 489 | if ($dummy_id eq "root" || $dummy_id eq "generic") { | ||
| 490 | $LOOP++; | ||
| 491 | } | ||
| 492 | } | ||
| 493 | } | ||
| 494 | |||
| 495 | } | ||
| 496 | return %ArrayCapFoundToPass; | ||
| 497 | } | ||
| 498 | sub IdentifyUAMethod { | ||
| 499 | my ($UserAgent) = @_; | ||
| 500 | my $ind=0; | ||
| 501 | my %ArrayPM; | ||
| 502 | my $pair; | ||
| 503 | my $pair2; | ||
| 504 | my $id_find=""; | ||
| 505 | my $dummy; | ||
| 506 | my $uagent_toMatch; | ||
| 507 | my $near_toFind=100; | ||
| 508 | my $near_toMatch; | ||
| 509 | my %ArrayUAType=$CommonLib->GetMultipleUa(lc($UserAgent),$deepSearch); | ||
| 510 | foreach $pair (reverse sort { $a <=> $b } keys %ArrayUAType) | ||
| 511 | { | ||
| 512 | my $dummy=$ArrayUAType{$pair}; | ||
| 513 | if ($Array_id{$dummy}) { | ||
| 514 | if (!$id_find) { | ||
| 515 | $id_find=$Array_id{$dummy}; | ||
| 516 | } | ||
| 517 | } | ||
| 518 | } | ||
| 519 | return $id_find; | ||
| 520 | } | ||
| 521 | sub IdentifyPCUAMethod { | ||
| 522 | my ($UserAgent) = @_; | ||
| 523 | my $ind=0; | ||
| 524 | my $id_find=""; | ||
| 525 | my $pair; | ||
| 526 | my $length=0; | ||
| 527 | foreach $pair (sort keys %PCArray) { | ||
| 528 | if ($UserAgent =~ m/$pair/) { | ||
| 529 | $id_find=$PCArray{$pair}; | ||
| 530 | } | ||
| 531 | } | ||
| 532 | if ($id_find eq "") { | ||
| 533 | if ($UserAgent =~ m/safari/ && !($UserAgent =~ m/kftt/)) { | ||
| 534 | $id_find='safari'; | ||
| 535 | } | ||
| 536 | } | ||
| 537 | return $id_find; | ||
| 538 | } | ||
| 539 | |||
| 540 | |||
| 541 | sub handler { | ||
| 542 | my $f = shift; | ||
| 543 | my $capability2; | ||
| 544 | my $variabile=""; | ||
| 545 | my $user_agent=lc($f->headers_in->{'User-Agent'}|| ''); | ||
| 546 | my $x_user_agent=$f->headers_in->{'X-Device-User-Agent'}|| ''; | ||
| 547 | my $x_operamini_phone_ua=$f->headers_in->{'X-OperaMini-Phone-Ua'}|| ''; | ||
| 548 | my $x_operamini_ua=$f->headers_in->{'X-OperaMini-Ua'}|| ''; | ||
| 549 | my $query_string=$f->args; | ||
| 550 | my $docroot = $f->document_root(); | ||
| 551 | my $id=""; | ||
| 552 | my $location="none"; | ||
| 553 | my $width_toSearch; | ||
| 554 | my $type_redirect="internal"; | ||
| 555 | my $return_value; | ||
| 556 | my $dummy=""; | ||
| 557 | my $variabile2=""; | ||
| 558 | my %ArrayCapFound; | ||
| 559 | my $controlCookie; | ||
| 560 | my $query_img=""; | ||
| 561 | $ArrayCapFound{is_transcoder}='false'; | ||
| 562 | $ArrayCapFound{'device_claims_web_support'}='none'; | ||
| 563 | $ArrayCapFound{'is_wireless_device'}='none'; | ||
| 564 | |||
| 565 | my %ArrayQuery; | ||
| 566 | my $var; | ||
| 567 | my $mobile=0; | ||
| 568 | my $version=""; | ||
| 569 | if ($user_agent eq "") { | ||
| 570 | $user_agent="no useragent found"; | ||
| 571 | } | ||
| 572 | if ($x_user_agent) { | ||
| 573 | $user_agent=lc($x_user_agent); | ||
| 574 | } | ||
| 575 | if ($x_operamini_phone_ua) { | ||
| 576 | $user_agent=lc($x_operamini_phone_ua); | ||
| 577 | } | ||
| 578 | my $cookie = $f->headers_in->{Cookie} || ''; | ||
| 579 | $id=$CommonLib->readCookie($cookie); | ||
| 580 | my $amfFull=$CommonLib->readCookie_fullB($cookie); | ||
| 581 | if ($query_string) { | ||
| 582 | my @vars = split(/&/, $query_string); | ||
| 583 | foreach $var (sort @vars){ | ||
| 584 | if ($var) { | ||
| 585 | my ($v,$i) = split(/=/, $var); | ||
| 586 | $v =~ tr/+/ /; | ||
| 587 | $v =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; | ||
| 588 | if ($i) { | ||
| 589 | $i =~ tr/+/ /; | ||
| 590 | $i =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; | ||
| 591 | $i =~ s/<!--(.|\n)*-->//g; | ||
| 592 | $ArrayQuery{$v}=$i; | ||
| 593 | } | ||
| 594 | } | ||
| 595 | } | ||
| 596 | if (($ArrayQuery{amf}) && $restmode eq 'true') { | ||
| 597 | $user_agent=lc($ArrayQuery{amf}); | ||
| 598 | } | ||
| 599 | if ($ArrayQuery{$mobilenable}) { | ||
| 600 | $f->err_headers_out->set('Set-Cookie' => "amfFull=false; path=/;"); | ||
| 601 | $amfFull='ok'; | ||
| 602 | } | ||
| 603 | |||
| 604 | } | ||
| 605 | |||
| 606 | $user_agent=lc($user_agent); | ||
| 607 | if ($user_agent =~ m/blackberry/i) { | ||
| 608 | $user_agent=substr($user_agent,index($user_agent,'blackberry')); | ||
| 609 | $mobile=1; | ||
| 610 | } | ||
| 611 | if ($user_agent =~ m/up.link/i ) { | ||
| 612 | $user_agent=substr($user_agent,0,index($user_agent,'up.link') - 1); | ||
| 613 | $mobile=1; | ||
| 614 | } | ||
| 615 | ($user_agent,$version)=$CommonLib->androidDetection($user_agent); | ||
| 616 | if ($cacheSystem->restore( 'wurfl-ua', $user_agent )) { | ||
| 617 | # | ||
| 618 | # cookie is not empty so I try to read in memory cache on my httpd cache | ||
| 619 | # | ||
| 620 | $id=$cacheSystem->restore( 'wurfl-ua', $user_agent ); | ||
| 621 | if ($cacheSystem->restore( 'wurfl-id', $id )) { | ||
| 622 | # | ||
| 623 | # I'm here only for old device | ||
| 624 | # | ||
| 625 | my @pairs = split(/&/, $cacheSystem->restore( 'wurfl-id', $id )); | ||
| 626 | my $param_tofound; | ||
| 627 | my $string_tofound; | ||
| 628 | foreach $param_tofound (@pairs) { | ||
| 629 | ($string_tofound,$dummy)=split(/=/, $param_tofound); | ||
| 630 | $ArrayCapFound{$string_tofound}=$dummy; | ||
| 631 | my $upper2=uc($string_tofound); | ||
| 632 | $f->subprocess_env("AMF_$upper2" => $ArrayCapFound{$string_tofound}); | ||
| 633 | $f->pnotes($string_tofound => $ArrayCapFound{$string_tofound}); | ||
| 634 | } | ||
| 635 | $id=$ArrayCapFound{id}; | ||
| 636 | } | ||
| 637 | } else { | ||
| 638 | if ($id eq "") { | ||
| 639 | if ($user_agent) { | ||
| 640 | my $pair; | ||
| 641 | my $lcuser_agent=lc($user_agent); | ||
| 642 | if ($mobile==0) { | ||
| 643 | foreach $pair (%MobileArray) { | ||
| 644 | if ($user_agent =~ m/$pair/i) { | ||
| 645 | $mobile=1; | ||
| 646 | } | ||
| 647 | } | ||
| 648 | if ($mobile==0) { | ||
| 649 | $user_agent=$CommonLib->botDetection($user_agent); | ||
| 650 | $id=IdentifyPCUAMethod($user_agent); | ||
| 651 | } | ||
| 652 | } | ||
| 653 | if (!$id) {$id="";}; | ||
| 654 | if ($id eq "") { | ||
| 655 | $id=IdentifyUAMethod($user_agent); | ||
| 656 | } | ||
| 657 | if ($id eq "") { | ||
| 658 | $id='generic_web_browser'; | ||
| 659 | } else { | ||
| 660 | #this check the correct version of Android | ||
| 661 | if ($version) { | ||
| 662 | if ($version ne 'nc') { | ||
| 663 | my $lengthId=length($version); | ||
| 664 | my $count=0; | ||
| 665 | while($count<$lengthId) { | ||
| 666 | my $idToCheck=$id."_sub".substr($version,0,length($version)-$count); | ||
| 667 | if ($Array_fb{$idToCheck}) { | ||
| 668 | $id=$idToCheck; | ||
| 669 | $count=$lengthId; | ||
| 670 | } | ||
| 671 | $count++; | ||
| 672 | } | ||
| 673 | } | ||
| 674 | } | ||
| 675 | } | ||
| 676 | $cacheSystem->store( 'wurfl-ua', $user_agent, $id); | ||
| 677 | } | ||
| 678 | } | ||
| 679 | if ($id ne "") { | ||
| 680 | # | ||
| 681 | # device detected | ||
| 682 | # | ||
| 683 | if ($cacheSystem->restore( 'wurfl-id', $id )) { | ||
| 684 | # | ||
| 685 | # I'm here only for old device looking in cache | ||
| 686 | # | ||
| 687 | my @pairs = split(/&/, $cacheSystem->restore( 'wurfl-id', $id )); | ||
| 688 | my $param_tofound; | ||
| 689 | my $string_tofound; | ||
| 690 | foreach $param_tofound (@pairs) { | ||
| 691 | ($string_tofound,$dummy)=split(/=/, $param_tofound); | ||
| 692 | $ArrayCapFound{$string_tofound}=$dummy; | ||
| 693 | my $upper2=uc($string_tofound); | ||
| 694 | $f->subprocess_env("AMF_$upper2" => $ArrayCapFound{$string_tofound}); | ||
| 695 | $f->pnotes("$string_tofound" => $ArrayCapFound{$string_tofound}); | ||
| 696 | } | ||
| 697 | $id=$ArrayCapFound{id}; | ||
| 698 | } else { | ||
| 699 | %ArrayCapFound=FallBack($id); | ||
| 700 | foreach $capability2 (sort keys %ArrayCapFound) { | ||
| 701 | $variabile2="$variabile2$capability2=$ArrayCapFound{$capability2}&"; | ||
| 702 | my $upper=uc($capability2); | ||
| 703 | $f->subprocess_env("AMF_$upper" => $ArrayCapFound{$capability2}); | ||
| 704 | $f->pnotes("$capability2" => $ArrayCapFound{$capability2}); | ||
| 705 | } | ||
| 706 | $variabile2="id=$id&$variabile2"; | ||
| 707 | $f->subprocess_env("AMF_ID" => $id); | ||
| 708 | $f->pnotes('id' => $id); | ||
| 709 | $cacheSystem->store( 'wurfl-id', $id, $variabile2 ); | ||
| 710 | $cacheSystem->store( 'wurfl-ua', $user_agent, $id); | ||
| 711 | } | ||
| 712 | if ($cookiecachesystem eq "true") { | ||
| 713 | $f->err_headers_out->set('Set-Cookie' => "amfID=$id; path=/;"); | ||
| 714 | } | ||
| 715 | } | ||
| 716 | } | ||
| 717 | my $amf_device_ismobile = 'true'; | ||
| 718 | if (($ArrayCapFound{'device_claims_web_support'}) && ($ArrayCapFound{'device_claims_web_support'})) { | ||
| 719 | if ($ArrayCapFound{'device_claims_web_support'} eq 'true' && $ArrayCapFound{'is_wireless_device'} eq 'false') { | ||
| 720 | $amf_device_ismobile = 'false'; | ||
| 721 | } | ||
| 722 | } | ||
| 723 | if ($amfFull ne "") { | ||
| 724 | $f->subprocess_env("AMF_FORCE_TO_DESKTOP" => 'true'); | ||
| 725 | $f->pnotes("amf_force_to_desktop" => 'true'); | ||
| 726 | } | ||
| 727 | if ($ArrayCapFound{'is_tablet'}) { | ||
| 728 | $f->subprocess_env("AMF_DEVICE_IS_TABLET" => lc($ArrayCapFound{'is_tablet'})); | ||
| 729 | } | ||
| 730 | $f->pnotes("amf_device_ismobile" => $amf_device_ismobile); | ||
| 731 | $f->subprocess_env("AMF_DEVICE_IS_MOBILE" => $amf_device_ismobile); | ||
| 732 | $f->subprocess_env("AMF_VER" => $VERSION); | ||
| 733 | $f->subprocess_env("AMF_WURFLVER" => $WURFLVersion); | ||
| 734 | $f->headers_out->set("AMF-Ver"=> $VERSION); | ||
| 735 | if ($x_operamini_ua) { | ||
| 736 | $f->subprocess_env("AMF_MOBILE_BROWSER" => $x_operamini_ua); | ||
| 737 | $f->pnotes("mobile_browser" => $x_operamini_ua); | ||
| 738 | $f->subprocess_env("AMF_IS_TRANCODER" => 'true'); | ||
| 739 | $f->pnotes("is_transcoder" => 'true'); | ||
| 740 | } | ||
| 741 | return Apache2::Const::DECLINED; | ||
| 742 | } | ||
| 743 | 1; | ||
| 744 | |||
| 745 | |||
| 746 | =head1 NAME | ||
| 747 | |||
| 748 | Apache2::AMFWURFLFilter - The module detects the mobile device and passes the WURFL capabilities on to the other web application as environment variables | ||
| 749 | |||
| 750 | =head1 DESCRIPTION | ||
| 751 | |||
| 752 | Module for device detection, the cache is based on file system | ||
| 753 | |||
| 754 | =head1 AMF PROJECT SITE | ||
| 755 | |||
| 756 | http://www.apachemobilefilter.org | ||
| 757 | |||
| 758 | =head1 DOCUMENTATION | ||
| 759 | |||
| 760 | http://wiki.apachemobilefilter.org | ||
| 761 | |||
| 762 | Perl Module Documentation: http://wiki.apachemobilefilter.org/index.php/AMFWURFLFilter | ||
| 763 | |||
| 764 | =head1 AUTHOR | ||
| 765 | |||
| 766 | Idel Fuschini (idel.fuschini [at] gmail [dot] com) | ||
| 767 | |||
| 768 | =head1 COPYRIGHT | ||
| 769 | |||
| 770 | You may distribute under the terms of either the GNU General Public | ||
| 771 | License or the Artistic License, as specified in the Perl README file. | ||
| 772 | |||
| 773 | =cut | ||
| 774 |