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