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