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